-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (75 loc) · 3.18 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
const (
org3MspID = "Org3MSP"
org3CryptoPath = "/Users/ahsansyed/Desktop/workspace/veritas/fabric-veritas-samples/test-network/organizations/peerOrganizations/org3.example.com"
org3CertPath = org3CryptoPath + "/users/User1@org3.example.com/msp/signcerts/User1@org3.example.com-cert.pem"
org3KeyPath = org3CryptoPath + "/users/User1@org3.example.com/msp/keystore"
org3TlsCertPath = org3CryptoPath + "/peers/peer0.org3.example.com/tls/ca.crt"
org3PeerEndpoint = "localhost:11051"
org3GatewayPeer = "peer0.org3.example.com"
org3ChannelName = "l2"
)
const (
org1MspID = "Org1MSP"
org1CryptoPath = "/Users/ahsansyed/Desktop/workspace/veritas/fabric-veritas-samples/test-network/organizations/peerOrganizations/org1.example.com"
org1CertPath = org1CryptoPath + "/users/User1@org1.example.com/msp/signcerts/User1@org1.example.com-cert.pem"
org1KeyPath = org1CryptoPath + "/users/User1@org1.example.com/msp/keystore"
org1TlsCertPath = org1CryptoPath + "/peers/peer0.org1.example.com/tls/ca.crt"
org1PeerEndpoint = "localhost:7051"
org1GatewayPeer = "peer0.org1.example.com"
org1ChannelName = "l1"
)
/*
step 1: receive txn
step 2: forward txn to HLF L2 channel
step 3: receive response from L2, forward response to client, add txn to batch
step 4: repeat until batch is complete or timeout
step 5: publish batch to L1 channel,
*/
type TransactionInfo struct {
ChaincodeName string `form:"chaincodeName" json:"chaincodeName" xml:"chaincodeName" binding:"required"`
TransactionName string `form:"transactionName" json:"transactionName" xml:"transactionName" binding:"required"`
Args []string `form:"args" json:"args" xml:"args" binding:"required"`
}
func main() {
layer2Connection := NewLayer2ConnectionManager()
layer1Connection := NewLayer1ConnectionManager()
defer layer2Connection.clientConnection.Close()
defer layer1Connection.clientConnection.Close()
defer layer2Connection.gateway.Close()
defer layer1Connection.gateway.Close()
transactionBuffer := make(chan TransactionInfo)
router := gin.Default()
router.POST("/executeTransaction", func(c *gin.Context) {
var transactionInfo TransactionInfo
if c.BindJSON(&transactionInfo) == nil {
transactionBuffer <- transactionInfo
c.JSON(http.StatusOK, gin.H{"response": "transaction submitted"})
}
})
//FOR DEBUGGING PURPOSES ONLY
router.POST("/executeL1Transaction", func(c *gin.Context) {
var transactionInfo TransactionInfo
if c.BindJSON(&transactionInfo) == nil {
contract := layer1Connection.network.GetContract(transactionInfo.ChaincodeName)
startTime := time.Now()
result, err := contract.SubmitTransaction(transactionInfo.TransactionName, transactionInfo.Args...)
if err != nil {
fmt.Printf("txn failed to execute: %s\n", err.Error())
} else {
finishTime := time.Since(startTime)
fmt.Printf("L1 Txn executed successfully. Took %dms. Result: %s\n", finishTime.Milliseconds(), result)
}
c.JSON(http.StatusOK, gin.H{"response": "transaction executed"})
}
})
batcher := NewBatcher(layer1Connection, layer2Connection)
go batcher.Run(transactionBuffer)
router.Run()
}