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

update contract integration #36

Merged
merged 4 commits into from
Dec 30, 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
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
},
{
"name": "Run Synchronizer Tests",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}/sequencer/synchronizer",
"envFile": "${workspaceFolder}/sequencer/.env",
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"solidity.compileUsingRemoteVersion": "v0.8.23+commit.f704f362"
}
4 changes: 2 additions & 2 deletions sequencer/cfg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ StatsUpdateFrequencyDivider = 100

[SmartContracts]
## Smart contract address of the rollup contract
Rollup = "0x3d4810E4F8377a34781A6Ceaab9dAc08E361192d"
Rollup = "0xd2Fcf423780E5D4e6F8fD4F3c7aa931454DFB9c4"

[Coordinator]
## Ethereum address that the coordinator is using to forge batches
ForgerAddress = "0x19795e3f46003bb44538708020dae2b4cc8b6425"
ForgerAddress = "0x0f466febb653fac8bdf5cca8031270740e6d2bbf"
### Minimum balance the forger address needs to start the coordinator in wei. If It is set to 0, the coordinator will not check the balance
#MinimumForgeAddressBalance = "0"
### Number of confirmation blocks to be sure that the tx has been mined correctly
Expand Down
53 changes: 0 additions & 53 deletions sequencer/eth/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ type RollupInterface interface {
RollupConstants() (*common.RollupConstants, error)
RollupEventsByBlock(blockNum int64, blockHash *ethCommon.Hash) (*RollupEvents, error)
RollupForgeBatchArgs(ethCommon.Hash, uint16) (*RollupForgeBatchArgs, *ethCommon.Address, error)
RollupEventInit(genesisBlockNum int64) (*RollupEventInitialize, int64, error)
}

//
Expand Down Expand Up @@ -263,36 +262,6 @@ func (ei *RollupEventInitialize) RollupVariables() *common.RollupVariables {
}
}

// RollupEventInit returns the initialize event with its corresponding block number
func (c *RollupClient) RollupEventInit(genesisBlockNum int64) (*RollupEventInitialize, int64, error) {
query := ethereum.FilterQuery{
Addresses: []ethCommon.Address{
c.address,
},
FromBlock: big.NewInt(max(0, genesisBlockNum-blocksPerDay)),
ToBlock: big.NewInt(genesisBlockNum),
Topics: [][]ethCommon.Hash{{logSYBInitialize}},
}
logs, err := c.client.client.FilterLogs(context.Background(), query)
if err != nil {
return nil, 0, common.Wrap(err)
}
if len(logs) != 1 {
return nil, 0, common.Wrap(fmt.Errorf("no event of type InitializeSYBEvent found"))
}
vLog := logs[0]
if vLog.Topics[0] != logSYBInitialize {
return nil, 0, common.Wrap(fmt.Errorf("event is not InitializeSYBEvent"))
}

var rollupInit RollupEventInitialize
if err := c.contractAbi.UnpackIntoInterface(&rollupInit, "Initialize",
vLog.Data); err != nil {
return nil, 0, common.Wrap(err)
}
return &rollupInit, int64(vLog.BlockNumber), common.Wrap(err)
}

