-
Notifications
You must be signed in to change notification settings - Fork 144
chore(dot/network, lib/grandpa): update network.ConsensusMessage, add grandpa.NeighbourMessage and handle accordingly #1519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e6b4efd
60246a2
a475b73
f976f1e
87b1f17
bf7072f
3915d03
dce28d6
223f1aa
b006e57
9b805ab
b4243ef
5388e0e
14e6c4a
8256bcb
d6b192c
bf13265
d3e30e1
a3620c0
c267ccb
9e8189f
41fe922
4b5d23b
9a6e937
d02b174
8708374
2d50d14
21f464f
28cab3e
004b5f6
83b5dfd
7622914
6d108d8
8a7a145
6d45402
2012c33
1c0a7ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,10 +38,10 @@ const pruneKeyBufferSize = 1000 | |
|
||
// BlockState defines fields for manipulating the state of blocks, such as BlockTree, BlockDB and Header | ||
type BlockState struct { | ||
bt *blocktree.BlockTree | ||
baseDB chaindb.Database | ||
db chaindb.Database | ||
lock sync.RWMutex | ||
bt *blocktree.BlockTree | ||
baseDB chaindb.Database | ||
db chaindb.Database | ||
sync.RWMutex | ||
genesisHash common.Hash | ||
|
||
// block notifiers | ||
|
@@ -268,7 +268,7 @@ func (bs *BlockState) GetHeader(hash common.Hash) (*types.Header, error) { | |
func (bs *BlockState) GetHashByNumber(num *big.Int) (common.Hash, error) { | ||
bh, err := bs.db.Get(headerHashKey(num.Uint64())) | ||
if err != nil { | ||
return common.Hash{}, fmt.Errorf("cannot get block %d: %s", num, err) | ||
return common.Hash{}, fmt.Errorf("cannot get block %d: %w", num, err) | ||
} | ||
|
||
return common.NewHash(bh), nil | ||
|
@@ -278,7 +278,7 @@ func (bs *BlockState) GetHashByNumber(num *big.Int) (common.Hash, error) { | |
func (bs *BlockState) GetHeaderByNumber(num *big.Int) (*types.Header, error) { | ||
bh, err := bs.db.Get(headerHashKey(num.Uint64())) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot get block %d: %s", num, err) | ||
return nil, fmt.Errorf("cannot get block %d: %w", num, err) | ||
} | ||
|
||
hash := common.NewHash(bh) | ||
|
@@ -304,7 +304,7 @@ func (bs *BlockState) GetBlockByNumber(num *big.Int) (*types.Block, error) { | |
// First retrieve the block hash in a byte array based on the block number from the database | ||
byteHash, err := bs.db.Get(headerHashKey(num.Uint64())) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot get block %d: %s", num, err) | ||
return nil, fmt.Errorf("cannot get block %d: %w", num, err) | ||
} | ||
|
||
// Then find the block based on the hash | ||
|
@@ -322,17 +322,14 @@ func (bs *BlockState) GetBlockHash(blockNumber *big.Int) (*common.Hash, error) { | |
// First retrieve the block hash in a byte array based on the block number from the database | ||
byteHash, err := bs.db.Get(headerHashKey(blockNumber.Uint64())) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot get block %d: %s", blockNumber, err) | ||
return nil, fmt.Errorf("cannot get block %d: %w", blockNumber, err) | ||
} | ||
hash := common.NewHash(byteHash) | ||
return &hash, nil | ||
} | ||
|
||
// SetHeader will set the header into DB | ||
func (bs *BlockState) SetHeader(header *types.Header) error { | ||
bs.lock.Lock() | ||
defer bs.lock.Unlock() | ||
|
||
hash := header.Hash() | ||
|
||
// Write the encoded header | ||
|
@@ -366,11 +363,7 @@ func (bs *BlockState) GetBlockBody(hash common.Hash) (*types.Body, error) { | |
|
||
// SetBlockBody will add a block body to the db | ||
func (bs *BlockState) SetBlockBody(hash common.Hash, body *types.Body) error { | ||
bs.lock.Lock() | ||
defer bs.lock.Unlock() | ||
Comment on lines
-369
to
-370
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a confused why some locks were removed (lines 333, and 369), but others added in different spots (add at line 423, 495 and 593). Doesn't seem to have any race conditions now, so this is working. How do we determine where locks are needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the db operations are already threadsafe (chaindb has locks for all db.Put, db.Get, etc.), and same for the blocktree, so I only added locks for functions that did multiple operations in them where some variables need to not change until the function is done, for example AddBlock requires no other blocks to be added until it's finished |
||
|
||
err := bs.db.Put(blockBodyKey(hash), body.AsOptional().Value()) | ||
return err | ||
return bs.db.Put(blockBodyKey(hash), body.AsOptional().Value()) | ||
} | ||
|
||
// HasFinalizedBlock returns true if there is a finalized block for a given round and setID, false otherwise | ||
|
@@ -427,6 +420,9 @@ func (bs *BlockState) GetFinalizedHash(round, setID uint64) (common.Hash, error) | |
|
||
// SetFinalizedHash sets the latest finalized block header | ||
func (bs *BlockState) SetFinalizedHash(hash common.Hash, round, setID uint64) error { | ||
bs.Lock() | ||
defer bs.Unlock() | ||
|
||
go bs.notifyFinalized(hash) | ||
if round > 0 { | ||
err := bs.SetRound(round) | ||
|
@@ -496,6 +492,8 @@ func (bs *BlockState) CompareAndSetBlockData(bd *types.BlockData) error { | |
|
||
// AddBlock adds a block to the blocktree and the DB with arrival time as current unix time | ||
func (bs *BlockState) AddBlock(block *types.Block) error { | ||
bs.Lock() | ||
defer bs.Unlock() | ||
return bs.AddBlockWithArrivalTime(block, time.Now()) | ||
} | ||
|
||
|
@@ -506,6 +504,8 @@ func (bs *BlockState) AddBlockWithArrivalTime(block *types.Block, arrivalTime ti | |
return err | ||
} | ||
|
||
prevHead := bs.bt.DeepestBlockHash() | ||
|
||
// add block to blocktree | ||
err = bs.bt.AddBlock(block.Header, uint64(arrivalTime.UnixNano())) | ||
if err != nil { | ||
|
@@ -541,12 +541,58 @@ func (bs *BlockState) AddBlockWithArrivalTime(block *types.Block, arrivalTime ti | |
return err | ||
} | ||
|
||
// check if there was a re-org, if so, re-set the canonical number->hash mapping | ||
err = bs.handleAddedBlock(prevHead, bs.bt.DeepestBlockHash()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
go bs.notifyImported(block) | ||
return bs.baseDB.Flush() | ||
} | ||
|
||
// handleAddedBlock re-sets the canonical number->hash mapping if there was a chain re-org. | ||
// prev is the previous best block hash before the new block was added to the blocktree. | ||
// curr is the current best blogetck hash. | ||
func (bs *BlockState) handleAddedBlock(prev, curr common.Hash) error { | ||
ancestor, err := bs.HighestCommonAncestor(prev, curr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// if the highest common ancestor of the previous chain head and current chain head is the previous chain head, | ||
// then the current chain head is the descendant of the previous and thus are on the same chain | ||
if ancestor == prev { | ||
return nil | ||
} | ||
|
||
subchain, err := bs.SubChain(ancestor, curr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
batch := bs.db.NewBatch() | ||
for _, hash := range subchain { | ||
// TODO: set number from ancestor.Number + i ? | ||
header, err := bs.GetHeader(hash) | ||
if err != nil { | ||
return fmt.Errorf("failed to get header in subchain: %w", err) | ||
} | ||
|
||
err = batch.Put(headerHashKey(header.Number.Uint64()), hash.ToBytes()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return batch.Flush() | ||
} | ||
|
||
// AddBlockToBlockTree adds the given block to the blocktree. It does not write it to the database. | ||
func (bs *BlockState) AddBlockToBlockTree(header *types.Header) error { | ||
bs.Lock() | ||
defer bs.Unlock() | ||
|
||
arrivalTime, err := bs.GetArrivalTime(header.Hash()) | ||
if err != nil { | ||
arrivalTime = time.Now() | ||
|
@@ -567,7 +613,7 @@ func (bs *BlockState) isBlockOnCurrentChain(header *types.Header) (bool, error) | |
} | ||
|
||
// if the new block is ahead of our best block, then it is on our current chain. | ||
if header.Number.Cmp(bestBlock.Number) == 1 { | ||
if header.Number.Cmp(bestBlock.Number) > 0 { | ||
return true, nil | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.