Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding interfaces for the aggregator and sequencer #3

Merged
merged 2 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
@@ -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()
Mikelle marked this conversation as resolved.
Show resolved Hide resolved
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() {

}
5 changes: 5 additions & 0 deletions aggregator/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cmd
Mikelle marked this conversation as resolved.
Show resolved Hide resolved

func main() {

}
4 changes: 4 additions & 0 deletions aggregator/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package aggregator

type Config struct {
}
5 changes: 5 additions & 0 deletions aggregator/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module aggregator

go 1.16

require github.com/ethereum/go-ethereum v1.10.12
8 changes: 8 additions & 0 deletions aggregator/prover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package aggregator

type ProverClient struct {
}

func NewProverClient() ProverClient {
return ProverClient{}
}
18 changes: 18 additions & 0 deletions aggregator/syncronizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package aggregator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically, the Synchronizer service should provide a package in order to allow other services to integrate into it and this file will be replaced by something like:

import "github.com/hermeznetwork/hermez-core/synchronizer/client"

Is that right?

If it's true, maybe you can talk to @ARR552 in order to create the shell of the interface and start to send PRs with the methods you need in the interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tclemos @arnaubennassar
Actually, I thought about some grpc endpoint, so when the synchronizer is synced with the network, it sends events to the sequencer/aggregator. What do you think about this?


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}
}
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
40 changes: 40 additions & 0 deletions pool/pool.go
Original file line number Diff line number Diff line change
@@ -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 {
Mikelle marked this conversation as resolved.
Show resolved Hide resolved
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")
}
16 changes: 16 additions & 0 deletions pool/transaction.go
Original file line number Diff line number Diff line change
@@ -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"
Mikelle marked this conversation as resolved.
Show resolved Hide resolved
)

type Transaction struct {
types.Transaction
state TxState
}
5 changes: 5 additions & 0 deletions sequencer/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cmd

func main() {

}
4 changes: 4 additions & 0 deletions sequencer/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package sequencer

type Config struct {
}
9 changes: 9 additions & 0 deletions sequencer/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
62 changes: 62 additions & 0 deletions sequencer/sequencer.go
Original file line number Diff line number Diff line change
@@ -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")
}
16 changes: 16 additions & 0 deletions sequencer/synchronizer.go
Original file line number Diff line number Diff line change
@@ -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
}