diff options
author | Wei-Ning Huang <w@dexon.org> | 2018-11-01 18:01:22 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-06-12 17:27:17 +0800 |
commit | 05cdd58111500b1b40caa9feffbea6c58f277a48 (patch) | |
tree | ff7c0b521b29a558a23f8fe8735502cb4810d00b | |
parent | 7cd4ecd047ee297e10753ff642ad4893418ace48 (diff) | |
download | go-tangerine-05cdd58111500b1b40caa9feffbea6c58f277a48.tar.gz go-tangerine-05cdd58111500b1b40caa9feffbea6c58f277a48.tar.zst go-tangerine-05cdd58111500b1b40caa9feffbea6c58f277a48.zip |
dex: minor refactor
-rw-r--r-- | core/blockchain.go | 11 | ||||
-rw-r--r-- | dex/app.go | 30 | ||||
-rwxr-xr-x | test/run_test.sh | 2 |
3 files changed, 24 insertions, 19 deletions
diff --git a/core/blockchain.go b/core/blockchain.go index 1a60805a1..8b5670081 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -244,7 +244,7 @@ func (bc *BlockChain) GetVMConfig() *vm.Config { } type blockInfo struct { - addresses map[common.Address]interface{} + addresses map[common.Address]struct{} block *coreTypes.Block } @@ -258,23 +258,22 @@ func (bc *BlockChain) AddConfirmedBlock(block *coreTypes.Block) error { return err } - addressMap := map[common.Address]interface{}{} + addressMap := map[common.Address]struct{}{} for _, tx := range transactions { msg, err := tx.AsMessage(types.MakeSigner(bc.Config(), new(big.Int))) if err != nil { return err } - addressMap[msg.From()] = nil + addressMap[msg.From()] = struct{}{} // get latest nonce in block bc.addressNonce[msg.From()] = msg.Nonce() // calculate max cost in confirmed blocks if bc.addressCost[msg.From()] == nil { - bc.addressCost[msg.From()] = tx.Cost() - } else { - bc.addressCost[msg.From()] = new(big.Int).Add(bc.addressCost[msg.From()], tx.Cost()) + bc.addressCost[msg.From()] = big.NewInt(0) } + bc.addressCost[msg.From()] = new(big.Int).Add(bc.addressCost[msg.From()], tx.Cost()) } for addr := range addressMap { diff --git a/dex/app.go b/dex/app.go index e3685205f..a3bc9f4c5 100644 --- a/dex/app.go +++ b/dex/app.go @@ -18,7 +18,6 @@ package dex import ( - "bytes" "fmt" "math/big" "sync" @@ -250,14 +249,16 @@ func (d *DexconApp) PrepareWitness(consensusHeight uint64) (witness coreTypes.Wi // VerifyBlock verifies if the payloads are valid. func (d *DexconApp) VerifyBlock(block *coreTypes.Block) coreTypes.BlockVerifyStatus { + const retries = 6 + var witnessData witnessData - err := rlp.Decode(bytes.NewReader(block.Witness.Data), &witnessData) + err := rlp.DecodeBytes(block.Witness.Data, &witnessData) if err != nil { log.Error("Witness rlp decode", "error", err) return coreTypes.VerifyInvalidBlock } - for i := 0; i < 6 && err != nil; i++ { + for i := 0; i < retries && err != nil; i++ { // check witness root exist err = nil _, err = d.blockchain.StateAt(witnessData.Root) @@ -266,8 +267,9 @@ func (d *DexconApp) VerifyBlock(block *coreTypes.Block) coreTypes.BlockVerifySta time.Sleep(500 * time.Millisecond) } } + if err != nil { - log.Error("Expect witness root not in stateDB", "err", err) + log.Error("Expected witness root not in stateDB", "err", err) return coreTypes.VerifyRetryLater } @@ -278,7 +280,8 @@ func (d *DexconApp) VerifyBlock(block *coreTypes.Block) coreTypes.BlockVerifySta // check if chain block height is sequential chainLastHeight := d.blockchain.GetChainLastConfirmedHeight(block.Position.ChainID) if chainLastHeight != block.Position.Height-1 { - log.Error("Check confirmed block height fail", "chain", block.Position.ChainID, "height", block.Position.Height-1, "cache height", chainLastHeight) + log.Error("Check confirmed block height fail", "chain", block.Position.ChainID, + "height", block.Position.Height-1, "cache height", chainLastHeight) return coreTypes.VerifyRetryLater } } @@ -288,6 +291,7 @@ func (d *DexconApp) VerifyBlock(block *coreTypes.Block) coreTypes.BlockVerifySta currentRoot := d.blockchain.CurrentBlock().Root() root = ¤tRoot } + // set state to the chain latest height latestState, err := d.blockchain.StateAt(*root) if err != nil { @@ -296,7 +300,7 @@ func (d *DexconApp) VerifyBlock(block *coreTypes.Block) coreTypes.BlockVerifySta } var transactions types.Transactions - err = rlp.Decode(bytes.NewReader(block.Payload), &transactions) + err = rlp.DecodeBytes(block.Payload, &transactions) if err != nil { log.Error("Payload rlp decode", "error", err) return coreTypes.VerifyInvalidBlock @@ -347,7 +351,8 @@ func (d *DexconApp) VerifyBlock(block *coreTypes.Block) coreTypes.BlockVerifySta } // validate tx to check available balance - blockGasLimit := new(big.Int).SetUint64(core.CalcGasLimit(d.blockchain.CurrentBlock(), d.config.GasFloor, d.config.GasCeil)) + blockGasLimit := new(big.Int).SetUint64(core.CalcGasLimit( + d.blockchain.CurrentBlock(), d.config.GasFloor, d.config.GasCeil)) blockGasUsed := new(big.Int) for _, tx := range transactions { msg, err := tx.AsMessage(types.MakeSigner(d.blockchain.Config(), new(big.Int))) @@ -363,7 +368,8 @@ func (d *DexconApp) VerifyBlock(block *coreTypes.Block) coreTypes.BlockVerifySta return coreTypes.VerifyInvalidBlock } if big.NewInt(int64(intrinsicGas)).Cmp(maxGasUsed) > 0 { - log.Error("Intrinsic gas is larger than (gas limit * gas price)", "intrinsic", intrinsicGas, "maxGasUsed", maxGasUsed) + log.Error("Intrinsic gas is larger than (gas limit * gas price)", + "intrinsic", intrinsicGas, "maxGasUsed", maxGasUsed) return coreTypes.VerifyInvalidBlock } @@ -399,7 +405,7 @@ func (d *DexconApp) BlockDelivered(blockHash coreCommon.Hash, result coreTypes.F defer d.chainUnlock(block.Position.ChainID) var transactions types.Transactions - err := rlp.Decode(bytes.NewReader(block.Payload), &transactions) + err := rlp.DecodeBytes(block.Payload, &transactions) if err != nil { log.Error("Payload rlp decode failed", "error", err) panic(err) @@ -445,6 +451,7 @@ func (d *DexconApp) BlockConfirmed(block coreTypes.Block) { func (d *DexconApp) validateNonce(txs types.Transactions) (map[common.Address]uint64, error) { addressFirstNonce := map[common.Address]uint64{} addressNonce := map[common.Address]uint64{} + for _, tx := range txs { msg, err := tx.AsMessage(types.MakeSigner(d.blockchain.Config(), new(big.Int))) if err != nil { @@ -453,16 +460,15 @@ func (d *DexconApp) validateNonce(txs types.Transactions) (map[common.Address]ui if _, exist := addressFirstNonce[msg.From()]; exist { if addressNonce[msg.From()]+1 != msg.Nonce() { - return nil, fmt.Errorf("address nonce check error: expect %v actual %v", addressNonce[msg.From()]+1, msg.Nonce()) + return nil, fmt.Errorf("address nonce check error: expect %v actual %v", + addressNonce[msg.From()]+1, msg.Nonce()) } addressNonce[msg.From()] = msg.Nonce() - continue } else { addressNonce[msg.From()] = msg.Nonce() addressFirstNonce[msg.From()] = msg.Nonce() } } - return addressFirstNonce, nil } diff --git a/test/run_test.sh b/test/run_test.sh index ddbe30b4a..ca2c391a0 100755 --- a/test/run_test.sh +++ b/test/run_test.sh @@ -10,7 +10,7 @@ for i in $(seq 0 3); do datadir=$PWD/Dexon.$i rm -rf $datadir $GDEX --datadir=$datadir init genesis.json - $GDEX --verbosity=4 --datadir=$datadir --nodekey=test$i.nodekey --port=$((28000 + $i)) --rpc --rpcaddr 127.0.0.1 --rpcport=$((8545 + $i)) --rpcapi=eth,net,web3,debug --rpcvhosts='*' --rpccorsdomain="http://localhost:8000,https://www.myetherwallet.com" > gdex.$i.log 2>&1 & + $GDEX --verbosity=4 --gcmode=archive --datadir=$datadir --nodekey=test$i.nodekey --port=$((28000 + $i)) --rpc --rpcaddr=0.0.0.0 --rpcport=$((8545 + $i * 2)) --rpcapi=eth,net,web3,debug --ws --wsapi=eth,net,web3,debug --wsaddr=0.0.0.0 --wsport=$((8546 + $i * 2)) --wsorigins='*' --rpcvhosts='*' --rpccorsdomain="*" > gdex.$i.log 2>&1 & done tail -f gdex.*.log |