diff options
author | Wei-Ning Huang <w@byzantine-lab.io> | 2019-07-25 20:41:46 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-09-17 16:57:31 +0800 |
commit | 174f6bfcdf4e2c7fea3a071653908b477ad9a3b3 (patch) | |
tree | 7990bd656bd9cc7ef3014534ad2fee5163da3790 /dex | |
parent | a9a85fa746c727063015d6e70881426ce8b3a3fb (diff) | |
download | go-tangerine-174f6bfcdf4e2c7fea3a071653908b477ad9a3b3.tar.gz go-tangerine-174f6bfcdf4e2c7fea3a071653908b477ad9a3b3.tar.zst go-tangerine-174f6bfcdf4e2c7fea3a071653908b477ad9a3b3.zip |
core: add GovUtil to unify governance state related access
Add GovUtil so we could use the same logic in everywhere that requires
access to governance state, such as configuration and CRS.
Diffstat (limited to 'dex')
-rw-r--r-- | dex/api_backend.go | 6 | ||||
-rw-r--r-- | dex/app.go | 41 | ||||
-rw-r--r-- | dex/app_test.go | 13 | ||||
-rw-r--r-- | dex/downloader/testchain_test.go | 8 | ||||
-rw-r--r-- | dex/governance.go | 10 |
5 files changed, 59 insertions, 19 deletions
diff --git a/dex/api_backend.go b/dex/api_backend.go index 39c34550b..31b5a650e 100644 --- a/dex/api_backend.go +++ b/dex/api_backend.go @@ -187,7 +187,11 @@ func (b *DexAPIBackend) ProtocolVersion() int { } func (b *DexAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) { - return b.dex.governance.MinGasPrice(b.dex.blockchain.CurrentBlock().Round()), nil + gs, err := b.dex.governance.GetConfigState(b.dex.blockchain.CurrentBlock().Round()) + if err != nil { + return nil, err + } + return gs.MinGasPrice(), nil } func (b *DexAPIBackend) ChainDb() ethdb.Database { diff --git a/dex/app.go b/dex/app.go index fb8ffa46d..10f977c2f 100644 --- a/dex/app.go +++ b/dex/app.go @@ -102,9 +102,14 @@ func (d *DexconApp) validateNonce(txs types.Transactions) (map[common.Address]ui // validateGasPrice checks if no gas price is lower than minGasPrice defined in // governance contract. func (d *DexconApp) validateGasPrice(txs types.Transactions, round uint64) bool { - minGasPrice := d.gov.MinGasPrice(round) + config, err := d.gov.RawConfiguration(round) + if err != nil { + log.Error("Failed to get configuration", "err", err) + return false + } + for _, tx := range txs { - if minGasPrice.Cmp(tx.GasPrice()) > 0 { + if config.MinGasPrice.Cmp(tx.GasPrice()) > 0 { return false } } @@ -180,8 +185,12 @@ func (d *DexconApp) preparePayload(ctx context.Context, position coreTypes.Posit return } - blockGasLimit := new(big.Int).SetUint64(d.gov.DexconConfiguration(position.Round).BlockGasLimit) - minGasPrice := d.gov.DexconConfiguration(position.Round).MinGasPrice + config, err := d.gov.RawConfiguration(position.Round) + if err != nil { + return + } + + blockGasLimit := new(big.Int).SetUint64(config.BlockGasLimit) blockGasUsed := new(big.Int) allTxs := make([]*types.Transaction, 0, 10000) @@ -217,8 +226,8 @@ addressMap: // Warning: the pending tx will also affect by syncing, so startIndex maybe negative for i := startIndex; i >= 0 && i < len(txs); i++ { tx := txs[i] - if minGasPrice.Cmp(tx.GasPrice()) > 0 { - log.Error("Invalid gas price minGas(%v) > get(%v)", minGasPrice, tx.GasPrice()) + if config.MinGasPrice.Cmp(tx.GasPrice()) > 0 { + log.Error("Invalid gas price minGas(%v) > get(%v)", config.MinGasPrice, tx.GasPrice()) break } @@ -372,8 +381,14 @@ func (d *DexconApp) VerifyBlock(block *coreTypes.Block) coreTypes.BlockVerifySta } } + config, err := d.gov.RawConfiguration(block.Position.Round) + if err != nil { + log.Error("Failed to get raw configuration", "err", err) + return coreTypes.VerifyRetryLater + } + // Validate if balance is enough for TXs in this block. - blockGasLimit := new(big.Int).SetUint64(d.gov.DexconConfiguration(block.Position.Round).BlockGasLimit) + blockGasLimit := new(big.Int).SetUint64(config.BlockGasLimit) blockGasUsed := new(big.Int) for _, tx := range transactions { @@ -436,7 +451,10 @@ func (d *DexconApp) BlockDelivered( var owner common.Address if !block.IsEmpty() { - gs := d.gov.GetStateForConfigAtRound(block.Position.Round) + gs, err := d.gov.GetConfigState(block.Position.Round) + if err != nil { + panic(err) + } node, err := gs.GetNodeByID(block.ProposerID) if err != nil { panic(err) @@ -444,11 +462,16 @@ func (d *DexconApp) BlockDelivered( owner = node.Owner } + config, err := d.gov.RawConfiguration(block.Position.Round) + if err != nil { + panic(err) + } + newBlock := types.NewBlock(&types.Header{ Number: new(big.Int).SetUint64(block.Position.Height), Time: uint64(block.Timestamp.UnixNano() / 1000000), Coinbase: owner, - GasLimit: d.gov.DexconConfiguration(block.Position.Round).BlockGasLimit, + GasLimit: config.BlockGasLimit, Difficulty: big.NewInt(1), Round: block.Position.Round, DexconMeta: dexconMeta, diff --git a/dex/app_test.go b/dex/app_test.go index 22b85b766..db9feb228 100644 --- a/dex/app_test.go +++ b/dex/app_test.go @@ -471,7 +471,12 @@ func (t *ppBlockLimitTester) ValidateResults(results []reflect.Value) error { } app := t.App.(*DexconApp) - blockLimit := app.gov.DexconConfiguration(t.round).BlockGasLimit + config, err := app.gov.RawConfiguration(t.round) + if err != nil { + return fmt.Errorf("unable to get raw configuration: %v", err) + } + + blockLimit := config.BlockGasLimit totalGas := uint64(0) for _, tx := range txs { totalGas += tx.Gas() @@ -2161,7 +2166,11 @@ func (f *TxFactory) Run() { blockchain := f.App.(*DexconApp).blockchain txPool := f.App.(*DexconApp).txPool for { - gasPrice := f.App.(*DexconApp).gov.GetHeadState().MinGasPrice() + hs, err := f.App.(*DexconApp).gov.GetHeadGovState() + if err != nil { + panic(err) + } + gasPrice := hs.MinGasPrice() for i, key := range f.keys { go func(at int, nonce uint64, key *ecdsa.PrivateKey) { f.stopTimeMu.RLock() diff --git a/dex/downloader/testchain_test.go b/dex/downloader/testchain_test.go index 9595dc212..32e6110c4 100644 --- a/dex/downloader/testchain_test.go +++ b/dex/downloader/testchain_test.go @@ -332,15 +332,15 @@ func (g *govStateFetcher) SnapshotRound(round uint64, root common.Hash) { g.rootByRound[round] = root } -func (g *govStateFetcher) GetStateForConfigAtRound(round uint64) *vm.GovernanceState { +func (g *govStateFetcher) GetConfigState(round uint64) (*vm.GovernanceState, error) { if root, ok := g.rootByRound[round]; ok { s, err := state.New(root, g.db) if err != nil { - panic(err) + return nil, err } - return &vm.GovernanceState{s} + return &vm.GovernanceState{s}, nil } - return nil + return nil, nil } func (g *govStateFetcher) DKGSetNodeKeyAddresses(round uint64) (map[common.Address]struct{}, error) { diff --git a/dex/governance.go b/dex/governance.go index 1ca94e1dc..50de31a0e 100644 --- a/dex/governance.go +++ b/dex/governance.go @@ -58,9 +58,13 @@ func NewDexconGovernance(backend *DexAPIBackend, chainConfig *params.ChainConfig return g } -// DexconConfiguration return raw config in state. -func (d *DexconGovernance) DexconConfiguration(round uint64) *params.DexconConfig { - return d.GetStateForConfigAtRound(round).Configuration() +// RawConfiguration return raw config in state. +func (d *DexconGovernance) RawConfiguration(round uint64) (*params.DexconConfig, error) { + gs, err := d.GetConfigState(round) + if err != nil { + return nil, err + } + return gs.Configuration(), nil } func (d *DexconGovernance) sendGovTx(ctx context.Context, data []byte) error { |