// NewRollupClient creates a new RollupClient
func NewRollupClient(client *EthereumClient, address ethCommon.Address) (*RollupClient, error) {
contractAbi, err := abi.JSON(strings.NewReader(string(Sybil.SybilABI)))
Expand Down Expand Up @@ -327,30 +296,10 @@ func NewRollupClient(client *EthereumClient, address ethCommon.Address) (*Rollup
func (c *RollupClient) RollupConstants() (rollupConstants *common.RollupConstants, err error) {
rollupConstants = new(common.RollupConstants)
if err := c.client.Call(func(ec *ethclient.Client) error {
absoluteMaxL1L2BatchTimeout, err := c.sybil.ABSOLUTEMAXBATCHTIMEOUT(c.opts)
if err != nil {
return common.Wrap(err)
}
rollupConstants.AbsoluteMaxL1L2BatchTimeout = int64(absoluteMaxL1L2BatchTimeout)
// rollupConstants.TokenHEZ, err = c.tokamak.TokenHEZ(c.opts)
// if err != nil {
// return common.Wrap(err)
// }
rollupVerifier, err := c.sybil.RollupVerifier(c.opts)
if err != nil {
return common.Wrap(err)
}
// for i := int64(0); i < rollupVerifiers.MaxTxs.Int64(); i++ {
// var newRollupVerifier common.RollupVerifierStruct
// rollupVerifier, err := c.sybil.RollupVerifiers(c.opts, big.NewInt(i))
// if err != nil {
// return common.Wrap(err)
// }
// newRollupVerifier.MaxTx = rollupVerifier.MaxTxs.Int64()
// newRollupVerifier.NLevels = rollupVerifier.NLevels.Int64()
// rollupConstants.Verifiers = append(rollupConstants.Verifiers,
// newRollupVerifier)
// }
var newRollupVerifier common.RollupVerifierStruct
newRollupVerifier.MaxTx = rollupVerifier.MaxTx.Int64()
newRollupVerifier.NLevels = rollupVerifier.NLevel.Int64()
Expand Down Expand Up @@ -390,8 +339,6 @@ var (
// "UpdateBucketsParameters(uint256[])"))
logSYBSafeMode = crypto.Keccak256Hash([]byte(
"SafeMode()"))
logSYBInitialize = crypto.Keccak256Hash([]byte(
"Initialize(uint8)"))
)

// RollupEventsByBlock returns the events in a block that happened in the
Expand Down
52 changes: 5 additions & 47 deletions sequencer/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,8 @@ import (

const SyncTime = 24 * 60 * time.Minute

// Mode sets the working mode of the node (synchronizer or coordinator)
// type Mode string

// const (
// // ModeCoordinator defines the mode of the HermezNode as Coordinator, which
// // means that the node is set to forge (which also will be synchronizing with
// // the L1 blockchain state)
// ModeCoordinator Mode = "coordinator"

// // ModeSynchronizer defines the mode of the HermezNode as Synchronizer, which
// // means that the node is set to only synchronize with the L1 blockchain state
// // and will not forge
// ModeSynchronizer Mode = "synchronizer"
// )
// block number of the Smart contract to sync from
const RollupStartBlockNum = 379608

// Node is the Hermez Node
type Node struct {
Expand Down Expand Up @@ -227,6 +215,7 @@ func NewNode(cfg *config.Node, version string) (*Node, error) {
},
})
if err != nil {
log.Errorw("eth.NewClient", "err", err)
return nil, common.Wrap(err)
}

Expand Down Expand Up @@ -265,6 +254,7 @@ func NewNode(cfg *config.Node, version string) (*Node, error) {
StatsUpdateBlockNumDiffThreshold: cfg.Synchronizer.StatsUpdateBlockNumDiffThreshold,
StatsUpdateFrequencyDivider: cfg.Synchronizer.StatsUpdateFrequencyDivider,
ChainID: chainIDU64,
StartBlockNum: RollupStartBlockNum,
})
if err != nil {
return nil, common.Wrap(err)
Expand All @@ -275,7 +265,6 @@ func NewNode(cfg *config.Node, version string) (*Node, error) {
Rollup: *sync.RollupConstants(),
}


// TODO: rename node configs or remove unnecessary configs if not needed
hdbNodeCfg := historydb.NodeConfig{
MaxPoolTxs: cfg.Coordinator.L2DB.MaxTxs,
Expand Down Expand Up @@ -394,37 +383,6 @@ func NewNode(cfg *config.Node, version string) (*Node, error) {
// MaxFeeTx: common.RollupConstMaxFeeIdxCoordinator,
MaxL1Tx: common.RollupConstMaxL1Tx,
}
var verifierIdx int
if cfg.Coordinator.Debug.RollupVerifierIndex == nil {
verifierIdx, err = scConsts.Rollup.FindVerifierIdx(
cfg.Coordinator.Circuit.MaxTx,
cfg.Coordinator.Circuit.NLevels,
)
if err != nil {
return nil, common.Wrap(err)
}
log.Infow("Found verifier that matches circuit config", "verifierIdx", verifierIdx)
} else {
verifierIdx = *cfg.Coordinator.Debug.RollupVerifierIndex
log.Infow("Using debug verifier index from config", "verifierIdx", verifierIdx)
if verifierIdx >= len(scConsts.Rollup.Verifiers) {
return nil, common.Wrap(
fmt.Errorf("verifierIdx (%v) >= "+
"len(scConsts.Rollup.Verifiers) (%v)",
verifierIdx, len(scConsts.Rollup.Verifiers)))
}
verifier := scConsts.Rollup.Verifiers[verifierIdx]
if verifier.MaxTx != cfg.Coordinator.Circuit.MaxTx ||
verifier.NLevels != cfg.Coordinator.Circuit.NLevels {
return nil, common.Wrap(
fmt.Errorf("circuit config and verifier params don't match. "+
"circuit.MaxTx = %v, circuit.NLevels = %v, "+
"verifier.MaxTx = %v, verifier.NLevels = %v",
cfg.Coordinator.Circuit.MaxTx, cfg.Coordinator.Circuit.NLevels,
verifier.MaxTx, verifier.NLevels,
))
}
}

coord, err = coordinator.NewCoordinator(
coordinator.Config{
Expand All @@ -447,7 +405,7 @@ func NewNode(cfg *config.Node, version string) (*Node, error) {
GasPriceIncPerc: cfg.Coordinator.EthClient.GasPriceIncPerc,
TxManagerCheckInterval: cfg.Coordinator.EthClient.CheckLoopInterval.Duration,
DebugBatchPath: cfg.Coordinator.Debug.BatchPath,
ForgeBatchGasCost: cfg.Coordinator.EthClient.ForgeBatchGasCost,
ForgeBatchGasCost: cfg.Coordinator.EthClient.ForgeBatchGasCost,
// VerifierIdx: uint8(verifierIdx),
TxProcessorConfig: txProcessorCfg,
ProverReadTimeout: cfg.Coordinator.ProverWaitReadTimeout.Duration,
Expand Down
22 changes: 3 additions & 19 deletions sequencer/synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ type Config struct {
StatsUpdateBlockNumDiffThreshold uint16
StatsUpdateFrequencyDivider uint16
ChainID uint64
StartBlockNum int64
Intizar-T marked this conversation as resolved.
Show resolved Hide resolved
}

// Synchronizer implements the Synchronizer type
Expand Down Expand Up @@ -155,19 +156,14 @@ func NewSynchronizer(
Rollup: *rollupConstants,
}

initVars, startBlockNum, err := getInitialVariables(ethClient /*&consts*/)
if err != nil {
return nil, common.Wrap(err)
}

startBlockNum := cfg.StartBlockNum
Intizar-T marked this conversation as resolved.
Show resolved Hide resolved
stats := NewStatsHolder(startBlockNum, cfg.StatsUpdateBlockNumDiffThreshold, cfg.StatsUpdateFrequencyDivider)
s := &Synchronizer{
EthClient: ethClient,
consts: &consts,
historyDB: historyDB,
stateDB: stateDB,
cfg: cfg,
initVars: *initVars,
startBlockNum: startBlockNum,
stats: stats,
}
Expand Down Expand Up @@ -249,6 +245,7 @@ func (s *Synchronizer) Sync(ctx context.Context,
// Get lastSavedBlock from History DB
lastSavedBlock, err = s.historyDB.GetLastBlock()
if err != nil && common.Unwrap(err) != sql.ErrNoRows {
log.Errorw("Sync GetLastBlock", "err", err)
return nil, nil, common.Wrap(err)
}
// If we don't have any stored block, we must do a full sync
Expand Down Expand Up @@ -443,19 +440,6 @@ func (s *Synchronizer) reorg(uncleBlock *common.Block) (int64, error) {
return block.Num, nil
}

func getInitialVariables(ethClient eth.ClientInterface,

/*consts *common.SCConsts*/) (*common.SCVariables, int64, error) {
rollupInit, rollupInitBlock, err := ethClient.RollupEventInit(77877) //TODO: Check this with hermuz code
if err != nil {
return nil, 0, common.Wrap(fmt.Errorf("RollupEventInit: %w", err))
}
rollupVars := rollupInit.RollupVariables()
return &common.SCVariables{
Rollup: *rollupVars,
}, rollupInitBlock, nil
}

func (s *Synchronizer) init() error {
// Update stats parameters so that they have valid values before the
// first Sync call
Expand Down
12 changes: 4 additions & 8 deletions sequencer/synchronizer/synchronizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func TestMain(m *testing.M) {
os.Exit(exitVal)
}

func newTestModules(t *testing.T) (*statedb.StateDB, *historydb.HistoryDB ) {
func newTestModules(t *testing.T) (*statedb.StateDB, *historydb.HistoryDB) {
// Int State DB
dir, err := os.MkdirTemp("", "tmpdb")
require.NoError(t, err)
Expand All @@ -261,7 +261,6 @@ func newTestModules(t *testing.T) (*statedb.StateDB, *historydb.HistoryDB ) {
require.NoError(t, err)
historyDB := historydb.NewHistoryDB(db, db /*, nil*/)


t.Cleanup(func() {
test.MigrationsDownTest(historyDB.DB())
stateDB.Close()
Expand Down Expand Up @@ -296,6 +295,7 @@ func TestSyncGeneral(t *testing.T) {
Config{
StatsUpdateBlockNumDiffThreshold: 100,
StatsUpdateFrequencyDivider: 100,
StartBlockNum: 1,
},
)
require.NoError(t, err)
Expand All @@ -319,8 +319,8 @@ func TestSyncGeneral(t *testing.T) {
assert.Equal(t, int64(1), stats.Eth.FirstBlockNum)
assert.Equal(t, int64(1), stats.Eth.LastBlock.Num)
assert.Equal(t, int64(1), stats.Sync.LastBlock.Num)
vars := s.SCVars()
assert.Equal(t, *clientSetup.RollupVariables, vars.Rollup)
// vars := s.SCVars()
// assert.Equal(t, *clientSetup.RollupVariables, vars.Rollup)

dbBlocks, err := s.historyDB.GetAllBlocks()
require.NoError(t, err)
Expand Down Expand Up @@ -490,8 +490,6 @@ func TestSyncGeneral(t *testing.T) {
assert.Equal(t, int64(1), stats.Eth.FirstBlockNum)
assert.Equal(t, int64(4), stats.Eth.LastBlock.Num)
assert.Equal(t, int64(4), stats.Sync.LastBlock.Num)
vars = s.SCVars()
assert.Equal(t, *clientSetup.RollupVariables, vars.Rollup)

// dbExits, err := s.historyDB.GetAllExits()
// require.NoError(t, err)
Expand Down Expand Up @@ -529,8 +527,6 @@ func TestSyncGeneral(t *testing.T) {
assert.Equal(t, int64(1), stats.Eth.FirstBlockNum)
assert.Equal(t, int64(5), stats.Eth.LastBlock.Num)
assert.Equal(t, int64(5), stats.Sync.LastBlock.Num)
vars = s.SCVars()
assert.NotEqual(t, clientSetup.RollupVariables, vars.Rollup)

dbRollupVars, err := s.historyDB.GetSCVars()
require.NoError(t, err)
Expand Down
Loading