-
Notifications
You must be signed in to change notification settings - Fork 12
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
feat(rpc): chain RPCs #1147
Merged
Merged
feat(rpc): chain RPCs #1147
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
51e937c
feat(rpc): chain RPCs
Yaiba 6a9315c
pass linter
Yaiba 63af9b4
support chain.validators and chain.consensus_params
Yaiba 0e39a4e
adjust for client
Yaiba fcbb5cf
adjust for client
Yaiba 4bbe029
fix build error
Yaiba 78e8529
add RecheckTxs method
Yaiba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 int, txs []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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package chain | ||
|
||
import ( | ||
"github.com/kwilteam/kwil-db/core/types" | ||
) | ||
|
||
type HealthRequest struct{} | ||
|
||
type BlockRequest struct { | ||
Height int64 `json:"height"` | ||
// Hash is the block hash. If both Height and Hash are provided, hash will be used | ||
Hash types.Hash `json:"hash"` | ||
} | ||
|
||
type BlockResultRequest struct { | ||
Height int64 `json:"height"` | ||
// Hash is the block hash. If both Height and Hash are provided, hash will be used | ||
Hash types.Hash `json:"hash"` | ||
} | ||
|
||
type TxRequest struct { | ||
Hash types.Hash `json:"hash"` | ||
} | ||
|
||
type GenesisRequest struct{} | ||
type ConsensusParamsRequest struct{} | ||
type ValidatorsRequest struct { | ||
Height int64 `json:"height"` | ||
} | ||
type UnconfirmedTxsRequest struct { | ||
Limit int `json:"limit"` | ||
} |
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,17 @@ | ||
package chain | ||
|
||
import ( | ||
jsonrpc "github.com/kwilteam/kwil-db/core/rpc/json" | ||
) | ||
|
||
const ( | ||
MethodVersion jsonrpc.Method = "chain.version" | ||
MethodHealth jsonrpc.Method = "chain.health" | ||
MethodBlock jsonrpc.Method = "chain.block" | ||
MethodBlockResult jsonrpc.Method = "chain.block_result" | ||
MethodTx jsonrpc.Method = "chain.tx" | ||
MethodGenesis jsonrpc.Method = "chain.genesis" | ||
MethodConsensusParams jsonrpc.Method = "chain.consensus_params" | ||
MethodValidators jsonrpc.Method = "chain.validators" | ||
MethodUnconfirmedTxs jsonrpc.Method = "chain.unconfirmed_txs" | ||
) |
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,66 @@ | ||
package chain | ||
|
||
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. | ||
type HealthResponse struct { | ||
ChainID string `json:"chain_id"` | ||
Height int64 `json:"height"` | ||
Healthy bool `json:"healthy"` | ||
} | ||
|
||
// BlockResponse is the block information | ||
type BlockResponse chaintypes.Block | ||
|
||
type BlockResultResponse chaintypes.BlockResult | ||
|
||
type TxResponse chaintypes.Tx | ||
|
||
// GenesisResponse is the same as kwil-db/config.GenesisConfig, with JSON tags. | ||
type GenesisResponse chaintypes.Genesis | ||
|
||
type ConsensusParamsResponse types.ConsensusParams | ||
|
||
func (r ConsensusParamsResponse) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(&struct { | ||
// 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"` | ||
|
||
// MigrationStatus determines the status of the migration. | ||
MigrationStatus string `json:"migration_status"` | ||
|
||
// MaxVotesPerTx is the maximum number of votes that can be included in a | ||
// single transaction. | ||
MaxVotesPerTx int64 `json:"max_votes_per_tx"` | ||
}{ | ||
MaxBlockSize: r.MaxBlockSize, | ||
JoinExpiry: r.JoinExpiry, | ||
VoteExpiry: r.VoteExpiry, | ||
DisabledGasCosts: r.DisabledGasCosts, | ||
MigrationStatus: string(r.MigrationStatus), | ||
MaxVotesPerTx: r.MaxVotesPerTx, | ||
}) | ||
} | ||
|
||
type ValidatorsResponse struct { | ||
Height int64 `json:"height"` | ||
Validators []*types.Validator `json:"validators"` | ||
} | ||
|
||
type UnconfirmedTxsResponse struct { | ||
Total int `json:"total"` | ||
Txs []chaintypes.NamedTx `json:"txs"` | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be simplify to just use core types