-
Notifications
You must be signed in to change notification settings - Fork 146
fix(dot/state, lib/grandpa): update justification and SignedVote handling in database #1682
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 16 commits
41b2226
f364251
819fcda
fe5c1c5
86cb466
de75114
30dc258
9c317b4
8e741eb
21365e5
b93945b
c40a58e
8b13356
e8f712b
3afb196
95cdc66
2e36d72
38e0262
5cab5a0
f4a23c0
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 |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package state | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math/big" | ||
|
|
||
| "github.com/ChainSafe/gossamer/dot/types" | ||
| "github.com/ChainSafe/gossamer/lib/common" | ||
| ) | ||
|
|
||
| // finalizedHashKey = FinalizedBlockHashKey + round + setID (LE encoded) | ||
| func finalizedHashKey(round, setID uint64) []byte { | ||
| return append(common.FinalizedBlockHashKey, roundSetIDKey(round, setID)...) | ||
| } | ||
|
|
||
| // HasFinalizedBlock returns true if there is a finalised block for a given round and setID, false otherwise | ||
| func (bs *BlockState) HasFinalizedBlock(round, setID uint64) (bool, error) { | ||
| return bs.db.Has(finalizedHashKey(round, setID)) | ||
| } | ||
|
|
||
| // NumberIsFinalised checks if a block number is finalised or not | ||
| func (bs *BlockState) NumberIsFinalised(num *big.Int) (bool, error) { | ||
|
Contributor
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. are we finalising on finalize or finalise?
Contributor
Author
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. substrate uses
Contributor
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. should we make the code consistent then?
Contributor
Author
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. what do you mean? it should use
Contributor
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. Should there be an issue created to refactor existing
Contributor
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 two functions above use
Contributor
Author
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. updated |
||
| header, err := bs.GetFinalizedHeader(0, 0) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| return num.Cmp(header.Number) <= 0, nil | ||
| } | ||
|
|
||
| // GetFinalizedHeader returns the finalised block header by round and setID | ||
| func (bs *BlockState) GetFinalizedHeader(round, setID uint64) (*types.Header, error) { | ||
| h, err := bs.GetFinalizedHash(round, setID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| header, err := bs.GetHeader(h) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return header, nil | ||
| } | ||
|
|
||
| // GetFinalizedHash gets the finalised block header by round and setID | ||
| func (bs *BlockState) GetFinalizedHash(round, setID uint64) (common.Hash, error) { | ||
| h, err := bs.db.Get(finalizedHashKey(round, setID)) | ||
| if err != nil { | ||
| return common.Hash{}, err | ||
| } | ||
|
|
||
| return common.NewHash(h), nil | ||
| } | ||
|
|
||
| // SetFinalizedHash sets the latest finalised block header | ||
| // Note that using round=0 and setID=0 would refer to the latest finalised hash | ||
| func (bs *BlockState) SetFinalizedHash(hash common.Hash, round, setID uint64) error { | ||
| bs.Lock() | ||
| defer bs.Unlock() | ||
|
|
||
| has, _ := bs.HasHeader(hash) | ||
| if !has { | ||
| return fmt.Errorf("cannot finalise unknown block %s", hash) | ||
| } | ||
|
|
||
| // if nothing was previously finalised, set the first slot of the network to the | ||
| // slot number of block 1, which is now being set as final | ||
| if bs.lastFinalised.Equal(bs.genesisHash) && !hash.Equal(bs.genesisHash) { | ||
| err := bs.setFirstSlotOnFinalisation() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if round > 0 { | ||
| bs.notifyFinalized(hash, round, setID) | ||
| } | ||
|
|
||
| pruned := bs.bt.Prune(hash) | ||
| for _, rem := range pruned { | ||
| header, err := bs.GetHeader(rem) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = bs.DeleteBlock(rem) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| logger.Trace("pruned block", "hash", rem, "number", header.Number) | ||
| bs.pruneKeyCh <- header | ||
|
Contributor
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. this could block if
Contributor
Author
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 storage state just deletes a key from the map on prune, so generally I don't think it would lag. how would you implement a timeout for this?
Contributor
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. May be a bit overkill for this case, but that's how I would do it.
Contributor
Author
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 code is writing to the channel, and your code is reading from it? is there a way to do a timeout when writing? either way I don't think it's too necessary for this case |
||
| } | ||
|
|
||
| bs.lastFinalised = hash | ||
| return bs.db.Put(finalizedHashKey(round, setID), hash[:]) | ||
| } | ||
|
|
||
| func (bs *BlockState) setFirstSlotOnFinalisation() error { | ||
| header, err := bs.GetHeaderByNumber(big.NewInt(1)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| slot, err := types.GetSlotFromHeader(header) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return bs.baseState.storeFirstSlot(slot) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: add copyright notice