Browse Source

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

Update to follow DB spec
feature/sql-semaphore1
Eduard S 4 years ago
committed by GitHub
parent
commit
2f93848292
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 172 additions and 107 deletions
  1. +16
    -0
      common/accountcreationauths.go
  2. +16
    -9
      common/batch.go
  3. +5
    -4
      common/bid.go
  4. +15
    -0
      common/block.go
  5. +4
    -8
      common/coordinator.go
  6. +11
    -8
      common/l1tx.go
  7. +1
    -7
      common/l2tx.go
  8. +0
    -20
      common/pohstate.go
  9. +16
    -5
      common/pooll2tx.go
  10. +0
    -21
      common/rollupstate.go
  11. +17
    -1
      common/slot.go
  12. +21
    -0
      common/smartcontractparams.go
  13. +16
    -0
      common/syncstate.go
  14. +8
    -5
      common/token.go
  15. +21
    -14
      common/tx.go
  16. +5
    -5
      l2db/l2db.go

+ 16
- 0
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
}

+ 16
- 9
common/batch.go

@ -1,20 +1,27 @@
package common package common
import ( import (
"math/big"
eth "github.com/ethereum/go-ethereum/common" eth "github.com/ethereum/go-ethereum/common"
) )
// Batch is a struct that represents Hermez network batch // Batch is a struct that represents Hermez network batch
type Batch struct { 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 // BatchNum identifies a batch

+ 5
- 4
common/bid.go

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

+ 15
- 0
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
}

+ 4
- 8
common/coordinator.go

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

+ 11
- 8
common/l1tx.go

@ -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
// WARNING: this struct is very unclear and a complete guess
// L1Tx is a struct that represents a L1 tx
type L1Tx struct { type L1Tx struct {
Tx 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
} }

+ 1
- 7
common/l2tx.go

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

+ 0
- 20
common/pohstate.go

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

+ 16
- 5
common/pooll2tx.go

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

+ 0
- 21
common/rollupstate.go

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

+ 17
- 1
common/slot.go

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

+ 21
- 0
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
}

+ 16
- 0
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
}

+ 8
- 5
common/token.go

@ -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
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 // TokenInfo provides the price of the token in USD

+ 21
- 14
common/tx.go

@ -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
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 // 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
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"
) )

+ 5
- 5
l2db/l2db.go

@ -56,8 +56,8 @@ func (l2db *L2DB) AddTx(tx *common.PoolL2Tx) error {
return nil 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 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
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. // StartForging updates the state of the transactions that will begin the forging process.

Loading…
Cancel
Save