Parametrize ChainID

This commit is contained in:
arnaucube
2020-12-23 14:53:00 +01:00
parent adc044001f
commit 150597c282
27 changed files with 185 additions and 132 deletions

View File

@@ -44,7 +44,8 @@ func TestDebugAPI(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
sdb, err := statedb.NewStateDB(dir, statedb.TypeSynchronizer, 32)
chainID := uint16(0)
sdb, err := statedb.NewStateDB(dir, statedb.TypeSynchronizer, 32, chainID)
require.Nil(t, err)
err = sdb.MakeCheckpoint() // Make a checkpoint to increment the batchNum
require.Nil(t, err)

View File

@@ -16,7 +16,7 @@ func TestCompileSetsBase(t *testing.T) {
_, err = parser.parse()
assert.NoError(t, err)
tc := NewContext(common.RollupConstMaxL1UserTx)
tc := NewContext(0, common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(SetBlockchain0)
assert.NoError(t, err)
_, err = tc.GeneratePoolL2Txs(SetPool0)
@@ -25,7 +25,7 @@ func TestCompileSetsBase(t *testing.T) {
func TestCompileSetsMinimumFlow(t *testing.T) {
// minimum flow
tc := NewContext(common.RollupConstMaxL1UserTx)
tc := NewContext(0, common.RollupConstMaxL1UserTx)
_, err := tc.GenerateBlocks(SetBlockchainMinimumFlow0)
assert.NoError(t, err)
_, err = tc.GeneratePoolL2Txs(SetPoolL2MinimumFlow0)

View File

@@ -61,6 +61,7 @@ type Context struct {
// queued in a batch
rollupConstMaxL1UserTx int
chainID uint16
idx int
currBlock common.BlockData
currBatch common.BatchData
@@ -78,7 +79,7 @@ type Context struct {
}
// NewContext returns a new Context
func NewContext(rollupConstMaxL1UserTx int) *Context {
func NewContext(chainID uint16, rollupConstMaxL1UserTx int) *Context {
currBatchNum := 1 // The protocol defines the first batchNum to be 1
return &Context{
Users: make(map[string]*User),
@@ -88,6 +89,7 @@ func NewContext(rollupConstMaxL1UserTx int) *Context {
LastRegisteredTokenID: 0,
rollupConstMaxL1UserTx: rollupConstMaxL1UserTx,
chainID: chainID,
idx: common.UserThreshold,
// We use some placeholder values for StateRoot and ExitTree
// because these values will never be nil
@@ -630,7 +632,7 @@ func (tc *Context) generatePoolL2Txs() ([]common.PoolL2Tx, error) {
}
tx = *nTx
// perform signature and set it to tx.Signature
toSign, err := tx.HashToSign()
toSign, err := tx.HashToSign(tc.chainID)
if err != nil {
return nil, tracerr.Wrap(fmt.Errorf("Line %d: %s", inst.LineNum, err.Error()))
}
@@ -656,7 +658,7 @@ func (tc *Context) generatePoolL2Txs() ([]common.PoolL2Tx, error) {
}
tx = *nTx
// perform signature and set it to tx.Signature
toSign, err := tx.HashToSign()
toSign, err := tx.HashToSign(tc.chainID)
if err != nil {
return nil, tracerr.Wrap(fmt.Errorf("Line %d: %s", inst.LineNum, err.Error()))
}

View File

@@ -20,7 +20,7 @@ func TestGenerateBlocksNoBatches(t *testing.T) {
> block
`
tc := NewContext(common.RollupConstMaxL1UserTx)
tc := NewContext(0, common.RollupConstMaxL1UserTx)
blocks, err := tc.GenerateBlocks(set)
require.NoError(t, err)
assert.Equal(t, 1, len(blocks))
@@ -87,7 +87,7 @@ func TestGenerateBlocks(t *testing.T) {
// batch and last block
Transfer(1) User1-User0: 1 (1)
`
tc := NewContext(common.RollupConstMaxL1UserTx)
tc := NewContext(0, common.RollupConstMaxL1UserTx)
blocks, err := tc.GenerateBlocks(set)
require.NoError(t, err)
assert.Equal(t, 2, len(blocks))
@@ -191,7 +191,7 @@ func TestGeneratePoolL2Txs(t *testing.T) {
> batchL1
> batchL1
`
tc := NewContext(common.RollupConstMaxL1UserTx)
tc := NewContext(0, common.RollupConstMaxL1UserTx)
_, err := tc.GenerateBlocks(set)
require.NoError(t, err)
set = `
@@ -251,7 +251,7 @@ func TestGeneratePoolL2Txs(t *testing.T) {
> batchL1
> block
`
tc = NewContext(common.RollupConstMaxL1UserTx)
tc = NewContext(0, common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(set)
require.NoError(t, err)
set = `
@@ -282,7 +282,7 @@ func TestGeneratePoolL2TxsFromInstructions(t *testing.T) {
> batchL1
> batchL1
`
tc := NewContext(common.RollupConstMaxL1UserTx)
tc := NewContext(0, common.RollupConstMaxL1UserTx)
_, err := tc.GenerateBlocks(set)
require.NoError(t, err)
@@ -314,7 +314,7 @@ func TestGeneratePoolL2TxsFromInstructions(t *testing.T) {
txsFromInstructions, err := tc.GeneratePoolL2TxsFromInstructions(instructionSet)
require.NoError(t, err)
// Generate Pool txs using string
tc = NewContext(common.RollupConstMaxL1UserTx)
tc = NewContext(0, common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(set)
require.NoError(t, err)
stringSet := `
@@ -338,7 +338,7 @@ func TestGenerateErrors(t *testing.T) {
CreateAccountDeposit(1) A: 5
> batchL1
`
tc := NewContext(common.RollupConstMaxL1UserTx)
tc := NewContext(0, common.RollupConstMaxL1UserTx)
_, err := tc.GenerateBlocks(set)
assert.Equal(t, "Line 2: Can not process CreateAccountDeposit: TokenID 1 not registered, last registered TokenID: 0", err.Error())
@@ -347,7 +347,7 @@ func TestGenerateErrors(t *testing.T) {
Type: Blockchain
AddToken(0)
`
tc = NewContext(common.RollupConstMaxL1UserTx)
tc = NewContext(0, common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(set)
require.Equal(t, "Line 2: AddToken can not register TokenID 0", err.Error())
@@ -355,7 +355,7 @@ func TestGenerateErrors(t *testing.T) {
Type: Blockchain
AddToken(2)
`
tc = NewContext(common.RollupConstMaxL1UserTx)
tc = NewContext(0, common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(set)
require.Equal(t, "Line 2: AddToken TokenID should be sequential, expected TokenID: 1, defined TokenID: 2", err.Error())
@@ -366,7 +366,7 @@ func TestGenerateErrors(t *testing.T) {
AddToken(3)
AddToken(5)
`
tc = NewContext(common.RollupConstMaxL1UserTx)
tc = NewContext(0, common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(set)
require.Equal(t, "Line 5: AddToken TokenID should be sequential, expected TokenID: 4, defined TokenID: 5", err.Error())
@@ -380,7 +380,7 @@ func TestGenerateErrors(t *testing.T) {
Transfer(1) A-B: 6 (1)
> batch
`
tc = NewContext(common.RollupConstMaxL1UserTx)
tc = NewContext(0, common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(set)
require.Equal(t, "Line 5: CreateAccountDeposit(1)BTransfer(1) A-B: 6 (1)\n, err: Expected ':', found 'Transfer'", err.Error())
set = `
@@ -394,7 +394,7 @@ func TestGenerateErrors(t *testing.T) {
Transfer(1) A-B: 6 (1)
> batch
`
tc = NewContext(common.RollupConstMaxL1UserTx)
tc = NewContext(0, common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(set)
require.NoError(t, err)
@@ -412,7 +412,7 @@ func TestGenerateErrors(t *testing.T) {
Exit(1) A: 3 (1)
> batch
`
tc = NewContext(common.RollupConstMaxL1UserTx)
tc = NewContext(0, common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(set)
require.NoError(t, err)
assert.Equal(t, common.Nonce(3), tc.Users["A"].Accounts[common.TokenID(1)].Nonce)
@@ -518,7 +518,7 @@ func TestGenerateFromInstructions(t *testing.T) {
Typ: TypeNewBlock,
})
tc := NewContext(common.RollupConstMaxL1UserTx)
tc := NewContext(0, common.RollupConstMaxL1UserTx)
blockFromInstructions, err := tc.GenerateBlocksFromInstructions(setInst)
require.NoError(t, err)
@@ -537,7 +537,7 @@ func TestGenerateFromInstructions(t *testing.T) {
> batch
> block
`
tc = NewContext(common.RollupConstMaxL1UserTx)
tc = NewContext(0, common.RollupConstMaxL1UserTx)
blockFromString, err := tc.GenerateBlocks(setString)
require.NoError(t, err)