Reorganize smart contract types, udate eth tests, etc.

- Move smart contract constants and structs for variables to
  common/{ethrollup.go, ethauction.go, ethwdelayer.go}:
    - This removes repeated code of the structs for variables
    - Allows reusing the constants and variables from all modules without
      import cycles
- Remove unused common/scvars.go
- In common.BlockData, split data from each smart contract into a sepparate
  field (Rollup, Auction, WDelayer).  This affects the structures that til uses
  as output, and HistoryDB in the AddBlockSCData.
- In Synchronizer:
    - Pass starting block of each smart contract as config, instead of
      incorrectly using the genesis block found in the acution constant (which
      has a very different meaning)
    - Use variable structs from common instead of an internal copy
    - Synchronize more stuff (resolve some TODOs)
    - Fix some issues found after initial testing with ganache
- In eth:
    - In auction.go: Add method to get constants
    - Update README to use ganache instead of buidlerevm as local blockchain
      for testing
    - Update env variables and test vectors to pass the tests with the
      deployment in the ganache testnet.
    - Use ethereum keys derived from paths (hdwallet) in testing to avoid
      hardcoding private keys and generate the same keys from a mnemonic used
      in the ganache tesnet.
This commit is contained in:
Eduard S
2020-10-28 16:09:05 +01:00
parent 954b24020c
commit e6fb0a03de
36 changed files with 1006 additions and 722 deletions

View File

