aboutsummaryrefslogtreecommitdiffstats
path: root/core/genesis.go
diff options
context:
space:
mode:
authorWei-Ning Huang <w@cobinhood.com>2018-10-15 17:34:36 +0800
committerWei-Ning Huang <w@dexon.org>2019-04-09 21:32:50 +0800
commit0a000e0595b6b683d6a57776b4e5381a92c20765 (patch)
tree0fcf72d10d317860b5fa28840d10b413db31931f /core/genesis.go
parent447c36e41e989d9e60c70ecc7a1a45bb1814009f (diff)
downloaddexon-0a000e0595b6b683d6a57776b4e5381a92c20765.tar
dexon-0a000e0595b6b683d6a57776b4e5381a92c20765.tar.gz
dexon-0a000e0595b6b683d6a57776b4e5381a92c20765.tar.bz2
dexon-0a000e0595b6b683d6a57776b4e5381a92c20765.tar.lz
dexon-0a000e0595b6b683d6a57776b4e5381a92c20765.tar.xz
dexon-0a000e0595b6b683d6a57776b4e5381a92c20765.tar.zst
dexon-0a000e0595b6b683d6a57776b4e5381a92c20765.zip
core: setup stake in order so genesis block is deterministic
Diffstat (limited to 'core/genesis.go')
-rw-r--r--core/genesis.go27
1 files changed, 26 insertions, 1 deletions
diff --git a/core/genesis.go b/core/genesis.go
index 04da8960c..eaca53673 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)
}