Skip to content

Commit

Permalink
abci height
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Aug 22, 2024
1 parent 04d5239 commit 7b6810f
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 3 deletions.
9 changes: 9 additions & 0 deletions gno.land/pkg/gnoclient/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type (
mockABCIQuery func(path string, data []byte) (*ctypes.ResultABCIQuery, error)
mockABCIInfo func() (*ctypes.ResultABCIInfo, error)
mockABCIQueryWithOptions func(path string, data []byte, opts client.ABCIQueryOptions) (*ctypes.ResultABCIQuery, error)
mockABCIHeight func(height int64) (*ctypes.ResultABCIQuery, error)
mockBroadcastTxAsync func(tx types.Tx) (*ctypes.ResultBroadcastTx, error)
mockBroadcastTxSync func(tx types.Tx) (*ctypes.ResultBroadcastTx, error)
mockGenesis func() (*ctypes.ResultGenesis, error)
Expand All @@ -126,6 +127,7 @@ type mockRPCClient struct {
abciQuery mockABCIQuery
abciInfo mockABCIInfo
abciQueryWithOptions mockABCIQueryWithOptions
abciHeight mockABCIHeight
broadcastTxAsync mockBroadcastTxAsync
broadcastTxSync mockBroadcastTxSync
genesis mockGenesis
Expand Down Expand Up @@ -173,6 +175,13 @@ func (m *mockRPCClient) ABCIQueryWithOptions(path string, data []byte, opts clie
return nil, nil
}

func (m *mockRPCClient) ABCIHeight(height int64) (*ctypes.ResultABCIQuery, error) {
if m.abciHeight != nil {
return m.abciHeight(height)
}
return nil, nil
}

func (m *mockRPCClient) BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error) {
if m.broadcastTxAsync != nil {
return m.broadcastTxAsync(tx)
Expand Down
15 changes: 15 additions & 0 deletions tm2/pkg/bft/rpc/client/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ func (b *RPCBatch) ABCIQueryWithOptions(path string, data []byte, opts ABCIQuery
return nil
}

func (b *RPCBatch) ABCIHeight(height int64) error {
// Prepare the RPC request
request, err := newRequest(
abciHeightMethod,
map[string]any{"height": height},
)
if err != nil {
return fmt.Errorf("unable to create request, %w", err)

Check warning on line 171 in tm2/pkg/bft/rpc/client/batch.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/bft/rpc/client/batch.go#L171

Added line #L171 was not covered by tests
}

b.addRequest(request, &ctypes.ResultABCIQuery{})

return nil
}

func (b *RPCBatch) BroadcastTxCommit(tx types.Tx) error {
// Prepare the RPC request
request, err := newRequest(
Expand Down
18 changes: 18 additions & 0 deletions tm2/pkg/bft/rpc/client/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@ func TestRPCBatch_Endpoints(t *testing.T) {
return castResult
},
},
{
abciHeightMethod,
&ctypes.ResultABCIQuery{
Response: abci.ResponseQuery{
Value: []byte("dummy"),
Height: 10,
},
},
func(batch *RPCBatch) {
require.NoError(t, batch.ABCIHeight(10))
},
func(result any) any {
castResult, ok := result.(*ctypes.ResultABCIQuery)
require.True(t, ok)

return castResult
},
},
{
broadcastTxCommitMethod,
&ctypes.ResultBroadcastTxCommit{
Expand Down
10 changes: 10 additions & 0 deletions tm2/pkg/bft/rpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
statusMethod = "status"
abciInfoMethod = "abci_info"
abciQueryMethod = "abci_query"
abciHeightMethod = "abci_height"
broadcastTxCommitMethod = "broadcast_tx_commit"
broadcastTxAsyncMethod = "broadcast_tx_async"
broadcastTxSyncMethod = "broadcast_tx_sync"
Expand Down Expand Up @@ -143,6 +144,15 @@ func (c *RPCClient) ABCIQueryWithOptions(path string, data []byte, opts ABCIQuer
)
}

func (c *RPCClient) ABCIHeight(height int64) (*ctypes.ResultABCIQuery, error) {
return sendRequestCommon[ctypes.ResultABCIQuery](
c.caller,
c.requestTimeout,
abciHeightMethod,
map[string]any{"height": height},
)
}

func (c *RPCClient) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
return sendRequestCommon[ctypes.ResultBroadcastTxCommit](
c.caller,
Expand Down
37 changes: 37 additions & 0 deletions tm2/pkg/bft/rpc/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,43 @@ func TestRPCClient_ABCIQuery(t *testing.T) {
assert.Equal(t, expectedQuery, query)
}

func TestRPCClient_ABCIHeight(t *testing.T) {
t.Parallel()

var (
height = int64(10)

expectedResult = &ctypes.ResultABCIQuery{
Response: abci.ResponseQuery{
Value: []byte("dummy"),
Height: height,
},
}

verifyFn = func(t *testing.T, params map[string]any) {
t.Helper()

assert.Equal(t, fmt.Sprintf("%d", height), params["height"])
}

mockClient = generateMockRequestClient(
t,
abciHeightMethod,
verifyFn,
expectedResult,
)
)

c := NewRPCClient(mockClient)

// Get the query result from given height
result, err := c.ABCIHeight(height)
require.NoError(t, err)

assert.Equal(t, expectedResult, result)
t.Log(result)
}

func TestRPCClient_BroadcastTxCommit(t *testing.T) {
t.Parallel()

Expand Down
15 changes: 15 additions & 0 deletions tm2/pkg/bft/rpc/client/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,21 @@ func TestRPCClient_E2E_Endpoints(t *testing.T) {
assert.Equal(t, expectedResult, result)
},
},
{
abciHeightMethod,
&ctypes.ResultABCIQuery{
Response: abci.ResponseQuery{
Value: []byte("dummy"),
Height: 1000,
},
},
func(client *RPCClient, expectedResult any) {
result, err := client.ABCIHeight(1000)
require.NoError(t, err)

assert.Equal(t, expectedResult, result)
},
},
{
broadcastTxCommitMethod,
&ctypes.ResultBroadcastTxCommit{
Expand Down
4 changes: 4 additions & 0 deletions tm2/pkg/bft/rpc/client/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (c *Local) ABCIQueryWithOptions(path string, data []byte, opts ABCIQueryOpt
return core.ABCIQuery(c.ctx, path, data, opts.Height, opts.Prove)
}

func (c *Local) ABCIHeight(height int64) (*ctypes.ResultABCIQuery, error) {
return core.ABCIHeight(c.ctx, height)

Check warning on line 70 in tm2/pkg/bft/rpc/client/local.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/bft/rpc/client/local.go#L69-L70

Added lines #L69 - L70 were not covered by tests
}

func (c *Local) BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
return core.BroadcastTxCommit(c.ctx, tx)
}
Expand Down
2 changes: 1 addition & 1 deletion tm2/pkg/bft/rpc/client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type ABCIClient interface {
ABCIQuery(path string, data []byte) (*ctypes.ResultABCIQuery, error)
ABCIQueryWithOptions(path string, data []byte,
opts ABCIQueryOptions) (*ctypes.ResultABCIQuery, error)

ABCIHeight(height int64) (*ctypes.ResultABCIQuery, error)
// Writing to abci app
BroadcastTxCommit(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error)
BroadcastTxAsync(tx types.Tx) (*ctypes.ResultBroadcastTx, error)
Expand Down
59 changes: 59 additions & 0 deletions tm2/pkg/bft/rpc/core/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package core

import (
"fmt"

abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"
rpctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/lib/types"
Expand Down Expand Up @@ -69,6 +71,63 @@ func ABCIQuery(ctx *rpctypes.Context, path string, data []byte, height int64, pr
return &ctypes.ResultABCIQuery{Response: resQuery}, nil
}

// Query at a specific block height.
//
// ```shell
// curl 'localhost:26657/abci_height?height=100'
// ```
//
// ```go
// client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket")
//
// err := client.Start()
//
// if err != nil {
// // handle error
// }
//
// defer client.Stop()
// result, err := client.ABCIHeight(100)
// ```
//
// > The above command returns JSON structured like this:
//
// ```json
//
// {
// "error": "",
// "result": {
// "response": {
// "log": "exists",
// "height": "100",
// // ...
// }
// },
// "id": "",
// "jsonrpc": "2.0"
// }
//
// ```
//
// ### Query Parameter
//
// - height (int64): The height to query at.
func ABCIHeight(ctx *rpctypes.Context, height int64) (*ctypes.ResultABCIQuery, error) {
if height <= 0 {
return nil, fmt.Errorf("height must be greater than 0")

Check warning on line 117 in tm2/pkg/bft/rpc/core/abci.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/bft/rpc/core/abci.go#L115-L117

Added lines #L115 - L117 were not covered by tests
}

resQuery, err := proxyAppQuery.QuerySync(abci.RequestQuery{
Height: height,
})
if err != nil {
return nil, err

Check warning on line 124 in tm2/pkg/bft/rpc/core/abci.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/bft/rpc/core/abci.go#L120-L124

Added lines #L120 - L124 were not covered by tests
}

logger.Info("ABCIAtHeight", "height", height, "result", resQuery)
return &ctypes.ResultABCIQuery{Response: resQuery}, nil

Check warning on line 128 in tm2/pkg/bft/rpc/core/abci.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/bft/rpc/core/abci.go#L127-L128

Added lines #L127 - L128 were not covered by tests
}

// Get some info about the application.
//
// ```shell
Expand Down
5 changes: 3 additions & 2 deletions tm2/pkg/bft/rpc/core/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ var Routes = map[string]*rpc.RPCFunc{
"broadcast_tx_async": rpc.NewRPCFunc(BroadcastTxAsync, "tx"),

// abci API
"abci_query": rpc.NewRPCFunc(ABCIQuery, "path,data,height,prove"),
"abci_info": rpc.NewRPCFunc(ABCIInfo, ""),
"abci_query": rpc.NewRPCFunc(ABCIQuery, "path,data,height,prove"),
"abci_info": rpc.NewRPCFunc(ABCIInfo, ""),
"abci_height": rpc.NewRPCFunc(ABCIHeight, "height"),
}

func AddUnsafeRoutes() {
Expand Down

0 comments on commit 7b6810f

Please sign in to comment.