diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2019-02-25 18:27:34 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-03-12 12:19:09 +0800 |
commit | d779f5aead9e91b97c8d05136ddb7a6bd84f4276 (patch) | |
tree | a0f497e1109cc1ad89183f97aee193938bf5306e /dex | |
parent | d5e4399839e04f5d85dd3a06fb14d6ad117a79f3 (diff) | |
download | dexon-d779f5aead9e91b97c8d05136ddb7a6bd84f4276.tar.gz dexon-d779f5aead9e91b97c8d05136ddb7a6bd84f4276.tar.zst dexon-d779f5aead9e91b97c8d05136ddb7a6bd84f4276.zip |
core: Fixed gas price (#205)
* core/vm: update abi
* core/vm: add MinGasPrice to gov
* params: Add MinGasPrice to Config
* dex: SuggestPrice from Governance
* test: add minGasPrice to genesis.json
* core: check underpriced tx
* dex: verify with gas price
Diffstat (limited to 'dex')
-rw-r--r-- | dex/api_backend.go | 2 | ||||
-rw-r--r-- | dex/app.go | 16 | ||||
-rw-r--r-- | dex/app_test.go | 37 |
3 files changed, 48 insertions, 7 deletions
diff --git a/dex/api_backend.go b/dex/api_backend.go index 719625946..d423b41f7 100644 --- a/dex/api_backend.go +++ b/dex/api_backend.go @@ -195,7 +195,7 @@ func (b *DexAPIBackend) ProtocolVersion() int { } func (b *DexAPIBackend) SuggestPrice(ctx context.Context) (*big.Int, error) { - return b.gpo.SuggestPrice(ctx) + return b.dex.governance.MinGasPrice(b.dex.blockchain.CurrentBlock().Round()), nil } func (b *DexAPIBackend) ChainDb() ethdb.Database { diff --git a/dex/app.go b/dex/app.go index d2bd02f0c..ab4e80058 100644 --- a/dex/app.go +++ b/dex/app.go @@ -124,6 +124,18 @@ func (d *DexconApp) validateNonce(txs types.Transactions) (map[common.Address]ui return addressFirstNonce, nil } +// 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) + for _, tx := range txs { + if minGasPrice.Cmp(tx.GasPrice()) > 0 { + return false + } + } + return true +} + // PreparePayload is called when consensus core is preparing payload for block. func (d *DexconApp) PreparePayload(position coreTypes.Position) (payload []byte, err error) { // softLimit limits the runtime of inner call to preparePayload. @@ -410,6 +422,10 @@ func (d *DexconApp) VerifyBlock(block *coreTypes.Block) coreTypes.BlockVerifySta log.Error("Validate nonce failed", "error", err) return coreTypes.VerifyInvalidBlock } + if !d.validateGasPrice(transactions, block.Position.Round) { + log.Error("Validate gas price failed") + return coreTypes.VerifyInvalidBlock + } // Check if nonce is strictly increasing for every address. chainID := big.NewInt(int64(block.Position.ChainID)) diff --git a/dex/app_test.go b/dex/app_test.go index 09c61c11c..73d952f31 100644 --- a/dex/app_test.go +++ b/dex/app_test.go @@ -234,7 +234,7 @@ func TestVerifyBlock(t *testing.T) { common.BytesToAddress([]byte{9}), big.NewInt(50000000000000001), params.TxGas, - big.NewInt(1), + big.NewInt(10), nil) tx, err = types.SignTx(tx, signer, key) if err != nil { @@ -269,7 +269,7 @@ func TestVerifyBlock(t *testing.T) { common.BytesToAddress([]byte{9}), big.NewInt(1), params.TxGas, - big.NewInt(1), + big.NewInt(10), make([]byte, 1)) tx, err = types.SignTx(tx, signer, key) if err != nil { @@ -304,7 +304,7 @@ func TestVerifyBlock(t *testing.T) { common.BytesToAddress([]byte{9}), big.NewInt(1), params.TxGas, - big.NewInt(1), + big.NewInt(10), make([]byte, 1)) tx1, err = types.SignTx(tx, signer, key) if err != nil { @@ -317,7 +317,7 @@ func TestVerifyBlock(t *testing.T) { common.BytesToAddress([]byte{9}), big.NewInt(1), params.TxGas, - big.NewInt(1), + big.NewInt(10), make([]byte, 1)) tx2, err = types.SignTx(tx, signer, key) if err != nil { @@ -334,6 +334,30 @@ func TestVerifyBlock(t *testing.T) { if status != coreTypes.VerifyInvalidBlock { t.Fatalf("verify fail expect invalid block but get %v", status) } + + // Invalid gas price. + tx = types.NewTransaction( + 0, + common.BytesToAddress([]byte{9}), + big.NewInt(1), + params.TxGas, + big.NewInt(5), + make([]byte, 1)) + tx, err = types.SignTx(tx, signer, key) + if err != nil { + return + } + block.Payload, err = rlp.EncodeToBytes(types.Transactions{tx}) + if err != nil { + return + } + + // Expect invalid block. + status = dex.app.VerifyBlock(block) + if status != coreTypes.VerifyInvalidBlock { + t.Fatalf("verify fail expect invalid block but get %v", status) + } + } func TestBlockConfirmed(t *testing.T) { @@ -489,6 +513,7 @@ func newTestDexonWithGenesis(allocKey *ecdsa.PrivateKey) (*Dexon, error) { PublicKey: crypto.FromECDSAPub(&key.PublicKey), }, } + genesis.Config.Dexcon.MinGasPrice = big.NewInt(10) chainConfig, _, err := core.SetupGenesisBlock(db, genesis) if err != nil { return nil, err @@ -530,7 +555,7 @@ func addTx(dex *Dexon, nonce int, signer types.Signer, key *ecdsa.PrivateKey) ( common.BytesToAddress([]byte{9}), big.NewInt(int64(nonce*1)), params.TxGas, - big.NewInt(1), + big.NewInt(10), nil) tx, err := types.SignTx(tx, signer, key) if err != nil { @@ -638,7 +663,7 @@ func prepareDataWithoutTxPool(dex *Dexon, key *ecdsa.PrivateKey, startNonce, txN common.BytesToAddress([]byte{9}), big.NewInt(int64(n*1)), params.TxGas, - big.NewInt(1), + big.NewInt(10), nil) tx, err = types.SignTx(tx, signer, key) if err != nil { |