From cdd1cb8cc00dc48ead749451c705fed69093f5d5 Mon Sep 17 00:00:00 2001 From: turtlebasket Date: Mon, 29 Aug 2022 01:03:09 +0000 Subject: [PATCH] fix memdb encode/decode EOF issue --- x/cosmostest/memdb/biddb.go | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/x/cosmostest/memdb/biddb.go b/x/cosmostest/memdb/biddb.go index ba482b7..65a65be 100644 --- a/x/cosmostest/memdb/biddb.go +++ b/x/cosmostest/memdb/biddb.go @@ -31,9 +31,6 @@ func (b *bidDB) Mount() { // Add a bid to the bid list under specified auction key. func (b *bidDB) AddBid(auctionId string, bid *types.Bid) error { - buf := bytes.NewBuffer([]byte{}) - enc := gob.NewEncoder(buf) - dec := gob.NewDecoder(buf) k := []byte(auctionId) err := b.db.Update(func(txn *badger.Txn) error { @@ -48,10 +45,7 @@ func (b *bidDB) AddBid(auctionId string, bid *types.Bid) error { } // key found -> decode contents to bids bidsOld.Value(func(val []byte) error { - buf.Reset() - if _, err := buf.Read(val); err != nil { - return err - } + dec := gob.NewDecoder(bytes.NewReader(val)) if err := dec.Decode(&bids); err != nil { return err } @@ -62,7 +56,8 @@ func (b *bidDB) AddBid(auctionId string, bid *types.Bid) error { // append bid bids = append(bids, bid) // encode new list - buf.Reset() + buf := bytes.NewBuffer(nil) + enc := gob.NewEncoder(buf) if err := enc.Encode(&bids); err != nil { return err } @@ -80,8 +75,6 @@ func (b *bidDB) AddBid(auctionId string, bid *types.Bid) error { // Get the highest bid in the list under specified auction key. func (b *bidDB) GetHighestBid(auctionId string) (*types.Bid, error) { k := []byte(auctionId) - buf := bytes.NewBuffer([]byte{}) - dec := gob.NewDecoder(buf) var bid *types.Bid @@ -92,13 +85,8 @@ func (b *bidDB) GetHighestBid(auctionId string) (*types.Bid, error) { } err = bidData.Value(func(val []byte) error { - buf.Reset() - if _, err := buf.Read(val); err != nil { - return err - } - // panic(len(val)) - var bids []*types.Bid + dec := gob.NewDecoder(bytes.NewReader(val)) if err := dec.Decode(&bids); err != nil { return err }