Browse Source

Update ethclient contracts & rollup iteration 1

feature/sql-semaphore1
laisolizq 4 years ago
parent
commit
202b98b3e6
13 changed files with 1415 additions and 1759 deletions
  1. +4
    -3
      common/account.go
  2. +76
    -139
      eth/auction.go
  3. +87
    -115
      eth/auction_test.go
  4. +3
    -2
      eth/contracts/README.md
  5. +289
    -424
      eth/contracts/auction/HermezAuctionProtocol.go
  6. +1
    -1
      eth/contracts/erc777/ERC777.go
  7. +442
    -861
      eth/contracts/hermez/Hermez.go
  8. +67
    -45
      eth/contracts/withdrawdelayer/WithdrawalDelayer.go
  9. +22
    -9
      eth/main_test.go
  10. +317
    -88
      eth/rollup.go
  11. +16
    -4
      eth/rollup_test.go
  12. +27
    -22
      eth/wdelayer.go
  13. +64
    -46
      eth/wdelayer_test.go

+ 4
- 3
common/account.go

@ -21,7 +21,8 @@ const (
// maxBalanceBytes is the maximum bytes that can use the Account.Balance *big.Int
maxBalanceBytes = 24
idxBytesLen = 6
// IdxBytesLen idx bytes
IdxBytesLen = 6
// maxIdxValue is the maximum value that Idx can have (48 bits: maxIdxValue=2**48-1)
maxIdxValue = 0xffffffffffff
@ -66,8 +67,8 @@ func (idx Idx) BigInt() *big.Int {
// IdxFromBytes returns Idx from a byte array
func IdxFromBytes(b []byte) (Idx, error) {
if len(b) != idxBytesLen {
return 0, fmt.Errorf("can not parse Idx, bytes len %d, expected %d", len(b), idxBytesLen)
if len(b) != IdxBytesLen {
return 0, fmt.Errorf("can not parse Idx, bytes len %d, expected %d", len(b), IdxBytesLen)
}
var idxBytes [8]byte
copy(idxBytes[2:], b[:])

+ 76
- 139
eth/auction.go

@ -27,37 +27,37 @@ type AuctionConstants struct {
InitialMinimalBidding *big.Int
// First block where the first slot begins
GenesisBlockNum int64
// Hermez Governanze Token smartcontract address who controls some parameters and collects HEZ fee
// Only for test
GovernanceAddress ethCommon.Address
// ERC777 token with which the bids will be made
TokenHEZ ethCommon.Address
// HermezRollup smartcontract address
HermezRollup ethCommon.Address
// Hermez Governanze Token smartcontract address who controls some parameters and collects HEZ fee
// Only for test
GovernanceAddress ethCommon.Address
}
// SlotState is the state of a slot
type SlotState struct {
Forger ethCommon.Address
Bidder ethCommon.Address
Fulfilled bool
BidAmount *big.Int
ClosedMinBid *big.Int
Fulfilled bool
}
// NewSlotState returns an empty SlotState
func NewSlotState() *SlotState {
return &SlotState{
Forger: ethCommon.Address{},
Bidder: ethCommon.Address{},
Fulfilled: false,
BidAmount: big.NewInt(0),
ClosedMinBid: big.NewInt(0),
Fulfilled: false,
}
}
// Coordinator is the details of the Coordinator identified by the forger address
type Coordinator struct {
WithdrawalAddress ethCommon.Address
URL string
Forger ethCommon.Address
URL string
}
// AuctionVariables are the variables of the Auction Smart Contract
@ -92,9 +92,9 @@ type AuctionState struct {
// AuctionEventNewBid is an event of the Auction Smart Contract
type AuctionEventNewBid struct {
Slot int64
BidAmount *big.Int
CoordinatorForger ethCommon.Address
Slot int64
BidAmount *big.Int
Bidder ethCommon.Address
}
// AuctionEventNewSlotDeadline is an event of the Auction Smart Contract
@ -132,24 +132,18 @@ type AuctionEventNewAllocationRatio struct {
NewAllocationRatio [3]uint16
}
// AuctionEventNewCoordinator is an event of the Auction Smart Contract
type AuctionEventNewCoordinator struct {
ForgerAddress ethCommon.Address
WithdrawalAddress ethCommon.Address
CoordinatorURL string
}
// AuctionEventCoordinatorUpdated is an event of the Auction Smart Contract
type AuctionEventCoordinatorUpdated struct {
ForgerAddress ethCommon.Address
WithdrawalAddress ethCommon.Address
CoordinatorURL string
// AuctionEventSetCoordinator is an event of the Auction Smart Contract
type AuctionEventSetCoordinator struct {
BidderAddress ethCommon.Address
ForgerAddress ethCommon.Address
CoordinatorURL string
}
// AuctionEventNewForgeAllocated is an event of the Auction Smart Contract
type AuctionEventNewForgeAllocated struct {
Bidder ethCommon.Address
Forger ethCommon.Address
CurrentSlot int64
SlotToForge int64
BurnAmount *big.Int
DonationAmount *big.Int
GovernanceAmount *big.Int
@ -164,7 +158,7 @@ type AuctionEventNewDefaultSlotSetBid struct {
// AuctionEventNewForge is an event of the Auction Smart Contract
type AuctionEventNewForge struct {
Forger ethCommon.Address
CurrentSlot int64
SlotToForge int64
}
// AuctionEventHEZClaimed is an event of the Auction Smart Contract
@ -183,8 +177,7 @@ type AuctionEvents struct { //nolint:structcheck
NewBootCoordinator []AuctionEventNewBootCoordinator
NewOpenAuctionSlots []AuctionEventNewOpenAuctionSlots
NewAllocationRatio []AuctionEventNewAllocationRatio
NewCoordinator []AuctionEventNewCoordinator
CoordinatorUpdated []AuctionEventCoordinatorUpdated
SetCoordinator []AuctionEventSetCoordinator
NewForgeAllocated []AuctionEventNewForgeAllocated
NewDefaultSlotSetBid []AuctionEventNewDefaultSlotSetBid
NewForge []AuctionEventNewForge
@ -202,8 +195,7 @@ func NewAuctionEvents() AuctionEvents {
NewBootCoordinator: make([]AuctionEventNewBootCoordinator, 0),
NewOpenAuctionSlots: make([]AuctionEventNewOpenAuctionSlots, 0),
NewAllocationRatio: make([]AuctionEventNewAllocationRatio, 0),
NewCoordinator: make([]AuctionEventNewCoordinator, 0),
CoordinatorUpdated: make([]AuctionEventCoordinatorUpdated, 0),
SetCoordinator: make([]AuctionEventSetCoordinator, 0),
NewForgeAllocated: make([]AuctionEventNewForgeAllocated, 0),
NewDefaultSlotSetBid: make([]AuctionEventNewDefaultSlotSetBid, 0),
NewForge: make([]AuctionEventNewForge, 0),
@ -235,9 +227,7 @@ type AuctionInterface interface {
AuctionChangeDefaultSlotSetBid(slotSet int64, newInitialMinBid *big.Int) (*types.Transaction, error)
// Coordinator Management
AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*types.Transaction, error)
AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error)
AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error)
AuctionSetCoordinator(forger ethCommon.Address, coordinatorURL string) (*types.Transaction, error)
// Slot Info
AuctionGetSlotNumber(blockNum int64) (int64, error)
@ -247,17 +237,16 @@ type AuctionInterface interface {
AuctionGetSlotSet(slot int64) (*big.Int, error)
// Bidding
// AuctionTokensReceived(operator, from, to ethCommon.Address, amount *big.Int,
// userData, operatorData []byte) error // Only called from another smart contract
AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error)
AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error)
AuctionBid(slot int64, bidAmount *big.Int) (*types.Transaction, error)
AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int) (*types.Transaction, error)
// Forge
AuctionCanForge(forger ethCommon.Address, blockNum int64) (bool, error)
// AuctionForge(forger ethCommon.Address) (bool, error) // Only called from another smart contract
AuctionForge(forger ethCommon.Address) (*types.Transaction, error)
// Fees
AuctionClaimHEZ(claimAddress ethCommon.Address) (*types.Transaction, error)
AuctionClaimHEZ() (*types.Transaction, error)
AuctionGetClaimableHEZ(bidder ethCommon.Address) (*big.Int, error)
//
// Smart Contract Status
@ -576,43 +565,8 @@ func (c *AuctionClient) AuctionGetClaimableHEZ(claimAddress ethCommon.Address) (
return claimableHEZ, nil
}
// AuctionRegisterCoordinator is the interface to call the smart contract function
func (c *AuctionClient) AuctionRegisterCoordinator(forgerAddress ethCommon.Address, URL string) (*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) {
auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
if err != nil {
return nil, err
}
return auction.RegisterCoordinator(auth, forgerAddress, URL)
},
); err != nil {
return nil, fmt.Errorf("Failed register coordinator: %w", err)
}
return tx, nil
}
// AuctionIsRegisteredCoordinator is the interface to call the smart contract function
func (c *AuctionClient) AuctionIsRegisteredCoordinator(forgerAddress ethCommon.Address) (bool, error) {
var registered bool
if err := c.client.Call(func(ec *ethclient.Client) error {
auction, err := HermezAuctionProtocol.NewHermezAuctionProtocol(c.address, ec)
if err != nil {
return err
}
registered, err = auction.IsRegisteredCoordinator(nil, forgerAddress)
return err
}); err != nil {
return false, err
}
return registered, nil
}
// AuctionUpdateCoordinatorInfo is the interface to call the smart contract function
func (c *AuctionClient) AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Address, newWithdrawAddress ethCommon.Address, newURL string) (*types.Transaction, error) {
// AuctionSetCoordinator is the interface to call the smart contract function
func (c *AuctionClient) AuctionSetCoordinator(forger ethCommon.Address, coordinatorURL string) (*types.Transaction, error) {
var tx *types.Transaction
var err error
if tx, err = c.client.CallAuth(
@ -622,10 +576,10 @@ func (c *AuctionClient) AuctionUpdateCoordinatorInfo(forgerAddress ethCommon.Add
if err != nil {
return nil, err
}
return auction.UpdateCoordinatorInfo(auth, forgerAddress, newWithdrawAddress, newURL)
return auction.SetCoordinator(auth, forger, coordinatorURL)
},
); err != nil {
return nil, fmt.Errorf("Failed update coordinator info: %w", err)
return nil, fmt.Errorf("Failed set coordinator: %w", err)
}
return tx, nil
}
@ -715,7 +669,7 @@ func (c *AuctionClient) AuctionGetSlotNumber(blockNum int64) (int64, error) {
}
// AuctionBid is the interface to call the smart contract function
func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int) (*types.Transaction, error) {
var tx *types.Transaction
var err error
if tx, err = c.client.CallAuth(
@ -725,7 +679,7 @@ func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int, forger ethCom
if err != nil {
return nil, err
}
bidFnSignature := []byte("bid(uint128,uint128,address)")
bidFnSignature := []byte("bid(uint128,uint128)")
hash := sha3.NewLegacyKeccak256()
_, err = hash.Write(bidFnSignature)
if err != nil {
@ -736,12 +690,10 @@ func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int, forger ethCom
binary.BigEndian.PutUint64(slotBytes, uint64(slot))
paddedSlot := ethCommon.LeftPadBytes(slotBytes, 32)
paddedAmount := ethCommon.LeftPadBytes(bidAmount.Bytes(), 32)
paddedAddress := ethCommon.LeftPadBytes(forger.Bytes(), 32)
var userData []byte
userData = append(userData, methodID...)
userData = append(userData, paddedSlot...)
userData = append(userData, paddedAmount...)
userData = append(userData, paddedAddress...)
return tokens.Send(auth, c.address, bidAmount, userData)
},
); err != nil {
@ -751,7 +703,7 @@ func (c *AuctionClient) AuctionBid(slot int64, bidAmount *big.Int, forger ethCom
}
// AuctionMultiBid is the interface to call the smart contract function
func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int, forger ethCommon.Address) (*types.Transaction, error) {
func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, slotSet [6]bool, maxBid, closedMinBid, budget *big.Int) (*types.Transaction, error) {
var tx *types.Transaction
var err error
if tx, err = c.client.CallAuth(
@ -761,7 +713,7 @@ func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, sl
if err != nil {
return nil, err
}
multiBidFnSignature := []byte("multiBid(uint128,uint128,bool[6],uint128,uint128,address)")
multiBidFnSignature := []byte("multiBid(uint128,uint128,bool[6],uint128,uint128)")
hash := sha3.NewLegacyKeccak256()
_, err = hash.Write(multiBidFnSignature)
if err != nil {
@ -776,7 +728,6 @@ func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, sl
paddedEndingSlot := ethCommon.LeftPadBytes(endingSlotBytes, 32)
paddedMinBid := ethCommon.LeftPadBytes(closedMinBid.Bytes(), 32)
paddedMaxBid := ethCommon.LeftPadBytes(maxBid.Bytes(), 32)
paddedAddress := ethCommon.LeftPadBytes(forger.Bytes(), 32)
var userData []byte
userData = append(userData, methodID...)
userData = append(userData, paddedStartingSlot...)
@ -792,7 +743,6 @@ func (c *AuctionClient) AuctionMultiBid(startingSlot int64, endingSlot int64, sl
}
userData = append(userData, paddedMaxBid...)
userData = append(userData, paddedMinBid...)
userData = append(userData, paddedAddress...)
return tokens.Send(auth, c.address, budget, userData)
},
); err != nil {
@ -817,13 +767,8 @@ func (c *AuctionClient) AuctionCanForge(forger ethCommon.Address, blockNum int64
return canForge, nil
}
// AuctionForge is the interface to call the smart contract function
// func (c *AuctionClient) AuctionForge(forger ethCommon.Address) (bool, error) {
// return false, errTODO
// }
// AuctionClaimHEZ is the interface to call the smart contract function
func (c *AuctionClient) AuctionClaimHEZ(claimAddress ethCommon.Address) (*types.Transaction, error) {
func (c *AuctionClient) AuctionClaimHEZ() (*types.Transaction, error) {
var tx *types.Transaction
var err error
if tx, err = c.client.CallAuth(
@ -833,7 +778,7 @@ func (c *AuctionClient) AuctionClaimHEZ(claimAddress ethCommon.Address) (*types.
if err != nil {
return nil, err
}
return auction.ClaimHEZ(auth, claimAddress)
return auction.ClaimHEZ(auth)
},
); err != nil {
return nil, fmt.Errorf("Failed claim HEZ: %w", err)
@ -942,20 +887,19 @@ func (c *AuctionClient) AuctionVariables() (*AuctionVariables, error) {
}
var (
logNewBid = crypto.Keccak256Hash([]byte("NewBid(uint128,uint128,address)"))
logNewSlotDeadline = crypto.Keccak256Hash([]byte("NewSlotDeadline(uint8)"))
logNewClosedAuctionSlots = crypto.Keccak256Hash([]byte("NewClosedAuctionSlots(uint16)"))
logNewOutbidding = crypto.Keccak256Hash([]byte("NewOutbidding(uint16)"))
logNewDonationAddress = crypto.Keccak256Hash([]byte("NewDonationAddress(address)"))
logNewBootCoordinator = crypto.Keccak256Hash([]byte("NewBootCoordinator(address)"))
logNewOpenAuctionSlots = crypto.Keccak256Hash([]byte("NewOpenAuctionSlots(uint16)"))
logNewAllocationRatio = crypto.Keccak256Hash([]byte("NewAllocationRatio(uint16[3])"))
logNewCoordinator = crypto.Keccak256Hash([]byte("NewCoordinator(address,address,string)"))
logCoordinatorUpdated = crypto.Keccak256Hash([]byte("CoordinatorUpdated(address,address,string)"))
logNewForgeAllocated = crypto.Keccak256Hash([]byte("NewForgeAllocated(address,uint128,uint128,uint128,uint128)"))
logNewDefaultSlotSetBid = crypto.Keccak256Hash([]byte("NewDefaultSlotSetBid(uint128,uint128)"))
logNewForge = crypto.Keccak256Hash([]byte("NewForge(address,uint128)"))
logHEZClaimed = crypto.Keccak256Hash([]byte("HEZClaimed(address,uint128)"))
logAuctionNewBid = crypto.Keccak256Hash([]byte("NewBid(uint128,uint128,address)"))
logAuctionNewSlotDeadline = crypto.Keccak256Hash([]byte("NewSlotDeadline(uint8)"))
logAuctionNewClosedAuctionSlots = crypto.Keccak256Hash([]byte("NewClosedAuctionSlots(uint16)"))
logAuctionNewOutbidding = crypto.Keccak256Hash([]byte("NewOutbidding(uint16)"))
logAuctionNewDonationAddress = crypto.Keccak256Hash([]byte("NewDonationAddress(address)"))
logAuctionNewBootCoordinator = crypto.Keccak256Hash([]byte("NewBootCoordinator(address)"))
logAuctionNewOpenAuctionSlots = crypto.Keccak256Hash([]byte("NewOpenAuctionSlots(uint16)"))
logAuctionNewAllocationRatio = crypto.Keccak256Hash([]byte("NewAllocationRatio(uint16[3])"))
logAuctionSetCoordinator = crypto.Keccak256Hash([]byte("SetCoordinator(address,address,string)"))
logAuctionNewForgeAllocated = crypto.Keccak256Hash([]byte("NewForgeAllocated(address,address,uint128,uint128,uint128,uint128)"))
logAuctionNewDefaultSlotSetBid = crypto.Keccak256Hash([]byte("NewDefaultSlotSetBid(uint128,uint128)"))
logAuctionNewForge = crypto.Keccak256Hash([]byte("NewForge(address,uint128)"))
logAuctionHEZClaimed = crypto.Keccak256Hash([]byte("HEZClaimed(address,uint128)"))
)
// AuctionEventsByBlock returns the events in a block that happened in the
@ -986,7 +930,7 @@ func (c *AuctionClient) AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *e
return nil, nil, ErrBlockHashMismatchEvent
}
switch vLog.Topics[0] {
case logNewBid:
case logAuctionNewBid:
var auxNewBid struct {
Slot *big.Int
BidAmount *big.Int
@ -998,71 +942,64 @@ func (c *AuctionClient) AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *e
}
newBid.BidAmount = auxNewBid.BidAmount
newBid.Slot = new(big.Int).SetBytes(vLog.Topics[1][:]).Int64()
newBid.CoordinatorForger = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
newBid.Bidder = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
auctionEvents.NewBid = append(auctionEvents.NewBid, newBid)
case logNewSlotDeadline:
case logAuctionNewSlotDeadline:
var newSlotDeadline AuctionEventNewSlotDeadline
if err := c.contractAbi.Unpack(&newSlotDeadline, "NewSlotDeadline", vLog.Data); err != nil {
return nil, nil, err
}
auctionEvents.NewSlotDeadline = append(auctionEvents.NewSlotDeadline, newSlotDeadline)
case logNewClosedAuctionSlots:
case logAuctionNewClosedAuctionSlots:
var newClosedAuctionSlots AuctionEventNewClosedAuctionSlots
if err := c.contractAbi.Unpack(&newClosedAuctionSlots, "NewClosedAuctionSlots", vLog.Data); err != nil {
return nil, nil, err
}
auctionEvents.NewClosedAuctionSlots = append(auctionEvents.NewClosedAuctionSlots, newClosedAuctionSlots)
case logNewOutbidding:
case logAuctionNewOutbidding:
var newOutbidding AuctionEventNewOutbidding
if err := c.contractAbi.Unpack(&newOutbidding, "NewOutbidding", vLog.Data); err != nil {
return nil, nil, err
}
auctionEvents.NewOutbidding = append(auctionEvents.NewOutbidding, newOutbidding)
case logNewDonationAddress:
case logAuctionNewDonationAddress:
var newDonationAddress AuctionEventNewDonationAddress
if err := c.contractAbi.Unpack(&newDonationAddress, "NewDonationAddress", vLog.Data); err != nil {
return nil, nil, err
}
newDonationAddress.NewDonationAddress = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
auctionEvents.NewDonationAddress = append(auctionEvents.NewDonationAddress, newDonationAddress)
case logNewBootCoordinator:
case logAuctionNewBootCoordinator:
var newBootCoordinator AuctionEventNewBootCoordinator
if err := c.contractAbi.Unpack(&newBootCoordinator, "NewBootCoordinator", vLog.Data); err != nil {
return nil, nil, err
}
newBootCoordinator.NewBootCoordinator = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
auctionEvents.NewBootCoordinator = append(auctionEvents.NewBootCoordinator, newBootCoordinator)
case logNewOpenAuctionSlots:
case logAuctionNewOpenAuctionSlots:
var newOpenAuctionSlots AuctionEventNewOpenAuctionSlots
if err := c.contractAbi.Unpack(&newOpenAuctionSlots, "NewOpenAuctionSlots", vLog.Data); err != nil {
return nil, nil, err
}
auctionEvents.NewOpenAuctionSlots = append(auctionEvents.NewOpenAuctionSlots, newOpenAuctionSlots)
case logNewAllocationRatio:
case logAuctionNewAllocationRatio:
var newAllocationRatio AuctionEventNewAllocationRatio
if err := c.contractAbi.Unpack(&newAllocationRatio, "NewAllocationRatio", vLog.Data); err != nil {
return nil, nil, err
}
auctionEvents.NewAllocationRatio = append(auctionEvents.NewAllocationRatio, newAllocationRatio)
case logNewCoordinator:
var newCoordinator AuctionEventNewCoordinator
if err := c.contractAbi.Unpack(&newCoordinator, "NewCoordinator", vLog.Data); err != nil {
return nil, nil, err
}
auctionEvents.NewCoordinator = append(auctionEvents.NewCoordinator, newCoordinator)
case logCoordinatorUpdated:
var coordinatorUpdated AuctionEventCoordinatorUpdated
if err := c.contractAbi.Unpack(&coordinatorUpdated, "CoordinatorUpdated", vLog.Data); err != nil {
case logAuctionSetCoordinator:
var setCoordinator AuctionEventSetCoordinator
if err := c.contractAbi.Unpack(&setCoordinator, "SetCoordinator", vLog.Data); err != nil {
return nil, nil, err
}
auctionEvents.CoordinatorUpdated = append(auctionEvents.CoordinatorUpdated, coordinatorUpdated)
case logNewForgeAllocated:
setCoordinator.BidderAddress = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
setCoordinator.ForgerAddress = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
auctionEvents.SetCoordinator = append(auctionEvents.SetCoordinator, setCoordinator)
case logAuctionNewForgeAllocated:
var newForgeAllocated AuctionEventNewForgeAllocated
if err := c.contractAbi.Unpack(&newForgeAllocated, "NewForgeAllocated", vLog.Data); err != nil {
return nil, nil, err
}
newForgeAllocated.Forger = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
newForgeAllocated.CurrentSlot = new(big.Int).SetBytes(vLog.Topics[2][:]).Int64()
newForgeAllocated.Bidder = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
newForgeAllocated.Forger = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
newForgeAllocated.SlotToForge = new(big.Int).SetBytes(vLog.Topics[3][:]).Int64()
auctionEvents.NewForgeAllocated = append(auctionEvents.NewForgeAllocated, newForgeAllocated)
case logNewDefaultSlotSetBid:
case logAuctionNewDefaultSlotSetBid:
var auxNewDefaultSlotSetBid struct {
SlotSet *big.Int
NewInitialMinBid *big.Int
@ -1074,12 +1011,12 @@ func (c *AuctionClient) AuctionEventsByBlock(blockNum int64) (*AuctionEvents, *e
newDefaultSlotSetBid.NewInitialMinBid = auxNewDefaultSlotSetBid.NewInitialMinBid
newDefaultSlotSetBid.SlotSet = auxNewDefaultSlotSetBid.SlotSet.Int64()
auctionEvents.NewDefaultSlotSetBid = append(auctionEvents.NewDefaultSlotSetBid, newDefaultSlotSetBid)
case logNewForge:
case logAuctionNewForge:
var newForge AuctionEventNewForge
newForge.Forger = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
newForge.CurrentSlot = new(big.Int).SetBytes(vLog.Topics[2][:]).Int64()
newForge.SlotToForge = new(big.Int).SetBytes(vLog.Topics[2][:]).Int64()
auctionEvents.NewForge = append(auctionEvents.NewForge, newForge)
case logHEZClaimed:
case logAuctionHEZClaimed:
var HEZClaimed AuctionEventHEZClaimed
if err := c.contractAbi.Unpack(&HEZClaimed, "HEZClaimed", vLog.Data); err != nil {
return nil, nil, err

+ 87
- 115
eth/auction_test.go

@ -16,10 +16,10 @@ const currentSlotConst = 0
var allocationRatioConst [3]uint16 = [3]uint16{4000, 4000, 2000}
var auctionClient *AuctionClient
var auctionClientTest *AuctionClient
//var genesisBlock = 91
var genesisBlock = 98
//var genesisBlock = 93
var genesisBlock = 100
var minBidStr = "10000000000000000000"
var URL = "http://localhost:3000"
@ -27,7 +27,7 @@ var newURL = "http://localhost:3002"
var BLOCKSPERSLOT = uint8(40)
func TestAuctionGetCurrentSlotNumber(t *testing.T) {
currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
currentSlot, err := auctionClientTest.AuctionGetCurrentSlotNumber()
require.Nil(t, err)
currentSlotInt := int(currentSlot)
assert.Equal(t, currentSlotConst, currentSlotInt)
@ -37,13 +37,13 @@ func TestAuctionConstants(t *testing.T) {
INITMINBID := new(big.Int)
INITMINBID.SetString(minBidStr, 10)
auctionConstants, err := auctionClient.AuctionConstants()
auctionConstants, err := auctionClientTest.AuctionConstants()
require.Nil(t, err)
assert.Equal(t, auctionConstants.BlocksPerSlot, BLOCKSPERSLOT)
// assert.Equal(t, auctionConstants.GenesisBlockNum, GENESISBLOCKNUM)
assert.Equal(t, auctionConstants.GenesisBlockNum, int64(genesisBlock))
assert.Equal(t, auctionConstants.HermezRollup, hermezRollupAddressTestConst)
assert.Equal(t, auctionConstants.InitialMinimalBidding, INITMINBID)
assert.Equal(t, auctionConstants.TokenHEZ, tokenHezAddressConst)
assert.Equal(t, auctionConstants.TokenHEZ, tokenERC777AddressConst)
}
func TestAuctionVariables(t *testing.T) {
@ -51,7 +51,7 @@ func TestAuctionVariables(t *testing.T) {
INITMINBID.SetString(minBidStr, 10)
defaultSlotSetBid := [6]*big.Int{INITMINBID, INITMINBID, INITMINBID, INITMINBID, INITMINBID, INITMINBID}
auctionVariables, err := auctionClient.AuctionVariables()
auctionVariables, err := auctionClientTest.AuctionVariables()
require.Nil(t, err)
assert.Equal(t, auctionVariables.AllocationRatio, allocationRatioConst)
assert.Equal(t, auctionVariables.BootCoordinator, bootCoordinatorAddressConst)
@ -64,7 +64,7 @@ func TestAuctionVariables(t *testing.T) {
}
func TestAuctionGetSlotDeadline(t *testing.T) {
slotDeadline, err := auctionClient.AuctionGetSlotDeadline()
slotDeadline, err := auctionClientTest.AuctionGetSlotDeadline()
require.Nil(t, err)
assert.Equal(t, slotDeadlineConst, slotDeadline)
}
@ -72,18 +72,18 @@ func TestAuctionGetSlotDeadline(t *testing.T) {
func TestAuctionSetSlotDeadline(t *testing.T) {
newSlotDeadline := uint8(25)
_, err := auctionClient.AuctionSetSlotDeadline(newSlotDeadline)
_, err := auctionClientTest.AuctionSetSlotDeadline(newSlotDeadline)
require.Nil(t, err)
slotDeadline, err := auctionClient.AuctionGetSlotDeadline()
slotDeadline, err := auctionClientTest.AuctionGetSlotDeadline()
require.Nil(t, err)
assert.Equal(t, newSlotDeadline, slotDeadline)
currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(currentBlockNum)
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
assert.Equal(t, newSlotDeadline, auctionEvents.NewSlotDeadline[0].NewSlotDeadline)
}
func TestAuctionGetOpenAuctionSlots(t *testing.T) {
openAuctionSlots, err := auctionClient.AuctionGetOpenAuctionSlots()
openAuctionSlots, err := auctionClientTest.AuctionGetOpenAuctionSlots()
require.Nil(t, err)
assert.Equal(t, openAuctionSlotsConst, openAuctionSlots)
}
@ -91,18 +91,18 @@ func TestAuctionGetOpenAuctionSlots(t *testing.T) {
func TestAuctionSetOpenAuctionSlots(t *testing.T) {
newOpenAuctionSlots := uint16(4500)
_, err := auctionClient.AuctionSetOpenAuctionSlots(newOpenAuctionSlots)
_, err := auctionClientTest.AuctionSetOpenAuctionSlots(newOpenAuctionSlots)
require.Nil(t, err)
openAuctionSlots, err := auctionClient.AuctionGetOpenAuctionSlots()
openAuctionSlots, err := auctionClientTest.AuctionGetOpenAuctionSlots()
require.Nil(t, err)
assert.Equal(t, newOpenAuctionSlots, openAuctionSlots)
currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(currentBlockNum)
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
assert.Equal(t, newOpenAuctionSlots, auctionEvents.NewOpenAuctionSlots[0].NewOpenAuctionSlots)
}
func TestAuctionGetClosedAuctionSlots(t *testing.T) {
closedAuctionSlots, err := auctionClient.AuctionGetClosedAuctionSlots()
closedAuctionSlots, err := auctionClientTest.AuctionGetClosedAuctionSlots()
require.Nil(t, err)
assert.Equal(t, closedAuctionSlotsConst, closedAuctionSlots)
}
@ -110,20 +110,20 @@ func TestAuctionGetClosedAuctionSlots(t *testing.T) {
func TestAuctionSetClosedAuctionSlots(t *testing.T) {
newClosedAuctionSlots := uint16(1)
_, err := auctionClient.AuctionSetClosedAuctionSlots(newClosedAuctionSlots)
_, err := auctionClientTest.AuctionSetClosedAuctionSlots(newClosedAuctionSlots)
require.Nil(t, err)
closedAuctionSlots, err := auctionClient.AuctionGetClosedAuctionSlots()
closedAuctionSlots, err := auctionClientTest.AuctionGetClosedAuctionSlots()
require.Nil(t, err)
assert.Equal(t, newClosedAuctionSlots, closedAuctionSlots)
currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(currentBlockNum)
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
assert.Equal(t, newClosedAuctionSlots, auctionEvents.NewClosedAuctionSlots[0].NewClosedAuctionSlots)
_, err = auctionClient.AuctionSetClosedAuctionSlots(closedAuctionSlots)
_, err = auctionClientTest.AuctionSetClosedAuctionSlots(closedAuctionSlots)
require.Nil(t, err)
}
func TestAuctionGetOutbidding(t *testing.T) {
outbidding, err := auctionClient.AuctionGetOutbidding()
outbidding, err := auctionClientTest.AuctionGetOutbidding()
require.Nil(t, err)
assert.Equal(t, outbiddingConst, outbidding)
}
@ -131,20 +131,20 @@ func TestAuctionGetOutbidding(t *testing.T) {
func TestAuctionSetOutbidding(t *testing.T) {
newOutbidding := uint16(0xb)
_, err := auctionClient.AuctionSetOutbidding(newOutbidding)
_, err := auctionClientTest.AuctionSetOutbidding(newOutbidding)
require.Nil(t, err)
outbidding, err := auctionClient.AuctionGetOutbidding()
outbidding, err := auctionClientTest.AuctionGetOutbidding()
require.Nil(t, err)
assert.Equal(t, newOutbidding, outbidding)
currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(currentBlockNum)
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
assert.Equal(t, newOutbidding, auctionEvents.NewOutbidding[0].NewOutbidding)
_, err = auctionClient.AuctionSetOutbidding(outbiddingConst)
_, err = auctionClientTest.AuctionSetOutbidding(outbiddingConst)
require.Nil(t, err)
}
func TestAuctionGetAllocationRatio(t *testing.T) {
allocationRatio, err := auctionClient.AuctionGetAllocationRatio()
allocationRatio, err := auctionClientTest.AuctionGetAllocationRatio()
require.Nil(t, err)
assert.Equal(t, allocationRatioConst, allocationRatio)
}
@ -152,26 +152,26 @@ func TestAuctionGetAllocationRatio(t *testing.T) {
func TestAuctionSetAllocationRatio(t *testing.T) {
newAllocationRatio := [3]uint16{3000, 3000, 4000}
_, err := auctionClient.AuctionSetAllocationRatio(newAllocationRatio)
_, err := auctionClientTest.AuctionSetAllocationRatio(newAllocationRatio)
require.Nil(t, err)
allocationRatio, err := auctionClient.AuctionGetAllocationRatio()
allocationRatio, err := auctionClientTest.AuctionGetAllocationRatio()
require.Nil(t, err)
assert.Equal(t, newAllocationRatio, allocationRatio)
currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(currentBlockNum)
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
assert.Equal(t, newAllocationRatio, auctionEvents.NewAllocationRatio[0].NewAllocationRatio)
_, err = auctionClient.AuctionSetAllocationRatio(allocationRatioConst)
_, err = auctionClientTest.AuctionSetAllocationRatio(allocationRatioConst)
require.Nil(t, err)
}
func TestAuctionGetDonationAddress(t *testing.T) {
donationAddress, err := auctionClient.AuctionGetDonationAddress()
donationAddress, err := auctionClientTest.AuctionGetDonationAddress()
require.Nil(t, err)
assert.Equal(t, &donationAddressConst, donationAddress)
}
func TestAuctionGetBootCoordinator(t *testing.T) {
bootCoordinator, err := auctionClient.AuctionGetBootCoordinator()
bootCoordinator, err := auctionClientTest.AuctionGetBootCoordinator()
require.Nil(t, err)
assert.Equal(t, &bootCoordinatorAddressConst, bootCoordinator)
}
@ -179,37 +179,37 @@ func TestAuctionGetBootCoordinator(t *testing.T) {
func TestAuctionSetDonationAddress(t *testing.T) {
newDonationAddress := governanceAddressConst
_, err := auctionClient.AuctionSetDonationAddress(newDonationAddress)
_, err := auctionClientTest.AuctionSetDonationAddress(newDonationAddress)
require.Nil(t, err)
donationAddress, err := auctionClient.AuctionGetDonationAddress()
donationAddress, err := auctionClientTest.AuctionGetDonationAddress()
require.Nil(t, err)
assert.Equal(t, &newDonationAddress, donationAddress)
currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(currentBlockNum)
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
assert.Equal(t, newDonationAddress, auctionEvents.NewDonationAddress[0].NewDonationAddress)
_, err = auctionClient.AuctionSetDonationAddress(donationAddressConst)
_, err = auctionClientTest.AuctionSetDonationAddress(donationAddressConst)
require.Nil(t, err)
}
func TestAuctionSetBootCoordinator(t *testing.T) {
newBootCoordinator := governanceAddressConst
_, err := auctionClient.AuctionSetBootCoordinator(newBootCoordinator)
_, err := auctionClientTest.AuctionSetBootCoordinator(newBootCoordinator)
require.Nil(t, err)
bootCoordinator, err := auctionClient.AuctionGetBootCoordinator()
bootCoordinator, err := auctionClientTest.AuctionGetBootCoordinator()
require.Nil(t, err)
assert.Equal(t, &newBootCoordinator, bootCoordinator)
currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(currentBlockNum)
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
assert.Equal(t, newBootCoordinator, auctionEvents.NewBootCoordinator[0].NewBootCoordinator)
_, err = auctionClient.AuctionSetBootCoordinator(bootCoordinatorAddressConst)
_, err = auctionClientTest.AuctionSetBootCoordinator(bootCoordinatorAddressConst)
require.Nil(t, err)
}
func TestAuctionGetSlotSet(t *testing.T) {
slot := int64(10)
slotSet, err := auctionClient.AuctionGetSlotSet(slot)
slotSet, err := auctionClientTest.AuctionGetSlotSet(slot)
require.Nil(t, err)
assert.Equal(t, slotSet, big.NewInt(4))
}
@ -217,7 +217,7 @@ func TestAuctionGetSlotSet(t *testing.T) {
func TestAuctionGetDefaultSlotSetBid(t *testing.T) {
slotSet := uint8(3)
minBid, err := auctionClient.AuctionGetDefaultSlotSetBid(slotSet)
minBid, err := auctionClientTest.AuctionGetDefaultSlotSetBid(slotSet)
require.Nil(t, err)
assert.Equal(t, minBid.String(), minBidStr)
}
@ -228,81 +228,53 @@ func TestAuctionChangeDefaultSlotSetBid(t *testing.T) {
newInitialMinBid := new(big.Int)
newInitialMinBid.SetString("20000000000000000000", 10)
_, err := auctionClient.AuctionChangeDefaultSlotSetBid(slotSet, newInitialMinBid)
_, err := auctionClientTest.AuctionChangeDefaultSlotSetBid(slotSet, newInitialMinBid)
require.Nil(t, err)
minBid, err := auctionClient.AuctionGetDefaultSlotSetBid(set)
minBid, err := auctionClientTest.AuctionGetDefaultSlotSetBid(set)
require.Nil(t, err)
assert.Equal(t, minBid, newInitialMinBid)
currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(currentBlockNum)
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
assert.Equal(t, slotSet, auctionEvents.NewDefaultSlotSetBid[0].SlotSet)
assert.Equal(t, newInitialMinBid, auctionEvents.NewDefaultSlotSetBid[0].NewInitialMinBid)
newMinBid := new(big.Int)
newMinBid.SetString("10000000000000000000", 10)
_, err = auctionClient.AuctionChangeDefaultSlotSetBid(slotSet, newMinBid)
_, err = auctionClientTest.AuctionChangeDefaultSlotSetBid(slotSet, newMinBid)
require.Nil(t, err)
}
func TestAuctionGetClaimableHEZ(t *testing.T) {
forgerAddress := governanceAddressConst
claimableHEZ, err := auctionClient.AuctionGetClaimableHEZ(forgerAddress)
claimableHEZ, err := auctionClientTest.AuctionGetClaimableHEZ(forgerAddress)
require.Nil(t, err)
assert.Equal(t, claimableHEZ.Int64(), int64(0))
}
func TestAuctionIsRegisteredCoordinator(t *testing.T) {
forgerAddress := governanceAddressConst
registered, err := auctionClient.AuctionIsRegisteredCoordinator(forgerAddress)
require.Nil(t, err)
assert.Equal(t, registered, false)
}
func TestAuctionRegisterCoordinator(t *testing.T) {
forgerAddress := governanceAddressConst
_, err := auctionClient.AuctionRegisterCoordinator(forgerAddress, URL)
require.Nil(t, err)
currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(currentBlockNum)
assert.Equal(t, forgerAddress, auctionEvents.NewCoordinator[0].ForgerAddress)
assert.Equal(t, forgerAddress, auctionEvents.NewCoordinator[0].WithdrawalAddress)
assert.Equal(t, URL, auctionEvents.NewCoordinator[0].CoordinatorURL)
}
func TestAuctionIsRegisteredCoordinatorTrue(t *testing.T) {
forgerAddress := governanceAddressConst
registered, err := auctionClient.AuctionIsRegisteredCoordinator(forgerAddress)
require.Nil(t, err)
assert.Equal(t, registered, true)
}
func TestAuctionUpdateCoordinatorInfo(t *testing.T) {
forgerAddress := governanceAddressConst
_, err := auctionClient.AuctionUpdateCoordinatorInfo(forgerAddress, forgerAddress, newURL)
_, err := auctionClientTest.AuctionSetCoordinator(forgerAddress, URL)
require.Nil(t, err)
currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(currentBlockNum)
assert.Equal(t, forgerAddress, auctionEvents.CoordinatorUpdated[0].ForgerAddress)
assert.Equal(t, forgerAddress, auctionEvents.CoordinatorUpdated[0].WithdrawalAddress)
assert.Equal(t, newURL, auctionEvents.CoordinatorUpdated[0].CoordinatorURL)
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
assert.Equal(t, forgerAddress, auctionEvents.SetCoordinator[0].ForgerAddress)
assert.Equal(t, forgerAddress, auctionEvents.SetCoordinator[0].BidderAddress)
assert.Equal(t, URL, auctionEvents.SetCoordinator[0].CoordinatorURL)
}
func TestAuctionBid(t *testing.T) {
currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
currentSlot, err := auctionClientTest.AuctionGetCurrentSlotNumber()
require.Nil(t, err)
bidAmount := new(big.Int)
bidAmount.SetString("12000000000000000000", 10)
forgerAddress := governanceAddressConst
_, err = auctionClient.AuctionBid(currentSlot+4, bidAmount, forgerAddress)
_, err = auctionClientTest.AuctionBid(currentSlot+4, bidAmount)
require.Nil(t, err)
currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(currentBlockNum)
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
assert.Equal(t, bidAmount, auctionEvents.NewBid[0].BidAmount)
assert.Equal(t, forgerAddress, auctionEvents.NewBid[0].CoordinatorForger)
assert.Equal(t, forgerAddress, auctionEvents.NewBid[0].Bidder)
assert.Equal(t, currentSlot+4, auctionEvents.NewBid[0].Slot)
}
@ -310,22 +282,22 @@ func TestAuctionGetSlotNumber(t *testing.T) {
slotConst := 4
blockNum := int(BLOCKSPERSLOT)*slotConst + genesisBlock
slot, err := auctionClient.AuctionGetSlotNumber(int64(blockNum))
slot, err := auctionClientTest.AuctionGetSlotNumber(int64(blockNum))
require.Nil(t, err)
assert.Equal(t, slot, big.NewInt(int64(slotConst)))
assert.Equal(t, slot, int64(slotConst))
}
func TestAuctionCanForge(t *testing.T) {
slotConst := 4
blockNum := int(BLOCKSPERSLOT)*slotConst + genesisBlock
canForge, err := auctionClient.AuctionCanForge(governanceAddressConst, int64(blockNum))
canForge, err := auctionClientTest.AuctionCanForge(governanceAddressConst, int64(blockNum))
require.Nil(t, err)
assert.Equal(t, canForge, true)
}
func TestAuctionMultiBid(t *testing.T) {
currentSlot, err := auctionClient.AuctionGetCurrentSlotNumber()
currentSlot, err := auctionClientTest.AuctionGetCurrentSlotNumber()
require.Nil(t, err)
slotSet := [6]bool{true, false, true, false, true, false}
maxBid := new(big.Int)
@ -335,17 +307,17 @@ func TestAuctionMultiBid(t *testing.T) {
budget := new(big.Int)
budget.SetString("45200000000000000000", 10)
forgerAddress := governanceAddressConst
_, err = auctionClient.AuctionMultiBid(currentSlot+4, currentSlot+10, slotSet, maxBid, minBid, budget, forgerAddress)
_, err = auctionClientTest.AuctionMultiBid(currentSlot+4, currentSlot+10, slotSet, maxBid, minBid, budget)
require.Nil(t, err)
currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(currentBlockNum)
assert.Equal(t, forgerAddress, auctionEvents.NewBid[0].CoordinatorForger)
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
assert.Equal(t, forgerAddress, auctionEvents.NewBid[0].Bidder)
assert.Equal(t, currentSlot+4, auctionEvents.NewBid[0].Slot)
assert.Equal(t, forgerAddress, auctionEvents.NewBid[1].CoordinatorForger)
assert.Equal(t, forgerAddress, auctionEvents.NewBid[1].Bidder)
assert.Equal(t, currentSlot+6, auctionEvents.NewBid[1].Slot)
assert.Equal(t, forgerAddress, auctionEvents.NewBid[2].CoordinatorForger)
assert.Equal(t, forgerAddress, auctionEvents.NewBid[2].Bidder)
assert.Equal(t, currentSlot+8, auctionEvents.NewBid[2].Slot)
assert.Equal(t, forgerAddress, auctionEvents.NewBid[3].CoordinatorForger)
assert.Equal(t, forgerAddress, auctionEvents.NewBid[3].Bidder)
assert.Equal(t, currentSlot+10, auctionEvents.NewBid[3].Slot)
}
@ -354,7 +326,7 @@ func TestAuctionGetClaimableHEZ2(t *testing.T) {
amount := new(big.Int)
amount.SetString("11000000000000000000", 10)
claimableHEZ, err := auctionClient.AuctionGetClaimableHEZ(forgerAddress)
claimableHEZ, err := auctionClientTest.AuctionGetClaimableHEZ(forgerAddress)
require.Nil(t, err)
assert.Equal(t, claimableHEZ, amount)
}
@ -363,26 +335,26 @@ func TestAuctionClaimHEZ(t *testing.T) {
amount := new(big.Int)
amount.SetString("11000000000000000000", 10)
_, err := auctionClient.AuctionClaimHEZ(governanceAddressConst)
_, err := auctionClientTest.AuctionClaimHEZ()
require.Nil(t, err)
currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClient.AuctionEventsByBlock(currentBlockNum)
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
auctionEvents, _, _ := auctionClientTest.AuctionEventsByBlock(currentBlockNum)
assert.Equal(t, amount, auctionEvents.HEZClaimed[0].Amount)
assert.Equal(t, governanceAddressConst, auctionEvents.HEZClaimed[0].Owner)
}
func TestAuctionForge(t *testing.T) {
auctionClientHermez, err := NewAuctionClient(ethereumClientHermez, auctionAddressConst, tokenHezAddressConst)
auctionClientTestHermez, err := NewAuctionClient(ethereumClientHermez, auctionTestAddressConst, tokenERC777AddressConst)
require.Nil(t, err)
slotConst := 4
blockNum := int64(int(BLOCKSPERSLOT)*slotConst + genesisBlock)
currentBlockNum, _ := auctionClient.client.EthCurrentBlock()
currentBlockNum, _ := auctionClientTest.client.EthCurrentBlock()
blocksToAdd := blockNum - currentBlockNum
addBlocks(blocksToAdd, ethClientDialURL)
currentBlockNum, _ = auctionClient.client.EthCurrentBlock()
currentBlockNum, _ = auctionClientTest.client.EthCurrentBlock()
assert.Equal(t, currentBlockNum, blockNum)
_, err = auctionClientHermez.AuctionForge(bootCoordinatorAddressConst)
_, err = auctionClientTestHermez.AuctionForge(bootCoordinatorAddressConst)
require.Contains(t, err.Error(), "Can't forge")
_, err = auctionClientHermez.AuctionForge(governanceAddressConst)
_, err = auctionClientTestHermez.AuctionForge(governanceAddressConst)
require.Nil(t, err)
}

+ 3
- 2
eth/contracts/README.md

@ -6,9 +6,10 @@ The go code of the contracts has been generated with the following command:
abigen --abi=WithdrawalDelayer.abi --bin=WithdrawalDelayer.bin --pkg=WithdrawalDelayer --out=WithdrawalDelayer.go
abigen --abi=Hermez.abi --bin=Hermez.bin --pkg=Hermez --out=Hermez.go
abigen --abi=HermezAuctionProtocol.abi --bin=HermezAuctionProtocol.bin --pkg=HermezAuctionProtocol --out=HermezAuctionProtocol.go
abigen --abi=ERC777.abi --bin=ERC777.bin --pkg=ERC777 --out=ERC777.go
```
You must compile the contracts to get the `.bin` and `.abi` files. The contracts used are in the repo: https://github.com/hermeznetwork/contracts-circuits
You must compile the contracts to get the `.bin` and `.abi` files. The contracts used are in the repo: https://github.com/hermeznetwork/contracts
Specifically they have been processed in the commit with hash: `745e8d588496d7762d4084a54bafd4435061ae35`
Specifically they have been processed in the commit with hash: `60e03e981f1ce607c27d405952bfc98de376f0c5`
> abigen version 1.9.21

+ 289
- 424
eth/contracts/auction/HermezAuctionProtocol.go
File diff suppressed because it is too large
View File


+ 1
- 1
eth/contracts/erc777/ERC777.go
File diff suppressed because it is too large
View File


+ 442
- 861
eth/contracts/hermez/Hermez.go
File diff suppressed because it is too large
View File


+ 67
- 45
eth/contracts/withdrawdelayer/WithdrawalDelayer.go
File diff suppressed because it is too large
View File


+ 22
- 9
eth/main_test.go

@ -25,20 +25,26 @@ var password = "pass"
// Smart Contract Addresses
var (
// auctionAddressStr = "0x3619DbE27d7c1e7E91aA738697Ae7Bc5FC3eACA5"
// wdelayerAddressStr = "0x1A1FEe7EeD918BD762173e4dc5EfDB8a78C924A8"
auctionAddressStr = "0x8B5B7a6055E54a36fF574bbE40cf2eA68d5554b3"
auctionAddressStr = "0x500D1d6A4c7D8Ae28240b47c8FCde034D827fD5e"
auctionAddressConst = ethCommon.HexToAddress(auctionAddressStr)
auctionTestAddressStr = "0x1d80315fac6aBd3EfeEbE97dEc44461ba7556160"
auctionTestAddressConst = ethCommon.HexToAddress(auctionTestAddressStr)
donationAddressStr = "0x6c365935CA8710200C7595F0a72EB6023A7706Cd"
donationAddressConst = ethCommon.HexToAddress(donationAddressStr)
bootCoordinatorAddressStr = "0xc783df8a850f42e7f7e57013759c285caa701eb6"
bootCoordinatorAddressConst = ethCommon.HexToAddress(bootCoordinatorAddressStr)
tokenHezAddressStr = "0xf4e77E5Da47AC3125140c470c71cBca77B5c638c" //nolint:gosec
tokenHezAddressConst = ethCommon.HexToAddress(tokenHezAddressStr)
hermezRollupAddressStr = "0xc4905364b78a742ccce7B890A89514061E47068D"
tokenERC777AddressStr = "0xf784709d2317D872237C4bC22f867d1BAe2913AB" //nolint:gosec
tokenERC777AddressConst = ethCommon.HexToAddress(tokenERC777AddressStr)
tokenERC20AddressStr = "0x3619DbE27d7c1e7E91aA738697Ae7Bc5FC3eACA5"
tokenERC20AddressConst = ethCommon.HexToAddress(tokenERC20AddressStr)
hermezRollupAddressStr = "0xEcc0a6dbC0bb4D51E4F84A315a9e5B0438cAD4f0"
hermezRollupAddressConst = ethCommon.HexToAddress(hermezRollupAddressStr)
wdelayerAddressStr = "0x20Ce94F404343aD2752A2D01b43fa407db9E0D00"
wdelayerAddressStr = "0xD6C850aeBFDC46D7F4c207e445cC0d6B0919BDBe"
wdelayerAddressConst = ethCommon.HexToAddress(wdelayerAddressStr)
wdelayerTestAddressStr = "0x52d3b94181f8654db2530b0fEe1B19173f519C52"
wdelayerTestAddressConst = ethCommon.HexToAddress(wdelayerTestAddressStr)
safetyAddressStr = "0xE5904695748fe4A84b40b3fc79De2277660BD1D3"
safetyAddressConst = ethCommon.HexToAddress(safetyAddressStr)
)
// Ethereum Accounts
@ -126,15 +132,22 @@ func TestMain(m *testing.M) {
// Controllable Governance Address
ethereumClientGov := NewEthereumClient(ethClient, accountGov, ks, nil)
auctionClient, err = NewAuctionClient(ethereumClientGov, auctionAddressConst, tokenHezAddressConst)
auctionClientTest, err = NewAuctionClient(ethereumClientGov, auctionTestAddressConst, tokenERC777AddressConst)
if err != nil {
panic(err)
}
rollupClient, err = NewRollupClient(ethereumClientGov, hermezRollupAddressConst)
if err != nil {
panic(err)
}
rollupClient = NewRollupClient(ethereumClientGov, hermezRollupAddressConst)
wdelayerClient, err = NewWDelayerClient(ethereumClientGov, wdelayerAddressConst)
if err != nil {
panic(err)
}
wdelayerClientTest, err = NewWDelayerClient(ethereumClientGov, wdelayerTestAddressConst)
if err != nil {
panic(err)
}
ethereumClientKep = NewEthereumClient(ethClient, accountKep, ks, nil)
ethereumClientWhite = NewEthereumClient(ethClient, accountWhite, ks, nil)

+ 317
- 88
eth/rollup.go

@ -1,27 +1,103 @@
package eth
import (
"context"
"math/big"
"strings"
Hermez "github.com/hermeznetwork/hermez-node/eth/contracts/hermez"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/hermeznetwork/hermez-node/common"
Hermez "github.com/hermeznetwork/hermez-node/eth/contracts/hermez"
"github.com/hermeznetwork/hermez-node/log"
"github.com/iden3/go-iden3-crypto/babyjub"
)
const (
// FeeIdxCoordinatorLen is the number of tokens the coordinator can use
// 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.
FeeIdxCoordinatorLen = 64
RollupConstFeeIdxCoordinatorLen = 64
// RollupConstReservedIDx First 256 indexes reserved, first user index will be the 256
RollupConstReservedIDx = 255
// RollupConstExitIDx IDX 1 is reserved for exits
RollupConstExitIDx = 1
// RollupConstLimitLoadAmount Max load amount allowed (loadAmount: L1 --> L2)
RollupConstLimitLoadAmount = (1 << 128)
// RollupConstLimitL2TransferAmount Max amount allowed (amount L2 --> L2)
RollupConstLimitL2TransferAmount = (1 << 192)
// RollupConstLimitTokens Max number of tokens allowed to be registered inside the rollup
RollupConstLimitTokens = (1 << 32)
// RollupConstL1CoordinatorTotalBytes [4 bytes] token + [32 bytes] babyjub + [65 bytes] compressedSignature
RollupConstL1CoordinatorTotalBytes = 101
// RollupConstL1UserTotalBytes [20 bytes] fromEthAddr + [32 bytes] fromBjj-compressed + [6 bytes] fromIdx +
// [2 bytes] loadAmountFloat16 + [2 bytes] amountFloat16 + [4 bytes] tokenId + [6 bytes] toIdx
RollupConstL1UserTotalBytes = 72
// RollupConstMaxL1UserTX Maximum L1-user transactions allowed to be queued in a batch
RollupConstMaxL1UserTX = 128
// RollupConstMaxL1TX Maximum L1 transactions allowed to be queued in a batch
RollupConstMaxL1TX = 256
// RollupConstRfield Modulus zkSNARK
RollupConstRfield = 21888242871839275222246405745257275088548364400416034343698204186575808495617
// RollupConstInputSHAConstantBytes [6 bytes] lastIdx + [6 bytes] newLastIdx + [32 bytes] stateRoot + [32 bytes] newStRoot + [32 bytes] newExitRoot +
// [_MAX_L1_TX * _L1_USER_TOTALBYTES bytes] l1TxsData + totalL2TxsDataLength + feeIdxCoordinatorLength + [2 bytes] chainID =
// 18542 bytes + totalL2TxsDataLength + feeIdxCoordinatorLength
RollupConstInputSHAConstantBytes = 18542
// RollupConstNumBuckets Number of buckets
RollupConstNumBuckets = 5
// RollupConstMaxWithdrawalDelay max withdrawal delay in seconds
RollupConstMaxWithdrawalDelay = 2 * 7 * 24 * 60 * 60
// RollupConstExchangeMultiplier exchange multiplier
RollupConstExchangeMultiplier = 1e14
// LenVerifiers number of Rollup Smart Contract Verifiers
LenVerifiers = 1
)
var (
// RollupConstEthAddressInternalOnly This ethereum address is used internally for rollup accounts that don't have ethereum address, only Babyjubjub
// This non-ethereum accounts can be created by the coordinator and allow users to have a rollup
// account without needing an ethereum address
RollupConstEthAddressInternalOnly = ethCommon.HexToAddress("0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF")
// RollupConstERC1820 ERC1820Registry address
RollupConstERC1820 = ethCommon.HexToAddress("0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24")
// ERC777 tokens signatures
// RollupConstRecipientInterfaceHash ERC777 recipient interface hash
RollupConstRecipientInterfaceHash = crypto.Keccak256([]byte("ERC777TokensRecipient"))
// RollupConstPerformL1UserTXSignature the signature of the function that can be called thru an ERC777 `send`
RollupConstPerformL1UserTXSignature = crypto.Keccak256([]byte("addL1Transaction(uint256,uint48,uint16,uint16,uint32,uint48)"))
// RollupConstAddTokenSignature the signature of the function that can be called thru an ERC777 `send`
RollupConstAddTokenSignature = crypto.Keccak256([]byte("addToken(address)"))
// RollupConstSendSignature ERC777 Signature
RollupConstSendSignature = crypto.Keccak256([]byte("send(address,uint256,bytes)"))
// RollupConstERC777Granularity ERC777 Signature
RollupConstERC777Granularity = crypto.Keccak256([]byte("granularity()"))
// RollupConstWithdrawalDelayerDeposit This constant are used to deposit tokens from ERC77 tokens into withdrawal delayer
RollupConstWithdrawalDelayerDeposit = crypto.Keccak256([]byte("deposit(address,address,uint192)"))
// ERC20 signature
// RollupConstTransferSignature This constant is used in the _safeTransfer internal method in order to safe GAS.
RollupConstTransferSignature = crypto.Keccak256([]byte("transfer(address,uint256)"))
// RollupConstTransferFromSignature This constant is used in the _safeTransfer internal method in order to safe GAS.
RollupConstTransferFromSignature = crypto.Keccak256([]byte("transferFrom(address,address,uint256)"))
// RollupConstApproveSignature This constant is used in the _safeTransfer internal method in order to safe GAS.
RollupConstApproveSignature = crypto.Keccak256([]byte("approve(address,uint256)"))
// RollupConstERC20Signature ERC20 decimals signature
RollupConstERC20Signature = crypto.Keccak256([]byte("decimals()"))
)
// RollupConstants are the constants of the Rollup Smart Contract
type RollupConstants struct {
/* type RollupConstants struct {
// Maxim Deposit allowed
MaxAmountDeposit *big.Int
MaxAmountL2 *big.Int
@ -50,12 +126,24 @@ type RollupConstants struct {
NoLimitToken int
NumBuckets int
MaxWDelay int64
}*/
// RollupPublicConstants are the constants of the Rollup Smart Contract
type RollupPublicConstants struct {
AbsoluteMaxL1L2BatchTimeout uint8
TokenHEZ ethCommon.Address
Verifiers []RollupVerifierStruct
HermezAuctionContract ethCommon.Address
HermezGovernanceDAOAddress ethCommon.Address
SafetyAddress ethCommon.Address
WithdrawDelayerContract ethCommon.Address
}
// RollupVariables are the variables of the Rollup Smart Contract
type RollupVariables struct {
FeeAddToken *big.Int
ForgeL1Timeout int64
FeeAddToken *big.Int
ForgeL1L2BatchTimeout int64
WithdrawalDelay uint64
}
// QueueStruct is the queue of L1Txs for a batch
@ -73,6 +161,12 @@ func NewQueueStruct() *QueueStruct {
}
}
// RollupVerifierStruct is the information about verifiers of the Rollup Smart Contract
type RollupVerifierStruct struct {
MaxTx *big.Int
NLevels *big.Int
}
// RollupState represents the state of the Rollup in the Smart Contract
//nolint:structcheck,unused
type RollupState struct {
@ -90,9 +184,16 @@ type RollupState struct {
// RollupEventL1UserTx is an event of the Rollup Smart Contract
type RollupEventL1UserTx struct {
ToForgeL1TxsNum uint64 // QueueIndex *big.Int
Position uint8 // TransactionIndex *big.Int
L1Tx common.L1Tx
ToForgeL1TxsNum int64 // QueueIndex *big.Int
Position int // TransactionIndex *big.Int
}
// RollupEventL1UserTxAux is an event of the Rollup Smart Contract
type RollupEventL1UserTxAux struct {
ToForgeL1TxsNum uint64 // QueueIndex *big.Int
Position uint8 // TransactionIndex *big.Int
L1Tx []byte
}
// RollupEventAddToken is an event of the Rollup Smart Contract
@ -109,7 +210,7 @@ type RollupEventForgeBatch struct {
// RollupEventUpdateForgeL1L2BatchTimeout is an event of the Rollup Smart Contract
type RollupEventUpdateForgeL1L2BatchTimeout struct {
ForgeL1Timeout *big.Int
ForgeL1L2BatchTimeout uint8
}
// RollupEventUpdateFeeAddToken is an event of the Rollup Smart Contract
@ -119,8 +220,8 @@ type RollupEventUpdateFeeAddToken struct {
// RollupEventWithdrawEvent is an event of the Rollup Smart Contract
type RollupEventWithdrawEvent struct {
Idx *big.Int
NumExitRoot *big.Int
Idx uint64
NumExitRoot uint64
InstantWithdraw bool
}
@ -149,19 +250,35 @@ func NewRollupEvents() RollupEvents {
// 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
L1CoordinatorTxs []*common.L1Tx
L1CoordinatorTxsAuths [][]byte // Authorization for accountCreations for each L1CoordinatorTxs
L2Txs []*common.L2Tx
FeeIdxCoordinator []common.Idx
NewLastIdx uint64
NewStRoot *big.Int
NewExitRoot *big.Int
L1CoordinatorTxs []*common.L1Tx
L2TxsData []*common.L2Tx
FeeIdxCoordinator []common.Idx
// Circuit selector
VerifierIdx uint8
L1Batch bool
ProofA [2]*big.Int
ProofB [2][2]*big.Int
ProofC [2]*big.Int
}
// RollupForgeBatchArgsAux are the arguments to the ForgeBatch function in the Rollup Smart Contract
//nolint:structcheck,unused
type RollupForgeBatchArgsAux struct {
NewLastIdx uint64
NewStRoot *big.Int
NewExitRoot *big.Int
L1CoordinatorTxs []byte
L2TxsData []byte
FeeIdxCoordinator []byte
// Circuit selector
VerifierIdx int64
VerifierIdx uint8
L1Batch bool
ProofA [2]*big.Int
ProofB [2][2]*big.Int
ProofC [2]*big.Int
}
// RollupInterface is the inteface to to Rollup Smart Contract
@ -201,7 +318,7 @@ type RollupInterface interface {
// Smart Contract Status
//
RollupConstants() (*RollupConstants, error)
RollupConstants() (*RollupPublicConstants, error)
RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error)
RollupForgeBatchArgs(ethCommon.Hash) (*RollupForgeBatchArgs, error)
}
@ -212,16 +329,22 @@ 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
client *EthereumClient
address ethCommon.Address
contractAbi abi.ABI
}
// NewRollupClient creates a new RollupClient
func NewRollupClient(client *EthereumClient, address ethCommon.Address) *RollupClient {
return &RollupClient{
client: client,
address: address,
func NewRollupClient(client *EthereumClient, address 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,
contractAbi: contractAbi,
}, nil
}
// RollupForgeBatch is the interface to call the smart contract function
@ -323,99 +446,205 @@ func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.
}
// RollupConstants returns the Constants of the Rollup Smart Contract
func (c *RollupClient) RollupConstants() (*RollupConstants, error) {
rollupConstants := new(RollupConstants)
func (c *RollupClient) RollupConstants() (*RollupPublicConstants, error) {
rollupConstants := new(RollupPublicConstants)
if err := c.client.Call(func(ec *ethclient.Client) error {
rollup, err := Hermez.NewHermez(c.address, ec)
if err != nil {
return err
}
// rollupConstants.GovernanceAddress :=
l1CoordinatorBytes, err := rollup.L1COORDINATORBYTES(nil)
if err != nil {
return err
}
rollupConstants.L1CoordinatorBytes = int(l1CoordinatorBytes.Int64())
l1UserBytes, err := rollup.L1USERBYTES(nil)
if err != nil {
return err
}
rollupConstants.L1UserBytes = int(l1UserBytes.Int64())
l2Bytes, err := rollup.L2BYTES(nil)
if err != nil {
return err
}
rollupConstants.L2Bytes = int(l2Bytes.Int64())
rollupConstants.LastIDx, err = rollup.LASTIDX(nil)
if err != nil {
return err
}
rollupConstants.MaxAmountDeposit, err = rollup.MAXLOADAMOUNT(nil)
hermez, err := Hermez.NewHermez(c.address, ec)
if err != nil {
return err
}
rollupConstants.MaxAmountL2, err = rollup.MAXAMOUNT(nil)
rollupConstants.AbsoluteMaxL1L2BatchTimeout, err = hermez.ABSOLUTEMAXL1L2BATCHTIMEOUT(nil)
if err != nil {
return err
}
maxL1Tx, err := rollup.MAXL1TX(nil)
rollupConstants.TokenHEZ, err = hermez.TokenHEZ(nil)
if err != nil {
return err
}
rollupConstants.MaxL1Tx = int(maxL1Tx.Int64())
maxL1UserTx, err := rollup.MAXL1USERTX(nil)
if err != nil {
return err
}
rollupConstants.MaxL1UserTx = int(maxL1UserTx.Int64())
maxTokens, err := rollup.MAXTOKENS(nil)
if err != nil {
return err
}
rollupConstants.MaxTokens = maxTokens.Int64()
maxWDelay, err := rollup.MAXWITHDRAWALDELAY(nil)
if err != nil {
return err
for i := int64(0); i < int64(LenVerifiers); i++ {
newRollupVerifier := new(RollupVerifierStruct)
rollupVerifier, err := hermez.RollupVerifiers(nil, big.NewInt(i))
if err != nil {
return err
}
newRollupVerifier.MaxTx = rollupVerifier.MaxTx
newRollupVerifier.NLevels = rollupVerifier.NLevels
rollupConstants.Verifiers = append(rollupConstants.Verifiers, *newRollupVerifier)
}
rollupConstants.MaxWDelay = maxWDelay.Int64()
noLimitToken, err := rollup.NOLIMIT(nil)
rollupConstants.HermezAuctionContract, err = hermez.HermezAuctionContract(nil)
if err != nil {
return err
}
rollupConstants.NoLimitToken = int(noLimitToken.Int64())
numBuckets, err := rollup.NUMBUCKETS(nil)
rollupConstants.HermezGovernanceDAOAddress, err = hermez.HermezGovernanceDAOAddress(nil)
if err != nil {
return err
}
rollupConstants.NumBuckets = int(numBuckets.Int64())
// rollupConstants.ReservedIDx =
rollupConstants.Rfield, err = rollup.RFIELD(nil)
rollupConstants.SafetyAddress, err = hermez.SafetyAddress(nil)
if err != nil {
return err
}
// rollupConstants.SafetyBot =
// rollupConstants.TokenHEZ =
// rollupConstants.WithdrawalContract =
return nil
rollupConstants.WithdrawDelayerContract, err = hermez.WithdrawDelayerContract(nil)
return err
}); err != nil {
return nil, err
}
return rollupConstants, nil
}
var (
logHermezL1UserTXEvent = crypto.Keccak256Hash([]byte("L1UserTxEvent(uint64,uint8,bytes)"))
logHermezAddToken = crypto.Keccak256Hash([]byte("AddToken(address,uint32)"))
logHermezForgeBatch = crypto.Keccak256Hash([]byte("ForgeBatch(uint64)"))
logHermezUpdateForgeL1L2BatchTimeout = crypto.Keccak256Hash([]byte("UpdateForgeL1L2BatchTimeout(uint8)"))
logHermezUpdateFeeAddToken = crypto.Keccak256Hash([]byte("UpdateFeeAddToken(uint256)"))
logHermezWithdrawEvent = crypto.Keccak256Hash([]byte("WithdrawEvent(uint48,uint48,bool)"))
)
// RollupEventsByBlock returns the events in a block that happened in the Rollup Smart Contract
func (c *RollupClient) RollupEventsByBlock(blockNum int64) (*RollupEvents, *ethCommon.Hash, error) {
log.Error("TODO")
return nil, nil, errTODO
var rollupEvents RollupEvents
var blockHash ethCommon.Hash
query := ethereum.FilterQuery{
FromBlock: big.NewInt(blockNum),
ToBlock: big.NewInt(blockNum),
Addresses: []ethCommon.Address{
c.address,
},
BlockHash: nil,
Topics: [][]ethCommon.Hash{},
}
logs, err := c.client.client.FilterLogs(context.Background(), query)
if err != nil {
return nil, nil, err
}
if len(logs) > 0 {
blockHash = logs[0].BlockHash
}
for _, vLog := range logs {
if vLog.BlockHash != blockHash {
return nil, nil, ErrBlockHashMismatchEvent
}
switch vLog.Topics[0] {
case logHermezL1UserTXEvent:
var L1UserTxAux RollupEventL1UserTxAux
var L1UserTx RollupEventL1UserTx
err := c.contractAbi.Unpack(&L1UserTxAux, "L1UserTxEvent", vLog.Data)
if err != nil {
return nil, nil, err
}
L1Tx, err := common.L1TxFromBytes(L1UserTxAux.L1Tx)
if err != nil {
return nil, nil, err
}
L1UserTx.ToForgeL1TxsNum = new(big.Int).SetBytes(vLog.Topics[1][:]).Uint64()
L1UserTx.Position = uint8(new(big.Int).SetBytes(vLog.Topics[2][:]).Uint64())
L1UserTx.L1Tx = *L1Tx
rollupEvents.L1UserTx = append(rollupEvents.L1UserTx, L1UserTx)
case logHermezAddToken:
var addToken RollupEventAddToken
err := c.contractAbi.Unpack(&addToken, "AddToken", vLog.Data)
if err != nil {
return nil, nil, err
}
addToken.Address = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
rollupEvents.AddToken = append(rollupEvents.AddToken, addToken)
case logHermezForgeBatch:
var forgeBatch RollupEventForgeBatch
forgeBatch.BatchNum = new(big.Int).SetBytes(vLog.Topics[1][:]).Int64()
rollupEvents.ForgeBatch = append(rollupEvents.ForgeBatch, forgeBatch)
case logHermezUpdateForgeL1L2BatchTimeout:
var updateForgeL1L2BatchTimeout RollupEventUpdateForgeL1L2BatchTimeout
err := c.contractAbi.Unpack(&updateForgeL1L2BatchTimeout, "UpdateForgeL1L2BatchTimeout", vLog.Data)
if err != nil {
return nil, nil, err
}
rollupEvents.UpdateForgeL1L2BatchTimeout = append(rollupEvents.UpdateForgeL1L2BatchTimeout, updateForgeL1L2BatchTimeout)
case logHermezUpdateFeeAddToken:
var updateFeeAddToken RollupEventUpdateFeeAddToken
err := c.contractAbi.Unpack(&updateFeeAddToken, "UpdateFeeAddToken", vLog.Data)
if err != nil {
return nil, nil, err
}
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()
rollupEvents.WithdrawEvent = append(rollupEvents.WithdrawEvent, withdraw)
}
}
return &rollupEvents, &blockHash, nil
}
// RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the Rollup Smart Contract in the given transaction
func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash) (*RollupForgeBatchArgs, error) {
tx, _, err := c.client.client.TransactionByHash(context.Background(), ethTxHash)
if err != nil {
return nil, err
}
txData := tx.Data()
method, err := c.contractAbi.MethodById(txData)
if err != nil {
return nil, err
}
aux := new(RollupForgeBatchArgsAux)
method.Inputs.Unpack(aux, txData)
rollupForgeBatchArgs := new(RollupForgeBatchArgs)
rollupForgeBatchArgs.L1Batch = aux.L1Batch
rollupForgeBatchArgs.NewExitRoot = aux.NewExitRoot
rollupForgeBatchArgs.NewLastIdx = aux.NewLastIdx
rollupForgeBatchArgs.NewStRoot = aux.NewStRoot
rollupForgeBatchArgs.ProofA = aux.ProofA
rollupForgeBatchArgs.ProofB = aux.ProofB
rollupForgeBatchArgs.ProofC = aux.ProofC
rollupForgeBatchArgs.VerifierIdx = aux.VerifierIdx
numTxsL1 := len(aux.L1CoordinatorTxs) / common.L1TxBytesLen
for i := 0; i < numTxsL1; i++ {
L1Tx, err := common.L1TxFromCoordinatorBytes(aux.L1CoordinatorTxs[i*common.L1CoordinatorTxBytesLen : (i+1)*common.L1CoordinatorTxBytesLen])
if err != nil {
return nil, err
}
rollupForgeBatchArgs.L1CoordinatorTxs = append(rollupForgeBatchArgs.L1CoordinatorTxs, L1Tx)
}
rollupConsts, err := c.RollupConstants()
if err != nil {
return nil, err
}
nLevels := rollupConsts.Verifiers[rollupForgeBatchArgs.VerifierIdx].NLevels.Int64()
lenL2TxsBytes := int((nLevels/8)*2 + 2 + 1)
numTxsL2 := len(aux.L2TxsData) / lenL2TxsBytes
for i := 0; i < numTxsL2; i++ {
L2Tx, err := common.L2TxFromBytes(aux.L2TxsData[i*lenL2TxsBytes:(i+1)*lenL2TxsBytes], int(nLevels))
if err != nil {
return nil, err
}
rollupForgeBatchArgs.L2TxsData = append(rollupForgeBatchArgs.L2TxsData, L2Tx)
}
lenFeeIdxCoordinatorBytes := int(nLevels / 8)
numFeeIdxCoordinator := len(aux.FeeIdxCoordinator) / lenFeeIdxCoordinatorBytes
for i := 0; i < numFeeIdxCoordinator; i++ {
var paddedFeeIdx [6]byte
if lenFeeIdxCoordinatorBytes < common.IdxBytesLen {
copy(paddedFeeIdx[6-lenFeeIdxCoordinatorBytes:], aux.FeeIdxCoordinator[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
} else {
copy(paddedFeeIdx[:], aux.FeeIdxCoordinator[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
}
FeeIdxCoordinator, err := common.IdxFromBytes(paddedFeeIdx[:])
if err != nil {
return nil, err
}
rollupForgeBatchArgs.FeeIdxCoordinator = append(rollupForgeBatchArgs.FeeIdxCoordinator, FeeIdxCoordinator)
}
return rollupForgeBatchArgs, nil
// tx := client.TransactionByHash(ethTxHash) -> types.Transaction
// txData := types.Transaction -> Data()
// m := abi.MethodById(txData) -> Method
// m.Inputs.Unpack(txData) -> Args
// client.TransactionReceipt()?
log.Error("TODO")
return nil, errTODO
}

+ 16
- 4
eth/rollup_test.go

@ -1,16 +1,28 @@
package eth
import (
"math/big"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var rollupClient *RollupClient
var absoluteMaxL1L2BatchTimeout = uint8(240)
var maxTx = big.NewInt(512)
var nLevels = big.NewInt(32)
func TestRollupConstants(t *testing.T) {
if rollupClient != nil {
_, err := rollupClient.RollupConstants()
require.Nil(t, err)
}
rollupConstants, err := rollupClient.RollupConstants()
require.Nil(t, err)
assert.Equal(t, absoluteMaxL1L2BatchTimeout, rollupConstants.AbsoluteMaxL1L2BatchTimeout)
assert.Equal(t, auctionAddressConst, rollupConstants.HermezAuctionContract)
assert.Equal(t, tokenERC777AddressConst, rollupConstants.TokenHEZ)
assert.Equal(t, maxTx, rollupConstants.Verifiers[0].MaxTx)
assert.Equal(t, nLevels, rollupConstants.Verifiers[0].NLevels)
assert.Equal(t, governanceAddressConst, rollupConstants.HermezGovernanceDAOAddress)
assert.Equal(t, safetyAddressConst, rollupConstants.SafetyAddress)
assert.Equal(t, wdelayerAddressConst, rollupConstants.WithdrawDelayerContract)
}

+ 27
- 22
eth/wdelayer.go

@ -58,9 +58,10 @@ type WDelayerEventNewWithdrawalDelay struct {
// WDelayerEventEscapeHatchWithdrawal an event of the WithdrawalDelayer Smart Contract
type WDelayerEventEscapeHatchWithdrawal struct {
Who ethCommon.Address
To ethCommon.Address
Token ethCommon.Address
Who ethCommon.Address
To ethCommon.Address
Token ethCommon.Address
Amount *big.Int
}
// WDelayerEventNewHermezKeeperAddress an event of the WithdrawalDelayer Smart Contract
@ -124,7 +125,7 @@ type WDelayerInterface interface {
WDelayerDepositInfo(owner, token ethCommon.Address) (*big.Int, uint64)
WDelayerDeposit(onwer, token ethCommon.Address, amount *big.Int) (*types.Transaction, error)
WDelayerWithdrawal(owner, token ethCommon.Address) (*types.Transaction, error)
WDelayerEscapeHatchWithdrawal(to, token ethCommon.Address) (*types.Transaction, error)
WDelayerEscapeHatchWithdrawal(to, token ethCommon.Address, amount *big.Int) (*types.Transaction, error)
}
//
@ -401,7 +402,7 @@ func (c *WDelayerClient) WDelayerWithdrawal(owner, token ethCommon.Address) (*ty
}
// WDelayerEscapeHatchWithdrawal is the interface to call the smart contract function
func (c *WDelayerClient) WDelayerEscapeHatchWithdrawal(to, token ethCommon.Address) (*types.Transaction, error) {
func (c *WDelayerClient) WDelayerEscapeHatchWithdrawal(to, token ethCommon.Address, amount *big.Int) (*types.Transaction, error) {
var tx *types.Transaction
var err error
if tx, err = c.client.CallAuth(
@ -411,7 +412,7 @@ func (c *WDelayerClient) WDelayerEscapeHatchWithdrawal(to, token ethCommon.Addre
if err != nil {
return nil, err
}
return wdelayer.EscapeHatchWithdrawal(auth, to, token)
return wdelayer.EscapeHatchWithdrawal(auth, to, token, amount)
},
); err != nil {
return nil, fmt.Errorf("Failed escapeHatchWithdrawal: %w", err)
@ -420,14 +421,14 @@ func (c *WDelayerClient) WDelayerEscapeHatchWithdrawal(to, token ethCommon.Addre
}
var (
logDeposit = crypto.Keccak256Hash([]byte("Deposit(address,address,uint192,uint64)"))
logWithdraw = crypto.Keccak256Hash([]byte("Withdraw(address,address,uint192)"))
logEmergencyModeEnabled = crypto.Keccak256Hash([]byte("EmergencyModeEnabled()"))
logNewWithdrawalDelay = crypto.Keccak256Hash([]byte("NewWithdrawalDelay(uint64)"))
logEscapeHatchWithdrawal = crypto.Keccak256Hash([]byte("EscapeHatchWithdrawal(address,address,address)"))
logNewHermezKeeperAddress = crypto.Keccak256Hash([]byte("NewHermezKeeperAddress(address)"))
logNewWhiteHackGroupAddress = crypto.Keccak256Hash([]byte("NewWhiteHackGroupAddress(address)"))
logNewHermezGovernanceDAOAddress = crypto.Keccak256Hash([]byte("NewHermezGovernanceDAOAddress(address)"))
logWDelayerDeposit = crypto.Keccak256Hash([]byte("Deposit(address,address,uint192,uint64)"))
logWDelayerWithdraw = crypto.Keccak256Hash([]byte("Withdraw(address,address,uint192)"))
logWDelayerEmergencyModeEnabled = crypto.Keccak256Hash([]byte("EmergencyModeEnabled()"))
logWDelayerNewWithdrawalDelay = crypto.Keccak256Hash([]byte("NewWithdrawalDelay(uint64)"))
logWDelayerEscapeHatchWithdrawal = crypto.Keccak256Hash([]byte("EscapeHatchWithdrawal(address,address,address,uint256)"))
logWDelayerNewHermezKeeperAddress = crypto.Keccak256Hash([]byte("NewHermezKeeperAddress(address)"))
logWDelayerNewWhiteHackGroupAddress = crypto.Keccak256Hash([]byte("NewWhiteHackGroupAddress(address)"))
logWDelayerNewHermezGovernanceDAOAddress = crypto.Keccak256Hash([]byte("NewHermezGovernanceDAOAddress(address)"))
)
// WDelayerEventsByBlock returns the events in a block that happened in the
@ -459,7 +460,7 @@ func (c *WDelayerClient) WDelayerEventsByBlock(blockNum int64) (*WDelayerEvents,
return nil, nil, ErrBlockHashMismatchEvent
}
switch vLog.Topics[0] {
case logDeposit:
case logWDelayerDeposit:
var deposit WDelayerEventDeposit
err := c.contractAbi.Unpack(&deposit, "Deposit", vLog.Data)
if err != nil {
@ -469,7 +470,7 @@ func (c *WDelayerClient) WDelayerEventsByBlock(blockNum int64) (*WDelayerEvents,
deposit.Token = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
wdelayerEvents.Deposit = append(wdelayerEvents.Deposit, deposit)
case logWithdraw:
case logWDelayerWithdraw:
var withdraw WDelayerEventWithdraw
err := c.contractAbi.Unpack(&withdraw, "Withdraw", vLog.Data)
if err != nil {
@ -479,11 +480,11 @@ func (c *WDelayerClient) WDelayerEventsByBlock(blockNum int64) (*WDelayerEvents,
withdraw.Owner = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
wdelayerEvents.Withdraw = append(wdelayerEvents.Withdraw, withdraw)
case logEmergencyModeEnabled:
case logWDelayerEmergencyModeEnabled:
var emergencyModeEnabled WDelayerEventEmergencyModeEnabled
wdelayerEvents.EmergencyModeEnabled = append(wdelayerEvents.EmergencyModeEnabled, emergencyModeEnabled)
case logNewWithdrawalDelay:
case logWDelayerNewWithdrawalDelay:
var withdrawalDelay WDelayerEventNewWithdrawalDelay
err := c.contractAbi.Unpack(&withdrawalDelay, "NewWithdrawalDelay", vLog.Data)
if err != nil {
@ -491,14 +492,18 @@ func (c *WDelayerClient) WDelayerEventsByBlock(blockNum int64) (*WDelayerEvents,
}
wdelayerEvents.NewWithdrawalDelay = append(wdelayerEvents.NewWithdrawalDelay, withdrawalDelay)
case logEscapeHatchWithdrawal:
case logWDelayerEscapeHatchWithdrawal:
var escapeHatchWithdrawal WDelayerEventEscapeHatchWithdrawal
err := c.contractAbi.Unpack(&escapeHatchWithdrawal, "EscapeHatchWithdrawal", vLog.Data)
if err != nil {
return nil, nil, err
}
escapeHatchWithdrawal.Who = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
escapeHatchWithdrawal.To = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
escapeHatchWithdrawal.Token = ethCommon.BytesToAddress(vLog.Topics[3].Bytes())
wdelayerEvents.EscapeHatchWithdrawal = append(wdelayerEvents.EscapeHatchWithdrawal, escapeHatchWithdrawal)
case logNewHermezKeeperAddress:
case logWDelayerNewHermezKeeperAddress:
var keeperAddress WDelayerEventNewHermezKeeperAddress
err := c.contractAbi.Unpack(&keeperAddress, "NewHermezKeeperAddress", vLog.Data)
if err != nil {
@ -506,7 +511,7 @@ func (c *WDelayerClient) WDelayerEventsByBlock(blockNum int64) (*WDelayerEvents,
}
wdelayerEvents.NewHermezKeeperAddress = append(wdelayerEvents.NewHermezKeeperAddress, keeperAddress)
case logNewWhiteHackGroupAddress:
case logWDelayerNewWhiteHackGroupAddress:
var whiteHackGroupAddress WDelayerEventNewWhiteHackGroupAddress
err := c.contractAbi.Unpack(&whiteHackGroupAddress, "NewWhiteHackGroupAddress", vLog.Data)
if err != nil {
@ -514,7 +519,7 @@ func (c *WDelayerClient) WDelayerEventsByBlock(blockNum int64) (*WDelayerEvents,
}
wdelayerEvents.NewWhiteHackGroupAddress = append(wdelayerEvents.NewWhiteHackGroupAddress, whiteHackGroupAddress)
case logNewHermezGovernanceDAOAddress:
case logWDelayerNewHermezGovernanceDAOAddress:
var governanceDAOAddress WDelayerEventNewHermezGovernanceDAOAddress
err := c.contractAbi.Unpack(&governanceDAOAddress, "NewHermezGovernanceDAOAddress", vLog.Data)
if err != nil {

+ 64
- 46
eth/wdelayer_test.go

@ -10,6 +10,7 @@ import (
)
var wdelayerClient *WDelayerClient
var wdelayerClientTest *WDelayerClient
// var wdelayerClientKep *WDelayerClient
@ -18,117 +19,117 @@ var newWithdrawalDelay = big.NewInt(79)
var maxEmergencyModeTime = time.Hour * 24 * 7 * 26
func TestWDelayerGetHermezGovernanceDAOAddress(t *testing.T) {
governanceAddress, err := wdelayerClient.WDelayerGetHermezGovernanceDAOAddress()
governanceAddress, err := wdelayerClientTest.WDelayerGetHermezGovernanceDAOAddress()
require.Nil(t, err)
assert.Equal(t, &hermezGovernanceDAOAddressConst, governanceAddress)
}
func TestWDelayerSetHermezGovernanceDAOAddress(t *testing.T) {
wdelayerClientGov, err := NewWDelayerClient(ethereumClientGovDAO, wdelayerAddressConst)
wdelayerClientGov, err := NewWDelayerClient(ethereumClientGovDAO, wdelayerTestAddressConst)
require.Nil(t, err)
_, err = wdelayerClientGov.WDelayerSetHermezGovernanceDAOAddress(auxAddressConst)
require.Nil(t, err)
auxAddress, err := wdelayerClient.WDelayerGetHermezGovernanceDAOAddress()
auxAddress, err := wdelayerClientTest.WDelayerGetHermezGovernanceDAOAddress()
require.Nil(t, err)
assert.Equal(t, &auxAddressConst, auxAddress)
currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
currentBlockNum, _ := wdelayerClientTest.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
assert.Equal(t, auxAddressConst, wdelayerEvents.NewHermezGovernanceDAOAddress[0].NewHermezGovernanceDAOAddress)
wdelayerClientAux, err := NewWDelayerClient(ethereumClientAux, wdelayerAddressConst)
wdelayerClientAux, err := NewWDelayerClient(ethereumClientAux, wdelayerTestAddressConst)
require.Nil(t, err)
_, err = wdelayerClientAux.WDelayerSetHermezGovernanceDAOAddress(hermezGovernanceDAOAddressConst)
require.Nil(t, err)
}
func TestWDelayerGetHermezKeeperAddress(t *testing.T) {
keeperAddress, err := wdelayerClient.WDelayerGetHermezKeeperAddress()
keeperAddress, err := wdelayerClientTest.WDelayerGetHermezKeeperAddress()
require.Nil(t, err)
assert.Equal(t, &hermezKeeperAddressConst, keeperAddress)
}
func TestWDelayerSetHermezKeeperAddress(t *testing.T) {
wdelayerClientKep, err := NewWDelayerClient(ethereumClientKep, wdelayerAddressConst)
wdelayerClientKep, err := NewWDelayerClient(ethereumClientKep, wdelayerTestAddressConst)
require.Nil(t, err)
_, err = wdelayerClientKep.WDelayerSetHermezKeeperAddress(auxAddressConst)
require.Nil(t, err)
auxAddress, err := wdelayerClient.WDelayerGetHermezKeeperAddress()
auxAddress, err := wdelayerClientTest.WDelayerGetHermezKeeperAddress()
require.Nil(t, err)
assert.Equal(t, &auxAddressConst, auxAddress)
currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
currentBlockNum, _ := wdelayerClientTest.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
assert.Equal(t, auxAddressConst, wdelayerEvents.NewHermezKeeperAddress[0].NewHermezKeeperAddress)
wdelayerClientAux, err := NewWDelayerClient(ethereumClientAux, wdelayerAddressConst)
wdelayerClientAux, err := NewWDelayerClient(ethereumClientAux, wdelayerTestAddressConst)
require.Nil(t, err)
_, err = wdelayerClientAux.WDelayerSetHermezKeeperAddress(hermezKeeperAddressConst)
require.Nil(t, err)
}
func TestWDelayerGetWhiteHackGroupAddress(t *testing.T) {
whiteHackGroupAddress, err := wdelayerClient.WDelayerGetWhiteHackGroupAddress()
whiteHackGroupAddress, err := wdelayerClientTest.WDelayerGetWhiteHackGroupAddress()
require.Nil(t, err)
assert.Equal(t, &whiteHackGroupAddressConst, whiteHackGroupAddress)
}
func TestWDelayerSetWhiteHackGroupAddress(t *testing.T) {
wdelayerClientWhite, err := NewWDelayerClient(ethereumClientWhite, wdelayerAddressConst)
wdelayerClientWhite, err := NewWDelayerClient(ethereumClientWhite, wdelayerTestAddressConst)
require.Nil(t, err)
_, err = wdelayerClientWhite.WDelayerSetWhiteHackGroupAddress(auxAddressConst)
require.Nil(t, err)
auxAddress, err := wdelayerClient.WDelayerGetWhiteHackGroupAddress()
auxAddress, err := wdelayerClientTest.WDelayerGetWhiteHackGroupAddress()
require.Nil(t, err)
assert.Equal(t, &auxAddressConst, auxAddress)
currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
currentBlockNum, _ := wdelayerClientTest.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
assert.Equal(t, auxAddressConst, wdelayerEvents.NewWhiteHackGroupAddress[0].NewWhiteHackGroupAddress)
wdelayerClientAux, err := NewWDelayerClient(ethereumClientAux, wdelayerAddressConst)
wdelayerClientAux, err := NewWDelayerClient(ethereumClientAux, wdelayerTestAddressConst)
require.Nil(t, err)
_, err = wdelayerClientAux.WDelayerSetWhiteHackGroupAddress(whiteHackGroupAddressConst)
require.Nil(t, err)
}
func TestWDelayerIsEmergencyMode(t *testing.T) {
emergencyMode, err := wdelayerClient.WDelayerIsEmergencyMode()
emergencyMode, err := wdelayerClientTest.WDelayerIsEmergencyMode()
require.Nil(t, err)
assert.Equal(t, false, emergencyMode)
}
func TestWDelayerGetWithdrawalDelay(t *testing.T) {
withdrawalDelay, err := wdelayerClient.WDelayerGetWithdrawalDelay()
withdrawalDelay, err := wdelayerClientTest.WDelayerGetWithdrawalDelay()
require.Nil(t, err)
assert.Equal(t, initWithdrawalDelay, withdrawalDelay)
}
func TestWDelayerChangeWithdrawalDelay(t *testing.T) {
wdelayerClientKep, err := NewWDelayerClient(ethereumClientKep, wdelayerAddressConst)
wdelayerClientKep, err := NewWDelayerClient(ethereumClientKep, wdelayerTestAddressConst)
require.Nil(t, err)
_, err = wdelayerClientKep.WDelayerChangeWithdrawalDelay(newWithdrawalDelay.Uint64())
require.Nil(t, err)
withdrawalDelay, err := wdelayerClient.WDelayerGetWithdrawalDelay()
withdrawalDelay, err := wdelayerClientTest.WDelayerGetWithdrawalDelay()
require.Nil(t, err)
assert.Equal(t, newWithdrawalDelay, withdrawalDelay)
currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
currentBlockNum, _ := wdelayerClientTest.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
assert.Equal(t, newWithdrawalDelay.Uint64(), wdelayerEvents.NewWithdrawalDelay[0].WithdrawalDelay)
}
func TestWDelayerDeposit(t *testing.T) {
amount := new(big.Int)
amount.SetString("1100000000000000000", 10)
wdelayerClientHermez, err := NewWDelayerClient(ethereumClientHermez, wdelayerAddressConst)
wdelayerClientHermez, err := NewWDelayerClient(ethereumClientHermez, wdelayerTestAddressConst)
require.Nil(t, err)
_, err = wdelayerClientHermez.WDelayerDeposit(auxAddressConst, tokenHezAddressConst, amount)
_, err = wdelayerClientHermez.WDelayerDeposit(auxAddressConst, tokenERC20AddressConst, amount)
require.Nil(t, err)
currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
currentBlockNum, _ := wdelayerClientTest.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
assert.Equal(t, amount, wdelayerEvents.Deposit[0].Amount)
assert.Equal(t, auxAddressConst, wdelayerEvents.Deposit[0].Owner)
assert.Equal(t, tokenHezAddressConst, wdelayerEvents.Deposit[0].Token)
assert.Equal(t, tokenERC20AddressConst, wdelayerEvents.Deposit[0].Token)
}
func TestWDelayerDepositInfo(t *testing.T) {
amount := new(big.Int)
amount.SetString("1100000000000000000", 10)
state, err := wdelayerClient.WDelayerDepositInfo(auxAddressConst, tokenHezAddressConst)
state, err := wdelayerClientTest.WDelayerDepositInfo(auxAddressConst, tokenERC20AddressConst)
require.Nil(t, err)
assert.Equal(t, state.Amount, amount)
}
@ -136,34 +137,48 @@ func TestWDelayerDepositInfo(t *testing.T) {
func TestWDelayerWithdrawal(t *testing.T) {
amount := new(big.Int)
amount.SetString("1100000000000000000", 10)
_, err := wdelayerClient.WDelayerWithdrawal(auxAddressConst, tokenHezAddressConst)
_, err := wdelayerClientTest.WDelayerWithdrawal(auxAddressConst, tokenERC20AddressConst)
require.Contains(t, err.Error(), "Withdrawal not allowed yet")
addBlocks(newWithdrawalDelay.Int64(), ethClientDialURL)
_, err = wdelayerClient.WDelayerWithdrawal(auxAddressConst, tokenHezAddressConst)
_, err = wdelayerClientTest.WDelayerWithdrawal(auxAddressConst, tokenERC20AddressConst)
require.Nil(t, err)
currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
currentBlockNum, _ := wdelayerClientTest.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
assert.Equal(t, amount, wdelayerEvents.Withdraw[0].Amount)
assert.Equal(t, auxAddressConst, wdelayerEvents.Withdraw[0].Owner)
assert.Equal(t, tokenHezAddressConst, wdelayerEvents.Withdraw[0].Token)
assert.Equal(t, tokenERC20AddressConst, wdelayerEvents.Withdraw[0].Token)
}
func TestWDelayerSecondDeposit(t *testing.T) {
amount := new(big.Int)
amount.SetString("1100000000000000000", 10)
wdelayerClientHermez, err := NewWDelayerClient(ethereumClientHermez, wdelayerTestAddressConst)
require.Nil(t, err)
_, err = wdelayerClientHermez.WDelayerDeposit(auxAddressConst, tokenERC20AddressConst, amount)
require.Nil(t, err)
currentBlockNum, _ := wdelayerClientTest.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
assert.Equal(t, amount, wdelayerEvents.Deposit[0].Amount)
assert.Equal(t, auxAddressConst, wdelayerEvents.Deposit[0].Owner)
assert.Equal(t, tokenERC20AddressConst, wdelayerEvents.Deposit[0].Token)
}
func TestWDelayerEnableEmergencyMode(t *testing.T) {
wdelayerClientKep, err := NewWDelayerClient(ethereumClientKep, wdelayerAddressConst)
wdelayerClientKep, err := NewWDelayerClient(ethereumClientKep, wdelayerTestAddressConst)
require.Nil(t, err)
_, err = wdelayerClientKep.WDelayerEnableEmergencyMode()
require.Nil(t, err)
emergencyMode, err := wdelayerClient.WDelayerIsEmergencyMode()
emergencyMode, err := wdelayerClientTest.WDelayerIsEmergencyMode()
require.Nil(t, err)
assert.Equal(t, true, emergencyMode)
currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
currentBlockNum, _ := wdelayerClientTest.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
auxEvent := new(WDelayerEventEmergencyModeEnabled)
assert.Equal(t, auxEvent, &wdelayerEvents.EmergencyModeEnabled[0])
}
func TestWDelayerGetEmergencyModeStartingTime(t *testing.T) {
emergencyModeStartingTime, err := wdelayerClient.WDelayerGetEmergencyModeStartingTime()
emergencyModeStartingTime, err := wdelayerClientTest.WDelayerGetEmergencyModeStartingTime()
require.Nil(t, err)
// `emergencyModeStartingTime` is initialized to 0 in the smart
// contract construction. Since we called WDelayerEnableEmergencyMode
@ -173,17 +188,20 @@ func TestWDelayerGetEmergencyModeStartingTime(t *testing.T) {
}
func TestWDelayerEscapeHatchWithdrawal(t *testing.T) {
wdelayerClientWhite, err := NewWDelayerClient(ethereumClientWhite, wdelayerAddressConst)
amount := new(big.Int)
amount.SetString("10000000000000000", 10)
wdelayerClientWhite, err := NewWDelayerClient(ethereumClientWhite, wdelayerTestAddressConst)
require.Nil(t, err)
_, err = wdelayerClientWhite.WDelayerEscapeHatchWithdrawal(governanceAddressConst, tokenHezAddressConst)
_, err = wdelayerClientWhite.WDelayerEscapeHatchWithdrawal(governanceAddressConst, tokenERC20AddressConst, amount)
require.Contains(t, err.Error(), "NO MAX_EMERGENCY_MODE_TIME")
seconds := maxEmergencyModeTime.Seconds()
addTime(seconds, ethClientDialURL)
_, err = wdelayerClientWhite.WDelayerEscapeHatchWithdrawal(governanceAddressConst, tokenHezAddressConst)
_, err = wdelayerClientWhite.WDelayerEscapeHatchWithdrawal(governanceAddressConst, tokenERC20AddressConst, amount)
require.Nil(t, err)
currentBlockNum, _ := wdelayerClient.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClient.WDelayerEventsByBlock(currentBlockNum)
assert.Equal(t, tokenHezAddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].Token)
currentBlockNum, _ := wdelayerClientTest.client.EthCurrentBlock()
wdelayerEvents, _, _ := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
assert.Equal(t, tokenERC20AddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].Token)
assert.Equal(t, governanceAddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].To)
assert.Equal(t, whiteHackGroupAddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].Who)
assert.Equal(t, amount, wdelayerEvents.EscapeHatchWithdrawal[0].Amount)
}

Loading…
Cancel
Save