Browse Source

Update ethclient rollup iteration 3

feature/sql-semaphore1
laisolizq 3 years ago
committed by Eduard S
parent
commit
a405ab3a20
2 changed files with 883 additions and 161 deletions
  1. +200
    -156
      eth/rollup.go
  2. +683
    -5
      eth/rollup_test.go

+ 200
- 156
eth/rollup.go

@ -22,11 +22,6 @@ import (
)
const (
// RollupConstFeeIdxCoordinatorLen is the number of tokens the coordinator can use
// to collect fees (determines the number of tokens that the
// coordinator can collect fees from). This value is determined by the
// circuit.
RollupConstFeeIdxCoordinatorLen = 64
// RollupConstReservedIDx First 256 indexes reserved, first user index will be the 256
RollupConstReservedIDx = 255
// RollupConstExitIDx IDX 1 is reserved for exits
@ -99,38 +94,6 @@ var (
RollupConstERC20Signature = crypto.Keccak256([]byte("decimals()"))
)
// RollupConstants are the constants of the Rollup Smart Contract
/* type RollupConstants struct {
// Maxim Deposit allowed
MaxAmountDeposit *big.Int
MaxAmountL2 *big.Int
MaxTokens int64
// maximum L1 transactions allowed to be queued for a batch
MaxL1Tx int
// maximum L1 user transactions allowed to be queued for a batch
MaxL1UserTx int
Rfield *big.Int
L1CoordinatorBytes int
L1UserBytes int
L2Bytes int
MaxTxVerifiers []int
TokenHEZ ethCommon.Address
// Only test
GovernanceAddress ethCommon.Address
// Only test
SafetyBot ethCommon.Address
// Only test
ConsensusContract ethCommon.Address
// Only test
WithdrawalContract ethCommon.Address
ReservedIDx uint32
LastIDx uint32
ExitIDx uint32
NoLimitToken int
NumBuckets int
MaxWDelay int64
}*/
// RollupPublicConstants are the constants of the Rollup Smart Contract
type RollupPublicConstants struct {
AbsoluteMaxL1L2BatchTimeout int64
@ -187,20 +150,20 @@ type RollupState struct {
type RollupEventL1UserTx struct {
ToForgeL1TxsNum int64 // QueueIndex *big.Int
Position int // TransactionIndex *big.Int
L1Tx common.L1Tx
L1UserTx common.L1Tx
}
// RollupEventL1UserTxAux is an event of the Rollup Smart Contract
type RollupEventL1UserTxAux struct {
ToForgeL1TxsNum uint64 // QueueIndex *big.Int
Position uint8 // TransactionIndex *big.Int
L1Tx []byte
L1UserTx []byte
}
// RollupEventAddToken is an event of the Rollup Smart Contract
type RollupEventAddToken struct {
Address ethCommon.Address
TokenID uint32
TokenAddress ethCommon.Address
TokenID uint32
}
// RollupEventForgeBatch is an event of the Rollup Smart Contract
@ -211,12 +174,12 @@ type RollupEventForgeBatch struct {
// RollupEventUpdateForgeL1L2BatchTimeout is an event of the Rollup Smart Contract
type RollupEventUpdateForgeL1L2BatchTimeout struct {
ForgeL1L2BatchTimeout uint8
NewForgeL1L2BatchTimeout uint8
}
// RollupEventUpdateFeeAddToken is an event of the Rollup Smart Contract
type RollupEventUpdateFeeAddToken struct {
FeeAddToken *big.Int
NewFeeAddToken *big.Int
}
// RollupEventWithdrawEvent is an event of the Rollup Smart Contract
@ -253,9 +216,9 @@ type RollupForgeBatchArgs struct {
NewLastIdx int64
NewStRoot *big.Int
NewExitRoot *big.Int
L1CoordinatorTxs []common.L1Tx
L1CoordinatorTxs []*common.L1Tx
L1CoordinatorTxsAuths [][]byte // Authorization for accountCreations for each L1CoordinatorTx
L2TxsData []common.L2Tx
L2TxsData []*common.L2Tx
FeeIdxCoordinator []common.Idx
// Circuit selector
VerifierIdx uint8
@ -291,29 +254,20 @@ type RollupInterface interface {
RollupForgeBatch(*RollupForgeBatchArgs) (*types.Transaction, error)
RollupAddToken(tokenAddress ethCommon.Address, feeAddToken *big.Int) (*types.Transaction, error)
RollupWithdraw(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey,
numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error)
RollupForceExit(fromIdx int64, amountF common.Float16, tokenID int64) (*types.Transaction, error)
RollupForceTransfer(fromIdx int64, amountF common.Float16, tokenID, toIdx int64) (*types.Transaction, error)
RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey,
loadAmountF, amountF common.Float16, tokenID int64, toIdx int64) (*types.Transaction, error)
RollupDepositTransfer(fromIdx int64, loadAmountF, amountF common.Float16,
tokenID int64, toIdx int64) (*types.Transaction, error)
RollupDeposit(fromIdx int64, loadAmountF common.Float16, tokenID int64) (*types.Transaction, error)
RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte,
babyPubKey babyjub.PublicKey, loadAmountF common.Float16) (*types.Transaction, error)
RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF common.Float16,
tokenID int64) (*types.Transaction, error)
RollupGetCurrentTokens() (*big.Int, error)
// RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error)
// RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error)
// RollupGetQueue(queue int64) ([]byte, error)
RollupWithdrawMerkleProof(babyPubKey *babyjub.PublicKey, tokenID uint32, numExitRoot, idx int64, amount *big.Int, siblings []*big.Int, instantWithdraw bool) (*types.Transaction, error)
RollupWithdrawCircuit(proofA, proofC [2]*big.Int, proofB [2][2]*big.Int, tokenID uint32, numExitRoot, idx int64, amount *big.Int, instantWithdraw bool) (*types.Transaction, error)
RollupL1UserTxERC20ETH(fromBJJ *babyjub.PublicKey, fromIdx int64, loadAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64) (*types.Transaction, error)
RollupL1UserTxERC777(fromBJJ *babyjub.PublicKey, fromIdx int64, loadAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64) (*types.Transaction, error)
// Governance Public Functions
RollupUpdateForgeL1L2BatchTimeout(newForgeL1Timeout int64) (*types.Transaction, error)
RollupUpdateForgeL1L2BatchTimeout(newForgeL1L2BatchTimeout uint8) (*types.Transaction, error)
RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error)
// Viewers
RollupRegisterTokensCount() (*big.Int, error)
//
// Smart Contract Status
//
@ -329,25 +283,25 @@ type RollupInterface interface {
// RollupClient is the implementation of the interface to the Rollup Smart Contract in ethereum.
type RollupClient struct {
client *EthereumClient
address ethCommon.Address
tokenHEZAddress ethCommon.Address
gasLimit uint64
contractAbi abi.ABI
client *EthereumClient
address ethCommon.Address
tokenAddress ethCommon.Address
gasLimit uint64
contractAbi abi.ABI
}
// NewRollupClient creates a new RollupClient
func NewRollupClient(client *EthereumClient, address ethCommon.Address, tokenHEZAddress ethCommon.Address) (*RollupClient, error) {
func NewRollupClient(client *EthereumClient, address ethCommon.Address, tokenAddress ethCommon.Address) (*RollupClient, error) {
contractAbi, err := abi.JSON(strings.NewReader(string(Hermez.HermezABI)))
if err != nil {
return nil, err
}
return &RollupClient{
client: client,
address: address,
tokenHEZAddress: tokenHEZAddress,
gasLimit: 1000000, //nolint:gomnd
contractAbi: contractAbi,
client: client,
address: address,
tokenAddress: tokenAddress,
gasLimit: 1000000, //nolint:gomnd
contractAbi: contractAbi,
}, nil
}
@ -367,7 +321,7 @@ func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (*types.Tran
return nil, err
}
nLevels := rollupConst.Verifiers[args.VerifierIdx].NLevels
lenBytes := nLevels / 8 //nolint:gomnd
lenBytes := nLevels / 8
newLastIdx := big.NewInt(int64(args.NewLastIdx))
var l1CoordinatorBytes []byte
for i := 0; i < len(args.L1CoordinatorTxs); i++ {
@ -404,16 +358,14 @@ func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs) (*types.Tran
return tx, nil
}
// RollupAddToken is the interface to call the smart contract function.
// `feeAddToken` is the amount of HEZ tokens that will be paid to add the
// token. `feeAddToken` must match the public value of the smart contract.
// RollupAddToken is the interface to call the smart contract function
func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address, feeAddToken *big.Int) (*types.Transaction, error) {
var tx *types.Transaction
var err error
if tx, err = c.client.CallAuth(
c.gasLimit,
func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
tokens, err := ERC777.NewERC777(c.tokenHEZAddress, ec)
tokens, err := ERC777.NewERC777(c.tokenAddress, ec)
if err != nil {
return nil, err
}
@ -436,90 +388,180 @@ func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address, feeAddToke
return tx, nil
}
// RollupWithdrawSNARK is the interface to call the smart contract function
// func (c *RollupClient) RollupWithdrawSNARK() (*types.Transaction, error) { // TODO (Not defined in Hermez.sol)
// return nil, errTODO
// }
// RollupWithdraw is the interface to call the smart contract function
func (c *RollupClient) RollupWithdraw(tokenID int64, balance *big.Int, babyPubKey *babyjub.PublicKey, numExitRoot int64, siblings []*big.Int, idx int64, instantWithdraw bool) (*types.Transaction, error) {
log.Error("TODO")
return nil, errTODO
}
// RollupForceExit is the interface to call the smart contract function
func (c *RollupClient) RollupForceExit(fromIdx int64, amountF common.Float16, tokenID int64) (*types.Transaction, error) {
log.Error("TODO")
return nil, errTODO
}
// RollupForceTransfer is the interface to call the smart contract function
func (c *RollupClient) RollupForceTransfer(fromIdx int64, amountF common.Float16, tokenID, toIdx int64) (*types.Transaction, error) {
log.Error("TODO")
return nil, errTODO
}
// RollupCreateAccountDepositTransfer is the interface to call the smart contract function
func (c *RollupClient) RollupCreateAccountDepositTransfer(babyPubKey babyjub.PublicKey, loadAmountF, amountF common.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
log.Error("TODO")
return nil, errTODO
}
// RollupDepositTransfer is the interface to call the smart contract function
func (c *RollupClient) RollupDepositTransfer(fromIdx int64, loadAmountF, amountF common.Float16, tokenID int64, toIdx int64) (*types.Transaction, error) {
log.Error("TODO")
return nil, errTODO
// RollupWithdrawMerkleProof is the interface to call the smart contract function
func (c *RollupClient) RollupWithdrawMerkleProof(fromBJJ *babyjub.PublicKey, tokenID uint32, numExitRoot, idx int64, amount *big.Int, siblings []*big.Int, instantWithdraw bool) (*types.Transaction, error) {
var tx *types.Transaction
var err error
if tx, err = c.client.CallAuth(
c.gasLimit,
func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
hermez, err := Hermez.NewHermez(c.address, ec)
if err != nil {
return nil, err
}
pkCompL := fromBJJ.Compress()
pkCompB := common.SwapEndianness(pkCompL[:])
babyPubKey := new(big.Int).SetBytes(pkCompB)
numExitRootB := big.NewInt(numExitRoot)
idxBig := big.NewInt(idx)
return hermez.WithdrawMerkleProof(auth, tokenID, amount, babyPubKey, numExitRootB, siblings, idxBig, instantWithdraw)
},
); err != nil {
return nil, fmt.Errorf("Failed update WithdrawMerkleProof: %w", err)
}
return tx, nil
}
// RollupDeposit is the interface to call the smart contract function
func (c *RollupClient) RollupDeposit(fromIdx int64, loadAmountF common.Float16, tokenID int64) (*types.Transaction, error) {
// RollupWithdrawCircuit is the interface to call the smart contract function
func (c *RollupClient) RollupWithdrawCircuit(proofA, proofC [2]*big.Int, proofB [2][2]*big.Int, tokenID uint32, numExitRoot, idx int64, amount *big.Int, instantWithdraw bool) (*types.Transaction, error) {
log.Error("TODO")
return nil, errTODO
}
// RollupCreateAccountDepositFromRelayer is the interface to call the smart contract function
func (c *RollupClient) RollupCreateAccountDepositFromRelayer(accountCreationAuthSig []byte, babyPubKey babyjub.PublicKey, loadAmountF common.Float16) (*types.Transaction, error) {
log.Error("TODO")
return nil, errTODO
// RollupL1UserTxERC20ETH is the interface to call the smart contract function
func (c *RollupClient) RollupL1UserTxERC20ETH(fromBJJ *babyjub.PublicKey, fromIdx int64, loadAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64) (*types.Transaction, error) {
var tx *types.Transaction
var err error
if tx, err = c.client.CallAuth(
c.gasLimit,
func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
hermez, err := Hermez.NewHermez(c.address, ec)
if err != nil {
return nil, err
}
pkCompL := fromBJJ.Compress()
pkCompB := common.SwapEndianness(pkCompL[:])
babyPubKey := new(big.Int).SetBytes(pkCompB)
fromIdxBig := big.NewInt(fromIdx)
toIdxBig := big.NewInt(toIdx)
tokenIDBig := uint32(tokenID)
loadAmountF, err := common.NewFloat16(loadAmount)
if err != nil {
return nil, err
}
amountF, err := common.NewFloat16(amount)
if tokenID == 0 {
auth.Value = loadAmount
}
return hermez.AddL1Transaction(auth, babyPubKey, fromIdxBig, uint16(loadAmountF), uint16(amountF), tokenIDBig, toIdxBig)
},
); err != nil {
return nil, fmt.Errorf("Failed add L1 Tx ERC20/ETH: %w", err)
}
return tx, nil
}
// RollupCreateAccountDeposit is the interface to call the smart contract function
func (c *RollupClient) RollupCreateAccountDeposit(babyPubKey babyjub.PublicKey, loadAmountF common.Float16, tokenID int64) (*types.Transaction, error) {
log.Error("TODO")
return nil, errTODO
// RollupL1UserTxERC777 is the interface to call the smart contract function
func (c *RollupClient) RollupL1UserTxERC777(fromBJJ *babyjub.PublicKey, fromIdx int64, loadAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64) (*types.Transaction, error) {
var tx *types.Transaction
var err error
if tx, err = c.client.CallAuth(
c.gasLimit,
func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
tokens, err := ERC777.NewERC777(c.tokenAddress, ec)
if err != nil {
return nil, err
}
addL1TxFnSignature := []byte("addL1Transaction(uint256,uint48,uint16,uint16,uint32,uint48)")
hash := sha3.NewLegacyKeccak256()
_, err = hash.Write(addL1TxFnSignature)
if err != nil {
return nil, err
}
methodID := hash.Sum(nil)[:4]
pkCompL := fromBJJ.Compress()
pkCompB := common.SwapEndianness(pkCompL[:])
paddedAddress := ethCommon.LeftPadBytes(pkCompB, 32)
fromIdxB, err := common.Idx(fromIdx).Bytes()
if err != nil {
return nil, err
}
paddedFromIdx := ethCommon.LeftPadBytes(fromIdxB[:], 32)
loadAmountF, err := common.NewFloat16(loadAmount)
if err != nil {
return nil, err
}
paddedLoadAmount := ethCommon.LeftPadBytes(loadAmountF.Bytes(), 32)
amountF, err := common.NewFloat16(amount)
if err != nil {
return nil, err
}
paddedAmount := ethCommon.LeftPadBytes(amountF.Bytes(), 32)
tokenIDB := common.TokenID(tokenID).Bytes()
paddedTokenID := ethCommon.LeftPadBytes(tokenIDB, 32)
toIdxB, err := common.Idx(toIdx).Bytes()
if err != nil {
return nil, err
}
paddedToIdx := ethCommon.LeftPadBytes(toIdxB[:], 32)
var data []byte
data = append(data, methodID...)
data = append(data, paddedAddress[:]...)
data = append(data, paddedFromIdx[:]...)
data = append(data, paddedLoadAmount[:]...)
data = append(data, paddedAmount[:]...)
data = append(data, paddedTokenID[:]...)
data = append(data, paddedToIdx[:]...)
return tokens.Send(auth, c.address, loadAmount, data)
},
); err != nil {
return nil, fmt.Errorf("Failed add L1 Tx ERC777: %w", err)
}
return tx, nil
}
// RollupGetTokenAddress is the interface to call the smart contract function
/* func (c *RollupClient) RollupGetTokenAddress(tokenID int64) (*ethCommon.Address, error) {
return nil, errTODO
} */
// RollupGetCurrentTokens is the interface to call the smart contract function
func (c *RollupClient) RollupGetCurrentTokens() (*big.Int, error) {
log.Error("TODO")
return nil, errTODO
// RollupRegisterTokensCount is the interface to call the smart contract function
func (c *RollupClient) RollupRegisterTokensCount() (*big.Int, error) {
var registerTokensCount *big.Int
if err := c.client.Call(func(ec *ethclient.Client) error {
hermez, err := Hermez.NewHermez(c.address, ec)
if err != nil {
return err
}
registerTokensCount, err = hermez.RegisterTokensCount(nil)
return err
}); err != nil {
return nil, err
}
return registerTokensCount, nil
}
// RollupGetL1TxFromQueue is the interface to call the smart contract function
/* func (c *RollupClient) RollupGetL1TxFromQueue(queue int64, position int64) ([]byte, error) {
return nil, errTODO
} */
// RollupGetQueue is the interface to call the smart contract function
/* func (c *RollupClient) RollupGetQueue(queue int64) ([]byte, error) {
return nil, errTODO
}*/
// RollupUpdateForgeL1L2BatchTimeout is the interface to call the smart contract function
func (c *RollupClient) RollupUpdateForgeL1L2BatchTimeout(newForgeL1Timeout int64) (*types.Transaction, error) {
log.Error("TODO")
return nil, errTODO
func (c *RollupClient) RollupUpdateForgeL1L2BatchTimeout(newForgeL1L2BatchTimeout uint8) (*types.Transaction, error) {
var tx *types.Transaction
var err error
if tx, err = c.client.CallAuth(
c.gasLimit,
func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
hermez, err := Hermez.NewHermez(c.address, ec)
if err != nil {
return nil, err
}
return hermez.UpdateForgeL1L2BatchTimeout(auth, newForgeL1L2BatchTimeout)
},
); err != nil {
return nil, fmt.Errorf("Failed update ForgeL1L2BatchTimeout: %w", err)
}
return tx, nil
}
// RollupUpdateFeeAddToken is the interface to call the smart contract function
func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error) {
log.Error("TODO")
return nil, errTODO
var tx *types.Transaction
var err error
if tx, err = c.client.CallAuth(
c.gasLimit,
func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
hermez, err := Hermez.NewHermez(c.address, ec)
if err != nil {
return nil, err
}
return hermez.UpdateFeeAddToken(auth, newFeeAddToken)
},
); err != nil {
return nil, fmt.Errorf("Failed update FeeAddToken: %w", err)
}
return tx, nil
}
// RollupConstants returns the Constants of the Rollup Smart Contract
@ -609,15 +651,17 @@ func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethC
var L1UserTx RollupEventL1UserTx
err := c.contractAbi.Unpack(&L1UserTxAux, "L1UserTxEvent", vLog.Data)
if err != nil {
fmt.Println(err)
return nil, nil, err
}
L1Tx, err := common.L1TxFromBytes(L1UserTxAux.L1Tx)
L1Tx, err := common.L1TxFromBytes(L1UserTxAux.L1UserTx)
if err != nil {
fmt.Println(err)
return nil, nil, err
}
L1UserTx.ToForgeL1TxsNum = new(big.Int).SetBytes(vLog.Topics[1][:]).Int64()
L1UserTx.Position = int(new(big.Int).SetBytes(vLog.Topics[2][:]).Int64())
L1UserTx.L1Tx = *L1Tx
L1UserTx.L1UserTx = *L1Tx
rollupEvents.L1UserTx = append(rollupEvents.L1UserTx, L1UserTx)
case logHermezAddToken:
var addToken RollupEventAddToken
@ -625,7 +669,7 @@ func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethC
if err != nil {
return nil, nil, err
}
addToken.Address = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
addToken.TokenAddress = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
rollupEvents.AddToken = append(rollupEvents.AddToken, addToken)
case logHermezForgeBatch:
var forgeBatch RollupEventForgeBatch
@ -648,12 +692,12 @@ func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethC
rollupEvents.UpdateFeeAddToken = append(rollupEvents.UpdateFeeAddToken, updateFeeAddToken)
case logHermezWithdrawEvent:
var withdraw RollupEventWithdrawEvent
err := c.contractAbi.Unpack(&withdraw, "WithdrawEvent", vLog.Data)
if err != nil {
return nil, nil, err
}
withdraw.Idx = new(big.Int).SetBytes(vLog.Topics[1][:]).Uint64()
withdraw.NumExitRoot = new(big.Int).SetBytes(vLog.Topics[2][:]).Uint64()
instantWithdraw := new(big.Int).SetBytes(vLog.Topics[3][:]).Uint64()
if instantWithdraw == 1 {
withdraw.InstantWithdraw = true
}
rollupEvents.WithdrawEvent = append(rollupEvents.WithdrawEvent, withdraw)
}
}
@ -698,7 +742,7 @@ func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupFo
if err != nil {
return nil, err
}
rollupForgeBatchArgs.L1CoordinatorTxs = append(rollupForgeBatchArgs.L1CoordinatorTxs, *l1Tx)
rollupForgeBatchArgs.L1CoordinatorTxs = append(rollupForgeBatchArgs.L1CoordinatorTxs, l1Tx)
rollupForgeBatchArgs.L1CoordinatorTxsAuths = append(rollupForgeBatchArgs.L1CoordinatorTxsAuths, signature)
}
rollupConsts, err := c.RollupConstants()
@ -713,7 +757,7 @@ func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupFo
if err != nil {
return nil, err
}
rollupForgeBatchArgs.L2TxsData = append(rollupForgeBatchArgs.L2TxsData, *l2Tx)
rollupForgeBatchArgs.L2TxsData = append(rollupForgeBatchArgs.L2TxsData, l2Tx)
}
lenFeeIdxCoordinatorBytes := int(nLevels / 8) //nolint:gomnd
numFeeIdxCoordinator := len(aux.FeeIdxCoordinator) / lenFeeIdxCoordinatorBytes