@@ -32,10 +32,10 @@ func init() {
// RollupBlock stores all the data related to the Rollup SC from an ethereum block
type RollupBlock struct {
State eth.RollupState
Vars eth.RollupVariables
Vars common.RollupVariables
Events eth.RollupEvents
Txs map[ethCommon.Hash]*types.Transaction
Constants *eth.RollupPublicConstants
Constants *common.RollupConstants
Eth *EthereumBlock
}
@@ -55,10 +55,10 @@ var (
// AuctionBlock stores all the data related to the Auction SC from an ethereum block
type AuctionBlock struct {
State eth.AuctionState
Vars eth.AuctionVariables
Vars common.AuctionVariables
Events eth.AuctionEvents
Txs map[ethCommon.Hash]*types.Transaction
Constants *eth.AuctionConstants
Constants *common.AuctionConstants
Eth *EthereumBlock
}
@@ -219,10 +219,10 @@ func (b *Block) Next() *Block {
// ClientSetup is used to initialize the constants of the Smart Contracts and
// other details of the test Client
type ClientSetup struct {
RollupConstants *eth.RollupPublicConstants
RollupVariables *eth.RollupVariables
AuctionConstants *eth.AuctionConstants
AuctionVariables *eth.AuctionVariables
RollupConstants *common.RollupConstants
RollupVariables *common.RollupVariables
AuctionConstants *common.AuctionConstants
AuctionVariables *common.AuctionVariables
VerifyProof bool
}
@@ -241,8 +241,8 @@ func NewClientSetupExample() *ClientSetup {
}
tokenHEZ := ethCommon.HexToAddress("0x51D243D62852Bba334DD5cc33f242BAc8c698074")
governanceAddress := ethCommon.HexToAddress("0x688EfD95BA4391f93717CF02A9aED9DBD2855cDd")
rollupConstants := &eth.RollupPublicConstants{
Verifiers: []eth.RollupVerifierStruct{
rollupConstants := &common.RollupConstants{
Verifiers: []common.RollupVerifierStruct{
{
MaxTx: 2048,
NLevels: 32,
@@ -254,12 +254,12 @@ func NewClientSetupExample() *ClientSetup {
HermezAuctionContract: ethCommon.HexToAddress("0x8E442975805fb1908f43050c9C1A522cB0e28D7b"),
WithdrawDelayerContract: ethCommon.HexToAddress("0x5CB7979cBdbf65719BEE92e4D15b7b7Ed3D79114"),
}
rollupVariables := &eth.RollupVariables{
rollupVariables := &common.RollupVariables{
FeeAddToken: big.NewInt(11),
ForgeL1L2BatchTimeout: 9,
WithdrawalDelay: 80,
}
auctionConstants := &eth.AuctionConstants{
auctionConstants := &common.AuctionConstants{
BlocksPerSlot: 40,
InitialMinimalBidding: initialMinimalBidding,
GenesisBlockNum: 1,
@@ -267,7 +267,7 @@ func NewClientSetupExample() *ClientSetup {
TokenHEZ: tokenHEZ,
HermezRollup: ethCommon.HexToAddress("0x474B6e29852257491cf283EfB1A9C61eBFe48369"),
}
auctionVariables := &eth.AuctionVariables{
auctionVariables := &common.AuctionVariables{
DonationAddress: ethCommon.HexToAddress("0x61Ed87CF0A1496b49A420DA6D84B58196b98f2e7"),
BootCoordinator: ethCommon.HexToAddress("0xE39fEc6224708f0772D2A74fd3f9055A90E0A9f2"),
DefaultSlotSetBid: [6]*big.Int{
@@ -310,8 +310,8 @@ type Client struct {
rw *sync.RWMutex
log bool
addr *ethCommon.Address
rollupConstants *eth.RollupPublicConstants
auctionConstants *eth.AuctionConstants
rollupConstants *common.RollupConstants
auctionConstants *common.AuctionConstants
blocks map[int64]*Block
// state state
blockNum int64 // last mined block num
@@ -653,7 +653,7 @@ func (c *Client) RollupL1UserTxERC20ETH(
nextBlock := c.nextBlock()
r := nextBlock.Rollup
queue := r.State.MapL1TxQueue[r.State.LastToForgeL1TxsNum]
if len(queue.L1TxQueue) >= eth.RollupConstMaxL1UserTx {
if len(queue.L1TxQueue) >= common.RollupConstMaxL1UserTx {
r.State.LastToForgeL1TxsNum++
r.State.MapL1TxQueue[r.State.LastToForgeL1TxsNum] = eth.NewQueueStruct()
queue = r.State.MapL1TxQueue[r.State.LastToForgeL1TxsNum]
@@ -883,12 +883,11 @@ func (c *Client) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (tx *types.Tra
// }
// RollupConstants returns the Constants of the Rollup Smart Contract
func (c *Client) RollupConstants() (*eth.RollupPublicConstants, error) {
func (c *Client) RollupConstants() (*common.RollupConstants, error) {
c.rw.RLock()
defer c.rw.RUnlock()
log.Error("TODO")
return nil, errTODO
return c.rollupConstants, nil
}
// RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
@@ -1326,7 +1325,7 @@ func (c *Client) AuctionGetClaimableHEZ(bidder ethCommon.Address) (*big.Int, err
}
// AuctionConstants returns the Constants of the Auction Smart Contract
func (c *Client) AuctionConstants() (*eth.AuctionConstants, error) {
func (c *Client) AuctionConstants() (*common.AuctionConstants, error) {
c.rw.RLock()
defer c.rw.RUnlock()

View File

@@ -4,7 +4,7 @@ import (
"strings"
"testing"
"github.com/hermeznetwork/hermez-node/eth"
"github.com/hermeznetwork/hermez-node/common"
"github.com/stretchr/testify/assert"
)
@@ -16,7 +16,7 @@ func TestCompileSetsBase(t *testing.T) {
_, err = parser.parse()
assert.Nil(t, err)
tc := NewContext(eth.RollupConstMaxL1UserTx)
tc := NewContext(common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(SetBlockchain0)
assert.Nil(t, err)
_, err = tc.GeneratePoolL2Txs(SetPool0)
@@ -25,7 +25,7 @@ func TestCompileSetsBase(t *testing.T) {
func TestCompileSetsMinimumFlow(t *testing.T) {
// minimum flow
tc := NewContext(eth.RollupConstMaxL1UserTx)
tc := NewContext(common.RollupConstMaxL1UserTx)
_, err := tc.GenerateBlocks(SetBlockchainMinimumFlow0)
assert.Nil(t, err)
_, err = tc.GeneratePoolL2Txs(SetPoolL2MinimumFlow0)

View File

@@ -30,7 +30,9 @@ func newBlock(blockNum int64) common.BlockData {
Block: common.Block{
EthBlockNum: blockNum,
},
L1UserTxs: []common.L1Tx{},
Rollup: common.RollupData{
L1UserTxs: []common.L1Tx{},
},
}
}
@@ -339,7 +341,7 @@ func (tc *Context) GenerateBlocks(set string) ([]common.BlockData, error) {
return nil, fmt.Errorf("Line %d: AddToken TokenID should be sequential, expected TokenID: %d, defined TokenID: %d", inst.lineNum, tc.LastRegisteredTokenID+1, inst.tokenID)
}
tc.LastRegisteredTokenID++
tc.currBlock.AddedTokens = append(tc.currBlock.AddedTokens, newToken)
tc.currBlock.Rollup.AddedTokens = append(tc.currBlock.Rollup.AddedTokens, newToken)
default:
return nil, fmt.Errorf("Line %d: Unexpected type: %s", inst.lineNum, inst.typ)
}
@@ -410,7 +412,7 @@ func (tc *Context) setIdxs() error {
}
tc.currBatch.Batch.LastIdx = int64(tc.idx - 1) // `-1` because tc.idx is the next available idx
tc.currBlock.Batches = append(tc.currBlock.Batches, tc.currBatch)
tc.currBlock.Rollup.Batches = append(tc.currBlock.Rollup.Batches, tc.currBatch)
tc.currBatchNum++
tc.currBatch = newBatchData(tc.currBatchNum)
tc.currBatchTest.l1CoordinatorTxs = nil
@@ -460,7 +462,7 @@ func (tc *Context) addToL1Queue(tx L1Tx) error {
tx.L1Tx = *nTx
tc.Queues[tc.openToForge] = append(tc.Queues[tc.openToForge], tx)
tc.currBlock.L1UserTxs = append(tc.currBlock.L1UserTxs, tx.L1Tx)
tc.currBlock.Rollup.L1UserTxs = append(tc.currBlock.Rollup.L1UserTxs, tx.L1Tx)
return nil
}

View File

@@ -5,7 +5,6 @@ import (
"testing"
"github.com/hermeznetwork/hermez-node/common"
"github.com/hermeznetwork/hermez-node/eth"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -21,13 +20,13 @@ func TestGenerateBlocksNoBatches(t *testing.T) {
> block
`
tc := NewContext(eth.RollupConstMaxL1UserTx)
tc := NewContext(common.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))
assert.Equal(t, 0, len(blocks[0].Rollup.Batches))
assert.Equal(t, 2, len(blocks[0].Rollup.AddedTokens))
assert.Equal(t, 2, len(blocks[0].Rollup.L1UserTxs))
}
func TestGenerateBlocks(t *testing.T) {
@@ -88,58 +87,58 @@ func TestGenerateBlocks(t *testing.T) {
// batch and last block
Transfer(1) User1-User0: 1 (1)
`
tc := NewContext(eth.RollupConstMaxL1UserTx)
tc := NewContext(common.RollupConstMaxL1UserTx)
blocks, err := tc.GenerateBlocks(set)
require.Nil(t, err)
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, 9, len(blocks[0].L1UserTxs))
assert.Equal(t, 4, len(blocks[0].Batches[3].L1CoordinatorTxs))
assert.Equal(t, 0, len(blocks[1].L1UserTxs))
assert.Equal(t, 5, len(blocks[0].Rollup.Batches))
assert.Equal(t, 1, len(blocks[1].Rollup.Batches))
assert.Equal(t, 9, len(blocks[0].Rollup.L1UserTxs))
assert.Equal(t, 4, len(blocks[0].Rollup.Batches[3].L1CoordinatorTxs))
assert.Equal(t, 0, len(blocks[1].Rollup.L1UserTxs))
// Check expected values generated by each line
// #0: Deposit(1) A: 10
tc.checkL1TxParams(t, blocks[0].L1UserTxs[0], common.TxTypeCreateAccountDeposit, 1, "A", "", big.NewInt(10), nil)
tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[0], common.TxTypeCreateAccountDeposit, 1, "A", "", big.NewInt(10), nil)
// #1: Deposit(2) A: 20
tc.checkL1TxParams(t, blocks[0].L1UserTxs[1], common.TxTypeCreateAccountDeposit, 2, "A", "", big.NewInt(20), nil)
tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[1], common.TxTypeCreateAccountDeposit, 2, "A", "", big.NewInt(20), nil)
// // #2: Deposit(1) A: 20
tc.checkL1TxParams(t, blocks[0].L1UserTxs[2], common.TxTypeCreateAccountDeposit, 1, "B", "", big.NewInt(5), nil)
tc.checkL1TxParams(t, blocks[0].Rollup.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)
tc.checkL1TxParams(t, blocks[0].Rollup.L1UserTxs[3], common.TxTypeCreateAccountDeposit, 1, "C", "", big.NewInt(5), nil)
// // #4: CreateAccountDeposit(1) D: 5
tc.checkL1TxParams(t, blocks[0].L1UserTxs[4], common.TxTypeCreateAccountDeposit, 1, "D", "", big.NewInt(5), nil)
tc.checkL1TxParams(t, blocks[0].Rollup.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))
tc.checkL2TxParams(t, blocks[0].Rollup.Batches[2].L2Txs[0], common.TxTypeTransfer, 1, "A", "B", big.NewInt(6), common.BatchNum(3))
// #6: Transfer(1) B-D: 3 (1)
tc.checkL2TxParams(t, blocks[0].Batches[2].L2Txs[1], common.TxTypeTransfer, 1, "B", "D", big.NewInt(3), common.BatchNum(3))
tc.checkL2TxParams(t, blocks[0].Rollup.Batches[2].L2Txs[1], common.TxTypeTransfer, 1, "B", "D", big.NewInt(3), common.BatchNum(3))
// #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))
tc.checkL2TxParams(t, blocks[0].Rollup.Batches[2].L2Txs[2], common.TxTypeTransfer, 1, "A", "D", big.NewInt(1), common.BatchNum(3))
// change of Batch
// #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))
tc.checkL1TxParams(t, blocks[0].Rollup.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))
tc.checkL1TxParams(t, blocks[0].Rollup.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))
tc.checkL2TxParams(t, blocks[0].Rollup.Batches[3].L2Txs[0], common.TxTypeTransfer, 1, "C", "A", big.NewInt(3), common.BatchNum(4))
// #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))
tc.checkL2TxParams(t, blocks[0].Rollup.Batches[3].L2Txs[1], common.TxTypeTransfer, 2, "A", "B", big.NewInt(15), common.BatchNum(4))
// #13: Deposit(1) User0: 20
tc.checkL1TxParams(t, blocks[0].L1UserTxs[7], common.TxTypeCreateAccountDeposit, 1, "User0", "", big.NewInt(20), nil)
tc.checkL1TxParams(t, blocks[0].Rollup.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)
tc.checkL1TxParams(t, blocks[0].Rollup.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))
tc.checkL2TxParams(t, blocks[0].Rollup.Batches[4].L2Txs[0], common.TxTypeTransfer, 1, "User0", "User1", big.NewInt(15), common.BatchNum(5))
// #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))
tc.checkL2TxParams(t, blocks[0].Rollup.Batches[4].L2Txs[1], common.TxTypeTransfer, 3, "User1", "User0", big.NewInt(15), common.BatchNum(5))
// #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))
tc.checkL2TxParams(t, blocks[0].Rollup.Batches[4].L2Txs[2], common.TxTypeTransfer, 1, "A", "C", big.NewInt(1), common.BatchNum(5))
// change of Batch
// #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))
tc.checkL2TxParams(t, blocks[1].Rollup.Batches[0].L2Txs[0], common.TxTypeTransfer, 1, "User1", "User0", big.NewInt(1), common.BatchNum(6))
// change of Block (implies also a change of batch)
// #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))
tc.checkL2TxParams(t, blocks[1].Rollup.Batches[0].L2Txs[1], common.TxTypeTransfer, 1, "A", "B", big.NewInt(1), common.BatchNum(6))
}
func (tc *Context) checkL1TxParams(t *testing.T, tx common.L1Tx, typ common.TxType, tokenID common.TokenID, from, to string, loadAmount, amount *big.Int) {
@@ -192,7 +191,7 @@ func TestGeneratePoolL2Txs(t *testing.T) {
> batchL1
> batchL1
`
tc := NewContext(eth.RollupConstMaxL1UserTx)
tc := NewContext(common.RollupConstMaxL1UserTx)
_, err := tc.GenerateBlocks(set)
require.Nil(t, err)
set = `
@@ -252,7 +251,7 @@ func TestGeneratePoolL2Txs(t *testing.T) {
> batchL1
> block
`
tc = NewContext(eth.RollupConstMaxL1UserTx)
tc = NewContext(common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(set)
require.Nil(t, err)
set = `
@@ -278,7 +277,7 @@ func TestGenerateErrors(t *testing.T) {
CreateAccountDeposit(1) A: 5
> batchL1
`
tc := NewContext(eth.RollupConstMaxL1UserTx)
tc := NewContext(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())
@@ -287,7 +286,7 @@ func TestGenerateErrors(t *testing.T) {
Type: Blockchain
AddToken(0)
`
tc = NewContext(eth.RollupConstMaxL1UserTx)
tc = NewContext(common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(set)
require.Equal(t, "Line 2: AddToken can not register TokenID 0", err.Error())
@@ -295,7 +294,7 @@ func TestGenerateErrors(t *testing.T) {
Type: Blockchain
AddToken(2)
`
tc = NewContext(eth.RollupConstMaxL1UserTx)
tc = NewContext(common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(set)
require.Equal(t, "Line 2: AddToken TokenID should be sequential, expected TokenID: 1, defined TokenID: 2", err.Error())
@@ -306,7 +305,7 @@ func TestGenerateErrors(t *testing.T) {
AddToken(3)
AddToken(5)
`
tc = NewContext(eth.RollupConstMaxL1UserTx)
tc = NewContext(common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(set)
require.Equal(t, "Line 5: AddToken TokenID should be sequential, expected TokenID: 4, defined TokenID: 5", err.Error())
@@ -320,7 +319,7 @@ func TestGenerateErrors(t *testing.T) {
Transfer(1) A-B: 6 (1)
> batch
`
tc = NewContext(eth.RollupConstMaxL1UserTx)
tc = NewContext(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 = `
@@ -334,7 +333,7 @@ func TestGenerateErrors(t *testing.T) {
Transfer(1) A-B: 6 (1)
> batch
`
tc = NewContext(eth.RollupConstMaxL1UserTx)
tc = NewContext(common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(set)
require.Nil(t, err)
@@ -352,7 +351,7 @@ func TestGenerateErrors(t *testing.T) {
Exit(1) A: 3 (1)
> batch
`
tc = NewContext(eth.RollupConstMaxL1UserTx)
tc = NewContext(common.RollupConstMaxL1UserTx)
_, err = tc.GenerateBlocks(set)
require.Nil(t, err)
assert.Equal(t, common.Nonce(3), tc.Users["A"].Accounts[common.TokenID(1)].Nonce)