parent
b5c7075bd0
commit
d6b4b51ca6
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"reflect"
|
||||
|
||||
"cosmos-test/x/cosmostest/memdb"
|
||||
"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)
|
||||
// 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)
|
||||
return nil, fmt.Errorf("failed to get highest bid: %s", reflect.TypeOf(err))
|
||||
}
|
||||
|
||||
if highestBid != nil {
|
||||
amtPrev := new(big.Int)
|
||||
amtPrev, ok = amtPrev.SetString(highestBid.Amount, 10)
|
||||
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 {
|
||||
return nil, fmt.Errorf("bid amount must be greater than largest bid")
|
||||
}
|
||||
}
|
||||
|
||||
bid := &types.Bid{
|
||||
Amount: msg.Amount,
|
||||
|
|
|
@ -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 {
|
||||
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 {
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue