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

rfct/l1coordtxs #45

Merged
merged 1 commit into from
Jan 8, 2025
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
56 changes: 1 addition & 55 deletions sequencer/common/l1tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,64 +265,10 @@ func L1TxFromDataAvailability(b []byte, nLevels uint32) (*L1Tx, error) {
return &l1tx, Wrap(err)
}

// TODO: Remove this, In the PR to initialize the coordinator setup, pipeline creation and helper function addition
// L1CoordinatorTxFromBytes decodes a L1Tx from []byte
func L1CoordinatorTxFromBytes(b []byte, chainID *big.Int, tokamakAddress ethCommon.Address) (*L1Tx,
error) {
if len(b) != RollupConstL1CoordinatorTotalBytes {
return nil, Wrap(
fmt.Errorf("cannot parse L1CoordinatorTx bytes, expected length %d, current: %d",
101, len(b)))
}

tx := &L1Tx{
UserOrigin: false,
}
v := b[0]
s := b[1:33]
r := b[33:65]
pkCompB := b[65:97]
pkCompL := SwapEndianness(pkCompB)
copy(tx.FromBJJ[:], pkCompL)
tx.Amount = big.NewInt(0)
tx.DepositAmount = big.NewInt(0)
if int(v) > 0 {
// L1CoordinatorTX ETH
// Ethereum adds 27 to v
v = b[0] - byte(27) //nolint:gomnd
var signature []byte
signature = append(signature, r[:]...)
signature = append(signature, s[:]...)
signature = append(signature, v)

accCreationAuth := AccountCreationAuth{
BJJ: tx.FromBJJ,
}
h, err := accCreationAuth.HashToSign(uint16(chainID.Uint64()), tokamakAddress)
if err != nil {
return nil, Wrap(err)
}

pubKeyBytes, err := ethCrypto.Ecrecover(h, signature)
if err != nil {
return nil, Wrap(err)
}
pubKey, err := ethCrypto.UnmarshalPubkey(pubKeyBytes)
if err != nil {
return nil, Wrap(err)
}
tx.FromEthAddr = ethCrypto.PubkeyToAddress(*pubKey)
} else {
// L1Coordinator Babyjub
tx.FromEthAddr = RollupConstEthAddressInternalOnly
}
return tx, nil
}

