code.vegaprotocol.io/vega@v0.79.0/core/blockchain/nullchain/replay_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package nullchain_test
    17  
    18  import (
    19  	"os"
    20  	"path"
    21  	"testing"
    22  
    23  	"code.vegaprotocol.io/vega/core/blockchain"
    24  	"code.vegaprotocol.io/vega/core/blockchain/nullchain"
    25  	"code.vegaprotocol.io/vega/core/blockchain/nullchain/mocks"
    26  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    27  	"code.vegaprotocol.io/vega/logging"
    28  
    29  	"github.com/golang/mock/gomock"
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  func TestNullchainReplayer(t *testing.T) {
    34  	t.Run("test no file provided", testReplayerNoFile)
    35  	t.Run("test truncate if record but not replay", testTruncateIfRecordButNoReplay)
    36  }
    37  
    38  func testReplayerNoFile(t *testing.T) {
    39  	ctrl := gomock.NewController(t)
    40  	app := mocks.NewMockApplicationService(ctrl)
    41  	defer ctrl.Finish()
    42  	r, err := nullchain.NewNullChainReplayer(app, blockchain.ReplayConfig{}, logging.NewTestLogger())
    43  	require.ErrorIs(t, err, nullchain.ErrReplayFileIsRequired)
    44  	require.Nil(t, r)
    45  }
    46  
    47  func testTruncateIfRecordButNoReplay(t *testing.T) {
    48  	ctrl := gomock.NewController(t)
    49  	app := mocks.NewMockApplicationService(ctrl)
    50  	defer ctrl.Finish()
    51  
    52  	// write some nonsense into the replay file as if we've recorded something
    53  	rplFile := path.Join(t.TempDir(), "rfile")
    54  	f, err := os.Create(rplFile)
    55  	require.NoError(t, err)
    56  
    57  	f.WriteString(vgrand.RandomStr(5))
    58  	f.Close()
    59  
    60  	r, err := nullchain.NewNullChainReplayer(app, blockchain.ReplayConfig{Record: true, Replay: false, ReplayFile: rplFile}, logging.NewTestLogger())
    61  	require.NoError(t, err)
    62  	require.NotNil(t, r)
    63  	defer r.Stop()
    64  
    65  	// check that the file is now empty
    66  	info, err := os.Stat(rplFile)
    67  	require.NoError(t, err)
    68  	require.Equal(t, int64(0), info.Size())
    69  }