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

feat: use sqlite on lastgersync #150

Merged
merged 4 commits into from
Nov 5, 2024
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
9 changes: 5 additions & 4 deletions lastgersync/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lastgersync_test
import (
"context"
"fmt"
"path"
"strconv"
"testing"
"time"
Expand All @@ -18,7 +19,7 @@ import (
func TestE2E(t *testing.T) {
ctx := context.Background()
env := aggoraclehelpers.SetupAggoracleWithEVMChain(t)
dbPathSyncer := t.TempDir()
dbPathSyncer := path.Join(t.TempDir(), "file::memory:?cache=shared")
syncer, err := lastgersync.New(
ctx,
dbPathSyncer,
Expand Down Expand Up @@ -65,8 +66,8 @@ func TestE2E(t *testing.T) {
}
require.True(t, syncerUpToDate, errMsg)

_, actualGER, err := syncer.GetFirstGERAfterL1InfoTreeIndex(ctx, uint32(i))
require.NoError(t, err)
require.Equal(t, common.Hash(expectedGER), actualGER)
e, err := syncer.GetFirstGERAfterL1InfoTreeIndex(ctx, uint32(i))
require.NoError(t, err, fmt.Sprint("iteration: ", i))
require.Equal(t, common.Hash(expectedGER), e.GlobalExitRoot, fmt.Sprint("iteration: ", i))
}
}
18 changes: 10 additions & 8 deletions lastgersync/evmdownloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,23 @@ func newDownloader(
func (d *downloader) Download(ctx context.Context, fromBlock uint64, downloadedCh chan sync.EVMBlock) {
var (
attempts int
lastIndex uint32
nextIndex uint32
err error
)
for {
lastIndex, err = d.processor.getLastIndex(ctx)
lastIndex, err := d.processor.getLastIndex()
if errors.Is(err, db.ErrNotFound) {
lastIndex = 0
nextIndex = 0
} else if err != nil {
log.Errorf("error getting last indes: %v", err)
attempts++
d.rh.Handle("getLastIndex", attempts)

continue
}

if lastIndex > 0 {
nextIndex = lastIndex + 1
}
break
}
for {
Expand All @@ -88,12 +90,12 @@ func (d *downloader) Download(ctx context.Context, fromBlock uint64, downloadedC
return
default:
}
lastBlock := d.WaitForNewBlocks(ctx, fromBlock)
fromBlock = d.WaitForNewBlocks(ctx, fromBlock)

attempts = 0
var gers []Event
for {
gers, err = d.getGERsFromIndex(ctx, lastIndex)
gers, err = d.getGERsFromIndex(ctx, nextIndex)
if err != nil {
log.Errorf("error getting GERs: %v", err)
attempts++
Expand All @@ -105,7 +107,7 @@ func (d *downloader) Download(ctx context.Context, fromBlock uint64, downloadedC
break
}

blockHeader, isCanceled := d.GetBlockHeader(ctx, lastBlock)
blockHeader, isCanceled := d.GetBlockHeader(ctx, fromBlock)
if isCanceled {
return
}
Expand All @@ -126,7 +128,7 @@ func (d *downloader) Download(ctx context.Context, fromBlock uint64, downloadedC
if !ok {
log.Errorf("unexpected type %T in events", block.Events[0])
}
lastIndex = event.L1InfoTreeIndex
nextIndex = event.L1InfoTreeIndex + 1
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lastgersync/lastgersync.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func New(
waitForNewBlocksPeriod time.Duration,
downloadBufferSize int,
) (*LastGERSync, error) {
processor, err := newProcessor(dbPath)
processor, err := newProcessor(dbPath, "lastGERSync")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func (s *LastGERSync) Start(ctx context.Context) {

func (s *LastGERSync) GetFirstGERAfterL1InfoTreeIndex(
ctx context.Context, atOrAfterL1InfoTreeIndex uint32,
) (injectedL1InfoTreeIndex uint32, ger common.Hash, err error) {
) (Event, error) {
return s.processor.GetFirstGERAfterL1InfoTreeIndex(ctx, atOrAfterL1InfoTreeIndex)
}

Expand Down
14 changes: 14 additions & 0 deletions lastgersync/migrations/lastgersync0001.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- +migrate Down
DROP TABLE IF EXISTS block;
DROP TABLE IF EXISTS global_exit_root;

-- +migrate Up
CREATE TABLE block (
num BIGINT PRIMARY KEY
);

CREATE TABLE imported_global_exit_root (
block_num INTEGER PRIMARY KEY REFERENCES block(num) ON DELETE CASCADE,
global_exit_root VARCHAR NOT NULL,
l1_info_tree_index INTEGER NOT NULL
);
21 changes: 21 additions & 0 deletions lastgersync/migrations/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package migrations

import (
_ "embed"

"github.com/0xPolygon/cdk/db"
"github.com/0xPolygon/cdk/db/types"
)

//go:embed lastgersync0001.sql
var mig001 string

func RunMigrations(dbPath string) error {
migrations := []types.Migration{
{
ID: "lastgersync0001",
SQL: mig001,
},
}
return db.RunMigrations(dbPath, migrations)
}
Loading
Loading