diff options
author | Bojie Wu <bojie@dexon.org> | 2018-10-12 13:50:17 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:49 +0800 |
commit | 7df00d48a6c8ad89de221c5d9a3606e2b39d55a3 (patch) | |
tree | 0f6231c7657b0d07a8f0002c5885783827e816c2 /core/blockchain.go | |
parent | efa16f95fce18659532cf650e4ce0b46ec9f9245 (diff) | |
download | dexon-7df00d48a6c8ad89de221c5d9a3606e2b39d55a3.tar dexon-7df00d48a6c8ad89de221c5d9a3606e2b39d55a3.tar.gz dexon-7df00d48a6c8ad89de221c5d9a3606e2b39d55a3.tar.bz2 dexon-7df00d48a6c8ad89de221c5d9a3606e2b39d55a3.tar.lz dexon-7df00d48a6c8ad89de221c5d9a3606e2b39d55a3.tar.xz dexon-7df00d48a6c8ad89de221c5d9a3606e2b39d55a3.tar.zst dexon-7df00d48a6c8ad89de221c5d9a3606e2b39d55a3.zip |
app: implement new interface method
Diffstat (limited to 'core/blockchain.go')
-rw-r--r-- | core/blockchain.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/core/blockchain.go b/core/blockchain.go index 41b8764f7..30516f7f6 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -18,6 +18,7 @@ package core import ( + "bytes" "errors" "fmt" "io" @@ -27,6 +28,9 @@ import ( "sync/atomic" "time" + coreCommon "github.com/dexon-foundation/dexon-consensus-core/common" + coreTypes "github.com/dexon-foundation/dexon-consensus-core/core/types" + "github.com/dexon-foundation/dexon/common" "github.com/dexon-foundation/dexon/common/mclock" "github.com/dexon-foundation/dexon/common/prque" @@ -136,6 +140,10 @@ type BlockChain struct { badBlocks *lru.Cache // Bad block cache shouldPreserve func(*types.Block) bool // Function used to determine whether should preserve the given block. + + confirmedBlockMu sync.Mutex + confirmedBlock map[coreCommon.Hash]*coreTypes.Block + filteredConfirmedBlock map[uint32]map[coreCommon.Hash]*coreTypes.Block } // NewBlockChain returns a fully initialised block chain using information @@ -215,6 +223,54 @@ func (bc *BlockChain) GetVMConfig() *vm.Config { return &bc.vmConfig } +func (bc *BlockChain) AddConfirmedBlock(block *coreTypes.Block) { + bc.confirmedBlockMu.Lock() + defer bc.confirmedBlockMu.Unlock() + + bc.confirmedBlock[block.Hash] = block + bc.filteredConfirmedBlock[block.Position.ChainID][block.Hash] = block +} + +func (bc *BlockChain) RemoveConfirmedBlock(hash coreCommon.Hash) { + bc.confirmedBlockMu.Lock() + defer bc.confirmedBlockMu.Unlock() + + block := bc.confirmedBlock[hash] + delete(bc.filteredConfirmedBlock[block.Position.ChainID], block.Hash) + delete(bc.confirmedBlock, block.Hash) +} + +func (bc *BlockChain) GetConfirmedBlockByHash(hash coreCommon.Hash) *coreTypes.Block { + return bc.confirmedBlock[hash] +} + +func (bc *BlockChain) GetNonceInConfirmedBlock(chainID uint32, address common.Address) (uint64, bool, error) { + var nonce uint64 + var init bool + for _, block := range bc.filteredConfirmedBlock[chainID] { + var transactions types.Transactions + err := rlp.Decode(bytes.NewReader(block.Payload), &transactions) + if err != nil { + return 0, init, err + } + + for _, tx := range transactions { + msg, err := tx.AsMessage(types.MakeSigner(bc.chainConfig, new(big.Int))) + if err != nil { + return 0, init, err + } + + if msg.From() == address && (tx.Nonce() > nonce || !init) { + if !init { + init = true + } + nonce = tx.Nonce() + } + } + } + return nonce, init, nil +} + // loadLastState loads the last known chain state from the database. This method // assumes that the chain manager mutex is held. func (bc *BlockChain) loadLastState() error { |