Merge pull request #26 from hermeznetwork/feature/register-common-update

Update to follow DB spec
This commit is contained in:
Eduard S
2020-08-03 10:31:18 +02:00
committed by GitHub
16 changed files with 172 additions and 107 deletions

View File

@@ -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
}

View File

@@ -1,6 +1,8 @@
package common package common
import ( import (
"math/big"
eth "github.com/ethereum/go-ethereum/common" eth "github.com/ethereum/go-ethereum/common"
) )
@@ -9,12 +11,17 @@ type Batch struct {
BatchNum BatchNum BatchNum BatchNum
SlotNum SlotNum // Slot in which the batch is forged SlotNum SlotNum // Slot in which the batch is forged
EthTxHash eth.Hash EthTxHash eth.Hash
EthBlockNum uint64 // Etherum block in which the batch is forged EthBlockNum uint64 // Ethereum block in which the batch is forged
Forger eth.Address
ExitRoot Hash ExitRoot Hash
OldRoot Hash OldStateRoot Hash
NewRoot Hash NewStateRoot Hash
TotalAccounts uint64 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 // BatchNum identifies a batch

View File

@@ -5,9 +5,10 @@ import (
) )
// Bid is a struct that represents one bid in the PoH // 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 { type Bid struct {
SlotNum SlotNum // Slot in which the bid is done SlotNum SlotNum // Slot in which the bid is done
InfoCoordinator Coordinator // Operaror bidder information Coordinator // Coordinator bidder information
Amount *big.Int BidValue *big.Int
EthBlockNum uint64
Won bool // boolean flag that tells that this is the final winning bid of this SlotNum
} }

15
common/block.go Normal file
View File

@@ -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
}

View File

@@ -7,12 +7,8 @@ import (
// Coordinator represents a Hermez network coordinator who wins an auction for an specific slot // 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. // WARNING: this is strongly based on the previous implementation, once the new spec is done, this may change a lot.
type Coordinator struct { type Coordinator struct {
CoordinatorID CoordinatorID
Forger eth.Address // address of the forger Forger eth.Address // address of the forger
Beneficiary eth.Address // address of the beneficiary Beneficiary eth.Address // address of the beneficiary
Withdraw eth.Address // address of the withdraw Withdraw eth.Address // address of the withdraw
URL string // URL of the coordinators API URL string // URL of the coordinators API
} }
// CoordinatorID is use to identify a Hermez coordinator
type CoordinatorID uint64

View File

@@ -7,14 +7,17 @@ import (
"github.com/iden3/go-iden3-crypto/babyjub" "github.com/iden3/go-iden3-crypto/babyjub"
) )
// L1Tx is a struct that represents an already forged L1 tx // L1Tx is a struct that represents a L1 tx
// WARNING: this struct is very unclear and a complete guess
type L1Tx struct { type L1Tx struct {
Tx Tx
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 PublicKey babyjub.PublicKey
LoadAmount *big.Int // amount transfered from L1 -> L2 LoadAmount *big.Int // amount transfered from L1 -> L2
EthBlockNum uint64 EthBlockNum uint64 // Ethereum Block Number in which this L1Tx was added to the queue
EthTxHash eth.Hash EthTxHash eth.Hash // TxHash that added this L1Tx to the queue
Position int // Position among all the L1Txs in that batch Position int // Position among all the L1Txs in that batch
ToForgeL1TxsNumber uint32 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
} }

View File

@@ -1,13 +1,7 @@
package common package common
import (
"time"
)
// L2Tx is a struct that represents an already forged L2 tx // L2Tx is a struct that represents an already forged L2 tx
type L2Tx struct { type L2Tx struct {
Tx 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
} }

View File

@@ -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
}

View File

