mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Feature/null refactor (#173)
* WIP: rebase * Make nullable fields use pointers
This commit is contained in:
@@ -566,7 +566,7 @@ func (c *Client) CtlAddL1TxUser(l1Tx *common.L1Tx) {
|
||||
r.State.MapL1TxQueue[r.State.LastToForgeL1TxsNum] = eth.NewQueueStruct()
|
||||
queue = r.State.MapL1TxQueue[r.State.LastToForgeL1TxsNum]
|
||||
}
|
||||
if int64(l1Tx.FromIdx) > r.State.CurrentIdx {
|
||||
if l1Tx.FromIdx != nil && int64(*l1Tx.FromIdx) > r.State.CurrentIdx {
|
||||
panic("l1Tx.FromIdx > r.State.CurrentIdx")
|
||||
}
|
||||
if int(l1Tx.TokenID)+1 > len(r.State.TokenList) {
|
||||
|
||||
@@ -140,7 +140,7 @@ func TestClientRollup(t *testing.T) {
|
||||
for i := 0; i < N; i++ {
|
||||
keys[i] = genKeys(int64(i))
|
||||
l1UserTx := common.L1Tx{
|
||||
FromIdx: common.Idx(0),
|
||||
FromIdx: nil,
|
||||
FromEthAddr: keys[i].Addr,
|
||||
FromBJJ: keys[i].BJJPublicKey,
|
||||
TokenID: common.TokenID(0),
|
||||
|
||||
@@ -74,7 +74,9 @@ func GenBatches(nBatches int, blocks []common.Block) []common.Batch {
|
||||
SlotNum: common.SlotNum(i),
|
||||
}
|
||||
if i%2 == 0 {
|
||||
batch.ForgeL1TxsNum = int64(i)
|
||||
toForge := new(int64)
|
||||
*toForge = int64(i)
|
||||
batch.ForgeL1TxsNum = toForge
|
||||
}
|
||||
batches = append(batches, batch)
|
||||
}
|
||||
@@ -155,7 +157,7 @@ func GenL1Txs(
|
||||
panic(err)
|
||||
}
|
||||
tx = *nTx
|
||||
if batches[i%len(batches)].ForgeL1TxsNum != 0 {
|
||||
if batches[i%len(batches)].ForgeL1TxsNum != nil {
|
||||
// Add already forged txs
|
||||
tx.BatchNum = &batches[i%len(batches)].BatchNum
|
||||
setFromToAndAppend(fromIdx, tx, i, nUserTxs, userAddr, accounts, &userTxs, &othersTxs)
|
||||
@@ -170,13 +172,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
|
||||
var toForgeL1TxsNum int64
|
||||
toForgeL1TxsNum := new(int64)
|
||||
found := false
|
||||
for i := len(batches) - 1; i >= 0; i-- {
|
||||
if batches[i].ForgeL1TxsNum != 0 {
|
||||
toForgeL1TxsNum = batches[i].ForgeL1TxsNum + 1
|
||||
if batches[i].ForgeL1TxsNum != nil {
|
||||
*toForgeL1TxsNum = *batches[i].ForgeL1TxsNum + 1
|
||||
found = true
|
||||
break
|
||||
}
|
||||
@@ -218,7 +220,9 @@ func setFromToAndAppend(
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
tx.FromIdx = from.Idx
|
||||
fromIdx := new(common.Idx)
|
||||
*fromIdx = from.Idx
|
||||
tx.FromIdx = fromIdx
|
||||
tx.FromEthAddr = from.EthAddr
|
||||
tx.FromBJJ = from.PublicKey
|
||||
tx.ToIdx = to.Idx
|
||||
@@ -232,7 +236,9 @@ func setFromToAndAppend(
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
tx.FromIdx = from.Idx
|
||||
fromIdx := new(common.Idx)
|
||||
*fromIdx = from.Idx
|
||||
tx.FromIdx = fromIdx
|
||||
tx.FromEthAddr = from.EthAddr
|
||||
tx.FromBJJ = from.PublicKey
|
||||
tx.ToIdx = to.Idx
|
||||
@@ -435,9 +441,6 @@ func randomTxType(seed int) common.TxType {
|
||||
case 0:
|
||||
return common.TxTypeExit
|
||||
//nolint:gomnd
|
||||
case 1:
|
||||
return common.TxTypeWithdrawn
|
||||
//nolint:gomnd
|
||||
case 2:
|
||||
return common.TxTypeTransfer
|
||||
//nolint:gomnd
|
||||
|
||||
37
test/l2db.go
37
test/l2db.go
@@ -53,10 +53,14 @@ func GenPoolTxs(n int, tokens []common.Token) []*common.PoolL2Tx {
|
||||
*usd = *token.USD * amountF
|
||||
*absFee = fee.Percentage() * *usd
|
||||
}
|
||||
toIdx := new(common.Idx)
|
||||
*toIdx = common.Idx(i + 1)
|
||||
toEthAddr := new(ethCommon.Address)
|
||||
*toEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
|
||||
tx := &common.PoolL2Tx{
|
||||
FromIdx: common.Idx(i),
|
||||
ToIdx: common.Idx(i + 1),
|
||||
ToEthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
|
||||
ToIdx: toIdx,
|
||||
ToEthAddr: toEthAddr,
|
||||
ToBJJ: privK.Public(),
|
||||
TokenID: token.TokenID,
|
||||
Amount: big.NewInt(int64(i)),
|
||||
@@ -67,7 +71,6 @@ func GenPoolTxs(n int, tokens []common.Token) []*common.PoolL2Tx {
|
||||
State: state,
|
||||
Signature: privK.SignPoseidon(big.NewInt(int64(i))),
|
||||
Timestamp: time.Now().UTC(),
|
||||
TokenSymbol: token.Symbol,
|
||||
AbsoluteFee: absFee,
|
||||
AbsoluteFeeUpdate: token.USDUpdate,
|
||||
}
|
||||
@@ -77,17 +80,31 @@ func GenPoolTxs(n int, tokens []common.Token) []*common.PoolL2Tx {
|
||||
panic(err)
|
||||
}
|
||||
if i%2 == 0 { // Optional parameters: rq
|
||||
tx.RqFromIdx = common.Idx(i)
|
||||
tx.RqToIdx = common.Idx(i + 1)
|
||||
tx.RqToEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
|
||||
rqFromIdx := new(common.Idx)
|
||||
*rqFromIdx = common.Idx(i)
|
||||
tx.RqFromIdx = rqFromIdx
|
||||
rqToIdx := new(common.Idx)
|
||||
*rqToIdx = common.Idx(i + 1)
|
||||
tx.RqToIdx = rqToIdx
|
||||
rqToEthAddr := new(ethCommon.Address)
|
||||
*rqToEthAddr = ethCommon.BigToAddress(big.NewInt(int64(i)))
|
||||
tx.RqToEthAddr = rqToEthAddr
|
||||
tx.RqToBJJ = privK.Public()
|
||||
tx.RqTokenID = common.TokenID(i)
|
||||
rqTokenID := new(common.TokenID)
|
||||
*rqTokenID = common.TokenID(i)
|
||||
tx.RqTokenID = rqTokenID
|
||||
tx.RqAmount = big.NewInt(int64(i))
|
||||
tx.RqFee = common.FeeSelector(i)
|
||||
tx.RqNonce = uint64(i)
|
||||
rqFee := new(common.FeeSelector)
|
||||
*rqFee = common.FeeSelector(i)
|
||||
tx.RqFee = rqFee
|
||||
rqNonce := new(uint64)
|
||||
*rqNonce = uint64(i)
|
||||
tx.RqNonce = rqNonce
|
||||
}
|
||||
if i%3 == 0 { // Optional parameters: things that get updated "a posteriori"
|
||||
tx.BatchNum = 489
|
||||
batchNum := new(common.BatchNum)
|
||||
*batchNum = 489
|
||||
tx.BatchNum = batchNum
|
||||
}
|
||||
txs = append(txs, tx)
|
||||
}
|
||||
|
||||
19
test/txs.go
19
test/txs.go
@@ -96,11 +96,16 @@ func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]*common.L1Tx,
|
||||
batchCoordinatorL1Txs = append(batchCoordinatorL1Txs, &tx)
|
||||
idx++
|
||||
}
|
||||
|
||||
toIdx := new(common.Idx)
|
||||
*toIdx = accounts[idxTokenIDToString(inst.To, inst.TokenID)].Idx
|
||||
toEthAddr := new(ethCommon.Address)
|
||||
*toEthAddr = accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr
|
||||
rqToEthAddr := new(ethCommon.Address)
|
||||
*rqToEthAddr = accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr
|
||||
tx := common.PoolL2Tx{
|
||||
FromIdx: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx,
|
||||
ToIdx: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Idx,
|
||||
ToEthAddr: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr,
|
||||
ToIdx: toIdx,
|
||||
ToEthAddr: toEthAddr,
|
||||
ToBJJ: accounts[idxTokenIDToString(inst.To, inst.TokenID)].BJJ.Public(),
|
||||
TokenID: inst.TokenID,
|
||||
Amount: big.NewInt(int64(inst.Amount)),
|
||||
@@ -108,8 +113,8 @@ func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]*common.L1Tx,
|
||||
Nonce: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Nonce,
|
||||
State: common.PoolL2TxStatePending,
|
||||
Timestamp: time.Now(),
|
||||
BatchNum: common.BatchNum(0),
|
||||
RqToEthAddr: accounts[idxTokenIDToString(inst.To, inst.TokenID)].Addr,
|
||||
BatchNum: nil,
|
||||
RqToEthAddr: rqToEthAddr,
|
||||
RqToBJJ: accounts[idxTokenIDToString(inst.To, inst.TokenID)].BJJ.Public(),
|
||||
Type: common.TxTypeTransfer,
|
||||
}
|
||||
@@ -130,8 +135,10 @@ func GenerateTestTxs(t *testing.T, instructions Instructions) ([][]*common.L1Tx,
|
||||
batchPoolL2Txs = append(batchPoolL2Txs, &tx)
|
||||
|
||||
case common.TxTypeExit, common.TxTypeForceExit:
|
||||
fromIdx := new(common.Idx)
|
||||
*fromIdx = accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx
|
||||
tx := common.L1Tx{
|
||||
FromIdx: accounts[idxTokenIDToString(inst.From, inst.TokenID)].Idx,
|
||||
FromIdx: fromIdx,
|
||||
ToIdx: common.Idx(1), // as is an Exit
|
||||
TokenID: inst.TokenID,
|
||||
Amount: big.NewInt(int64(inst.Amount)),
|
||||
|
||||
@@ -50,7 +50,7 @@ func TestGenerateTestL2Txs(t *testing.T) {
|
||||
// l2txs
|
||||
assert.Equal(t, common.TxTypeTransfer, l2txs[0][0].Type)
|
||||
assert.Equal(t, common.Idx(256), l2txs[0][0].FromIdx)
|
||||
assert.Equal(t, common.Idx(258), l2txs[0][0].ToIdx)
|
||||
assert.Equal(t, common.Idx(258), *l2txs[0][0].ToIdx)
|
||||
assert.Equal(t, accounts["B1"].BJJ.Public().String(), l2txs[0][0].ToBJJ.String())
|
||||
assert.Equal(t, accounts["B1"].Addr.Hex(), l2txs[0][0].ToEthAddr.Hex())
|
||||
assert.Equal(t, common.Nonce(0), l2txs[0][0].Nonce)
|
||||
|
||||
Reference in New Issue
Block a user