From b79de4d876a76cf8c07ca8447001b6fc9a915efd Mon Sep 17 00:00:00 2001 From: Ivan Sukach Date: Tue, 20 Feb 2024 19:58:04 +0100 Subject: [PATCH] add TestRejectBlock --- vm/block.go | 2 +- vm/vm_test.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/vm/block.go b/vm/block.go index 500d20cc3..139b34b24 100644 --- a/vm/block.go +++ b/vm/block.go @@ -51,7 +51,7 @@ func (block *Block) Accept(context.Context) error { func (block *Block) Reject(context.Context) error { block.vm.log.Debug("try to reject block", "block", block.ID()) block.st = choices.Rejected - panic("implement me") + return nil } // Status returns this element's current status. diff --git a/vm/vm_test.go b/vm/vm_test.go index 06b92ba91..53625855c 100644 --- a/vm/vm_test.go +++ b/vm/vm_test.go @@ -4,11 +4,13 @@ import ( "context" _ "embed" "fmt" + "github.com/consideritdone/landslidecore/abci/example/kvstore" + rpctypes "github.com/consideritdone/landslidecore/rpc/jsonrpc/types" + "github.com/consideritdone/landslidecore/types" "os" + "reflect" "testing" - "github.com/consideritdone/landslidecore/abci/example/kvstore" - "github.com/ava-labs/avalanchego/database/manager" "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow" @@ -152,3 +154,55 @@ func TestInitVm(t *testing.T) { t.Logf("Block: %d", blk2.Height()) t.Logf("TM Block Tx count: %d", len(tmBlk2.Data.Txs)) } + +func TestRejectBlock(t *testing.T) { + tx1 := []byte{0x01} + tx2 := []byte{0x02} + tx3 := []byte{0x03} + txs := []types.Tx{tx1, tx2, tx3} + vm, service, _ := mustNewKVTestVm(t) + + blk0, err := vm.BuildBlock(context.Background()) + assert.ErrorIs(t, err, errNoPendingTxs, "expecting error no txs") + assert.Nil(t, blk0) + + for _, tx := range txs { + txReply, err := service.BroadcastTxSync(&rpctypes.Context{}, tx) + assert.NoError(t, err) + assert.Equal(t, atypes.CodeTypeOK, txReply.Code) + } + + // build 1st block + blk1, err := vm.BuildBlock(context.Background()) + assert.NoError(t, err) + assert.NotNil(t, blk1) + assert.NoError(t, blk1.Reject(context.Background())) + height1 := int64(blk1.Height()) + + // build 1st block + blk2, err := vm.BuildBlock(context.Background()) + assert.NoError(t, err) + assert.NotNil(t, blk1) + assert.NoError(t, blk1.Accept(context.Background())) + height2 := int64(blk2.Height()) + + assert.Equal(t, height1, height2) + + reply, err := service.Block(&rpctypes.Context{}, &height2) + assert.NoError(t, err) + if assert.NotNil(t, reply.Block) { + assert.EqualValues(t, height2, reply.Block.Height) + } + assert.EqualValues(t, height2, reply.Block.Height) + assert.EqualValues(t, len(txs), len(reply.Block.Txs)) + for _, tx := range txs { + match := false + for _, blkTx := range reply.Block.Txs { + if reflect.DeepEqual(tx, blkTx) { + match = true + break + } + } + require.True(t, match) + } +}