Skip to content

Commit

Permalink
adjust for client
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaiba committed Dec 12, 2024
1 parent 63af9b4 commit 0e39a4e
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 94 deletions.
21 changes: 21 additions & 0 deletions core/rpc/client/chain/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package chain

import (
"context"

"github.com/kwilteam/kwil-db/core/types"
chaintypes "github.com/kwilteam/kwil-db/core/types/chain"
)

type Client interface {
Version(ctx context.Context) (string, error)
BlockByHeight(ctx context.Context, height int64) (*chaintypes.Block, error)
BlockByHash(ctx context.Context, hash types.Hash) (*chaintypes.Block, error)
BlockResultByHeight(ctx context.Context, height int64) (*chaintypes.BlockResult, error)
BlockResultByHash(ctx context.Context, hash types.Hash) (*chaintypes.BlockResult, error)
Tx(ctx context.Context, hash types.Hash) (*chaintypes.Tx, error)
Genesis(ctx context.Context) (*chaintypes.Genesis, error)
ConsensusParams(ctx context.Context) (*types.ConsensusParams, error)
Validators(ctx context.Context) (height int64, validators []*types.Validator, err error)
UnconfirmedTxs(ctx context.Context) (total int64, tx chaintypes.NamedTx, err error)
}
145 changes: 145 additions & 0 deletions core/rpc/client/chain/jsonrpc/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package jsonrpc

import (
"context"
"net/url"

rpcclient "github.com/kwilteam/kwil-db/core/rpc/client"
"github.com/kwilteam/kwil-db/core/rpc/client/chain"
"github.com/kwilteam/kwil-db/core/rpc/client/user"
userClient "github.com/kwilteam/kwil-db/core/rpc/client/user/jsonrpc"
chainjson "github.com/kwilteam/kwil-db/core/rpc/json/chain"
userjson "github.com/kwilteam/kwil-db/core/rpc/json/user"
"github.com/kwilteam/kwil-db/core/types"
chaintypes "github.com/kwilteam/kwil-db/core/types/chain"
)

// Client is a chain RPC client. It provides all methods of the user RPC
// service, plus methods that are specific to the chain service.
type Client struct {
*userClient.Client // expose all user service methods, and methods for chain svc
}

// Version reports the version of the running node.
func (c *Client) Version(ctx context.Context) (string, error) {
req := &userjson.VersionRequest{}
res := &userjson.VersionResponse{}
err := c.CallMethod(ctx, string(chainjson.MethodVersion), req, res)
if err != nil {
return "", err
}
return res.KwilVersion, err
}

func (c *Client) BlockByHeight(ctx context.Context, height int64) (*chaintypes.Block, error) {
req := &chainjson.BlockRequest{
Height: height,
}
res := &chainjson.BlockResponse{}
err := c.CallMethod(ctx, string(chainjson.MethodBlock), req, res)
if err != nil {
return nil, err
}
return (*chaintypes.Block)(res), nil
}

func (c *Client) BlockByHash(ctx context.Context, hash types.Hash) (*chaintypes.Block, error) {
req := &chainjson.BlockRequest{
Hash: hash,
}
res := &chainjson.BlockResponse{}
err := c.CallMethod(ctx, string(chainjson.MethodBlock), req, res)
if err != nil {
return nil, err
}
return (*chaintypes.Block)(res), nil
}

func (c *Client) BlockResultByHeight(ctx context.Context, height int64) (*chaintypes.BlockResult, error) {
req := &chainjson.BlockResultRequest{
Height: height,
}
res := &chainjson.BlockResultResponse{}
err := c.CallMethod(ctx, string(chainjson.MethodBlockResult), req, res)
if err != nil {
return nil, err
}
return (*chaintypes.BlockResult)(res), nil
}

func (c *Client) BlockResultByHash(ctx context.Context, hash types.Hash) (*chaintypes.BlockResult, error) {
req := &chainjson.BlockResultRequest{
Hash: hash,
}
res := &chainjson.BlockResultResponse{}
err := c.CallMethod(ctx, string(chainjson.MethodBlockResult), req, res)
if err != nil {
return nil, err
}
return (*chaintypes.BlockResult)(res), nil
}

func (c *Client) Tx(ctx context.Context, hash types.Hash) (*chaintypes.Tx, error) {
req := &chainjson.TxRequest{
Hash: hash,
}
res := &chainjson.TxResponse{}
err := c.CallMethod(ctx, string(chainjson.MethodTx), req, res)
if err != nil {
return nil, err
}
return (*chaintypes.Tx)(res), err
}

