aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorBojie Wu <bojie@dexon.org>2018-10-09 13:28:45 +0800
committerWei-Ning Huang <w@dexon.org>2018-12-19 20:54:27 +0800
commitde30ca37c261d7744bca8c495c59360d751c29b5 (patch)
tree69808aa7cce9b68fd8f74cebb71b177ec6b3b985 /core
parent787c731b94179f85cbdced946d6cf9f42df8f280 (diff)
downloaddexon-de30ca37c261d7744bca8c495c59360d751c29b5.tar
dexon-de30ca37c261d7744bca8c495c59360d751c29b5.tar.gz
dexon-de30ca37c261d7744bca8c495c59360d751c29b5.tar.bz2
dexon-de30ca37c261d7744bca8c495c59360d751c29b5.tar.lz
dexon-de30ca37c261d7744bca8c495c59360d751c29b5.tar.xz
dexon-de30ca37c261d7744bca8c495c59360d751c29b5.tar.zst
dexon-de30ca37c261d7744bca8c495c59360d751c29b5.zip
app: implement verify block logic
Diffstat (limited to 'core')
-rw-r--r--core/blockchain.go50
-rw-r--r--core/types/block.go2
2 files changed, 51 insertions, 1 deletions
diff --git a/core/blockchain.go b/core/blockchain.go
index 9ddb8186f..20b6a341e 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 {
@@ -1453,7 +1501,7 @@ func (bc *BlockChain) insertSidechain(it *insertIterator) (int, []interface{}, [
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 454273dbc..069221b82 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