From 638defd0315b237b5fa50fb073cc1a4e2ffee0dc Mon Sep 17 00:00:00 2001 From: a_bennassar Date: Fri, 31 Jul 2020 15:55:09 +0200 Subject: [PATCH] Update to follow DB spec --- common/accountcreationauths.go | 16 ++++++++++++++++ common/batch.go | 25 +++++++++++++++--------- common/bid.go | 9 +++++---- common/block.go | 15 +++++++++++++++ common/coordinator.go | 12 ++++-------- common/l1tx.go | 19 ++++++++++-------- common/l2tx.go | 8 +------- common/pohstate.go | 20 ------------------- common/pooll2tx.go | 21 +++++++++++++++----- common/rollupstate.go | 21 -------------------- common/slot.go | 18 ++++++++++++++++- common/smartcontractparams.go | 21 ++++++++++++++++++++ common/syncstate.go | 16 ++++++++++++++++ common/token.go | 13 ++++++++----- common/tx.go | 35 ++++++++++++++++++++-------------- l2db/l2db.go | 10 +++++----- 16 files changed, 172 insertions(+), 107 deletions(-) create mode 100644 common/accountcreationauths.go create mode 100644 common/block.go delete mode 100644 common/pohstate.go delete mode 100644 common/rollupstate.go create mode 100644 common/smartcontractparams.go create mode 100644 common/syncstate.go diff --git a/common/accountcreationauths.go b/common/accountcreationauths.go new file mode 100644 index 0000000..53a8a29 --- /dev/null +++ b/common/accountcreationauths.go @@ -0,0 +1,16 @@ +package common + +import ( + "time" + + eth "github.com/ethereum/go-ethereum/common" + "github.com/iden3/go-iden3-crypto/babyjub" +) + +// AccountCreationAuth authorizations sent by users to the L2DB, to be used for account creations when necessary +type AccountCreationAuth struct { + Timestamp time.Time + EthAddr eth.Address + BJJ babyjub.PublicKey + Signature babyjub.Signature +} diff --git a/common/batch.go b/common/batch.go index c9f94a2..20e6d2f 100644 --- a/common/batch.go +++ b/common/batch.go @@ -1,20 +1,27 @@ package common import ( + "math/big" + eth "github.com/ethereum/go-ethereum/common" ) // Batch is a struct that represents Hermez network batch type Batch struct { - BatchNum BatchNum - SlotNum SlotNum // Slot in which the batch is forged - EthTxHash eth.Hash - EthBlockNum uint64 // Etherum block in which the batch is forged - Forger eth.Address - ExitRoot Hash - OldRoot Hash - NewRoot Hash - TotalAccounts uint64 + BatchNum BatchNum + SlotNum SlotNum // Slot in which the batch is forged + EthTxHash eth.Hash + EthBlockNum uint64 // Ethereum block in which the batch is forged + ExitRoot Hash + OldStateRoot Hash + NewStateRoot Hash + OldNumAccounts int + NewNumAccounts int + ToForgeL1TxsNum uint32 // optional, Only when the batch forges L1 txs. Identifier that corresponds to the group of L1 txs forged in the current batch. + ToForgeL1TxsHash eth.Hash // optional, Only when the batch forges L1 txs. Frozen from pendingL1TxsHash (which are the group of L1UserTxs), to be forged in ToForgeL1TxsNum + 1. + ForgedL1TxsHash eth.Hash // optional, Only when the batch forges L1 txs. This will be the Hash of the group of L1 txs (L1UserTxs + L1CoordinatorTx) forged in the current batch. + CollectedFees map[TokenID]*big.Int + ForgerAddr eth.Address // TODO: Should this be retrieved via slot reference? } // BatchNum identifies a batch diff --git a/common/bid.go b/common/bid.go index 3fcade0..ae17db7 100644 --- a/common/bid.go +++ b/common/bid.go @@ -5,9 +5,10 @@ import ( ) // Bid is a struct that represents one bid in the PoH -// WARNING: this is strongly based on the previous implementation, once the new spec is done, this may change a lot. type Bid struct { - SlotNum SlotNum // Slot in which the bid is done - InfoCoordinator Coordinator // Operaror bidder information - Amount *big.Int + SlotNum SlotNum // Slot in which the bid is done + Coordinator // Coordinator bidder information + BidValue *big.Int + EthBlockNum uint64 + Won bool // boolean flag that tells that this is the final winning bid of this SlotNum } diff --git a/common/block.go b/common/block.go new file mode 100644 index 0000000..6a8965b --- /dev/null +++ b/common/block.go @@ -0,0 +1,15 @@ +package common + +import ( + "time" + + eth "github.com/ethereum/go-ethereum/common" +) + +// Block represents of an Ethereum block +type Block struct { + EthBlockNum uint64 + Timestamp time.Time + Hash eth.Hash + PrevHash eth.Hash +} diff --git a/common/coordinator.go b/common/coordinator.go index 73b714a..7a5d9ff 100644 --- a/common/coordinator.go +++ b/common/coordinator.go @@ -7,12 +7,8 @@ import ( // Coordinator represents a Hermez network coordinator who wins an auction for an specific slot // WARNING: this is strongly based on the previous implementation, once the new spec is done, this may change a lot. type Coordinator struct { - CoordinatorID CoordinatorID - Forger eth.Address // address of the forger - Beneficiary eth.Address // address of the beneficiary - Withdraw eth.Address // address of the withdraw - URL string // URL of the coordinators API + Forger eth.Address // address of the forger + Beneficiary eth.Address // address of the beneficiary + Withdraw eth.Address // address of the withdraw + URL string // URL of the coordinators API } - -// CoordinatorID is use to identify a Hermez coordinator -type CoordinatorID uint64 diff --git a/common/l1tx.go b/common/l1tx.go index 8b27cbb..3324bb8 100644 --- a/common/l1tx.go +++ b/common/l1tx.go @@ -7,14 +7,17 @@ import ( "github.com/iden3/go-iden3-crypto/babyjub" ) -// L1Tx is a struct that represents an already forged L1 tx -// WARNING: this struct is very unclear and a complete guess +// L1Tx is a struct that represents a L1 tx type L1Tx struct { Tx - PublicKey babyjub.PublicKey - LoadAmount *big.Int // amount transfered from L1 -> L2 - EthBlockNum uint64 - EthTxHash eth.Hash - Position int // Position among all the L1Txs in that batch - ToForgeL1TxsNumber uint32 + UserOrigin bool // true if the tx was originated by a user, false if it was aoriginated by a coordinator. Note that this differ from the spec for implementation simplification purpposes + PublicKey babyjub.PublicKey + LoadAmount *big.Int // amount transfered from L1 -> L2 + EthBlockNum uint64 // Ethereum Block Number in which this L1Tx was added to the queue + EthTxHash eth.Hash // TxHash that added this L1Tx to the queue + Position int // Position among all the L1Txs in that batch + ToForgeL1TxsNum uint32 // toForgeL1TxsNum in which the tx was forged / will be forged + FromBJJ babyjub.PublicKey + CreateAccount bool // "from" + token ID is a new account + FromEthAddr eth.Address } diff --git a/common/l2tx.go b/common/l2tx.go index 3f8c3a2..127b152 100644 --- a/common/l2tx.go +++ b/common/l2tx.go @@ -1,13 +1,7 @@ package common -import ( - "time" -) - // L2Tx is a struct that represents an already forged L2 tx type L2Tx struct { Tx - Forged time.Time // time when received by the tx pool - BatchNum BatchNum // Batch in which the tx was forged - Position int // Position among all the L1Txs in that batch + Position int // Position among all the L1Txs in that batch } diff --git a/common/pohstate.go b/common/pohstate.go deleted file mode 100644 index 3886ed8..0000000 --- a/common/pohstate.go +++ /dev/null @@ -1,20 +0,0 @@ -package common - -import ( - "math/big" - - eth "github.com/ethereum/go-ethereum/common" -) - -// PoHState give information about the forging mechanism of the Hermez network, and the synchronization status between the coordinator and the smart contract -// WARNING: this is strongly based on the previous implementation, once the new spec is done, this may change a lot. -type PoHState struct { - IsSynched bool // true if the coordinator is fully synched with the ¿PoH? smart contract - SyncProgress float32 // percentage of synced progress with the ¿PoH? smart contract - CurrentSlot SlotNum // slot in which batches are being forged at the current time - ContractAddr eth.Address // Etherum address of the ¿PoH? smart contract - BlocksPerSlot uint16 // Slot duration measured in Etherum blocks - SlotDeadline uint16 // Time of the slot in which another coordinator can forge if the coordinator winner has not forge any block before - GenesisBlock uint64 // uint64 is a guess, Etherum block in which the ¿PoH? contract was deployed - MinBid *big.Int // Minimum amount that an coordinator has to bid to participate in a slot auction -} diff --git a/common/pooll2tx.go b/common/pooll2tx.go index 0b1d3e5..28671c9 100644 --- a/common/pooll2tx.go +++ b/common/pooll2tx.go @@ -1,6 +1,7 @@ package common import ( + "math/big" "time" eth "github.com/ethereum/go-ethereum/common" @@ -10,13 +11,23 @@ import ( // PoolL2Tx is a struct that represents a L2Tx sent by an account to the coordinator hat is waiting to be forged type PoolL2Tx struct { Tx + ToBJJ babyjub.PublicKey Status PoolL2TxStatus - RqTxCompressedData []byte // 253 bits, optional for atomic txs - RqToEthAddr eth.Address // optional for atomic txs - RqToBjj babyjub.PublicKey // optional for atomic txs - RqFromeEthAddr eth.Address // optional for atomic txs - Received time.Time // time when added to the tx pool + RqTxCompressedData []byte // 253 bits, optional for atomic txs + RqTx RqTx + Timestamp time.Time // time when added to the tx pool Signature babyjub.Signature // tx signature + ToEthAddr eth.Address +} + +// RqTx Transaction Data used to indicate that a transaction depends on another transaction +type RqTx struct { + FromEthAddr eth.Address + ToEthAddr eth.Address + TokenID TokenID + Amount *big.Int + FeeSelector FeeSelector + Nonce uint64 // effective 48 bits used } // PoolL2TxStatus is a struct that represents the status of a L2 transaction diff --git a/common/rollupstate.go b/common/rollupstate.go deleted file mode 100644 index 1b4ff48..0000000 --- a/common/rollupstate.go +++ /dev/null @@ -1,21 +0,0 @@ -package common - -import ( - "math/big" - - eth "github.com/ethereum/go-ethereum/common" -) - -// RollupState give information about the rollup, and the synchronization status between the coordinator and the smart contract -type RollupState struct { - IsSynched bool // true if the coordinator is fully synched with the rollup smart contract - SyncProgress float32 // percentage of synced progress with the rollup smart contract - LastBlockSynched uint64 // last Etherum block synchronized by the coordinator - LastBatchSynched BatchNum // last batch synchronized by the coordinator - FeeDeposit *big.Int // amount of eth (in wei) that has to be payed to do a deposit - FeeL1Tx *big.Int // amount of eth (in wei) that has to be payed to do a L1 tx - ContractAddr eth.Address // Etherum address of the rollup smart contract - MaxTx uint16 // Max amount of txs that can be added in a batch, either L1 or L2 - MaxL1Tx uint16 // Max amount of L1 txs that can be added in a batch - NLevels uint16 // Heigth of the SMT. This will determine the maximum number of accounts that can coexist in the Hermez network by 2^nLevels -} diff --git a/common/slot.go b/common/slot.go index 8ad22fc..b13695c 100644 --- a/common/slot.go +++ b/common/slot.go @@ -1,12 +1,28 @@ package common +import ( + "math/big" +) + // Slot represents a slot of the Hermez network // WARNING: this is strongly based on the previous implementation, once the new spec is done, this may change a lot. type Slot struct { SlotNum SlotNum - StartingBlock uint64 // Etherum block in which the slot starts + StartingBlock uint64 // Ethereum block in which the slot starts Forger Coordinator // Current Operaror winner information } +// SlotMinPrice is the policy of minimum prices for strt bidding in the slots +type SlotMinPrice struct { + EthBlockNum uint64 // Etherum block in which the min price was updated + MinPrices [6]big.Int +} + +// GetMinPrice returns the minimum bid to enter the auction for a specific slot +func (smp *SlotMinPrice) GetMinPrice(slotNum SlotNum) *big.Int { + // TODO + return nil +} + // SlotNum identifies a slot type SlotNum uint32 diff --git a/common/smartcontractparams.go b/common/smartcontractparams.go new file mode 100644 index 0000000..0faf00c --- /dev/null +++ b/common/smartcontractparams.go @@ -0,0 +1,21 @@ +package common + +import ( + "math/big" + + eth "github.com/ethereum/go-ethereum/common" +) + +// SmartContractParameters describes the constant values of the parameters of the Hermez smart contracts +// WARNING: not stable at all +type SmartContractParameters struct { + SlotDuration uint64 // number of ethereum blocks in a slot + Slot0BlockNum uint64 // ethereum block number of the first slot (slot 0) + MaxL1UserTxs uint64 // maximum number of L1UserTxs that can be queued for a single batch + FreeCoordinatorWait uint64 // if the winning coordinator doesn't forge during this number of blocks, anyone can forge + ContractAddr eth.Address // Ethereum address of the rollup smart contract + NLevels uint16 // Heigth of the SMT. This will determine the maximum number of accounts that can coexist in the Hermez network by 2^nLevels + MaxTxs uint16 // Max amount of txs that can be added in a batch, either L1 or L2 + FeeL1Tx *big.Int // amount of eth (in wei) that has to be payed to do a L1 tx + FeeDeposit *big.Int // amount of eth (in wei) that has to be payed to do a deposit +} diff --git a/common/syncstate.go b/common/syncstate.go new file mode 100644 index 0000000..5c5d5f4 --- /dev/null +++ b/common/syncstate.go @@ -0,0 +1,16 @@ +package common + +import ( + "time" +) + +// SyncronizerState describes the syncronization progress of the smart contracts +type SyncronizerState struct { + LastUpdate time.Time // last time this information was updated + CurrentBatchNum BatchNum // Last batch that was forged on the blockchain + CurrentBlockNum uint64 // Last block that was mined on Ethereum + CurrentToForgeL1TxsNum uint32 + LastSyncedBatchNum BatchNum // last batch synchronized by the coordinator + LastSyncedBlockNum uint64 // last Ethereum block synchronized by the coordinator + LastSyncedToForgeL1TxsNum uint32 +} diff --git a/common/token.go b/common/token.go index 8f0bbbd..62f0126 100644 --- a/common/token.go +++ b/common/token.go @@ -6,12 +6,15 @@ import ( eth "github.com/ethereum/go-ethereum/common" ) -// Token is a struct that represents an Etherum token that is supported in Hermez network +// Token is a struct that represents an Ethereum token that is supported in Hermez network type Token struct { - ID TokenID - Addr eth.Address - Symbol string - Decimals uint64 + TokenID TokenID + EthAddr eth.Address + Name string + Symbol string + Decimals uint64 + EthTxHash eth.Hash // Ethereum TxHash in which this token was registered + EthBlockNum uint64 // Ethereum block number in which this token was registered } // TokenInfo provides the price of the token in USD diff --git a/common/tx.go b/common/tx.go index 132fcfd..58b2e20 100644 --- a/common/tx.go +++ b/common/tx.go @@ -2,22 +2,19 @@ package common import ( "math/big" - - eth "github.com/ethereum/go-ethereum/common" ) // Tx is a struct that represents a Hermez network transaction type Tx struct { - ID TxID - FromEthAddr eth.Address - ToEthAddr eth.Address - FromIdx uint32 - ToIdx uint32 - TokenID TokenID - Amount *big.Int - Nonce uint64 // effective 48 bits used - FeeSelector FeeSelector - Type TxType // optional, descrives which kind of tx it's + TxID TxID + FromIdx uint32 + ToIdx uint32 + TokenID TokenID + Amount *big.Int + Nonce uint64 // effective 48 bits used + Fee FeeSelector + Type TxType // optional, descrives which kind of tx it's + BatchNum BatchNum // batchNum in which this tx was forged. Presence indicates "forged" state. } // TxID is the identifier of a Hermez network transaction @@ -37,6 +34,16 @@ const ( TxTypeDeposit TxType = "Deposit" // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit" - // TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer - TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer" + // TxTypeCreateAccountDepositAndTransfer represents L1->L2 transfer + L2->L2 transfer + TxTypeCreateAccountDepositAndTransfer TxType = "CreateAccountDepositAndTransfer" + // TxTypeDepositAndTransfer TBD + TxTypeDepositAndTransfer TxType = "TxTypeDepositAndTransfer" + // TxTypeForceTransfer TBD + TxTypeForceTransfer TxType = "TxTypeForceTransfer" + // TxTypeForceExit TBD + TxTypeForceExit TxType = "TxTypeForceExit" + // TxTypeTransferToEthAddr TBD + TxTypeTransferToEthAddr TxType = "TxTypeTransferToEthAddr" + // TxTypeTransferToBJJ TBD + TxTypeTransferToBJJ TxType = "TxTypeTransferToBJJ" ) diff --git a/l2db/l2db.go b/l2db/l2db.go index cbd54c4..c679648 100644 --- a/l2db/l2db.go +++ b/l2db/l2db.go @@ -56,8 +56,8 @@ func (l2db *L2DB) AddTx(tx *common.PoolL2Tx) error { return nil } -// AddRegisterAuthorization inserts a register authorization into the DB -func (l2db *L2DB) AddRegisterAuthorization() error { // TODO: AddRegisterAuthorization(auth &common.RegisterAuthorization) +// AddAccountCreationAuth inserts an account creation authorization into the DB +func (l2db *L2DB) AddAccountCreationAuth(auth *common.AccountCreationAuth) error { // TODO: AddRegisterAuthorization(auth &common.RegisterAuthorization) return nil } @@ -71,9 +71,9 @@ func (l2db *L2DB) GetPendingTxs() ([]common.PoolL2Tx, error) { return nil, nil } -// GetRegisterAuthorization return the authorization to make registers of an Etherum address -func (l2db *L2DB) GetRegisterAuthorization(ethAddr eth.Address) (int, error) { // TODO: int will be changed to *common.RegisterAuthorization - return 0, nil +// GetAccountCreationAuth return the authorization to make registers of an Ethereum address +func (l2db *L2DB) GetAccountCreationAuth(ethAddr eth.Address) (*common.AccountCreationAuth, error) { + return nil, nil } // StartForging updates the state of the transactions that will begin the forging process.