forked from hyperledger-archives/fabric-chaincode-evm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web3_e2e_test.js
217 lines (177 loc) · 7.61 KB
/
web3_e2e_test.js
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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
solidityEvent = require("web3/lib/web3/event.js");
vote = require('./voting_contract.js')
instructor = require('./instructor_contract.js')
Web3 = require('web3')
web3 = new Web3()
function TestVotingContract(fab3Address1, fab3Address2){
var user1 = new Web3(new Web3.providers.HttpProvider(fab3Address1))
var user2 = new Web3(new Web3.providers.HttpProvider(fab3Address2))
// Get user addresses
var user1Addr = user1.eth.accounts[0]
if (user1Addr == ""){
console.log("Unable to query user1's account address")
process.exit(1)
}
user1.eth.defaultAccount = user1Addr
var user2Addr = user2.eth.accounts[0]
if (user2Addr == ""){
console.log("Unable to query user2's account address")
process.exit(1)
}
user2.eth.defaultAccount = user2Addr
if (user1Addr == user2Addr){
console.log("Both user accounts are the same")
process.exit(1)
}
// Create and Deploy Contract Objects
// Each user needs their own version of the contract as the contract object is connected to the account that
// created the object
var user1VotingContract = user1.eth.contract(vote.votingContractABI)
var user2VotingContract = user2.eth.contract(vote.votingContractABI)
var deployedContract = user1VotingContract.new(['a','b'], {data: vote.compiledVotingContract})
var contractTransaction = user1.eth.getTransaction(deployedContract.transactionHash)
if ( ! contractTransaction.input.includes(vote.compiledVotingContract) ){
console.log("getTransaction should return transaction with full details that includes the compiled contract")
console.log(contractTransaction)
console.log(vote.compiledVotingContract)
console.log(contractTransaction.input)
process.exit(1)
}
var contractTransactionBlock = user1.eth.getBlock(contractTransaction.blockNumber, false)
if ( contractTransactionBlock.transactions[contractTransaction.transactionIndex] != contractTransaction.hash ) {
console.log("getBlock should have a block with the same transaction as before")
console.log(contractTransactionBlock)
console.log(contractTransaction)
process.exit(1)
}
var contractAddress = user1.eth.getTransactionReceipt(deployedContract.transactionHash).contractAddress
var user1Contract = user1VotingContract.at(contractAddress)
var user2Contract = user2VotingContract.at(contractAddress)
deployedRuntimeCode = user1.eth.getCode(contractAddress)
if (deployedRuntimeCode != vote.runtimeVotingContract){
console.log("Failed to deploy Voting smart contract")
process.exit(1)
}
// Check that proposals have been properly initialized
CheckProposal(user1Contract.proposals('0'), 'a',0)
CheckProposal(user1Contract.proposals('1'), 'b',0)
// Interact with the contract
user1Contract.vote('0')
// Check that proposals have been properly updated after vote.
CheckProposal(user1Contract.proposals('0'), 'a',1)
CheckProposal(user1Contract.proposals('1'), 'b',0)
// Check that User2 can query the smart contract code
if (user2.eth.getCode(contractAddress) != vote.runtimeVotingContract){
console.log("User2 was unable to getCode for the contract")
process.exit(1)
}
// User1 Should give User2 ability to vote
user1Contract.giveRightToVote(user2.eth.defaultAccount)
// User2 should be able to vote
user2Contract.vote('0')
// Check the votes
CheckProposal(user1Contract.proposals('0'), 'a',2)
CheckProposal(user1Contract.proposals('1'), 'b',0)
console.log("Successfully able to deploy Voting Smart Contract and interact with it")
}
function TestInstructorContractEvents(fab3Address){
var user = new Web3(new Web3.providers.HttpProvider(fab3Address))
// Get user addresses
var userAddr = user.eth.accounts[0]
if (userAddr == ""){
console.log("Unable to query user1's account address")
process.exit(1)
}
user.eth.defaultAccount = userAddr
// Create and Deploy Contract Objects
var instructorContract = user.eth.contract(instructor.instructorContractABI)
var deployedContract = instructorContract.new(['a','b'], {data: instructor.compiledInstructorContract})
var contractAddress = user.eth.getTransactionReceipt(deployedContract.transactionHash).contractAddress
var userContract = instructorContract.at(contractAddress)
deployedRuntimeCode = user.eth.getCode(contractAddress)
if (deployedRuntimeCode != instructor.runtimeInstructorContract){
console.log("Failed to deploy Instructor smart contract")
process.exit(1)
}
txID = userContract.setInstructor('0x53616d', 25, 30000)
receipt = user.eth.getTransactionReceipt(txID)
if (receipt.logs == null) {
console.log("No logs were found for Instructor Contract transaction")
process.exit(1)
}
var logs = receipt.logs
if (logs.length != 1) {
// Instructor Contract should only produce one log object
console.log("Only one log should exist from instructor contract transaction")
process.exit(1)
}
if (logs[0].topics.length != 2) {
// Setter Event should two topics, 1. setter signature, 2. value of indexed param
console.log("Setter event should have two topics")
process.exit(1)
}
if (logs[0].topics[0] != "0x" + instructor.setterEventSignature) {
console.log("First topic should be the Event Signature: Setter(bytes32,uint,uint)")
console.log("Expect topic: 0x" + logs[0].topics[0] + " to equal: 0x" + instructor.setterEventSignature )
process.exit(1)
}
EventDecoder(logs, instructor.instructorContractABI)
decodedEvent = logs[0]
if (decodedEvent.event != "Setter"){
console.log("Incorrect Event in decoded event")
process.exit(1)
}
eventArgs = decodedEvent.args
if (eventArgs.name != '0x53616d0000000000000000000000000000000000000000000000000000000000'){
console.log("Event has incorrect name")
console.log("Expected name " + eventArgs.name + " to equal 0x53616d0000000000000000000000000000000000000000000000000000000000")
process.exit(1)
}
if (eventArgs.age.toNumber() != 25){
console.log("Event has the wrong age")
console.log("Expected age " + eventArgs.age.toNumber() + " to equal 25")
process.exit(1)
}
if (eventArgs.salary.toNumber() != 30000){
console.log("Event has the wrong salary")
console.log("Expected salary " + eventArgs.salary.toNumber() + " to equal 30000")
process.exit(1)
}
console.log("Successfully able to deploy Instructor Smart Contract, see events, and interact with it")
}
//Check Proposal checks the proposal object in the Voting Contract
function CheckProposal(proposal, expectedName, expectedCount){
var proposalName = web3.toUtf8(proposal[0])
if ( proposalName != expectedName ){
console.log("Proposal name: "+ proposalName + " does not match expected name "+ expectedName)
process.exit(1)
}
if (proposal[1].toNumber() != expectedCount ){
console.log("Proposal count: "+ proposal[1].toNumber() + " does not match expected count "+ expectedCount)
process.exit(1)
}
}
//This function will in place decode the log objects that match events in the abi
function EventDecoder(logs, abi){
var decoders = abi.filter(function (json) {
return json.type === 'event';
}).map(function(json) {
return new solidityEvent(null, json, null);
});
return logs.map(function (log) {
return decoders.find(function(decoder) {
return (decoder.signature() == log.topics[0].replace("0x",""));
}).decode(log);
})
}
console.log("Starting Web3 E2E Test")
// node web3_e2e_test.js addr1 addr2
var user1Address = process.argv[2]
var user2Address = process.argv[3]
TestVotingContract(user1Address, user2Address)
TestInstructorContractEvents(user1Address)
console.log("Finished Web3 Tests")