diff options
Diffstat (limited to 'core/test/app_test.go')
-rw-r--r-- | core/test/app_test.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/core/test/app_test.go b/core/test/app_test.go index 80580a3..21c9a81 100644 --- a/core/test/app_test.go +++ b/core/test/app_test.go @@ -18,6 +18,7 @@ package test import ( + "bytes" "testing" "time" @@ -196,6 +197,72 @@ func (s *AppTestSuite) TestVerify() { req.Equal(ErrAckingBlockNotDelivered, app7.Verify()) } +func (s *AppTestSuite) TestWitness() { + // Deliver several blocks, there is only one chain only. + app := NewApp(nil) + deliver := func(b *types.Block) { + app.BlockConfirmed(*b) + app.BlockDelivered(b.Hash, b.Position, b.Finalization) + } + b00 := &types.Block{ + Hash: common.NewRandomHash(), + Finalization: types.FinalizationResult{ + Height: 1, + Timestamp: time.Now().UTC(), + }} + b01 := &types.Block{ + Hash: common.NewRandomHash(), + Position: types.Position{Height: 1}, + Finalization: types.FinalizationResult{ + ParentHash: b00.Hash, + Height: 2, + Timestamp: time.Now().UTC(), + }, + Witness: types.Witness{ + Height: 1, + Data: b00.Hash.Bytes(), + }} + b02 := &types.Block{ + Hash: common.NewRandomHash(), + Position: types.Position{Height: 2}, + Finalization: types.FinalizationResult{ + ParentHash: b01.Hash, + Height: 3, + Timestamp: time.Now().UTC(), + }, + Witness: types.Witness{ + Height: 1, + Data: b00.Hash.Bytes(), + }} + deliver(b00) + deliver(b01) + deliver(b02) + // A block with higher witness height, should retry later. + s.Require().Equal(types.VerifyRetryLater, app.VerifyBlock(&types.Block{ + Witness: types.Witness{Height: 4}})) + // Mismatched witness height and data, should return invalid. + s.Require().Equal(types.VerifyInvalidBlock, app.VerifyBlock(&types.Block{ + Witness: types.Witness{Height: 1, Data: b01.Hash.Bytes()}})) + // We can only verify a block followed last confirmed block. + s.Require().Equal(types.VerifyRetryLater, app.VerifyBlock(&types.Block{ + Witness: types.Witness{Height: 2, Data: b01.Hash.Bytes()}, + Position: types.Position{Height: 4}})) + // It's the OK case. + s.Require().Equal(types.VerifyOK, app.VerifyBlock(&types.Block{ + Witness: types.Witness{Height: 2, Data: b01.Hash.Bytes()}, + Position: types.Position{Height: 3}})) + // Check current last pending height. + s.Require().Equal(app.LastPendingHeight, uint64(3)) + // We can only prepare witness for what've delivered. + _, err := app.PrepareWitness(4) + s.Require().IsType(err, ErrLowerPendingHeight) + // It should be ok to prepare for height that already delivered. + w, err := app.PrepareWitness(3) + s.Require().NoError(err) + s.Require().Equal(w.Height, b02.Finalization.Height) + s.Require().Equal(0, bytes.Compare(w.Data, b02.Hash[:])) +} + func TestApp(t *testing.T) { suite.Run(t, new(AppTestSuite)) } |