// BytesGeneric returns the generic representation of a L1Tx. This method is
// used to compute the []byte representation of a L1UserTx, and also to compute
// the L1TxData for the ZKInputs (at the HashGlobalInputs), using this method
// for L1CoordinatorTxs & L1UserTxs (for the ZKInputs case).
// for L1UserTxs (for the ZKInputs case).
func (tx *L1Tx) BytesGeneric() ([]byte, error) {
var b [RollupConstL1UserTotalBytes]byte
copy(b[0:20], tx.FromEthAddr.Bytes())
Expand Down
2 changes: 0 additions & 2 deletions sequencer/coordinator/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ type BatchInfo struct {
// L1Batch bool
VerifierIdx uint8
L1UserTxs []common.L1Tx
// L1CoordTxs []common.L1Tx
L1CoordinatorTxsAuths [][]byte

ForgeBatchArgs *eth.RollupForgeBatchArgs
Auth *bind.TransactOpts `json:"-"`
Expand Down
17 changes: 7 additions & 10 deletions sequencer/coordinator/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ func (p *Pipeline) forgeBatch(batchNum common.BatchNum) (
batchInfo.Debug.StartBlockNum = p.stats.Eth.LastBlock.Num + 1

var l1UserTxs []common.L1Tx
var auths [][]byte

_l1UserTxs, err := p.historyDB.GetUnforgedL1UserTxs(p.state.lastForgeL1TxsNum + 1)
if err != nil {
Expand All @@ -224,7 +223,7 @@ func (p *Pipeline) forgeBatch(batchNum common.BatchNum) (
}

// TODO: figure out what happens here and potentially remove txSelector
auths, l1UserTxs, err =
_, l1UserTxs, err =
p.txSelector.GetL1TxSelection(p.cfg.TxProcessorConfig, _l1UserTxs, l1UserFutureTxs)
if err != nil {
return nil, nil, common.Wrap(err)
Expand All @@ -245,7 +244,6 @@ func (p *Pipeline) forgeBatch(batchNum common.BatchNum) (

// Save metadata from TxSelector output for BatchNum
batchInfo.L1UserTxs = l1UserTxs
batchInfo.L1CoordinatorTxsAuths = auths

// Call BatchBuilder with TxSelector output
configBatch := &batchbuilder.ConfigBatch{
Expand Down Expand Up @@ -446,13 +444,12 @@ func prepareForgeBatchArgs(batchInfo *BatchInfo) *eth.RollupForgeBatchArgs {
proof := batchInfo.Proof
zki := batchInfo.ZKInputs
return &eth.RollupForgeBatchArgs{
NewLastIdx: int64(zki.NewLastIdxRaw),
NewAccountRoot: zki.NewAccountRootRaw.BigInt(),
NewVouchRoot: zki.NewVouchRootRaw.BigInt(),
NewScoreRoot: zki.NewScoreRootRaw.BigInt(),
NewExitRoot: zki.NewExitRootRaw.BigInt(),
L1UserTxs: batchInfo.L1UserTxs,
L1CoordinatorTxsAuths: batchInfo.L1CoordinatorTxsAuths,
NewLastIdx: int64(zki.NewLastIdxRaw),
NewAccountRoot: zki.NewAccountRootRaw.BigInt(),
NewVouchRoot: zki.NewVouchRootRaw.BigInt(),
NewScoreRoot: zki.NewScoreRootRaw.BigInt(),
NewExitRoot: zki.NewExitRootRaw.BigInt(),
L1UserTxs: batchInfo.L1UserTxs,
// Circuit selector
VerifierIdx: batchInfo.VerifierIdx,
ProofA: [2]*big.Int{proof.PiA[0], proof.PiA[1]},
Expand Down
17 changes: 0 additions & 17 deletions sequencer/database/historydb/historydb.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,23 +554,6 @@ func (hdb *HistoryDB) GetAllL1UserTxs() ([]common.L1Tx, error) {
return database.SlicePtrsToSlice(txs).([]common.L1Tx), common.Wrap(err)
}

// GetAllL1CoordinatorTxs returns all L1CoordinatorTxs from the DB
func (hdb *HistoryDB) GetAllL1CoordinatorTxs() ([]common.L1Tx, error) {
var txs []*common.L1Tx
// Since the query specifies that only coordinator txs are returned, it's safe to assume
// that returned txs will always have effective amounts
err := meddler.QueryAll(
hdb.dbRead, &txs,
`SELECT tx.id, tx.to_forge_l1_txs_num, tx.position, tx.user_origin,
tx.from_idx, tx.effective_from_idx, tx.from_eth_addr, tx.from_bjj, tx.to_idx,
tx.amount, tx.amount AS effective_amount,
tx.deposit_amount, tx.deposit_amount AS effective_deposit_amount,
tx.eth_block_num, tx.type, tx.batch_num
FROM tx WHERE is_l1 = TRUE AND user_origin = FALSE ORDER BY item_id;`,
)
return database.SlicePtrsToSlice(txs).([]common.L1Tx), common.Wrap(err)
}

// GetUnforgedL1UserTxs gets L1 User Txs to be forged in the L1Batch with toForgeL1TxsNum.
func (hdb *HistoryDB) GetUnforgedL1UserTxs(toForgeL1TxsNum int64) ([]common.L1Tx, error) {
var txs []*common.L1Tx
Expand Down
78 changes: 21 additions & 57 deletions sequencer/eth/rollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,17 @@ func NewRollupEvents() RollupEvents {

// RollupForgeBatchArgs are the arguments to the ForgeBatch function in the Rollup Smart Contract
type RollupForgeBatchArgs struct {
NewLastIdx int64
NewAccountRoot *big.Int
NewScoreRoot *big.Int
NewVouchRoot *big.Int
NewExitRoot *big.Int
L1UserTxs []common.L1Tx
L1CoordinatorTxs []common.L1Tx
L1CoordinatorTxsAuths [][]byte // Authorization for accountCreations for each L1CoordinatorTx
// Circuit selector
VerifierIdx uint8
L1Batch bool
ProofA [2]*big.Int
ProofB [2][2]*big.Int
ProofC [2]*big.Int
NewLastIdx int64
NewAccountRoot *big.Int
NewScoreRoot *big.Int
NewVouchRoot *big.Int
NewExitRoot *big.Int
L1UserTxs []common.L1Tx
VerifierIdx uint8
L1Batch bool
ProofA [2]*big.Int
ProofB [2][2]*big.Int
ProofC [2]*big.Int
}

// RollupForgeBatchArgsAux are the arguments to the ForgeBatch function in the Rollup Smart Contract
Expand Down Expand Up @@ -569,23 +566,20 @@ func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash,
return nil, nil, common.Wrap(err)
}
rollupForgeBatchArgs := RollupForgeBatchArgs{
L1Batch: aux.L1Batch,
NewExitRoot: aux.NewExitRoot,
NewLastIdx: aux.NewLastIdx.Int64(),
NewAccountRoot: aux.NewAccountRoot,
NewVouchRoot: aux.NewVouchRoot,
NewScoreRoot: aux.NewScoreRoot,
ProofA: aux.ProofA,
ProofB: aux.ProofB,
ProofC: aux.ProofC,
VerifierIdx: aux.VerifierIdx,
L1CoordinatorTxs: []common.L1Tx{},
L1CoordinatorTxsAuths: [][]byte{},
L1Batch: aux.L1Batch,
NewExitRoot: aux.NewExitRoot,
NewLastIdx: aux.NewLastIdx.Int64(),
NewAccountRoot: aux.NewAccountRoot,
NewVouchRoot: aux.NewVouchRoot,
NewScoreRoot: aux.NewScoreRoot,
ProofA: aux.ProofA,
ProofB: aux.ProofB,
ProofC: aux.ProofC,
VerifierIdx: aux.VerifierIdx,
}
nLevels := c.consts.Verifiers[rollupForgeBatchArgs.VerifierIdx].NLevels
lenL1TxsBytes := int((nLevels/8)*2 + common.Float40BytesLength + 1) //nolint:gomnd
numBytesL1TxUser := int(l1UserTxsLen) * lenL1TxsBytes
numTxsL1Coord := len(aux.EncodedL1CoordinatorTx) / common.RollupConstL1CoordinatorTotalBytes
l1UserTxsData := []byte{}
if l1UserTxsLen > 0 {
l1UserTxsData = aux.L1L2TxsData[:numBytesL1TxUser]
Expand All @@ -599,35 +593,5 @@ func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash,
}
rollupForgeBatchArgs.L1UserTxs = append(rollupForgeBatchArgs.L1UserTxs, *l1Tx)
}
for i := 0; i < numTxsL1Coord; i++ {
bytesL1Coordinator :=
aux.EncodedL1CoordinatorTx[i*common.RollupConstL1CoordinatorTotalBytes : (i+1)*common.RollupConstL1CoordinatorTotalBytes] //nolint:lll
var signature []byte
v := bytesL1Coordinator[0]
s := bytesL1Coordinator[1:33]
r := bytesL1Coordinator[33:65]
signature = append(signature, r[:]...)
signature = append(signature, s[:]...)
signature = append(signature, v)
l1Tx, err := common.L1CoordinatorTxFromBytes(bytesL1Coordinator, c.chainID, c.address)
if err != nil {
return nil, nil, common.Wrap(err)
}
rollupForgeBatchArgs.L1CoordinatorTxs = append(rollupForgeBatchArgs.L1CoordinatorTxs, *l1Tx)
rollupForgeBatchArgs.L1CoordinatorTxsAuths =
append(rollupForgeBatchArgs.L1CoordinatorTxsAuths, signature)
}
lenFeeIdxCoordinatorBytes := int(nLevels / 8) //nolint:gomnd
numFeeIdxCoordinator := len(aux.FeeIdxCoordinator) / lenFeeIdxCoordinatorBytes
for i := 0; i < numFeeIdxCoordinator; i++ {
var paddedFeeIdx [6]byte
if lenFeeIdxCoordinatorBytes < common.AccountIdxBytesLen {
copy(paddedFeeIdx[6-lenFeeIdxCoordinatorBytes:],
aux.FeeIdxCoordinator[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
} else {
copy(paddedFeeIdx[:],
aux.FeeIdxCoordinator[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
}
}
return &rollupForgeBatchArgs, &sender, nil
}
2 changes: 0 additions & 2 deletions sequencer/synchronizer/synchronizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ func checkSyncBlock(t *testing.T, s *Synchronizer, blockNum int, block,
require.NoError(t, err)
dbExits, err := s.historyDB.GetAllExits()
require.NoError(t, err)
// dbL1CoordinatorTxs := []common.L1Tx{}
for i, batch := range block.Rollup.Batches {
var dbBatch *common.Batch
// Find batch in DB output
Expand Down Expand Up @@ -352,7 +351,6 @@ func TestSyncGeneral(t *testing.T) {
require.Equal(t, 2, int(blocks[i].Block.Num))
require.Equal(t, 4, len(blocks[i].Rollup.L1UserTxs))
require.Equal(t, 2, len(blocks[i].Rollup.Batches))
// require.Equal(t, 2, len(blocks[i].Rollup.Batches[0].L1CoordinatorTxs))

// Set StateRoots for batches manually (til doesn't set it)
blocks[i].Rollup.Batches[0].Batch.AccountRoot =
Expand Down
7 changes: 0 additions & 7 deletions sequencer/test/ethClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,20 +848,13 @@ func (c *Client) CtlAddBlocks(blocks []common.BlockData) (err error) {
}
c.CtlSetAddr(ethCommon.HexToAddress("0xE39fEc6224708f0772D2A74fd3f9055A90E0A9f2"))
for _, batch := range block.Rollup.Batches {
// auths := make([][]byte, len(batch.L1CoordinatorTxs))
// for i := range auths {
// auths[i] = make([]byte, 65)
// }
if _, err := c.RollupForgeBatch(&eth.RollupForgeBatchArgs{
NewLastIdx: batch.Batch.LastIdx,

NewAccountRoot: batch.Batch.AccountRoot,
NewVouchRoot: batch.Batch.VouchRoot,
NewScoreRoot: batch.Batch.ScoreRoot,
NewExitRoot: batch.Batch.ExitRoot,
// L1CoordinatorTxs: batch.L1CoordinatorTxs,
// L1CoordinatorTxsAuths: auths,
// L2TxsData: batch.L2Txs,
// Circuit selector
VerifierIdx: 0, // Intentionally empty
L1Batch: batch.L1Batch,
Expand Down
Loading