func (c *Client) Genesis(ctx context.Context) (*chaintypes.Genesis, error) {
req := &chainjson.GenesisRequest{}
res := &chainjson.GenesisResponse{}
err := c.CallMethod(ctx, string(chainjson.MethodGenesis), req, res)
if err != nil {
return nil, err
}
return (*chaintypes.Genesis)(res), err
}

func (c *Client) ConsensusParams(ctx context.Context) (*types.ConsensusParams, error) {
req := &chainjson.ConsensusParamsRequest{}
res := &chainjson.ConsensusParamsResponse{}
err := c.CallMethod(ctx, string(chainjson.MethodConsensusParams), req, res)
if err != nil {
return nil, err
}
return (*types.ConsensusParams)(res), nil
}

func (c *Client) Validators(ctx context.Context) (int64, []*types.Validator, error) {
req := &chainjson.ValidatorsRequest{}
res := &chainjson.ValidatorsResponse{}
err := c.CallMethod(ctx, string(chainjson.MethodValidators), req, res)
if err != nil {
return 0, nil, err
}

return res.Height, res.Validators, nil
}

func (c *Client) UnconfirmedTxs(ctx context.Context) (total int, tx []chaintypes.NamedTx, err error) {
req := &chainjson.UnconfirmedTxsRequest{}
res := &chainjson.UnconfirmedTxsResponse{}
err = c.CallMethod(ctx, string(chainjson.MethodUnconfirmedTxs), req, res)
if err != nil {
return 0, nil, err
}

return res.Total, res.Txs, nil
}

// NewClient constructs a new chain Client.
func NewClient(u *url.URL, opts ...rpcclient.RPCClientOpts) *Client {
userClient := userClient.NewClient(u, opts...)
return &Client{
Client: userClient,
}
}

var _ user.TxSvcClient = (*Client)(nil) // via embedded userClient.Client
var _ chain.Client = (*Client)(nil) // with extra methods
71 changes: 8 additions & 63 deletions core/rpc/json/chain/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"

"github.com/kwilteam/kwil-db/core/types"
chaintypes "github.com/kwilteam/kwil-db/core/types/chain"
)

// HealthResponse is the health check response.
Expand All @@ -13,66 +14,15 @@ type HealthResponse struct {
Healthy bool `json:"healthy"`
}

type BlockHeader types.BlockHeader

func (b BlockHeader) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Version uint16 `json:"version"`
Height int64 `json:"height"`
NumTxns uint32 `json:"num_txns"`
PrevHash types.Hash `json:"prev_hash"`
PrevAppHash types.Hash `json:"prev_app_hash"`
// Timestamp is the unix millisecond timestamp
Timestamp int64 `json:"timestamp"`
MerkleRoot types.Hash `json:"merkle_root"`
ValidatorSetHash types.Hash `json:"validator_set_hash"`
}{
Version: b.Version,
Height: b.Height,
NumTxns: b.NumTxns,
PrevHash: b.PrevHash,
PrevAppHash: b.PrevAppHash,
Timestamp: b.Timestamp.UnixMilli(),
MerkleRoot: b.MerkleRoot,
ValidatorSetHash: b.ValidatorSetHash,
})
}

// BlockResponse is the block information
type BlockResponse struct {
Header *BlockHeader `json:"header"`
Txns [][]byte `json:"txns"`
Signature []byte `json:"signature"`
Hash types.Hash `json:"hash"`
AppHash types.Hash `json:"app_hash"`
}
type BlockResponse chaintypes.Block

type BlockResultResponse struct {
Height int64 `json:"height"`
TxResults []types.TxResult `json:"tx_results"`
}
type BlockResultResponse chaintypes.BlockResult

type TxResponse chaintypes.Tx

// GenesisResponse is the same as kwil-db/config.GenesisConfig, with JSON tags.
type GenesisResponse struct {
ChainID string `json:"chain_id"`
// Leader is the leader's public key.
Leader types.HexBytes `json:"leader"`
// Validators is the list of genesis validators (including the leader).
Validators []*types.Validator `json:"validators"`
// MaxBlockSize is the maximum size of a block in bytes.
MaxBlockSize int64 `json:"max_block_size"`
// JoinExpiry is the number of blocks after which the validators
// join request expires if not approved.
JoinExpiry int64 `json:"join_expiry"`
// VoteExpiry is the default number of blocks after which the validators
// vote expires if not approved.
VoteExpiry int64 `json:"vote_expiry"`
// DisabledGasCosts dictates whether gas costs are disabled.
DisabledGasCosts bool `json:"disabled_gas_costs"`
// MaxVotesPerTx is the maximum number of votes that can be included in a
// single transaction.
MaxVotesPerTx int64 `json:"max_votes_per_tx"`
}
type GenesisResponse chaintypes.Genesis

type ConsensusParamsResponse types.ConsensusParams

