aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWei-Ning Huang <w@cobinhood.com>2018-10-15 17:34:36 +0800
committerWei-Ning Huang <w@dexon.org>2018-12-19 20:54:27 +0800
commit48071f3c63d780e13b547a33c00f371145d80f30 (patch)
tree4ad10638e50c208767711e84847b2fe533f9b350
parentb18e8b5c2a10479d6a1f76d5b8a3b5f5d5097cd3 (diff)
downloaddexon-48071f3c63d780e13b547a33c00f371145d80f30.tar
dexon-48071f3c63d780e13b547a33c00f371145d80f30.tar.gz
dexon-48071f3c63d780e13b547a33c00f371145d80f30.tar.bz2
dexon-48071f3c63d780e13b547a33c00f371145d80f30.tar.lz
dexon-48071f3c63d780e13b547a33c00f371145d80f30.tar.xz
dexon-48071f3c63d780e13b547a33c00f371145d80f30.tar.zst
dexon-48071f3c63d780e13b547a33c00f371145d80f30.zip
core: setup stake in order so genesis block is deterministic
-rw-r--r--core/genesis.go27
-rwxr-xr-xtest/run_test.sh2
2 files changed, 27 insertions, 2 deletions
diff --git a/core/genesis.go b/core/genesis.go
index 3d957f265..13ba2fcc0 100644
--- a/core/genesis.go
+++ b/core/genesis.go
@@ -23,6 +23,7 @@ import (
"errors"
"fmt"
"math/big"
+ "sort"
"strings"
"github.com/dexon-foundation/dexon/common"
@@ -230,6 +231,20 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
}
}
+type AllocKey []common.Address
+
+func (a AllocKey) Len() int {
+ return len(a)
+}
+
+func (a AllocKey) Less(i int, j int) bool {
+ return bytes.Compare(a[i][:], a[j][:]) < 0
+}
+
+func (a AllocKey) Swap(i int, j int) {
+ a[i], a[j] = a[j], a[i]
+}
+
// ToBlock creates the genesis block and writes state of a genesis specification
// to the given database (or discards it if nil).
func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
@@ -238,6 +253,7 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
}
statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
govStateHelper := vm.GovernanceStateHelper{statedb}
+
for addr, account := range g.Alloc {
statedb.AddBalance(addr, new(big.Int).Sub(account.Balance, account.Staked))
statedb.SetCode(addr, account.Code)
@@ -245,8 +261,17 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
for key, value := range account.Storage {
statedb.SetState(addr, key, value)
}
+ }
+
+ // Stake in governance state.
+ keys := AllocKey{}
+ for addr := range g.Alloc {
+ keys = append(keys, addr)
+ }
+ sort.Sort(keys)
- // Stake in governance state.
+ for _, addr := range keys {
+ account := g.Alloc[addr]
if account.Staked.Cmp(big.NewInt(0)) > 0 {
govStateHelper.Stake(addr, account.PublicKey, account.Staked)
}
diff --git a/test/run_test.sh b/test/run_test.sh
index f02200994..53c375705 100755
--- a/test/run_test.sh
+++ b/test/run_test.sh
@@ -11,7 +11,7 @@ for i in $(seq 1 7); do
rm -rf $datadir
$GETH --datadir=$datadir init genesis.json
cp test$i.nodekey $datadir/geth/nodekey
- $GETH --datadir=$datadir --port=$((21000 + $i)) > geth.$i.log 2>&1 &
+ $GETH --verbosity=4 --datadir=$datadir --port=$((28000 + $i)) > geth.$i.log 2>&1 &
done
tail -f geth.*.log