Skip to content

Commit

Permalink
Merge branch 'main' into sevey/setup-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
MSevey committed Jun 23, 2023
2 parents 39443e6 + 505ff80 commit afc6b99
Show file tree
Hide file tree
Showing 15 changed files with 2,666 additions and 2 deletions.
2 changes: 1 addition & 1 deletion NOTICE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Rollkit
celestia-openrpc
Copyright 2023 and onwards Strange Loop Labs AG
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# celestia-operpc
# celestia-openrpc

OpenRPC is a client of [celestia-node RPC](https://docs.celestia.org/category/rpc-api/), without depenencies on celestia-node/celestia-app/cosmos-sdk.

This is a temporary measure to resolve dependency issues when celestia-node is imported by rollkit.
162 changes: 162 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package client

import (
"context"

"github.com/celestiaorg/go-fraud"
libhead "github.com/celestiaorg/go-header"
"github.com/celestiaorg/go-header/sync"
"github.com/celestiaorg/rsmt2d"
"github.com/filecoin-project/go-jsonrpc/auth"
"github.com/libp2p/go-libp2p/core/metrics"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"

"github.com/rollkit/celestia-openrpc/types/das"
"github.com/rollkit/celestia-openrpc/types/header"
"github.com/rollkit/celestia-openrpc/types/namespace"
"github.com/rollkit/celestia-openrpc/types/node"
"github.com/rollkit/celestia-openrpc/types/share"
"github.com/rollkit/celestia-openrpc/types/state"
)

type FraudAPI struct {
Subscribe func(context.Context, fraud.ProofType) (<-chan Proof, error) `perm:"public"`
Get func(context.Context, fraud.ProofType) ([]Proof, error) `perm:"public"`
}

// Proof embeds the fraud.Proof interface type to provide a concrete type for JSON serialization.
type Proof struct {
fraud.Proof
}
type DASAPI struct {
SamplingStats func(ctx context.Context) (das.SamplingStats, error) `perm:"read"`
WaitCatchUp func(ctx context.Context) error `perm:"read"`
}

type HeaderAPI struct {
LocalHead func(context.Context) (*header.ExtendedHeader, error) `perm:"read"`
GetByHash func(
ctx context.Context,
hash libhead.Hash,
) (*header.ExtendedHeader, error) `perm:"public"`
GetVerifiedRangeByHeight func(
context.Context,
*header.ExtendedHeader,
uint64,
) ([]*header.ExtendedHeader, error) `perm:"public"`
GetByHeight func(context.Context, uint64) (*header.ExtendedHeader, error) `perm:"public"`
SyncState func(ctx context.Context) (sync.State, error) `perm:"read"`
SyncWait func(ctx context.Context) error `perm:"read"`
NetworkHead func(ctx context.Context) (*header.ExtendedHeader, error) `perm:"public"`
Subscribe func(ctx context.Context) (<-chan *header.ExtendedHeader, error) `perm:"public"`
}
type StateAPI struct {
AccountAddress func(ctx context.Context) (state.Address, error) `perm:"read"`
IsStopped func(ctx context.Context) bool `perm:"public"`
Balance func(ctx context.Context) (*state.Balance, error) `perm:"read"`
BalanceForAddress func(ctx context.Context, addr state.Address) (*state.Balance, error) `perm:"public"`
Transfer func(
ctx context.Context,
to state.AccAddress,
amount,
fee state.Int,
gasLimit uint64,
) (*state.TxResponse, error) `perm:"write"`
SubmitTx func(ctx context.Context, tx state.Tx) (*state.TxResponse, error) `perm:"write"`
SubmitPayForBlob func(
ctx context.Context,
nID namespace.ID,
data []byte,
fee state.Int,
gasLim uint64,
) (*state.TxResponse, error) `perm:"write"`
CancelUnbondingDelegation func(
ctx context.Context,
valAddr state.ValAddress,
amount,
height,
fee state.Int,
gasLim uint64,
) (*state.TxResponse, error) `perm:"write"`
BeginRedelegate func(
ctx context.Context,
srcValAddr,
dstValAddr state.ValAddress,
amount,
fee state.Int,
gasLim uint64,
) (*state.TxResponse, error) `perm:"write"`
Undelegate func(
ctx context.Context,
delAddr state.ValAddress,
amount,
fee state.Int,
gasLim uint64,
) (*state.TxResponse, error) `perm:"write"`
Delegate func(
ctx context.Context,
delAddr state.ValAddress,
amount,
fee state.Int,
gasLim uint64,
) (*state.TxResponse, error) `perm:"write"`
QueryDelegation func(
ctx context.Context,
valAddr state.ValAddress,
) (*state.QueryDelegationResponse, error) `perm:"public"`
QueryUnbonding func(
ctx context.Context,
valAddr state.ValAddress,
) (*state.QueryUnbondingDelegationResponse, error) `perm:"public"`
QueryRedelegations func(
ctx context.Context,
srcValAddr,
dstValAddr state.ValAddress,
) (*state.QueryRedelegationsResponse, error) `perm:"public"`
}
type ShareAPI struct {
SharesAvailable func(context.Context, *share.Root) error `perm:"public"`
ProbabilityOfAvailability func(context.Context) float64 `perm:"public"`
GetShare func(
ctx context.Context,
dah *share.Root,
row, col int,
) (share.Share, error) `perm:"public"`
GetEDS func(
ctx context.Context,
root *share.Root,
) (*rsmt2d.ExtendedDataSquare, error) `perm:"public"`
GetSharesByNamespace func(
ctx context.Context,
root *share.Root,
namespace namespace.ID,
) (share.NamespacedShares, error) `perm:"public"`
}
type P2PAPI struct {
Peers func(context.Context) ([]peer.ID, error) `perm:"admin"`
PeerInfo func(ctx context.Context, id peer.ID) (peer.AddrInfo, error) `perm:"admin"`
Connect func(ctx context.Context, pi peer.AddrInfo) error `perm:"admin"`
ClosePeer func(ctx context.Context, id peer.ID) error `perm:"admin"`
Connectedness func(ctx context.Context, id peer.ID) (network.Connectedness, error) `perm:"admin"`
NATStatus func(context.Context) (network.Reachability, error) `perm:"admin"`
BlockPeer func(ctx context.Context, p peer.ID) error `perm:"admin"`
UnblockPeer func(ctx context.Context, p peer.ID) error `perm:"admin"`
ListBlockedPeers func(context.Context) ([]peer.ID, error) `perm:"admin"`
Protect func(ctx context.Context, id peer.ID, tag string) error `perm:"admin"`
Unprotect func(ctx context.Context, id peer.ID, tag string) (bool, error) `perm:"admin"`
IsProtected func(ctx context.Context, id peer.ID, tag string) (bool, error) `perm:"admin"`
BandwidthStats func(context.Context) (metrics.Stats, error) `perm:"admin"`
BandwidthForPeer func(ctx context.Context, id peer.ID) (metrics.Stats, error) `perm:"admin"`
BandwidthForProtocol func(ctx context.Context, proto protocol.ID) (metrics.Stats, error) `perm:"admin"`
ResourceState func(context.Context) (rcmgr.ResourceManagerStat, error) `perm:"admin"`
PubSubPeers func(ctx context.Context, topic string) ([]peer.ID, error) `perm:"admin"`
}
type NodeAPI struct {
Info func(context.Context) (node.Info, error) `perm:"admin"`
LogLevelSet func(ctx context.Context, name, level string) error `perm:"admin"`
AuthVerify func(ctx context.Context, token string) ([]auth.Permission, error) `perm:"admin"`
AuthNew func(ctx context.Context, perms []auth.Permission) ([]byte, error) `perm:"admin"`
}
70 changes: 70 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package client

import (
"context"
"fmt"
"github.com/filecoin-project/go-jsonrpc"
"net/http"
)

const AuthKey = "Authorization"

type Client struct {
Fraud FraudAPI
Header HeaderAPI
State StateAPI
Share ShareAPI
DAS DASAPI
P2P P2PAPI
Node NodeAPI

closer multiClientCloser
}

// multiClientCloser is a wrapper struct to close clients across multiple namespaces.
type multiClientCloser struct {
closers []jsonrpc.ClientCloser
}

// register adds a new closer to the multiClientCloser
func (m *multiClientCloser) register(closer jsonrpc.ClientCloser) {
m.closers = append(m.closers, closer)
}

// closeAll closes all saved clients.
func (m *multiClientCloser) closeAll() {
for _, closer := range m.closers {
closer()
}
}

// Close closes the connections to all namespaces registered on the client.
func (c *Client) Close() {
c.closer.closeAll()
}

func NewClient(ctx context.Context, addr string, token string) (*Client, error) {
authHeader := http.Header{AuthKey: []string{fmt.Sprintf("Bearer %s", token)}}

var client Client

modules := map[string]interface{}{
"fraud": &client.Fraud,
"header": &client.Header,
"state": &client.State,
"share": &client.Share,
"das": &client.DAS,
"p2p": &client.P2P,
"node": &client.Node,
}

for name, module := range modules {
closer, err := jsonrpc.NewClient(ctx, addr, name, module, authHeader)
if err != nil {
return nil, err
}
client.closer.register(closer)
}

return &client, nil
}
26 changes: 26 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package client

import (
"context"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func TestClient(t *testing.T) {
client, err := NewClient(context.Background(), "http://localhost:26658", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBbGxvdyI6WyJwdWJsaWMiLCJyZWFkIiwid3JpdGUiLCJhZG1pbiJdfQ.aBWqglHA-R1u4X1In5HMAqX88V5nDetjA6KflxB0p9U")
defer client.Close()

assert.NoError(t, err)
assert.NotNil(t, client)

ctx, closer := context.WithTimeout(context.Background(), 1*time.Second)
defer closer()

resp := client.Share.ProbabilityOfAvailability(ctx)
assert.NotZero(t, resp)

info, err := client.Node.Info(ctx)
assert.NoError(t, err)
assert.NotEmpty(t, info.APIVersion)
}
79 changes: 79 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
module github.com/rollkit/celestia-openrpc

go 1.20

require (
cosmossdk.io/math v1.0.1
github.com/celestiaorg/go-fraud v0.1.0
github.com/celestiaorg/go-header v0.2.7
github.com/celestiaorg/nmt v0.15.0
github.com/celestiaorg/rsmt2d v0.9.0
github.com/filecoin-project/go-jsonrpc v0.3.1
github.com/gogo/protobuf v1.3.2
github.com/libp2p/go-libp2p v0.27.3
github.com/stretchr/testify v1.8.2
github.com/tendermint/tendermint v0.35.9
)

require (
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/btcsuite/btcd v0.22.1 // indirect
github.com/celestiaorg/merkletree v0.0.0-20210714075610-a84dc3ddbbe4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.2 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
github.com/ipfs/go-log/v2 v2.5.1 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/klauspost/reedsolomon v1.11.1 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-flow-metrics v0.1.0 // indirect
github.com/libp2p/go-libp2p-pubsub v0.9.3 // indirect
github.com/libp2p/go-msgio v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multiaddr v0.9.0 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multicodec v0.8.1 // indirect
github.com/multiformats/go-multihash v0.2.1 // indirect
github.com/multiformats/go-multistream v0.4.1 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/oasisprotocol/curve25519-voi v0.0.0-20210609091139-0a56a4bca00b // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/zerolog v1.27.0 // indirect
github.com/sasha-s/go-deadlock v0.2.1-0.20190427202633-1595213edefa // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/otel v1.11.2 // indirect
go.opentelemetry.io/otel/metric v0.34.0 // indirect
go.opentelemetry.io/otel/trace v1.11.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
google.golang.org/grpc v1.47.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.1.7 // indirect
)
Loading

0 comments on commit afc6b99

Please sign in to comment.