Skip to content
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

go-kosu: RPC numberPosters and totalOrders #224

Merged
merged 3 commits into from Aug 22, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Next

go-kosu: RPC numberPosters and totalOrders

  • Loading branch information
gchaincl committed Aug 20, 2019
commit 7a627cc831a906245dea63052467f3b5eef914fc
@@ -54,7 +54,7 @@ func NewAppWithConfig(db db.DB, cfg *config.Config) *App {
}

app := &App{
store: cosmos.NewStore(db, new(cosmos.ProtoCodec)),
store: cosmos.NewStore(db, store.DefaultCodec),
Config: cfg,
log: logger.With("module", "app"),
}
@@ -158,6 +158,26 @@ func (c *Client) QueryValidator(addr string) (*types.Validator, error) {
return &pb, nil
}

// QueryTotalOrders performs a ABCI Query to "/chain/totalorders"
func (c *Client) QueryTotalOrders() (uint64, error) {
out, err := c.ABCIQuery("/chain/key", []byte("totalorders"))
if err != nil {
return 0, err
}

res := out.Response
if res.IsErr() {
return 0, errors.New(res.GetLog())
}

if len(res.Value) == 0 {
return 0, errors.New("empty")
}

pb := proto.NewBuffer(res.Value)
return pb.DecodeFixed64()
}

