From b52b1b2106f075260674660638aa60fe5ec81f87 Mon Sep 17 00:00:00 2001 From: Wei-Ning Huang Date: Fri, 19 Oct 2018 16:31:44 +0800 Subject: core: vm: add blockReward to governance --- core/genesis.go | 8 ++++---- core/vm/governance.go | 35 +++++++++++++++++++++++++++++++++++ params/config.go | 10 +++++----- params/gen_dexcon_config.go | 22 +++++++++++----------- test/genesis.json | 6 +++--- 5 files changed, 58 insertions(+), 23 deletions(-) diff --git a/core/genesis.go b/core/genesis.go index c9f4e2499..4d3edb3e8 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -276,16 +276,16 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block { govStateHelper.Stake(addr, account.PublicKey, account.Staked) } } + // Genesis CRS. + crs := crypto.Keccak256([]byte(g.Config.Dexcon.GenesisCRSText)) + govStateHelper.PushCRS(common.BytesToHash(crs)) + // Owner. govStateHelper.SetOwner(g.Config.Dexcon.Owner) // Governance configuration. govStateHelper.UpdateConfiguration(g.Config.Dexcon) - // Genesis CRS. - crs := crypto.Keccak256([]byte(g.Config.Dexcon.GenesisCRSText)) - govStateHelper.PushCRS(common.BytesToHash(crs)) - root := statedb.IntermediateRoot(false) head := &types.Header{ Number: new(big.Int).SetUint64(g.Number), diff --git a/core/vm/governance.go b/core/vm/governance.go index 2e07ba3eb..b2bb531f2 100644 --- a/core/vm/governance.go +++ b/core/vm/governance.go @@ -40,6 +40,20 @@ var minStake = big.NewInt(10000000000000) const abiJSON = ` [ + { + "constant": true, + "inputs": [], + "name": "blockReward", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, { "constant": true, "inputs": [ @@ -406,6 +420,10 @@ const abiJSON = ` { "constant": false, "inputs": [ + { + "name": "BlockReward", + "type": "uint256" + }, { "name": "NumChains", "type": "uint256" @@ -679,6 +697,12 @@ func RunGovernanceContract(evm *EVM, input []byte, contract *Contract) ( // Solidity auto generated methods. // -------------------------------- + case "blockReward": + res, err := method.Outputs.Pack(g.state.BlockReward()) + if err != nil { + return nil, errExecutionReverted + } + return res, nil case "crs": round := new(big.Int) if err := method.Inputs.Unpack(&round, arguments); err != nil { @@ -856,6 +880,7 @@ const ( dkgFinailizedLoc dkgFinalizedsCountLoc ownerLoc + blockRewardLoc numChainsLoc lambdaBALoc lambdaDKGLoc @@ -1183,6 +1208,14 @@ func (s *GovernanceStateHelper) SetOwner(newOwner common.Address) { s.setState(common.BigToHash(big.NewInt(ownerLoc)), newOwner.Hash()) } +// uint256 public blockReward; +func (s *GovernanceStateHelper) BlockReward() *big.Int { + return s.getStateBigInt(big.NewInt(blockRewardLoc)) +} +func (s *GovernanceStateHelper) SetBlockReward(reward *big.Int) { + s.setStateBigInt(big.NewInt(blockRewardLoc), reward) +} + // uint256 public numChains; func (s *GovernanceStateHelper) NumChains() *big.Int { return s.getStateBigInt(big.NewInt(numChainsLoc)) @@ -1247,6 +1280,7 @@ func (s *GovernanceStateHelper) Stake(addr common.Address, publicKey []byte, sta // Configuration returns the current configuration. func (s *GovernanceStateHelper) Configuration() *params.DexconConfig { return ¶ms.DexconConfig{ + BlockReward: s.getStateBigInt(big.NewInt(blockRewardLoc)), NumChains: uint32(s.getStateBigInt(big.NewInt(numChainsLoc)).Uint64()), LambdaBA: s.getStateBigInt(big.NewInt(lambdaBALoc)).Uint64(), LambdaDKG: s.getStateBigInt(big.NewInt(lambdaDKGLoc)).Uint64(), @@ -1262,6 +1296,7 @@ func (s *GovernanceStateHelper) Configuration() *params.DexconConfig { // UpdateConfiguration updates system configuration. func (s *GovernanceStateHelper) UpdateConfiguration(cfg *params.DexconConfig) { + s.setStateBigInt(big.NewInt(blockRewardLoc), cfg.BlockReward) s.setStateBigInt(big.NewInt(numChainsLoc), big.NewInt(int64(cfg.NumChains))) s.setStateBigInt(big.NewInt(lambdaBALoc), big.NewInt(int64(cfg.LambdaBA))) s.setStateBigInt(big.NewInt(lambdaDKGLoc), big.NewInt(int64(cfg.LambdaDKG))) diff --git a/params/config.go b/params/config.go index 45f515c7a..fe21e101d 100644 --- a/params/config.go +++ b/params/config.go @@ -223,8 +223,9 @@ func (c *CliqueConfig) String() string { // DexconConfig is the consensus engine configs for DEXON consensus. type DexconConfig struct { - Owner common.Address `json:"owner"` GenesisCRSText string `json:"genesisCRSText"` + Owner common.Address `json:"owner"` + BlockReward *big.Int `json:"blockReward"` NumChains uint32 `json:"numChains"` LambdaBA uint64 `json:"lambdaBA"` LambdaDKG uint64 `json:"lambdaDKG"` @@ -235,7 +236,6 @@ type DexconConfig struct { RoundInterval uint64 `json:"roundInterval"` MinBlockInterval uint64 `json:"minBlockInterval"` MaxBlockInterval uint64 `json:"maxBlockInterval"` - BlockReward *big.Int `json:"blockReward"` } type dexconConfigSpecMarshaling struct { @@ -244,9 +244,10 @@ type dexconConfigSpecMarshaling struct { // String implements the stringer interface, returning the consensus engine details. func (d *DexconConfig) String() string { - return fmt.Sprintf("{Owner: %v GenesisCRSText: %v NumChains: %v LambdaBA: %v LambdaDKG: %v K: %v PhiRatio: %v NotarySetSize: %v DKGSetSize: %v RoundInterval: %v MinBlockInterval: %v MaxBlockInterval: %v BlockReward: %v", - d.Owner, + return fmt.Sprintf("{GenesisCRSText: %v Owner: %v BlockReward: %v NumChains: %v LambdaBA: %v LambdaDKG: %v K: %v PhiRatio: %v NotarySetSize: %v DKGSetSize: %v RoundInterval: %v MinBlockInterval: %v MaxBlockInterval: %v BlockReward: %v", d.GenesisCRSText, + d.Owner, + d.BlockReward, d.NumChains, d.LambdaBA, d.LambdaDKG, @@ -257,7 +258,6 @@ func (d *DexconConfig) String() string { d.RoundInterval, d.MinBlockInterval, d.MaxBlockInterval, - d.BlockReward, ) } diff --git a/params/gen_dexcon_config.go b/params/gen_dexcon_config.go index e53560e47..f81951140 100644 --- a/params/gen_dexcon_config.go +++ b/params/gen_dexcon_config.go @@ -15,8 +15,9 @@ var _ = (*dexconConfigSpecMarshaling)(nil) // MarshalJSON marshals as JSON. func (d DexconConfig) MarshalJSON() ([]byte, error) { type DexconConfig struct { - Owner common.Address `json:"owner"` GenesisCRSText string `json:"genesisCRSText"` + Owner common.Address `json:"owner"` + BlockReward *math.HexOrDecimal256 `json:"blockReward"` NumChains uint32 `json:"numChains"` LambdaBA uint64 `json:"lambdaBA"` LambdaDKG uint64 `json:"lambdaDKG"` @@ -27,11 +28,11 @@ func (d DexconConfig) MarshalJSON() ([]byte, error) { RoundInterval uint64 `json:"roundInterval"` MinBlockInterval uint64 `json:"minBlockInterval"` MaxBlockInterval uint64 `json:"maxBlockInterval"` - BlockReward *math.HexOrDecimal256 `json:"blockReward"` } var enc DexconConfig - enc.Owner = d.Owner enc.GenesisCRSText = d.GenesisCRSText + enc.Owner = d.Owner + enc.BlockReward = (*math.HexOrDecimal256)(d.BlockReward) enc.NumChains = d.NumChains enc.LambdaBA = d.LambdaBA enc.LambdaDKG = d.LambdaDKG @@ -42,15 +43,15 @@ func (d DexconConfig) MarshalJSON() ([]byte, error) { enc.RoundInterval = d.RoundInterval enc.MinBlockInterval = d.MinBlockInterval enc.MaxBlockInterval = d.MaxBlockInterval - enc.BlockReward = (*math.HexOrDecimal256)(d.BlockReward) return json.Marshal(&enc) } // UnmarshalJSON unmarshals from JSON. func (d *DexconConfig) UnmarshalJSON(input []byte) error { type DexconConfig struct { - Owner *common.Address `json:"owner"` GenesisCRSText *string `json:"genesisCRSText"` + Owner *common.Address `json:"owner"` + BlockReward *math.HexOrDecimal256 `json:"blockReward"` NumChains *uint32 `json:"numChains"` LambdaBA *uint64 `json:"lambdaBA"` LambdaDKG *uint64 `json:"lambdaDKG"` @@ -61,17 +62,19 @@ func (d *DexconConfig) UnmarshalJSON(input []byte) error { RoundInterval *uint64 `json:"roundInterval"` MinBlockInterval *uint64 `json:"minBlockInterval"` MaxBlockInterval *uint64 `json:"maxBlockInterval"` - BlockReward *math.HexOrDecimal256 `json:"blockReward"` } var dec DexconConfig if err := json.Unmarshal(input, &dec); err != nil { return err } + if dec.GenesisCRSText != nil { + d.GenesisCRSText = *dec.GenesisCRSText + } if dec.Owner != nil { d.Owner = *dec.Owner } - if dec.GenesisCRSText != nil { - d.GenesisCRSText = *dec.GenesisCRSText + if dec.BlockReward != nil { + d.BlockReward = (*big.Int)(dec.BlockReward) } if dec.NumChains != nil { d.NumChains = *dec.NumChains @@ -103,8 +106,5 @@ func (d *DexconConfig) UnmarshalJSON(input []byte) error { if dec.MaxBlockInterval != nil { d.MaxBlockInterval = *dec.MaxBlockInterval } - if dec.BlockReward != nil { - d.BlockReward = (*big.Int)(dec.BlockReward) - } return nil } diff --git a/test/genesis.json b/test/genesis.json index 0a47ec209..0ac1e1989 100644 --- a/test/genesis.json +++ b/test/genesis.json @@ -7,8 +7,9 @@ "eip155Block": 0, "eip158Block": 0, "dexcon": { - "owner": "0x7C3c31B19395A5e2627F921Cc2802560B71f1caB", "genesisCRSText": "In DEXON, we trust.", + "owner": "0x7C3c31B19395A5e2627F921Cc2802560B71f1caB", + "blockReward": "1000000000000000000", "numChains": 1, "lambdaBA": 50, "lambdaDKG": 2500, @@ -18,8 +19,7 @@ "dkgSetSize": 4, "roundInterval": 99999999999, "minBlockInterval": 900, - "maxBlockInterval": 1100, - "blockReward": "1000000000000000000" + "maxBlockInterval": 1100 } }, "nonce": "0x42", -- cgit v1.2.3