Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial import of the client #4

Merged
merged 2 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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