-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d17db59
commit fb289c9
Showing
12 changed files
with
204 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package lastgersync | ||
package injectedgersync | ||
|
||
import ( | ||
"github.com/0xPolygon/cdk/config/types" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
-- +migrate Down | ||
DROP TABLE IF EXISTS block; | ||
DROP TABLE IF EXISTS claim; | ||
DROP TABLE IF EXISTS bridge; | ||
|
||
-- +migrate Up | ||
CREATE TABLE block ( | ||
num BIGINT PRIMARY KEY | ||
); | ||
|
||
CREATE TABLE injected_ger ( | ||
block_num INTEGER NOT NULL REFERENCES block(num) ON DELETE CASCADE, | ||
block_pos INTEGER NOT NULL, | ||
l1_info_tree_index INTEGER NOT NULL, | ||
global_exit_root VARCHAR NOT NULL, | ||
PRIMARY KEY (block_num, block_pos) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package migrations | ||
|
||
import ( | ||
_ "embed" | ||
"strings" | ||
|
||
"github.com/0xPolygon/cdk/db" | ||
migrate "github.com/rubenv/sql-migrate" | ||
) | ||
|
||
const upDownSeparator = "-- +migrate Up" | ||
|
||
//go:embed lastgersync0001.sql | ||
var mig001 string | ||
var mig001splitted = strings.Split(mig001, upDownSeparator) | ||
|
||
var lastgerMigrations = &migrate.MemoryMigrationSource{ | ||
Migrations: []*migrate.Migration{ | ||
{ | ||
Id: "bridgesync001", | ||
Up: []string{mig001splitted[1]}, | ||
Down: []string{mig001splitted[0]}, | ||
}, | ||
}, | ||
} | ||
|
||
func RunMigrations(dbPath string) error { | ||
return db.RunMigrations(dbPath, lastgerMigrations) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package injectedgersync | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
|
||
"github.com/0xPolygon/cdk/db" | ||
"github.com/0xPolygon/cdk/injectedgersync/migrations" | ||
"github.com/0xPolygon/cdk/log" | ||
"github.com/0xPolygon/cdk/sync" | ||
ethCommon "github.com/ethereum/go-ethereum/common" | ||
"github.com/russross/meddler" | ||
) | ||
|
||
var ( | ||
// TODO: use db.ErrNotFound instead | ||
ErrNotFound = errors.New("not found") | ||
) | ||
|
||
type InjectedGER struct { | ||
BlockNum uint64 `meddler:"block_num"` | ||
BlockPos uint64 `meddler:"block_pos"` | ||
L1InfoTreeIndex uint32 `meddler:"l1_info_tree_index"` | ||
GlobalExitRoot ethCommon.Hash `meddler:"global_exit_root,hash"` | ||
} | ||
|
||
type blockWithGERs struct { | ||
// inclusive | ||
FirstIndex uint32 | ||
// not inclusive | ||
LastIndex uint32 | ||
} | ||
|
||
type processor struct { | ||
db *sql.DB | ||
} | ||
|
||
func newProcessor(dbPath string) (*processor, error) { | ||
err := migrations.RunMigrations(dbPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
db, err := db.NewSQLiteDB(dbPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &processor{ | ||
db: db, | ||
}, nil | ||
} | ||
|
||
// GetLastProcessedBlockAndL1InfoTreeIndex returns the last processed block oby the processor, including blocks | ||
// that don't have events | ||
func (p *processor) GetLastProcessedBlock(ctx context.Context) (uint64, error) { | ||
return p.getLastProcessedBlockWithTx(p.db) | ||
} | ||
|
||
func (p *processor) getLastIndex() (uint32, error) { | ||
var lastIndex uint32 | ||
row := p.db.QueryRow("SELECT l1_info_tree_index FROM injected_ger ORDER BY l1_info_tree_index DESC LIMIT 1;") | ||
err := row.Scan(&lastIndex) | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return 0, ErrNotFound | ||
} | ||
return lastIndex, err | ||
} | ||
|
||
func (p *processor) getLastProcessedBlockWithTx(tx db.DBer) (uint64, error) { | ||
var lastProcessedBlock uint64 | ||
row := tx.QueryRow("SELECT num FROM BLOCK ORDER BY num DESC LIMIT 1;") | ||
err := row.Scan(&lastProcessedBlock) | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return 0, nil | ||
} | ||
return lastProcessedBlock, err | ||
} | ||
|
||
func (p *processor) ProcessBlock(ctx context.Context, block sync.Block) error { | ||
tx, err := db.NewTx(ctx, p.db) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
if err != nil { | ||
if errRllbck := tx.Rollback(); errRllbck != nil { | ||
log.Errorf("error while rolling back tx %v", errRllbck) | ||
} | ||
} | ||
}() | ||
|
||
if _, err := tx.Exec(`INSERT INTO block (num) VALUES ($1)`, block.Num); err != nil { | ||
return err | ||
} | ||
|
||
for _, e := range block.Events { | ||
event := e.(InjectedGER) | ||
if err = meddler.Insert(tx, "injected_ger", event); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
err = tx.Commit() | ||
return err | ||
} | ||
|
||
func (p *processor) Reorg(ctx context.Context, firstReorgedBlock uint64) error { | ||
_, err := p.db.Exec(`DELETE FROM block WHERE num >= $1;`, firstReorgedBlock) | ||
return err | ||
} | ||
|
||
// GetFirstGERAfterL1InfoTreeIndex returns the first GER injected on the chain that is related to l1InfoTreeIndex | ||
// or greater | ||
func (p *processor) GetFirstGERAfterL1InfoTreeIndex(ctx context.Context, l1InfoTreeIndex uint32) (*InjectedGER, error) { | ||
injectedGER := &InjectedGER{} | ||
if err := meddler.QueryRow(p.db, injectedGER, ` | ||
SELECT * FROM injected_ger | ||
WHERE l1_info_tree_index >= $1 | ||
ORDER BY l1_info_tree_index ASC | ||
LIMIT 1; | ||
`, l1InfoTreeIndex); err != nil { | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return nil, ErrNotFound | ||
} | ||
return nil, err | ||
} | ||
|
||
return injectedGER, nil | ||
} |
Oops, something went wrong.