diff options
author | Bojie Wu <bojie@dexon.org> | 2018-10-09 13:28:45 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:50 +0800 |
commit | 0cf107b225f25602527ddda3f1897f182ebb205a (patch) | |
tree | 536f6c1f1656cd71e337592d4be3ae5f71bbd033 /core | |
parent | 72342301371f9f4d7a9fe256efa072c5c41d00d3 (diff) | |
download | dexon-0cf107b225f25602527ddda3f1897f182ebb205a.tar dexon-0cf107b225f25602527ddda3f1897f182ebb205a.tar.gz dexon-0cf107b225f25602527ddda3f1897f182ebb205a.tar.bz2 dexon-0cf107b225f25602527ddda3f1897f182ebb205a.tar.lz dexon-0cf107b225f25602527ddda3f1897f182ebb205a.tar.xz dexon-0cf107b225f25602527ddda3f1897f182ebb205a.tar.zst dexon-0cf107b225f25602527ddda3f1897f182ebb205a.zip |
app: implement verify block logic
Diffstat (limited to 'core')
-rw-r--r-- | core/blockchain.go | 50 | ||||
-rw-r--r-- | core/types/block.go | 2 |
2 files changed, 51 insertions, 1 deletions
diff --git a/core/blockchain.go b/core/blockchain.go index b66c0928f..e59c36653 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -254,6 +254,9 @@ func (bc *BlockChain) RemoveConfirmedBlock(hash coreCommon.Hash) { chainBlocks := bc.chainConfirmedBlocks[block.Position.ChainID] bc.chainConfirmedBlocks[block.Position.ChainID] = chainBlocks[1:] + if len(bc.chainConfirmedBlocks[block.Position.ChainID]) == 0 { + delete(bc.chainConfirmedBlocks, block.Position.ChainID) + } } func (bc *BlockChain) GetConfirmedBlockByHash(hash coreCommon.Hash) *coreTypes.Block { @@ -283,6 +286,51 @@ func (bc *BlockChain) GetConfirmedTxsByAddress(chainID uint32, address common.Ad return addressTxs, nil } +func (bc *BlockChain) GetLastNonceFromConfirmedBlocks(chainID uint32, address common.Address) (uint64, bool, error) { + chainBlocks, exist := bc.chainConfirmedBlocks[chainID] + if !exist { + return 0, true, nil + } + + for i := len(chainBlocks) - 1; i >= 0; i-- { + var transactions types.Transactions + err := rlp.Decode(bytes.NewReader(chainBlocks[i].Payload), &transactions) + if err != nil { + return 0, true, err + } + + for _, tx := range transactions { + msg, err := tx.AsMessage(types.MakeSigner(bc.chainConfig, new(big.Int))) + if err != nil { + return 0, true, err + } + + if msg.From() == address { + return msg.Nonce(), false, nil + } + } + } + + return 0, true, nil +} + +func (bc *BlockChain) GetChainLastConfirmedHeight(chainID uint32) (uint64, bool) { + bc.confirmedBlockMu.Lock() + defer bc.confirmedBlockMu.Unlock() + + chainBlocks := bc.chainConfirmedBlocks[chainID] + size := len(chainBlocks) + if size == 0 { + return 0, true + } + + return chainBlocks[size-1].Position.Height, false +} + +func (bc *BlockChain) GetConfirmedBlocksByChainID(chainID uint32) []*coreTypes.Block { + return bc.chainConfirmedBlocks[chainID] +} + // 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 { @@ -1459,7 +1507,7 @@ func (bc *BlockChain) insertSidechain(block *types.Block, it *insertIterator) (i return 0, nil, nil, nil } -func (bc *BlockChain) InsertPendingBlock(chain types.Blocks) (int, error) { +func (bc *BlockChain) InsertPendingBlocks(chain types.Blocks) (int, error) { n, events, logs, err := bc.insertPendingBlocks(chain) bc.PostChainEvents(events, logs) return n, err diff --git a/core/types/block.go b/core/types/block.go index b814bd975..02d52f30f 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -90,6 +90,8 @@ type Header struct { WitnessHeight uint64 `json:"witnessHeight" gencodec:"required"` WitnessRoot common.Hash `json:"WitnessRoot" gencodec:"required"` WitnessReceiptHash common.Hash `json:"WitnessReceiptHash" gencodec:"required"` + ChainID uint32 `json:"chainID" gencodec:"required"` + ChainBlockHeight uint64 `json:"chainBlockHeight" gencodec:"required"` } // field type overrides for gencodec |