+ 683
- 5
eth/rollup_test.go

@ -1,12 +1,16 @@
package eth
import (
"crypto/ecdsa"
"encoding/binary"
"encoding/hex"
"math/big"
"testing"
ethCommon "github.com/ethereum/go-ethereum/common"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/hermeznetwork/hermez-node/common"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -21,6 +25,32 @@ var absoluteMaxL1L2BatchTimeout = int64(240)
var maxTx = int64(512)
var nLevels = int64(32)
var tokenIDERC777 uint32
var tokenIDERC20 uint32
type keys struct {
BJJSecretKey *babyjub.PrivateKey
BJJPublicKey *babyjub.PublicKey
Addr ethCommon.Address
}
func genKeysBjj(i int64) *keys {
i++ // i = 0 doesn't work for the ecdsa key generation
var sk babyjub.PrivateKey
binary.LittleEndian.PutUint64(sk[:], uint64(i))
// eth address
var key ecdsa.PrivateKey
key.D = big.NewInt(i) // only for testing
key.PublicKey.X, key.PublicKey.Y = ethCrypto.S256().ScalarBaseMult(key.D.Bytes())
key.Curve = ethCrypto.S256()
return &keys{
BJJSecretKey: &sk,
BJJPublicKey: sk.Public(),
}
}
func TestRollupConstants(t *testing.T) {
rollupConstants, err := rollupClient.RollupConstants()
require.Nil(t, err)
@ -34,11 +64,38 @@ func TestRollupConstants(t *testing.T) {
assert.Equal(t, wdelayerAddressConst, rollupConstants.WithdrawDelayerContract)
}
func TestRollupRegisterTokensCount(t *testing.T) {
registerTokensCount, err := rollupClient.RollupRegisterTokensCount()
require.Nil(t, err)
assert.Equal(t, big.NewInt(1), registerTokensCount)
}
func TestAddToken(t *testing.T) {
feeAddToken := big.NewInt(10)
// Addtoken
_, err := rollupClient.RollupAddToken(tokenERC777AddressConst, feeAddToken)
var feeAddToken = new(big.Int)
feeAddToken = big.NewInt(10)
// Addtoken ERC20
registerTokensCount, err := rollupClient.RollupRegisterTokensCount()
require.Nil(t, err)
_, err = rollupClient.RollupAddToken(tokenERC20AddressConst, feeAddToken)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, tokenERC20AddressConst, rollupEvents.AddToken[0].TokenAddress)
assert.Equal(t, registerTokensCount, common.TokenID(rollupEvents.AddToken[0].TokenID).BigInt())
tokenIDERC20 = rollupEvents.AddToken[0].TokenID
// Addtoken ERC777
registerTokensCount, err = rollupClient.RollupRegisterTokensCount()
require.Nil(t, err)
_, err = rollupClient.RollupAddToken(tokenERC777AddressConst, feeAddToken)
require.Nil(t, err)
currentBlockNum, _ = rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ = rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, tokenERC777AddressConst, rollupEvents.AddToken[0].TokenAddress)
assert.Equal(t, registerTokensCount, common.TokenID(rollupEvents.AddToken[0].TokenID).BigInt())
tokenIDERC777 = rollupEvents.AddToken[0].TokenID
}
func TestRollupForgeBatch(t *testing.T) {
@ -97,12 +154,15 @@ func TestRollupForgeBatch(t *testing.T) {
signature = append(signature, v)
L1Tx, err := common.L1TxFromCoordinatorBytes(bytesL1Coordinator)
require.Nil(t, err)
args.L1CoordinatorTxs = append(args.L1CoordinatorTxs, *L1Tx)
args.L1CoordinatorTxs = append(args.L1CoordinatorTxs, L1Tx)
args.L1CoordinatorTxsAuths = append(args.L1CoordinatorTxsAuths, signature)
}
newStateRoot := new(big.Int)
newStateRoot.SetString("18317824016047294649053625209337295956588174734569560016974612130063629505228", 10)
newExitRoot := big.NewInt(0)
newExitRoot := new(big.Int)
bytesNumExitRoot, err := hex.DecodeString("10a89d5fe8d488eda1ba371d633515739933c706c210c604f5bd209180daa43b")
require.Nil(t, err)
newExitRoot.SetBytes(bytesNumExitRoot)
args.NewLastIdx = int64(256)
args.NewStRoot = newStateRoot
args.NewExitRoot = newExitRoot
@ -140,3 +200,621 @@ func TestRollupForgeBatchArgs(t *testing.T) {
assert.Equal(t, argsForge.NewStRoot, args.NewStRoot)
assert.Equal(t, argsForge.VerifierIdx, args.VerifierIdx)
}
func TestRollupUpdateForgeL1L2BatchTimeout(t *testing.T) {
newForgeL1L2BatchTimeout := uint8(222)
_, err := rollupClient.RollupUpdateForgeL1L2BatchTimeout(newForgeL1L2BatchTimeout)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, newForgeL1L2BatchTimeout, rollupEvents.UpdateForgeL1L2BatchTimeout[0].NewForgeL1L2BatchTimeout)
}
func TestRollupUpdateFeeAddToken(t *testing.T) {
newFeeAddToken := big.NewInt(12)
_, err := rollupClient.RollupUpdateFeeAddToken(newFeeAddToken)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, newFeeAddToken, rollupEvents.UpdateFeeAddToken[0].NewFeeAddToken)
}
func TestRollupL1UserTxETHCreateAccountDeposit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(2)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
tokenIDUint32 := uint32(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDUint32),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC20CreateAccountDeposit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(1)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC20),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC777CreateAccountDeposit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(3)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC777),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxETHDeposit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(2)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
tokenIDUint32 := uint32(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDUint32),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC20Deposit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(1)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC20),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC777Deposit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(3)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC777),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxETHDepositTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(2)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
tokenIDUint32 := uint32(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDUint32),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC20DepositTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(1)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC20),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC777DepositTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(3)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC777),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxETHCreateAccountDepositTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(2)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
tokenIDUint32 := uint32(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDUint32),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC20CreateAccountDepositTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(1)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC20),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC777CreateAccountDepositTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(3)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC777),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxETHForceTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(2)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
tokenIDUint32 := uint32(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDUint32),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC20ForceTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(1)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC20),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC777ForceTransfer(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(3)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC777),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxETHForceExit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(2)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
tokenIDUint32 := uint32(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDUint32),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDUint32, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC20ForceExit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(1)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC20),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC20ETH(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC20, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupL1UserTxERC777ForceExit(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
key := genKeysBjj(3)
fromIdxInt64 := int64(0)
toIdxInt64 := int64(0)
fromIdx := new(common.Idx)
*fromIdx = 0
l1Tx := common.L1Tx{
FromBJJ: key.BJJPublicKey,
FromIdx: common.Idx(fromIdxInt64),
ToIdx: common.Idx(toIdxInt64),
LoadAmount: big.NewInt(10),
TokenID: common.TokenID(tokenIDERC777),
Amount: big.NewInt(0),
}
_, err = rollupClientAux.RollupL1UserTxERC777(l1Tx.FromBJJ, fromIdxInt64, l1Tx.LoadAmount, l1Tx.Amount, tokenIDERC777, toIdxInt64)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, l1Tx.FromBJJ, rollupEvents.L1UserTx[0].L1UserTx.FromBJJ)
assert.Equal(t, l1Tx.ToIdx, rollupEvents.L1UserTx[0].L1UserTx.ToIdx)
assert.Equal(t, l1Tx.LoadAmount, rollupEvents.L1UserTx[0].L1UserTx.LoadAmount)
assert.Equal(t, l1Tx.TokenID, rollupEvents.L1UserTx[0].L1UserTx.TokenID)
assert.Equal(t, l1Tx.Amount, rollupEvents.L1UserTx[0].L1UserTx.Amount)
assert.Equal(t, rollupClientAux.client.account.Address, rollupEvents.L1UserTx[0].L1UserTx.FromEthAddr)
}
func TestRollupForgeBatch2(t *testing.T) {
// Forge
args := new(RollupForgeBatchArgs)
feeIdxCoordinatorBytes, err := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
require.Nil(t, err)
lenFeeIdxCoordinatorBytes := int(4)
numFeeIdxCoordinator := len(feeIdxCoordinatorBytes) / lenFeeIdxCoordinatorBytes
for i := 0; i < numFeeIdxCoordinator; i++ {
var paddedFeeIdx [6]byte
if lenFeeIdxCoordinatorBytes < common.IdxBytesLen {
copy(paddedFeeIdx[6-lenFeeIdxCoordinatorBytes:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
} else {
copy(paddedFeeIdx[:], feeIdxCoordinatorBytes[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
}
FeeIdxCoordinator, err := common.IdxFromBytes(paddedFeeIdx[:])
require.Nil(t, err)
args.FeeIdxCoordinator = append(args.FeeIdxCoordinator, FeeIdxCoordinator)
}
newStateRoot := new(big.Int)
newStateRoot.SetString("0", 10)
newExitRoot := new(big.Int)
newExitRoot.SetString("6442511778188868333499919207091562876207840300369859025739972956758642594045", 10)
args.NewLastIdx = int64(1000)
args.NewStRoot = newStateRoot
args.NewExitRoot = newExitRoot
args.L1Batch = true
args.VerifierIdx = 0
args.ProofA[0] = big.NewInt(0)
args.ProofA[1] = big.NewInt(0)
args.ProofB[0] = [2]*big.Int{big.NewInt(0), big.NewInt(0)}
args.ProofB[1] = [2]*big.Int{big.NewInt(0), big.NewInt(0)}
args.ProofC[0] = big.NewInt(0)
args.ProofC[1] = big.NewInt(0)
argsForge = args
_, err = rollupClient.RollupForgeBatch(argsForge)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, int64(2), rollupEvents.ForgeBatch[0].BatchNum)
ethHashForge = rollupEvents.ForgeBatch[0].EthTxHash
}
func TestRollupWithdrawMerkleProof(t *testing.T) {
rollupClientAux, err := NewRollupClient(ethereumClientAux, hermezRollupAddressConst, tokenHEZAddressConst)
require.Nil(t, err)
var pkComp babyjub.PublicKeyComp
pkCompB, err := hex.DecodeString("9a0c13552c3a0b7b2b63ec4ab8a906b4af471ef3aa4463491ff08a0b489ac50f")
require.Nil(t, err)
pkCompL := common.SwapEndianness(pkCompB)
err = pkComp.UnmarshalText([]byte(hex.EncodeToString(pkCompL)))
require.Nil(t, err)
pk, err := pkComp.Decompress()
require.Nil(t, err)
require.Nil(t, err)
tokenID := uint32(1)
numExitRoot := int64(2)
fromIdx := int64(256)
amount := big.NewInt(10)
siblingBytes0, _ := new(big.Int).SetString("19508838618377323910556678335932426220272947530531646682154552299216398748115", 10)
require.Nil(t, err)
siblingBytes1, _ := new(big.Int).SetString("15198806719713909654457742294233381653226080862567104272457668857208564789571", 10)
require.Nil(t, err)
var siblings []*big.Int
siblings = append(siblings, siblingBytes0)
siblings = append(siblings, siblingBytes1)
instantWithdraw := true
_, err = rollupClientAux.RollupWithdrawMerkleProof(pk, tokenID, numExitRoot, fromIdx, amount, siblings, instantWithdraw)
require.Nil(t, err)
currentBlockNum, _ := rollupClient.client.EthCurrentBlock()
rollupEvents, _, _ := rollupClient.RollupEventsByBlock(currentBlockNum)
assert.Equal(t, uint64(fromIdx), rollupEvents.WithdrawEvent[0].Idx)
assert.Equal(t, instantWithdraw, rollupEvents.WithdrawEvent[0].InstantWithdraw)
assert.Equal(t, uint64(numExitRoot), rollupEvents.WithdrawEvent[0].NumExitRoot)
}

Loading…
Cancel
Save