Skip to content

Commit

Permalink
Backport v0.0.1-RC3 changes (0xPolygonHermez#1463)
Browse files Browse the repository at this point in the history
* fix: update aggregator proto to send pub address

This commit applies the changes introduced to the aggregator proto file.
The changes are regarding the GetGenFinalProofRequest message that now
needs to send the aggregator public address.

* chore: add goreleaser file

* Get NewLocalExitRoot and NewStateRoot from last batch in the aggregated proof

* feat: add prover IP address

This commit adds the prover IP address to the Prover struct. The address
is retrieved from the grpc connection and logged alogside the prover ID.

* fix: remove ShortID method from Prover

This commit removes the ShortID method from the Prover. This because the
prover ID can be a fixed arbitrary string and not only a UUID.

* fix accinputhash (0xPolygonHermez#1458)

* refac: send final proof in FinalProofInputs

This commit modifies the FinalProofInputs struct to hold also the final
proof. The VerifyBatches signature has been modified accordingly.

Co-authored-by: zkronos73 <[email protected]>
Co-authored-by: Alonso Rodriguez <[email protected]>

Co-authored-by: zkronos73 <[email protected]>
Co-authored-by: Alonso Rodriguez <[email protected]>
  • Loading branch information
3 people authored Dec 16, 2022
1 parent 58334d4 commit 8354a86
Show file tree
Hide file tree
Showing 14 changed files with 435 additions and 312 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ builds:
- amd64
- arm64
env:
- CGO_ENABLED=0
- CGO_ENABLED=0
72 changes: 49 additions & 23 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (

"github.com/0xPolygonHermez/zkevm-node/aggregator/pb"
"github.com/0xPolygonHermez/zkevm-node/aggregator/prover"
ethmanTypes "github.com/0xPolygonHermez/zkevm-node/etherman/types"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/state"
"google.golang.org/grpc"
grpchealth "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/peer"
)

const (
Expand Down Expand Up @@ -134,14 +136,18 @@ func (a *Aggregator) Stop() {
// Channel implements the bi-directional communication channel between the
// Prover client and the Aggregator server.
func (a *Aggregator) Channel(stream pb.AggregatorService_ChannelServer) error {
prover, err := prover.New(stream, a.cfg.ProofStatePollingInterval)
ctx := stream.Context()
var proverAddr net.Addr
p, ok := peer.FromContext(ctx)
if ok {
proverAddr = p.Addr
}
prover, err := prover.New(stream, proverAddr, a.cfg.ProofStatePollingInterval)
if err != nil {
return err
}

log.Debugf("Establishing stream connection with prover %s", prover.ID())

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

for {
select {
Expand All @@ -154,7 +160,7 @@ func (a *Aggregator) Channel(stream pb.AggregatorService_ChannelServer) error {

default:
if !prover.IsIdle() {
log.Debug("Prover ID %s is not idle", prover.ID())
log.Debugf("Prover { ID [%s], addr [%s] } is not idle", prover.ID(), prover.Addr())
time.Sleep(a.cfg.RetryTime.Duration)
continue
}
Expand Down Expand Up @@ -198,9 +204,23 @@ func (a *Aggregator) sendFinalProof() {

log.Infof("Verifying final proof with ethereum smart contract, batches %d-%d", proof.BatchNumber, proof.BatchNumberFinal)

tx, err := a.EthTxManager.VerifyBatches(ctx, proof.BatchNumber-1, proof.BatchNumberFinal, msg.finalProof)
finalBatch, err := a.State.GetBatchByNumber(ctx, proof.BatchNumberFinal, nil)
if err != nil {
log.Errorf("Error verifiying final proof for batches %d-%d, err: %v", proof.BatchNumber, proof.BatchNumberFinal, err)
log.Errorf("Failed to retrieve batch with number [%d]", proof.BatchNumberFinal)
continue
}

inputs := ethmanTypes.FinalProofInputs{
FinalProof: msg.finalProof,
NewLocalExitRoot: finalBatch.LocalExitRoot.Bytes(),
NewStateRoot: finalBatch.StateRoot.Bytes(),
}

log.Infof("Final proof inputs: NewLocalExitRoot [%#x], NewStateRoot [%#x]", inputs.NewLocalExitRoot, inputs.NewStateRoot)

tx, err := a.EthTxManager.VerifyBatches(ctx, proof.BatchNumber-1, proof.BatchNumberFinal, &inputs)
if err != nil {
log.Errorf("Error verifiying final proof for batches [%d-%d], err: %v", proof.BatchNumber, proof.BatchNumberFinal, err)

// unlock the underlying proof (generating=false)
proof.Generating = false
Expand All @@ -211,7 +231,7 @@ func (a *Aggregator) sendFinalProof() {
continue
}

log.Infof("Final proof for batches [%d-%d] verified in transaction [%v]", proof.BatchNumber, proof.BatchNumberFinal, tx.Hash().TerminalString())
log.Infof("Final proof for batches [%d-%d] verified in transaction [%v]", proof.BatchNumber, proof.BatchNumberFinal, tx.Hash())

// wait for the synchronizer to catch up the verified batches
log.Debug("A final proof has been sent, waiting for the network to be synced")
Expand All @@ -233,24 +253,30 @@ func (a *Aggregator) sendFinalProof() {

// buildFinalProof builds and return the final proof for an aggregated/batch proof.
func (a *Aggregator) buildFinalProof(ctx context.Context, prover proverInterface, proof *state.Proof) (*pb.FinalProof, error) {
log.Infof("Prover %s is going to be used to generate final proof for batches: %d-%d", prover.ID(), proof.BatchNumber, proof.BatchNumberFinal)
log.Infof("Prover { ID[%s], addr[%s] } is going to be used to generate final proof for batches [%d-%d]",
prover.ID(), prover.Addr(), proof.BatchNumber, proof.BatchNumberFinal)

pubAddr, err := a.Ethman.GetPublicAddress()
if err != nil {
return nil, fmt.Errorf("Failed to get public address, %w", err)
}

finalProofID, err := prover.FinalProof(proof.Proof)
finalProofID, err := prover.FinalProof(proof.Proof, pubAddr.String())
if err != nil {
return nil, fmt.Errorf("Failed to get final proof id, %w", err)
}

proof.ProofID = finalProofID

log.Infof("Proof ID for final proof %d-%d: %s", proof.BatchNumber, proof.BatchNumberFinal, *proof.ProofID)
log.Infof("Final proof ID for batches [%d-%d]: %s", proof.BatchNumber, proof.BatchNumberFinal, *proof.ProofID)

finalProof, err := prover.WaitFinalProof(ctx, *proof.ProofID)
if err != nil {
return nil, fmt.Errorf("Failed to get final proof from prover, %w", err)
}

//b, err := json.Marshal(resGetProof.FinalProof)
log.Infof("Final proof %s generated", *proof.ProofID)
log.Infof("Final proof [%s] generated", *proof.ProofID)

// mock prover sanity check
if string(finalProof.Public.NewStateRoot) == mockedStateRoot && string(finalProof.Public.NewLocalExitRoot) == mockedLocalExitRoot {
Expand All @@ -274,7 +300,7 @@ func (a *Aggregator) buildFinalProof(ctx context.Context, prover proverInterface
// generated proof. If the proof is eligible, then the final proof generation
// is triggered.
func (a *Aggregator) tryBuildFinalProof(ctx context.Context, prover proverInterface, proof *state.Proof) (bool, error) {
log.Debugf("tryBuildFinalProof start %s", prover.ID())
log.Debugf("tryBuildFinalProof start prover { ID [%s], addr [%s] }", prover.ID(), prover.Addr())

if !a.verifyProofTimeReached() {
log.Debug("Time to verify proof not reached")
Expand Down Expand Up @@ -466,7 +492,7 @@ func (a *Aggregator) getAndLockProofsToAggregate(ctx context.Context, prover pro
}

func (a *Aggregator) tryAggregateProofs(ctx context.Context, prover proverInterface) (bool, error) {
log.Debugf("tryAggregateProofs start %s", prover.ID())
log.Debugf("tryAggregateProofs start prover { ID [%s], addr [%s] }", prover.ID(), prover.Addr())

proof1, proof2, err0 := a.getAndLockProofsToAggregate(ctx, prover)
if errors.Is(err0, state.ErrNotFound) {
Expand All @@ -490,8 +516,8 @@ func (a *Aggregator) tryAggregateProofs(ctx context.Context, prover proverInterf
log.Debug("tryAggregateProofs end")
}()

log.Infof("Prover %s is going to be used to aggregate proofs: %d-%d and %d-%d",
prover.ID(), proof1.BatchNumber, proof1.BatchNumberFinal, proof2.BatchNumber, proof2.BatchNumberFinal)
log.Infof("Prover { ID [%s], addr [%s] } is going to be used to aggregate proofs: %d-%d and %d-%d",
prover.ID(), prover.Addr(), proof1.BatchNumber, proof1.BatchNumberFinal, proof2.BatchNumber, proof2.BatchNumberFinal)

proverID := prover.ID()
inputProver := map[string]interface{}{
Expand All @@ -500,7 +526,7 @@ func (a *Aggregator) tryAggregateProofs(ctx context.Context, prover proverInterf
}
b, err := json.Marshal(inputProver)
if err != nil {
return false, fmt.Errorf("Failed to serialize input prover, err: %w", err)
return false, fmt.Errorf("Failed to serialize input prover, %w", err)
}

proof := &state.Proof{
Expand Down Expand Up @@ -625,7 +651,7 @@ func (a *Aggregator) getAndLockBatchToProve(ctx context.Context, prover *prover.
}

func (a *Aggregator) tryGenerateBatchProof(ctx context.Context, prover *prover.Prover) (bool, error) {
log.Debugf("tryGenerateBatchProof start %s", prover.ID())
log.Debugf("tryGenerateBatchProof start prover { ID [%s], addr [%s] }", prover.ID(), prover.Addr())

batchToProve, proof, err0 := a.getAndLockBatchToProve(ctx, prover)
if errors.Is(err0, state.ErrNotFound) {
Expand All @@ -649,22 +675,22 @@ func (a *Aggregator) tryGenerateBatchProof(ctx context.Context, prover *prover.P
log.Debug("tryGenerateBatchProof end")
}()

log.Infof("Prover %s is going to be used to generate batch proof: %d", prover.ID(), batchToProve.BatchNumber)
log.Infof("Prover { ID [%s], addr [%s] } is going to be used to generate proof from batch [%d]", prover.ID(), prover.Addr(), batchToProve.BatchNumber)

log.Infof("Sending zki + batch to the prover, batchNumber: %d", batchToProve.BatchNumber)
log.Infof("Sending zki + batch to the prover, batchNumber [%d]", batchToProve.BatchNumber)
inputProver, err := a.buildInputProver(ctx, batchToProve)
if err != nil {
return false, fmt.Errorf("Failed to build input prover, err: %w", err)
return false, fmt.Errorf("Failed to build input prover, %w", err)
}

b, err := json.Marshal(inputProver)
if err != nil {
return false, fmt.Errorf("Failed to serialize input prover, err: %w", err)
return false, fmt.Errorf("Failed to serialize input prover, %w", err)
}

proof.InputProver = string(b)

log.Infof("Sending a batch to the prover, OLDSTATEROOT: %#x, OLDBATCHNUM: %d",
log.Infof("Sending a batch to the prover. OldStateRoot [%#x], OldBatchNum [%d]",
inputProver.PublicInputs.OldStateRoot, inputProver.PublicInputs.OldBatchNum)

genProofID, err := prover.BatchProof(inputProver)
Expand Down
6 changes: 4 additions & 2 deletions aggregator/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"

"github.com/0xPolygonHermez/zkevm-node/aggregator/pb"
ethmanTypes "github.com/0xPolygonHermez/zkevm-node/etherman/types"
"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -15,18 +16,19 @@ import (

type proverInterface interface {
ID() string
Addr() string
IsIdle() bool
BatchProof(input *pb.InputProver) (*string, error)
AggregatedProof(inputProof1, inputProof2 string) (*string, error)
FinalProof(inputProof string) (*string, error)
FinalProof(inputProof string, aggregatorAddr string) (*string, error)
WaitRecursiveProof(ctx context.Context, proofID string) (string, error)
WaitFinalProof(ctx context.Context, proofID string) (*pb.FinalProof, error)
}

// ethTxManager contains the methods required to send txs to
// ethereum.
type ethTxManager interface {
VerifyBatches(ctx context.Context, lastVerifiedBatch uint64, batchNum uint64, resGetProof *pb.FinalProof) (*types.Transaction, error)
VerifyBatches(ctx context.Context, lastVerifiedBatch uint64, batchNum uint64, inputs *ethmanTypes.FinalProofInputs) (*types.Transaction, error)
}

// etherman contains the methods required to interact with ethereum
Expand Down
31 changes: 0 additions & 31 deletions aggregator/mocks/mock_etherman.go

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

32 changes: 31 additions & 1 deletion aggregator/mocks/mock_ethtxmanager.go

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

Loading

0 comments on commit 8354a86

Please sign in to comment.