Skip to content

Commit

Permalink
feat: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ToniRamirezM committed Jun 27, 2024
1 parent c7e9d0b commit d0de169
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 1,272 deletions.
8 changes: 1 addition & 7 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import (

"github.com/0xPolygon/cdk-rpc/rpc"
cdkTypes "github.com/0xPolygon/cdk-rpc/types"
"github.com/0xPolygon/cdk/aggregator/metrics"
"github.com/0xPolygon/cdk/aggregator/prover"
ethmanTypes "github.com/0xPolygon/cdk/aggregator/types"
"github.com/0xPolygon/cdk/config/types"
ethmanTypes "github.com/0xPolygon/cdk/etherman/types"
"github.com/0xPolygon/cdk/l1infotree"
"github.com/0xPolygon/cdk/log"
"github.com/0xPolygon/cdk/state"
Expand Down Expand Up @@ -419,8 +418,6 @@ func (a *Aggregator) Start(ctx context.Context) error {
a.ctx = ctx
a.exit = cancel

metrics.Register()

address := fmt.Sprintf("%s:%d", a.cfg.Host, a.cfg.Port)
lis, err := net.Listen("tcp", address)
if err != nil {
Expand Down Expand Up @@ -530,9 +527,6 @@ func (a *Aggregator) Stop() {
// Channel implements the bi-directional communication channel between the
// Prover client and the Aggregator server.
func (a *Aggregator) Channel(stream prover.AggregatorService_ChannelServer) error {
metrics.ConnectedProver()
defer metrics.DisconnectedProver()

ctx := stream.Context()
var proverAddr net.Addr
p, ok := peer.FromContext(ctx)
Expand Down
6 changes: 4 additions & 2 deletions aggregator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/0xPolygon/cdk/config/types"
"github.com/0xPolygon/cdk/db"
"github.com/0xPolygon/cdk/encoding"
"github.com/0xPolygon/cdk/log"
"github.com/0xPolygonHermez/zkevm-ethtx-manager/ethtxmanager"
syncronizerConfig "github.com/0xPolygonHermez/zkevm-synchronizer-l1/config"
Expand All @@ -25,6 +24,9 @@ const (

// L1 settlement backend
L1 SettlementBackend = "l1"

// TenToThePowerOf18 represents 1000000000000000000
TenToThePowerOf18 = 1000000000000000000
)

// TokenAmountWithDecimals is a wrapper type that parses token amount with decimals to big int
Expand All @@ -38,7 +40,7 @@ func (t *TokenAmountWithDecimals) UnmarshalText(data []byte) error {
if !ok {
return fmt.Errorf("failed to unmarshal string to float")
}
coin := new(big.Float).SetInt(big.NewInt(encoding.TenToThePowerOf18))
coin := new(big.Float).SetInt(big.NewInt(TenToThePowerOf18))
bigval := new(big.Float).Mul(amount, coin)
result := new(big.Int)
bigval.Int(result)
Expand Down
2 changes: 1 addition & 1 deletion aggregator/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"math/big"

"github.com/0xPolygon/cdk/aggregator/prover"
ethmanTypes "github.com/0xPolygon/cdk/etherman/types"
ethmanTypes "github.com/0xPolygon/cdk/aggregator/types"
"github.com/0xPolygon/cdk/state"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand Down
51 changes: 0 additions & 51 deletions aggregator/metrics/metrics.go

This file was deleted.

9 changes: 0 additions & 9 deletions aggregator/prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net"
"time"

"github.com/0xPolygon/cdk/aggregator/metrics"
"github.com/0xPolygon/cdk/config/types"
"github.com/0xPolygon/cdk/log"
)
Expand Down Expand Up @@ -103,8 +102,6 @@ func (p *Prover) SupportsForkID(forkID uint64) bool {
// BatchProof instructs the prover to generate a batch proof for the provided
// input. It returns the ID of the proof being computed.
func (p *Prover) BatchProof(input *StatelessInputProver) (*string, error) {
metrics.WorkingProver()

req := &AggregatorMessage{
Request: &AggregatorMessage_GenStatelessBatchProofRequest{
GenStatelessBatchProofRequest: &GenStatelessBatchProofRequest{Input: input},
Expand Down Expand Up @@ -136,8 +133,6 @@ func (p *Prover) BatchProof(input *StatelessInputProver) (*string, error) {
// AggregatedProof instructs the prover to generate an aggregated proof from
// the two inputs provided. It returns the ID of the proof being computed.
func (p *Prover) AggregatedProof(inputProof1, inputProof2 string) (*string, error) {
metrics.WorkingProver()

req := &AggregatorMessage{
Request: &AggregatorMessage_GenAggregatedProofRequest{
GenAggregatedProofRequest: &GenAggregatedProofRequest{
Expand Down Expand Up @@ -176,8 +171,6 @@ func (p *Prover) AggregatedProof(inputProof1, inputProof2 string) (*string, erro
// FinalProof instructs the prover to generate a final proof for the given
// input. It returns the ID of the proof being computed.
func (p *Prover) FinalProof(inputProof string, aggregatorAddr string) (*string, error) {
metrics.WorkingProver()

req := &AggregatorMessage{
Request: &AggregatorMessage_GenFinalProofRequest{
GenFinalProofRequest: &GenFinalProofRequest{
Expand Down Expand Up @@ -270,8 +263,6 @@ func (p *Prover) WaitFinalProof(ctx context.Context, proofID string) (*FinalProo
// waitProof waits for a proof to be generated by the prover and returns the
// prover response.
func (p *Prover) waitProof(ctx context.Context, proofID string) (*GetProofResponse, error) {
defer metrics.IdlingProver()

req := &AggregatorMessage{
Request: &AggregatorMessage_GetProofRequest{
GetProofRequest: &GetProofRequest{
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion dataavailability/dataavailability.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dataavailability
import (
"context"

"github.com/0xPolygon/cdk/etherman/types"
"github.com/0xPolygon/cdk/aggregator/types"
)

// DataAvailability implements an abstract data availability integration
Expand Down
110 changes: 0 additions & 110 deletions encoding/encoding.go

This file was deleted.

14 changes: 11 additions & 3 deletions etherman/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package etherman

import (
"context"
"encoding/hex"
"errors"
"fmt"
"math/big"
"strings"

"github.com/0xPolygon/cdk/encoding"
ethmanTypes "github.com/0xPolygon/cdk/etherman/types"
ethmanTypes "github.com/0xPolygon/cdk/aggregator/types"
"github.com/0xPolygon/cdk/log"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -101,7 +101,7 @@ func convertProof(p string) ([24][32]byte, error) {
proof := [24][32]byte{}
for i := 0; i < 24; i++ {
data := p[i*64 : (i+1)*64]
p, err := encoding.DecodeBytes(&data)
p, err := DecodeBytes(&data)
if err != nil {
return [24][32]byte{}, fmt.Errorf("failed to decode proof, err: %w", err)
}
Expand All @@ -111,3 +111,11 @@ func convertProof(p string) ([24][32]byte, error) {
}
return proof, nil
}

// DecodeBytes decodes a hex string into a []byte
func DecodeBytes(val *string) ([]byte, error) {
if val == nil {
return []byte{}, nil
}
return hex.DecodeString(strings.TrimPrefix(*val, "0x"))
}
2 changes: 1 addition & 1 deletion etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"time"

ethmanTypes "github.com/0xPolygon/cdk/aggregator/types"
"github.com/0xPolygon/cdk/etherman/etherscan"
"github.com/0xPolygon/cdk/etherman/ethgasstation"
"github.com/0xPolygon/cdk/etherman/smartcontracts/dataavailabilityprotocol"
Expand All @@ -21,7 +22,6 @@ import (
"github.com/0xPolygon/cdk/etherman/smartcontracts/polygonrollupmanager"
"github.com/0xPolygon/cdk/etherman/smartcontracts/polygonzkevm"
"github.com/0xPolygon/cdk/etherman/smartcontracts/polygonzkevmglobalexitroot"
ethmanTypes "github.com/0xPolygon/cdk/etherman/types"
"github.com/0xPolygon/cdk/log"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down
11 changes: 8 additions & 3 deletions etherman/etherscan/etherscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import (
"io"
"math/big"
"net/http"
)

"github.com/0xPolygon/cdk/encoding"
const (
// Base10 decimal base
Base10 = 10
// Gwei represents 1000000000 wei
Gwei = 1000000000
)

type etherscanResponse struct {
Expand Down Expand Up @@ -72,6 +77,6 @@ func (e *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
if err != nil {
return big.NewInt(0), fmt.Errorf("Reading body failed: %w", err)
}
fgp, _ := big.NewInt(0).SetString(resBody.Result.FastGasPrice, encoding.Base10)
return new(big.Int).Mul(fgp, big.NewInt(encoding.Gwei)), nil
fgp, _ := big.NewInt(0).SetString(resBody.Result.FastGasPrice, Base10)
return new(big.Int).Mul(fgp, big.NewInt(Gwei)), nil
}
7 changes: 5 additions & 2 deletions etherman/ethgasstation/ethgasstation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"io"
"math/big"
"net/http"
)

"github.com/0xPolygon/cdk/encoding"
const (
// Gwei represents 1000000000 wei
Gwei = 1000000000
)

type ethGasStationResponse struct {
Expand Down Expand Up @@ -60,5 +63,5 @@ func (e *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
return big.NewInt(0), fmt.Errorf("Reading body failed: %w", err)
}
fgp := big.NewInt(0).SetUint64(resBody.GasPrice.Instant)
return new(big.Int).Mul(fgp, big.NewInt(encoding.Gwei)), nil
return new(big.Int).Mul(fgp, big.NewInt(Gwei)), nil
}
16 changes: 0 additions & 16 deletions metrics/api.go

This file was deleted.

Loading

0 comments on commit d0de169

Please sign in to comment.