Skip to content

Commit

Permalink
EVM-352 Event tracker block finalization (0xPolygon#1228)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcrevar authored Feb 27, 2023
1 parent 3171aa5 commit 9a0c634
Show file tree
Hide file tree
Showing 19 changed files with 679 additions and 103 deletions.
9 changes: 8 additions & 1 deletion command/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ type Config struct {
JSONRPCBatchRequestLimit uint64 `json:"json_rpc_batch_request_limit" yaml:"json_rpc_batch_request_limit"`
JSONRPCBlockRangeLimit uint64 `json:"json_rpc_block_range_limit" yaml:"json_rpc_block_range_limit"`
JSONLogFormat bool `json:"json_log_format" yaml:"json_log_format"`
Relayer bool `json:"relayer" yaml:"relayer"`

Relayer bool `json:"relayer" yaml:"relayer"`
NumBlockConfirmations uint64 `json:"num_block_confirmations" yaml:"num_block_confirmations"`
}

// Telemetry holds the config details for metric services.
Expand Down Expand Up @@ -76,6 +78,10 @@ const (
// DefaultJSONRPCBlockRangeLimit maximum block range allowed for json_rpc
// requests with fromBlock/toBlock values (e.g. eth_getLogs)
DefaultJSONRPCBlockRangeLimit uint64 = 1000

// DefaultNumBlockConfirmations minimal number of child blocks required for the parent block to be considered final
// on ethereum epoch lasts for 32 blocks. more details: https://www.alchemy.com/overviews/ethereum-commitment-levels
DefaultNumBlockConfirmations uint64 = 64
)

// DefaultConfig returns the default server configuration
Expand Down Expand Up @@ -113,6 +119,7 @@ func DefaultConfig() *Config {
JSONRPCBatchRequestLimit: DefaultJSONRPCBatchRequestLimit,
JSONRPCBlockRangeLimit: DefaultJSONRPCBlockRangeLimit,
Relayer: false,
NumBlockConfirmations: DefaultNumBlockConfirmations,
}
}

Expand Down
8 changes: 6 additions & 2 deletions command/server/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ const (
devFlag = "dev"
corsOriginFlag = "access-control-allow-origins"
logFileLocationFlag = "log-to"
relayerFlag = "relayer"

relayerFlag = "relayer"
numBlockConfirmationsFlag = "num-block-confirmations"
)

// Flags that are deprecated, but need to be preserved for
Expand Down Expand Up @@ -181,6 +183,8 @@ func (p *serverParams) generateConfig() *server.Config {
LogLevel: hclog.LevelFromString(p.rawConfig.LogLevel),
JSONLogFormat: p.rawConfig.JSONLogFormat,
LogFilePath: p.logFileLocation,
Relayer: p.relayer,

Relayer: p.relayer,
NumBlockConfirmations: p.rawConfig.NumBlockConfirmations,
}
}
7 changes: 7 additions & 0 deletions command/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ func setFlags(cmd *cobra.Command) {
"start the state sync relayer service (PolyBFT only)",
)

cmd.Flags().Uint64Var(
&params.rawConfig.NumBlockConfirmations,
numBlockConfirmationsFlag,
defaultConfig.NumBlockConfirmations,
"minimal number of child blocks required for the parent block to be considered final",
)

setLegacyFlags(cmd)

setDevFlags(cmd)
Expand Down
2 changes: 2 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type Params struct {
Logger hclog.Logger
SecretsManager secrets.SecretsManager
BlockTime uint64

NumBlockConfirmations uint64
}

