Skip to content

Commit

Permalink
Implement fork id in node (#1637)
Browse files Browse the repository at this point in the history
* handle fork Id

* new errors

* 1 as default fork id

* 1 as default fork id

* add default fork id parameter

* add default fork id parameter

* Update prover image

* solve conflicts

* update prover image

* new prover image

* new prover image

* new prover image

* new prover image

* new prover image

* new prover image

* new prover image

* new prover image

---------

Co-authored-by: Alonso <ARR551@protonmail.com>
  • Loading branch information
ToniRamirezM and ARR552 committed Feb 7, 2023
1 parent 364a76a commit 922734d
Show file tree
Hide file tree
Showing 29 changed files with 810 additions and 627 deletions.
7 changes: 7 additions & 0 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ func (a *Aggregator) Channel(stream pb.AggregatorService_ChannelServer) error {

log.Debugf("Establishing stream connection with prover ID [%s], addr [%s]", prover.ID(), prover.Addr())

// Check if prover supports the required Fork ID
if !prover.SupportsForkID(a.cfg.ForkId) {
log.Warn("Prover does not support required fork ID.")
return errors.New("prover does not support required fork ID")
}

for {
select {
case <-a.ctx.Done():
Expand Down Expand Up @@ -820,6 +826,7 @@ func (a *Aggregator) buildInputProver(ctx context.Context, batchToVerify *state.
OldAccInputHash: previousBatch.AccInputHash.Bytes(),
OldBatchNum: previousBatch.BatchNumber,
ChainId: a.cfg.ChainID,
ForkId: a.cfg.ForkId,
BatchL2Data: batchToVerify.BatchL2Data,
GlobalExitRoot: batchToVerify.GlobalExitRoot.Bytes(),
EthTimestamp: uint64(batchToVerify.Timestamp.Unix()),
Expand Down
3 changes: 3 additions & 0 deletions aggregator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,7 @@ type Config struct {

// ChainID is the L2 ChainID provided by the Network Config
ChainID uint64

// ForkID is the L2 ForkID provided by the Network Config
ForkId uint64
}
461 changes: 249 additions & 212 deletions aggregator/pb/aggregator.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion aggregator/pb/aggregator_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 37 additions & 24 deletions aggregator/prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,20 @@ func (p *Prover) IsIdle() bool {
log.Warnf("Error asking status for prover ID %s: %w", p.ID(), err)
return false
}
return status.Status == pb.GetStatusResponse_IDLE
return status.Status == pb.GetStatusResponse_STATUS_IDLE
}

// SupportsForkID returns true if the prover supports the given fork id.
func (p *Prover) SupportsForkID(forkID uint64) bool {
status, err := p.Status()
if err != nil {
log.Warnf("Error asking status for prover ID %s: %w", p.ID(), err)
return false
}

log.Debugf("Prover %s supports fork ID %d", p.ID(), status.ForkId)

return status.ForkId == forkID
}

// BatchProof instructs the prover to generate a batch proof for the provided
Expand All @@ -97,13 +110,13 @@ func (p *Prover) BatchProof(input *pb.InputProver) (*string, error) {
}
if msg, ok := res.Response.(*pb.ProverMessage_GenBatchProofResponse); ok {
switch msg.GenBatchProofResponse.Result {
case pb.Result_UNSPECIFIED:
case pb.Result_RESULT_UNSPECIFIED:
return nil, fmt.Errorf("Failed to generate proof %s, %w, input %v", msg.GenBatchProofResponse.String(), ErrUnspecified, input)
case pb.Result_OK:
case pb.Result_RESULT_OK:
return &msg.GenBatchProofResponse.Id, nil
case pb.Result_ERROR:
case pb.Result_RESULT_ERROR:
return nil, fmt.Errorf("Failed to generate proof %s, %w, input %v", msg.GenBatchProofResponse.String(), ErrBadRequest, input)
case pb.Result_INTERNAL_ERROR:
case pb.Result_RESULT_INTERNAL_ERROR:
return nil, fmt.Errorf("Failed to generate proof %s, %w, input %v", msg.GenBatchProofResponse.String(), ErrProverInternalError, input)
default:
return nil, fmt.Errorf("Failed to generate proof %s, %w,input %v", msg.GenBatchProofResponse.String(), ErrUnknown, input)
Expand All @@ -130,15 +143,15 @@ func (p *Prover) AggregatedProof(inputProof1, inputProof2 string) (*string, erro
}
if msg, ok := res.Response.(*pb.ProverMessage_GenAggregatedProofResponse); ok {
switch msg.GenAggregatedProofResponse.Result {
case pb.Result_UNSPECIFIED:
case pb.Result_RESULT_UNSPECIFIED:
return nil, fmt.Errorf("Failed to aggregate proofs %s, %w, input 1 %s, input 2 %s",
msg.GenAggregatedProofResponse.String(), ErrUnspecified, inputProof1, inputProof2)
case pb.Result_OK:
case pb.Result_RESULT_OK:
return &msg.GenAggregatedProofResponse.Id, nil
case pb.Result_ERROR:
case pb.Result_RESULT_ERROR:
return nil, fmt.Errorf("Failed to aggregate proofs %s, %w, input 1 %s, input 2 %s",
msg.GenAggregatedProofResponse.String(), ErrBadRequest, inputProof1, inputProof2)
case pb.Result_INTERNAL_ERROR:
case pb.Result_RESULT_INTERNAL_ERROR:
return nil, fmt.Errorf("Failed to aggregate proofs %s, %w, input 1 %s, input 2 %s",
msg.GenAggregatedProofResponse.String(), ErrProverInternalError, inputProof1, inputProof2)
default:
Expand Down Expand Up @@ -166,15 +179,15 @@ func (p *Prover) FinalProof(inputProof string, aggregatorAddr string) (*string,
}
if msg, ok := res.Response.(*pb.ProverMessage_GenFinalProofResponse); ok {
switch msg.GenFinalProofResponse.Result {
case pb.Result_UNSPECIFIED:
case pb.Result_RESULT_UNSPECIFIED:
return nil, fmt.Errorf("Failed to generate final proof %s, %w, input %s",
msg.GenFinalProofResponse.String(), ErrUnspecified, inputProof)
case pb.Result_OK:
case pb.Result_RESULT_OK:
return &msg.GenFinalProofResponse.Id, nil
case pb.Result_ERROR:
case pb.Result_RESULT_ERROR:
return nil, fmt.Errorf("Failed to generate final proof %s, %w, input %s",
msg.GenFinalProofResponse.String(), ErrBadRequest, inputProof)
case pb.Result_INTERNAL_ERROR:
case pb.Result_RESULT_INTERNAL_ERROR:
return nil, fmt.Errorf("Failed to generate final proof %s, %w, input %s",
msg.GenFinalProofResponse.String(), ErrProverInternalError, inputProof)
default:
Expand All @@ -200,15 +213,15 @@ func (p *Prover) CancelProofRequest(proofID string) error {
if msg, ok := res.Response.(*pb.ProverMessage_CancelResponse); ok {
// TODO(pg): handle all cases
switch msg.CancelResponse.Result {
case pb.Result_UNSPECIFIED:
case pb.Result_RESULT_UNSPECIFIED:
return fmt.Errorf("Failed to cancel proof id [%s], %w, %s",
proofID, ErrUnspecified, msg.CancelResponse.String())
case pb.Result_OK:
case pb.Result_RESULT_OK:
return nil
case pb.Result_ERROR:
case pb.Result_RESULT_ERROR:
return fmt.Errorf("Failed to cancel proof id [%s], %w, %s",
proofID, ErrBadRequest, msg.CancelResponse.String())
case pb.Result_INTERNAL_ERROR:
case pb.Result_RESULT_INTERNAL_ERROR:
return fmt.Errorf("Failed to cancel proof id [%s], %w, %s",
proofID, ErrProverInternalError, msg.CancelResponse.String())
default:
Expand Down Expand Up @@ -264,24 +277,24 @@ func (p *Prover) waitProof(ctx context.Context, proofID string) (*pb.GetProofRes
}
if msg, ok := res.Response.(*pb.ProverMessage_GetProofResponse); ok {
switch msg.GetProofResponse.Result {
case pb.GetProofResponse_PENDING:
case pb.GetProofResponse_RESULT_PENDING:
time.Sleep(p.proofStatePollingInterval.Duration)
continue
case pb.GetProofResponse_UNSPECIFIED:
case pb.GetProofResponse_RESULT_UNSPECIFIED:
return nil, fmt.Errorf("Failed to get proof ID: %s, %w, prover response: %s",
proofID, ErrUnspecified, msg.GetProofResponse.String())
case pb.GetProofResponse_COMPLETED_OK:
case pb.GetProofResponse_RESULT_COMPLETED_OK:
return msg.GetProofResponse, nil
case pb.GetProofResponse_ERROR:
case pb.GetProofResponse_RESULT_ERROR:
return nil, fmt.Errorf("Failed to get proof with ID %s, %w, prover response: %s",
proofID, ErrBadRequest, msg.GetProofResponse.String())
case pb.GetProofResponse_COMPLETED_ERROR:
case pb.GetProofResponse_RESULT_COMPLETED_ERROR:
return nil, fmt.Errorf("Failed to get proof with ID %s, %w, prover response: %s",
proofID, ErrProverCompletedError, msg.GetProofResponse.String())
case pb.GetProofResponse_INTERNAL_ERROR:
case pb.GetProofResponse_RESULT_INTERNAL_ERROR:
return nil, fmt.Errorf("Failed to get proof ID: %s, %w, prover response: %s",
proofID, ErrProverInternalError, msg.GetProofResponse.String())
case pb.GetProofResponse_CANCEL:
case pb.GetProofResponse_RESULT_CANCEL:
return nil, fmt.Errorf("Proof generation was cancelled for proof ID %s, %w, prover response: %s",
proofID, ErrProofCanceled, msg.GetProofResponse.String())
default:
Expand Down
25 changes: 23 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io/ioutil"
"math"
"math/big"
"net"
"net/http"
Expand Down Expand Up @@ -69,12 +70,30 @@ func start(cliCtx *cli.Context) error {
if err != nil {
log.Fatal(err)
}
// Read Fork ID FROM POE SC
// TODO: Uncomment when the POE SC is implemented
/*
currentForkID, err := etherman.GetL2ForkID()
if err != nil {
log.Fatal(err)
}
forkIDIntervals, err := etherman.GetL2ForkIDIntervals()
if err != nil {
log.Fatal(err)
}
*/

currentForkID := c.DefaultForkID
forkIDIntervals := []state.ForkIDInterval{{FromBatchNumber: 0, ToBatchNumber: math.MaxUint64, ForkId: c.DefaultForkID}}

c.Aggregator.ChainID = l2ChainID
c.Aggregator.ForkId = currentForkID
c.RPC.ChainID = l2ChainID
log.Infof("Chain ID read from POE SC = %v", l2ChainID)

ctx := context.Background()
st := newState(ctx, c, l2ChainID, stateSqlDB)
st := newState(ctx, c, l2ChainID, currentForkID, forkIDIntervals, stateSqlDB)

ethTxManager := ethtxmanager.New(c.EthTxManager, etherman, st)

Expand Down Expand Up @@ -277,7 +296,7 @@ func newAuthFromKeystore(path, password string, chainID uint64) (*bind.TransactO
return auth, nil
}

func newState(ctx context.Context, c *config.Config, l2ChainID uint64, sqlDB *pgxpool.Pool) *state.State {
func newState(ctx context.Context, c *config.Config, l2ChainID uint64, currentForkID uint64, forkIDIntervals []state.ForkIDInterval, sqlDB *pgxpool.Pool) *state.State {
stateDb := state.NewPostgresStorage(sqlDB)
executorClient, _, _ := executor.NewExecutorClient(ctx, c.Executor)
stateDBClient, _, _ := merkletree.NewMTDBServiceClient(ctx, c.MTClient)
Expand All @@ -286,6 +305,8 @@ func newState(ctx context.Context, c *config.Config, l2ChainID uint64, sqlDB *pg
stateCfg := state.Config{
MaxCumulativeGasUsed: c.Sequencer.MaxCumulativeGasUsed,
ChainID: l2ChainID,
CurrentForkID: currentForkID,
ForkIDIntervals: forkIDIntervals,
}

st := state.NewState(stateCfg, stateDb, executorClient, stateTree)
Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ const (

// Config represents the configuration of the entire Hermez Node
type Config struct {
IsTrustedSequencer bool `mapstructure:"IsTrustedSequencer"`
IsTrustedSequencer bool `mapstructure:"IsTrustedSequencer"`
DefaultForkID uint64 `mapstructure:"DefaultForkID"`
Log log.Config
Etherman etherman.Config
EthTxManager ethtxmanager.Config
Expand Down
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
// DefaultValues is the default configuration
const DefaultValues = `
IsTrustedSequencer = false
DefaultForkID = 1
[Log]
Level = "debug"
Expand Down
1 change: 1 addition & 0 deletions config/environments/local/local.node.config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
IsTrustedSequencer = false
DefaultForkID = 1

[Log]
Level = "debug"
Expand Down
1 change: 0 additions & 1 deletion config/environments/public/public.prover.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@

"inputFile": "input_executor.json",
"outputPath": "output",
"romFile": "rom.json",
"cmPolsFile_disabled": "zkevm.commit",
"cmPolsFileC12a_disabled": "zkevm.c12a.commit",
"cmPolsFileRecursive1_disabled": "zkevm.recursive1.commit",
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ services:

zkevm-prover:
container_name: zkevm-prover
image: hermeznetwork/zkevm-prover:9c4a1fe
image: hermeznetwork/zkevm-prover:c136cc1
ports:
- 50061:50061 # MT
- 50071:50071 # Executor
Expand Down
2 changes: 1 addition & 1 deletion etherman/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var (
//ErrContentLengthTooLarge content length is too large
ErrContentLengthTooLarge = errors.New("content length too large")
//ErrTimestampMustBeInsideRange Timestamp must be inside range
ErrTimestampMustBeInsideRange = errors.New("Timestamp must be inside range")
ErrTimestampMustBeInsideRange = errors.New("timestamp must be inside range")
//ErrInsufficientAllowance insufficient allowance
ErrInsufficientAllowance = errors.New("insufficient allowance")
//ErrBothGasPriceAndMaxFeeGasAreSpecified both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified
Expand Down
13 changes: 13 additions & 0 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"math/big"
"strings"
"time"
Expand Down Expand Up @@ -853,6 +854,18 @@ func (etherMan *Client) GetL2ChainID() (uint64, error) {
return etherMan.PoE.ChainID(&bind.CallOpts{Pending: false})
}

// GetL2ForkID returns current L2 Fork ID
func (etherMan *Client) GetL2ForkID() (uint64, error) {
// TODO: implement this
return 1, nil
}

// GetL2ForkIDIntervals return L2 Fork ID intervals
func (etherMan *Client) GetL2ForkIDIntervals() ([]state.ForkIDInterval, error) {
// TODO: implement this
return []state.ForkIDInterval{{FromBatchNumber: 0, ToBatchNumber: math.MaxUint64, ForkId: 1}}, nil
}

func (etherMan *Client) getGasPrice(ctx context.Context) *big.Int {
// Get gasPrice from providers
gasPrice := big.NewInt(0)
Expand Down
4 changes: 2 additions & 2 deletions merkletree/pb/statedb.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion merkletree/pb/statedb_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 922734d

Please sign in to comment.