Update txs constructors and helpers

For each tx, move the logic of setting the Type and TxID to separate functions,
so that they can be called when necessary.

In synchronizer, set all the required fields using the `SetID` and `SetType`
for l2txs when needed.  This is necessary because the `ProcessTxs()` works with
`common.PoolL2Tx`, which misses some fields from `common.L2Tx`, and because
`ProcessTxs()` needs the Type to be set, but at the same time `ProcessTxs()`
sets the Nonce, which is required for the `TxID`.
This commit is contained in:
Eduard S
2020-12-16 15:25:55 +01:00
parent 61255d730c
commit 7a30294688
6 changed files with 207 additions and 157 deletions

View File

@@ -50,74 +50,88 @@ type L1Tx struct {
// NewL1Tx returns the given L1Tx with the TxId & Type parameters calculated // NewL1Tx returns the given L1Tx with the TxId & Type parameters calculated
// from the L1Tx values // from the L1Tx values
func NewL1Tx(l1Tx *L1Tx) (*L1Tx, error) { func NewL1Tx(tx *L1Tx) (*L1Tx, error) {
// calculate TxType txTypeOld := tx.Type
var txType TxType if err := tx.SetType(); err != nil {
if l1Tx.FromIdx == 0 { return nil, err
if l1Tx.ToIdx == Idx(0) {
txType = TxTypeCreateAccountDeposit
} else if l1Tx.ToIdx >= IdxUserThreshold {
txType = TxTypeCreateAccountDepositTransfer
} else {
return l1Tx, tracerr.Wrap(fmt.Errorf("Can not determine type of L1Tx, invalid ToIdx value: %d", l1Tx.ToIdx))
} }
} else if l1Tx.FromIdx >= IdxUserThreshold { // If original Type doesn't match the correct one, return error
if l1Tx.ToIdx == Idx(0) { if txTypeOld != "" && txTypeOld != tx.Type {
txType = TxTypeDeposit return nil, tracerr.Wrap(fmt.Errorf("L1Tx.Type: %s, should be: %s",
} else if l1Tx.ToIdx == Idx(1) { tx.Type, txTypeOld))
txType = TxTypeForceExit
} else if l1Tx.ToIdx >= IdxUserThreshold {
if l1Tx.DepositAmount.Int64() == int64(0) {
txType = TxTypeForceTransfer
} else {
txType = TxTypeDepositTransfer
}
} else {
return l1Tx, tracerr.Wrap(fmt.Errorf("Can not determine type of L1Tx, invalid ToIdx value: %d", l1Tx.ToIdx))
}
} else {
return l1Tx, tracerr.Wrap(fmt.Errorf("Can not determine type of L1Tx, invalid FromIdx value: %d", l1Tx.FromIdx))
} }
if l1Tx.Type != "" && l1Tx.Type != txType { txIDOld := tx.TxID
return l1Tx, tracerr.Wrap(fmt.Errorf("L1Tx.Type: %s, should be: %s", l1Tx.Type, txType)) if err := tx.SetID(); err != nil {
return nil, err
} }
l1Tx.Type = txType // If original TxID doesn't match the correct one, return error
if txIDOld != (TxID{}) && txIDOld != tx.TxID {
txID, err := l1Tx.CalcTxID() return tx, tracerr.Wrap(fmt.Errorf("L1Tx.TxID: %s, should be: %s",
if err != nil { tx.TxID.String(), txIDOld.String()))
return nil, tracerr.Wrap(err)
} }
l1Tx.TxID = *txID
return l1Tx, nil return tx, nil
} }
// CalcTxID calculates the TxId of the L1Tx // SetType sets the type of the transaction
func (tx *L1Tx) CalcTxID() (*TxID, error) { func (tx *L1Tx) SetType() error {
var txID TxID if tx.FromIdx == 0 {
if tx.ToIdx == Idx(0) {
tx.Type = TxTypeCreateAccountDeposit
} else if tx.ToIdx >= IdxUserThreshold {
tx.Type = TxTypeCreateAccountDepositTransfer
} else {
return tracerr.Wrap(fmt.Errorf(
"Can not determine type of L1Tx, invalid ToIdx value: %d", tx.ToIdx))
}
} else if tx.FromIdx >= IdxUserThreshold {
if tx.ToIdx == Idx(0) {
tx.Type = TxTypeDeposit
} else if tx.ToIdx == Idx(1) {
tx.Type = TxTypeForceExit
} else if tx.ToIdx >= IdxUserThreshold {
if tx.DepositAmount.Int64() == int64(0) {
tx.Type = TxTypeForceTransfer
} else {
tx.Type = TxTypeDepositTransfer
}
} else {
return tracerr.Wrap(fmt.Errorf(
"Can not determine type of L1Tx, invalid ToIdx value: %d", tx.ToIdx))
}
} else {
return tracerr.Wrap(fmt.Errorf(
"Can not determine type of L1Tx, invalid FromIdx value: %d", tx.FromIdx))
}
return nil
}
// SetID sets the ID of the transaction. For L1UserTx uses (ToForgeL1TxsNum,
// Position), for L1CoordinatorTx uses (BatchNum, Position).
func (tx *L1Tx) SetID() error {
if tx.UserOrigin { if tx.UserOrigin {
if tx.ToForgeL1TxsNum == nil { if tx.ToForgeL1TxsNum == nil {
return nil, tracerr.Wrap(fmt.Errorf("L1Tx.UserOrigin == true && L1Tx.ToForgeL1TxsNum == nil")) return tracerr.Wrap(fmt.Errorf("L1Tx.UserOrigin == true && L1Tx.ToForgeL1TxsNum == nil"))
} }
txID[0] = TxIDPrefixL1UserTx tx.TxID[0] = TxIDPrefixL1UserTx
var toForgeL1TxsNumBytes [8]byte var toForgeL1TxsNumBytes [8]byte
binary.BigEndian.PutUint64(toForgeL1TxsNumBytes[:], uint64(*tx.ToForgeL1TxsNum)) binary.BigEndian.PutUint64(toForgeL1TxsNumBytes[:], uint64(*tx.ToForgeL1TxsNum))
copy(txID[1:9], toForgeL1TxsNumBytes[:]) copy(tx.TxID[1:9], toForgeL1TxsNumBytes[:])
} else { } else {
if tx.BatchNum == nil { if tx.BatchNum == nil {
return nil, tracerr.Wrap(fmt.Errorf("L1Tx.UserOrigin == false && L1Tx.BatchNum == nil")) return tracerr.Wrap(fmt.Errorf("L1Tx.UserOrigin == false && L1Tx.BatchNum == nil"))
} }
txID[0] = TxIDPrefixL1CoordTx tx.TxID[0] = TxIDPrefixL1CoordTx
var batchNumBytes [8]byte var batchNumBytes [8]byte
binary.BigEndian.PutUint64(batchNumBytes[:], uint64(*tx.BatchNum)) binary.BigEndian.PutUint64(batchNumBytes[:], uint64(*tx.BatchNum))
copy(txID[1:9], batchNumBytes[:]) copy(tx.TxID[1:9], batchNumBytes[:])
} }
var positionBytes [2]byte var positionBytes [2]byte
binary.BigEndian.PutUint16(positionBytes[:], uint16(tx.Position)) binary.BigEndian.PutUint16(positionBytes[:], uint16(tx.Position))
copy(txID[9:11], positionBytes[:]) copy(tx.TxID[9:11], positionBytes[:])
return &txID, nil return nil
} }
// Tx returns a *Tx from the L1Tx // Tx returns a *Tx from the L1Tx

View File

@@ -24,38 +24,57 @@ type L2Tx struct {
// NewL2Tx returns the given L2Tx with the TxId & Type parameters calculated // NewL2Tx returns the given L2Tx with the TxId & Type parameters calculated
// from the L2Tx values // from the L2Tx values
func NewL2Tx(l2Tx *L2Tx) (*L2Tx, error) { func NewL2Tx(tx *L2Tx) (*L2Tx, error) {
// calculate TxType txTypeOld := tx.Type
var txType TxType if err := tx.SetType(); err != nil {
if l2Tx.ToIdx == Idx(1) { return nil, err
txType = TxTypeExit }
} else if l2Tx.ToIdx >= IdxUserThreshold { // If original Type doesn't match the correct one, return error
txType = TxTypeTransfer if txTypeOld != "" && txTypeOld != tx.Type {
return nil, tracerr.Wrap(fmt.Errorf("L2Tx.Type: %s, should be: %s",
tx.Type, txTypeOld))
}
txIDOld := tx.TxID
if err := tx.SetID(); err != nil {
return nil, err
}
// If original TxID doesn't match the correct one, return error
if txIDOld != (TxID{}) && txIDOld != tx.TxID {
return tx, tracerr.Wrap(fmt.Errorf("L2Tx.TxID: %s, should be: %s",
tx.TxID.String(), txIDOld.String()))
}
return tx, nil
}
// SetType sets the type of the transaction. Uses (FromIdx, Nonce).
func (tx *L2Tx) SetType() error {
if tx.ToIdx == Idx(1) {
tx.Type = TxTypeExit
} else if tx.ToIdx >= IdxUserThreshold {
tx.Type = TxTypeTransfer
} else { } else {
return l2Tx, tracerr.Wrap(fmt.Errorf("Can not determine type of L2Tx, invalid ToIdx value: %d", l2Tx.ToIdx)) return tracerr.Wrap(fmt.Errorf(
"cannot determine type of L2Tx, invalid ToIdx value: %d", tx.ToIdx))
} }
return nil
}
// if TxType!=l2Tx.TxType return error // SetID sets the ID of the transaction
if l2Tx.Type != "" && l2Tx.Type != txType { func (tx *L2Tx) SetID() error {
return l2Tx, tracerr.Wrap(fmt.Errorf("L2Tx.Type: %s, should be: %s", l2Tx.Type, txType)) tx.TxID[0] = TxIDPrefixL2Tx
} fromIdxBytes, err := tx.FromIdx.Bytes()
l2Tx.Type = txType
var txid [TxIDLen]byte
txid[0] = TxIDPrefixL2Tx
fromIdxBytes, err := l2Tx.FromIdx.Bytes()
if err != nil { if err != nil {
return l2Tx, tracerr.Wrap(err) return tracerr.Wrap(err)
} }
copy(txid[1:7], fromIdxBytes[:]) copy(tx.TxID[1:7], fromIdxBytes[:])
nonceBytes, err := l2Tx.Nonce.Bytes() nonceBytes, err := tx.Nonce.Bytes()
if err != nil { if err != nil {
return l2Tx, tracerr.Wrap(err) return tracerr.Wrap(err)
} }
copy(txid[7:12], nonceBytes[:]) copy(tx.TxID[7:12], nonceBytes[:])
l2Tx.TxID = TxID(txid) return nil
return l2Tx, nil
} }
// Tx returns a *Tx from the L2Tx // Tx returns a *Tx from the L2Tx

View File

@@ -50,49 +50,62 @@ type PoolL2Tx struct {
// NewPoolL2Tx returns the given L2Tx with the TxId & Type parameters calculated // NewPoolL2Tx returns the given L2Tx with the TxId & Type parameters calculated
// from the L2Tx values // from the L2Tx values
func NewPoolL2Tx(poolL2Tx *PoolL2Tx) (*PoolL2Tx, error) { func NewPoolL2Tx(tx *PoolL2Tx) (*PoolL2Tx, error) {
// calculate TxType txTypeOld := tx.Type
var txType TxType if err := tx.SetType(); err != nil {
if poolL2Tx.ToIdx >= IdxUserThreshold { return nil, err
txType = TxTypeTransfer }
} else if poolL2Tx.ToIdx == 1 { // If original Type doesn't match the correct one, return error
txType = TxTypeExit if txTypeOld != "" && txTypeOld != tx.Type {
} else if poolL2Tx.ToIdx == 0 { return nil, tracerr.Wrap(fmt.Errorf("L2Tx.Type: %s, should be: %s",
if poolL2Tx.ToBJJ != nil && poolL2Tx.ToEthAddr == FFAddr { tx.Type, txTypeOld))
txType = TxTypeTransferToBJJ }
} else if poolL2Tx.ToEthAddr != FFAddr && poolL2Tx.ToEthAddr != EmptyAddr {
txType = TxTypeTransferToEthAddr txIDOld := tx.TxID
if err := tx.SetID(); err != nil {
return nil, err
}
// If original TxID doesn't match the correct one, return error
if txIDOld != (TxID{}) && txIDOld != tx.TxID {
return tx, tracerr.Wrap(fmt.Errorf("PoolL2Tx.TxID: %s, should be: %s",
tx.TxID.String(), txIDOld.String()))
}
return tx, nil
}
// SetType sets the type of the transaction
func (tx *PoolL2Tx) SetType() error {
if tx.ToIdx >= IdxUserThreshold {
tx.Type = TxTypeTransfer
} else if tx.ToIdx == 1 {
tx.Type = TxTypeExit
} else if tx.ToIdx == 0 {
if tx.ToBJJ != nil && tx.ToEthAddr == FFAddr {
tx.Type = TxTypeTransferToBJJ
} else if tx.ToEthAddr != FFAddr && tx.ToEthAddr != EmptyAddr {
tx.Type = TxTypeTransferToEthAddr
} }
} else { } else {
return nil, tracerr.Wrap(errors.New("malformed transaction")) return tracerr.Wrap(errors.New("malformed transaction"))
} }
return nil
}
// if TxType!=poolL2Tx.TxType return error // SetID sets the ID of the transaction. Uses (FromIdx, Nonce).
if poolL2Tx.Type != "" && poolL2Tx.Type != txType { func (tx *PoolL2Tx) SetID() error {
return poolL2Tx, tracerr.Wrap(fmt.Errorf("type: %s, should be: %s", poolL2Tx.Type, txType)) tx.TxID[0] = TxIDPrefixL2Tx
} fromIdxBytes, err := tx.FromIdx.Bytes()
poolL2Tx.Type = txType
var txid [TxIDLen]byte
txid[0] = TxIDPrefixL2Tx
fromIdxBytes, err := poolL2Tx.FromIdx.Bytes()
if err != nil { if err != nil {
return poolL2Tx, tracerr.Wrap(err) return tracerr.Wrap(err)
} }
copy(txid[1:7], fromIdxBytes[:]) copy(tx.TxID[1:7], fromIdxBytes[:])
nonceBytes, err := poolL2Tx.Nonce.Bytes() nonceBytes, err := tx.Nonce.Bytes()
if err != nil { if err != nil {
return poolL2Tx, tracerr.Wrap(err) return tracerr.Wrap(err)
} }
copy(txid[7:12], nonceBytes[:]) copy(tx.TxID[7:12], nonceBytes[:])
txID := TxID(txid) return nil
// if TxID!=poolL2Tx.TxID return error
if poolL2Tx.TxID != (TxID{}) && poolL2Tx.TxID != txID {
return poolL2Tx, tracerr.Wrap(fmt.Errorf("id: %s, should be: %s", poolL2Tx.TxID.String(), txID.String()))
}
poolL2Tx.TxID = txID
return poolL2Tx, nil
} }
// TxCompressedData spec: // TxCompressedData spec:
@@ -305,11 +318,11 @@ func (tx PoolL2Tx) Tx() Tx {
// PoolL2TxsToL2Txs returns an array of []L2Tx from an array of []PoolL2Tx // PoolL2TxsToL2Txs returns an array of []L2Tx from an array of []PoolL2Tx
func PoolL2TxsToL2Txs(txs []PoolL2Tx) ([]L2Tx, error) { func PoolL2TxsToL2Txs(txs []PoolL2Tx) ([]L2Tx, error) {
var r []L2Tx l2Txs := make([]L2Tx, len(txs))
for _, poolTx := range txs { for i, poolTx := range txs {
r = append(r, poolTx.L2Tx()) l2Txs[i] = poolTx.L2Tx()
} }
return r, nil return l2Txs, nil
} }
// PoolL2TxState is a struct that represents the status of a L2 transaction // PoolL2TxState is a struct that represents the status of a L2 transaction

View File

@@ -144,9 +144,9 @@ func TestBatches(t *testing.T) {
CoordUser: "A", CoordUser: "A",
} }
blocks, err := tc.GenerateBlocks(set) blocks, err := tc.GenerateBlocks(set)
require.Nil(t, err) require.NoError(t, err)
err = tc.FillBlocksExtra(blocks, &tilCfgExtra) err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
assert.Nil(t, err) require.NoError(t, err)
// Insert to DB // Insert to DB
batches := []common.Batch{} batches := []common.Batch{}
tokensValue := make(map[common.TokenID]float64) tokensValue := make(map[common.TokenID]float64)
@@ -365,9 +365,9 @@ func TestTxs(t *testing.T) {
CoordUser: "A", CoordUser: "A",
} }
blocks, err := tc.GenerateBlocks(set) blocks, err := tc.GenerateBlocks(set)
require.Nil(t, err) require.NoError(t, err)
err = tc.FillBlocksExtra(blocks, &tilCfgExtra) err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
assert.Nil(t, err) require.NoError(t, err)
// Sanity check // Sanity check
require.Equal(t, 7, len(blocks)) require.Equal(t, 7, len(blocks))
@@ -617,7 +617,7 @@ func TestGetUnforgedL1UserTxs(t *testing.T) {
` `
tc := til.NewContext(128) tc := til.NewContext(128)
blocks, err := tc.GenerateBlocks(set) blocks, err := tc.GenerateBlocks(set)
require.Nil(t, err) require.NoError(t, err)
// Sanity check // Sanity check
require.Equal(t, 1, len(blocks)) require.Equal(t, 1, len(blocks))
require.Equal(t, 5, len(blocks[0].Rollup.L1UserTxs)) require.Equal(t, 5, len(blocks[0].Rollup.L1UserTxs))
@@ -626,17 +626,17 @@ func TestGetUnforgedL1UserTxs(t *testing.T) {
for i := range blocks { for i := range blocks {
err = historyDB.AddBlockSCData(&blocks[i]) err = historyDB.AddBlockSCData(&blocks[i])
require.Nil(t, err) require.NoError(t, err)
} }
l1UserTxs, err := historyDB.GetUnforgedL1UserTxs(toForgeL1TxsNum) l1UserTxs, err := historyDB.GetUnforgedL1UserTxs(toForgeL1TxsNum)
require.Nil(t, err) require.NoError(t, err)
assert.Equal(t, 5, len(l1UserTxs)) assert.Equal(t, 5, len(l1UserTxs))
assert.Equal(t, blocks[0].Rollup.L1UserTxs, l1UserTxs) assert.Equal(t, blocks[0].Rollup.L1UserTxs, l1UserTxs)
// No l1UserTxs for this toForgeL1TxsNum // No l1UserTxs for this toForgeL1TxsNum
l1UserTxs, err = historyDB.GetUnforgedL1UserTxs(2) l1UserTxs, err = historyDB.GetUnforgedL1UserTxs(2)
require.Nil(t, err) require.NoError(t, err)
assert.Equal(t, 0, len(l1UserTxs)) assert.Equal(t, 0, len(l1UserTxs))
} }
@@ -685,9 +685,9 @@ func TestSetInitialSCVars(t *testing.T) {
assert.Equal(t, sql.ErrNoRows, tracerr.Unwrap(err)) assert.Equal(t, sql.ErrNoRows, tracerr.Unwrap(err))
rollup, auction, wDelayer := exampleInitSCVars() rollup, auction, wDelayer := exampleInitSCVars()
err = historyDB.SetInitialSCVars(rollup, auction, wDelayer) err = historyDB.SetInitialSCVars(rollup, auction, wDelayer)
require.Nil(t, err) require.NoError(t, err)
dbRollup, dbAuction, dbWDelayer, err := historyDB.GetSCVars() dbRollup, dbAuction, dbWDelayer, err := historyDB.GetSCVars()
assert.Nil(t, err) require.NoError(t, err)
require.Equal(t, rollup, dbRollup) require.Equal(t, rollup, dbRollup)
require.Equal(t, auction, dbAuction) require.Equal(t, auction, dbAuction)
require.Equal(t, wDelayer, dbWDelayer) require.Equal(t, wDelayer, dbWDelayer)
@@ -718,16 +718,16 @@ func TestSetL1UserTxEffectiveAmounts(t *testing.T) {
CoordUser: "A", CoordUser: "A",
} }
blocks, err := tc.GenerateBlocks(set) blocks, err := tc.GenerateBlocks(set)
require.Nil(t, err) require.NoError(t, err)
err = tc.FillBlocksExtra(blocks, &tilCfgExtra) err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
assert.Nil(t, err) require.NoError(t, err)
err = tc.FillBlocksForgedL1UserTxs(blocks) err = tc.FillBlocksForgedL1UserTxs(blocks)
require.Nil(t, err) require.NoError(t, err)
// Add only first block so that the L1UserTxs are not marked as forged // Add only first block so that the L1UserTxs are not marked as forged
for i := range blocks[:1] { for i := range blocks[:1] {
err = historyDB.AddBlockSCData(&blocks[i]) err = historyDB.AddBlockSCData(&blocks[i])
require.Nil(t, err) require.NoError(t, err)
} }
// Add second batch to trigger the update of the batch_num, // Add second batch to trigger the update of the batch_num,
// while avoiding the implicit call of setL1UserTxEffectiveAmounts // while avoiding the implicit call of setL1UserTxEffectiveAmounts
@@ -735,7 +735,7 @@ func TestSetL1UserTxEffectiveAmounts(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
err = historyDB.addBatch(historyDB.db, &blocks[1].Rollup.Batches[0].Batch) err = historyDB.addBatch(historyDB.db, &blocks[1].Rollup.Batches[0].Batch)
assert.NoError(t, err) assert.NoError(t, err)
require.Nil(t, err) require.NoError(t, err)
// Set the Effective{Amount,DepositAmount} of the L1UserTxs that are forged in the second block // Set the Effective{Amount,DepositAmount} of the L1UserTxs that are forged in the second block
l1Txs := blocks[1].Rollup.Batches[0].L1UserTxs l1Txs := blocks[1].Rollup.Batches[0].L1UserTxs
@@ -805,14 +805,14 @@ func TestUpdateExitTree(t *testing.T) {
CoordUser: "A", CoordUser: "A",
} }
blocks, err := tc.GenerateBlocks(set) blocks, err := tc.GenerateBlocks(set)
require.Nil(t, err) require.NoError(t, err)
err = tc.FillBlocksExtra(blocks, &tilCfgExtra) err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
assert.Nil(t, err) require.NoError(t, err)
// Add all blocks except for the last two // Add all blocks except for the last two
for i := range blocks[:len(blocks)-2] { for i := range blocks[:len(blocks)-2] {
err = historyDB.AddBlockSCData(&blocks[i]) err = historyDB.AddBlockSCData(&blocks[i])
require.Nil(t, err) require.NoError(t, err)
} }
// Add withdraws to the second-to-last block, and insert block into the DB // Add withdraws to the second-to-last block, and insert block into the DB
@@ -832,15 +832,15 @@ func TestUpdateExitTree(t *testing.T) {
Owner: tc.UsersByIdx[259].Addr, Token: tokenAddr}, Owner: tc.UsersByIdx[259].Addr, Token: tokenAddr},
) )
err = historyDB.addBlock(historyDB.db, &block.Block) err = historyDB.addBlock(historyDB.db, &block.Block)
require.Nil(t, err) require.NoError(t, err)
err = historyDB.updateExitTree(historyDB.db, block.Block.Num, err = historyDB.updateExitTree(historyDB.db, block.Block.Num,
block.Rollup.Withdrawals, block.WDelayer.Withdrawals) block.Rollup.Withdrawals, block.WDelayer.Withdrawals)
require.Nil(t, err) require.NoError(t, err)
// Check that exits in DB match with the expected values // Check that exits in DB match with the expected values
dbExits, err := historyDB.GetAllExits() dbExits, err := historyDB.GetAllExits()
require.Nil(t, err) require.NoError(t, err)
assert.Equal(t, 4, len(dbExits)) assert.Equal(t, 4, len(dbExits))
dbExitsByIdx := make(map[common.Idx]common.ExitInfo) dbExitsByIdx := make(map[common.Idx]common.ExitInfo)
for _, dbExit := range dbExits { for _, dbExit := range dbExits {
@@ -865,15 +865,15 @@ func TestUpdateExitTree(t *testing.T) {
Amount: big.NewInt(80), Amount: big.NewInt(80),
}) })
err = historyDB.addBlock(historyDB.db, &block.Block) err = historyDB.addBlock(historyDB.db, &block.Block)
require.Nil(t, err) require.NoError(t, err)
err = historyDB.updateExitTree(historyDB.db, block.Block.Num, err = historyDB.updateExitTree(historyDB.db, block.Block.Num,
block.Rollup.Withdrawals, block.WDelayer.Withdrawals) block.Rollup.Withdrawals, block.WDelayer.Withdrawals)
require.Nil(t, err) require.NoError(t, err)
// Check that delayed withdrawn has been set // Check that delayed withdrawn has been set
dbExits, err = historyDB.GetAllExits() dbExits, err = historyDB.GetAllExits()
require.Nil(t, err) require.NoError(t, err)
for _, dbExit := range dbExits { for _, dbExit := range dbExits {
dbExitsByIdx[dbExit.AccountIdx] = dbExit dbExitsByIdx[dbExit.AccountIdx] = dbExit
} }
@@ -885,16 +885,16 @@ func TestGetBestBidCoordinator(t *testing.T) {
rollup, auction, wDelayer := exampleInitSCVars() rollup, auction, wDelayer := exampleInitSCVars()
err := historyDB.SetInitialSCVars(rollup, auction, wDelayer) err := historyDB.SetInitialSCVars(rollup, auction, wDelayer)
require.Nil(t, err) require.NoError(t, err)
tc := til.NewContext(common.RollupConstMaxL1UserTx) tc := til.NewContext(common.RollupConstMaxL1UserTx)
blocks, err := tc.GenerateBlocks(` blocks, err := tc.GenerateBlocks(`
Type: Blockchain Type: Blockchain
> block // blockNum=2 > block // blockNum=2
`) `)
require.Nil(t, err) require.NoError(t, err)
err = historyDB.AddBlockSCData(&blocks[0]) err = historyDB.AddBlockSCData(&blocks[0])
require.Nil(t, err) require.NoError(t, err)
coords := []common.Coordinator{ coords := []common.Coordinator{
{ {
@@ -911,7 +911,7 @@ func TestGetBestBidCoordinator(t *testing.T) {
}, },
} }
err = historyDB.addCoordinators(historyDB.db, coords) err = historyDB.addCoordinators(historyDB.db, coords)
require.Nil(t, err) require.NoError(t, err)
err = historyDB.addBids(historyDB.db, []common.Bid{ err = historyDB.addBids(historyDB.db, []common.Bid{
{ {
SlotNum: 10, SlotNum: 10,
@@ -926,10 +926,10 @@ func TestGetBestBidCoordinator(t *testing.T) {
Bidder: coords[1].Bidder, Bidder: coords[1].Bidder,
}, },
}) })
require.Nil(t, err) require.NoError(t, err)
forger10, err := historyDB.GetBestBidCoordinator(10) forger10, err := historyDB.GetBestBidCoordinator(10)
require.Nil(t, err) require.NoError(t, err)
require.Equal(t, coords[1].Forger, forger10.Forger) require.Equal(t, coords[1].Forger, forger10.Forger)
require.Equal(t, coords[1].Bidder, forger10.Bidder) require.Equal(t, coords[1].Bidder, forger10.Bidder)
require.Equal(t, coords[1].URL, forger10.URL) require.Equal(t, coords[1].URL, forger10.URL)

View File

@@ -753,13 +753,11 @@ func (s *Synchronizer) rollupSync(ethBlock *common.Block) (*common.RollupData, e
// L1CoordinatorTxs, PoolL2Txs) into stateDB so that they are // L1CoordinatorTxs, PoolL2Txs) into stateDB so that they are
// processed. // processed.
// Add TxID, TxType, Position, BlockNum and BatchNum to L2 txs // Set TxType to the forged L2Txs
for i := range forgeBatchArgs.L2TxsData { for i := range forgeBatchArgs.L2TxsData {
nTx, err := common.NewL2Tx(&forgeBatchArgs.L2TxsData[i]) if err := forgeBatchArgs.L2TxsData[i].SetType(); err != nil {
if err != nil {
return nil, tracerr.Wrap(err) return nil, tracerr.Wrap(err)
} }
forgeBatchArgs.L2TxsData[i] = *nTx
} }
// Transform L2 txs to PoolL2Txs // Transform L2 txs to PoolL2Txs
@@ -780,19 +778,22 @@ func (s *Synchronizer) rollupSync(ethBlock *common.Block) (*common.RollupData, e
} }
// Transform processed PoolL2 txs to L2 and store in BatchData // Transform processed PoolL2 txs to L2 and store in BatchData
if poolL2Txs != nil {
l2Txs, err := common.PoolL2TxsToL2Txs(poolL2Txs) // NOTE: This is a big uggly, find a better way l2Txs, err := common.PoolL2TxsToL2Txs(poolL2Txs) // NOTE: This is a big uggly, find a better way
if err != nil { if err != nil {
return nil, tracerr.Wrap(err) return nil, tracerr.Wrap(err)
} }
// Set TxID, BlockNum, BatchNum and Position to the forged L2Txs
for i := range l2Txs { for i := range l2Txs {
l2Txs[i].Position = position if err := l2Txs[i].SetID(); err != nil {
return nil, err
}
l2Txs[i].EthBlockNum = blockNum l2Txs[i].EthBlockNum = blockNum
l2Txs[i].BatchNum = batchNum l2Txs[i].BatchNum = batchNum
l2Txs[i].Position = position
position++ position++
} }
batchData.L2Txs = l2Txs batchData.L2Txs = l2Txs
}
// Set the BatchNum in the forged L1UserTxs // Set the BatchNum in the forged L1UserTxs
for i := range l1UserTxs { for i := range l1UserTxs {

View File

@@ -882,6 +882,9 @@ func (tc *Context) FillBlocksExtra(blocks []common.BlockData, cfg *ConfigExtra)
position++ position++
tc.extra.nonces[tx.FromIdx]++ tc.extra.nonces[tx.FromIdx]++
tx.Nonce = tc.extra.nonces[tx.FromIdx] tx.Nonce = tc.extra.nonces[tx.FromIdx]
if err := tx.SetID(); err != nil {
return err
}
nTx, err := common.NewL2Tx(tx) nTx, err := common.NewL2Tx(tx)
if err != nil { if err != nil {
return tracerr.Wrap(err) return tracerr.Wrap(err)