forked from hyperledger-archives/fabric-chaincode-evm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e_test.go
214 lines (188 loc) · 7.68 KB
/
e2e_test.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
Copyright IBM Corp All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package e2e
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"syscall"
"time"
docker "github.com/fsouza/go-dockerclient"
"github.com/hyperledger/fabric-chaincode-evm/integration/helpers"
"github.com/hyperledger/fabric/integration/nwo"
"github.com/hyperledger/fabric/integration/nwo/commands"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"github.com/tedsuo/ifrit"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
const LongEventualTimeout = time.Minute
var _ = Describe("EndToEnd", func() {
var (
testDir string
client *docker.Client
network *nwo.Network
chaincode nwo.Chaincode
process ifrit.Process
zeroAddress = "0000000000000000000000000000000000000000"
SimpleStorage = helpers.SimpleStorageContract()
InvokeContract = helpers.InvokeContract()
)
BeforeEach(func() {
var err error
testDir, err = ioutil.TempDir("", "e2e")
Expect(err).NotTo(HaveOccurred())
client, err = docker.NewClientFromEnv()
Expect(err).NotTo(HaveOccurred())
chaincode = nwo.Chaincode{
Name: "evmcc",
Version: "0.0",
Path: "github.com/hyperledger/fabric-chaincode-evm/evmcc",
Ctor: `{"Args":[]}`,
Policy: `AND ('Org1MSP.member')`,
}
network = nwo.New(helpers.SimpleSoloNetwork(), testDir, client, 30000, components)
network.GenerateConfigTree()
network.Bootstrap()
networkRunner := network.NetworkGroupRunner()
process = ifrit.Invoke(networkRunner)
Eventually(process.Ready()).Should(BeClosed())
})
AfterEach(func() {
if process != nil {
process.Signal(syscall.SIGTERM)
Eventually(process.Wait(), LongEventualTimeout).Should(Receive())
}
if network != nil {
network.Cleanup()
}
os.RemoveAll(testDir)
})
It("is able to run evm bytecode contracts", func() {
By("getting the orderer by name")
orderer := network.Orderer("orderer")
By("setting up the channel")
network.CreateAndJoinChannel(orderer, "testchannel")
By("deploying the chaincode")
nwo.DeployChaincode(network, "testchannel", orderer, chaincode)
By("getting the client peer by name")
peer := network.Peer("Org1", "peer0")
By("installing a Simple Storage SmartContract")
sess, err := network.PeerUserSession(peer, "User1", commands.ChaincodeInvoke{
ChannelID: "testchannel",
Orderer: network.OrdererAddress(orderer, nwo.ListenPort),
Name: "evmcc",
Ctor: fmt.Sprintf(`{"Args":["%s","%s"]}`, zeroAddress, SimpleStorage.CompiledBytecode),
PeerAddresses: []string{
network.PeerAddress(network.Peer("Org1", "peer0"), nwo.ListenPort),
},
WaitForEvent: true,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, LongEventualTimeout).Should(gexec.Exit(0))
Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful. result: status:200"))
output := sess.Err.Contents()
contractAddr := string(regexp.MustCompile(`Chaincode invoke successful. result: status:200 payload:"([0-9a-fA-F]{40})"`).FindSubmatch(output)[1])
Expect(contractAddr).ToNot(BeEmpty())
By("invoking the smart contract")
sess, err = network.PeerUserSession(peer, "User1", commands.ChaincodeInvoke{
ChannelID: "testchannel",
Orderer: network.OrdererAddress(orderer, nwo.ListenPort),
Name: "evmcc",
//set(3)
Ctor: fmt.Sprintf(`{"Args":["%s","%s0000000000000000000000000000000000000000000000000000000000000003"]}`, contractAddr, SimpleStorage.FunctionHashes["set"]),
PeerAddresses: []string{
network.PeerAddress(network.Peer("Org1", "peer0"), nwo.ListenPort),
},
WaitForEvent: true,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, LongEventualTimeout).Should(gexec.Exit(0))
Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful. result: status:200"))
By("verifying SimpleStorage runtime bytecode")
sess, err = network.PeerUserSession(peer, "User1", commands.ChaincodeQuery{
ChannelID: "testchannel",
Name: "evmcc",
//get()
Ctor: fmt.Sprintf(`{"Args":["getCode","%s"]}`, contractAddr),
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, LongEventualTimeout).Should(gexec.Exit(0))
output, _ = sess.Command.CombinedOutput()
fmt.Println(string(output))
Expect(sess.Out).To(gbytes.Say(SimpleStorage.RuntimeBytecode))
By("querying the smart contract")
sess, err = network.PeerUserSession(peer, "User1", helpers.ChaincodeQueryWithHex{
ChannelID: "testchannel",
Name: "evmcc",
//get()
Ctor: fmt.Sprintf(`{"Args":["%s","%s"]}`, contractAddr, SimpleStorage.FunctionHashes["get"]),
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, LongEventualTimeout).Should(gexec.Exit(0))
output, _ = sess.Command.CombinedOutput()
fmt.Println(string(output))
Expect(sess.Out).To(gbytes.Say("0000000000000000000000000000000000000000000000000000000000000003"))
By("deploying an InvokeContract to invoke SimpleStorage")
sess, err = network.PeerUserSession(peer, "User1", commands.ChaincodeInvoke{
ChannelID: "testchannel",
Orderer: network.OrdererAddress(orderer, nwo.ListenPort),
Name: "evmcc",
Ctor: fmt.Sprintf(`{"Args":["%s","%s"]}`, zeroAddress, InvokeContract.CompiledBytecode+"000000000000000000000000"+contractAddr),
PeerAddresses: []string{
network.PeerAddress(network.Peer("Org1", "peer0"), nwo.ListenPort),
},
WaitForEvent: true,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, LongEventualTimeout).Should(gexec.Exit(0))
Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful. result: status:200"))
output = sess.Err.Contents()
invokeAddr := string(regexp.MustCompile(`Chaincode invoke successful. result: status:200 payload:"([0-9a-fA-F]{40})"`).FindSubmatch(output)[1])
Expect(invokeAddr).ToNot(BeEmpty())
By("invoking SimpleStorage through the InvokeContract")
sess, err = network.PeerUserSession(peer, "User1", commands.ChaincodeInvoke{
ChannelID: "testchannel",
Orderer: network.OrdererAddress(orderer, nwo.ListenPort),
Name: "evmcc",
//InvokeContract.setVal(8) which will cause SimpleStorage.set(8) to be invoked.
Ctor: fmt.Sprintf(`{"Args":["%s","%s0000000000000000000000000000000000000000000000000000000000000008"]}`, invokeAddr, InvokeContract.FunctionHashes["setVal"]),
PeerAddresses: []string{
network.PeerAddress(network.Peer("Org1", "peer0"), nwo.ListenPort),
},
WaitForEvent: true,
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, LongEventualTimeout).Should(gexec.Exit(0))
Expect(sess.Err).To(gbytes.Say("Chaincode invoke successful. result: status:200"))
By("querying the SimpleStorage smart contract")
sess, err = network.PeerUserSession(peer, "User1", helpers.ChaincodeQueryWithHex{
ChannelID: "testchannel",
Name: "evmcc",
//get()
Ctor: fmt.Sprintf(`{"Args":["%s","%s"]}`, contractAddr, SimpleStorage.FunctionHashes["get"]),
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, LongEventualTimeout).Should(gexec.Exit(0))
output, _ = sess.Command.CombinedOutput()
fmt.Println(string(output))
Expect(sess.Out).To(gbytes.Say("0000000000000000000000000000000000000000000000000000000000000008"))
// The following query tests the opcode STATICCALL
By("querying the SimpleStorage Contract through the InvokeContract")
sess, err = network.PeerUserSession(peer, "User1", helpers.ChaincodeQueryWithHex{
ChannelID: "testchannel",
Name: "evmcc",
//get()
Ctor: fmt.Sprintf(`{"Args":["%s","%s"]}`, invokeAddr, InvokeContract.FunctionHashes["getVal"]),
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, LongEventualTimeout).Should(gexec.Exit(0))
output, _ = sess.Command.CombinedOutput()
fmt.Println(string(output))
Expect(sess.Out).To(gbytes.Say("0000000000000000000000000000000000000000000000000000000000000008"))
})
})