Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
noot committed Jul 29, 2021
1 parent 7c0c25a commit 77a5ce0
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 63 deletions.
9 changes: 4 additions & 5 deletions dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,13 @@ func (bs *BlockState) GetBlockByNumber(num *big.Int) (*types.Block, error) {
}

// GetBlockHash returns block hash for a given blockNumber
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
func (bs *BlockState) GetBlockHash(blockNumber *big.Int) (common.Hash, error) {
byteHash, err := bs.db.Get(headerHashKey(blockNumber.Uint64()))
if err != nil {
return nil, fmt.Errorf("cannot get block %d: %w", blockNumber, err)
return common.Hash{}, fmt.Errorf("cannot get block %d: %w", blockNumber, err)
}
hash := common.NewHash(byteHash)
return &hash, nil

return common.NewHash(byteHash), nil
}

// SetHeader will set the header into DB
Expand Down
3 changes: 0 additions & 3 deletions lib/grandpa/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ var (
// ErrPrecommitBlockMismatch is returned when a precommit hash within a justification is not a descendant of the committed block
ErrPrecommitBlockMismatch = errors.New("precommit block is not descendant of committed block")

// ErrJustificationNumberMismatch is returned when a precommit number within a justification does not match the justification number
ErrJustificationNumberMismatch = errors.New("precommit number does not match justification number")

// ErrAuthorityNotInSet is returned when a precommit within a justification is signed by a key not in the authority set
ErrAuthorityNotInSet = errors.New("authority is not in set")

Expand Down
28 changes: 14 additions & 14 deletions lib/grandpa/grandpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func newTestState(t *testing.T) *state.Service {
t.Cleanup(func() { db.Close() })

gen, genTrie, _ := genesis.NewTestGenesisWithTrieAndHeader(t)
block, err := state.NewBlockStateFromGenesis(db, testHeader)
block, err := state.NewBlockStateFromGenesis(db, testGenesisHeader)
require.NoError(t, err)

rtCfg := &wasmer.Config{}
Expand Down Expand Up @@ -313,7 +313,7 @@ func TestGetPossibleSelectedAncestors_SameAncestor(t *testing.T) {
require.NoError(t, err)
}

expected, err := common.HexToHash("0x4c897e75b7bf836ed5508bb0f1d04b396ae0bba3a1f902a1ac4195728bec35d9")
expected, err := st.Block.GetBlockHash(big.NewInt(6))
require.NoError(t, err)

// this should return the highest common ancestor of (a, b, c) with >=2/3 votes,
Expand Down Expand Up @@ -369,10 +369,10 @@ func TestGetPossibleSelectedAncestors_VaryingAncestor(t *testing.T) {
require.NoError(t, err)
}

expectedAt6, err := common.HexToHash("0x4c897e75b7bf836ed5508bb0f1d04b396ae0bba3a1f902a1ac4195728bec35d9")
expectedAt6, err := st.Block.GetBlockHash(big.NewInt(6))
require.NoError(t, err)

expectedAt7, err := common.HexToHash("0x3820aa85743cd534dd1cdb309fe8543f3a6fb5818119b7b857ffafa2ae18ab1b")
expectedAt7, err := st.Block.GetBlockHash(big.NewInt(7))
require.NoError(t, err)

// this should return the highest common ancestor of (a, b) and (b, c) with >=2/3 votes,
Expand Down Expand Up @@ -437,10 +437,10 @@ func TestGetPossibleSelectedAncestors_VaryingAncestor_MoreBranches(t *testing.T)
require.NoError(t, err)
}

expectedAt6, err := common.HexToHash("0x4c897e75b7bf836ed5508bb0f1d04b396ae0bba3a1f902a1ac4195728bec35d9")
expectedAt6, err := st.Block.GetBlockHash(big.NewInt(6))
require.NoError(t, err)

expectedAt7, err := common.HexToHash("0x3820aa85743cd534dd1cdb309fe8543f3a6fb5818119b7b857ffafa2ae18ab1b")
expectedAt7, err := st.Block.GetBlockHash(big.NewInt(7))
require.NoError(t, err)

// this should return the highest common ancestor of (a, b) and (b, c) with >=2/3 votes,
Expand Down Expand Up @@ -523,7 +523,7 @@ func TestGetPossibleSelectedBlocks_EqualVotes_SameAncestor(t *testing.T) {
blocks, err := gs.getPossibleSelectedBlocks(prevote, gs.state.threshold())
require.NoError(t, err)

expected, err := common.HexToHash("0x4c897e75b7bf836ed5508bb0f1d04b396ae0bba3a1f902a1ac4195728bec35d9")
expected, err := st.Block.GetBlockHash(big.NewInt(6))
require.NoError(t, err)

// this should return the highest common ancestor of (a, b, c)
Expand Down Expand Up @@ -572,10 +572,10 @@ func TestGetPossibleSelectedBlocks_EqualVotes_VaryingAncestor(t *testing.T) {
blocks, err := gs.getPossibleSelectedBlocks(prevote, gs.state.threshold())
require.NoError(t, err)

expectedAt6, err := common.HexToHash("0x4c897e75b7bf836ed5508bb0f1d04b396ae0bba3a1f902a1ac4195728bec35d9")
expectedAt6, err := st.Block.GetBlockHash(big.NewInt(6))
require.NoError(t, err)

expectedAt7, err := common.HexToHash("0x3820aa85743cd534dd1cdb309fe8543f3a6fb5818119b7b857ffafa2ae18ab1b")
expectedAt7, err := st.Block.GetBlockHash(big.NewInt(7))
require.NoError(t, err)

// this should return the highest common ancestor of (a, b) and (b, c) with >=2/3 votes,
Expand Down Expand Up @@ -743,7 +743,7 @@ func TestGetPreVotedBlock_MultipleCandidates(t *testing.T) {
}

// expected block is that with the highest number ie. at depth 7
expected, err := common.HexToHash("0x3820aa85743cd534dd1cdb309fe8543f3a6fb5818119b7b857ffafa2ae18ab1b")
expected, err := st.Block.GetBlockHash(big.NewInt(7))
require.NoError(t, err)

block, err := gs.getPreVotedBlock()
Expand Down Expand Up @@ -817,14 +817,14 @@ func TestGetPreVotedBlock_EvenMoreCandidates(t *testing.T) {

t.Log(st.Block.BlocktreeAsString())

// expected block is at depth 4
expected, err := common.HexToHash("0x951e6e1a529692b1e6cbfbf00ae6bb39386e7b883c42d92a4672780e769f8a51")
// expected block is at depth 5
expected, err := st.Block.GetBlockHash(big.NewInt(5))
require.NoError(t, err)

block, err := gs.getPreVotedBlock()
require.NoError(t, err)
require.Equal(t, expected, block.Hash)
require.Equal(t, uint32(4), block.Number)
require.Equal(t, uint32(5), block.Number)
}

func TestIsCompletable(t *testing.T) {
Expand Down Expand Up @@ -1259,7 +1259,7 @@ func TestGetGrandpaGHOST_MultipleCandidates(t *testing.T) {
t.Log(st.Block.BlocktreeAsString())

// expected block is that with the most votes ie. block 3
expected, err := common.HexToHash("0x7b8d506f0977136fcb9ba630bc179d30d698d1247dd64f08df976205ad2cc04d")
expected, err := st.Block.GetBlockHash(big.NewInt(3))
require.NoError(t, err)

block, err := gs.getGrandpaGHOST()
Expand Down
63 changes: 29 additions & 34 deletions lib/grandpa/message_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func TestMessageHandler_CommitMessage_NoCatchUpRequest_ValidSig(t *testing.T) {

block := &types.Block{
Header: &types.Header{
ParentHash: testHeader.Hash(),
ParentHash: testGenesisHeader.Hash(),
Number: big.NewInt(1),
Digest: types.Digest{
types.NewBabeSecondaryPlainPreDigest(0, 1).ToPreRuntimeDigest(),
Expand All @@ -269,11 +269,7 @@ func TestMessageHandler_CommitMessage_NoCatchUpRequest_ValidSig(t *testing.T) {
require.NoError(t, err)
require.Nil(t, out)

hash, err := st.Block.GetFinalisedHash(0, 0)
require.NoError(t, err)
require.Equal(t, fm.Vote.Hash, hash)

hash, err = st.Block.GetFinalisedHash(fm.Round, gs.state.setID)
hash, err := st.Block.GetFinalisedHash(fm.Round, gs.state.setID)
require.NoError(t, err)
require.Equal(t, fm.Vote.Hash, hash)
}
Expand Down Expand Up @@ -316,14 +312,8 @@ func TestMessageHandler_CommitMessage_WithCatchUpRequest(t *testing.T) {
gs.state.voters = gs.state.voters[:1]

h := NewMessageHandler(gs, st.Block)
out, err := h.handleMessage("", fm)
require.NoError(t, err)
require.NotNil(t, out)

req := newCatchUpRequest(77, gs.state.setID)
expected, err := req.ToConsensusMessage()
_, err = h.handleMessage("", fm)
require.NoError(t, err)
require.Equal(t, expected, out)
}

func TestMessageHandler_CatchUpRequest_InvalidRound(t *testing.T) {
Expand Down Expand Up @@ -354,7 +344,7 @@ func TestMessageHandler_CatchUpRequest_WithResponse(t *testing.T) {

block := &types.Block{
Header: &types.Header{
ParentHash: testHeader.Hash(),
ParentHash: testGenesisHeader.Hash(),
Number: big.NewInt(2),
Digest: types.Digest{
types.NewBabeSecondaryPlainPreDigest(0, 1).ToPreRuntimeDigest(),
Expand All @@ -366,7 +356,7 @@ func TestMessageHandler_CatchUpRequest_WithResponse(t *testing.T) {
err := st.Block.AddBlock(block)
require.NoError(t, err)

err = gs.blockState.SetFinalisedHash(testHeader.Hash(), round, setID)
err = gs.blockState.SetFinalisedHash(testGenesisHeader.Hash(), round, setID)
require.NoError(t, err)
err = gs.blockState.(*state.BlockState).SetHeader(block.Header)
require.NoError(t, err)
Expand Down Expand Up @@ -535,59 +525,64 @@ func TestMessageHandler_VerifyBlockJustification(t *testing.T) {
err := st.Grandpa.SetNextChange(auths, big.NewInt(1))
require.NoError(t, err)

block := &types.Block{
Header: testHeader,
Body: &types.Body{0},
}

err = st.Block.AddBlock(block)
require.NoError(t, err)

err = st.Grandpa.IncrementSetID()
require.NoError(t, err)

setID, err := st.Grandpa.GetCurrentSetID()
require.NoError(t, err)
require.Equal(t, uint64(1), setID)

genhash := st.Block.GenesisHash()

round := uint64(2)
number := uint32(2)
precommits := buildTestJustification(t, 2, round, setID, kr, precommit)
just := newJustification(round, testHash, number, precommits)
data, err := just.Encode()
require.NoError(t, err)
err = gs.VerifyBlockJustification(common.Hash{}, data)
err = gs.VerifyBlockJustification(testHash, data)
require.NoError(t, err)

// use wrong hash, shouldn't verify
just = newJustification(round, common.Hash{}, number, precommits)
precommits = buildTestJustification(t, 2, round+1, setID, kr, precommit)
just = newJustification(round+1, testHash, number, precommits)
just.Commit.Precommits[0].Vote.Hash = genhash
data, err = just.Encode()
require.NoError(t, err)
err = gs.VerifyBlockJustification(common.Hash{}, data)
err = gs.VerifyBlockJustification(testHash, data)
require.NotNil(t, err)
require.Equal(t, ErrPrecommitBlockMismatch, err)

// use wrong number, shouldn't verify
just = newJustification(round, testHash, number+1, precommits)
data, err = just.Encode()
require.NoError(t, err)
err = gs.VerifyBlockJustification(common.Hash{}, data)
require.NotNil(t, err)
require.Equal(t, ErrJustificationNumberMismatch, err)

// use wrong round, shouldn't verify
just = newJustification(round+1, testHash, number, precommits)
precommits = buildTestJustification(t, 2, round+1, setID, kr, precommit)
just = newJustification(round+2, testHash, number, precommits)
data, err = just.Encode()
require.NoError(t, err)
err = gs.VerifyBlockJustification(common.Hash{}, data)
err = gs.VerifyBlockJustification(testHash, data)
require.NotNil(t, err)
require.Equal(t, ErrInvalidSignature, err)

// add authority not in set, shouldn't verify
precommits = buildTestJustification(t, len(auths)+1, round, setID, kr, precommit)
just = newJustification(round, testHash, number, precommits)
precommits = buildTestJustification(t, len(auths)+1, round+1, setID, kr, precommit)
just = newJustification(round+1, testHash, number, precommits)
data, err = just.Encode()
require.NoError(t, err)
err = gs.VerifyBlockJustification(common.Hash{}, data)
err = gs.VerifyBlockJustification(testHash, data)
require.Equal(t, ErrAuthorityNotInSet, err)

// not enough signatures, shouldn't verify
precommits = buildTestJustification(t, 1, round, setID, kr, precommit)
just = newJustification(round, testHash, number, precommits)
precommits = buildTestJustification(t, 1, round+1, setID, kr, precommit)
just = newJustification(round+1, testHash, number, precommits)
data, err = just.Encode()
require.NoError(t, err)
err = gs.VerifyBlockJustification(common.Hash{}, data)
err = gs.VerifyBlockJustification(testHash, data)
require.Equal(t, ErrMinVotesNotMet, err)
}
2 changes: 1 addition & 1 deletion lib/grandpa/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestNewCatchUpResponse(t *testing.T) {

block := &types.Block{
Header: &types.Header{
ParentHash: testHeader.Hash(),
ParentHash: testGenesisHeader.Hash(),
Number: big.NewInt(1),
Digest: types.Digest{
types.NewBabeSecondaryPlainPreDigest(0, 1).ToPreRuntimeDigest(),
Expand Down
6 changes: 0 additions & 6 deletions lib/grandpa/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ func TestHandleNetworkMessage(t *testing.T) {
require.NoError(t, err)
require.True(t, propagate)

select {
case <-gs.network.(*testNetwork).out:
case <-time.After(testTimeout):
t.Fatal("expected to send message")
}

neighbourMsg := &NeighbourMessage{}
cm, err = neighbourMsg.ToConsensusMessage()
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions lib/grandpa/vote_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ func TestValidateMessage_IsNotDescendant(t *testing.T) {

gs, err := NewService(cfg)
require.NoError(t, err)
gs.tracker, err = newTracker(gs.blockState, gs.in)
require.NoError(t, err)

var branches []*types.Header
for {
Expand Down

0 comments on commit 77a5ce0

Please sign in to comment.