aboutsummaryrefslogtreecommitdiffstats
path: root/core/genesis.go
diff options
context:
space:
mode:
authorWei-Ning Huang <w@cobinhood.com>2018-10-11 15:01:10 +0800
committerWei-Ning Huang <w@dexon.org>2019-04-09 13:49:52 +0800
commitc67324c68e74aed9f12718b899136f53459dcff8 (patch)
treebf020bc1590034e68e664982e40952320d5f2b9f /core/genesis.go
parentee9678ab194a7daec5a7bc353d994d0dcbee2209 (diff)
downloaddexon-c67324c68e74aed9f12718b899136f53459dcff8.tar.gz
dexon-c67324c68e74aed9f12718b899136f53459dcff8.tar.zst
dexon-c67324c68e74aed9f12718b899136f53459dcff8.zip
core: add stake and public key to genesis account info
Diffstat (limited to 'core/genesis.go')
-rw-r--r--core/genesis.go26
1 files changed, 24 insertions, 2 deletions
diff --git a/core/genesis.go b/core/genesis.go
index f795ac241..6982312d2 100644
--- a/core/genesis.go
+++ b/core/genesis.go
@@ -31,6 +31,8 @@ import (
"github.com/dexon-foundation/dexon/core/rawdb"
"github.com/dexon-foundation/dexon/core/state"
"github.com/dexon-foundation/dexon/core/types"
+ "github.com/dexon-foundation/dexon/core/vm"
+ "github.com/dexon-foundation/dexon/crypto"
"github.com/dexon-foundation/dexon/ethdb"
"github.com/dexon-foundation/dexon/log"
"github.com/dexon-foundation/dexon/params"
@@ -83,6 +85,8 @@ type GenesisAccount struct {
Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
Balance *big.Int `json:"balance" gencodec:"required"`
Nonce uint64 `json:"nonce,omitempty"`
+ Staked *big.Int `json:"staked"`
+ PublicKey []byte `json:"publicKey"`
PrivateKey []byte `json:"secretKey,omitempty"` // for tests
}
@@ -101,8 +105,10 @@ type genesisSpecMarshaling struct {
type genesisAccountMarshaling struct {
Code hexutil.Bytes
Balance *math.HexOrDecimal256
+ Staked *math.HexOrDecimal256
Nonce math.HexOrDecimal64
Storage map[storageJSON]storageJSON
+ PublicKey hexutil.Bytes
PrivateKey hexutil.Bytes
}
@@ -231,13 +237,29 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
db = ethdb.NewMemDatabase()
}
statedb, _ := state.New(common.Hash{}, state.NewDatabase(db))
+ govStateHelper := vm.GovernanceStateHelper{
+ Address: vm.GovernanceContractAddress,
+ StateDB: statedb,
+ }
for addr, account := range g.Alloc {
- statedb.AddBalance(addr, account.Balance)
+ statedb.AddBalance(addr, new(big.Int).Sub(account.Balance, account.Staked))
statedb.SetCode(addr, account.Code)
statedb.SetNonce(addr, account.Nonce)
for key, value := range account.Storage {
statedb.SetState(addr, key, value)
}
+
+ // Stake in governance state.
+ if account.Staked.Cmp(big.NewInt(0)) > 0 {
+ pk, err := crypto.DecompressPubkey(account.PublicKey)
+ if err != nil {
+ panic(err)
+ }
+ if crypto.PubkeyToAddress(*pk) != addr {
+ panic(fmt.Errorf("public key does not belones to %s", addr))
+ }
+ govStateHelper.Stake(addr, account.PublicKey, account.Staked)
+ }
}
root := statedb.IntermediateRoot(false)
head := &types.Header{
@@ -373,7 +395,7 @@ func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis {
common.BytesToAddress([]byte{6}): {Balance: big.NewInt(1)}, // ECAdd
common.BytesToAddress([]byte{7}): {Balance: big.NewInt(1)}, // ECScalarMul
common.BytesToAddress([]byte{8}): {Balance: big.NewInt(1)}, // ECPairing
- faucet: {Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))},
+ faucet: {Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))},
},
}
}