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

View File

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

View File

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