-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go.bak
136 lines (112 loc) · 3.1 KB
/
main.go.bak
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"fabric-sdk-go/sdkInit"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
shell "github.com/ipfs/go-ipfs-api"
)
const (
cc_name = "simplecc"
cc_version = "1.0.0"
)
var App sdkInit.Application
func main() {
// init orgs information
orgs := []*sdkInit.OrgInfo{
{
OrgAdminUser: "Admin",
OrgName: "Org1",
OrgMspId: "Org1MSP",
OrgUser: "User1",
OrgPeerNum: 1,
OrgAnchorFile: os.Getenv("GOPATH") + "/src/fabric-sdk-go/fixtures/channel-artifacts/Org1MSPanchors.tx",
},
{
OrgAdminUser: "Admin",
OrgName: "Org2",
OrgMspId: "Org2MSP",
OrgUser: "User2",
OrgPeerNum: 1,
OrgAnchorFile: os.Getenv("GOPATH") + "/src/fabric-sdk-go/fixtures/channel-artifacts/Org2MSPanchors.tx",
},
}
// init sdk env info
info := sdkInit.SdkEnvInfo{
ChannelID: "mychannel",
ChannelConfig: os.Getenv("GOPATH") + "/src/fabric-sdk-go/fixtures/channel-artifacts/channel.tx",
Orgs: orgs,
OrdererAdminUser: "Admin",
OrdererOrgName: "OrdererOrg",
OrdererEndpoint: "orderer.example.com",
ChaincodeID: cc_name,
ChaincodePath: os.Getenv("GOPATH") + "/src/fabric-sdk-go/chaincode/",
ChaincodeVersion: cc_version,
}
// sdk setup
sdk, err := sdkInit.Setup("config.yaml", &info)
if err != nil {
fmt.Println(">> SDK setup error:", err)
os.Exit(-1)
}
// create channel and join
if err := sdkInit.CreateAndJoinChannel(&info); err != nil {
fmt.Println(">> Create channel and join error:", err)
os.Exit(-1)
}
// create chaincode lifecycle
if err := sdkInit.CreateCCLifecycle(&info, 1, false, sdk); err != nil {
fmt.Println(">> create chaincode lifecycle error: %v", err)
os.Exit(-1)
}
// invoke chaincode set status
fmt.Println(">> 通过链码外部服务设置链码状态......")
if err := info.InitService(info.ChaincodeID, info.ChannelID, info.Orgs[0], sdk); err != nil {
fmt.Println("InitService successful")
os.Exit(-1)
}
App = sdkInit.Application{
SdkEnvInfo: &info,
}
fmt.Println(">> 设置链码状态完成")
defer info.EvClient.Unregister(sdkInit.BlockListener(info.EvClient))
defer info.EvClient.Unregister(sdkInit.ChainCodeEventListener(info.EvClient, info.ChaincodeID))
a := []string{"set", "ID1", "123"}
ret, err := App.Set(a)
if err != nil {
fmt.Println(err)
}
fmt.Println("<--- 添加信息 --->:", ret)
a = []string{"set", "ID2", "456"}
ret, err = App.Set(a)
if err != nil {
fmt.Println(err)
}
fmt.Println("<--- 添加信息 --->:", ret)
sh := shell.NewShell("localhost:5001")
cid, err := sh.Add(strings.NewReader("test"))
fmt.Println("<--- 哈希编号 --->:", cid)
a = []string{"set", "ID3", cid}
ret, err = App.Set(a)
if err != nil {
fmt.Println(err)
}
fmt.Println("<--- 添加信息 --->:", ret)
a = []string{"get", "ID3"}
response, err := App.Get(a)
if err != nil {
fmt.Println(err)
}
content, err := sh.Cat(response)
if err != nil {
fmt.Println(err)
}
data, err := ioutil.ReadAll(content)
if err != nil {
fmt.Println(err)
}
fmt.Println("<--- 查询信息 --->:", string(data))
time.Sleep(time.Second * 10)
}