From 352fbc6f4e1faf442e6efff255307b427093bc86 Mon Sep 17 00:00:00 2001 From: Makis Christou Date: Wed, 20 Dec 2023 09:30:12 +0200 Subject: [PATCH] Add consensus tests (#626) --- consensus/consensus_test.go | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/consensus/consensus_test.go b/consensus/consensus_test.go index c4f53323f..9f98a5ae1 100644 --- a/consensus/consensus_test.go +++ b/consensus/consensus_test.go @@ -215,6 +215,48 @@ func (tc *testConsensus) consent(blk *block.Block) error { return err } +func TestNewConsensus(t *testing.T) { + // Mock dependencies + mockRepo := &chain.Repository{} + mockStater := &state.Stater{} + mockForkConfig := thor.ForkConfig{} + + // Create a new consensus instance + consensus := New(mockRepo, mockStater, mockForkConfig) + + // Assert that the consensus instance is not nil + assert.NotNil(t, consensus, "Failed to create new consensus instance") +} + +func TestNewRuntimeForReplay(t *testing.T) { + consensus, err := newTestConsensus() + b1 := consensus.parent + + // Test for success scenario + runtime, err := consensus.con.NewRuntimeForReplay(b1.Header(), false) + + assert.Nil(t, err) + assert.NotNil(t, runtime) +} + +func TestNewRuntimeForReplayWithError(t *testing.T) { + consensus, _ := newTestConsensus() + + // give invalid parent ID + builder := consensus.builder(&block.Header{}) + + b1, err := consensus.sign(builder.Timestamp(consensus.parent.Header().Timestamp())) + if err != nil { + t.Fatal(err) + } + + // Test for success scenario + runtime, err := consensus.con.NewRuntimeForReplay(b1.Header(), false) + + assert.NotNil(t, err) + assert.Nil(t, runtime) +} + func TestValidateBlockHeader(t *testing.T) { tc, err := newTestConsensus() if err != nil { @@ -288,6 +330,7 @@ func TestValidateBlockHeader(t *testing.T) { err = tc.consent(blk) expected := errFutureBlock assert.Equal(t, expected, err) + assert.True(t, IsFutureBlock(expected)) }, }, { @@ -306,6 +349,8 @@ func TestValidateBlockHeader(t *testing.T) { ), ) assert.Equal(t, expected, err) + assert.True(t, IsCritical(err)) + assert.Equal(t, err.Error(), "block gas limit invalid: parent 10000000, current 20000000") }, },