Skip to content

Commit bf194bb

Browse files
committed
feat(contracts): add scripts for keyper set change
1 parent 86f2769 commit bf194bb

File tree

14 files changed

+235
-49
lines changed

14 files changed

+235
-49
lines changed

contracts/deploy/090_fund.js

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,17 @@
11
const { ethers } = require("hardhat");
22
const { BigNumber } = require("ethers");
3+
const { fund } = require("../lib/fund.js");
34

45
module.exports = async function (hre) {
56
const fundValue = hre.deployConf.fundValue;
6-
if (fundValue == "") {
7-
console.log("fund: not doing any funding");
8-
return;
9-
}
10-
117
const { bank, deployer } = await hre.getNamedAccounts();
128
const bankSigner = await ethers.getSigner(bank);
139

1410
let addresses = [];
1511
if (deployer !== bank) {
1612
addresses.push(deployer);
1713
}
18-
addresses.push(...(await hre.getKeyperAddresses()));
14+
addresses.push(...(await hre.getKeyperAddresses(0)));
1915
addresses.push(await hre.getCollatorAddress());
20-
21-
const value = ethers.utils.parseEther(fundValue);
22-
console.log(
23-
"fund: funding %s adresses with %s eth",
24-
addresses.length,
25-
fundValue
26-
);
27-
28-
const txs = [];
29-
for (const a of addresses) {
30-
const balance = await ethers.provider.getBalance(a);
31-
const weiFund = ethers.utils.parseEther(fundValue);
32-
if (balance.gt(BigNumber.from(weiFund))) {
33-
console.log(a + " already funded");
34-
continue;
35-
}
36-
const tx = await bankSigner.sendTransaction({
37-
to: a,
38-
value: value,
39-
});
40-
txs.push(tx);
41-
}
42-
for (const tx of txs) {
43-
await tx.wait();
44-
}
16+
await fund(addresses, bankSigner, fundValue);
4517
};

contracts/hardhat.config.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,17 @@ extendEnvironment((hre) => {
8686
};
8787
}
8888