func (c *Client) query(path string, data []byte, pb proto.Message) error {
out, err := c.ABCIQuery(path, data)
if err != nil {
@@ -62,6 +62,9 @@ func (app *App) deliverOrderTx(tx *types.TransactionOrder) abci.ResponseDeliverT
// begin state modification
poster.Limit--
app.store.SetPoster(posterAddress.String(), *poster)

total := app.store.TotalOrders()
app.store.SetTotalOrders(total + 1)
// end state modification

return abci.ResponseDeliverTx{
@@ -159,3 +159,27 @@ func TestNewRebalances(t *testing.T) {
assert.Equal(t, tx.String(), e.String())
}
}

func TestNumberPosters(t *testing.T) {
app, rpc, closer := newServerClient(t)
defer closer()

addresses := []string{
"0x0000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000002",
"0x0000000000000000000000000000000000000003",
"0x0000000000000000000000000000000000000004",
}

for _, addr := range addresses {
app.Store().SetPoster(addr, types.Poster{
Balance: types.NewBigIntFromInt(100),
})
}

var num uint64
err := rpc.Call(&num, "kosu_numberPosters")
require.NoError(t, err)

assert.EqualValues(t, len(addresses), num)
}
@@ -5,6 +5,7 @@ import (
"encoding/hex"
"go-kosu/abci"
"go-kosu/abci/types"
"go-kosu/store"
"log"
"strings"

@@ -688,3 +689,23 @@ curl -X POST localhost:14341 \
func (s *Service) QueryPoster(addr string) (*types.Poster, error) {
return s.abci.QueryPoster(addr)
}

// TotalOrders ...
func (s *Service) TotalOrders() (uint64, error) {
return s.abci.QueryTotalOrders()
}

// NumberPosters ...
func (s *Service) NumberPosters() (uint64, error) {
res, err := s.abci.ABCIQuery("/poster/number", nil)
if err != nil {
return 0, err
}

var num uint64
if err := store.DefaultCodec.Decode(res.Response.Value, &num); err != nil {
return 0, err
}

return num, nil
}
@@ -1,4 +1,4 @@
package cosmos
package store

import (
"bytes"
@@ -98,3 +98,6 @@ func (c *ProtoCodec) Decode(bs []byte, s interface{}) error {
msg := s.(proto.Message)
return buf.Unmarshal(msg)
}

// DefaultCodec is the default codec to be used
var DefaultCodec = new(ProtoCodec)
@@ -11,11 +11,12 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"go-kosu/abci/types"
"go-kosu/store"
)

// Store stores the application state
type Store struct {
codec Codec
codec store.Codec

cms *rootmulti.Store

@@ -26,7 +27,7 @@ type Store struct {
}

// NewStore returns a new store
func NewStore(db db.DB, cdc Codec) *Store {
func NewStore(db db.DB, cdc store.Codec) *Store {
s := &Store{codec: cdc,
cms: rootmulti.NewStore(db),

@@ -105,9 +106,33 @@ func (s *Store) LastCommitID() cosmos.CommitID { return s.cms.LastCommitID() }

// Query wrap the rootmulti.Query method
func (s *Store) Query(req abci.RequestQuery) abci.ResponseQuery {
switch req.Path {
case "/poster/number":
return s.queryPosterNumber()
}
return s.cms.Query(req)
}

func (s *Store) queryPosterNumber() (resp abci.ResponseQuery) {
var num uint64
s.All(s.posterKey, func(_ string, _ []byte) {
num++
})

buf, err := s.codec.Encode(num)
if err != nil {
resp.Code = 1
resp.Log = err.Error()
} else {
resp.Value = buf
}

return resp
}

// Codec returns the storage codec
func (s *Store) Codec() store.Codec { return s.codec }

// SetRoundInfo sets the RoundInfo
func (s *Store) SetRoundInfo(v types.RoundInfo) {
s.Set("roundinfo", s.chainKey, &v)
@@ -144,6 +169,18 @@ func (s *Store) LastEvent() uint64 {
return v
}

// SetTotalOrders sets the TotalOrders
func (s *Store) SetTotalOrders(v uint64) {
s.Set("totalorders", s.chainKey, v)
}

// TotalOrders gets the TotalOrders
func (s *Store) TotalOrders() uint64 {
var v uint64
s.Get("totalorders", s.chainKey, &v)
return v
}

// WitnessTxExists checks if a given WitnessTx has been persisted
func (s *Store) WitnessTxExists(id []byte) bool {
return s.Has(string(id), s.witnessKey)
@@ -10,9 +10,9 @@ import (
)

func TestCosmosStore(t *testing.T) {
for _, cdc := range []Codec{
&ProtoCodec{},
&GobCodec{},
for _, cdc := range []store.Codec{
&store.ProtoCodec{},
&store.GobCodec{},
} {
t.Run(cdc.String(), func(t *testing.T) {
f := func() (store.Store, func()) {
@@ -13,6 +13,7 @@ type Store interface {
Commit() store.CommitID
LastCommitID() store.CommitID
Query(abci.RequestQuery) abci.ResponseQuery
Codec() Codec

RoundInfo() types.RoundInfo
SetRoundInfo(types.RoundInfo)
@@ -23,6 +24,9 @@ type Store interface {
LastEvent() uint64
SetLastEvent(uint64)

TotalOrders() uint64
SetTotalOrders(uint64)

WitnessTxExists([]byte) bool
WitnessTx([]byte) *types.TransactionWitness
IterateWitnessTxs(func(tx *types.TransactionWitness))
@@ -23,6 +23,7 @@ func TestSuite(t *testing.T, f Factory) {
{"RoundInfo", TestRoundInfo},
{"ConsensusParams", TestConsensusParams},
{"LastEvent", TestLastEvent},
{"TotalOrders", TestTotalOrders},
{"Witness", TestWitness},
{"Poster", TestPoster},
{"Validator", TestValidator},
@@ -64,6 +65,16 @@ func TestLastEvent(t *testing.T, s store.Store) {
assert.Equal(t, lastEvent, s.LastEvent())
}

// TestTotalOrders verifies the LastTotalOrders storage behavior
func TestTotalOrders(t *testing.T, s store.Store) {
s.SetTotalOrders(1)
s.SetTotalOrders(2)
s.SetTotalOrders(3)
s.SetTotalOrders(4)

assert.Equal(t, uint64(4), s.TotalOrders())
}

// TestWitness verifies the Witness storage behavior
func TestWitness(t *testing.T, s store.Store) {
witnessTx := &types.TransactionWitness{
@@ -43,7 +43,7 @@ func (s *Suite) TestOrderTx() {
})
})

Convey("When a RPC subscription is active", func() {
Convey("Given a RPC client", func() {
client := rpc.DialInProc(
rpc.NewServer(s.client),
)
@@ -58,13 +58,21 @@ func (s *Suite) TestOrderTx() {
defer sub.Unsubscribe()

Convey("And a OrderTx is sent", func() {
BroadcastTxSync(t, s.client, tx)
BroadcastTxCommit(t, s.client, tx)

Convey("Event should be sent and matches the Broadcasted Tx", func() {
event := <-ch
So(event.String(), ShouldEqual, tx.String())
})

Convey("TotalOrders is updated", func() {
var total uint64
err := client.Call(&total, "kosu_totalOrders")
require.NoError(t, err)
So(total, ShouldEqual, 1)
})
})

})

Convey("And a non existing poster", func() {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.