diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 28fb4cac..1f74b2ea 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 \ No newline at end of file diff --git a/lightclient/client.go b/lightclient/client.go index 63944702..541f5b86 100644 --- a/lightclient/client.go +++ b/lightclient/client.go @@ -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) @@ -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 @@ -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 diff --git a/lightclient/client_test.go b/lightclient/client_test.go index b13cee1b..f12cc0e0 100644 --- a/lightclient/client_test.go +++ b/lightclient/client_test.go @@ -27,7 +27,6 @@ func skipCI(t *testing.T) { } func TestSetScripts(t *testing.T) { - skipCI(t) scriptDetail := ScriptDetail{ // ckt1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsq2qf8keemy2p5uu0g0gn8cd4ju23s5269qk8rg4r Script: scriptForTest, @@ -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) @@ -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) } @@ -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) { @@ -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) +} diff --git a/lightclient/type.go b/lightclient/type.go index 26d0c7c8..a0cb6b60 100644 --- a/lightclient/type.go +++ b/lightclient/type.go @@ -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 (