Skip to content

Commit

Permalink
Illuminate the code (0xPolygonHermez#47)
Browse files Browse the repository at this point in the history
* Illuminate the code
  • Loading branch information
arnaubennassar authored Nov 24, 2021
1 parent ecbfe68 commit 0fef6c9
Show file tree
Hide file tree
Showing 35 changed files with 355 additions and 299 deletions.
10 changes: 8 additions & 2 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package aggregator

import (
"context"
"log"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"

"github.com/hermeznetwork/hermez-core/etherman"
"github.com/hermeznetwork/hermez-core/state"
"github.com/hermeznetwork/hermez-core/synchronizer"
)

// Aggregator represents an aggregator
type Aggregator struct {
State state.State
BatchProcessor state.BatchProcessor
Expand All @@ -23,6 +24,7 @@ type Aggregator struct {
txsByBatchNum map[uint64]txsWithProof
}

// NewAggregator creates a new aggregator
func NewAggregator(
cfg Config,
state state.State,
Expand Down Expand Up @@ -56,13 +58,16 @@ type txsWithProof struct {
proof *state.Proof
}

// Start starts the aggregator
func (a *Aggregator) Start() {
// reads from batchesChan
// get txs from state by batchNum
// check if it's profitable or not
// send zki + txs to the prover
// send proof + txs to the SC
a.Synchronizer.Sync()
if err := a.Synchronizer.Sync(); err != nil {
log.Fatal(err)
}
}

func (a *Aggregator) onNewBatchPropostal(batchNumber uint64, root common.Hash) {
Expand Down Expand Up @@ -101,6 +106,7 @@ func (a *Aggregator) isProfitable(txs []*types.Transaction) bool {
return true
}

// Stop stops the aggregator
func (a *Aggregator) Stop() {
a.cancel()
}
1 change: 1 addition & 0 deletions aggregator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aggregator

import "github.com/hermeznetwork/hermez-core/etherman"

// Config represents the configuration of the aggregator
type Config struct {
// PrivateKey is used to sign l1 tx
PrivateKey string
Expand Down
3 changes: 3 additions & 0 deletions aggregator/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"github.com/hermeznetwork/hermez-core/state"
)

// ProverClient is used to interact with the prover
type ProverClient struct {
}

// NewProverClient creates a new prover client
func NewProverClient() ProverClient {
return ProverClient{}
}

// SendTxs sends txs to the prover
func (p *ProverClient) SendTxs(txs []*types.Transaction) (*state.Proof, error) {
return nil, nil
}
17 changes: 14 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func runJSONRpcServer(c jsonrpc.Config) {
p := mocks.NewPool()
s := mocks.NewState()

jsonrpc.NewServer(c, p, s).Start()
if err := jsonrpc.NewServer(c, p, s).Start(); err != nil {
log.Fatal(err)
}
}

func runSequencer(c sequencer.Config) {
Expand All @@ -46,10 +48,15 @@ func runSequencer(c sequencer.Config) {
if err != nil {
log.Fatal(err)
}
sequencer.NewSequencer(c, p, s, e, sy)
seq, err := sequencer.NewSequencer(c, p, s, e, sy)
if err != nil {
log.Fatal(err)
}
seq.Start()
}

func runAggregator(c aggregator.Config) {
// TODO: have more readable variables
s := mocks.NewState()
bp := s.NewBatchProcessor(common.Hash{}, false)
e, err := etherman.NewEtherman(c.Etherman)
Expand All @@ -61,7 +68,11 @@ func runAggregator(c aggregator.Config) {
log.Fatal(err)
}
pc := aggregator.NewProverClient()
aggregator.NewAggregator(c, s, bp, e, sy, pc)
agg, err := aggregator.NewAggregator(c, s, bp, e, sy, pc)
if err != nil {
log.Fatal(err)
}
agg.Start()
}

func waitSignal() {
Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import (
"github.com/hermeznetwork/hermez-core/sequencer"
)

// Config represents the configuration of the entire Hermez Node
type Config struct {
Log log.Config
RPC jsonrpc.Config
Sequencer sequencer.Config
Aggregator aggregator.Config
}

// Load loads the configuration
func Load() Config {
// TODO: load from config file
//nolint:gomnd
return Config{
Log: log.Config{
Level: "debug",
Expand Down
1 change: 1 addition & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/jackc/pgx/v4/pgxpool"
)

// NewSQLDB creates a new SQL DB
func NewSQLDB(dbName, dbUser, dbPassword, dbHost, dbPort string) (*pgxpool.Pool, error) {
return pgxpool.Connect(context.Background(), "postgres://"+dbUser+":"+dbPassword+"@"+dbHost+":"+dbPort+"/"+dbName)
}
3 changes: 2 additions & 1 deletion etherman/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package etherman

import "github.com/ethereum/go-ethereum/common"

// Config represents the configuration of the etherman
type Config struct {
Url string
URL string
PoeAddress common.Address
}
7 changes: 5 additions & 2 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hermeznetwork/hermez-core/state"
)

// EtherMan represents an Ethereum Manager
type EtherMan interface {
EthBlockByNumber(ctx context.Context, blockNum int64) (*types.Block, error)
GetBatchesByBlock(blockNum int64) ([]state.Batch, error)
Expand All @@ -20,17 +21,19 @@ type EtherMan interface {
ConsolidateBatch(batch state.Batch, proof state.Proof) (common.Hash, error)
}

// BasicEtherMan is a simple implementation of EtherMan
type BasicEtherMan struct {
EtherClient *ethclient.Client
PoE *proofofefficiency.Proofofefficiency
}

// NewEtherman creates a new etherman
func NewEtherman(cfg Config) (EtherMan, error) {
//TODO
//Connect to ethereum node
ethClient, err := ethclient.Dial(cfg.Url)
ethClient, err := ethclient.Dial(cfg.URL)
if err != nil {
log.Errorf("error connecting to %s: %+v", cfg.Url, err)
log.Errorf("error connecting to %s: %+v", cfg.URL, err)
return nil, err
}
//Create smc clients
Expand Down
42 changes: 17 additions & 25 deletions jsonrpc/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ type ErrorResponse struct {
}

// Id returns error response id
func (e *ErrorResponse) Id() interface{} {
func (e *ErrorResponse) Id() interface{} { //nolint:golint
return e.ID
}

// Data returns ErrorObject
func (e *ErrorResponse) Data() json.RawMessage {

data, err := json.Marshal(e.Error)
if err != nil {
return json.RawMessage(err.Error())
Expand All @@ -45,7 +44,6 @@ func (e *ErrorResponse) Data() json.RawMessage {

// Bytes return the serialized response
func (e *ErrorResponse) Bytes() ([]byte, error) {

return json.Marshal(e)
}

Expand All @@ -58,23 +56,21 @@ type SuccessResponse struct {
}

// Id returns success response id
func (s *SuccessResponse) Id() interface{} {
func (s *SuccessResponse) Id() interface{} { //nolint:golint
return s.ID
}

// Data returns the result
func (s *SuccessResponse) Data() json.RawMessage {

if s.Result != nil {
return s.Result
}
return json.RawMessage("No Data")
}

// Bytes return the serialized response
func (e *SuccessResponse) Bytes() ([]byte, error) {

return json.Marshal(e)
func (s *SuccessResponse) Bytes() ([]byte, error) {
return json.Marshal(s)
}

// ErrorObject is a jsonrpc error
Expand All @@ -94,11 +90,15 @@ func (e *ErrorObject) Error() string {
}

const (
PendingBlockNumber = BlockNumber(-3)
LatestBlockNumber = BlockNumber(-2)
// PendingBlockNumber represents the pending block number
PendingBlockNumber = BlockNumber(-3)
// LatestBlockNumber represents the latest block number
LatestBlockNumber = BlockNumber(-2)
// EarliestBlockNumber represents the earliest block number
EarliestBlockNumber = BlockNumber(-1)
)

// BlockNumber is the number of a ethereum block
type BlockNumber int64

func stringToBlockNumber(str string) (BlockNumber, error) {
Expand All @@ -123,14 +123,6 @@ func stringToBlockNumber(str string) (BlockNumber, error) {
return BlockNumber(n), nil
}

func createBlockNumberPointer(str string) (*BlockNumber, error) {
blockNumber, err := stringToBlockNumber(str)
if err != nil {
return nil, err
}
return &blockNumber, nil
}

// UnmarshalJSON automatically decodes the user input for the block number, when a JSON RPC method is called
func (b *BlockNumber) UnmarshalJSON(buffer []byte) error {
num, err := stringToBlockNumber(string(buffer))
Expand All @@ -141,6 +133,7 @@ func (b *BlockNumber) UnmarshalJSON(buffer []byte) error {
return nil
}

// Index of a item
type Index int64

// UnmarshalJSON automatically decodes the user input for the block number, when a JSON RPC method is called
Expand All @@ -154,25 +147,24 @@ func (i *Index) UnmarshalJSON(buffer []byte) error {
return nil
}

// NewRpcErrorResponse is used to create a custom error response
func NewRpcErrorResponse(req Request, err Error) Response {
// NewRPCErrorResponse is used to create a custom error response
func NewRPCErrorResponse(req Request, err detailedError) Response {
response := &ErrorResponse{
JSONRPC: req.JSONRPC,
ID: req.ID,
Error: &ErrorObject{err.ErrorCode(), err.Error(), nil},
Error: &ErrorObject{err.Code(), err.Error(), nil},
}
return response
}

// NewRpcResponse returns Success/Error response object
func NewRpcResponse(req Request, reply []byte, err Error) Response {

// NewRPCResponse returns Success/Error response object
func NewRPCResponse(req Request, reply []byte, err detailedError) Response {
var response Response
switch err.(type) {
case nil:
response = &SuccessResponse{JSONRPC: req.JSONRPC, ID: req.ID, Result: reply}
default:
response = NewRpcErrorResponse(req, err)
response = NewRPCErrorResponse(req, err)
}

return response
Expand Down
1 change: 1 addition & 0 deletions jsonrpc/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jsonrpc

// Config represents the configuration of the json rpc
type Config struct {
Host string
Port int
Expand Down
11 changes: 9 additions & 2 deletions jsonrpc/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hermeznetwork/hermez-core/jsonrpc/hex"
)

// DecodeUint64orHex decodes a string uint64 or hex string into a uint64
func DecodeUint64orHex(val *string) (uint64, error) {
if val == nil {
return 0, nil
Expand All @@ -20,9 +21,10 @@ func DecodeUint64orHex(val *string) (uint64, error) {
str = str[2:]
base = 16
}
return strconv.ParseUint(str, base, 64)
return strconv.ParseUint(str, base, bitSize64)
}

// DecodeUint256orHex decodes a string uint256 or hex string into a bit.Int
func DecodeUint256orHex(val *string) (*big.Int, error) {
if val == nil {
return nil, nil
Expand All @@ -41,11 +43,13 @@ func DecodeUint256orHex(val *string) (*big.Int, error) {
return b, nil
}

// DecodeInt64orHex decodes a string int64 or hex string into a int64
func DecodeInt64orHex(val *string) (int64, error) {
i, err := DecodeUint64orHex(val)
return int64(i), err
}

// DecodeBytes decodes a hex string into a []byte
func DecodeBytes(val *string) ([]byte, error) {
if val == nil {
return []byte{}, nil
Expand All @@ -56,17 +60,20 @@ func DecodeBytes(val *string) ([]byte, error) {
return hex.DecodeString(str)
}

// EncodeUint64 encodes a uint64 into a hex string
func EncodeUint64(b uint64) *string {
res := fmt.Sprintf("0x%x", b)
return &res
}

// EncodeBytes encodes a []bytes into a hex string
func EncodeBytes(b []byte) *string {
res := "0x" + hex.EncodeToString(b)
return &res
}

// EncodeBigInt encodes a big.Int into a hex string
func EncodeBigInt(b *big.Int) *string {
res := "0x" + b.Text(16)
res := "0x" + b.Text(hexBase)
return &res
}
Loading

0 comments on commit 0fef6c9

Please sign in to comment.