// Factory is the factory function to create a discovery consensus
Expand Down
30 changes: 16 additions & 14 deletions consensus/polybft/consensus_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ type guardedDataDTO struct {

// runtimeConfig is a struct that holds configuration data for given consensus runtime
type runtimeConfig struct {
PolyBFTConfig *PolyBFTConfig
DataDir string
Key *wallet.Key
State *State
blockchain blockchainBackend
polybftBackend polybftBackend
txPool txPoolInterface
bridgeTopic topic
PolyBFTConfig *PolyBFTConfig
DataDir string
Key *wallet.Key
State *State
blockchain blockchainBackend
polybftBackend polybftBackend
txPool txPoolInterface
bridgeTopic topic
numBlockConfirmations uint64
}

// consensusRuntime is a struct that provides consensus runtime features like epoch, state and event management
Expand Down Expand Up @@ -160,12 +161,13 @@ func (c *consensusRuntime) initStateSyncManager(logger hcf.Logger) error {
logger,
c.config.State,
&stateSyncConfig{
key: c.config.Key,
stateSenderAddr: c.config.PolyBFTConfig.Bridge.BridgeAddr,
jsonrpcAddr: c.config.PolyBFTConfig.Bridge.JSONRPCEndpoint,
dataDir: c.config.DataDir,
topic: c.config.bridgeTopic,
maxCommitmentSize: maxCommitmentSize,
key: c.config.Key,
stateSenderAddr: c.config.PolyBFTConfig.Bridge.BridgeAddr,
jsonrpcAddr: c.config.PolyBFTConfig.Bridge.JSONRPCEndpoint,
dataDir: c.config.DataDir,
topic: c.config.bridgeTopic,
maxCommitmentSize: maxCommitmentSize,
numBlockConfirmations: c.config.numBlockConfirmations,
},
)

Expand Down
17 changes: 9 additions & 8 deletions consensus/polybft/polybft.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,15 @@ func (p *Polybft) Start() error {
// initRuntime creates consensus runtime
func (p *Polybft) initRuntime() error {
runtimeConfig := &runtimeConfig{
PolyBFTConfig: p.consensusConfig,
Key: p.key,
DataDir: p.dataDir,
State: p.state,
blockchain: p.blockchain,
polybftBackend: p,
txPool: p.txPool,
bridgeTopic: p.bridgeTopic,
PolyBFTConfig: p.consensusConfig,
Key: p.key,
DataDir: p.dataDir,
State: p.state,
blockchain: p.blockchain,
polybftBackend: p,
txPool: p.txPool,
bridgeTopic: p.bridgeTopic,
numBlockConfirmations: p.config.NumBlockConfirmations,
}

runtime, err := newConsensusRuntime(p.logger, runtimeConfig)
Expand Down
14 changes: 8 additions & 6 deletions consensus/polybft/state_sync_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ func (n *dummyStateSyncManager) GetStateSyncProof(stateSyncID uint64) (types.Pro

// stateSyncConfig holds the configuration data of state sync manager
type stateSyncConfig struct {
stateSenderAddr types.Address
jsonrpcAddr string
dataDir string
topic topic
key *wallet.Key
maxCommitmentSize uint64
stateSenderAddr types.Address
jsonrpcAddr string
dataDir string
topic topic
key *wallet.Key
maxCommitmentSize uint64
numBlockConfirmations uint64
}

var _ StateSyncManager = (*stateSyncManager)(nil)
Expand Down Expand Up @@ -130,6 +131,7 @@ func (s *stateSyncManager) initTracker() error {
s.config.jsonrpcAddr,
ethgo.Address(s.config.stateSenderAddr),
s,
s.config.numBlockConfirmations,
s.logger)

go func() {
Expand Down
1 change: 1 addition & 0 deletions consensus/polybft/statesyncrelayer/state_sync_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (r *StateSyncRelayer) Start() error {
r.rpcEndpoint,
r.stateReceiverAddr,
r,
0, // sidechain (Polygon POS) is instant finality, so no need to wait
r.logger,
)

Expand Down
21 changes: 19 additions & 2 deletions e2e-polybft/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ func checkLogs(
}

func TestE2E_Bridge_MainWorkflow(t *testing.T) {
const num = 10
const (
num = 10
numBlockConfirmations = 4
)

var (
accounts = make([]ethgo.Key, num)
Expand All @@ -71,7 +74,9 @@ func TestE2E_Bridge_MainWorkflow(t *testing.T) {
amounts[i] = fmt.Sprintf("%d", 100)
}

cluster := framework.NewTestCluster(t, 5, framework.WithBridge(), framework.WithPremine(premine[:]...))
cluster := framework.NewTestCluster(t, 5,
framework.WithBridge(), framework.WithPremine(premine[:]...),
framework.WithNumBlockConfirmations(numBlockConfirmations))
defer cluster.Stop()

// wait for a couple of blocks
Expand All @@ -87,6 +92,18 @@ func TestE2E_Bridge_MainWorkflow(t *testing.T) {
),
)

require.NoError(t, cluster.WaitForBlock(2+numBlockConfirmations*2, 2*time.Minute))

// send again to trigger previous transactions
require.NoError(
t,
cluster.EmitTransfer(
contracts.NativeTokenContract.String(),
strings.Join(wallets[:], ","),
strings.Join(amounts[:], ","),
),
)

// wait for a few more sprints
require.NoError(t, cluster.WaitForBlock(35, 2*time.Minute))

Expand Down
9 changes: 9 additions & 0 deletions e2e-polybft/framework/test-cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ type TestClusterConfig struct {
PropertyBaseTests bool
SecretsCallback func([]types.Address, *TestClusterConfig)

NumBlockConfirmations uint64

logsDirOnce sync.Once
}

Expand Down Expand Up @@ -224,6 +226,12 @@ func WithPropertyBaseTests(propertyBaseTests bool) ClusterOption {
}
}

func WithNumBlockConfirmations(numBlockConfirmations uint64) ClusterOption {
return func(h *TestClusterConfig) {
h.NumBlockConfirmations = numBlockConfirmations
}
}

func isTrueEnv(e string) bool {
return strings.ToLower(os.Getenv(e)) == "true"
}
Expand Down Expand Up @@ -389,6 +397,7 @@ func (c *TestCluster) InitTestServer(t *testing.T, i int, isValidator bool, rela
config.P2PPort = c.getOpenPort()
config.LogLevel = logLevel
config.Relayer = relayer
config.NumBlockConfirmations = c.Config.NumBlockConfirmations
})

// watch the server for stop signals. It is important to fix the specific
Expand Down
21 changes: 12 additions & 9 deletions e2e-polybft/framework/test-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ import (
)

type TestServerConfig struct {
Name string
JSONRPCPort int64
GRPCPort int64
P2PPort int64
Seal bool
DataDir string
Chain string
LogLevel string
Relayer bool
Name string
JSONRPCPort int64
GRPCPort int64
P2PPort int64
Seal bool
DataDir string
Chain string
LogLevel string
Relayer bool
NumBlockConfirmations uint64
}

type TestServerConfigCallback func(*TestServerConfig)
Expand Down Expand Up @@ -124,6 +125,8 @@ func (t *TestServer) Start() {
"--grpc-address", fmt.Sprintf("localhost:%d", config.GRPCPort),
// enable jsonrpc
"--jsonrpc", fmt.Sprintf(":%d", config.JSONRPCPort),
// minimal number of child blocks required for the parent block to be considered final
"--num-block-confirmations", strconv.FormatUint(config.NumBlockConfirmations, 10),
}

if len(config.LogLevel) > 0 {
Expand Down
5 changes: 5 additions & 0 deletions helper/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,8 @@ func EncodeUint64ToBytes(value uint64) []byte {

return result
}

// EncodeBytesToUint64 big endian byte slice to uint64
func EncodeBytesToUint64(b []byte) uint64 {
return binary.BigEndian.Uint64(b)
}
8 changes: 4 additions & 4 deletions scripts/cluster
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ function createGenesis() {
}

function startServerFromBinary() {
./polygon-edge server --data-dir ./test-chain-1 --chain genesis.json --grpc-address :10000 --libp2p :30301 --jsonrpc :10002 --seal --log-level DEBUG &
./polygon-edge server --data-dir ./test-chain-2 --chain genesis.json --grpc-address :20000 --libp2p :30302 --jsonrpc :20002 --seal --log-level DEBUG &
./polygon-edge server --data-dir ./test-chain-3 --chain genesis.json --grpc-address :30000 --libp2p :30303 --jsonrpc :30002 --seal --log-level DEBUG &
./polygon-edge server --data-dir ./test-chain-4 --chain genesis.json --grpc-address :40000 --libp2p :30304 --jsonrpc :40002 --seal --log-level DEBUG &
./polygon-edge server --data-dir ./test-chain-1 --chain genesis.json --grpc-address :10000 --libp2p :30301 --jsonrpc :10002 --num-block-confirmations 2 --seal --log-level DEBUG &
./polygon-edge server --data-dir ./test-chain-2 --chain genesis.json --grpc-address :20000 --libp2p :30302 --jsonrpc :20002 --num-block-confirmations 2 --seal --log-level DEBUG &
./polygon-edge server --data-dir ./test-chain-3 --chain genesis.json --grpc-address :30000 --libp2p :30303 --jsonrpc :30002 --num-block-confirmations 2 --seal --log-level DEBUG &
./polygon-edge server --data-dir ./test-chain-4 --chain genesis.json --grpc-address :40000 --libp2p :30304 --jsonrpc :40002 --num-block-confirmations 2 --seal --log-level DEBUG &
wait
}

Expand Down
2 changes: 2 additions & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Config struct {
LogFilePath string

Relayer bool

NumBlockConfirmations uint64
}

// Telemetry holds the config details for metric services
Expand Down
21 changes: 11 additions & 10 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,16 +429,17 @@ func (s *Server) setupConsensus() error {

consensus, err := engine(
&consensus.Params{
Context: context.Background(),
Config: config,
TxPool: s.txpool,
Network: s.network,
Blockchain: s.blockchain,
Executor: s.executor,
Grpc: s.grpcServer,
Logger: s.logger,
SecretsManager: s.secretsManager,
BlockTime: s.config.BlockTime,
Context: context.Background(),
Config: config,
TxPool: s.txpool,
Network: s.network,
Blockchain: s.blockchain,
Executor: s.executor,
Grpc: s.grpcServer,
Logger: s.logger,
SecretsManager: s.secretsManager,
BlockTime: s.config.BlockTime,
NumBlockConfirmations: s.config.NumBlockConfirmations,
},
)

Expand Down
Loading

0 comments on commit 9a0c634

Please sign in to comment.