fix up proto problems with orderbook

This commit is contained in:
Callum Waters
2022-11-22 14:24:43 +01:00
parent 5651a21adb
commit 22d04dd19d
11 changed files with 3483 additions and 1060 deletions

View File

@@ -3,6 +3,7 @@ package orderbook
import (
"bytes"
"encoding/binary"
fmt "fmt"
"github.com/cosmos/gogoproto/proto"
dbm "github.com/tendermint/tm-db"
@@ -89,28 +90,29 @@ func New(db dbm.DB) (*StateMachine, error) {
publicKeys = make(map[string]struct{})
commodities = make(map[string]struct{})
pairs = make(map[string]*Pair)
markets = make(map[string]*Market)
lastHeight uint64
lastHash []byte
)
for ; iter.Valid(); iter.Next() {
if bytes.HasPrefix(iter.Key(), pairKey) {
var pair *Pair
if err := proto.Unmarshal(iter.Value(), pair); err != nil {
var pair Pair
if err := proto.Unmarshal(iter.Value(), &pair); err != nil {
return nil, err
}
pairs[pair.String()] = pair
pairs[pair.String()] = &pair
commodities[pair.BuyersDenomination] = struct{}{}
markets[pair.String()] = NewMarket(&pair)
}
if bytes.HasPrefix(iter.Key(), accountKey) {
var acc *Account
if err := proto.Unmarshal(iter.Value(), acc); err != nil {
var acc Account
if err := proto.Unmarshal(iter.Value(), &acc); err != nil {
return nil, err
}
accounts = append(accounts, acc)
accounts = append(accounts, &acc)
publicKeys[string(acc.PublicKey)] = struct{}{}
}
@@ -119,13 +121,6 @@ func New(db dbm.DB) (*StateMachine, error) {
lastHeight = binary.BigEndian.Uint64(state[:4])
lastHash = state[4:]
}
var acc *Account
if err := proto.Unmarshal(iter.Value(), acc); err != nil {
return nil, err
}
accounts = append(accounts, acc)
}
return &StateMachine{
@@ -133,7 +128,7 @@ func New(db dbm.DB) (*StateMachine, error) {
pairs: pairs,
commodities: commodities,
publicKeys: publicKeys,
markets: make(map[string]*Market),
markets: markets,
lastHeight: int64(lastHeight),
lastHash: lastHash,
db: db,
@@ -408,10 +403,7 @@ func (sm *StateMachine) Commit() types.ResponseCommit {
if err != nil {
panic(err)
}
var key []byte
copy(key, accountKey)
binary.BigEndian.PutUint64(key, accountID)
key := binary.BigEndian.AppendUint64(accountKey, accountID)
if err := batch.Set(key, value); err != nil {
panic(err)
}
@@ -424,9 +416,7 @@ func (sm *StateMachine) Commit() types.ResponseCommit {
if err != nil {
panic(err)
}
var key []byte
copy(key, pairKey)
binary.BigEndian.PutUint64(key, uint64(pairID+id))
key := binary.BigEndian.AppendUint64(pairKey, uint64(pairID+id))
if err := batch.Set(key, value); err != nil {
panic(err)
}
@@ -536,6 +526,37 @@ func (sm *StateMachine) isMatchedOrderValid(order *MatchedOrder, pair *Pair) boo
return true
}
// InitDB takes an empty DB instance and populates it with the
// provided pairs and accounts. Note that the order here is important
func InitDB(db dbm.DB, pairs []*Pair, accounts []*Account) error {
batch := db.NewBatch()
for id, account := range accounts {
value, err := proto.Marshal(account)
if err != nil {
return err
}
key := binary.BigEndian.AppendUint64(accountKey, uint64(id))
if err := batch.Set(key, value); err != nil {
return err
}
}
for id, pair := range pairs {
value, err := proto.Marshal(pair)
if err != nil {
return err
}
key := binary.BigEndian.AppendUint64(pairKey, uint64(id))
fmt.Println(key)
if err := batch.Set(key, value); err != nil {
return err
}
}
return batch.WriteSync()
}
func rejectProposal() types.ResponseProcessProposal {
return types.ResponseProcessProposal{Status: types.ResponseProcessProposal_REJECT}
}

View File

@@ -1,6 +1,7 @@
package orderbook_test
import (
"crypto/ed25519"
"testing"
"github.com/cosmos/gogoproto/proto"
@@ -9,56 +10,65 @@ import (
"github.com/tendermint/tendermint/abci/example/orderbook"
"github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
)
// TODO: we should also check that CheckTx adds bids and asks to the app-side mempool
func TestCheckTx(t *testing.T) {
app, err := orderbook.New(dbm.NewMemDB())
db := dbm.NewMemDB()
require.NoError(t, orderbook.InitDB(db, []*orderbook.Pair{testPair}, nil))
app, err := orderbook.New(db)
require.NoError(t, err)
testCases := []struct {
name string
msg *orderbook.Msg
responseCode int
responseCode uint32
expOrderSize int
}{
{
name: "test empty tx",
msg: &orderbook.Msg{},
responseCode: orderbook.StatusErrUnknownMessage,
},
// {
// name: "test empty tx",
// msg: &orderbook.Msg{},
// responseCode: orderbook.StatusErrUnknownMessage,
// },
{
name: "test msg ask",
msg: &orderbook.Msg{Sum: &orderbook.Msg_MsgAsk{MsgAsk: &orderbook.MsgAsk{
Pair: testPair,
AskOrder: &orderbook.OrderAsk{
Quantity: 10,
AskPrice: 1,
OwnerId: 1,
Signature: []byte("signature"),
msg: &orderbook.Msg{
Sum: &orderbook.Msg_MsgAsk{
MsgAsk: &orderbook.MsgAsk{
Pair: testPair,
AskOrder: &orderbook.OrderAsk{
Quantity: 10,
AskPrice: 1,
OwnerId: 1,
Signature: crypto.CRandBytes(ed25519.SignatureSize),
},
},
},
}}},
responseCode: orderbook.StatusOK,
},
{
name: "test msg bid",
msg: &orderbook.Msg{Sum: &orderbook.Msg_MsgBid{MsgBid: &orderbook.MsgBid{
Pair: testPair,
BidOrder: &orderbook.OrderBid{
MaxQuantity: 15,
MaxPrice: 5,
OwnerId: 1,
Signature: []byte("signature"),
},
}}},
responseCode: orderbook.StatusOK,
},
{
name: "test msg register pair",
msg: &orderbook.Msg{Sum: &orderbook.Msg_MsgRegisterPair{MsgRegisterPair: &orderbook.MsgRegisterPair{
Pair: testPair,
}}},
},
responseCode: orderbook.StatusOK,
expOrderSize: 1,
},
// {
// name: "test msg bid",
// msg: &orderbook.Msg{Sum: &orderbook.Msg_MsgBid{MsgBid: &orderbook.MsgBid{
// Pair: testPair,
// BidOrder: &orderbook.OrderBid{
// MaxQuantity: 15,
// MaxPrice: 5,
// OwnerId: 1,
// Signature: []byte("signature"),
// },
// }}},
// responseCode: orderbook.StatusOK,
// },
// {
// name: "test msg register pair",
// msg: &orderbook.Msg{Sum: &orderbook.Msg_MsgRegisterPair{MsgRegisterPair: &orderbook.MsgRegisterPair{
// Pair: testPair,
// }}},
// responseCode: orderbook.StatusOK,
// },
}
for _, tc := range testCases {
@@ -66,7 +76,9 @@ func TestCheckTx(t *testing.T) {
bz, err := proto.Marshal(tc.msg)
require.NoError(t, err)
resp := app.CheckTx(types.RequestCheckTx{Tx: bz})
require.Equal(t, tc.responseCode, resp.Code)
require.Equal(t, tc.responseCode, resp.Code, resp.Log)
bids, asks := app.Orders(testPair)
require.Equal(t, tc.expOrderSize, len(bids) + len(asks))
})
}
}

View File

@@ -0,0 +1,9 @@
version: v1
plugins:
- name: gogofaster
out: .
opt:
- Mgoogle/protobuf/timestamp.proto=github.com/cosmos/gogoproto/types
- Mgoogle/protobuf/duration.proto=github.com/golang/protobuf/ptypes/duration
- plugins=grpc
- paths=source_relative

View File

@@ -1,5 +1,5 @@
//go:generate go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
//go:generate protoc -I. -I../.. --go_out=. --go_opt=paths=source_relative wire.proto msgs.proto
//go:generate go install github.com/bufbuild/buf/cmd/buf
//go:generate buf generate
// The orderbook presents a more advanced example of a Tendermint application than the simple kvstore
//

File diff suppressed because it is too large Load Diff

View File

@@ -1,11 +0,0 @@
package orderbook
import dbm "github.com/tendermint/tm-db"
type AccountStore struct {
db dbm.DB
}
// iterate over the account database
// Add to the account database
// find an account

File diff suppressed because it is too large Load Diff

View File

@@ -27,6 +27,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package
// PublicKey defines the keys available for use with Tendermint Validators
type PublicKey struct {
// Types that are valid to be assigned to Sum:
//
// *PublicKey_Ed25519
// *PublicKey_Secp256K1
Sum isPublicKey_Sum `protobuf_oneof:"sum"`

View File

@@ -68,6 +68,7 @@ func (m *Txs) GetTxs() [][]byte {
type Message struct {
// Types that are valid to be assigned to Sum:
//
// *Message_Txs
Sum isMessage_Sum `protobuf_oneof:"sum"`
}

View File

@@ -158,6 +158,7 @@ func (m *PacketMsg) GetData() []byte {
type Packet struct {
// Types that are valid to be assigned to Sum:
//
// *Packet_PacketPing
// *Packet_PacketPong
// *Packet_PacketMsg

View File

@@ -24,6 +24,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package
type Message struct {
// Types that are valid to be assigned to Sum:
//
// *Message_SnapshotsRequest
// *Message_SnapshotsResponse
// *Message_ChunkRequest