Skip to content

Commit

Permalink
feat: add rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Sep 17, 2024
1 parent 9677e51 commit fed06f7
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ jobs:
repository: 0xPolygon/kurtosis-cdk
ref: v0.2.9
path: "ext/kurtosis-cdk"

- name: Setup Bats and bats libs
uses: bats-core/[email protected]

- name: Test
run: make test-e2e-${{ matrix.e2e-group }}
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/0xPolygonHermez/zkevm-synchronizer-l1/etherman"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/log"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/rpcsync"
storage "github.com/0xPolygonHermez/zkevm-synchronizer-l1/state/storage"
syncconfig "github.com/0xPolygonHermez/zkevm-synchronizer-l1/synchronizer/config"
"github.com/mitchellh/mapstructure"
Expand All @@ -31,6 +32,7 @@ type Config struct {
SQLDB storage.Config `mapstructure:"SQLDB"`
Synchronizer syncconfig.Config `mapstructure:"Synchronizer"`
Etherman etherman.Config `mapstructure:"Etherman"`
RPC rpcsync.Config `mapstructure:"RPC"`
}

// Default parses the default configuration values.
Expand Down
4 changes: 4 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ const DefaultValues = `
[Etherman.Validium.RateLimit]
NumRequests = 900
Interval = "1s"
[RPC]
Enabled = false
Port = 8025
MaxRequestsPerIPAndSecond = 1000
`
6 changes: 6 additions & 0 deletions config/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/dataavailability"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/etherman"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/log"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/rpcsync"
storage "github.com/0xPolygonHermez/zkevm-synchronizer-l1/state/storage"
syncconfig "github.com/0xPolygonHermez/zkevm-synchronizer-l1/synchronizer/config"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/translator"
Expand Down Expand Up @@ -54,6 +55,11 @@ func TestDefault(t *testing.T) {
RateLimit: utils.NewRateLimitConfig(900, time.Second),
},
},
RPC: rpcsync.Config{
Enabled: false,
Port: 8025,
MaxRequestsPerIPAndSecond: 1000,
},
}
cfg, err := config.Default()
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions dataavailability/dataavailability.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ func (d *DataAvailability) GetBatchL2Data(batchNums []uint64, batchHashes []comm
if !d.isTrustedSequencer {
data, err := d.rpcData(batchNums, batchHashes, d.zkEVMClient.BatchesByNumbers)
if err != nil {
log.Warnf(failedDataRetrievalTemplate, batchNums, err.Error())
log.Warnf("Trusted RPC: "+failedDataRetrievalTemplate, batchNums, err.Error())
} else {
return data, nil
}
}
case External:
batchl2dataRaw, err := d.backend.GetSequence(d.ctx, batchHashes, dataAvailabilityMessage)
if err != nil {
log.Warnf(failedDataRetrievalTemplate, batchNums, err.Error())
log.Warnf("DAC Server: "+failedDataRetrievalTemplate, batchNums, err.Error())
return nil, err
}
return createBatchL2DataResonses(batchl2dataRaw, External), nil
Expand Down
1 change: 1 addition & 0 deletions dataavailability/datacommittee/datacommittee.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ func (d *DataCommitteeBackend) getCurrentDataCommitteeMembers() ([]DataCommittee
Addr: member.Addr,
URL: member.Url,
})
log.Debugf("member %d: %s, %s", i, member.Addr.Hex(), member.Url)
}
return members, nil
}
2 changes: 1 addition & 1 deletion jsonrpcclient/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ type BatchData struct {

// BatchDataResult is a list of BatchData for a BatchFilter
type BatchDataResult struct {
Data []*BatchData `json:"data"`
Data []*BatchData
}

// TransactionOrHash for union type of transaction and types.Hash
Expand Down
24 changes: 24 additions & 0 deletions jsonrpcclient/types/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package types

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
)

func TestMarshalArgUint64(t *testing.T) {
var value ArgUint64

Check failure on line 11 in jsonrpcclient/types/types_test.go

View workflow job for this annotation

GitHub Actions / lint

S1021: should merge variable declaration with assignment on next line (gosimple)
value = 1
a, err := json.Marshal(value)
require.NoError(t, err)
require.Equal(t, "\"0x1\"", string(a))
}

func TestUnMarshalArgUint64(t *testing.T) {
var value ArgUint64

Check failure on line 19 in jsonrpcclient/types/types_test.go

View workflow job for this annotation

GitHub Actions / lint

S1021: should merge variable declaration with assignment on next line (gosimple)
value = 1
err := json.Unmarshal([]byte("\"0x1\""), &value)
require.NoError(t, err)

}
4 changes: 3 additions & 1 deletion jsonrpcclient/zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/0xPolygonHermez/zkevm-synchronizer-l1/hex"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/jsonrpcclient/types"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/log"
"github.com/ethereum/go-ethereum/common"
)

Expand Down Expand Up @@ -92,9 +93,10 @@ func (c *Client) batchesByNumbers(_ context.Context, numbers []*big.Int, method
var result *types.BatchDataResult
err = json.Unmarshal(response.Result, &result)
if err != nil {
log.Warnf("Error unmarshalling batch data: %v result:%s", err, response.Result)
return nil, err
}

log.Debugf("Query: url:%s method:%s Response: %s", c.url, method, response.Result)
return result.Data, nil
}

Expand Down
7 changes: 7 additions & 0 deletions rpcsync/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package rpcsync

