From 5bda6cec66d7b03b23963f3d44514ac1e98da2c4 Mon Sep 17 00:00:00 2001 From: Wei-Ning Huang Date: Thu, 11 Oct 2018 15:01:10 +0800 Subject: core: add stake and public key to genesis account info --- core/genesis.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'core/genesis.go') 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))}, }, } } -- cgit