@@ -1,6 +1,7 @@
package common package common
import ( import (
"math/big"
"time" "time"
eth "github.com/ethereum/go-ethereum/common" 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 // PoolL2Tx is a struct that represents a L2Tx sent by an account to the coordinator hat is waiting to be forged
type PoolL2Tx struct { type PoolL2Tx struct {
Tx Tx
ToBJJ babyjub.PublicKey
Status PoolL2TxStatus Status PoolL2TxStatus
RqTxCompressedData []byte // 253 bits, optional for atomic txs RqTxCompressedData []byte // 253 bits, optional for atomic txs
RqToEthAddr eth.Address // optional for atomic txs RqTx RqTx
RqToBjj babyjub.PublicKey // optional for atomic txs Timestamp time.Time // time when added to the tx pool
RqFromeEthAddr eth.Address // optional for atomic txs
Received time.Time // time when added to the tx pool
Signature babyjub.Signature // tx signature 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 // PoolL2TxStatus is a struct that represents the status of a L2 transaction

View File

@@ -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
}

View File

@@ -1,12 +1,28 @@
package common package common
import (
"math/big"
)
// Slot represents a slot of the Hermez network // 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. // WARNING: this is strongly based on the previous implementation, once the new spec is done, this may change a lot.
type Slot struct { type Slot struct {
SlotNum SlotNum 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 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 // SlotNum identifies a slot
type SlotNum uint32 type SlotNum uint32

View File

@@ -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
}

16
common/syncstate.go Normal file
View File

@@ -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
}

View File

@@ -6,12 +6,15 @@ import (
eth "github.com/ethereum/go-ethereum/common" 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 { type Token struct {
ID TokenID TokenID TokenID
Addr eth.Address EthAddr eth.Address
Name string
Symbol string Symbol string
Decimals uint64 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 // TokenInfo provides the price of the token in USD

View File

@@ -2,22 +2,19 @@ package common
import ( import (
"math/big" "math/big"
eth "github.com/ethereum/go-ethereum/common"
) )
// Tx is a struct that represents a Hermez network transaction // Tx is a struct that represents a Hermez network transaction
type Tx struct { type Tx struct {
ID TxID TxID TxID
FromEthAddr eth.Address
ToEthAddr eth.Address
FromIdx uint32 FromIdx uint32
ToIdx uint32 ToIdx uint32
TokenID TokenID TokenID TokenID
Amount *big.Int Amount *big.Int
Nonce uint64 // effective 48 bits used Nonce uint64 // effective 48 bits used
FeeSelector FeeSelector Fee FeeSelector
Type TxType // optional, descrives which kind of tx it's 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 // TxID is the identifier of a Hermez network transaction
@@ -37,6 +34,16 @@ const (
TxTypeDeposit TxType = "Deposit" TxTypeDeposit TxType = "Deposit"
// TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer // TxTypeCreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit" TxTypeCreateAccountDeposit TxType = "CreateAccountDeposit"
// TxTypeCreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer // TxTypeCreateAccountDepositAndTransfer represents L1->L2 transfer + L2->L2 transfer
TxTypeCreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer" 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"
) )

View File

@@ -56,8 +56,8 @@ func (l2db *L2DB) AddTx(tx *common.PoolL2Tx) error {
return nil return nil
} }
// AddRegisterAuthorization inserts a register authorization into the DB // AddAccountCreationAuth inserts an account creation authorization into the DB
func (l2db *L2DB) AddRegisterAuthorization() error { // TODO: AddRegisterAuthorization(auth &common.RegisterAuthorization) func (l2db *L2DB) AddAccountCreationAuth(auth *common.AccountCreationAuth) error { // TODO: AddRegisterAuthorization(auth &common.RegisterAuthorization)
return nil return nil
} }
@@ -71,9 +71,9 @@ func (l2db *L2DB) GetPendingTxs() ([]common.PoolL2Tx, error) {
return nil, nil return nil, nil
} }
// GetRegisterAuthorization return the authorization to make registers of an Etherum address // GetAccountCreationAuth return the authorization to make registers of an Ethereum address
func (l2db *L2DB) GetRegisterAuthorization(ethAddr eth.Address) (int, error) { // TODO: int will be changed to *common.RegisterAuthorization func (l2db *L2DB) GetAccountCreationAuth(ethAddr eth.Address) (*common.AccountCreationAuth, error) {
return 0, nil return nil, nil
} }
// StartForging updates the state of the transactions that will begin the forging process. // StartForging updates the state of the transactions that will begin the forging process.