diff options
author | Wei-Ning Huang <w@cobinhood.com> | 2018-10-12 13:54:32 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-03-12 12:19:09 +0800 |
commit | cb49d3ced400076fb5d58b98c5e33edece2aa2d4 (patch) | |
tree | a53df3d3af9434e658dbb40da635936ea5538dea | |
parent | 99c57e329cdc2d4c75305d6df8463f77c71e608d (diff) | |
download | dexon-cb49d3ced400076fb5d58b98c5e33edece2aa2d4.tar dexon-cb49d3ced400076fb5d58b98c5e33edece2aa2d4.tar.gz dexon-cb49d3ced400076fb5d58b98c5e33edece2aa2d4.tar.bz2 dexon-cb49d3ced400076fb5d58b98c5e33edece2aa2d4.tar.lz dexon-cb49d3ced400076fb5d58b98c5e33edece2aa2d4.tar.xz dexon-cb49d3ced400076fb5d58b98c5e33edece2aa2d4.tar.zst dexon-cb49d3ced400076fb5d58b98c5e33edece2aa2d4.zip |
dex: governance: implement governance interface
-rw-r--r-- | core/genesis.go | 5 | ||||
-rw-r--r-- | core/vm/governance.go | 29 | ||||
-rw-r--r-- | dex/api_backend.go | 7 | ||||
-rw-r--r-- | dex/backend.go | 32 | ||||
-rw-r--r-- | dex/governance.go | 15 |
5 files changed, 61 insertions, 27 deletions
diff --git a/core/genesis.go b/core/genesis.go index f7e3f7a12..1c95e9c12 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -237,10 +237,7 @@ 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, - } + 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) diff --git a/core/vm/governance.go b/core/vm/governance.go index a84fc9646..d35fd54e4 100644 --- a/core/vm/governance.go +++ b/core/vm/governance.go @@ -776,20 +776,19 @@ const ( // State manipulation helper fro the governance contract. type GovernanceStateHelper struct { - Address common.Address StateDB StateDB } func (s *GovernanceStateHelper) getState(loc common.Hash) common.Hash { - return s.StateDB.GetState(s.Address, loc) + return s.StateDB.GetState(GovernanceContractAddress, loc) } func (s *GovernanceStateHelper) setState(loc common.Hash, val common.Hash) { - s.StateDB.SetState(s.Address, loc, val) + s.StateDB.SetState(GovernanceContractAddress, loc, val) } func (s *GovernanceStateHelper) getStateBigInt(loc *big.Int) *big.Int { - res := s.StateDB.GetState(s.Address, common.BigToHash(loc)) + res := s.StateDB.GetState(GovernanceContractAddress, common.BigToHash(loc)) return new(big.Int).SetBytes(res.Bytes()) } @@ -1115,6 +1114,22 @@ func (s *GovernanceStateHelper) maxBlockInterval() *big.Int { return s.getStateBigInt(big.NewInt(maxBlockIntervalLoc)) } +// GetConfiguration returns the current configuration. +func (s *GovernanceStateHelper) GetConfiguration() *params.DexconConfig { + return ¶ms.DexconConfig{ + NumChains: uint32(s.getStateBigInt(big.NewInt(numChainsLoc)).Uint64()), + LambdaBA: s.getStateBigInt(big.NewInt(lambdaBALoc)).Uint64(), + LambdaDKG: s.getStateBigInt(big.NewInt(lambdaDKGLoc)).Uint64(), + K: int(s.getStateBigInt(big.NewInt(kLoc)).Uint64()), + PhiRatio: float32(s.getStateBigInt(big.NewInt(phiRatioLoc)).Uint64()) / 1000000.0, + NotarySetSize: uint32(s.getStateBigInt(big.NewInt(notarySetSizeLoc)).Uint64()), + DKGSetSize: uint32(s.getStateBigInt(big.NewInt(dkgSetSizeLoc)).Uint64()), + RoundInterval: s.getStateBigInt(big.NewInt(roundIntervalLoc)).Uint64(), + MinBlockInterval: s.getStateBigInt(big.NewInt(minBlockIntervalLoc)).Uint64(), + MaxBlockInterval: s.getStateBigInt(big.NewInt(maxBlockIntervalLoc)).Uint64(), + } +} + // UpdateConfiguration updates system configuration. func (s *GovernanceStateHelper) UpdateConfiguration(cfg *params.DexconConfig) { s.setStateBigInt(big.NewInt(numChainsLoc), big.NewInt(int64(cfg.NumChains))) @@ -1132,7 +1147,7 @@ func (s *GovernanceStateHelper) UpdateConfiguration(cfg *params.DexconConfig) { // event ConfigurationChanged(); func (s *GovernanceStateHelper) emitConfigurationChangedEvent() { s.StateDB.AddLog(&types.Log{ - Address: s.Address, + Address: GovernanceContractAddress, Topics: []common.Hash{events["ConfigurationChanged"].Id()}, Data: []byte{}, }) @@ -1141,7 +1156,7 @@ func (s *GovernanceStateHelper) emitConfigurationChangedEvent() { // event CRSProposed(uint256 round, bytes32 crs); func (s *GovernanceStateHelper) emitCRSProposed(round *big.Int, crs common.Hash) { s.StateDB.AddLog(&types.Log{ - Address: s.Address, + Address: GovernanceContractAddress, Topics: []common.Hash{events["CRSProposed"].Id(), common.BigToHash(round)}, Data: crs.Bytes(), }) @@ -1157,7 +1172,7 @@ type GovernanceContract struct { func newGovernanceContract(evm *EVM, contract *Contract) *GovernanceContract { return &GovernanceContract{ evm: evm, - state: GovernanceStateHelper{contract.Address(), evm.StateDB}, + state: GovernanceStateHelper{evm.StateDB}, contract: contract, } } diff --git a/dex/api_backend.go b/dex/api_backend.go index 8a147aab2..7f2677dc8 100644 --- a/dex/api_backend.go +++ b/dex/api_backend.go @@ -29,6 +29,7 @@ import ( "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/eth/downloader" "github.com/dexon-foundation/dexon/eth/gasprice" "github.com/dexon-foundation/dexon/ethdb" @@ -181,9 +182,9 @@ func (b *DexAPIBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.S return b.dex.TxPool().SubscribeNewTxsEvent(ch) } -//func (b *DexAPIBackend) Downloader() *downloader.Downloader { -// return b.dex.Downloader() -//} +func (b *DexAPIBackend) Downloader() *downloader.Downloader { + return b.dex.Downloader() +} func (b *DexAPIBackend) ProtocolVersion() int { return b.dex.DexVersion() diff --git a/dex/backend.go b/dex/backend.go index 3b7fde400..8213f582e 100644 --- a/dex/backend.go +++ b/dex/backend.go @@ -31,6 +31,8 @@ import ( "github.com/dexon-foundation/dexon/core/bloombits" "github.com/dexon-foundation/dexon/core/rawdb" "github.com/dexon-foundation/dexon/core/vm" + "github.com/dexon-foundation/dexon/dex/gasprice" + "github.com/dexon-foundation/dexon/eth/downloader" "github.com/dexon-foundation/dexon/ethdb" "github.com/dexon-foundation/dexon/event" "github.com/dexon-foundation/dexon/internal/ethapi" @@ -64,6 +66,8 @@ type Dexon struct { bloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests bloomIndexer *core.ChainIndexer // Bloom indexer operating during block imports + APIBackend *DexAPIBackend + // Dexon consensus. app *DexconApp governance *DexconGovernance @@ -81,7 +85,6 @@ func New(ctx *node.ServiceContext, config *Config) (*Dexon, error) { if err != nil { panic(err) } - gov := NewDexconGovernance() network := NewDexconNetwork() // TODO(w): replace this with node key. @@ -116,7 +119,6 @@ func New(ctx *node.ServiceContext, config *Config) (*Dexon, error) { shutdownChan: make(chan bool), networkID: config.NetworkId, bloomRequests: make(chan chan *bloombits.Retrieval), - governance: gov, network: network, blockdb: db, engine: dexcon.New(¶ms.DexconConfig{}), @@ -145,9 +147,16 @@ func New(ctx *node.ServiceContext, config *Config) (*Dexon, error) { } dex.txPool = core.NewTxPool(config.TxPool, dex.chainConfig, dex.blockchain) - dex.app = NewDexconApp(dex.txPool, dex.blockchain, gov, chainDb, config, vmConfig) + dex.APIBackend = &DexAPIBackend{dexon, nil} + gpoParams := config.GPO + //if gpoParams.Default == nil { + // gpoParams.Default = config.MinerGasPrice + //} + dex.APIBackend.gpo = gasprice.NewOracle(dex.APIBackend, gpoParams) - dex.consensus = dexCore.NewConsensus(dex.app, gov, db, network, privKey) + dex.governance = NewDexconGovernance(dex.APIBackend) + dex.app = NewDexconApp(dex.txPool, dex.blockchain, dex.governance, chainDb, config, vmConfig) + dex.consensus = dexCore.NewConsensus(dex.app, dex.governance, db, network, privKey) return dex, nil } @@ -180,10 +189,11 @@ func CreateDB(ctx *node.ServiceContext, config *Config, name string) (ethdb.Data return db, nil } -func (d *Dexon) AccountManager() *accounts.Manager { return d.accountManager } -func (d *Dexon) BlockChain() *core.BlockChain { return d.blockchain } -func (d *Dexon) TxPool() *core.TxPool { return d.txPool } -func (d *Dexon) DexVersion() int { return int(d.protocolManager.SubProtocols[0].Version) } -func (d *Dexon) EventMux() *event.TypeMux { return d.eventMux } -func (d *Dexon) Engine() consensus.Engine { return d.engine } -func (d *Dexon) ChainDb() ethdb.Database { return d.chainDb } +func (d *Dexon) AccountManager() *accounts.Manager { return d.accountManager } +func (d *Dexon) BlockChain() *core.BlockChain { return d.blockchain } +func (d *Dexon) TxPool() *core.TxPool { return d.txPool } +func (d *Dexon) DexVersion() int { return int(d.protocolManager.SubProtocols[0].Version) } +func (d *Dexon) EventMux() *event.TypeMux { return d.eventMux } +func (d *Dexon) Engine() consensus.Engine { return d.engine } +func (d *Dexon) ChainDb() ethdb.Database { return d.chainDb } +func (d *Dexon) Downloader() *downloader.Downloader { return d.protocolManager.downloader } diff --git a/dex/governance.go b/dex/governance.go index e990f32f3..f49355816 100644 --- a/dex/governance.go +++ b/dex/governance.go @@ -4,19 +4,30 @@ import ( coreCommon "github.com/dexon-foundation/dexon-consensus-core/common" "github.com/dexon-foundation/dexon-consensus-core/core/crypto" "github.com/dexon-foundation/dexon-consensus-core/core/types" + "github.com/dexon-foundation/dexon/core/vm" ) type DexconGovernance struct { + b *DexAPIBackend } // NewDexconGovernance retruns a governance implementation of the DEXON // consensus governance interface. -func NewDexconGovernance() *DexconGovernance { - return &DexconGovernance{} +func NewDexconGovernance(backend *DexAPIBackend) *DexconGovernance { + return &DexconGovernance{ + b: backend, + } } // Configuration return the total ordering K constant. func (d *DexconGovernance) Configuration(round uint64) *types.Config { + state, _, err := d.b.StateAndHeaderByNumber(ctx, blockNr) + if state == nil || err != nil { + return nil, err + } + + s := vm.GovernanceStateHelper{state} + return &types.Config{} } |