From b9936f856427ef2268fb716b3ce7986b32516f46 Mon Sep 17 00:00:00 2001 From: a_bennassar Date: Fri, 24 Jul 2020 13:04:17 +0200 Subject: [PATCH 1/2] Initial dract of the common structs --- common/account.go | 18 ++++++++++++++++++ common/batch.go | 24 ++++++++++++++++++++++++ common/bid.go | 13 +++++++++++++ common/fee.go | 36 ++++++++++++++++++++++++++++++++++++ common/hash.go | 4 ++++ common/l1tx.go | 15 +++++++++++++++ common/l2tx.go | 12 ++++++++++++ common/operator.go | 14 ++++++++++++++ common/pohstate.go | 20 ++++++++++++++++++++ common/pooll2tx.go | 34 ++++++++++++++++++++++++++++++++++ common/rollupstate.go | 21 +++++++++++++++++++++ common/slot.go | 12 ++++++++++++ common/token.go | 25 +++++++++++++++++++++++++ common/tx.go | 42 ++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 ++- go.sum | 22 ++++++++++++++++++---- 16 files changed, 310 insertions(+), 5 deletions(-) create mode 100644 common/account.go create mode 100644 common/batch.go create mode 100644 common/bid.go create mode 100644 common/fee.go create mode 100644 common/hash.go create mode 100644 common/l1tx.go create mode 100644 common/l2tx.go create mode 100644 common/operator.go create mode 100644 common/pohstate.go create mode 100644 common/pooll2tx.go create mode 100644 common/rollupstate.go create mode 100644 common/slot.go create mode 100644 common/token.go create mode 100644 common/tx.go diff --git a/common/account.go b/common/account.go new file mode 100644 index 0000000..248e463 --- /dev/null +++ b/common/account.go @@ -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 +} diff --git a/common/batch.go b/common/batch.go new file mode 100644 index 0000000..f8af15a --- /dev/null +++ b/common/batch.go @@ -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 diff --git a/common/bid.go b/common/bid.go new file mode 100644 index 0000000..0ea8551 --- /dev/null +++ b/common/bid.go @@ -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 +} diff --git a/common/fee.go b/common/fee.go new file mode 100644 index 0000000..a4d5914 --- /dev/null +++ b/common/fee.go @@ -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, +} diff --git a/common/hash.go b/common/hash.go new file mode 100644 index 0000000..3854ff7 --- /dev/null +++ b/common/hash.go @@ -0,0 +1,4 @@ +package common + +// Hash is the used hash for Hermez network +type Hash []byte diff --git a/common/l1tx.go b/common/l1tx.go new file mode 100644 index 0000000..d406aa5 --- /dev/null +++ b/common/l1tx.go @@ -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 +} diff --git a/common/l2tx.go b/common/l2tx.go new file mode 100644 index 0000000..d6e54bf --- /dev/null +++ b/common/l2tx.go @@ -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 +} diff --git a/common/operator.go b/common/operator.go new file mode 100644 index 0000000..06750d9 --- /dev/null +++ b/common/operator.go @@ -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 +} diff --git a/common/pohstate.go b/common/pohstate.go new file mode 100644 index 0000000..6a14f5d --- /dev/null +++ b/common/pohstate.go @@ -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 +} diff --git a/common/pooll2tx.go b/common/pooll2tx.go new file mode 100644 index 0000000..d3b534d --- /dev/null +++ b/common/pooll2tx.go @@ -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" +) diff --git a/common/rollupstate.go b/common/rollupstate.go new file mode 100644 index 0000000..8ad1930 --- /dev/null +++ b/common/rollupstate.go @@ -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 +} diff --git a/common/slot.go b/common/slot.go new file mode 100644 index 0000000..e750c46 --- /dev/null +++ b/common/slot.go @@ -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 diff --git a/common/token.go b/common/token.go new file mode 100644 index 0000000..8f0bbbd --- /dev/null +++ b/common/token.go @@ -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 diff --git a/common/tx.go b/common/tx.go new file mode 100644 index 0000000..98d4fb9 --- /dev/null +++ b/common/tx.go @@ -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" +) diff --git a/go.mod b/go.mod index cc6cdfb..d9a0537 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,6 @@ go 1.14 require ( github.com/ethereum/go-ethereum v1.9.17 - github.com/stretchr/testify v1.6.1 + github.com/iden3/go-iden3-crypto v0.0.5 + github.com/stretchr/testify v1.6.1 // indirect ) diff --git a/go.sum b/go.sum index 6abdc9d..f61b359 100644 --- a/go.sum +++ b/go.sum @@ -13,7 +13,9 @@ github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6L github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/OneOfOne/xxhash v1.2.5/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q= github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= +github.com/VictoriaMetrics/fastcache v1.5.3/go.mod h1:+jv9Ckb+za/P1ZRg/sulP5Ni1v49daAVERr0H3CuscE= github.com/VictoriaMetrics/fastcache v1.5.7/go.mod h1:ptDBkNMQI4RtmVo8VS/XwRY6RoTu1dAWCbrk+6WsEM8= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= @@ -21,15 +23,18 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax github.com/aristanetworks/goarista v0.0.0-20170210015632-ea17b1a17847/go.mod h1:D/tb0zPVXnP7fmsLZjtdUhSsumbK/ij54UXjjVgMGxQ= github.com/aws/aws-sdk-go v1.25.48/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/btcsuite/btcd v0.0.0-20171128150713-2e60448ffcc6 h1:Eey/GGQ/E5Xp1P2Lyx1qj007hLZfbi0+CoVeJruGCtI= github.com/btcsuite/btcd v0.0.0-20171128150713-2e60448ffcc6/go.mod h1:Dmm/EzmjnCiweXmzRIAiUWCInVmPgjkzgv5k4tVyXiQ= github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/cespare/xxhash/v2 v2.0.1-0.20190104013014-3767db7a7e18/go.mod h1:HD5P3vAIAh+Y2GAxg0PrPN1P8WkepXGpjbUPDHJqqKM= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cloudflare/cloudflare-go v0.10.2-0.20190916151808-a80f83b9add9/go.mod h1:1MxXX1Ux4x6mqPmjkUgTP1CdXIBXKX7T+Jk9Gxrmx+U= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dchest/blake512 v1.0.0 h1:oDFEQFIqFSeuA34xLtXZ/rWxCXdSjirjzPhey5EUvmA= +github.com/dchest/blake512 v1.0.0/go.mod h1:FV1x7xPPLWukZlpDpWQ88rF/SFwZ5qbskrzhLMB92JI= github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= @@ -37,6 +42,8 @@ github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55k github.com/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/dop251/goja v0.0.0-20200219165308-d1232e640a87/go.mod h1:Mw6PkjjMXWbTj+nnj4s3QPXq1jaT0s5pC0iFD4+BOAA= github.com/edsrzf/mmap-go v0.0.0-20160512033002-935e0e8a636c/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= +github.com/elastic/gosigar v0.8.1-0.20180330100440-37f05ff46ffa/go.mod h1:cdorVVzy1fhmEqmtgqkoE3bYtCfSCkVyjTyCIo22xvs= +github.com/ethereum/go-ethereum v1.9.12/go.mod h1:PvsVkQmhZFx92Y+h2ylythYlheEDt/uBgFbl61Js/jo= github.com/ethereum/go-ethereum v1.9.17 h1:2D02O8KcoyQHxfizvMi0vGXXzFIkQTMeKXwt0+4SYEA= github.com/ethereum/go-ethereum v1.9.17/go.mod h1:kihoiSg74VC4dZAXMkmoWp70oQabz48BJg1tuzricFc= github.com/fatih/color v1.3.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= @@ -56,11 +63,15 @@ github.com/golang/snappy v0.0.2-0.20200707131729-196ae77b8a26/go.mod h1:/XxbfmMg github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/gorilla/websocket v1.4.1-0.20190629185528-ae1634f6a989/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/graph-gophers/graphql-go v0.0.0-20191115155744-f33e81362277/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc= +github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/holiman/uint256 v1.1.0/go.mod h1:y4ga/t+u+Xwd7CpDgZESaRcWy0I7XMlTMA25ApIH5Jw= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/huin/goupnp v0.0.0-20161224104101-679507af18f3/go.mod h1:MZ2ZmwcBpvOoJ22IJsc7va19ZwoheaBk43rKg12SKag= github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7Bnc= github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o= +github.com/iden3/go-iden3-crypto v0.0.5 h1:inCSm5a+ry+nbpVTL/9+m6UcIwSv6nhUm0tnIxEbcps= +github.com/iden3/go-iden3-crypto v0.0.5/go.mod h1:XKw1oDwYn2CIxKOtr7m/mL5jMn4mLOxAxtZBRxQBev8= github.com/influxdata/influxdb v1.2.3-0.20180221223340-01288bdb0883/go.mod h1:qZna6X/4elxqT3yI9iZYdZrWWdeFOOprn86kgg4+IzY= github.com/jackpal/go-nat-pmp v1.0.2-0.20160603034137-1fa385a6f458/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= @@ -91,7 +102,6 @@ github.com/pborman/uuid v0.0.0-20170112150404-1b00554d8222/go.mod h1:VyrYX9gd7ir github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= @@ -105,6 +115,7 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD github.com/shirou/gopsutil v2.20.5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spaolacci/murmur3 v1.0.1-0.20190317074736-539464a789e9/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q= github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570/go.mod h1:8OR4w3TdeIHIh1g6EMY5p0gVNOovcWC+1vpc7naMuAw= github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3/go.mod h1:hpGUWaI9xL8pRQCTXQgocU38Qw1g0Us7n5PxxTwTCYU= @@ -112,18 +123,19 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d/go.mod h1:9OrXJhf154huy1nPWmuSrkgjPUtUNhA+Zmy+6AESzuA= github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208/go.mod h1:IotVbo4F+mw0EzQ08zFqg7pK3FebNXpaMsRy2RT+Ees= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181011144130-49bb7cea24b1/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -131,6 +143,8 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= @@ -141,11 +155,11 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c= +gopkg.in/olebedev/go-duktape.v3 v3.0.0-20190213234257-ec84240a7772/go.mod h1:uAJfkITjFhyEEuUfm7bsmCZRbW5WRq8s9EY8HZ6hCns= gopkg.in/olebedev/go-duktape.v3 v3.0.0-20200619000410-60c24ae608a6/go.mod h1:uAJfkITjFhyEEuUfm7bsmCZRbW5WRq8s9EY8HZ6hCns= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/urfave/cli.v1 v1.20.0/go.mod h1:vuBzUtMdQeixQj8LVd+/98pzhxNGQoyuPBlsXHOQNO0= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= From 1d8f9411f7c8e7016d49230f9641864853856bab Mon Sep 17 00:00:00 2001 From: a_bennassar Date: Tue, 28 Jul 2020 16:47:37 +0200 Subject: [PATCH 2/2] Review changes and operator -> coordinator --- common/account.go | 2 +- common/batch.go | 5 +---- common/bid.go | 6 +++--- common/coordinator.go | 18 ++++++++++++++++++ common/fee.go | 21 ++------------------- common/l1tx.go | 15 ++++++++++----- common/l2tx.go | 3 ++- common/operator.go | 14 -------------- common/pohstate.go | 8 ++++---- common/pooll2tx.go | 18 +++++++++--------- common/rollupstate.go | 8 ++++---- common/slot.go | 4 ++-- common/tx.go | 24 ++++++++++++------------ 13 files changed, 68 insertions(+), 78 deletions(-) create mode 100644 common/coordinator.go delete mode 100644 common/operator.go diff --git a/common/account.go b/common/account.go index 248e463..652a4ac 100644 --- a/common/account.go +++ b/common/account.go @@ -14,5 +14,5 @@ type Account struct { 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 + PublicKey babyjub.PublicKey } diff --git a/common/batch.go b/common/batch.go index f8af15a..c9f94a2 100644 --- a/common/batch.go +++ b/common/batch.go @@ -1,8 +1,6 @@ package common import ( - "time" - eth "github.com/ethereum/go-ethereum/common" ) @@ -11,8 +9,7 @@ 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 + EthBlockNum uint64 // Etherum block in which the batch is forged Forger eth.Address ExitRoot Hash OldRoot Hash diff --git a/common/bid.go b/common/bid.go index 0ea8551..3fcade0 100644 --- a/common/bid.go +++ b/common/bid.go @@ -7,7 +7,7 @@ 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 - InfoOperator Operator // Operaror bidder information - Amount *big.Int + SlotNum SlotNum // Slot in which the bid is done + InfoCoordinator Coordinator // Operaror bidder information + Amount *big.Int } diff --git a/common/coordinator.go b/common/coordinator.go new file mode 100644 index 0000000..73b714a --- /dev/null +++ b/common/coordinator.go @@ -0,0 +1,18 @@ +package common + +import ( + eth "github.com/ethereum/go-ethereum/common" +) + +// 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 +} + +// CoordinatorID is use to identify a Hermez coordinator +type CoordinatorID uint64 diff --git a/common/fee.go b/common/fee.go index a4d5914..da3314f 100644 --- a/common/fee.go +++ b/common/fee.go @@ -12,25 +12,8 @@ type RecommendedFee struct { CreatesAccountAndRegister float64 } -// FeeSelector is used to select a percentage from the FeePlan. Max value is 16 +// FeeSelector is used to select a percentage from the FeePlan. 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, -} +var FeePlan = [256]float64{} diff --git a/common/l1tx.go b/common/l1tx.go index d406aa5..8b27cbb 100644 --- a/common/l1tx.go +++ b/common/l1tx.go @@ -1,15 +1,20 @@ package common -import "math/big" +import ( + "math/big" + + eth "github.com/ethereum/go-ethereum/common" + "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 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 + PublicKey babyjub.PublicKey LoadAmount *big.Int // amount transfered from L1 -> L2 - Mined bool - BlockNum uint64 + EthBlockNum uint64 + EthTxHash eth.Hash + Position int // Position among all the L1Txs in that batch ToForgeL1TxsNumber uint32 } diff --git a/common/l2tx.go b/common/l2tx.go index d6e54bf..3f8c3a2 100644 --- a/common/l2tx.go +++ b/common/l2tx.go @@ -8,5 +8,6 @@ import ( 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 + BatchNum BatchNum // Batch in which the tx was forged + Position int // Position among all the L1Txs in that batch } diff --git a/common/operator.go b/common/operator.go deleted file mode 100644 index 06750d9..0000000 --- a/common/operator.go +++ /dev/null @@ -1,14 +0,0 @@ -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 -} diff --git a/common/pohstate.go b/common/pohstate.go index 6a14f5d..3886ed8 100644 --- a/common/pohstate.go +++ b/common/pohstate.go @@ -6,15 +6,15 @@ import ( 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 +// 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 operator is fully synched with the ¿PoH? smart contract + 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 operator can forge if the operator winner has not forge any block before + 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 operator has to bid to participate in a slot auction + 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 d3b534d..0b1d3e5 100644 --- a/common/pooll2tx.go +++ b/common/pooll2tx.go @@ -7,7 +7,7 @@ import ( "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 +// 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 Status PoolL2TxStatus @@ -23,12 +23,12 @@ type PoolL2Tx struct { 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" + // PoolL2TxStatusPending represents a valid L2Tx that hasn't started the forging process + PoolL2TxStatusPending PoolL2TxStatus = "Pending" + // PoolL2TxStatusForging represents a valid L2Tx that has started the forging process + PoolL2TxStatusForging PoolL2TxStatus = "Forging" + // PoolL2TxStatusForged represents a L2Tx that has already been forged + PoolL2TxStatusForged PoolL2TxStatus = "Forged" + // PoolL2TxStatusInvalid represents a L2Tx that has been invalidated + PoolL2TxStatusInvalid PoolL2TxStatus = "Invalid" ) diff --git a/common/rollupstate.go b/common/rollupstate.go index 8ad1930..1b4ff48 100644 --- a/common/rollupstate.go +++ b/common/rollupstate.go @@ -6,12 +6,12 @@ import ( eth "github.com/ethereum/go-ethereum/common" ) -// RollupState give information about the rollup, and the synchronization status between the operator and the smart contract +// 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 operator is fully synched with the rollup smart contract + 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 operator - LastBatchSynched BatchNum // last batch synchronized by the operator + 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 diff --git a/common/slot.go b/common/slot.go index e750c46..8ad22fc 100644 --- a/common/slot.go +++ b/common/slot.go @@ -4,8 +4,8 @@ package common // 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 + StartingBlock uint64 // Etherum block in which the slot starts + Forger Coordinator // Current Operaror winner information } // SlotNum identifies a slot diff --git a/common/tx.go b/common/tx.go index 98d4fb9..132fcfd 100644 --- a/common/tx.go +++ b/common/tx.go @@ -27,16 +27,16 @@ type TxID Hash // Hash is a guess 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" + // TxTypeExit represents L2->L1 token transfer. A leaf for this account appears in the exit tree of the block + TxTypeExit TxType = "Exit" + // TxTypeWithdrawn represents the balance that was moved from L2->L1 has been widthrawn from the smart contract + TxTypeWithdrawn TxType = "Withdrawn" + // TxTypeTransfer represents L2->L2 token transfer + TxTypeTransfer TxType = "Transfer" + // TxTypeDeposit represents L1->L2 transfer + 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" )