From 84bb357bc8714ec30edc0121e949fa8a95f9e38b Mon Sep 17 00:00:00 2001 From: turtlebasket Date: Tue, 13 Sep 2022 21:56:38 +0000 Subject: [PATCH] verified providers in memdb + tests --- x/colinearcore/memdb/biddb.go | 101 ++++++++++++++++++++++++----- x/colinearcore/memdb/biddb_test.go | 55 +++++++++++++--- 2 files changed, 132 insertions(+), 24 deletions(-) diff --git a/x/colinearcore/memdb/biddb.go b/x/colinearcore/memdb/biddb.go index e4af205..fe60507 100644 --- a/x/colinearcore/memdb/biddb.go +++ b/x/colinearcore/memdb/biddb.go @@ -11,14 +11,14 @@ import ( badger "github.com/dgraph-io/badger/v3" ) -type bidDB struct { +type auctionDB struct { db *badger.DB } -var BidDB bidDB +var AuctionDB auctionDB // Mount Db & initialize encoder/decoder -func (b *bidDB) Mount() { +func (b *auctionDB) Mount() { db, err := badger.Open(badger.DefaultOptions("").WithInMemory(true)) if err != nil { // must force crash here, since db is absolutely required @@ -30,9 +30,13 @@ func (b *bidDB) Mount() { gob.Register([]types.Bid{}) } +// ----------------- +// * BID FUNCTIONS * +// ----------------- + // Add a bid to the bid list under specified auction key. -func (b *bidDB) AddBid(auctionId string, bid *types.Bid) error { - k := []byte(auctionId) +func (b *auctionDB) AddBid(auctionId string, bid *types.Bid) error { + k := []byte(auctionId + "_bids") err := b.db.Update(func(txn *badger.Txn) error { var bids []*types.Bid @@ -74,8 +78,8 @@ func (b *bidDB) AddBid(auctionId string, bid *types.Bid) error { } // Get the highest bid in the list under specified auction key. -func (b *bidDB) GetLowestBid(auctionId string) (*types.Bid, error) { - k := []byte(auctionId) +func (b *auctionDB) GetLowestBid(auctionId string) (*types.Bid, error) { + k := []byte(auctionId + "_bids") var bid *types.Bid @@ -124,8 +128,8 @@ func (b *bidDB) GetLowestBid(auctionId string) (*types.Bid, error) { return bid, err } -func (b *bidDB) GetBids(auctionId string) ([]*types.Bid, error) { - k := []byte(auctionId) +func (b *auctionDB) GetBids(auctionId string) ([]*types.Bid, error) { + k := []byte(auctionId + "_bids") var bids []*types.Bid err := b.db.View(func(txn *badger.Txn) error { @@ -145,8 +149,8 @@ func (b *bidDB) GetBids(auctionId string) ([]*types.Bid, error) { return bids, err } -func (b *bidDB) ClearAuction(auctionId string) error { - k := []byte(auctionId) +func (b *auctionDB) ClearAuctionBids(auctionId string) error { + k := []byte(auctionId + "_bids") err := b.db.Update(func(txn *badger.Txn) error { return txn.Delete(k) @@ -155,8 +159,8 @@ func (b *bidDB) ClearAuction(auctionId string) error { return err } -// Iterate over all auction keys in memory. VIEW-ONLY. -func (b *bidDB) ForEachAuction(viewFunc func(string) error) error { +// Iterate over all auction bid-list keys in memory and retrieve bids from each. VIEW-ONLY. +func (b *auctionDB) ForEachAuctionBidList(viewFunc func(string) error) error { err := b.db.View(func(txn *badger.Txn) error { opts := badger.DefaultIteratorOptions // can customize options down here if we want @@ -165,12 +169,77 @@ func (b *bidDB) ForEachAuction(viewFunc func(string) error) error { for iter.Rewind(); iter.Valid(); iter.Next() { item := iter.Item() key := string(item.Key()) - err := viewFunc(key) - if err != nil { - return err + // require that this be an auction bids key + if len(key) > 5 && key[len(key)-5:len(key)-1] == "_bids" { + err := viewFunc(key) + if err != nil { + return err + } } } return nil }) return err } + +// ------------------------------- +// * VERIFIED PROVIDER FUNCTIONS * +// ------------------------------- + +func (b *auctionDB) SetVerifiedProviders(auctionId string, providers []string) error { + k := []byte(auctionId + "_vp") + + if len(providers) == 0 { + return errors.New("must include at least one provider") + } + + buf := bytes.NewBuffer(nil) + enc := gob.NewEncoder(buf) + if err := enc.Encode(&providers); err != nil { + return err + } + + err := b.db.Update(func(txn *badger.Txn) error { + return txn.Set(k, buf.Bytes()) + }) + + return err +} + +func (b *auctionDB) GetVerifiedProviders(auctionId string) ([]string, error) { + k := []byte(auctionId + "_vp") + var providers []string + err := b.db.View(func(txn *badger.Txn) error { + + res, err := txn.Get(k) + if err != nil { + return err + } + + err = res.Value(func(val []byte) error { + dec := gob.NewDecoder(bytes.NewReader(val)) + err := dec.Decode(&providers) + return err + }) + + return err + }) + if err != nil { + return nil, err + } + if providers == nil { + return nil, errors.New("nil providers value") + } + return providers, nil +} + +func (b *auctionDB) ClearVerifiedProviders(auctionId string) error { + k := []byte(auctionId + "_vp") + err := b.db.Update(func(txn *badger.Txn) error { + if _, err := txn.Get(k); err != nil && errors.Is(err, badger.ErrKeyNotFound) { + return err + } + return txn.Delete(k) + }) + return err +} diff --git a/x/colinearcore/memdb/biddb_test.go b/x/colinearcore/memdb/biddb_test.go index 7cc7f33..1187c5f 100644 --- a/x/colinearcore/memdb/biddb_test.go +++ b/x/colinearcore/memdb/biddb_test.go @@ -8,22 +8,22 @@ import ( "github.com/dgraph-io/badger/v3" ) -func TestBasicFlow(t *testing.T) { +func TestBidFlow(t *testing.T) { // this should panic if there's a problem - BidDB.Mount() + AuctionDB.Mount() // add a bid - if err := BidDB.AddBid("0", &types.Bid{Owner: "cosmos123", Amount: "100"}); err != nil { + if err := AuctionDB.AddBid("0", &types.Bid{Owner: "cosmos123", Amount: "100"}); err != nil { panic(err) } // add a higher bid (db funcs won't check amount, though) - if err := BidDB.AddBid("0", &types.Bid{Owner: "cosmos456", Amount: "200"}); err != nil { + if err := AuctionDB.AddBid("0", &types.Bid{Owner: "cosmos456", Amount: "200"}); err != nil { panic(err) } // get all bids out - if bids, err := BidDB.GetBids("0"); err != nil { + if bids, err := AuctionDB.GetBids("0"); err != nil { panic(err) } else { if bids == nil { @@ -35,7 +35,7 @@ func TestBasicFlow(t *testing.T) { } // check lowest bid - if bid, err := BidDB.GetLowestBid("0"); err != nil { + if bid, err := AuctionDB.GetLowestBid("0"); err != nil { panic(err) } else { if bid.Owner != "cosmos123" || bid.Amount != "100" { @@ -44,11 +44,11 @@ func TestBasicFlow(t *testing.T) { } // clear auction under id 0 - if err := BidDB.ClearAuction("0"); err != nil { + if err := AuctionDB.ClearAuctionBids("0"); err != nil { panic(err) } else { // expect auction key to be unknown - if err := BidDB.db.View(func(txn *badger.Txn) error { + if err := AuctionDB.db.View(func(txn *badger.Txn) error { _, err := txn.Get([]byte("0")) return err }); err != nil { @@ -58,3 +58,42 @@ func TestBasicFlow(t *testing.T) { } } } + +func TestVerifiedFlow(t *testing.T) { + // this should panic if there's a problem + AuctionDB.Mount() + + providers := []string{ + "colinear1...", + "colinear2...", + "colinear3...", + "colinear4...", + } + + if err := AuctionDB.SetVerifiedProviders("0", providers); err != nil { + panic(err) + } + + if prov, err := AuctionDB.GetVerifiedProviders("0"); err != nil { + panic(err) + } else { + if len(prov) != 4 { + panic(fmt.Sprintf("providers length should be 4, got %d instead", len(prov))) + } + if prov[0] != "colinear1..." { + panic(fmt.Sprintf("verified provider 1 should be `colinear1...`, instead got `%s`", prov[0])) + } + } + + if err := AuctionDB.ClearVerifiedProviders("0"); err != nil { + panic(err) + } + + if _, err := AuctionDB.GetVerifiedProviders("0"); err == nil { + panic("Should not return a provider") + } + + // } else { // just a test - should exit with "key not found" + // panic(err) + // } +}