link up bid txs and memdb storage + key fixes

lfg it works
master
michael 2022-08-30 23:29:04 +00:00
parent b5c7075bd0
commit d6b4b51ca6
3 changed files with 30 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"math/big" "math/big"
"reflect"
"cosmos-test/x/cosmostest/memdb" "cosmos-test/x/cosmostest/memdb"
"cosmos-test/x/cosmostest/types" "cosmos-test/x/cosmostest/types"
@ -30,9 +31,12 @@ func (k msgServer) NewBid(goCtx context.Context, msg *types.MsgNewBid) (*types.M
} }
highestBid, err := memdb.BidDB.GetHighestBid(msg.AuctionIndex) highestBid, err := memdb.BidDB.GetHighestBid(msg.AuctionIndex)
// we manually handle KeyNotFound in GetHighestBid, so should return (nil, nil) if not found
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get highest bid: %s", err) return nil, fmt.Errorf("failed to get highest bid: %s", reflect.TypeOf(err))
} }
if highestBid != nil {
amtPrev := new(big.Int) amtPrev := new(big.Int)
amtPrev, ok = amtPrev.SetString(highestBid.Amount, 10) amtPrev, ok = amtPrev.SetString(highestBid.Amount, 10)
if !ok { // this should have been checked before, but whatever if !ok { // this should have been checked before, but whatever
@ -42,6 +46,7 @@ func (k msgServer) NewBid(goCtx context.Context, msg *types.MsgNewBid) (*types.M
if amt.Cmp(amtPrev) < 1 { if amt.Cmp(amtPrev) < 1 {
return nil, fmt.Errorf("bid amount must be greater than largest bid") return nil, fmt.Errorf("bid amount must be greater than largest bid")
} }
}
bid := &types.Bid{ bid := &types.Bid{
Amount: msg.Amount, Amount: msg.Amount,

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"cosmos-test/x/cosmostest/types" "cosmos-test/x/cosmostest/types"
"encoding/gob" "encoding/gob"
"errors"
"log" "log"
"math/big" "math/big"
@ -36,7 +37,7 @@ func (b *bidDB) AddBid(auctionId string, bid *types.Bid) error {
err := b.db.Update(func(txn *badger.Txn) error { err := b.db.Update(func(txn *badger.Txn) error {
var bids []*types.Bid var bids []*types.Bid
bidsOld, err := txn.Get(k) bidsOld, err := txn.Get(k)
if err == badger.ErrKeyNotFound { if errors.Is(err, badger.ErrKeyNotFound) {
// key not found -> just create a new Bid array // key not found -> just create a new Bid array
bids = []*types.Bid{} bids = []*types.Bid{}
} else { } else {
@ -81,7 +82,11 @@ func (b *bidDB) GetHighestBid(auctionId string) (*types.Bid, error) {
err := b.db.View(func(txn *badger.Txn) error { err := b.db.View(func(txn *badger.Txn) error {
bidData, err := txn.Get(k) bidData, err := txn.Get(k)
if err != nil { if err != nil {
if !errors.Is(err, badger.ErrKeyNotFound) {
return err return err
} else {
return nil
}
} }
err = bidData.Value(func(val []byte) error { err = bidData.Value(func(val []byte) error {
@ -91,6 +96,10 @@ func (b *bidDB) GetHighestBid(auctionId string) (*types.Bid, error) {
return err return err
} }
if bids == nil {
return nil
}
amt := big.NewInt(0) amt := big.NewInt(0)
rAmt := big.NewInt(0) rAmt := big.NewInt(0)
for _, rBid := range bids { for _, rBid := range bids {

View File

@ -13,7 +13,9 @@ import (
"cosmos-test/x/cosmostest/client/cli" "cosmos-test/x/cosmostest/client/cli"
"cosmos-test/x/cosmostest/keeper" "cosmos-test/x/cosmostest/keeper"
"cosmos-test/x/cosmostest/memdb"
"cosmos-test/x/cosmostest/types" "cosmos-test/x/cosmostest/types"
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types" cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
@ -109,6 +111,10 @@ func NewAppModule(
accountKeeper types.AccountKeeper, accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper, bankKeeper types.BankKeeper,
) AppModule { ) AppModule {
// initialize in-memory database
memdb.BidDB.Mount()
return AppModule{ return AppModule{
AppModuleBasic: NewAppModuleBasic(cdc), AppModuleBasic: NewAppModuleBasic(cdc),
keeper: keeper, keeper: keeper,