type Config struct {
Enabled bool `mapstructure:"Enabled"`
Port int `mapstructure:"Port"`
MaxRequestsPerIPAndSecond float64 `mapstructure:"MaxRequestsPerIPAndSecond"`
}
24 changes: 10 additions & 14 deletions rpcsync/rpc_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
package rpcsync

import (
"log"

jRPC "github.com/0xPolygon/cdk-rpc/rpc"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/log"
)

/*
Expand All @@ -27,22 +26,19 @@ curl -X POST http://localhost:1025/ -H "Con -application/json" -d '{"method":"de
curl -X POST http://localhost:1025/ -H "Con -application/json" -d '{"method":"debug_forceReorg", "params":[5159956], "id":1}'
*/

func StartRPC(state interface{}) {
cfg := jRPC.Config{
Port: 1025,
MaxRequestsPerIPAndSecond: 1000,
func buildRPCServiceEndPoints(sync interface{}, state interface{}) []jRPC.Service {
stateImpl, ok := state.(StateDebugInterface)
if !ok {
log.Fatal("State must implement StateDebugInterface")
}
server := jRPC.NewServer(cfg, []jRPC.Service{
endPoints := []jRPC.Service{
{
Name: "debug",
Service: &DebugEndpoints{
State: state.(StateDebugInterface),
State: stateImpl,
},
},
})
go func() {
if err := server.Start(); err != nil {
log.Fatal(err)
}
}()
}
endPoints = addSyncerEndpoint(sync, endPoints)
return endPoints
}
11 changes: 7 additions & 4 deletions rpcsync/rpc_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

package rpcsync

import "github.com/0xPolygonHermez/zkevm-synchronizer-l1/log"
import (
jRPC "github.com/0xPolygon/cdk-rpc/rpc"
)

// On release (no debug) there are no RPC server
func StartRPC(state interface{}) {
log.Debug("RPC server is disabled on release")
func buildRPCServiceEndPoints(sync interface{}, _ interface{}) []jRPC.Service {
endPoints := []jRPC.Service{}
endPoints = addSyncerEndpoint(sync, endPoints)
return endPoints
}
41 changes: 41 additions & 0 deletions rpcsync/rpc_share.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package rpcsync

import (
jRPC "github.com/0xPolygon/cdk-rpc/rpc"
"github.com/0xPolygonHermez/zkevm-synchronizer-l1/log"
)

func StartRPC(cfg Config, sync interface{}, state interface{}) {
if !cfg.Enabled {
log.Info("RPC server is disabled")
return
}
cfgRPC := jRPC.Config{
Port: cfg.Port,
MaxRequestsPerIPAndSecond: cfg.MaxRequestsPerIPAndSecond,
}

serverCfg := buildRPCServiceEndPoints(sync, state)

server := jRPC.NewServer(cfgRPC, serverCfg)
log.Infof("RPC server started on port %d (MaxRequestsPerIPAndSecond=%f)", cfg.Port, cfg.MaxRequestsPerIPAndSecond)
go func() {
if err := server.Start(); err != nil {
log.Fatal(err)
}
}()
}

func addSyncerEndpoint(sync interface{}, endPoints []jRPC.Service) []jRPC.Service {
syncImpl, ok := sync.(SyncInterface)
if !ok {
log.Fatal("State must implement Sync")
}
endPoints = append(endPoints, jRPC.Service{
Name: "sync",
Service: &SyncEndpoints{
Sync: syncImpl,
},
})
return endPoints
}
25 changes: 25 additions & 0 deletions rpcsync/sync_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package rpcsync

import (
"github.com/0xPolygon/cdk-rpc/rpc"
zkevm_synchronizer_l1 "github.com/0xPolygonHermez/zkevm-synchronizer-l1"
)

type SyncInterface interface {
// IsSynced returns true if the synchronizer is synced or false if it's not
IsSynced() bool
}

type SyncEndpoints struct {
Sync SyncInterface
}

// curl -X POST http://localhost:8025/ -H "Con -application/json" -d '{"method":"sync_version", "params":[], "id":1}'
func (b *SyncEndpoints) Version() (interface{}, rpc.Error) {
return zkevm_synchronizer_l1.Version, nil
}

// curl -X POST http://localhost:1025/ -H "Con -application/json" -d '{"method":"sync_isSynced", "params":[], "id":1}'
func (b *SyncEndpoints) IsSynced() (interface{}, rpc.Error) {
return b.Sync.IsSynced(), nil
}
2 changes: 1 addition & 1 deletion synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func NewSynchronizer(ctx context.Context, config config.Config) (Synchronizer, e
log.Debugf("Creating synchronizer adapter")
syncAdapter := NewSynchronizerAdapter(NewSyncrhronizerQueries(state, storage, ctx), sync)
log.Debugf("Starting RPC if enabled")
rpcsync.StartRPC(state)
rpcsync.StartRPC(config.RPC, syncAdapter, state)

return syncAdapter, nil
}
3 changes: 3 additions & 0 deletions test/config/config.local.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Environment = "development" # "production" or "development"
Level = "info"
Outputs = ["stderr"]
[SQLDB]
DriverName = "sqlite3"
DataSource = "file:/tmp/sync_db.sqlite"
[DB]
Name = "sync"
User = "test_user"
Expand Down

0 comments on commit fed06f7

Please sign in to comment.