Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.
Merged
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
6 changes: 4 additions & 2 deletions db/migrations/state/0009.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
ALTER TABLE IF EXISTS state.fork_id DROP CONSTRAINT IF EXISTS fork_id_block_num_fkey;

-- +migrate Down
DELETE FROM state.fork_id f
WHERE NOT EXISTS(SELECT 1 FROM state.block b WHERE b.block_num = f.block_num);

ALTER TABLE IF EXISTS state.fork_id ADD CONSTRAINT fork_id_block_num_fkey
FOREIGN KEY(block_num)
REFERENCES state.block (block_num) ON DELETE CASCADE;
FOREIGN KEY(block_num) REFERENCES state.block (block_num) ON DELETE CASCADE;
8 changes: 5 additions & 3 deletions state/pgstatestorage/forkid.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ func (p *PostgresStorage) AddForkIDInterval(ctx context.Context, newForkID state
var forkIDs []state.ForkIDInterval
forkIDs = oldForkIDs
// Check to detect forkID inconsistencies
if forkIDs[len(forkIDs)-1].ForkId+1 != newForkID.ForkId {
log.Errorf("error checking forkID sequence. Last ForkID stored: %d. New ForkID received: %d", forkIDs[len(forkIDs)-1].ForkId, newForkID.ForkId)
return fmt.Errorf("error checking forkID sequence. Last ForkID stored: %d. New ForkID received: %d", forkIDs[len(forkIDs)-1].ForkId, newForkID.ForkId)
if forkIDs[len(forkIDs)-1].ForkId >= newForkID.ForkId {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this fixes "my" problem, I believe that this restriction shouldn't be here. Conceptually, the node is an implementation of the protocol, and the protocol allow to move from ForkID X to Y, where Y < X, and even X == Y.

FYI @invocamanman

errMsg := "error checking forkID sequence. Last ForkID stored: %d. New ForkID received: %d"
err := fmt.Errorf(errMsg, forkIDs[len(forkIDs)-1].ForkId, newForkID.ForkId)
log.Errorf(err.Error())
return err
}
forkIDs[len(forkIDs)-1].ToBatchNumber = newForkID.FromBatchNumber - 1
err := p.UpdateForkID(ctx, forkIDs[len(forkIDs)-1], dbTx)
Expand Down
72 changes: 72 additions & 0 deletions state/pgstatestorage/forkid_external_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package pgstatestorage_test

import (
"context"
"fmt"
"testing"

"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/0xPolygonHermez/zkevm-node/state/pgstatestorage"
"github.com/0xPolygonHermez/zkevm-node/test/dbutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAddForkIDInterval(t *testing.T) {
if err := dbutils.InitOrResetState(stateDBCfg); err != nil {
panic(err)
}
pgStateStorage = pgstatestorage.NewPostgresStorage(state.Config{}, stateDb)
testState = state.NewState(stateCfg, pgstatestorage.NewPostgresStorage(stateCfg, stateDb), executorClient, stateTree, nil, nil)

for i := 1; i <= 6; i++ {
err = testState.AddForkID(ctx, state.ForkIDInterval{FromBatchNumber: uint64(i * 10), ToBatchNumber: uint64(i*10) + 9, ForkId: uint64(i)}, nil)
require.NoError(t, err)
}

testCases := []struct {
name string
forkIDToAdd state.ForkIDInterval
expectedError error
}{
{
name: "fails to add because forkID already exists",
forkIDToAdd: state.ForkIDInterval{ForkId: 3},
expectedError: fmt.Errorf("error checking forkID sequence. Last ForkID stored: 6. New ForkID received: 3"),
},
{
name: "fails to add because forkID is smaller than the latest forkID",
forkIDToAdd: state.ForkIDInterval{ForkId: 5},
expectedError: fmt.Errorf("error checking forkID sequence. Last ForkID stored: 6. New ForkID received: 5"),
},
{
name: "fails to add because forkID is equal to the latest forkID",
forkIDToAdd: state.ForkIDInterval{ForkId: 6},
expectedError: fmt.Errorf("error checking forkID sequence. Last ForkID stored: 6. New ForkID received: 6"),
},
{
name: "adds successfully",
forkIDToAdd: state.ForkIDInterval{ForkId: 7},
expectedError: nil,
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
dbTx, err := testState.BeginStateTransaction(ctx)
require.NoError(t, err)

err = testState.AddForkIDInterval(ctx, tc.forkIDToAdd, dbTx)

if tc.expectedError == nil {
assert.Nil(t, err)
} else {
assert.Equal(t, tc.expectedError.Error(), err.Error())
}

require.NoError(t, dbTx.Commit(ctx))
})
}
}