Expand Down Expand Up @@ -110,12 +60,7 @@ type ValidatorsResponse struct {
Validators []*types.Validator `json:"validators"`
}

type NamedTx struct {
Hash types.Hash `json:"hash"`
Tx []byte `json:"tx"`
}

type UnconfirmedTxsResponse struct {
Total int `json:"total"`
Txs []NamedTx `json:"txs"`
Total int `json:"total"`
Txs []chaintypes.NamedTx `json:"txs"`
}
84 changes: 76 additions & 8 deletions core/types/chain/types.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,80 @@
// Package types contains the type used by the chain stat RPC client and servers.
// Package types contains the type used by the chain RPC client and server.
package types

import "github.com/kwilteam/kwil-db/core/types"
import (
"encoding/json"
"github.com/kwilteam/kwil-db/core/types"
)

type ChainTx struct {
Hash types.Hash `json:"hash"`
Height int64 `json:"height"`
Index uint32 `json:"index"`
Tx []byte `json:"tx"`
TxResult *types.TxResult `json:"tx_result"`
type Tx struct {
Hash types.Hash `json:"hash"`
Height int64 `json:"height"`
Index uint32 `json:"index"`
Tx *types.Transaction `json:"tx"`
TxResult *types.TxResult `json:"tx_result"`
}

type BlockHeader types.BlockHeader

func (b BlockHeader) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Version uint16 `json:"version"`
Height int64 `json:"height"`
NumTxns uint32 `json:"num_txns"`
PrevHash types.Hash `json:"prev_hash"`
PrevAppHash types.Hash `json:"prev_app_hash"`
// Timestamp is the unix millisecond timestamp
Timestamp int64 `json:"timestamp"`
MerkleRoot types.Hash `json:"merkle_root"`
ValidatorSetHash types.Hash `json:"validator_set_hash"`
}{
Version: b.Version,
Height: b.Height,
NumTxns: b.NumTxns,
PrevHash: b.PrevHash,
PrevAppHash: b.PrevAppHash,
Timestamp: b.Timestamp.UnixMilli(),
MerkleRoot: b.MerkleRoot,
ValidatorSetHash: b.ValidatorSetHash,
})
}

type Block struct {
Header *BlockHeader `json:"header"`
Txns [][]byte `json:"txns"`
Signature []byte `json:"signature"`
Hash types.Hash `json:"hash"`
AppHash types.Hash `json:"app_hash"`
}

type BlockResult struct {
Height int64 `json:"height"`
Hash types.Hash `json:"hash"`
TxResults []types.TxResult `json:"tx_results"`
}

type Genesis struct {
ChainID string `json:"chain_id"`
// Leader is the leader's public key.
Leader types.HexBytes `json:"leader"`
// Validators is the list of genesis validators (including the leader).
Validators []*types.Validator `json:"validators"`
// MaxBlockSize is the maximum size of a block in bytes.
MaxBlockSize int64 `json:"max_block_size"`
// JoinExpiry is the number of blocks after which the validators
// join request expires if not approved.
JoinExpiry int64 `json:"join_expiry"`
// VoteExpiry is the default number of blocks after which the validators
// vote expires if not approved.
VoteExpiry int64 `json:"vote_expiry"`
// DisabledGasCosts dictates whether gas costs are disabled.
DisabledGasCosts bool `json:"disabled_gas_costs"`
// MaxVotesPerTx is the maximum number of votes that can be included in a
// single transaction.
MaxVotesPerTx int64 `json:"max_votes_per_tx"`
}

type NamedTx struct {
Hash types.Hash `json:"hash"`
Tx *types.Transaction `json:"tx"`
}
8 changes: 4 additions & 4 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,8 @@ func (n *Node) BroadcastTx(ctx context.Context, tx *ktypes.Transaction, _ /*sync
}

// ChainTx return tx info that is used in Chain rpc.
func (n *Node) ChainTx(hash types.Hash) (*chainTypes.ChainTx, error) {
raw, height, blkHash, blkIdx, err := n.bki.GetTx(hash)
func (n *Node) ChainTx(hash types.Hash) (*chainTypes.Tx, error) {
tx, height, blkHash, blkIdx, err := n.bki.GetTx(hash)
if err != nil {
return nil, err
}
Expand All @@ -525,11 +525,11 @@ func (n *Node) ChainTx(hash types.Hash) (*chainTypes.ChainTx, error) {
return nil, errors.New("invalid block index")
}
res := blkResults[blkIdx]
return &chainTypes.ChainTx{
return &chainTypes.Tx{
Hash: hash,
Height: height,
Index: blkIdx,
Tx: raw,
Tx: tx,
TxResult: &res,
}, nil
}
Expand Down
Loading

0 comments on commit 0e39a4e

Please sign in to comment.