forked from ethereum/hive
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
177 lines (147 loc) · 5.04 KB
/
main.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
package main
import (
"context"
"log"
"math/big"
"time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/hive/hivesim"
"github.com/ethereum/hive/simulators/kcc/ishikari-distribute-block-reward/reservepool"
)
var (
ValidatorsAddress = common.HexToAddress("0x000000000000000000000000000000000000f333")
ReservePoolAddress = common.HexToAddress("0x000000000000000000000000000000000000f999")
ONE_KCS = new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
)
func main() {
suite := hivesim.Suite{
Name: "KCC Ishikari distribute block reward",
Description: "",
}
suite.Add(hivesim.TestSpec{
Name: "distributeBlockReward",
Description: "Compare the balances in validatorsContract before and after calling 'distributeBlockReward'.",
Run: distributeBlockRewardTest,
})
hivesim.MustRunSuite(hivesim.New(), suite)
}
func distributeBlockRewardTest(t *hivesim.T) {
// In genesis.json, we have preallocated some KCS to the reservePool
// create a kcc client
// note: We are using an archive node, as we are going
// to check the balances at different blocks.
client := t.StartClient("kcc", hivesim.Params{
"HIVE_CLIQUE_PRIVATEKEY": "9c647b8b7c4e7c3490668fb6c11473619db80c93704c70893d3813af4090c39c",
"HIVE_MINER": "658bdf435d810c91414ec09147daa6db62406379",
"HIVE_CHAIN_ID": "321",
// block interval: 1s
"HIVE_KCC_POSA_BLOCK_INTERVAL": "3",
// epoch : 10
"HIVE_KCC_POSA_EPOCH": "10",
// initial valiators
"HIVE_KCC_POSA_ISHIKARI_INIT_VALIDATORS": "0x658bdf435d810c91414ec09147daa6db62406379",
// admin
"HIVE_KCC_POSA_ADMIN": "0x658bdf435d810c91414ec09147daa6db62406379",
// KCC Ishikari fork number
"HIVE_FORK_KCC_ISHIKARI": "9",
// KCC Ishikari Patch001 fork number
"HIVE_FORK_KCC_ISHIKARI_PATCH001": "9",
// KCC Ishikari Patch002 fork number
"HIVE_FORK_KCC_ISHIKARI_PATCH002": "9",
// sync mode
"HIVE_NODETYPE": "archive",
}, hivesim.WithStaticFiles(
map[string]string{
"/genesis.json": "genesis.json",
}))
ethClient := ethclient.NewClient(client.RPC())
//
// Wait for the hardfork
// i.e block #9
//
t.Logf("waiting for the hardfork to come...")
for {
number, err := ethClient.BlockNumber(context.TODO())
if err != nil {
t.Fatalf("failed to get block number: %v", err)
}
if number < 9 {
t.Logf("current Block number is #%v", number)
time.Sleep(time.Second * 3)
continue
}
break
}
//
// Set blockReward of reservePool
//
reservePool, err := reservepool.NewReservepool(ReservePoolAddress, ethClient)
if err != nil {
t.Fatalf("failed to create reservessPool: %v", err)
}
privateKey, err := crypto.HexToECDSA("9c647b8b7c4e7c3490668fb6c11473619db80c93704c70893d3813af4090c39c")
if err != nil {
t.Fatalf("failed to decode privatekey hex as private key")
}
transactor, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(321))
if err != nil {
t.Fatalf("failed to create transactor with private key: %v", err)
}
// Set the blockReward amount to 1 KCS
tx, err := reservePool.SetBlockRewardAmount(transactor, ONE_KCS)
if err != nil {
log.Fatalf("failed to setBlockReward: %v", err)
}
// wait for the tx to be included in a block
for {
_, pending, err := ethClient.TransactionByHash(context.TODO(), tx.Hash())
if err != nil {
t.Fatalf("failed to get TX: %v", err)
}
if pending {
t.Logf("waiting for the transaction of setting blockReward to be included in some block ...")
time.Sleep(time.Second * 2)
continue
}
break
}
currentBlockNumber, err := ethClient.BlockNumber(context.TODO())
if err != nil {
t.Fatalf("failed to get block number: %v", err)
}
// the first block of the next epoch
nextEpochStartBlock := (currentBlockNumber + 10) - (currentBlockNumber+10)%10
// wait for block "nextEpochStartBlock"
for {
number, err := ethClient.BlockNumber(context.TODO())
if err != nil {
t.Fatalf("failed to get block number: %v", err)
}
if number <= nextEpochStartBlock {
t.Logf("current Block number is #%v", number)
time.Sleep(time.Second * 3)
continue
}
break
}
// check balance change
// balance in validators Contract at block (nextEpochStartBlock-1)
balanceBefore, err := ethClient.BalanceAt(context.TODO(), ValidatorsAddress, big.NewInt(int64(nextEpochStartBlock-1)))
if err != nil {
t.Logf("failed to get balance:%v", err)
}
// balance in validators Contract at block (nextEpochStartBlock)
balanceAfter, err := ethClient.BalanceAt(context.TODO(), ValidatorsAddress, big.NewInt(int64(nextEpochStartBlock)))
if err != nil {
t.Logf("failed to get balance:%v", err)
}
// balance diff
diff := new(big.Int).Sub(balanceAfter, balanceBefore)
t.Logf("The difference of balances in validators Contract at block #%v and block #%v is %v wei", nextEpochStartBlock, nextEpochStartBlock-1, diff.String())
if diff.Cmp(ONE_KCS) != 0 {
t.Fatalf("expected balance difference is : %v, actual: %v", ONE_KCS.String(), diff.String())
}
}