Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
noot committed Apr 16, 2021
1 parent 28c7003 commit 59389ab
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
7 changes: 4 additions & 3 deletions dot/network/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ func (q *syncQueue) pushResponse(resp *BlockResponseMessage, pid peer.ID) error
}

if numJustifications == 0 {
logger.Debug("got empty justification data", "start hash", startHash)
return errEmptyJustificationData
}

Expand All @@ -484,7 +485,7 @@ func (q *syncQueue) pushResponse(resp *BlockResponseMessage, pid peer.ID) error
from: pid,
})

logger.Info("pushed justification data to queue", "hash", startHash)
logger.Debug("pushed justification data to queue", "hash", startHash)
q.responseCh <- justificationResponses
return nil
}
Expand Down Expand Up @@ -666,15 +667,15 @@ func (q *syncQueue) processBlockResponses() {

func (q *syncQueue) handleBlockJustification(data []*types.BlockData) {
startHash, endHash := data[0].Hash, data[len(data)-1].Hash
logger.Info("sending justification data to syncer", "start", startHash, "end", endHash)
logger.Debug("sending justification data to syncer", "start", startHash, "end", endHash)

_, err := q.s.syncer.ProcessJustification(data)
if err != nil {
logger.Warn("failed to handle block justifications", "error", err)
return
}

logger.Info("finished processing justification data", "start", startHash, "end", endHash)
logger.Debug("finished processing justification data", "start", startHash, "end", endHash)

// update peer's score
var from peer.ID
Expand Down
2 changes: 1 addition & 1 deletion dot/network/sync_justification.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (q *syncQueue) pushJustificationRequest(to peer.ID, start uint64) {
req := createBlockRequestWithHash(startHash, blockRequestSize)
req.RequestedData = RequestedDataJustification

logger.Info("pushing justification request to queue", "start", start, "hash", startHash)
logger.Debug("pushing justification request to queue", "start", start, "hash", startHash)
q.justificationRequestData.Store(startHash, requestData{
received: false,
})
Expand Down
2 changes: 1 addition & 1 deletion dot/sync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (s *Service) ProcessJustification(data []*types.BlockData) (int, error) {
}

if bd.Justification != nil && bd.Justification.Exists() {
logger.Info("handling Justification...", "number", header.Number, "hash", bd.Hash)
logger.Debug("handling Justification...", "number", header.Number, "hash", bd.Hash)
s.handleJustification(header, bd.Justification.Value())
}
}
Expand Down
27 changes: 27 additions & 0 deletions dot/sync/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/common/optional"
"github.com/ChainSafe/gossamer/lib/common/variadic"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/runtime"
Expand Down Expand Up @@ -362,3 +363,29 @@ func TestSyncer_HandleJustification(t *testing.T) {
require.NoError(t, err)
require.Equal(t, just, res)
}

func TestSyncer_ProcessJustification(t *testing.T) {
syncer := newTestSyncer(t)

parent, err := syncer.blockState.(*state.BlockState).BestBlockHeader()
require.NoError(t, err)
block := buildBlock(t, syncer.runtime, parent)
err = syncer.blockState.(*state.BlockState).AddBlock(block)
require.NoError(t, err)

just := []byte("testjustification")

data := []*types.BlockData{
{
Hash: syncer.blockState.BestBlockHash(),
Justification: optional.NewBytes(true, just),
},
}

_, err = syncer.ProcessJustification(data)
require.NoError(t, err)

res, err := syncer.blockState.GetJustification(syncer.blockState.BestBlockHash())
require.NoError(t, err)
require.Equal(t, just, res)
}

0 comments on commit 59389ab

Please sign in to comment.