-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
265 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.