Browse Source

In til, process L1UserTxs when defined, not in batch

feature/sql-semaphore1
Eduard S 4 years ago
parent
commit
4dbfedee49
3 changed files with 90 additions and 60 deletions
  1. +2
    -5
      db/historydb/historydb_test.go
  2. +46
    -38
      test/til/txs.go
  3. +42
    -17
      test/til/txs_test.go

+ 2
- 5
db/historydb/historydb_test.go

@ -390,14 +390,12 @@ func TestGetL1UserTxs(t *testing.T) {
AddToken(2)
AddToken(3)
CreateAccountDeposit(1) A: 10
CreateAccountDeposit(1) A: 20
CreateAccountDeposit(2) A: 20
CreateAccountDeposit(1) B: 5
CreateAccountDeposit(1) C: 5
CreateAccountDepositTransfer(1) D-A: 15, 10 (3)
CreateAccountDeposit(1) D: 5
> batchL1 // freeze open l1UserTxs queue
> batchL1 // forge current l1UserTxs queue
> block
`
tc := til.NewContext(128)
@ -406,7 +404,6 @@ func TestGetL1UserTxs(t *testing.T) {
// Sanity check
require.Equal(t, 1, len(blocks))
require.Equal(t, 5, len(blocks[0].L1UserTxs))
require.Equal(t, 2, len(blocks[0].Batches))
// fmt.Printf("DBG Blocks: %+v\n", blocks)
// TODO: Move this logic to `func (tc *TestContext) GenerateBlocks(set string) ([]common.BlockData, error)`

+ 46
- 38
test/til/txs.go

@ -114,7 +114,7 @@ func (tc *Context) GenerateBlocks(set string) ([]common.BlockData, error) {
var blocks []common.BlockData
for _, inst := range parsedSet.instructions {
switch inst.typ {
case txTypeCreateAccountDepositCoordinator:
case txTypeCreateAccountDepositCoordinator: // tx source: L1CoordinatorTx
if err := tc.checkIfTokenIsRegistered(inst); err != nil {
log.Error(err)
return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
@ -133,7 +133,7 @@ func (tc *Context) GenerateBlocks(set string) ([]common.BlockData, error) {
L1Tx: tx,
}
tc.currBatchTest.l1CoordinatorTxs = append(tc.currBatchTest.l1CoordinatorTxs, testTx)
case common.TxTypeCreateAccountDeposit, common.TxTypeCreateAccountDepositTransfer:
case common.TxTypeCreateAccountDeposit, common.TxTypeCreateAccountDepositTransfer: // tx source: L1UserTx
if err := tc.checkIfTokenIsRegistered(inst); err != nil {
log.Error(err)
return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
@ -155,8 +155,10 @@ func (tc *Context) GenerateBlocks(set string) ([]common.BlockData, error) {
toIdxName: inst.to,
L1Tx: tx,
}
tc.addToL1Queue(testTx)
case common.TxTypeDeposit, common.TxTypeDepositTransfer:
if err := tc.addToL1Queue(testTx); err != nil {
return nil, err
}
case common.TxTypeDeposit, common.TxTypeDepositTransfer: // tx source: L1UserTx
if err := tc.checkIfTokenIsRegistered(inst); err != nil {
log.Error(err)
return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
@ -180,8 +182,10 @@ func (tc *Context) GenerateBlocks(set string) ([]common.BlockData, error) {
toIdxName: inst.to,
L1Tx: tx,
}
tc.addToL1Queue(testTx)
case common.TxTypeTransfer:
if err := tc.addToL1Queue(testTx); err != nil {
return nil, err
}
case common.TxTypeTransfer: // L2Tx
if err := tc.checkIfTokenIsRegistered(inst); err != nil {
log.Error(err)
return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
@ -200,7 +204,7 @@ func (tc *Context) GenerateBlocks(set string) ([]common.BlockData, error) {
L2Tx: tx,
}
tc.currBatchTest.l2Txs = append(tc.currBatchTest.l2Txs, testTx)
case common.TxTypeExit:
case common.TxTypeExit: // tx source: L2Tx
if err := tc.checkIfTokenIsRegistered(inst); err != nil {
log.Error(err)
return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
@ -219,7 +223,7 @@ func (tc *Context) GenerateBlocks(set string) ([]common.BlockData, error) {
L2Tx: tx,
}
tc.currBatchTest.l2Txs = append(tc.currBatchTest.l2Txs, testTx)
case common.TxTypeForceExit:
case common.TxTypeForceExit: // tx source: L1UserTx
if err := tc.checkIfTokenIsRegistered(inst); err != nil {
log.Error(err)
return nil, fmt.Errorf("Line %d: %s", inst.lineNum, err.Error())
@ -237,7 +241,9 @@ func (tc *Context) GenerateBlocks(set string) ([]common.BlockData, error) {
toIdxName: inst.to,
L1Tx: tx,
}
tc.addToL1Queue(testTx)
if err := tc.addToL1Queue(testTx); err != nil {
return nil, err
}
case typeNewBatch:
if err = tc.calculateIdxForL1Txs(true, tc.currBatchTest.l1CoordinatorTxs); err != nil {
return nil, err
@ -254,34 +260,6 @@ func (tc *Context) GenerateBlocks(set string) ([]common.BlockData, error) {
if err = tc.calculateIdxForL1Txs(true, tc.currBatchTest.l1CoordinatorTxs); err != nil {
return nil, err
}
// once Idxs are calculated, update transactions to use the real Idxs
for i := 0; i < len(tc.queues[tc.toForgeNum]); i++ {
testTx := &tc.queues[tc.toForgeNum][i]
if testTx.L1Tx.Type != common.TxTypeCreateAccountDeposit && testTx.L1Tx.Type != common.TxTypeCreateAccountDepositTransfer {
testTx.L1Tx.FromIdx = tc.Users[testTx.fromIdxName].Accounts[testTx.L1Tx.TokenID].Idx
}
testTx.L1Tx.FromEthAddr = tc.Users[testTx.fromIdxName].Addr
testTx.L1Tx.FromBJJ = tc.Users[testTx.fromIdxName].BJJ.Public()
if testTx.toIdxName == "" {
testTx.L1Tx.ToIdx = common.Idx(0)
} else {
testTx.L1Tx.ToIdx = tc.Users[testTx.toIdxName].Accounts[testTx.L1Tx.TokenID].Idx
}
if testTx.L1Tx.Type == common.TxTypeExit {
testTx.L1Tx.ToIdx = common.Idx(1)
}
// bn := common.BatchNum(tc.currBatchNum)
// testTx.L1Tx.BatchNum = &bn
nTx, err := common.NewL1Tx(&testTx.L1Tx)
if err != nil {
fmt.Println(testTx)
return nil, fmt.Errorf("Line %d: %s", testTx.lineNum, err.Error())
}
testTx.L1Tx = *nTx
tc.currBlock.L1UserTxs = append(tc.currBlock.L1UserTxs, testTx.L1Tx)
}
if err = tc.setIdxs(); err != nil {
log.Error(err)
return nil, err
@ -382,7 +360,7 @@ func (tc *Context) setIdxs() error {
}
// addToL1Queue adds the L1Tx into the queue that is open and has space
func (tc *Context) addToL1Queue(tx L1Tx) {
func (tc *Context) addToL1Queue(tx L1Tx) error {
if len(tc.queues[tc.openToForge]) >= tc.rollupConstMaxL1UserTx {
// if current OpenToForge queue reached its Max, move into a
// new queue
@ -390,10 +368,40 @@ func (tc *Context) addToL1Queue(tx L1Tx) {
newQueue := []L1Tx{}
tc.queues = append(tc.queues, newQueue)
}
// Fill L1UserTx specific parameters
tx.L1Tx.UserOrigin = true
toForgeL1TxsNum := int64(tc.openToForge)
tx.L1Tx.ToForgeL1TxsNum = &toForgeL1TxsNum
// When an L1UserTx is generated, all idxs must be available (except when idx == 0 or idx == 1)
if tx.L1Tx.Type != common.TxTypeCreateAccountDeposit && tx.L1Tx.Type != common.TxTypeCreateAccountDepositTransfer {
tx.L1Tx.FromIdx = tc.Users[tx.fromIdxName].Accounts[tx.L1Tx.TokenID].Idx
}
tx.L1Tx.FromEthAddr = tc.Users[tx.fromIdxName].Addr
tx.L1Tx.FromBJJ = tc.Users[tx.fromIdxName].BJJ.Public()
if tx.toIdxName == "" {
tx.L1Tx.ToIdx = common.Idx(0)
} else {
account, ok := tc.Users[tx.toIdxName].Accounts[tx.L1Tx.TokenID]
if !ok {
return fmt.Errorf("Line %d: Transfer to User: %s, for TokenID: %d, "+
"while account not created yet", tx.lineNum, tx.toIdxName, tx.L1Tx.TokenID)
}
tx.L1Tx.ToIdx = account.Idx
}
if tx.L1Tx.Type == common.TxTypeExit {
tx.L1Tx.ToIdx = common.Idx(1)
}
nTx, err := common.NewL1Tx(&tx.L1Tx)
if err != nil {
return fmt.Errorf("Line %d: %s", tx.lineNum, err.Error())
}
tx.L1Tx = *nTx
tc.queues[tc.openToForge] = append(tc.queues[tc.openToForge], tx)
tc.currBlock.L1UserTxs = append(tc.currBlock.L1UserTxs, tx.L1Tx)
return nil
}
func (tc *Context) checkIfAccountExists(tf string, inst instruction) error {

+ 42
- 17
test/til/txs_test.go

@ -10,6 +10,26 @@ import (
"github.com/stretchr/testify/require"
)
func TestGenerateBlocksNoBatches(t *testing.T) {
set := `
Type: Blockchain
AddToken(1)
AddToken(2)
CreateAccountDeposit(1) A: 11
CreateAccountDeposit(2) B: 22
> block
`
tc := NewContext(eth.RollupConstMaxL1UserTx)
blocks, err := tc.GenerateBlocks(set)
require.Nil(t, err)
assert.Equal(t, 1, len(blocks))
assert.Equal(t, 0, len(blocks[0].Batches))
assert.Equal(t, 2, len(blocks[0].AddedTokens))
assert.Equal(t, 2, len(blocks[0].L1UserTxs))
}
func TestGenerateBlocks(t *testing.T) {
set := `
Type: Blockchain
@ -21,11 +41,13 @@ func TestGenerateBlocks(t *testing.T) {
CreateAccountDeposit(2) A: 20
CreateAccountDeposit(1) B: 5
CreateAccountDeposit(1) C: 5
CreateAccountDepositTransfer(1) D-A: 15, 10 (3)
CreateAccountDeposit(1) D: 5
> batchL1 // batchNum = 1
> batchL1 // batchNum = 2
CreateAccountDepositTransfer(1) F-A: 15, 10 (3)
Transfer(1) A-B: 6 (1)
Transfer(1) B-D: 3 (1)
Transfer(1) A-D: 1 (1)
@ -72,7 +94,7 @@ func TestGenerateBlocks(t *testing.T) {
assert.Equal(t, 2, len(blocks))
assert.Equal(t, 5, len(blocks[0].Batches))
assert.Equal(t, 1, len(blocks[1].Batches))
assert.Equal(t, 8, len(blocks[0].L1UserTxs))
assert.Equal(t, 9, len(blocks[0].L1UserTxs))
assert.Equal(t, 4, len(blocks[0].Batches[3].L1CoordinatorTxs))
assert.Equal(t, 0, len(blocks[1].L1UserTxs))
@ -85,8 +107,8 @@ func TestGenerateBlocks(t *testing.T) {
tc.checkL1TxParams(t, blocks[0].L1UserTxs[2], common.TxTypeCreateAccountDeposit, 1, "B", "", big.NewInt(5), nil)
// // #3: CreateAccountDeposit(1) C: 5
tc.checkL1TxParams(t, blocks[0].L1UserTxs[3], common.TxTypeCreateAccountDeposit, 1, "C", "", big.NewInt(5), nil)
// // #4: CreateAccountDepositTransfer(1) D-A: 15, 10 (3)
tc.checkL1TxParams(t, blocks[0].L1UserTxs[4], common.TxTypeCreateAccountDepositTransfer, 1, "D", "A", big.NewInt(15), big.NewInt(10))
// // #4: CreateAccountDeposit(1) D: 5
tc.checkL1TxParams(t, blocks[0].L1UserTxs[4], common.TxTypeCreateAccountDeposit, 1, "D", "", big.NewInt(5), nil)
// #5: Transfer(1) A-B: 6 (1)
tc.checkL2TxParams(t, blocks[0].Batches[2].L2Txs[0], common.TxTypeTransfer, 1, "A", "B", big.NewInt(6), common.BatchNum(3), common.Nonce(1))
// #6: Transfer(1) B-D: 3 (1)
@ -94,27 +116,29 @@ func TestGenerateBlocks(t *testing.T) {
// #7: Transfer(1) A-D: 1 (1)
tc.checkL2TxParams(t, blocks[0].Batches[2].L2Txs[2], common.TxTypeTransfer, 1, "A", "D", big.NewInt(1), common.BatchNum(3), common.Nonce(2))
// change of Batch
// #8: DepositTransfer(1) A-B: 15, 10 (1)
tc.checkL1TxParams(t, blocks[0].L1UserTxs[5], common.TxTypeDepositTransfer, 1, "A", "B", big.NewInt(15), big.NewInt(10))
// #10: Transfer(1) C-A : 3 (1)
// #8: CreateAccountDepositTransfer(1) F-A: 15, 10 (3)
tc.checkL1TxParams(t, blocks[0].L1UserTxs[5], common.TxTypeCreateAccountDepositTransfer, 1, "F", "A", big.NewInt(15), big.NewInt(10))
// #9: DepositTransfer(1) A-B: 15, 10 (1)
tc.checkL1TxParams(t, blocks[0].L1UserTxs[6], common.TxTypeDepositTransfer, 1, "A", "B", big.NewInt(15), big.NewInt(10))
// #11: Transfer(1) C-A : 3 (1)
tc.checkL2TxParams(t, blocks[0].Batches[3].L2Txs[0], common.TxTypeTransfer, 1, "C", "A", big.NewInt(3), common.BatchNum(4), common.Nonce(1))
// #11: Transfer(2) A-B: 15 (1)
// #12: Transfer(2) A-B: 15 (1)
tc.checkL2TxParams(t, blocks[0].Batches[3].L2Txs[1], common.TxTypeTransfer, 2, "A", "B", big.NewInt(15), common.BatchNum(4), common.Nonce(1))
// #12: Deposit(1) User0: 20
tc.checkL1TxParams(t, blocks[0].L1UserTxs[6], common.TxTypeCreateAccountDeposit, 1, "User0", "", big.NewInt(20), nil)
// // #13: Deposit(3) User1: 20
tc.checkL1TxParams(t, blocks[0].L1UserTxs[7], common.TxTypeCreateAccountDeposit, 3, "User1", "", big.NewInt(20), nil)
// #14: Transfer(1) User0-User1: 15 (1)
// #13: Deposit(1) User0: 20
tc.checkL1TxParams(t, blocks[0].L1UserTxs[7], common.TxTypeCreateAccountDeposit, 1, "User0", "", big.NewInt(20), nil)
// // #14: Deposit(3) User1: 20
tc.checkL1TxParams(t, blocks[0].L1UserTxs[8], common.TxTypeCreateAccountDeposit, 3, "User1", "", big.NewInt(20), nil)
// #15: Transfer(1) User0-User1: 15 (1)
tc.checkL2TxParams(t, blocks[0].Batches[4].L2Txs[0], common.TxTypeTransfer, 1, "User0", "User1", big.NewInt(15), common.BatchNum(5), common.Nonce(1))
// #15: Transfer(3) User1-User0: 15 (1)
// #16: Transfer(3) User1-User0: 15 (1)
tc.checkL2TxParams(t, blocks[0].Batches[4].L2Txs[1], common.TxTypeTransfer, 3, "User1", "User0", big.NewInt(15), common.BatchNum(5), common.Nonce(1))
// #16: Transfer(1) A-C: 1 (1)
// #17: Transfer(1) A-C: 1 (1)
tc.checkL2TxParams(t, blocks[0].Batches[4].L2Txs[2], common.TxTypeTransfer, 1, "A", "C", big.NewInt(1), common.BatchNum(5), common.Nonce(4))
// change of Batch
// #17: Transfer(1) User1-User0: 1 (1)
// #18: Transfer(1) User1-User0: 1 (1)
tc.checkL2TxParams(t, blocks[1].Batches[0].L2Txs[0], common.TxTypeTransfer, 1, "User1", "User0", big.NewInt(1), common.BatchNum(6), common.Nonce(1))
// change of Block (implies also a change of batch)
// #18: Transfer(1) A-B: 1 (1)
// #19: Transfer(1) A-B: 1 (1)
tc.checkL2TxParams(t, blocks[1].Batches[0].L2Txs[1], common.TxTypeTransfer, 1, "A", "B", big.NewInt(1), common.BatchNum(6), common.Nonce(5))
}
@ -135,6 +159,7 @@ func (tc *Context) checkL1TxParams(t *testing.T, tx common.L1Tx, typ common.TxTy
assert.Equal(t, amount, tx.Amount)
}
}
func (tc *Context) checkL2TxParams(t *testing.T, tx common.L2Tx, typ common.TxType, tokenID common.TokenID, from, to string, amount *big.Int, batchNum common.BatchNum, nonce common.Nonce) {
assert.Equal(t, typ, tx.Type)
assert.Equal(t, tc.Users[from].Accounts[tokenID].Idx, tx.FromIdx)

Loading…
Cancel
Save