Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add consensus tests #626

Merged
merged 6 commits into from
Dec 20, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,52 @@ 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
if consensus == nil {
t.Errorf("Failed to create new consensus instance")
}
MakisChristou marked this conversation as resolved.
Show resolved Hide resolved
}

func TestNewRuntimeForReplay(t *testing.T) {

MakisChristou marked this conversation as resolved.
Show resolved Hide resolved
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)

MakisChristou marked this conversation as resolved.
Show resolved Hide resolved
}

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 {
Expand Down Expand Up @@ -288,6 +334,8 @@ func TestValidateBlockHeader(t *testing.T) {
err = tc.consent(blk)
expected := errFutureBlock
assert.Equal(t, expected, err)
IsFutureBlockError := IsFutureBlock(expected)
assert.True(t, IsFutureBlockError)
MakisChristou marked this conversation as resolved.
Show resolved Hide resolved
},
},
{
Expand All @@ -306,6 +354,10 @@ func TestValidateBlockHeader(t *testing.T) {
),
)
assert.Equal(t, expected, err)
isCriticalError := IsCritical(err)
assert.True(t, isCriticalError)
stringError := err.Error()
assert.Equal(t, stringError, "block gas limit invalid: parent 10000000, current 20000000")
MakisChristou marked this conversation as resolved.
Show resolved Hide resolved

},
},
Expand Down