diff options
author | Mission Liao <mission.liao@dexon.org> | 2018-12-27 10:30:17 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-27 10:30:17 +0800 |
commit | 2a8254195f56cc10252753f828458b6efe222558 (patch) | |
tree | 568812179ac9485667e4a35a5c01710c900868e3 /core/test/app_test.go | |
parent | ca82c3c26ac206c2c81dfa636902aacf327381b1 (diff) | |
download | tangerine-consensus-2a8254195f56cc10252753f828458b6efe222558.tar tangerine-consensus-2a8254195f56cc10252753f828458b6efe222558.tar.gz tangerine-consensus-2a8254195f56cc10252753f828458b6efe222558.tar.bz2 tangerine-consensus-2a8254195f56cc10252753f828458b6efe222558.tar.lz tangerine-consensus-2a8254195f56cc10252753f828458b6efe222558.tar.xz tangerine-consensus-2a8254195f56cc10252753f828458b6efe222558.tar.zst tangerine-consensus-2a8254195f56cc10252753f828458b6efe222558.zip |
test: add witness preparation/verification (#386)
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)) } |