Skip to content

Commit

Permalink
[Checkpointing] JSON RPC point for generating exit proofs (#844)
Browse files Browse the repository at this point in the history
* JSON RPC point for generating exit proofs

* Comments fix
  • Loading branch information
goran-ethernal authored Oct 31, 2022
1 parent a6cbda3 commit 213aa83
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 5 deletions.
9 changes: 9 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type Consensus interface {
// GetSyncProgression retrieves the current sync progression, if any
GetSyncProgression() *progress.Progression

// GetBridgeProvider returns an instance of BridgeDataProvider
GetBridgeProvider() BridgeDataProvider

// Initialize initializes the consensus (e.g. setup data)
Initialize() error

Expand Down Expand Up @@ -75,3 +78,9 @@ type Params struct {

// Factory is the factory function to create a discovery consensus
type Factory func(*Params) (Consensus, error)

// BridgeDataProvider is an interface providing bridge related functions
type BridgeDataProvider interface {
// GenerateExit proof generates proof of exit for given exit event
GenerateExitProof(exitID, epoch, checkpointBlock uint64) ([]types.Hash, error)
}
4 changes: 4 additions & 0 deletions consensus/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,7 @@ func (d *Dev) Close() error {

return nil
}

func (d *Dev) GetBridgeProvider() consensus.BridgeDataProvider {
return nil
}
4 changes: 4 additions & 0 deletions consensus/dummy/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func (d *Dummy) Close() error {
return nil
}

func (d *Dummy) GetBridgeProvider() consensus.BridgeDataProvider {
return nil
}

func (d *Dummy) run() {
d.logger.Info("started")
// do nothing
Expand Down
5 changes: 5 additions & 0 deletions consensus/ibft/ibft.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,11 @@ func (i *backendIBFT) SetHeaderHash() {
}
}

// GetBridgeProvider returns an instance of BridgeDataProvider
func (i *backendIBFT) GetBridgeProvider() consensus.BridgeDataProvider {
return nil
}

// updateCurrentModules updates Signer, Hooks, and Validators
// that are used at specified height
// by fetching from ForkManager
Expand Down
4 changes: 2 additions & 2 deletions consensus/polybft/consensus_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,8 @@ func (c *consensusRuntime) getExitEventRootHash(epoch uint64) (types.Hash, error
return tree.Hash(), nil
}

// generateExitProof generates proof of exit
func (c *consensusRuntime) generateExitProof(exitID, epoch, checkpointBlock uint64) ([]types.Hash, error) {
// GenerateExitProof generates proof of exit
func (c *consensusRuntime) GenerateExitProof(exitID, epoch, checkpointBlock uint64) ([]types.Hash, error) {
exitEvent, err := c.state.getExitEvent(exitID, epoch, checkpointBlock)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions consensus/polybft/consensus_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1671,7 +1671,7 @@ func TestConsensusRuntime_getExitEventRootHash(t *testing.T) {
})
}

func TestConsensusRuntime_generateExitProof(t *testing.T) {
func TestConsensusRuntime_GenerateExitProof(t *testing.T) {
const (
numOfBlocks = 10
numOfEventsPerBlock = 2
Expand All @@ -1689,7 +1689,7 @@ func TestConsensusRuntime_generateExitProof(t *testing.T) {
tree, err := NewMerkleTree(checkpointEvents)
require.NoError(t, err)

proof, err := runtime.generateExitProof(1, 1, 1)
proof, err := runtime.GenerateExitProof(1, 1, 1)
require.NoError(t, err)
require.NotNil(t, proof)

Expand All @@ -1707,7 +1707,7 @@ func TestConsensusRuntime_generateExitProof(t *testing.T) {
})

t.Run("Generate exit proof - no event", func(t *testing.T) {
_, err := runtime.generateExitProof(21, 1, 1)
_, err := runtime.GenerateExitProof(21, 1, 1)
require.ErrorContains(t, err, "could not find any exit event that has an id")
})
}
Expand Down
5 changes: 5 additions & 0 deletions consensus/polybft/polybft.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,11 @@ func (p *Polybft) PreCommitState(_ *types.Header, _ *state.Transition) error {
return nil
}

// GetBridgeProvider returns an instance of BridgeDataProvider
func (p *Polybft) GetBridgeProvider() consensus.BridgeDataProvider {
return p.runtime
}

type pbftTransportWrapper struct {
topic *network.Topic
}
Expand Down
20 changes: 20 additions & 0 deletions jsonrpc/bridge_endpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package jsonrpc

import (
"github.com/0xPolygon/polygon-edge/types"
)

// bridgeStore interface provides access to the methods needed by bridge endpoint
type bridgeStore interface {
GenerateExitProof(exitID, epoch, checkpointBlock uint64) ([]types.Hash, error)
}

// Bridge is the bridge jsonrpc endpoint
type Bridge struct {
store bridgeStore
}

// GenerateExitProof generates exit proof for given exit event
func (b *Bridge) GenerateExitProof(exitID, epoch, checkpointBlock argUint64) (interface{}, error) {
return b.store.GenerateExitProof(uint64(exitID), uint64(epoch), uint64(checkpointBlock))
}
40 changes: 40 additions & 0 deletions jsonrpc/bridge_endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package jsonrpc

import (
"encoding/json"
"testing"

"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/require"
)

func TestBridgeEndpoint(t *testing.T) {
store := newMockStore()

dispatcher := newDispatcher(
hclog.NewNullLogger(),
store,
&dispatcherParams{
chainID: 0,
priceLimit: 0,
jsonRPCBatchLengthLimit: 20,
blockRangeLimit: 1000,
},
)

mockConnection, _ := newMockWsConnWithMsgCh()

msg := []byte(`{
"method": "bridge_generateExitProof",
"params": ["0x0001", "0x0001", "0x0002"],
"id": 1
}`)

data, err := dispatcher.HandleWs(msg, mockConnection)
require.NoError(t, err)

resp := new(SuccessResponse)
require.NoError(t, json.Unmarshal(data, resp))
require.Nil(t, resp.Error)
require.NotNil(t, resp.Result)
}
3 changes: 3 additions & 0 deletions jsonrpc/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type endpoints struct {
Web3 *Web3
Net *Net
TxPool *TxPool
Bridge *Bridge
}

// Dispatcher handles all json rpc requests by delegating
Expand Down Expand Up @@ -93,11 +94,13 @@ func (d *Dispatcher) registerEndpoints(store JSONRPCStore) {
d.params.chainName,
}
d.endpoints.TxPool = &TxPool{store}
d.endpoints.Bridge = &Bridge{store}

d.registerService("eth", d.endpoints.Eth)
d.registerService("net", d.endpoints.Net)
d.registerService("web3", d.endpoints.Web3)
d.registerService("txpool", d.endpoints.TxPool)
d.registerService("bridge", d.endpoints.Bridge)
}

func (d *Dispatcher) getFnHandler(req Request) (*serviceData, *funcData, Error) {
Expand Down
1 change: 1 addition & 0 deletions jsonrpc/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type JSONRPCStore interface {
networkStore
txPoolStore
filterManagerStore
bridgeStore
}

type Config struct {
Expand Down
6 changes: 6 additions & 0 deletions jsonrpc/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,9 @@ func (m *mockStore) GetTxs(inclQueued bool) (
func (m *mockStore) GetCapacity() (uint64, uint64) {
return 0, 0
}

func (m *mockStore) GenerateExitProof(exitID, epoch, checkpointNumber uint64) ([]types.Hash, error) {
hash := types.BytesToHash([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})

return []types.Hash{hash}, nil
}
2 changes: 2 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ type jsonRPCHub struct {
*state.Executor
*network.Server
consensus.Consensus
consensus.BridgeDataProvider
}

// HELPER + WRAPPER METHODS //
Expand Down Expand Up @@ -563,6 +564,7 @@ func (s *Server) setupJSONRPC() error {
Executor: s.executor,
Consensus: s.consensus,
Server: s.network,
BridgeDataProvider: s.consensus.GetBridgeProvider(),
}

conf := &jsonrpc.Config{
Expand Down

0 comments on commit 213aa83

Please sign in to comment.