Implement initial version of test.Client

This commit is contained in:
Eduard S
2020-09-07 18:09:52 +02:00
parent 56604ecc69
commit bcd93fee36
9 changed files with 617 additions and 102 deletions

View File

@@ -171,6 +171,26 @@ type AuctionEvents struct { //nolint:structcheck
HEZClaimed []AuctionEventHEZClaimed
}
// NewAuctionEvents creates an empty AuctionEvents with the slices initialized.
func NewAuctionEvents() AuctionEvents {
return AuctionEvents{
NewBid: make([]AuctionEventNewBid, 0),
NewSlotDeadline: make([]AuctionEventNewSlotDeadline, 0),
NewClosedAuctionSlots: make([]AuctionEventNewClosedAuctionSlots, 0),
NewOutbidding: make([]AuctionEventNewOutbidding, 0),
NewDonationAddress: make([]AuctionEventNewDonationAddress, 0),
NewBootCoordinator: make([]AuctionEventNewBootCoordinator, 0),
NewOpenAuctionSlots: make([]AuctionEventNewOpenAuctionSlots, 0),
NewAllocationRatio: make([]AuctionEventNewAllocationRatio, 0),
NewCoordinator: make([]AuctionEventNewCoordinator, 0),
CoordinatorUpdated: make([]AuctionEventCoordinatorUpdated, 0),
NewForgeAllocated: make([]AuctionEventNewForgeAllocated, 0),
NewMinBidEpoch: make([]AuctionEventNewMinBidEpoch, 0),
NewForge: make([]AuctionEventNewForge, 0),
HEZClaimed: make([]AuctionEventHEZClaimed, 0),
}
}
// AuctionInterface is the inteface to to Auction Smart Contract
type AuctionInterface interface {
//

View File

@@ -18,9 +18,9 @@ import (
// EthereumInterface is the interface to Ethereum
type EthereumInterface interface {
EthCurrentBlock() (*big.Int, error)
EthHeaderByNumber(context.Context, *big.Int) (*types.Header, error)
EthBlockByNumber(context.Context, *big.Int) (*common.Block, error)
EthCurrentBlock() (int64, error)
// EthHeaderByNumber(context.Context, *big.Int) (*types.Header, error)
EthBlockByNumber(context.Context, int64) (*common.Block, error)
}
var (
@@ -214,29 +214,33 @@ func (c *EthereumClient) waitReceipt(ctx context.Context, tx *types.Transaction,
}
// EthCurrentBlock returns the current block number in the blockchain
func (c *EthereumClient) EthCurrentBlock() (*big.Int, error) {
func (c *EthereumClient) EthCurrentBlock() (int64, error) {
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)
defer cancel()
header, err := c.client.HeaderByNumber(ctx, nil)
if err != nil {
return nil, err
return 0, err
}
return header.Number, nil
return header.Number.Int64(), nil
}
// EthHeaderByNumber internally calls ethclient.Client HeaderByNumber
func (c *EthereumClient) EthHeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
return c.client.HeaderByNumber(ctx, number)
}
// func (c *EthereumClient) EthHeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
// return c.client.HeaderByNumber(ctx, number)
// }
// EthBlockByNumber internally calls ethclient.Client BlockByNumber and returns *common.Block
func (c *EthereumClient) EthBlockByNumber(ctx context.Context, number *big.Int) (*common.Block, error) {
block, err := c.client.BlockByNumber(ctx, number)
func (c *EthereumClient) EthBlockByNumber(ctx context.Context, number int64) (*common.Block, error) {
blockNum := big.NewInt(number)
if number == 0 {
blockNum = nil
}
block, err := c.client.BlockByNumber(ctx, blockNum)
if err != nil {
return nil, err
}
b := &common.Block{
EthBlockNum: block.Number().Uint64(),
EthBlockNum: block.Number().Int64(),
Timestamp: time.Unix(int64(block.Time()), 0),
Hash: block.Hash(),
}

View File

@@ -5,10 +5,19 @@ import (
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/hermeznetwork/hermez-node/common"
"github.com/hermeznetwork/hermez-node/utils"
"github.com/iden3/go-iden3-crypto/babyjub"
)
const (
// FeeIdxCoordinatorLen 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.
FeeIdxCoordinatorLen = 64
)
// RollupConstants are the constants of the Rollup Smart Contract
type RollupConstants struct {
// Maxim Deposit allowed
@@ -41,21 +50,28 @@ type RollupVariables struct {
// QueueStruct is the queue of L1Txs for a batch
//nolint:structcheck
type QueueStruct struct {
L1TxQueue [][]byte
CurrentIndex int64
L1TxQueue []common.L1Tx
TotalL1TxFee *big.Int
}
// NewQueueStruct creates a new clear QueueStruct.
func NewQueueStruct() *QueueStruct {
return &QueueStruct{
L1TxQueue: make([]common.L1Tx, 0),
TotalL1TxFee: big.NewInt(0),
}
}
// RollupState represents the state of the Rollup in the Smart Contract
//nolint:structcheck,unused
type RollupState struct {
StateRoot *big.Int
ExitRoots []*big.Int
ExiNullifierMap map[[256 / 8]byte]bool
ExitNullifierMap map[[256 / 8]byte]bool
TokenList []ethCommon.Address
TokenMap map[ethCommon.Address]bool
mapL1TxQueue map[int64]QueueStruct
LastLTxBatch int64
MapL1TxQueue map[int64]*QueueStruct
LastL1L2Batch int64
CurrentToForgeL1TxsNum int64
LastToForgeL1TxsNum int64
CurrentIdx int64
@@ -63,9 +79,7 @@ type RollupState struct {
// RollupEventL1UserTx is an event of the Rollup Smart Contract
type RollupEventL1UserTx struct {
L1UserTx []byte
ToForgeL1TxsNum int64
Position int
L1Tx common.L1Tx
}
// RollupEventAddToken is an event of the Rollup Smart Contract
@@ -76,7 +90,8 @@ type RollupEventAddToken struct {
// RollupEventForgeBatch is an event of the Rollup Smart Contract
type RollupEventForgeBatch struct {
BatchNum int64
BatchNum int64
EthTxHash ethCommon.Hash
}
// RollupEventUpdateForgeL1Timeout is an event of the Rollup Smart Contract
@@ -117,21 +132,35 @@ type RollupEvents struct { //nolint:structcheck
Withdraw []RollupEventWithdraw
}
// NewRollupEvents creates an empty RollupEvents with the slices initialized.
func NewRollupEvents() RollupEvents {
return RollupEvents{
L1UserTx: make([]RollupEventL1UserTx, 0),
AddToken: make([]RollupEventAddToken, 0),
ForgeBatch: make([]RollupEventForgeBatch, 0),
UpdateForgeL1Timeout: make([]RollupEventUpdateForgeL1Timeout, 0),
UpdateFeeL1UserTx: make([]RollupEventUpdateFeeL1UserTx, 0),
UpdateFeeAddToken: make([]RollupEventUpdateFeeAddToken, 0),
UpdateTokenHez: make([]RollupEventUpdateTokenHez, 0),
Withdraw: make([]RollupEventWithdraw, 0),
}
}
// RollupForgeBatchArgs are the arguments to the ForgeBatch function in the Rollup Smart Contract
//nolint:structcheck,unused
type RollupForgeBatchArgs struct {
proofA [2]*big.Int
proofB [2][2]*big.Int
proofC [2]*big.Int
newLastIdx int64
newStRoot *big.Int
newExitRoot *big.Int
// TODO: Replace compressedL1CoordinatorTx, l2TxsData, feeIdxCoordinator for vectors
compressedL1CoordinatorTx []byte
l2TxsData []byte
feeIdxCoordinator []byte
verifierIdx int64
l1Batch bool
ProofA [2]*big.Int
ProofB [2][2]*big.Int
ProofC [2]*big.Int
NewLastIdx int64
NewStRoot *big.Int
NewExitRoot *big.Int
L1CoordinatorTxs []*common.L1Tx
L2Txs []*common.L2Tx
FeeIdxCoordinator []common.Idx
// Circuit selector
VerifierIdx int64
L1Batch bool
}
// RollupInterface is the inteface to to Rollup Smart Contract
@@ -176,7 +205,7 @@ type RollupInterface interface {
RollupConstants() (*RollupConstants, error)
RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error)
RollupForgeBatchArgs(*types.Transaction) (*RollupForgeBatchArgs, error)
RollupForgeBatchArgs(ethCommon.Hash) (*RollupForgeBatchArgs, error)
}
//
@@ -293,6 +322,11 @@ func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethC
}
// RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
func (c *RollupClient) RollupForgeBatchArgs(transaction *types.Transaction) (*RollupForgeBatchArgs, error) {
func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupForgeBatchArgs, error) {
// tx := client.TransactionByHash(ethTxHash) -> types.Transaction
// txData := types.Transaction -> Data()
// m := abi.MethodById(txData) -> Method
// m.Inputs.Unpack(txData) -> Args
// client.TransactionReceipt()?
return nil, errTODO
}