mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Extend ethclient test, implement new TxID spec
- Implement new TxID spec that distinguishes L1UserTx and L1CoordinatorTx - Replace some type []*Foo by []Foo - Fix HistoryDB & L2DB bug: in case of error, a rollback was applied and the returned error was nil - Reorder inserts in historydb.NewHistoryDB() to follow foreign key dependencies - Add initial synchronizer test with test.Client (for now, only tested l1UserTxs, blocks, addToken) - Update L1UserTx event in test.Client
This commit is contained in:
@@ -329,10 +329,11 @@ func NewClient(l bool, timer Timer, addr *ethCommon.Address, setup *ClientSetup)
|
||||
blockCurrent := Block{
|
||||
Rollup: &RollupBlock{
|
||||
State: eth.RollupState{
|
||||
StateRoot: big.NewInt(0),
|
||||
ExitRoots: make([]*big.Int, 0),
|
||||
ExitNullifierMap: make(map[[256 / 8]byte]bool),
|
||||
TokenList: make([]ethCommon.Address, 0),
|
||||
StateRoot: big.NewInt(0),
|
||||
ExitRoots: make([]*big.Int, 0),
|
||||
ExitNullifierMap: make(map[[256 / 8]byte]bool),
|
||||
// TokenID = 0 is ETH. Set first entry in TokenList with 0x0 address for ETH.
|
||||
TokenList: []ethCommon.Address{ethCommon.Address{}},
|
||||
TokenMap: make(map[ethCommon.Address]bool),
|
||||
MapL1TxQueue: mapL1TxQueue,
|
||||
LastL1L2Batch: 0,
|
||||
@@ -597,7 +598,11 @@ func (c *Client) CtlAddL1TxUser(l1Tx *common.L1Tx) {
|
||||
panic("l1Tx.TokenID + 1 > len(r.State.TokenList)")
|
||||
}
|
||||
queue.L1TxQueue = append(queue.L1TxQueue, *l1Tx)
|
||||
r.Events.L1UserTx = append(r.Events.L1UserTx, eth.RollupEventL1UserTx{L1Tx: *l1Tx})
|
||||
r.Events.L1UserTx = append(r.Events.L1UserTx, eth.RollupEventL1UserTx{
|
||||
L1Tx: *l1Tx,
|
||||
ToForgeL1TxsNum: r.State.LastToForgeL1TxsNum,
|
||||
Position: len(queue.L1TxQueue) - 1,
|
||||
})
|
||||
}
|
||||
|
||||
type transactionData struct {
|
||||
|
||||
@@ -152,6 +152,12 @@ func GenL1Txs(
|
||||
LoadAmountUSD: lUSD,
|
||||
EthBlockNum: blocks[i%len(blocks)].EthBlockNum,
|
||||
}
|
||||
if tx.UserOrigin {
|
||||
n := nextTxsNum
|
||||
tx.ToForgeL1TxsNum = &n
|
||||
} else {
|
||||
tx.BatchNum = &batches[i%len(batches)].BatchNum
|
||||
}
|
||||
nTx, err := common.NewL1Tx(&tx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -163,7 +169,8 @@ func GenL1Txs(
|
||||
setFromToAndAppend(fromIdx, tx, i, nUserTxs, userAddr, accounts, &userTxs, &othersTxs)
|
||||
} else {
|
||||
// Add unforged txs
|
||||
tx.ToForgeL1TxsNum = nextTxsNum
|
||||
n := nextTxsNum
|
||||
tx.ToForgeL1TxsNum = &n
|
||||
tx.UserOrigin = true
|
||||
setFromToAndAppend(fromIdx, tx, i, nUserTxs, userAddr, accounts, &userTxs, &othersTxs)
|
||||
}
|
||||
@@ -172,13 +179,13 @@ func GenL1Txs(
|
||||
}
|
||||
|
||||
// GetNextToForgeNumAndBatch returns the next BatchNum and ForgeL1TxsNum to be added
|
||||
func GetNextToForgeNumAndBatch(batches []common.Batch) (common.BatchNum, *int64) {
|
||||
func GetNextToForgeNumAndBatch(batches []common.Batch) (common.BatchNum, int64) {
|
||||
batchNum := batches[len(batches)-1].BatchNum + 1
|
||||
toForgeL1TxsNum := new(int64)
|
||||
var toForgeL1TxsNum int64
|
||||
found := false
|
||||
for i := len(batches) - 1; i >= 0; i-- {
|
||||
if batches[i].ForgeL1TxsNum != nil {
|
||||
*toForgeL1TxsNum = *batches[i].ForgeL1TxsNum + 1
|
||||
toForgeL1TxsNum = *batches[i].ForgeL1TxsNum + 1
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
31
test/txs.go
31
test/txs.go
@@ -52,16 +52,16 @@ func GenerateKeys(t *testing.T, accNames []string) map[string]*Account {
|
||||
|
||||
// GenerateTestTxs generates L1Tx & PoolL2Tx in a deterministic way for the
|
||||
// given Instructions.
|
||||
func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]*common.L1Tx, [][]*common.L1Tx, [][]*common.PoolL2Tx, []common.Token) {
|
||||
func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]common.L1Tx, [][]common.L1Tx, [][]common.PoolL2Tx, []common.Token) {
|
||||
accounts := GenerateKeys(t, instructions.Accounts)
|
||||
l1CreatedAccounts := make(map[string]*Account)
|
||||
|
||||
var batchL1Txs []*common.L1Tx
|
||||
var batchCoordinatorL1Txs []*common.L1Tx
|
||||
var batchPoolL2Txs []*common.PoolL2Tx
|
||||
var l1Txs [][]*common.L1Tx
|
||||
var coordinatorL1Txs [][]*common.L1Tx
|
||||
var poolL2Txs [][]*common.PoolL2Tx
|
||||
var batchL1Txs []common.L1Tx
|
||||
var batchCoordinatorL1Txs []common.L1Tx
|
||||
var batchPoolL2Txs []common.PoolL2Tx
|
||||
var l1Txs [][]common.L1Tx
|
||||
var coordinatorL1Txs [][]common.L1Tx
|
||||
var poolL2Txs [][]common.PoolL2Tx
|
||||
idx := 256
|
||||
for _, inst := range instructions.Instructions {
|
||||
switch inst.Type {
|
||||
@@ -71,10 +71,11 @@ func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]*common.L1Tx,
|
||||
FromEthAddr: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Addr,
|
||||
FromBJJ: accounts[idxTokenIDToString(inst.From, inst.TokenID)].BJJ.Public(),
|
||||
TokenID: inst.TokenID,
|
||||
Amount: big.NewInt(0),
|
||||
LoadAmount: big.NewInt(int64(inst.Amount)),
|
||||
Type: common.TxTypeCreateAccountDeposit,
|
||||
}
|
||||
batchL1Txs = append(batchL1Txs, &tx)
|
||||
batchL1Txs = append(batchL1Txs, tx)
|
||||
if accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx == common.Idx(0) { // if account.Idx is not set yet, set it and increment idx
|
||||
accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx = common.Idx(idx)
|
||||
|
||||
@@ -93,7 +94,7 @@ func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]*common.L1Tx,
|
||||
}
|
||||
accounts[idxTokenIDToString(inst.To, inst.TokenID)].Idx = common.Idx(idx)
|
||||
l1CreatedAccounts[idxTokenIDToString(inst.To, inst.TokenID)] = accounts[idxTokenIDToString(inst.To, inst.TokenID)]
|
||||
batchCoordinatorL1Txs = append(batchCoordinatorL1Txs, &tx)
|
||||
batchCoordinatorL1Txs = append(batchCoordinatorL1Txs, tx)
|
||||
idx++
|
||||
}
|
||||
toIdx := new(common.Idx)
|
||||
@@ -132,7 +133,7 @@ func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]*common.L1Tx,
|
||||
tx.Signature = sig
|
||||
|
||||
accounts[idxTokenIDToString(inst.From, inst.TokenID)].Nonce++
|
||||
batchPoolL2Txs = append(batchPoolL2Txs, &tx)
|
||||
batchPoolL2Txs = append(batchPoolL2Txs, tx)
|
||||
|
||||
case common.TxTypeExit, common.TxTypeForceExit:
|
||||
fromIdx := new(common.Idx)
|
||||
@@ -144,14 +145,14 @@ func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]*common.L1Tx,
|
||||
Amount: big.NewInt(int64(inst.Amount)),
|
||||
Type: common.TxTypeExit,
|
||||
}
|
||||
batchL1Txs = append(batchL1Txs, &tx)
|
||||
batchL1Txs = append(batchL1Txs, tx)
|
||||
case TypeNewBatch:
|
||||
l1Txs = append(l1Txs, batchL1Txs)
|
||||
coordinatorL1Txs = append(coordinatorL1Txs, batchCoordinatorL1Txs)
|
||||
poolL2Txs = append(poolL2Txs, batchPoolL2Txs)
|
||||
batchL1Txs = []*common.L1Tx{}
|
||||
batchCoordinatorL1Txs = []*common.L1Tx{}
|
||||
batchPoolL2Txs = []*common.PoolL2Tx{}
|
||||
batchL1Txs = []common.L1Tx{}
|
||||
batchCoordinatorL1Txs = []common.L1Tx{}
|
||||
batchPoolL2Txs = []common.PoolL2Tx{}
|
||||
default:
|
||||
continue
|
||||
}
|
||||
@@ -184,7 +185,7 @@ func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]*common.L1Tx,
|
||||
|
||||
// GenerateTestTxsFromSet reurns the L1 & L2 transactions for a given Set of
|
||||
// Instructions code
|
||||
func GenerateTestTxsFromSet(t *testing.T, set string) ([][]*common.L1Tx, [][]*common.L1Tx, [][]*common.PoolL2Tx, []common.Token) {
|
||||
func GenerateTestTxsFromSet(t *testing.T, set string) ([][]common.L1Tx, [][]common.L1Tx, [][]common.PoolL2Tx, []common.Token) {
|
||||
parser := NewParser(strings.NewReader(set))
|
||||
instructions, err := parser.Parse()
|
||||
require.Nil(t, err)
|
||||
|
||||
Reference in New Issue
Block a user