-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpoa.js
179 lines (138 loc) · 5.51 KB
/
poa.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
/**
* PoA contract tests.
* Mostly tests validators logic, as other functional tested in Bridge test.
*
* @module contracts/PoAGovernment
*/
'use strict';
/*global web3,artifacts*/
require('chai').should();
const PoAInterface = artifacts.require('PoAGovernment');
const BSInterface = artifacts.require('BankStorage');
const {getValidators, ZERO_ADDRESS, DESTINATION} = require('./helpers/accounts');
const poaTxs = require('./helpers/poaTxs');
function getRequired(amount) {
return web3.utils.toBN(amount).divn(2).addn(1).toString();
}
describe('PoA', () => {
let owner, validators, otherValidators, wbAddresses;
let bs, poa;
before(async () => {
const accounts = await getValidators();
owner = accounts.validators.shift();
validators = accounts.validators.slice(0, 3);
otherValidators = accounts.validators.slice(3);
wbAddresses = accounts.wbAddresses.slice(1);
const BS = new web3.eth.Contract(BSInterface.abi, null, {
data: BSInterface.bytecode
});
bs = await BS.deploy({
arguments: []
}).send({
from: owner,
gas: 6000000
});
const PoA = new web3.eth.Contract(PoAInterface.abi, null, {
data: PoAInterface.bytecode
});
poa = await PoA.deploy({
arguments: [
owner,
bs.options.address
]
}).send({
from: owner,
gas: 6000000
});
poaTxs.setPoA(poa);
await bs.methods.setup(poa.options.address, ZERO_ADDRESS).send({
from: owner,
gas: 120000
});
await poa.methods.setup(
validators,
wbAddresses.splice(0, 3)
).send({
from: owner,
gas: 6000000
});
});
it('should return correct confirmations', async () => {
const required = await poa.methods.required().call();
const expected = getRequired(validators.length);
required.should.equal(expected);
});
it('should reject add existing validator', async () => {
const data = poa.methods.addValidator(validators[0], wbAddresses[0]).encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.SELF, data, {
from: validators[0],
gas: 600000
}, validators.slice(1));
isExecuted.should.equal(false);
});
it('should add new validator', async () => {
const newValidator = otherValidators.splice(0, 1).pop();
const wbAddress = wbAddresses.splice(0, 1).pop();
const data = poa.methods.addValidator(newValidator, wbAddress).encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.SELF, data, {
from: validators[0],
gas: 600000
}, validators.slice(1));
isExecuted.should.equal(true);
validators.push(newValidator);
const addedValidator = await poa.methods.validators(validators.length-1).call();
addedValidator.ethAddress.should.equal(newValidator);
addedValidator.wbAddress.should.equal(wbAddress);
});
it('should update required', async () => {
const required = await poa.methods.required().call();
const expected = getRequired(validators.length);
required.should.be.equal(expected);
});
it('should replace validator', async () => {
const toReplace = validators[0];
const newValidator = otherValidators.splice(0, 1).pop();
const wbAddress = wbAddresses.splice(0, 1).pop();
const data = poa.methods.replaceValidator(toReplace, newValidator, wbAddress).encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.SELF, data, {
from: validators[0],
gas: 600000
}, validators.slice(1));
isExecuted.should.equal(true);
validators[0] = newValidator;
const replaced = await poa.methods.validators(0).call();
replaced.ethAddress.should.equal(validators[0]);
replaced.wbAddress.should.equal(wbAddress);
});
it('should remove validator', async () => {
const toRemove = validators[0];
const data = poa.methods.removeValidator(toRemove).encodeABI();
const isExecuted = await poaTxs.sendAndConfirm(DESTINATION.SELF, data, {
from: validators[0],
gas: 6000000
}, validators.slice(1));
isExecuted.should.equal(true);
validators.shift();
});
it('should update required after removing', async () => {
const required = await poa.methods.required().call();
const expected = getRequired(validators.length);
required.should.be.equal(expected);
});
it('should return correct txId by txHash', async () => {
const newValidator = otherValidators.splice(0, 1).pop();
const wbAddress = wbAddresses.splice(0, 1).pop();
const data = poa.methods.addValidator(newValidator, wbAddress).encodeABI();
const receipt = await poa.methods.submitTransaction(
DESTINATION.SELF,
data
).send({
from: validators[0],
gas: 600000
});
const {_transactionId: expectedTxId, _hash: txHash } = receipt.events.TX_SUBMITTED.returnValues;
const transactionIndex = receipt.events.TX_SUBMITTED.transactionIndex;
const txId = await poa.methods.getTxIdByHash(txHash, transactionIndex).call();
txId.should.be.equal(expectedTxId);
});
});