From 0c1355ca0532df95a652b9deb88e8eb5e1075502 Mon Sep 17 00:00:00 2001 From: Mikelle Date: Thu, 11 Nov 2021 14:48:19 +0300 Subject: [PATCH 1/2] adding interfaces for the aggregator and sequencer --- aggregator/aggregator.go | 39 ++++++++++++++++++++++++ aggregator/cmd/main.go | 5 ++++ aggregator/config.go | 4 +++ aggregator/go.mod | 5 ++++ aggregator/prover.go | 8 +++++ aggregator/syncronizer.go | 18 ++++++++++++ go.mod | 6 ++++ pool/pool.go | 40 +++++++++++++++++++++++++ pool/transaction.go | 16 ++++++++++ sequencer/cmd/main.go | 5 ++++ sequencer/config.go | 4 +++ sequencer/go.mod | 9 ++++++ sequencer/sequencer.go | 62 +++++++++++++++++++++++++++++++++++++++ sequencer/synchronizer.go | 16 ++++++++++ 14 files changed, 237 insertions(+) create mode 100644 aggregator/aggregator.go create mode 100644 aggregator/cmd/main.go create mode 100644 aggregator/config.go create mode 100644 aggregator/go.mod create mode 100644 aggregator/prover.go create mode 100644 aggregator/syncronizer.go create mode 100644 pool/pool.go create mode 100644 pool/transaction.go create mode 100644 sequencer/cmd/main.go create mode 100644 sequencer/config.go create mode 100644 sequencer/go.mod create mode 100644 sequencer/sequencer.go create mode 100644 sequencer/synchronizer.go diff --git a/aggregator/aggregator.go b/aggregator/aggregator.go new file mode 100644 index 0000000000..e37f30c014 --- /dev/null +++ b/aggregator/aggregator.go @@ -0,0 +1,39 @@ +package aggregator + +import "github.com/ethereum/go-ethereum/core/types" + +type Aggregator struct { + State state.State + BatchProcessor state.BatchProcessor + EthClient eth.Client + Synchronizer SynchronizerClient +} + +func NewAggregator(cfg Config) (Aggregator, error) { + state := state.NewState() + bp := state.NewBatchProcessor(cfg.StartingHash, cfg.WithProofCalulation) + ethClient := eth.NewClient() + synchronizerClient := NewSynchronizerClient() + return Aggregator{ + State: state, + BatchProcessor: bp, + EthClient: ethClient, + Synchronizer: synchronizerClient, + }, nil +} + +func (agr *Aggregator) generateAndSendProofs() { + // reads from batchesChan + // TODO: get txs from MT by batchNum + // check if it's profitable or not + // send proof + txs to the prover + // send proof + txs to the SC +} + +func (agr *Aggregator) isProfitable(txs []types.Transaction) bool { + // get strategy from the config and check +} + +func (agr *Aggregator) Run() { + +} diff --git a/aggregator/cmd/main.go b/aggregator/cmd/main.go new file mode 100644 index 0000000000..70dfb1fa35 --- /dev/null +++ b/aggregator/cmd/main.go @@ -0,0 +1,5 @@ +package cmd + +func main() { + +} diff --git a/aggregator/config.go b/aggregator/config.go new file mode 100644 index 0000000000..2873637675 --- /dev/null +++ b/aggregator/config.go @@ -0,0 +1,4 @@ +package aggregator + +type Config struct { +} diff --git a/aggregator/go.mod b/aggregator/go.mod new file mode 100644 index 0000000000..bbe1a78002 --- /dev/null +++ b/aggregator/go.mod @@ -0,0 +1,5 @@ +module aggregator + +go 1.16 + +require github.com/ethereum/go-ethereum v1.10.12 diff --git a/aggregator/prover.go b/aggregator/prover.go new file mode 100644 index 0000000000..da2361309b --- /dev/null +++ b/aggregator/prover.go @@ -0,0 +1,8 @@ +package aggregator + +type ProverClient struct { +} + +func NewProverClient() ProverClient { + return ProverClient{} +} diff --git a/aggregator/syncronizer.go b/aggregator/syncronizer.go new file mode 100644 index 0000000000..0f8ded49da --- /dev/null +++ b/aggregator/syncronizer.go @@ -0,0 +1,18 @@ +package aggregator + +import "math/big" + +type SyncEvent struct { + LastStateRoot *big.Int + // ??? which type proof will be? + Proof *big.Int +} + +type SynchronizerClient struct { + SyncEventChan chan SyncEvent +} + +func NewSynchronizerClient() SynchronizerClient { + syncEventChan := make(chan SyncEvent) + return SynchronizerClient{SyncEventChan: syncEventChan} +} diff --git a/go.mod b/go.mod index 8a61d95549..ff2972fb43 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,9 @@ module github.com/hermeznetwork/hermez-core go 1.16 + +require ( + github.com/ethereum/go-ethereum v1.10.12 + github.com/jmoiron/sqlx v1.3.4 + github.com/lib/pq v1.10.4 +) diff --git a/pool/pool.go b/pool/pool.go new file mode 100644 index 0000000000..d4fa07896b --- /dev/null +++ b/pool/pool.go @@ -0,0 +1,40 @@ +package pool + +import ( + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/jmoiron/sqlx" + //nolint:errcheck // driver for postgres DB + _ "github.com/lib/pq" +) + +type Pool struct { + db *sqlx.DB + ttl time.Duration + maxTxs uint32 +} + +func NewPool( + db *sqlx.DB, + ttl time.Duration, + maxTxs uint32, +) *Pool { + return &Pool{ + db: db, + ttl: ttl, + maxTxs: maxTxs, + } +} + +func (pool *Pool) GetPendingTxs() ([]Transaction, error) { + panic("not implemented") +} + +func (pool *Pool) UpdateTxState(hash common.Hash, newState TxState) error { + panic("not implemented") +} + +func (pool *Pool) CleanUpInvalidAndNonSelectedTx() error { + panic("not implemented") +} diff --git a/pool/transaction.go b/pool/transaction.go new file mode 100644 index 0000000000..d1e1a13b5d --- /dev/null +++ b/pool/transaction.go @@ -0,0 +1,16 @@ +package pool + +import "github.com/ethereum/go-ethereum/core/types" + +type TxState string + +const ( + TxStatePending TxState = "pending" + TxStateInvalid TxState = "invalid" + TxStateSelected TxState = "selected" +) + +type Transaction struct { + types.Transaction + state TxState +} diff --git a/sequencer/cmd/main.go b/sequencer/cmd/main.go new file mode 100644 index 0000000000..70dfb1fa35 --- /dev/null +++ b/sequencer/cmd/main.go @@ -0,0 +1,5 @@ +package cmd + +func main() { + +} diff --git a/sequencer/config.go b/sequencer/config.go new file mode 100644 index 0000000000..37cca5e952 --- /dev/null +++ b/sequencer/config.go @@ -0,0 +1,4 @@ +package sequencer + +type Config struct { +} diff --git a/sequencer/go.mod b/sequencer/go.mod new file mode 100644 index 0000000000..95b28b46a3 --- /dev/null +++ b/sequencer/go.mod @@ -0,0 +1,9 @@ +module github.com/hermeznetwork/hermez-core/sequencer + +go 1.16 + +require ( + github.com/ethereum/go-ethereum v1.10.12 + github.com/jmoiron/sqlx v1.3.4 + github.com/lib/pq v1.2.0 +) diff --git a/sequencer/sequencer.go b/sequencer/sequencer.go new file mode 100644 index 0000000000..a9613abe16 --- /dev/null +++ b/sequencer/sequencer.go @@ -0,0 +1,62 @@ +package sequencer + +import ( + "github.com/hermeznetwork/hermez-core/pool" + "github.com/jmoiron/sqlx" + "time" +) + +type Sequencer struct { + Pool pool.Pool + State state.State + BatchProcessor state.BatchProcessor + EthClient eth.Client + SynchronizerClient SynchronizerClient +} + +func NewSequencer(cfg Config) (Sequencer, error) { + db, err := sqlx.Connect("postgres", "") + if err != nil { + return Sequencer{}, err + } + pool := pool.NewPool() + state := state.NewState() + bp := state.NewBatchProcessor(cfg.StartingHash, cfg.WithProofCalulation) + ethClient := eth.NewClient() + synchronizer := NewSynchronizerClient() + return Sequencer{ + Pool: pool, + State: state, + EthClient: ethClient, + BatchProcessor: bp, + Synchronizer: synchronizer, + }, nil +} + +func (s *Sequencer) Start() { + // Infinite for loop: + // 1. Wait for synchronizer to sync last batch + // 2. Estimate available time to run selection + // 3. Run selection + // 4. Is selection profitable? + // YES: send selection to Ethereum + // NO: discard selection and wait for the new batch +} + +type batch struct { + txs []pool.Transaction +} + +// selectTxs process txs and split valid txs into batches of txs. This process should be completed in less than selectionTime +func (s *Sequencer) selectTxs(pendingTxs []pool.Transaction, selectionTime time.Duration) ([]batch, error) { + panic("not implemented") +} + +// estimateTime Estimate available time to run selection +func (s *Sequencer) estimateTime() (time.Duration, error) { + panic("not implemented") +} + +func (s *Sequencer) isSelectionProfitable(b batch) bool { + panic("not implemented") +} diff --git a/sequencer/synchronizer.go b/sequencer/synchronizer.go new file mode 100644 index 0000000000..d13f996b5c --- /dev/null +++ b/sequencer/synchronizer.go @@ -0,0 +1,16 @@ +package sequencer + +type SynchronizerClient struct { + IsSynced chan int +} + +func NewSynchronizerClient() SynchronizerClient { + // connect to synch to get signal, that it's synced + isSynced := make(chan int) + return SynchronizerClient{IsSynced: isSynced} +} + +func (sc *SynchronizerClient) subscrToSyncedSignal() { + // get signal + sc.IsSynced <- 1 +} From f9bdd4326c3eb462d4e10b42f12522a3198fccb3 Mon Sep 17 00:00:00 2001 From: Mikelle Date: Thu, 11 Nov 2021 20:30:52 +0300 Subject: [PATCH 2/2] updated pr according to the comments --- aggregator/aggregator.go | 10 +++------- aggregator/cmd/main.go | 5 ----- aggregator/config.go | 3 +-- aggregator/go.mod | 5 ----- pool/pool.go | 37 +++++++------------------------------ sequencer/cmd/main.go | 5 ----- sequencer/go.mod | 9 --------- sequencer/sequencer.go | 25 ++++++++----------------- 8 files changed, 19 insertions(+), 80 deletions(-) delete mode 100644 aggregator/cmd/main.go delete mode 100644 aggregator/go.mod delete mode 100644 sequencer/cmd/main.go delete mode 100644 sequencer/go.mod diff --git a/aggregator/aggregator.go b/aggregator/aggregator.go index e37f30c014..7503eb63be 100644 --- a/aggregator/aggregator.go +++ b/aggregator/aggregator.go @@ -9,22 +9,18 @@ type Aggregator struct { Synchronizer SynchronizerClient } -func NewAggregator(cfg Config) (Aggregator, error) { - state := state.NewState() - bp := state.NewBatchProcessor(cfg.StartingHash, cfg.WithProofCalulation) - ethClient := eth.NewClient() - synchronizerClient := NewSynchronizerClient() +func NewAggregator(cfg Config, state state.State, bp state.BatchProcessor, ethClient eth.Client, syncClient SynchronizerClient) (Aggregator, error) { return Aggregator{ State: state, BatchProcessor: bp, EthClient: ethClient, - Synchronizer: synchronizerClient, + Synchronizer: syncClient, }, nil } func (agr *Aggregator) generateAndSendProofs() { // reads from batchesChan - // TODO: get txs from MT by batchNum + // get txs from state by batchNum // check if it's profitable or not // send proof + txs to the prover // send proof + txs to the SC diff --git a/aggregator/cmd/main.go b/aggregator/cmd/main.go deleted file mode 100644 index 70dfb1fa35..0000000000 --- a/aggregator/cmd/main.go +++ /dev/null @@ -1,5 +0,0 @@ -package cmd - -func main() { - -} diff --git a/aggregator/config.go b/aggregator/config.go index 2873637675..ac717d8714 100644 --- a/aggregator/config.go +++ b/aggregator/config.go @@ -1,4 +1,3 @@ package aggregator -type Config struct { -} +type Config struct{} diff --git a/aggregator/go.mod b/aggregator/go.mod deleted file mode 100644 index bbe1a78002..0000000000 --- a/aggregator/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module aggregator - -go 1.16 - -require github.com/ethereum/go-ethereum v1.10.12 diff --git a/pool/pool.go b/pool/pool.go index d4fa07896b..903d03229b 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -1,40 +1,17 @@ package pool import ( - "time" - "github.com/ethereum/go-ethereum/common" - "github.com/jmoiron/sqlx" - //nolint:errcheck // driver for postgres DB - _ "github.com/lib/pq" + "github.com/ethereum/go-ethereum/core/types" ) -type Pool struct { - db *sqlx.DB - ttl time.Duration - maxTxs uint32 -} - -func NewPool( - db *sqlx.DB, - ttl time.Duration, - maxTxs uint32, -) *Pool { - return &Pool{ - db: db, - ttl: ttl, - maxTxs: maxTxs, - } -} - -func (pool *Pool) GetPendingTxs() ([]Transaction, error) { - panic("not implemented") -} - -func (pool *Pool) UpdateTxState(hash common.Hash, newState TxState) error { - panic("not implemented") +type Pool interface { + AddTx(tx types.Transaction) error + GetPendingTxs() ([]Transaction, error) + UpdateTxState(hash common.Hash, newState TxState) error + CleanUpInvalidAndNonSelectedTxs() error } -func (pool *Pool) CleanUpInvalidAndNonSelectedTx() error { +func NewPool() *Pool { panic("not implemented") } diff --git a/sequencer/cmd/main.go b/sequencer/cmd/main.go deleted file mode 100644 index 70dfb1fa35..0000000000 --- a/sequencer/cmd/main.go +++ /dev/null @@ -1,5 +0,0 @@ -package cmd - -func main() { - -} diff --git a/sequencer/go.mod b/sequencer/go.mod deleted file mode 100644 index 95b28b46a3..0000000000 --- a/sequencer/go.mod +++ /dev/null @@ -1,9 +0,0 @@ -module github.com/hermeznetwork/hermez-core/sequencer - -go 1.16 - -require ( - github.com/ethereum/go-ethereum v1.10.12 - github.com/jmoiron/sqlx v1.3.4 - github.com/lib/pq v1.2.0 -) diff --git a/sequencer/sequencer.go b/sequencer/sequencer.go index a9613abe16..fbe0e8193b 100644 --- a/sequencer/sequencer.go +++ b/sequencer/sequencer.go @@ -1,9 +1,9 @@ package sequencer import ( - "github.com/hermeznetwork/hermez-core/pool" - "github.com/jmoiron/sqlx" "time" + + "github.com/hermeznetwork/hermez-core/pool" ) type Sequencer struct { @@ -14,22 +14,13 @@ type Sequencer struct { SynchronizerClient SynchronizerClient } -func NewSequencer(cfg Config) (Sequencer, error) { - db, err := sqlx.Connect("postgres", "") - if err != nil { - return Sequencer{}, err - } - pool := pool.NewPool() - state := state.NewState() - bp := state.NewBatchProcessor(cfg.StartingHash, cfg.WithProofCalulation) - ethClient := eth.NewClient() - synchronizer := NewSynchronizerClient() +func NewSequencer(pool pool.Pool, state state.State, bp state.BatchProcessor, ethClient eth.Client, syncClient SynchronizerClient) (Sequencer, error) { return Sequencer{ - Pool: pool, - State: state, - EthClient: ethClient, - BatchProcessor: bp, - Synchronizer: synchronizer, + Pool: pool, + State: state, + EthClient: ethClient, + BatchProcessor: bp, + SynchronizerClient: syncClient, }, nil }