Skip to content

Commit

Permalink
go-kosu: Add LatestOrders RPC endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Chain committed Oct 9, 2019
1 parent 46ca894 commit 70eac80
Show file tree
Hide file tree
Showing 21 changed files with 316 additions and 93 deletions.
1 change: 1 addition & 0 deletions packages/go-kosu/CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## master

- Add LatestOrders RPC endpoint
- Refactor ./cmd/kosud
- Allow to start the node in --lite mode
- Embed RPC Server in kosud
Expand Down
23 changes: 23 additions & 0 deletions packages/go-kosu/abci/cli/cli.go
Expand Up @@ -9,11 +9,13 @@ import (
"strconv"

"github.com/spf13/cobra"

"github.com/tendermint/tendermint/config"

"github.com/ParadigmFoundation/kosu-monorepo/packages/go-kosu/abci"
"github.com/ParadigmFoundation/kosu-monorepo/packages/go-kosu/abci/types"
"github.com/ParadigmFoundation/kosu-monorepo/packages/go-kosu/service"
"github.com/ParadigmFoundation/kosu-monorepo/packages/go-kosu/store"
)

// CLI provides functions to build a cli client
Expand Down Expand Up @@ -188,6 +190,27 @@ func (cli *CLI) QueryPoster() *cobra.Command {
}
}

func (cli *CLI) QueryLatestOrders() *cobra.Command {
return &cobra.Command{
Use: "latest-orders",
Short: "Query the latest orders",
Run: func(cmd *cobra.Command, _ []string) {
txs, err := cli.client.QueryLatestOrders()
if err != nil {
printAndExit("%v\n", err)
}

for _, tx := range txs {
order, err := store.NewOrderFromProto(&tx)
if err != nil {
printAndExit("%v\n", err)
}
fmt.Println(order)
}
},
}
}

// NewInitCommand returns the init command
func NewInitCommand(homeFlag string) *cobra.Command {
return &cobra.Command{
Expand Down
43 changes: 43 additions & 0 deletions packages/go-kosu/abci/client.go
Expand Up @@ -5,13 +5,18 @@ import (
"errors"

"github.com/gogo/protobuf/proto"

"github.com/tendermint/tendermint/libs/pubsub/query"
"github.com/tendermint/tendermint/rpc/client"
rpctypes "github.com/tendermint/tendermint/rpc/core/types"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/codec"
sdktypes "github.com/cosmos/cosmos-sdk/store/types"

"github.com/ParadigmFoundation/kosu-monorepo/packages/go-kosu/abci/types"
"github.com/ParadigmFoundation/kosu-monorepo/packages/go-kosu/store"
"github.com/ParadigmFoundation/kosu-monorepo/packages/go-kosu/store/cosmos"
)

var (
Expand Down Expand Up @@ -179,6 +184,24 @@ func (c *Client) QueryTotalOrders() (uint64, error) {
return num, nil
}

func (c *Client) QueryLatestOrders() ([]types.TransactionOrder, error) {
KVs, err := c.QuerySubSpace("orders", []byte(cosmos.OrderKeyPrefix))
if err != nil {
return nil, err
}

var txs []types.TransactionOrder
for _, kv := range KVs {
var tx types.TransactionOrder
if err := c.cdc.Decode(kv.Value, &tx); err != nil {
return nil, err
}
txs = append(txs, tx)
}

return txs, nil
}

// Query is a generic query interface.
// It will use the store.DefaultCodec codec to decode the `response.Value`.
func (c *Client) Query(path string, data []byte, v interface{}) error {
Expand All @@ -198,3 +221,23 @@ func (c *Client) Query(path string, data []byte, v interface{}) error {

return c.cdc.Decode(res.Value, v)
}

func (c *Client) QuerySubSpace(store string, data []byte) ([]sdktypes.KVPair, error) {
out, err := c.ABCIQuery("/store/"+store+"/subspace", data)
if err != nil {
return nil, err
}
res := out.Response

if res.IsErr() {
return nil, errors.New(res.GetLog())
}

var KVs []sdktypes.KVPair
cdc := codec.New()
if err := cdc.UnmarshalBinaryLengthPrefixed(res.Value, &KVs); err != nil {
return nil, err
}

return KVs, nil
}
1 change: 1 addition & 0 deletions packages/go-kosu/abci/genesis.go
Expand Up @@ -93,6 +93,7 @@ var GenesisAppState = &Genesis{
PeriodLength: 10,
PeriodLimit: 100000,
BlocksBeforePruning: 10,
OrdersLimit: 100,
},
SnapshotBlock: 0,
InitialValidatorInfo: nil,
Expand Down
4 changes: 2 additions & 2 deletions packages/go-kosu/abci/order.go
Expand Up @@ -63,8 +63,8 @@ func (app *App) deliverOrderTx(tx *types.TransactionOrder) abci.ResponseDeliverT
poster.Limit--
app.store.SetPoster(posterAddress.String(), *poster)

total := app.store.TotalOrders()
app.store.SetTotalOrders(total + 1)
limit := app.store.ConsensusParams().OrdersLimit
app.store.SetOrder(tx, int(limit))
// end state modification

return abci.ResponseDeliverTx{
Expand Down
16 changes: 8 additions & 8 deletions packages/go-kosu/abci/types/README.md
Expand Up @@ -47,14 +47,14 @@ BigInt

ConsensusParams

| Field | Type | Label | Description |
| ---------------------- | ----------------- | ----- | ----------- |
| finality_threshold | [uint32](#uint32) | | |
| period_limit | [uint64](#uint64) | | |
| period_length | [uint32](#uint32) | | |
| max_order_bytes | [uint32](#uint32) | | |
| confirmation_threshold | [uint64](#uint64) | | |
| blocks_before_pruning | [uint64](#uint64) | | |
| Field | Type | Label | Description |
| --------------------- | ----------------- | ----- | ----------- |
| finality_threshold | [uint32](#uint32) | | |
| period_limit | [uint64](#uint64) | | |
| period_length | [uint32](#uint32) | | |
| max_order_bytes | [uint32](#uint32) | | |
| blocks_before_pruning | [uint64](#uint64) | | |
| orders_limit | [uint32](#uint32) | | |

<a name="kosu.OrderArgument"></a>

Expand Down
135 changes: 72 additions & 63 deletions packages/go-kosu/abci/types/types.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/go-kosu/abci/types/types.proto
Expand Up @@ -35,6 +35,7 @@ message ConsensusParams {
uint32 period_length = 3;
uint32 max_order_bytes = 4;
uint64 blocks_before_pruning = 5;
uint32 orders_limit = 6;
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/go-kosu/cmd/kosu-cli/main.go
Expand Up @@ -74,6 +74,7 @@ func main() {
abci.QueryConsensusParams(),
abci.QueryPoster(),
abci.QueryRoundInfo(),
abci.QueryLatestOrders(),
)

rootCmd.AddCommand(
Expand Down
4 changes: 3 additions & 1 deletion packages/go-kosu/docker-compose.yml
Expand Up @@ -15,7 +15,9 @@ services:
container_name: kosu-node-0
ports:
- "8000:26657"
command: "kosud --home /go-kosu/testnet/node0 start -E ws://172.17.0.1:8546"
- "14341:14341"
- "14342:14342"
command: "kosud --home /go-kosu/testnet/node0 start -E ws://172.17.0.1:8546 --rpc"
networks:
localnet:
ipv4_address: 192.167.10.2
Expand Down

0 comments on commit 70eac80

Please sign in to comment.