Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiden-araki committed Dec 16, 2024
2 parents 7e8bf12 + 160c752 commit bf391eb
Show file tree
Hide file tree
Showing 18 changed files with 148 additions and 92 deletions.
10 changes: 4 additions & 6 deletions sequencer/common/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ type Batch struct {
EthBlockNum int64 `meddler:"eth_block_num"`
ForgerAddr ethCommon.Address `meddler:"forger_addr"`

// TODO: implement
StateRoot *big.Int `meddler:"state_root,bigint"`
// AccountStateRoot *big.Int `meddler:"state_root,bigint"`
// VouchStateRoot *big.Int `meddler:"state_root,bigint"`
// ScoreStateRoot *big.Int `meddler:"state_root,bigint"`
AccountRoot *big.Int `meddler:"account_root,bigint"`
VouchRoot *big.Int `meddler:"vouch_root,bigint"`
ScoreRoot *big.Int `meddler:"score_root,bigint"`
ExitRoot *big.Int `meddler:"exit_root,bigint"`

NumAccounts int `meddler:"num_accounts"`
LastIdx int64 `meddler:"last_idx"`
ExitRoot *big.Int `meddler:"exit_root,bigint"`
GasUsed uint64 `meddler:"gas_used"`
GasPrice *big.Int `meddler:"gas_price,bigint"`
EtherPriceUSD float64 `meddler:"ether_price_usd"`
Expand Down
32 changes: 12 additions & 20 deletions sequencer/coordinator/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,9 @@ type fromBatch struct {
BatchNum common.BatchNum
ForgerAddr ethCommon.Address

// TODO: implement
StateRoot *big.Int
// AccountStateRoot *big.Int
// VouchStateRoot *big.Int
// ScoreStateRoot *big.Int
AccountRoot *big.Int
VouchRoot *big.Int
ScoreRoot *big.Int
}

// Coordinator implements the Coordinator type
Expand Down Expand Up @@ -270,11 +268,9 @@ func NewCoordinator(cfg Config,
BatchNum: 0,
ForgerAddr: ethCommon.Address{},

// TODO: implement
StateRoot: big.NewInt(0),
// AccountStateRoot: big.NewInt(0),
// VouchStateRoot: big.NewInt(0),
// ScoreStateRoot: big.NewInt(0),
AccountRoot: big.NewInt(0),
VouchRoot: big.NewInt(0),
ScoreRoot: big.NewInt(0),
},
prover: prover,
consts: *scConsts,
Expand Down Expand Up @@ -470,21 +466,17 @@ func (c *Coordinator) syncStats(ctx context.Context, stats *synchronizer.Stats)
BatchNum: stats.Sync.LastBatch.BatchNum,
ForgerAddr: stats.Sync.LastBatch.ForgerAddr,

// TODO: implement
StateRoot: stats.Sync.LastBatch.StateRoot,
// AccountStateRoot: stats.Sync.LastBatch.AccountStateRoot,
// VouchStateRoot: stats.Sync.LastBatch.VouchStateRoot,
// ScoreStateRoot: stats.Sync.LastBatch.ScoreStateRoot,
AccountRoot: stats.Sync.LastBatch.AccountRoot,
VouchRoot: stats.Sync.LastBatch.VouchRoot,
ScoreRoot: stats.Sync.LastBatch.ScoreRoot,
}
if c.lastNonFailedBatchNum > fromBatch.BatchNum {
fromBatch.BatchNum = c.lastNonFailedBatchNum
fromBatch.ForgerAddr = c.cfg.ForgerAddress

// TODO: implement
fromBatch.StateRoot = big.NewInt(0)
// fromBatch.AccountStateRoot = big.NewInt(0)
// fromBatch.VouchStateRoot = big.NewInt(0)
// fromBatch.ScoreStateRoot = big.NewInt(0)
fromBatch.AccountRoot = big.NewInt(0)
fromBatch.VouchRoot = big.NewInt(0)
fromBatch.ScoreRoot = big.NewInt(0)
}

