Skip to content

Commit

Permalink
feature: updates with latest light client rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
Code Monad committed Feb 2, 2023
1 parent 33236ac commit 2fb59be
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ jobs:
uses: actions/setup-go@v2
with:
go-version: '1.17'
- name: Install Rust tools
uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Checkout code
uses: actions/checkout@v2
- name: Install ckb-light-client
uses: actions-rs/cargo@v1
with:
command: install
args: --locked --git https://github.com/nervosnetwork/ckb-light-client.git
- name: Run CKB Light Client
run: wget https://raw.githubusercontent.com/nervosnetwork/ckb-light-client/develop/config/testnet.toml && ckb-light-client run --config-file testnet.toml &
- name: Test
run: go test ./... -short
30 changes: 27 additions & 3 deletions lightclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ type Client interface {
GetTipHeader(ctx context.Context) (*types.Header, error)
GetGenesisBlock(ctx context.Context) (*types.Block, error)
GetHeader(ctx context.Context, hash types.Hash) (*types.Header, error)
GetTransaction(ctx context.Context, hash types.Hash) (*TransactionWithHeader, error)
GetTransaction(ctx context.Context, hash types.Hash) (*TransactionStatus, error)
FetchHeader(ctx context.Context, hash types.Hash) (*FetchedHeader, error)
FetchTransaction(ctx context.Context, hash types.Hash) (*FetchedTransaction, error)
GetPeers(ctx context.Context) ([]*types.RemoteNode, error)
LocalNodeInfo(ctx context.Context) (*types.LocalNode, error)
GetCells(ctx context.Context, searchKey *indexer.SearchKey, order indexer.SearchOrder, limit uint64, afterCursor string) (*indexer.LiveCells, error)
GetTransactions(ctx context.Context, searchKey *indexer.SearchKey, order indexer.SearchOrder, limit uint64, afterCursor string) (*TxsWithCell, error)
GetTransactionsGrouped(ctx context.Context, searchKey *indexer.SearchKey, order indexer.SearchOrder, limit uint64, afterCursor string) (*TxsWithCells, error)
Expand Down Expand Up @@ -80,8 +82,8 @@ func (cli *client) GetHeader(ctx context.Context, hash types.Hash) (*types.Heade
return &result, nil
}

func (cli *client) GetTransaction(ctx context.Context, hash types.Hash) (*TransactionWithHeader, error) {
var result TransactionWithHeader
func (cli *client) GetTransaction(ctx context.Context, hash types.Hash) (*TransactionStatus, error) {
var result TransactionStatus
err := cli.c.CallContext(ctx, &result, "get_transaction", hash)
if err != nil {
return nil, err
Expand All @@ -107,6 +109,28 @@ func (cli *client) FetchTransaction(ctx context.Context, hash types.Hash) (*Fetc
return &result, nil
}

func (cli *client) GetPeers(ctx context.Context) ([]*types.RemoteNode, error) {
var result []*types.RemoteNode

err := cli.c.CallContext(ctx, &result, "get_peers")
if err != nil {
return nil, err
}

return result, err
}

func (cli *client) LocalNodeInfo(ctx context.Context) (*types.LocalNode, error) {
var result types.LocalNode

err := cli.c.CallContext(ctx, &result, "local_node_info")
if err != nil {
return nil, err
}

return &result, err
}

func (cli *client) GetCells(ctx context.Context, searchKey *indexer.SearchKey, order indexer.SearchOrder, limit uint64, afterCursor string) (*indexer.LiveCells, error) {
var (
result indexer.LiveCells
Expand Down
26 changes: 22 additions & 4 deletions lightclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func skipCI(t *testing.T) {
}

func TestSetScripts(t *testing.T) {
skipCI(t)
scriptDetail := ScriptDetail{
// ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2qf8keemy2p5uu0g0gn8cd4ju23s5269qk8rg4r
Script: scriptForTest,
Expand All @@ -39,7 +38,6 @@ func TestSetScripts(t *testing.T) {
}

func TestGetScripts(t *testing.T) {
skipCI(t)
scriptDetails, err := c.GetScripts(ctx)
assert.NoError(t, err)
assert.NotEmpty(t, scriptDetails)
Expand All @@ -66,7 +64,7 @@ func TestGetGenesisBlock(t *testing.T) {
func TestGetHeader(t *testing.T) {
skipCI(t)
header, err := c.GetHeader(ctx,
types.HexToHash("0xc78c65185c14e1b02d6457a06b4678bab7e15f194f49a840319b57c67d20053c"))
types.HexToHash("0x86487ca41db5141bb750a0b5dbea8b87c0b3a05dda1c1e587ca9f7ccae3b4ad5"))
assert.NoError(t, err)
assert.NotEmpty(t, header)
}
Expand All @@ -77,7 +75,7 @@ func TestGetTransaction(t *testing.T) {
types.HexToHash("0x151d4d450c9e3bccf4b47d1ba6942d4e9c8c0eeeb7b9f708df827c164f035aa8"))
assert.NoError(t, err)
assert.NotEmpty(t, txWitHeader.Transaction)
assert.NotEmpty(t, txWitHeader.Header)
assert.NotEmpty(t, txWitHeader.TxStatus)
}

func TestFetchHeader(t *testing.T) {
Expand Down Expand Up @@ -152,3 +150,23 @@ func TestGetCellsCapacity(t *testing.T) {
assert.NotEmpty(t, resp.BlockHash)
assert.NotEmpty(t, resp.Capacity)
}

func TestGetPeers(t *testing.T) {
peers, err := c.GetPeers(ctx)
if err != nil {
t.Fatal(err)
}
assert.True(t, len(peers) > 0)
assert.True(t, len(peers[0].Addresses) > 0)
assert.True(t, len(peers[0].Protocols) > 0)
}

func TestClient_LocalNodeInfo(t *testing.T) {
nodeInfo, err := c.LocalNodeInfo(ctx)
if err != nil {
t.Fatal(err)
}
assert.True(t, len(nodeInfo.Addresses) > 0)
assert.True(t, len(nodeInfo.Protocols) > 0)
assert.True(t, len(nodeInfo.Protocols[0].SupportVersions) > 0)
}
19 changes: 19 additions & 0 deletions lightclient/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,30 @@ type ScriptDetail struct {
BlockNumber uint64 `json:"block_number"`
}

type TxStatusString string

const (
TxStatusPending TxStatusString = "pending"
TxStatusCommitted TxStatusString = "committed"
TxStatusUnknown TxStatusString = "unknown"
)

type TxStatus struct {
Status TxStatusString `json:"status"`
BlockHash *types.Hash `json:"block_hash"`
}

type TransactionWithHeader struct {
Transaction *types.Transaction `json:"transaction"`
Header *types.Header `json:"header"`
}

type TransactionStatus struct {
Transaction *types.Transaction `json:"transaction"`
Cycles uint64 `json:"cycles"`
TxStatus *TxStatus `json:"tx_status"`
}

type FetchStatus string

const (
Expand Down

0 comments on commit 2fb59be

Please sign in to comment.