-
Notifications
You must be signed in to change notification settings - Fork 11
/
validator-control.ts
73 lines (58 loc) · 2.75 KB
/
validator-control.ts
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
/**
* Copyright (c) 2024 DSR Corporation, Denver, Colorado.
* https://www.dsr-corporation.com
* SPDX-License-Identifier: Apache-2.0
*/
import environment from '../environment'
import { ROLES } from '../contracts-ts'
import { Account } from '../utils'
import { Actor } from './utils/actor'
// Change it with the new node address!!
const nodeAddress = '0xdf2aa4dfb7be2d4de6f9b1a4574248502ea198b1'
async function getValidatorsForLastBLock() {
const response = await fetch(environment.besu.rpcnode.url, {
method: 'POST',
body: JSON.stringify({
jsonrpc: '2.0',
method: 'qbft_getValidatorsByBlockNumber',
params: ['latest'],
id: 1,
}),
})
return await response.json()
}
async function demo() {
const trustee = await new Actor(environment.accounts.account1).init()
const steward = await new Actor().init()
console.log('1. Get the lst of current validate nodes')
let validators = await trustee.validatorControl.getValidators()
console.log(`Validators: \n ${JSON.stringify(validators, null, 2)}`)
console.log('2. Get the list of validate for last block')
let data = await getValidatorsForLastBLock()
console.log(`Response: ${JSON.stringify(data, null, 2)}`)
console.log('3. Assign Steward role to target account')
await trustee.roleControl.assignRole(ROLES.STEWARD, steward.address)
console.log('4. Add new validator node')
const addValidatorReceipt = await steward.validatorControl.addValidator(nodeAddress)
console.log(`Validator ${nodeAddress} added to the network -- ${JSON.stringify(addValidatorReceipt)}`)
console.log('5. Get the lst of current validate nodes')
validators = await steward.validatorControl.getValidators()
console.log(`Validators: \n ${JSON.stringify(validators, null, 2)}`)
const newAccount = new Account()
console.log('6. Assign Trustee role to new account to ensure that network works')
const receipt2 = await trustee.roleControl.assignRole(ROLES.TRUSTEE, newAccount.address)
console.log(`Role ${ROLES.TRUSTEE} assigned to account ${newAccount.address} -- ${JSON.stringify(receipt2)}`)
console.log('7. Get role of target account')
const newAccountAssignedRole = await trustee.roleControl.getRole(newAccount.address)
console.log(`Account ${newAccount.address} has ${newAccountAssignedRole} role assigned`)
console.log('8. Get the list of validate for last block')
data = await getValidatorsForLastBLock()
console.log(`Response: ${JSON.stringify(data, null, 2)}`)
console.log('9. Remove the validator node')
const removeValidatorReceipt = await steward.validatorControl.removeValidator(nodeAddress)
console.log(`Validator ${nodeAddress} removed from the network -- ${JSON.stringify(addValidatorReceipt)}`)
}
if (require.main === module) {
demo()
}
module.exports = exports = demo