var err error
Expand Down
2 changes: 1 addition & 1 deletion sequencer/database/historydb/apiqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (hdb *HistoryDB) getBatchAPI(d meddler.DB, batchNum common.BatchNum) (*Batc
if err := meddler.QueryRow(
d, batch,
`SELECT batch.item_id, batch.batch_num, batch.eth_block_num,
batch.forger_addr, batch.state_root, batch.num_accounts, batch.exit_root, batch.forge_l1_txs_num,
batch.forger_addr, batch.account_root, batch.score_root, batch.vouch_root, batch.num_accounts, batch.exit_root, batch.forge_l1_txs_num,
COALESCE(batch.eth_tx_hash, DECODE('0000000000000000000000000000000000000000000000000000000000000000', 'hex')) as eth_tx_hash,
block.timestamp, block.hash, COALESCE ((SELECT COUNT(*) FROM tx WHERE batch_num = batch.batch_num), 0) AS forged_txs
FROM batch INNER JOIN block ON batch.eth_block_num = block.eth_block_num
Expand Down
12 changes: 8 additions & 4 deletions sequencer/database/historydb/historydb.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ func (hdb *HistoryDB) GetLastBatch() (*common.Batch, error) {
var batch common.Batch
err := meddler.QueryRow(
hdb.dbRead, &batch, `SELECT batch.batch_num, batch.eth_block_num, batch.forger_addr,
batch.state_root,
batch.account_root,
batch.vouch_root,
batch.score_root,
batch.num_accounts, batch.last_idx, batch.exit_root, batch.forge_l1_txs_num,
batch.slot_num, batch.total_fees_usd, batch.gas_price, batch.gas_used, batch.ether_price_usd
FROM batch ORDER BY batch_num DESC LIMIT 1;`,
Expand Down Expand Up @@ -207,7 +209,7 @@ func (hdb *HistoryDB) GetAllBatches() ([]common.Batch, error) {
err := meddler.QueryAll(
hdb.dbRead, &batches,
`SELECT batch.batch_num, batch.eth_block_num, batch.forger_addr,
batch.state_root, batch.num_accounts, batch.last_idx, batch.exit_root,
batch.account_root, batch.vouch_root, batch.score_root, batch.num_accounts, batch.last_idx, batch.exit_root,
batch.forge_l1_txs_num, batch.slot_num, batch.total_fees_usd, batch.eth_tx_hash FROM batch
ORDER BY item_id;`,
)
Expand All @@ -220,7 +222,7 @@ func (hdb *HistoryDB) GetBatches(from, to common.BatchNum) ([]common.Batch, erro
err := meddler.QueryAll(
hdb.dbRead, &batches,
`SELECT batch_num, eth_block_num, forger_addr,
state_root, num_accounts, last_idx, exit_root, forge_l1_txs_num, slot_num, total_fees_usd, gas_price, gas_used, ether_price_usd
account_root, vouch_root, score_root, num_accounts, last_idx, exit_root, forge_l1_txs_num, slot_num, total_fees_usd, gas_price, gas_used, ether_price_usd
FROM batch WHERE $1 <= batch_num AND batch_num < $2 ORDER BY batch_num;`,
from, to,
)
Expand Down Expand Up @@ -250,7 +252,9 @@ func (hdb *HistoryDB) GetBatch(batchNum common.BatchNum) (*common.Batch, error)
var batch common.Batch
err := meddler.QueryRow(
hdb.dbRead, &batch, `SELECT batch.batch_num, batch.eth_block_num, batch.forger_addr,
batch.state_root,
batch.account_root,
batch.score_root,
batch.vouch_root,
batch.num_accounts, batch.last_idx, batch.exit_root, batch.forge_l1_txs_num,
batch.slot_num, batch.total_fees_usd, batch.gas_price, batch.gas_used, batch.ether_price_usd
FROM batch WHERE batch_num = $1;`,
Expand Down
4 changes: 3 additions & 1 deletion sequencer/database/historydb/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ type BatchAPI struct {
CollectedFeesDB map[common.TokenID]*big.Int `json:"-" meddler:"fees_collected,json"`
CollectedFeesAPI apitypes.CollectedFeesAPI `json:"collectedFees" meddler:"-"`
TotalFeesUSD *float64 `json:"historicTotalCollectedFeesUSD" meddler:"total_fees_usd"`
StateRoot apitypes.BigIntStr `json:"stateRoot" meddler:"state_root"`
AccountRoot apitypes.BigIntStr `json:"accountRoot" meddler:"account_root"`
VouchRoot apitypes.BigIntStr `json:"vouchRoot" meddler:"vouch_root"`
ScoreRoot apitypes.BigIntStr `json:"scoreRoot" meddler:"score_root"`
NumAccounts int `json:"numAccounts" meddler:"num_accounts"`
ExitRoot apitypes.BigIntStr `json:"exitRoot" meddler:"exit_root"`
ForgeL1TxsNum *int64 `json:"forgeL1TransactionsNum" meddler:"forge_l1_txs_num"`
Expand Down
8 changes: 6 additions & 2 deletions sequencer/database/migrations/0001.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ CREATE TABLE batch (
forger_addr BYTEA NOT NULL, -- fake foreign key for coordinator
-- fees_collected BYTEA NOT NULL,
-- fee_idxs_coordinator BYTEA NOT NULL,
state_root DECIMAL(78,0) NOT NULL,

account_root DECIMAL(78,0) NOT NULL,
vouch_root DECIMAL(78,0) NOT NULL,
score_root DECIMAL(78,0) NOT NULL,
exit_root DECIMAL(78,0) NOT NULL,

num_accounts BIGINT NOT NULL,
last_idx BIGINT NOT NULL,
exit_root DECIMAL(78,0) NOT NULL,
forge_l1_txs_num BIGINT,
slot_num BIGINT NOT NULL,
total_fees_usd NUMERIC
Expand Down
4 changes: 3 additions & 1 deletion sequencer/database/migrations/0002_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func (m migrationTest0002) InsertData(db *sqlx.DB) error {
batch_num,
eth_block_num,
forger_addr,
state_root,
account_root,
vouch_root,
score_root,
num_accounts,
last_idx,
exit_root,
Expand Down
12 changes: 9 additions & 3 deletions sequencer/database/migrations/0003_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ func (m migrationTest0003) InsertData(db *sqlx.DB) error {
batch_num,
eth_block_num,
forger_addr,
state_root,
account_root,
vouch_root,
score_root,
num_accounts,
last_idx,
exit_root,
Expand Down Expand Up @@ -52,7 +54,9 @@ func (m migrationTest0003) RunAssertsAfterMigrationUp(t *testing.T, db *sqlx.DB)
batch_num = 6758 AND
eth_block_num = 4417296 AND
forger_addr = decode('459264CC7D2BF350AFDDA828C273E81367729C1F', 'hex') AND
state_root = 12898140512818699175738765060248919016800434587665040485377676113605873428098 AND
account_root = 12898140512818699175738765060248919016800434587665040485377676113605873428098 AND
vouch_root = 12898140512818699175738765060248919016800434587665040485377676113605873428098 AND
score_root = 12898140512818699175738765060248919016800434587665040485377676113605873428098 AND
num_accounts = 256 AND
last_idx = 1044 AND
exit_root = 0 AND
Expand All @@ -73,7 +77,9 @@ func (m migrationTest0003) RunAssertsAfterMigrationDown(t *testing.T, db *sqlx.D
batch_num = 6758 AND
eth_block_num = 4417296 AND
forger_addr = decode('459264CC7D2BF350AFDDA828C273E81367729C1F', 'hex') AND
state_root = 12898140512818699175738765060248919016800434587665040485377676113605873428098 AND
account_root = 12898140512818699175738765060248919016800434587665040485377676113605873428098 AND
vouch_root = 12898140512818699175738765060248919016800434587665040485377676113605873428098 AND
score_root = 12898140512818699175738765060248919016800434587665040485377676113605873428098 AND
num_accounts = 256 AND
last_idx = 1044 AND
exit_root = 0 AND
Expand Down
4 changes: 3 additions & 1 deletion sequencer/database/migrations/0006_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func (m migrationTest0006) InsertData(db *sqlx.DB) error {
batch_num,
eth_block_num,
forger_addr,
state_root,
account_root,
vouch_root,
score_root,
num_accounts,
last_idx,
exit_root,
Expand Down
4 changes: 3 additions & 1 deletion sequencer/database/migrations/0007_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func (m migrationTest0007) InsertData(db *sqlx.DB) error {
batch_num,
eth_block_num,
forger_addr,
state_root,
account_root,
vouch_root,
score_root,
num_accounts,
last_idx,
exit_root,
Expand Down
12 changes: 10 additions & 2 deletions sequencer/database/migrations/0010_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@ func (m migrationTest0010) InsertData(db *sqlx.DB) error {
INSERT INTO block (eth_block_num, "timestamp", hash)
VALUES(206, '2021-08-16 10:34:42.000', decode('29F4F4128E6E8165DB13A1638C9C0B52B759B8FAEA9F339754D10F0050F3ACA2','hex'));
INSERT INTO batch (item_id, batch_num, eth_block_num, forger_addr, state_root, num_accounts, last_idx, exit_root, forge_l1_txs_num, slot_num, total_fees_usd, eth_tx_hash)
INSERT INTO batch (item_id, batch_num, eth_block_num, forger_addr,
account_root,
vouch_root,
score_root,
num_accounts, last_idx, exit_root, forge_l1_txs_num, slot_num, total_fees_usd, eth_tx_hash)
VALUES(6, 6, 197, decode('DCC5DD922FB1D0FD0C450A0636A8CE827521F0ED','hex'), decode('7B7D0A','hex'), decode('5B5D0A','hex'), 18824406808947086769639114603381416975270151877490721532469555446804684467592, 1, 256, 0, 5, 2, 0, decode('21BCAEC5472E6F578A5938516A76B438AF7D1739CB10C65A86D5F0FCF19A514F','hex'));
INSERT INTO batch (item_id, batch_num, eth_block_num, forger_addr, state_root, num_accounts, last_idx, exit_root, forge_l1_txs_num, slot_num, total_fees_usd, eth_tx_hash)
INSERT INTO batch (item_id, batch_num, eth_block_num, forger_addr,
account_root,
vouch_root,
score_root,
num_accounts, last_idx, exit_root, forge_l1_txs_num, slot_num, total_fees_usd, eth_tx_hash)
VALUES(7, 7, 206, decode('DCC5DD922FB1D0FD0C450A0636A8CE827521F0ED','hex'), decode('7B7D0A','hex'), decode('5B5D0A','hex'), 7234362616256345251637054085980477919716486801577186942795287784595347908748, 1, 257, 0, 6, 3, 0, decode('7BEFD7FEE85583F26C236BAF3DAA98D0C35E60F5DBCF53865230B067ABFD31E1','hex'));
INSERT INTO token (item_id, token_id, eth_block_num, eth_addr, "name", symbol, decimals, usd, usd_update)
Expand Down
18 changes: 15 additions & 3 deletions sequencer/database/migrations/0011_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ func (m migrationTest0011) InsertData(db *sqlx.DB) error {
VALUES(48278, '2021-09-13 08:28:39.000', decode('2AB24E7021318D6CF0686E8F8FBFB0A63CB79A9FB5CDECE7C09FD4438E67242E','hex'));
INSERT INTO batch
(item_id, batch_num, eth_block_num, forger_addr, state_root, num_accounts, last_idx, exit_root, forge_l1_txs_num, slot_num, total_fees_usd, eth_tx_hash)
(item_id, batch_num, eth_block_num, forger_addr,
account_root,
vouch_root,
score_root,
num_accounts, last_idx, exit_root, forge_l1_txs_num, slot_num, total_fees_usd, eth_tx_hash)
VALUES(1420, 1420, 48295, decode('DCC5DD922FB1D0FD0C450A0636A8CE827521F0ED','hex'), decode('7B7D0A','hex'), decode('5B5D0A','hex'), 0, 0, 255, 0, 1419, 1205, 0, decode('AE80AB27E97213DEC805C78ED9C637E0414A541D489377F766B3372170F4AD66','hex'));
INSERT INTO batch
(item_id, batch_num, eth_block_num, forger_addr, state_root, num_accounts, last_idx, exit_root, forge_l1_txs_num, slot_num, total_fees_usd, eth_tx_hash)
(item_id, batch_num, eth_block_num, forger_addr,
account_root,
vouch_root,
score_root,
num_accounts, last_idx, exit_root, forge_l1_txs_num, slot_num, total_fees_usd, eth_tx_hash)
VALUES(1419, 1419, 48286, decode('DCC5DD922FB1D0FD0C450A0636A8CE827521F0ED','hex'), decode('7B7D0A','hex'), decode('5B5D0A','hex'), 0, 0, 255, 0, 1418, 1205, 0, decode('4BC9C94E8CF93AD475F8C8394BC934AF5EB0802FE4009D13F58AE25F6047DA95','hex'));
`
_, err := db.Exec(queryInsert)
Expand All @@ -44,7 +52,11 @@ func (m migrationTest0011) RunAssertsAfterMigrationUp(t *testing.T, db *sqlx.DB)
assert.Equal(t, 1, result)

insert := `INSERT INTO batch
(item_id, batch_num, eth_block_num, forger_addr, state_root, num_accounts, last_idx, exit_root, forge_l1_txs_num, slot_num, total_fees_usd, eth_tx_hash, gas_price, gas_used, ether_price_usd)
(item_id, batch_num, eth_block_num, forger_addr,
account_root,
vouch_root,
score_root,
num_accounts, last_idx, exit_root, forge_l1_txs_num, slot_num, total_fees_usd, eth_tx_hash, gas_price, gas_used, ether_price_usd)
VALUES(1418, 1418, 48278, decode('DCC5DD922FB1D0FD0C450A0636A8CE827521F0ED','hex'), decode('7B7D0A','hex'), decode('5B5D0A','hex'), 0, 0, 255, 0, 1417, 1204, 0, decode('285CE6A154901AF5197382DC8A5CCE02588BDA1B078768C5077B6996FA2EA0A7','hex'), 500000000000, 15000000, 3492.21);
`
_, err := db.Exec(insert)
Expand Down
24 changes: 16 additions & 8 deletions sequencer/eth/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ func NewQueueStruct() *QueueStruct {

// RollupState represents the state of the Rollup in the Smart Contract
type RollupState struct {
StateRoot *big.Int
ExitRoots []*big.Int
AccountRoot *big.Int
VouchRoot *big.Int
ScoreRoot *big.Int
ExitRoots []*big.Int
// ExitNullifierMap map[[256 / 8]byte]bool
ExitNullifierMap map[int64]map[int64]bool // batchNum -> idx -> bool
MapL1TxQueue map[int64]*QueueStruct
Expand Down Expand Up @@ -169,7 +171,9 @@ func NewRollupEvents() RollupEvents {
// RollupForgeBatchArgs are the arguments to the ForgeBatch function in the Rollup Smart Contract
type RollupForgeBatchArgs struct {
NewLastIdx int64
NewStRoot *big.Int
NewAccountRoot *big.Int
NewScoreRoot *big.Int
NewVouchRoot *big.Int
NewExitRoot *big.Int
L1UserTxs []common.L1Tx
L1CoordinatorTxs []common.L1Tx
Expand All @@ -187,7 +191,9 @@ type RollupForgeBatchArgs struct {
// RollupForgeBatchArgsAux are the arguments to the ForgeBatch function in the Rollup Smart Contract
type rollupForgeBatchArgsAux struct {
NewLastIdx *big.Int
NewStRoot *big.Int
NewAccountRoot *big.Int
NewVouchRoot *big.Int
NewScoreRoot *big.Int
NewExitRoot *big.Int
EncodedL1CoordinatorTx []byte
L1L2TxsData []byte
Expand Down Expand Up @@ -328,9 +334,9 @@ func (c *RollupClient) RollupConstants() (rollupConstants *common.RollupConstant
}
rollupConstants.AbsoluteMaxL1L2BatchTimeout = int64(absoluteMaxL1L2BatchTimeout)
// rollupConstants.TokenHEZ, err = c.tokamak.TokenHEZ(c.opts)
if err != nil {
return common.Wrap(err)
}
// if err != nil {
// return common.Wrap(err)
// }
rollupVerifier, err := c.sybil.RollupVerifiers(c.opts, big.NewInt(0))
if err != nil {
return common.Wrap(err)
Expand Down Expand Up @@ -587,7 +593,9 @@ func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash,
L1Batch: aux.L1Batch,
NewExitRoot: aux.NewExitRoot,
NewLastIdx: aux.NewLastIdx.Int64(),
NewStRoot: aux.NewStRoot,
NewAccountRoot: aux.NewAccountRoot,
NewVouchRoot: aux.NewVouchRoot,
NewScoreRoot: aux.NewScoreRoot,
ProofA: aux.ProofA,
ProofB: aux.ProofB,
ProofC: aux.ProofC,
Expand Down
34 changes: 15 additions & 19 deletions sequencer/synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,19 @@ func (s *StatsHolder) UpdateSync(lastBlock *common.Block, lastBatch *common.Batc
func (s *StatsHolder) CopyStats() *Stats {
s.rw.RLock()
sCopy := s.Stats
if s.Sync.LastBatch.StateRoot != nil {
sCopy.Sync.LastBatch.StateRoot =
common.CopyBigInt(s.Sync.LastBatch.StateRoot)
}

// TODO: implement
// if s.Sync.LastBatch.AccountStateRoot != nil {
// sCopy.Sync.LastBatch.AccountStateRoot =
// common.CopyBigInt(s.Sync.LastBatch.AccountStateRoot)
// }
// if s.Sync.LastBatch.VouchStateRoot != nil {
// sCopy.Sync.LastBatch.VouchStateRoot =
// common.CopyBigInt(s.Sync.LastBatch.VouchStateRoot)
// }
// if s.Sync.LastBatch.ScoreStateRoot != nil {
// sCopy.Sync.LastBatch.ScoreStateRoot =
// common.CopyBigInt(s.Sync.LastBatch.ScoreStateRoot)
// }
if s.Sync.LastBatch.AccountRoot != nil {
sCopy.Sync.LastBatch.AccountRoot =
common.CopyBigInt(s.Sync.LastBatch.AccountRoot)
}
if s.Sync.LastBatch.VouchRoot != nil {
sCopy.Sync.LastBatch.VouchRoot =
common.CopyBigInt(s.Sync.LastBatch.VouchRoot)
}
if s.Sync.LastBatch.ScoreRoot != nil {
sCopy.Sync.LastBatch.ScoreRoot =
common.CopyBigInt(s.Sync.LastBatch.ScoreRoot)
}
s.rw.RUnlock()
return &sCopy
}
Expand Down Expand Up @@ -807,8 +802,9 @@ func (s *Synchronizer) rollupSync(ethBlock *common.Block) (*common.RollupData, e
EthBlockNum: blockNum,
ForgerAddr: *sender,

// TODO: add AccountStateRoot, VouchStateRoot, ScoreStateRoot to Rollup
StateRoot: forgeBatchArgs.NewStRoot,
AccountRoot: forgeBatchArgs.NewAccountRoot,
VouchRoot: forgeBatchArgs.NewVouchRoot,
ScoreRoot: forgeBatchArgs.NewScoreRoot,
NumAccounts: len(batchData.CreatedAccounts),
LastIdx: forgeBatchArgs.NewLastIdx,
ExitRoot: forgeBatchArgs.NewExitRoot,
Expand Down
Loading

0 comments on commit bf391eb

Please sign in to comment.