Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 13 additions & 15 deletions msm/fake_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ func TestFakeNodeEpochChangesDespiteEmptyMempool(t *testing.T) {

pChainHeight.Store(200)

for node.Epoch() == 1 {
firstEpoch := node.Epoch()

for node.Epoch() == firstEpoch {
node.buildAndNotarizeBlock()
if node.canFinalize() {
node.tryFinalizeNextBlock()
Expand Down Expand Up @@ -229,6 +231,7 @@ type blockState struct {

type fakeNode struct {
t *testing.T
epoch uint64
sm *StateMachine
mempoolEmpty bool
// blocks holds notarized blocks in order. Finalized blocks always form a
Expand Down Expand Up @@ -260,8 +263,9 @@ func newFakeNode(t *testing.T) *fakeNode {
sm, _ := newStateMachine(t)

fn := &fakeNode{
t: t,
sm: sm,
t: t,
sm: sm,
epoch: 1,
}

fn.sm.BlockBuilder = fn
Expand Down Expand Up @@ -293,17 +297,6 @@ func newFakeNode(t *testing.T) *fakeNode {
return StateMachineBlock{}, nil, fmt.Errorf("block not found")
}

fn.sm.FirstEverSimplexBlock = func() *StateMachineBlock {
for _, block := range fn.blocks {
if block.block.Metadata.SimplexEpochInfo.EpochNumber == 0 {
continue
}
return &block.block
}
require.FailNow(t, "block not found")
return nil
}

return fn
}

Expand Down Expand Up @@ -378,6 +371,9 @@ func (fn *fakeNode) tryFinalizeNextBlock() {
if block.Metadata.SimplexEpochInfo.BlockValidationDescriptor != nil {
fn.blocks = fn.blocks[:nextIndex+1]
fn.t.Logf("Trimmed notarized blocks, new length: %d", len(fn.blocks))
prevEpoch := fn.epoch
fn.epoch = md.Seq
fn.t.Logf("Epoch change from %d to %d", prevEpoch, fn.epoch)
}
}

Expand Down Expand Up @@ -421,6 +417,7 @@ func (fn *fakeNode) buildBlock() (VMBlock, *StateMachineBlock) {
block, err := fn.sm.BuildBlock(context.Background(), simplex.ProtocolMetadata{
Seq: lastMD.Seq + 1,
Round: lastMD.Round + 1,
Epoch: fn.epoch,
Prev: prevBlockDigest,
}, nil)
require.NoError(fn.t, err)
Expand All @@ -439,7 +436,8 @@ func (fn *fakeNode) prepareMetadataAndPrevBlockDigest() (*simplex.ProtocolMetada
require.NoError(fn.t, err)
} else {
lastMD = &simplex.ProtocolMetadata{
Prev: lastBlockDigest,
Prev: lastBlockDigest,
Epoch: 1,
}
}
return lastMD, lastBlockDigest
Expand Down
5 changes: 4 additions & 1 deletion msm/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package metadata

import (
"context"
"errors"
"fmt"
"math"
"math/big"
Expand All @@ -15,9 +16,11 @@ import (
// but are not imported here to prevent us from importing the entire Avalanchego codebase.
// Once we incorporate Simplex into Avalanchego, we can remove this file and import the relevant code from Avalanchego instead.

var errOverflow = errors.New("overflow")

func safeAdd(a, b uint64) (uint64, error) {
if a > math.MaxUint64-b {
return 0, fmt.Errorf("overflow: %d + %d > maxuint64", a, b)
return 0, fmt.Errorf("%w: %d + %d > maxuint64", errOverflow, a, b)
}
return a + b, nil
}
Expand Down
Loading
Loading