Skip to content

Commit

Permalink
Trusted state synchronization via Broadcast service (0xPolygonHermez#918
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tclemos authored Jul 27, 2022
1 parent 58c6d9c commit beb9b73
Show file tree
Hide file tree
Showing 26 changed files with 519 additions and 138 deletions.
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ DOCKERCOMPOSEEXPLORER := zkevm-explorer
DOCKERCOMPOSEEXPLORERDB := zkevm-explorer-db
DOCKERCOMPOSEEXPLORERRPC := zkevm-explorer-json-rpc
DOCKERCOMPOSEZKPROVER := zkevm-prover
DOCKERCOMPOSEPERMISSIONLESSDB := zkevm-permissionless-db
DOCKERCOMPOSEPERMISSIONLESSNODE := zkevm-permissionless-node

RUNDB := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEDB)
RUNSEQUENCER := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEAPPSEQ)
Expand All @@ -26,6 +28,9 @@ RUNEXPLORERDB := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEEXPLORERDB)
RUNEXPLORERJSONRPC := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEEXPLORERRPC)
RUNZKPROVER := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEZKPROVER)

RUNPERMISSIONLESSDB := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEPERMISSIONLESSDB)
RUNPERMISSIONLESSNODE := $(DOCKERCOMPOSE) up -d $(DOCKERCOMPOSEPERMISSIONLESSNODE)

RUN := $(DOCKERCOMPOSE) up -d

STOPDB := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEDB) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEDB)
Expand All @@ -42,6 +47,9 @@ STOPEXPLORERDB := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEEXPLORERDB) && $(DOCKERCO
STOPEXPLORERRPC := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEEXPLORERRPC) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEEXPLORERRPC)
STOPZKPROVER := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEZKPROVER) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEZKPROVER)

STOPPERMISSIONLESSDB := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEPERMISSIONLESSDB) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEPERMISSIONLESSDB)
STOPPERMISSIONLESSNODE := $(DOCKERCOMPOSE) stop $(DOCKERCOMPOSEPERMISSIONLESSNODE) && $(DOCKERCOMPOSE) rm -f $(DOCKERCOMPOSEPERMISSIONLESSNODE)

STOP := $(DOCKERCOMPOSE) down --remove-orphans

VERSION := $(shell git describe --tags --always)
Expand Down Expand Up @@ -225,6 +233,16 @@ run-seq:
stop-broadcast: ## Stops the broadcast service
$(STOPBROADCAST)

.PHONY: run-permissionless
run-permissionless: ## Runs the permissionless node
$(RUNPERMISSIONLESSDB)
$(RUNPERMISSIONLESSNODE)

.PHONY: stop-permissionless
stop-permissionless: ## Stops the permissionless node
$(STOPPERMISSIONLESSNODE)
$(STOPPERMISSIONLESSDB)

