diff options
author | Wei-Ning Huang <aitjcize@gmail.com> | 2018-11-13 15:26:21 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-03-12 12:19:09 +0800 |
commit | 91a4a2d552a13dfbd7f58bbfa2a9dd7215d72ea0 (patch) | |
tree | 2bd2bae2f1ccb52b3be644b4d5400db14ee3c527 | |
parent | 143a9b6e1d2fc7c9360bbc5c0bdd8b715ce80191 (diff) | |
download | dexon-91a4a2d552a13dfbd7f58bbfa2a9dd7215d72ea0.tar dexon-91a4a2d552a13dfbd7f58bbfa2a9dd7215d72ea0.tar.gz dexon-91a4a2d552a13dfbd7f58bbfa2a9dd7215d72ea0.tar.bz2 dexon-91a4a2d552a13dfbd7f58bbfa2a9dd7215d72ea0.tar.lz dexon-91a4a2d552a13dfbd7f58bbfa2a9dd7215d72ea0.tar.xz dexon-91a4a2d552a13dfbd7f58bbfa2a9dd7215d72ea0.tar.zst dexon-91a4a2d552a13dfbd7f58bbfa2a9dd7215d72ea0.zip |
dex: return correct pending nonce (#14)
We need to return the correct pending nonce (include those in the tx
pool). Also, StateAndHeaderByNumber is also fixed to use pending block.
-rw-r--r-- | core/blockchain.go | 13 | ||||
-rw-r--r-- | dex/api_backend.go | 3 | ||||
-rw-r--r-- | dex/app.go | 6 |
3 files changed, 16 insertions, 6 deletions
diff --git a/core/blockchain.go b/core/blockchain.go index 606927c77..19b17db8f 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1779,20 +1779,29 @@ func (bc *BlockChain) addPendingBlock(block *types.Block, receipts types.Receipt bc.lastPendingHeight = block.NumberU64() } -func (bc *BlockChain) GetLastPendingHeight() uint64 { +func (bc *BlockChain) GetPendingHeight() uint64 { bc.pendingBlockMu.RLock() defer bc.pendingBlockMu.RUnlock() return bc.lastPendingHeight } -func (bc *BlockChain) GetLastPendingBlock() *types.Block { +func (bc *BlockChain) GetPendingBlock() *types.Block { bc.pendingBlockMu.RLock() defer bc.pendingBlockMu.RUnlock() return bc.pendingBlocks[bc.lastPendingHeight].block } +func (bc *BlockChain) GetPending() (*types.Block, *state.StateDB) { + block := bc.GetPendingBlock() + s, err := state.New(block.Header().Root, bc.stateCache) + if err != nil { + panic(err) + } + return block, s +} + // reorgs takes two blocks, an old chain and a new chain and will reconstruct the blocks and inserts them // to be part of the new canonical chain and accumulates potential missing transactions and post an // event about them diff --git a/dex/api_backend.go b/dex/api_backend.go index 411855bf3..172b4ec44 100644 --- a/dex/api_backend.go +++ b/dex/api_backend.go @@ -80,7 +80,8 @@ func (b *DexAPIBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumb func (b *DexAPIBackend) StateAndHeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*state.StateDB, *types.Header, error) { if blockNr == rpc.PendingBlockNumber { - blockNr = rpc.BlockNumber(b.dex.blockchain.CurrentBlock().Header().Number.Uint64()) + block, state := b.dex.BlockChain().GetPending() + return state, block.Header(), nil } header, err := b.HeaderByNumber(ctx, blockNr) if header == nil || err != nil { diff --git a/dex/app.go b/dex/app.go index d0d911e86..a90dfae37 100644 --- a/dex/app.go +++ b/dex/app.go @@ -275,13 +275,13 @@ addressMap: // PrepareWitness will return the witness data no lower than consensusHeight. func (d *DexconApp) PrepareWitness(consensusHeight uint64) (witness coreTypes.Witness, err error) { var witnessBlock *types.Block - lastPendingHeight := d.blockchain.GetLastPendingHeight() + lastPendingHeight := d.blockchain.GetPendingHeight() if lastPendingHeight == 0 && consensusHeight == 0 { witnessBlock = d.blockchain.CurrentBlock() } else if lastPendingHeight >= consensusHeight { - witnessBlock = d.blockchain.GetLastPendingBlock() + witnessBlock = d.blockchain.GetPendingBlock() } else if h := <-d.addNotify(consensusHeight); h >= consensusHeight { - witnessBlock = d.blockchain.GetLastPendingBlock() + witnessBlock = d.blockchain.GetPendingBlock() } else { log.Error("need pending block") return witness, fmt.Errorf("need pending block") |