89-
hre.getKeyperAddresses = async function () {
90-
if (hre.deployConf.keypers === null) {
89+
hre.getKeyperAddresses = async function (index = 0) {
90+
const keypers = hre.deployConf.keypers?.at(index);
91+
if (keypers === undefined) {
9192
const { keyper0, keyper1, keyper2 } = await hre.getNamedAccounts();
9293
if (keyper0 && keyper1 && keyper2) {
9394
return [keyper0, keyper1, keyper2];
9495
} else {
9596
return [];
9697
}
9798
} else {
98-
return hre.deployConf.keypers;
99+
return keypers;
99100
}
100101
};
101102

@@ -125,6 +126,7 @@ module.exports = {
125126
keyper0: 1,
126127
keyper1: 2,
127128
keyper2: 3,
129+
keyper3: 4,
128130
collator: 7,
129131
bank: {
130132
// an account that has funds

contracts/lib/configure-keypers.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ const { ethers } = require("hardhat");
66

77
// const { inspect } = require("util");
88

9-
async function configure_keypers(keyperAddrs) {
9+
//TODO since we want to call this for new keypers
10+
// this should also check and eventually fund the
11+
// keypers if they are below the target funding.
12+
async function configure_keypers(keyperAddrs, blockOffset = 10) {
1013
if (keyperAddrs.length == 0) {
1114
console.log("WARNING: cannot configure keypers: no keyper addresses given");
1215
return;

contracts/lib/fund.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module.exports = {
2+
fund: fund,
3+
};
4+
5+
const { ethers } = require("hardhat");
6+
const { BigNumber } = require("ethers");
7+
8+
async function fund(addresses, bankSigner, fundValue = "1000") {
9+
if (fundValue == "") {
10+
// TODO return error?
11+
console.log("fund: not doing any funding");
12+
return;
13+
}
14+
15+
// TODO errorhandling?
16+
const value = ethers.utils.parseEther(fundValue);
17+
console.log(
18+
"fund: funding %s adresses with %s eth",
19+
addresses.length,
20+
fundValue
21+
);
22+
23+
const txs = [];
24+
for (const a of addresses) {
25+
const balance = await ethers.provider.getBalance(a);
26+
const weiFund = ethers.utils.parseEther(fundValue);
27+
if (balance.gt(BigNumber.from(weiFund))) {
28+
console.log(a + " already funded");
29+
continue;
30+
}
31+
const tx = await bankSigner.sendTransaction({
32+
to: a,
33+
value: value,
34+
});
35+
txs.push(tx);
36+
}
37+
const txPromises = [];
38+
for (const tx of txs) {
39+
txPromises.push(tx.wait());
40+
}
41+
return Promise.all(txPromises);
42+
}

contracts/scripts/change-keypers.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env node
2+
/**
3+
This script configures a new set of keypers
4+
*/
5+
const fs = require("fs");
6+
const process = require("process");
7+
const path = require("path");
8+
const { configure_keypers } = require("../lib/configure-keypers.js");
9+
const { fund } = require("../lib/fund.js");
10+
11+
// const { inspect } = require("util");
12+
13+
/* global __dirname */
14+
process.chdir(path.dirname(__dirname)); // allow calling the script from anywhere
15+
const hre = require("hardhat");
16+
17+
async function main() {
18+
// TODO can we get access to the hre in a script like this?
19+
// TODO use a different json file to determine the keyper changes
20+
const deployConf = JSON.parse(fs.readFileSync(process.env.DEPLOY_CONF));
21+
22+
const newKeyperSet = deployConf.keypers?.at(1);
23+
if (newKeyperSet === undefined) {
24+
console.error("no updated keyper set defined in DEPLOY_CONF");
25+
return;
26+
}
27+
28+
const { bank } = await hre.getNamedAccounts();
29+
const bankSigner = await ethers.getSigner(bank);
30+
31+
const fundValue = hre.deployConf.fundValue;
32+
await fund(newKeyperSet, bankSigner, fundValue);
33+
await configure_keypers(newKeyperSet, 30);
34+
}
35+
36+
main()
37+
.then(() => process.exit(0))
38+
.catch((error) => {
39+
console.error(error);
40+
process.exit(1);
41+
});

docker/01-init-db.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ${BB} rm -rf data/db
1010
$DC up -d db
1111
$DC run --rm --no-deps dockerize -wait tcp://db:5432 -timeout 40s
1212

13-
for cmd in snapshot keyper-0 keyper-1 keyper-2; do
13+
for cmd in snapshot keyper-0 keyper-1 keyper-2 keyper-3; do
1414
$DC exec db createdb -U postgres $cmd
1515
$DC run --rm --no-deps $cmd initdb --config /config/${cmd}.toml
1616
done

docker/02-init-chain.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ source ./common.sh
44

55
$DC stop geth
66
$DC rm -f geth
7-
$DC stop chain-{0..2}-{validator,sentry} chain-seed
8-
$DC rm -f chain-{0..2}-{validator,sentry} chain-seed
7+
$DC stop chain-{0..3}-{validator,sentry} chain-seed
8+
$DC rm -f chain-{0..3}-{validator,sentry} chain-seed
99

1010
${BB} rm -rf data/geth
11-
${BB} rm -rf data/chain-{0..2}-{validator,sentry} data/chain-seed
12-
${BB} mkdir -p data/chain-{0..2}-{validator,sentry}/config data/chain-seed/config
13-
${BB} chmod -R a+rwX data/chain-{0..2}-{validator,sentry}/config data/chain-seed/config
11+
${BB} rm -rf data/chain-{0..3}-{validator,sentry} data/chain-seed
12+
${BB} mkdir -p data/chain-{0..3}-{validator,sentry}/config data/chain-seed/config
13+
${BB} chmod -R a+rwX data/chain-{0..3}-{validator,sentry}/config data/chain-seed/config
1414
${BB} rm -rf data/deployments
1515

1616
# has geth as dependency
@@ -25,7 +25,7 @@ $DC run --rm --no-deps chain-seed init \
2525
--listen-address tcp://127.0.0.1:${TM_RPC_PORT} \
2626
--role seed
2727

28-
for num in 0 1 2; do
28+
for num in 0 1 2 3; do
2929
validator_cmd=chain-$num-validator
3030
sentry_cmd=chain-$num-sentry
3131

@@ -54,7 +54,7 @@ done
5454

5555
seed_node=$(cat data/chain-seed/config/node_key.json.id)@chain-seed:${TM_P2P_PORT}
5656

57-
for num in 0 1 2; do
57+
for num in 0 1 2 3; do
5858
sentry_cmd=chain-$num-sentry
5959
validator_cmd=chain-$num-validator
6060

@@ -74,7 +74,7 @@ for num in 0 1 2; do
7474
${BB} sed -i "/^external-address =/c\external-address = \"${validator_cmd}:${TM_P2P_PORT}\"" data/${validator_cmd}/config/config.toml
7575
done
7676

77-
$DC up -d chain-seed chain-{0..2}-{sentry,validator} keyper-{0..2}
77+
$DC up -d chain-seed chain-{0..3}-{sentry,validator} keyper-{0..3}
7878

7979
echo "We need to wait for the chain to reach height >= 1"
8080
sleep 5
@@ -86,4 +86,4 @@ $DC run --rm --no-deps --entrypoint /rolling-shutter chain-0-validator bootstrap
8686
--shuttermint-url http://chain-0-sentry:${TM_RPC_PORT} \
8787
--signing-key 479968ffa5ee4c84514a477a8f15f3db0413964fd4c20b08a55fed9fed790fad
8888

89-
$DC stop -t 30 geth chain-seed chain-{0..2}-{sentry,validator} keyper-{0..2}
89+
$DC stop -t 30 geth chain-seed chain-{0..3}-{sentry,validator} keyper-{0..3}

docker/10-change-keyperset.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
source ./common.sh
4+
5+
$DC run --rm --no-deps deploy-contracts run scripts/change-keypers.js

docker/config.example/bootnode-0.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Peer role: bootstrap
33

44
# whether to register handlers on the messages and log them
5+
InstanceID = 0
56
ListenMessages = true
67

78
[P2P]

docker/config.example/bootnode-1.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Peer role: bootstrap
33

44
# whether to register handlers on the messages and log them
5+
InstanceID = 0
56
ListenMessages = true
67

78
[P2P]

0 commit comments

Comments
 (0)