Initial dract of the common structs

This commit is contained in:
a_bennassar
2020-07-24 13:04:17 +02:00
parent ecf731982e
commit b9936f8564
16 changed files with 310 additions and 5 deletions

18
common/account.go Normal file
View File

@@ -0,0 +1,18 @@
package common
import (
"math/big"
eth "github.com/ethereum/go-ethereum/common"
"github.com/iden3/go-iden3-crypto/babyjub"
)
// Account is a struct that gives information of the holdings of an address for a specific token
type Account struct {
EthAddr eth.Address
TokenID TokenID // effective 32 bits
Idx uint32 // bits = SMT levels (SMT levels needs to be decided)
Nonce uint64 // effective 48 bits
Balance *big.Int // Up to 192 bits
Signature babyjub.PublicKey
}

24
common/batch.go Normal file
View File

@@ -0,0 +1,24 @@
package common
import (
"time"
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
BlockNum uint64 // Etherum block in which the batch is forged
Timestamp time.Time
Forger eth.Address
ExitRoot Hash
OldRoot Hash
NewRoot Hash
TotalAccounts uint64
}
// BatchNum identifies a batch
type BatchNum uint32

13
common/bid.go Normal file
View File

@@ -0,0 +1,13 @@
package common
import (
"math/big"
)
// 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
InfoOperator Operator // Operaror bidder information
Amount *big.Int
}

36
common/fee.go Normal file
View File

@@ -0,0 +1,36 @@
package common
// Fee is a type that represents the percentage of tokens that will be payed in a transaction
// to incentivaise the materialization of it
type Fee float64
// RecommendedFee is the recommended fee to pay in USD per transaction set by the coordinator
// according to the tx type (if the tx requires to create an account and register, only register or he account already esists)
type RecommendedFee struct {
ExistingAccount float64
CreatesAccount float64
CreatesAccountAndRegister float64
}
// FeeSelector is used to select a percentage from the FeePlan. Max value is 16
type FeeSelector uint8
// FeePlan represents the fee model, a position in the array indicates the percentage of tokens paid in concept of fee for a transaction
var FeePlan = [16]float64{
0,
.001,
.002,
.005,
.01,
.02,
.05,
.1,
.2,
.5,
1,
2,
5,
10,
20,
50,
}

4
common/hash.go Normal file
View File

@@ -0,0 +1,4 @@
package common
// Hash is the used hash for Hermez network
type Hash []byte

15
common/l1tx.go Normal file
View File

@@ -0,0 +1,15 @@
package common
import "math/big"
// L1Tx is a struct that represents an already forged L1 tx
// WARNING: this struct is very unclear and a complete guess
type L1Tx struct {
Tx
Ax *big.Int // Ax is the x coordinate of the BabyJubJub curve point
Ay *big.Int // Ay is the y coordinate of the BabyJubJub curve point
LoadAmount *big.Int // amount transfered from L1 -> L2
Mined bool
BlockNum uint64
ToForgeL1TxsNumber uint32
}

12
common/l2tx.go Normal file
View File

@@ -0,0 +1,12 @@
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, 0 means not forged
}

14
common/operator.go Normal file
View File

@@ -0,0 +1,14 @@
package common
import (
eth "github.com/ethereum/go-ethereum/common"
)
// Operator represents a Hermez network operator 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 Operator struct {
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 operators API
}

20
common/pohstate.go Normal file
View File

@@ -0,0 +1,20 @@
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 operator 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 operator 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 operator can forge if the operator 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 operator has to bid to participate in a slot auction
}

34
common/pooll2tx.go Normal file
View File

@@ -0,0 +1,34 @@
package common
import (
"time"
eth "github.com/ethereum/go-ethereum/common"
"github.com/iden3/go-iden3-crypto/babyjub"
)
// PoolL2Tx is a struct that represents a L2Tx sent by an account to the operator hat is waiting to be forged
type PoolL2Tx struct {
Tx
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
Signature babyjub.Signature // tx signature
}
// PoolL2TxStatus is a struct that represents the status of a L2 transaction
type PoolL2TxStatus string
const (
// Pending represents a valid L2Tx that hasn't started the forging process
Pending PoolL2TxStatus = "Pending"
// Forging represents a valid L2Tx that has started the forging process
Forging PoolL2TxStatus = "Forging"
// Forged represents a L2Tx that has already been forged
Forged PoolL2TxStatus = "Forged"
// Invalid represents a L2Tx that has been invalidated
Invalid PoolL2TxStatus = "Invalid"
)

21
common/rollupstate.go Normal file
View File

@@ -0,0 +1,21 @@
package common
import (
"math/big"
eth "github.com/ethereum/go-ethereum/common"
)
// RollupState give information about the rollup, and the synchronization status between the operator and the smart contract
type RollupState struct {
IsSynched bool // true if the operator 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 operator
LastBatchSynched BatchNum // last batch synchronized by the operator
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
}

12
common/slot.go Normal file
View File

@@ -0,0 +1,12 @@
package common
// 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
Forger Operator // Current Operaror winner information
}
// SlotNum identifies a slot
type SlotNum uint32

25
common/token.go Normal file
View File

@@ -0,0 +1,25 @@
package common
import (
"time"
eth "github.com/ethereum/go-ethereum/common"
)
// Token is a struct that represents an Etherum token that is supported in Hermez network
type Token struct {
ID TokenID
Addr eth.Address
Symbol string
Decimals uint64
}
// TokenInfo provides the price of the token in USD
type TokenInfo struct {
TokenID uint32
Value float64
LastUpdated time.Time
}
// TokenID is the unique identifier of the token, as set in the smart contract
type TokenID uint32 // current implementation supports up to 2^32 tokens

42
common/tx.go Normal file
View File

@@ -0,0 +1,42 @@
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 is the identifier of a Hermez network transaction
type TxID Hash // Hash is a guess
// TxType is a string that represents the type of a Hermez network transaction
type TxType string
const (
// Exit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block
Exit TxType = "Exit"
// Withdrawn represents the balance that was moved from L2->L1 has been widthrawn from the smart contract
Withdrawn TxType = "Withdrawn"
// Transfer represents L2->L2 token transfer
Transfer TxType = "Transfer"
// Deposit represents L1->L2 transfer
Deposit TxType = "Deposit"
// CreateAccountDeposit represents creation of a new leaf in the state tree (newAcconut) + L1->L2 transfer
CreateAccountDeposit TxType = "CreateAccountDeposit"
// CreateAccountDepositTransfer represents L1->L2 transfer + L2->L2 transfer
CreateAccountDepositTransfer TxType = "CreateAccountDepositTransfer"
)