From 3a986d86354a0f930192a8d6ca3c6e7c75a08dae Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 26 Nov 2020 12:33:34 +0300 Subject: [PATCH] core: add test for `RemoveUntraceableBlocks` setting --- pkg/core/blockchain_test.go | 64 +++++++++++++++++++++++++++++++++---- pkg/core/helper_test.go | 8 +++-- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/pkg/core/blockchain_test.go b/pkg/core/blockchain_test.go index bbe22475a..05c65f426 100644 --- a/pkg/core/blockchain_test.go +++ b/pkg/core/blockchain_test.go @@ -9,6 +9,7 @@ import ( "github.com/nspcc-dev/neo-go/internal/random" "github.com/nspcc-dev/neo-go/internal/testchain" + "github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/core/chaindump" @@ -124,7 +125,9 @@ func TestAddBlock(t *testing.T) { } func TestAddBlockStateRoot(t *testing.T) { - bc := newTestChainWithStateRoot(t, true) + bc := newTestChainWithCustomCfg(t, func(c *config.Config) { + c.ProtocolConfiguration.StateRootInHeader = true + }) defer bc.Close() sr, err := bc.GetStateRoot(bc.BlockHeight()) @@ -1235,8 +1238,12 @@ func TestSubscriptions(t *testing.T) { require.NoError(t, err) } -func testDumpAndRestore(t *testing.T, stateRootInHeader bool) { - bc := newTestChainWithStateRoot(t, stateRootInHeader) +func testDumpAndRestore(t *testing.T, dumpF, restoreF func(c *config.Config)) { + if restoreF == nil { + restoreF = dumpF + } + + bc := newTestChainWithCustomCfg(t, dumpF) defer bc.Close() initBasicChain(t, bc) @@ -1248,14 +1255,14 @@ func testDumpAndRestore(t *testing.T, stateRootInHeader bool) { buf := w.Bytes() t.Run("invalid start", func(t *testing.T) { - bc2 := newTestChainWithStateRoot(t, stateRootInHeader) + bc2 := newTestChainWithCustomCfg(t, restoreF) defer bc2.Close() r := io.NewBinReaderFromBuf(buf) require.Error(t, chaindump.Restore(bc2, r, 2, 1, nil)) }) t.Run("good", func(t *testing.T) { - bc2 := newTestChainWithStateRoot(t, stateRootInHeader) + bc2 := newTestChainWithCustomCfg(t, restoreF) defer bc2.Close() r := io.NewBinReaderFromBuf(buf) @@ -1288,9 +1295,52 @@ func testDumpAndRestore(t *testing.T, stateRootInHeader bool) { func TestDumpAndRestore(t *testing.T) { t.Run("no state root", func(t *testing.T) { - testDumpAndRestore(t, false) + testDumpAndRestore(t, func(c *config.Config) { + c.ProtocolConfiguration.StateRootInHeader = false + }, nil) }) t.Run("with state root", func(t *testing.T) { - testDumpAndRestore(t, true) + testDumpAndRestore(t, func(c *config.Config) { + c.ProtocolConfiguration.StateRootInHeader = false + }, nil) + }) + t.Run("remove untraceable", func(t *testing.T) { + // Dump can only be created if all blocks and transactions are present. + testDumpAndRestore(t, nil, func(c *config.Config) { + c.ProtocolConfiguration.MaxTraceableBlocks = 2 + c.ProtocolConfiguration.RemoveUntraceableBlocks = true + }) }) } + +func TestRemoveUntraceable(t *testing.T) { + bc := newTestChainWithCustomCfg(t, func(c *config.Config) { + c.ProtocolConfiguration.MaxTraceableBlocks = 2 + c.ProtocolConfiguration.RemoveUntraceableBlocks = true + }) + defer bc.Close() + + tx1, err := testchain.NewTransferFromOwner(bc, bc.contracts.NEO.Hash, util.Uint160{}, 1, 0, bc.BlockHeight()+1) + require.NoError(t, err) + b1 := bc.newBlock(tx1) + require.NoError(t, bc.AddBlock(b1)) + tx1Height := bc.BlockHeight() + + tx2, err := testchain.NewTransferFromOwner(bc, bc.contracts.NEO.Hash, util.Uint160{}, 1, 0, bc.BlockHeight()+1) + require.NoError(t, err) + require.NoError(t, bc.AddBlock(bc.newBlock(tx2))) + + _, h1, err := bc.GetTransaction(tx1.Hash()) + require.NoError(t, err) + require.Equal(t, tx1Height, h1) + + require.NoError(t, bc.AddBlock(bc.newBlock())) + + _, _, err = bc.GetTransaction(tx1.Hash()) + require.Error(t, err) + _, err = bc.GetAppExecResults(tx1.Hash(), trigger.Application) + require.Error(t, err) + b, err := bc.GetBlock(b1.Hash()) + require.NoError(t, err) + require.Len(t, b.Transactions, 0) +} diff --git a/pkg/core/helper_test.go b/pkg/core/helper_test.go index d9c854164..796705a07 100644 --- a/pkg/core/helper_test.go +++ b/pkg/core/helper_test.go @@ -41,13 +41,15 @@ var neoOwner = testchain.MultisigScriptHash() // newTestChain should be called before newBlock invocation to properly setup // global state. func newTestChain(t *testing.T) *Blockchain { - return newTestChainWithStateRoot(t, false) + return newTestChainWithCustomCfg(t, nil) } -func newTestChainWithStateRoot(t *testing.T, stateRootInHeader bool) *Blockchain { +func newTestChainWithCustomCfg(t *testing.T, f func(*config.Config)) *Blockchain { unitTestNetCfg, err := config.Load("../../config", testchain.Network()) require.NoError(t, err) - unitTestNetCfg.ProtocolConfiguration.StateRootInHeader = stateRootInHeader + if f != nil { + f(&unitTestNetCfg) + } chain, err := NewBlockchain(storage.NewMemoryStore(), unitTestNetCfg.ProtocolConfiguration, zaptest.NewLogger(t)) require.NoError(t, err) go chain.Run()