This repository has been archived by the owner on Dec 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
/
fab3_test.go
398 lines (332 loc) · 14.5 KB
/
fab3_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
Copyright IBM Corp All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package fab3
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
"strings"
"syscall"
"github.com/hyperledger/fabric-chaincode-evm/fab3/types"
"github.com/hyperledger/fabric-chaincode-evm/integration/helpers"
. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/config"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/tedsuo/ifrit"
"github.com/tedsuo/ifrit/ginkgomon"
)
func sendRPCRequest(client *http.Client, method, proxyAddress string, id int, params interface{}) []byte {
request := helpers.JsonRPCRequest{
JsonRPC: "2.0",
Method: method,
ID: id,
Params: params,
}
reqBody, err := json.Marshal(request)
Expect(err).ToNot(HaveOccurred())
body := strings.NewReader(string(reqBody))
fmt.Fprintln(GinkgoWriter, string(reqBody))
req, err := http.NewRequest("POST", proxyAddress, body)
Expect(err).ToNot(HaveOccurred())
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
Expect(err).ToNot(HaveOccurred())
responseBody, err := ioutil.ReadAll(resp.Body)
fmt.Fprintln(GinkgoWriter, string(responseBody))
Expect(err).ToNot(HaveOccurred())
return responseBody
}
var _ = Describe("Fab3", func() {
var (
proxy ifrit.Process
proxyRunner *ginkgomon.Runner
proxyAddress string
client *http.Client
SimpleStorage helpers.Contract
)
BeforeEach(func() {
SimpleStorage = helpers.SimpleStorageContract()
client = &http.Client{}
//Start up Proxy
proxyPort := uint16(6000 + config.GinkgoConfig.ParallelNode)
proxyRunner = helpers.Fab3Runner(components.Paths["fab3"], components.Paths["Fab3Config"], "Org1", "User1", channelName, ccid, proxyPort)
proxy = ifrit.Invoke(proxyRunner)
Eventually(proxy.Ready(), LongEventualTimeout, LongPollingInterval).Should(BeClosed())
proxyAddress = fmt.Sprintf("http://127.0.0.1:%d", proxyPort)
helpers.WaitForFab3(proxyPort)
})
AfterEach(func() {
if proxy != nil {
proxy.Signal(syscall.SIGTERM)
Eventually(proxy.Wait(), LongEventualTimeout, LongPollingInterval).Should(Receive())
}
})
It("implements the ethereum json rpc api", func() {
var err error
var respBody helpers.JsonRPCResponse
var respBlockBody helpers.JsonRPCBlockResponse
var respArrayBody helpers.JsonRPCArrayResponse
By("querying for an account")
rBody := sendRPCRequest(client, "eth_accounts", proxyAddress, 5, []interface{}{})
err = json.Unmarshal(rBody, &respArrayBody)
Expect(err).ToNot(HaveOccurred())
Expect(respArrayBody.Error).To(BeZero())
Expect(respArrayBody.Result).To(HaveLen(1))
account := respArrayBody.Result[0]
checkHexEncoded(account)
expectedArrayBody := helpers.JsonRPCArrayResponse{JsonRPC: "2.0", ID: 5}
// Set the same result so that next expectation can check all other fields
expectedArrayBody.Result = respArrayBody.Result
Expect(respArrayBody).To(Equal(expectedArrayBody))
By("Deploying the Simple Storage Contract")
params := helpers.MessageParams{
To: "0000000000000000000000000000000000000000",
Data: SimpleStorage.CompiledBytecode,
}
rBody = sendRPCRequest(client, "eth_sendTransaction", proxyAddress, 6, params)
err = json.Unmarshal(rBody, &respBody)
Expect(err).ToNot(HaveOccurred())
Expect(respBody.Error).To(BeZero())
expectedBody := helpers.JsonRPCResponse{JsonRPC: "2.0", ID: 6}
// Set the same result so that next expectation can check all other fields
expectedBody.Result = respBody.Result
Expect(respBody).To(Equal(expectedBody))
By("Getting the Transaction Receipt")
txHash := respBody.Result
var rpcResp helpers.JsonRPCTxReceipt
// It takes a couple seconds for the transaction to be found
Eventually(func() helpers.JsonRPCError {
rBody = sendRPCRequest(client, "eth_getTransactionReceipt", proxyAddress, 16, []string{txHash})
rpcResp = helpers.JsonRPCTxReceipt{}
err = json.Unmarshal(rBody, &rpcResp)
Expect(err).ToNot(HaveOccurred())
return rpcResp.Error
}, LongEventualTimeout, LongPollingInterval).Should(BeZero())
receipt := rpcResp.Result
Expect(receipt.Logs).ToNot(BeNil())
checkHexEncoded(receipt.ContractAddress)
Expect(receipt.TransactionHash).To(Equal("0x" + txHash))
checkHexEncoded(receipt.BlockNumber)
checkHexEncoded(receipt.BlockHash)
checkHexEncoded(receipt.TransactionIndex)
Expect(receipt.From).To(Equal(account))
By("verifying the code")
contractAddr := receipt.ContractAddress
rBody = sendRPCRequest(client, "eth_getCode", proxyAddress, 17, []string{contractAddr})
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(rBody, &respBody)
Expect(err).ToNot(HaveOccurred())
Expect(rpcResp.Error).To(BeZero())
Expect(respBody.Result).To(Equal(SimpleStorage.RuntimeBytecode))
By("interacting with the contract")
val := "000000000000000000000000000000000000000000000000000000000000002a"
params = helpers.MessageParams{
To: contractAddr,
Data: SimpleStorage.FunctionHashes["set"] + val,
}
rBody = sendRPCRequest(client, "eth_sendTransaction", proxyAddress, 18, params)
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(rBody, &respBody)
Expect(err).ToNot(HaveOccurred())
Expect(respBody.Error).To(BeZero())
txHash = respBody.Result
By("verifying it returned a valid transaction hash")
Eventually(func() helpers.JsonRPCError {
rBody = sendRPCRequest(client, "eth_getTransactionReceipt", proxyAddress, 16, []string{txHash})
rpcResp = helpers.JsonRPCTxReceipt{}
err = json.Unmarshal(rBody, &rpcResp)
Expect(err).ToNot(HaveOccurred())
return rpcResp.Error
}, LongEventualTimeout, LongPollingInterval).Should(BeZero())
receipt = rpcResp.Result
Expect(receipt.TransactionHash).To(Equal("0x" + txHash))
checkHexEncoded(receipt.BlockNumber)
checkHexEncoded(receipt.BlockHash)
checkHexEncoded(receipt.TransactionIndex)
Expect(receipt.ContractAddress).To(BeEmpty())
Expect(receipt.From).To(Equal(account))
By("querying the contract")
params = helpers.MessageParams{
To: contractAddr,
Data: SimpleStorage.FunctionHashes["get"],
}
rBody = sendRPCRequest(client, "eth_call", proxyAddress, 19, params)
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(rBody, &respBody)
Expect(err).ToNot(HaveOccurred())
Expect(respBody.Error).To(BeZero())
Expect(respBody.Result).To(Equal("0x" + val))
By("querying the latest block number")
rBody = sendRPCRequest(client, "eth_blockNumber", proxyAddress, 20, []interface{}{})
Expect(err).ToNot(HaveOccurred())
err = json.Unmarshal(rBody, &respBody)
Expect(err).ToNot(HaveOccurred())
Expect(respBody.Error).To(BeZero())
Expect(respBody.Result).To(Equal(receipt.BlockNumber))
checkHexEncoded(respBody.Result)
By("getting the block by number we just got")
rBody = sendRPCRequest(client, "eth_getBlockByNumber", proxyAddress, rand.Int(), []interface{}{receipt.BlockNumber, false})
err = json.Unmarshal(rBody, &respBlockBody)
Expect(err).ToNot(HaveOccurred())
latestBlock := respBlockBody.Result
Expect(latestBlock.Number).To(Equal(receipt.BlockNumber))
By("querying for logs of a transaction with no logs, we get no logs")
rBody = sendRPCRequest(client, "eth_getLogs", proxyAddress, 20, []interface{}{})
err = json.Unmarshal(rBody, &respArrayBody)
Expect(err).ToNot(HaveOccurred())
Expect(respArrayBody.Result).To(HaveLen(0))
By("querying for logs of the genesis block, we get no logs")
rBody = sendRPCRequest(client, "eth_getLogs", proxyAddress,
28, types.GetLogsArgs{FromBlock: "earliest", ToBlock: "0x0"})
fmt.Fprintln(GinkgoWriter, string(rBody))
err = json.Unmarshal(rBody, &respArrayBody)
Expect(err).ToNot(HaveOccurred(), "problem unmarshalling", string(rBody))
Expect(respArrayBody.Result).To(HaveLen(0))
})
It("implements the ethereum json rpc api with logs", func() {
var err error
var rBody []byte
var respBody helpers.JsonRPCResponse
var rpcResp helpers.JsonRPCTxReceipt
var respArrayBody helpers.JsonRPCLogArrayResponse
By("Deploying the Simple Storage With Logs Contract")
params := helpers.MessageParams{
To: "0000000000000000000000000000000000000000",
Data: helpers.SimpleStorageWithLog().CompiledBytecode,
}
rBody = sendRPCRequest(client, "eth_sendTransaction", proxyAddress, 6, params)
err = json.Unmarshal(rBody, &respBody)
Expect(err).ToNot(HaveOccurred())
Expect(respBody.Error).To(BeZero())
txHash := respBody.Result
// It takes a couple seconds for the transaction to be found
Eventually(func() helpers.JsonRPCError {
rBody = sendRPCRequest(client, "eth_getTransactionReceipt", proxyAddress, 16, []string{txHash})
rpcResp = helpers.JsonRPCTxReceipt{}
err = json.Unmarshal(rBody, &rpcResp)
Expect(err).ToNot(HaveOccurred())
return rpcResp.Error
}, LongEventualTimeout, LongPollingInterval).Should(BeZero())
receipt := rpcResp.Result
contractAddr := receipt.ContractAddress
By("using one log arg for both log gets")
logFilter := types.GetLogsArgs{ToBlock: "latest", // FromBlock as a default argument
Address: types.AddressFilter{contractAddr},
Topics: types.TopicsFilter{
types.TopicFilter{}, // a null topic filter, and a no 0x prefix topic
types.TopicFilter{"0000000000000000000000000000000000000000000000000000000000000000"}}}
By("starting a logs filter before creating new blocks with logs")
rBody = sendRPCRequest(client, "eth_newFilter", proxyAddress, rand.Int(), logFilter)
err = json.Unmarshal(rBody, &respBody)
Expect(err).ToNot(HaveOccurred())
// logsFilterID := respBody.Result
By("interacting with the contract")
val := "3737373737373737373737373737373737373737373737373737373737373737"
params = helpers.MessageParams{
To: contractAddr,
Data: helpers.SimpleStorageWithLog().FunctionHashes["set"] + val,
}
rBody = sendRPCRequest(client, "eth_sendTransaction", proxyAddress, 18, params)
err = json.Unmarshal(rBody, &respBody)
Expect(err).ToNot(HaveOccurred())
Expect(respBody.Error).To(BeZero())
txHash = respBody.Result
// It takes a couple seconds for the transaction to be found
Eventually(func() helpers.JsonRPCError {
rBody = sendRPCRequest(client, "eth_getTransactionReceipt", proxyAddress, 16, []string{txHash})
rpcResp = helpers.JsonRPCTxReceipt{}
err = json.Unmarshal(rBody, &rpcResp)
Expect(err).ToNot(HaveOccurred())
return rpcResp.Error
}, LongEventualTimeout, LongPollingInterval).Should(BeZero())
By("querying for logs of a contract with logs, we get a log")
rBody = sendRPCRequest(client, "eth_getLogs", proxyAddress, 23, logFilter)
err = json.Unmarshal(rBody, &respArrayBody)
Expect(err).ToNot(HaveOccurred())
logs := respArrayBody.Result
Expect(logs).To(HaveLen(1), "this contract only emits one log entry")
log := logs[0]
Expect(log.Address).To(Equal(contractAddr), "logs come from the contract")
Expect(log.Topics).To(HaveLen(3))
Expect(log.Index).To(Equal("0x0"))
topics := log.Topics
// topics are, hash of event signature, 0 from initial setting, & the value we set earlier
Expect(topics[0]).To(Equal("0xd81ec364c58bcc9b49b6c953fc8e1f1c158ee89255bae73029133234a2936aad"))
Expect(topics[1]).To(Equal("0x0000000000000000000000000000000000000000000000000000000000000000"))
Expect(topics[2]).To(Equal("0x" + val))
txReceipt := rpcResp.Result
Expect(log.BlockNumber).To(Equal(txReceipt.BlockNumber))
Expect(log.BlockHash).To(Equal(txReceipt.BlockHash))
Expect(log.TxIndex).To(Equal(txReceipt.TransactionIndex))
Expect(log.TxHash).To(Equal(txReceipt.TransactionHash))
By("doing the same thing with the known blockhash instead of block parameters, we get the same results")
rBody = sendRPCRequest(client, "eth_getLogs", proxyAddress, 23,
types.GetLogsArgs{BlockHash: txReceipt.BlockHash,
Address: types.AddressFilter{contractAddr},
Topics: types.TopicsFilter{
types.TopicFilter{}, // a null topic filter, and a no 0x prefix topic
types.TopicFilter{"0000000000000000000000000000000000000000000000000000000000000000"}}})
respArrayBody = helpers.JsonRPCLogArrayResponse{}
err = json.Unmarshal(rBody, &respArrayBody)
Expect(err).ToNot(HaveOccurred())
logs = respArrayBody.Result
Expect(logs).To(HaveLen(1), "this contract only emits one log entry")
log = logs[0]
Expect(log.Address).To(Equal(contractAddr), "logs come from the contract")
Expect(log.Topics).To(HaveLen(3))
Expect(log.Index).To(Equal("0x0"))
topics = log.Topics
// topics are, hash of event signature, 0 from initial setting, & the value we set earlier
Expect(topics[0]).To(Equal("0xd81ec364c58bcc9b49b6c953fc8e1f1c158ee89255bae73029133234a2936aad"))
Expect(topics[1]).To(Equal("0x0000000000000000000000000000000000000000000000000000000000000000"))
Expect(topics[2]).To(Equal("0x" + val))
txReceipt = rpcResp.Result
Expect(log.BlockNumber).To(Equal(txReceipt.BlockNumber))
Expect(log.BlockHash).To(Equal(txReceipt.BlockHash))
Expect(log.TxIndex).To(Equal(txReceipt.TransactionIndex))
Expect(log.TxHash).To(Equal(txReceipt.TransactionHash))
})
It("implements the ethereum json rpc api for async-logs", func() {
var err error
var respBody helpers.JsonRPCResponse
var uninstallBody helpers.JsonRPCBoolResponse
rBody := sendRPCRequest(client, "eth_newFilter", proxyAddress, 37, []interface{}{})
err = json.Unmarshal(rBody, &respBody)
Expect(err).ToNot(HaveOccurred())
filterID := respBody.Result
rBody = sendRPCRequest(client, "eth_uninstallFilter", proxyAddress, 38, filterID)
err = json.Unmarshal(rBody, &uninstallBody)
Expect(err).ToNot(HaveOccurred())
Expect(uninstallBody.Result).To(BeTrue())
uninstallBody = helpers.JsonRPCBoolResponse{}
rBody = sendRPCRequest(client, "eth_uninstallFilter", proxyAddress, 39, filterID)
err = json.Unmarshal(rBody, &uninstallBody)
Expect(err).ToNot(HaveOccurred())
Expect(uninstallBody.Result).To(BeFalse(), "filter just now removed")
})
It("shuts down gracefully when it receives an Interrupt signal", func() {
proxy.Signal(os.Interrupt)
Eventually(proxy.Wait()).Should(Receive())
Eventually(proxyRunner.Err()).Should(gbytes.Say("Fab3 exited"))
})
It("shuts down gracefully when it receives an SIGTERM signal", func() {
proxy.Signal(syscall.SIGTERM)
Eventually(proxy.Wait()).Should(Receive())
Eventually(proxyRunner.Err()).Should(gbytes.Say("Fab3 exited"))
})
})
func checkHexEncoded(value string) {
// Check to see that the result is a hexadecimal string
// Check if the prefix is provided
Expect(value[0:2]).To(Equal("0x"))
// Check that the string is not empty
Expect(len(value)).To(BeNumerically(">=", 3), value+" is an empty hex string")
// Ensure the string is hex
Expect(value).To(MatchRegexp(fmt.Sprintf(`[0-9A-Fa-f]{%d}`, len(value[2:]))))
}