.PHONY: init-network
init-network: ## Initializes the network
go run ./scripts/init_network/main.go .
Expand Down
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ func main() {
&cli.StringSliceFlag{
Name: config.FlagHTTPAPI,
Aliases: []string{"ha"},
Usage: fmt.Sprintf("List of JSON RPC apis to be exposed by the server: --http.api=%v,%v,%v,%v,%v,%v", jsonrpc.APIEth, jsonrpc.APINet, jsonrpc.APIDebug, jsonrpc.APIHez, jsonrpc.APITxPool, jsonrpc.APIWeb3),
Usage: fmt.Sprintf("List of JSON RPC apis to be exposed by the server: --http.api=%v,%v,%v,%v,%v,%v", jsonrpc.APIEth, jsonrpc.APINet, jsonrpc.APIDebug, jsonrpc.APIZKEVM, jsonrpc.APITxPool, jsonrpc.APIWeb3),
Required: false,
Value: cli.NewStringSlice(jsonrpc.APIEth, jsonrpc.APINet, jsonrpc.APIHez, jsonrpc.APITxPool, jsonrpc.APIWeb3),
Value: cli.NewStringSlice(jsonrpc.APIEth, jsonrpc.APINet, jsonrpc.APIZKEVM, jsonrpc.APITxPool, jsonrpc.APIWeb3),
},
}
app.Commands = []*cli.Command{
Expand Down
14 changes: 7 additions & 7 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func start(cliCtx *cli.Context) error {
go runJSONRPCServer(*c, npool, st, gpe, apis)
case SYNCHRONIZER:
log.Info("Running synchronizer")
go runSynchronizer(c.NetworkConfig, etherman, st, c.Synchronizer, ch)
go runSynchronizer(*c, etherman, st, ch)
case BROADCAST:
log.Info("Running broadcast service")
go runBroadcastServer(c.BroadcastServer, st)
Expand Down Expand Up @@ -140,14 +140,14 @@ func newEtherman(c config.Config) (*etherman.Client, error) {
return etherman, nil
}

func runSynchronizer(networkConfig config.NetworkConfig, etherman *etherman.Client, st *state.State, cfg synchronizer.Config, reorgTrustedStateChan chan struct{}) {
func runSynchronizer(cfg config.Config, etherman *etherman.Client, st *state.State, reorgTrustedStateChan chan struct{}) {
genesis := state.Genesis{
Balances: networkConfig.Genesis.Balances,
SmartContracts: networkConfig.Genesis.SmartContracts,
Storage: networkConfig.Genesis.Storage,
Nonces: networkConfig.Genesis.Nonces,
Balances: cfg.NetworkConfig.Genesis.Balances,
SmartContracts: cfg.NetworkConfig.Genesis.SmartContracts,
Storage: cfg.NetworkConfig.Genesis.Storage,
Nonces: cfg.NetworkConfig.Genesis.Nonces,
}
sy, err := synchronizer.NewSynchronizer(etherman, st, networkConfig.GenBlockNumber, genesis, reorgTrustedStateChan, cfg)
sy, err := synchronizer.NewSynchronizer(cfg.IsTrustedSequencer, etherman, st, cfg.NetworkConfig.GenBlockNumber, genesis, reorgTrustedStateChan, cfg.Synchronizer)
if err != nil {
log.Fatal(err)
}
Expand Down
7 changes: 4 additions & 3 deletions config/config.debug.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
IsTrustedSequencer = true

[Log]
Level = "debug"
Outputs = ["stdout"]
Expand All @@ -21,10 +23,12 @@ Host = "0.0.0.0"
Port = 8123
MaxRequestsPerIPAndSecond = 100
SequencerNodeURI = ""
BroadcastURI = "127.0.0.1:61090"

[Synchronizer]
SyncInterval = "5s"
SyncChunkSize = 100
TrustedSequencerURI = ""

[Sequencer]
WaitPeriodPoolIsEmpty = "15s"
Expand Down Expand Up @@ -63,6 +67,3 @@ URI = "127.0.0.1:50071"
[BroadcastServer]
Host = "0.0.0.0"
Port = 61090

[BroadcastClient]
URI = "127.0.0.1:61090"
32 changes: 16 additions & 16 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ const (

// Config represents the configuration of the entire Hermez Node
type Config struct {
Log log.Config
Database db.Config
Etherman etherman.Config
EthTxManager ethtxmanager.Config
RPC jsonrpc.Config
Synchronizer synchronizer.Config
Sequencer sequencer.Config
PriceGetter pricegetter.Config
Aggregator aggregator.Config
Prover proverclient.Config
NetworkConfig NetworkConfig
GasPriceEstimator gasprice.Config
Executor executor.Config
BroadcastServer broadcast.ServerConfig
BroadcastClient broadcast.ClientConfig
MTClient merkletree.Config
IsTrustedSequencer bool `mapstructure:"IsTrustedSequencer"`
Log log.Config
Database db.Config
Etherman etherman.Config
EthTxManager ethtxmanager.Config
RPC jsonrpc.Config
Synchronizer synchronizer.Config
Sequencer sequencer.Config
PriceGetter pricegetter.Config
Aggregator aggregator.Config
Prover proverclient.Config
NetworkConfig NetworkConfig
GasPriceEstimator gasprice.Config
Executor executor.Config
BroadcastServer broadcast.ServerConfig
MTClient merkletree.Config
}

// Load loads the configuration
Expand Down
7 changes: 4 additions & 3 deletions config/config.local.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
IsTrustedSequencer = true

[Log]
Level = "debug"
Outputs = ["stdout"]
Expand All @@ -21,10 +23,12 @@ Host = "0.0.0.0"
Port = 8123
MaxRequestsPerIPAndSecond = 5000
SequencerNodeURI = ""
BroadcastURI = "127.0.0.1:61090"

[Synchronizer]
SyncInterval = "1s"
SyncChunkSize = 100
TrustedSequencerURI = ""

[Sequencer]
WaitPeriodPoolIsEmpty = "15s"
Expand Down Expand Up @@ -63,6 +67,3 @@ URI = "zkevm-prover:50071"
[BroadcastServer]
Host = "0.0.0.0"
Port = 61090

[BroadcastClient]
URI = "127.0.0.1:61090"
8 changes: 4 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ func Test_Defaults(t *testing.T) {
path: "RPC.MaxRequestsPerIPAndSecond",
expectedValue: float64(50),
},
{
path: "RPC.BroadcastURI",
expectedValue: "127.0.0.1:61090",
},
{
path: "Executor.URI",
expectedValue: "127.0.0.1:50071",
Expand All @@ -148,10 +152,6 @@ func Test_Defaults(t *testing.T) {
path: "BroadcastServer.Port",
expectedValue: 61090,
},
{
path: "BroadcastClient.URI",
expectedValue: "127.0.0.1:61090",
},
}

ctx := cli.NewContext(cli.NewApp(), flag.NewFlagSet("", flag.PanicOnError), nil)
Expand Down
7 changes: 4 additions & 3 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

// DefaultValues is the default configuration
const DefaultValues = `
IsTrustedSequencer = false
[Log]
Level = "debug"
Outputs = ["stdout"]
Expand Down Expand Up @@ -29,10 +31,12 @@ Host = "0.0.0.0"
Port = 8123
MaxRequestsPerIPAndSecond = 50
SequencerNodeURI = ""
BroadcastURI = "127.0.0.1:61090"
[Synchronizer]
SyncInterval = "0s"
SyncChunkSize = 100
TrustedSequencerURI = ""
[Sequencer]
WaitPeriodPoolIsEmpty = "15s"
Expand Down Expand Up @@ -83,7 +87,4 @@ URI = "127.0.0.1:50071"
[BroadcastServer]
Host = "0.0.0.0"
Port = 61090
[BroadcastClient]
URI = "127.0.0.1:61090"
`
60 changes: 59 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ services:
- ZKEVM_NODE_DATABASE_PASSWORD=test_password
- ZKEVM_NODE_DATABASE_NAME=test_db
- ZKEVM_NODE_DATABASE_HOST=zkevm-db
- ZKEVM_NODE_RPC_BROADCASTURI=zkevm-broadcast:61090
volumes:
- ./config/config.local.toml:/app/config.toml
command:
- "/bin/sh"
- "-c"
- "/app/zkevm-node run --network local --cfg /app/config.toml --components rpc"

zkevm-non-sequencer-json-rpc:
container_name: zkevm-non-sequencer-json-rpc
image: zkevm-node
ports:
- 8125:8125
environment:
- ZKEVM_NODE_DATABASE_USER=test_user
- ZKEVM_NODE_DATABASE_PASSWORD=test_password
- ZKEVM_NODE_DATABASE_NAME=test_db
- ZKEVM_NODE_DATABASE_HOST=zkevm-db
- ZKEVM_NODE_RPC_SEQUENCERNODEURI=http://zkevm-json-rpc:8123
volumes:
- ./config/config.local.toml:/app/config.toml
command:
Expand Down Expand Up @@ -202,4 +221,43 @@ services:
command:
- "/bin/sh"
- "-c"
- "/app/zkevm-node approve --am 10000000000000000 -y --network local --cfg /app/config.toml"
- "/app/zkevm-node approve --am 10000000000000000 -y --network local --cfg /app/config.toml"

zkevm-permissionless-db:
container_name: zkevm-permissionless-db
image: postgres
deploy:
resources:
limits:
memory: 2G
reservations:
memory: 1G
ports:
- 5434:5432
environment:
- POSTGRES_USER=test_user
- POSTGRES_PASSWORD=test_password
- POSTGRES_DB=test_db
command: ["postgres", "-N", "500"]

zkevm-permissionless-node:
container_name: zkevm-permissionless-node
image: zkevm-node
ports:
- 8126:8126
environment:
- ZKEVM_NODE_TRUSTED=false
- ZKEVM_NODE_DATABASE_USER=test_user
- ZKEVM_NODE_DATABASE_PASSWORD=test_password
- ZKEVM_NODE_DATABASE_NAME=test_db
- ZKEVM_NODE_DATABASE_HOST=zkevm-permissionless-db
- ZKEVM_NODE_ETHERMAN_PRIVATEKEYPATH=/pk/keystore
- ZKEVM_NODE_RPC_PORT=8126
volumes:
- ./test/test.keystore:/pk/keystore
- ./config/config.local.toml:/app/config.toml
command:
- "/bin/sh"
- "-c"
- "/app/zkevm-node run --network local --cfg /app/config.toml --components \"rpc,synchronizer\""

5 changes: 5 additions & 0 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,11 @@ func (etherMan *Client) ApproveMatic(maticAmount *big.Int, to common.Address) (*
return tx, nil
}

// GetTrustedSequencerURL Gets the trusted sequencer url from rollup smc
func (etherMan *Client) GetTrustedSequencerURL() (string, error) {
return etherMan.PoE.TrustedSequencerURL(&bind.CallOpts{Pending: false})
}

// VerifyBatch function allows the aggregator send the proof for a batch and consolidate it
func (etherMan *Client) verifyBatch(opts *bind.TransactOpts, batchNumber uint64, resGetProof *pb.GetProofResponse) (*types.Transaction, error) {
publicInputs := resGetProof.Public.PublicInputs
Expand Down
4 changes: 3 additions & 1 deletion jsonrpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ type Config struct {

// SequencerNodeURI is used allow Non-Sequencer nodes
// to relay transactions to the Sequencer node
SequencerNodeURI string `mapstructure:"URI"`
SequencerNodeURI string `mapstructure:"SequencerNodeURI"`

BroadcastURI string `mapstructure:"BroadcastURI"`
}
10 changes: 5 additions & 5 deletions jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const (
APINet = "net"
// APIDebug represents the debug API prefix.
APIDebug = "debug"
// APIHez represents the hez API prefix.
APIHez = "hez"
// APIZKEVM represents the zkevm API prefix.
APIZKEVM = "zkevm"
// APITxPool represents the txpool API prefix.
APITxPool = "txpool"
// APIWeb3 represents the web3 API prefix.
Expand Down Expand Up @@ -51,9 +51,9 @@ func NewServer(cfg Config, p jsonRPCTxPool, s stateInterface,
handler.registerService(APINet, netEndpoints)
}

if _, ok := apis[APIHez]; ok {
hezEndpoints := &Hez{state: s}
handler.registerService(APIHez, hezEndpoints)
if _, ok := apis[APIZKEVM]; ok {
hezEndpoints := &ZKEVM{state: s, config: cfg}
handler.registerService(APIZKEVM, hezEndpoints)
}

if _, ok := apis[APITxPool]; ok {
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func newMockedServer(t *testing.T, cfg Config) (*mockedServer, *mocks, *ethclien
APIEth: true,
APINet: true,
APIDebug: true,
APIHez: true,
APIZKEVM: true,
APITxPool: true,
APIWeb3: true,
}
Expand Down
17 changes: 12 additions & 5 deletions jsonrpc/hez.go → jsonrpc/zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"github.com/jackc/pgx/v4"
)

// Hez contains implementations for the "hez" RPC endpoints
type Hez struct {
state stateInterface
txMan dbTxManager
// ZKEVM contains implementations for the "zkevm" RPC endpoints
type ZKEVM struct {
config Config
state stateInterface
txMan dbTxManager
}

// ConsolidatedBlockNumber returns current block number for consolidated blocks
func (h *Hez) ConsolidatedBlockNumber() (interface{}, rpcError) {
func (h *ZKEVM) ConsolidatedBlockNumber() (interface{}, rpcError) {
return h.txMan.NewDbTxScope(h.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
lastBlockNumber, err := h.state.GetLastConsolidatedL2BlockNumber(ctx, dbTx)
if err != nil {
Expand All @@ -27,3 +28,9 @@ func (h *Hez) ConsolidatedBlockNumber() (interface{}, rpcError) {
return hex.EncodeUint64(lastBlockNumber), nil
})
}

// GetBroadcastURI returns the IP:PORT of the broadcast service provided
// by the Trusted Sequencer JSON RPC server
func (h *ZKEVM) GetBroadcastURI() (interface{}, rpcError) {
return h.config.BroadcastURI, nil
}
2 changes: 1 addition & 1 deletion jsonrpc/hez_test.go → jsonrpc/zkevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestConsolidatedBlockNumber(t *testing.T) {
tc := testCase
tc.SetupMocks(m)

res, err := s.JSONRPCCall("hez_consolidatedBlockNumber")
res, err := s.JSONRPCCall("zkevm_consolidatedBlockNumber")
require.NoError(t, err)

if res.Result != nil {
Expand Down
Loading

0 comments on commit beb9b73

Please sign in to comment.