Skip to content

Commit

Permalink
JSON RPC point for generating exit proofs
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Oct 27, 2022
1 parent a298d0e commit 8aa1f52
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 0 deletions.
3 changes: 3 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

// GenerateExitProof generates exit proof for given exit event id
GenerateExitProof(exitID, epoch, checkpointNumber uint64) ([]types.Hash, error)

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

Expand Down
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) GenerateExitProof(exitID, epoch, checkpointBlock uint64) ([]types.Hash, error) {
return nil, 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) GenerateExitProof(exitID, epoch, checkpointBlock uint64) ([]types.Hash, error) {
return nil, nil
}

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

func (i *backendIBFT) GenerateExitProof(exitID, epoch, checkpointBlock uint64) ([]types.Hash, error) {
return nil, nil
}

// updateCurrentModules updates Signer, Hooks, and Validators
// that are used at specified height
// by fetching from ForkManager
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
}

// GenerateExitProof generates proof for given exit event
func (p *Polybft) GenerateExitProof(exitID, epoch, checkpointBlock uint64) ([]types.Hash, error) {
return p.runtime.generateExitProof(exitID, epoch, checkpointBlock)
}

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 BlockNumber) (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
}

0 comments on commit 8aa1f52

Please sign in to comment.