Skip to content

Commit

Permalink
fix hoarder (#97)
Browse files Browse the repository at this point in the history
* hoarder working with dreamland

* handle retry if err

* better naming convention, clean up some methods

* upgrade interfaces
  • Loading branch information
tafseer-khan authored Sep 18, 2023
1 parent 9280c85 commit c27d989
Show file tree
Hide file tree
Showing 39 changed files with 604 additions and 488 deletions.
2 changes: 1 addition & 1 deletion clients/p2p/hoarder/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package p2p
package hoarder

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion clients/p2p/hoarder/dreamland.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package p2p
package hoarder

import (
"github.com/taubyte/go-interfaces/common"
Expand Down
2 changes: 1 addition & 1 deletion clients/p2p/hoarder/list.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package p2p
package hoarder

import (
"fmt"
Expand Down
3 changes: 2 additions & 1 deletion clients/p2p/hoarder/rare.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package p2p
package hoarder

import (
"fmt"
Expand All @@ -7,6 +7,7 @@ import (
"github.com/taubyte/utils/maps"
)

// TODO: Add cids to dht
func (c *Client) Rare() ([]string, error) {
// looks for items that only have one copy in the network
resp, err := c.client.Send("hoarder", command.Body{"action": "rare"})
Expand Down
2 changes: 1 addition & 1 deletion clients/p2p/hoarder/stash.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package p2p
package hoarder

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion clients/p2p/hoarder/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package p2p
package hoarder

import (
iface "github.com/taubyte/go-interfaces/services/hoarder"
Expand Down
2 changes: 1 addition & 1 deletion clients/p2p/hoarder/vars.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package p2p
package hoarder

import (
"github.com/ipfs/go-log/v2"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require (
github.com/taubyte/cli-common v0.1.1
github.com/taubyte/config-compiler v0.4.6
github.com/taubyte/domain-validation v1.0.1
github.com/taubyte/go-interfaces v0.2.14-0.20230901215338-f4991db4f44d
github.com/taubyte/go-interfaces v0.2.14-0.20230918143025-f035534c6a59
github.com/taubyte/go-project-schema v0.9.3
github.com/taubyte/go-sdk v0.3.9
github.com/taubyte/go-sdk-smartops v0.1.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -885,8 +885,8 @@ github.com/taubyte/config-compiler v0.4.6 h1:9s3xn955imE7gAyVFpcsBEkowhCir78b1HP
github.com/taubyte/config-compiler v0.4.6/go.mod h1:gaohk1BSknIudo16QSgeQqVWVGLyusPPdLn+wQ+xAmY=
github.com/taubyte/domain-validation v1.0.1 h1:T1iRls4p5+uJLR8R/wf+dBt9Rlahg9BcCtBVbkoD0Ik=
github.com/taubyte/domain-validation v1.0.1/go.mod h1:/X3yd7sBjnE323rA8I9PiUt5+NlKU4I02nQik25Vqe8=
github.com/taubyte/go-interfaces v0.2.14-0.20230901215338-f4991db4f44d h1:RhXP+VfYp1qyGWyIVBT9pmx8kiZnBi+MsNsMKmyW3Jw=
github.com/taubyte/go-interfaces v0.2.14-0.20230901215338-f4991db4f44d/go.mod h1:4DHAcs77uhLXLGWnoRca6kK/vUw0BPHGr0iXcwnBTaE=
github.com/taubyte/go-interfaces v0.2.14-0.20230918143025-f035534c6a59 h1:rIsZHo4Dnx9mayawHUyjioH8UR/2Kdy0wusDN4Bc2Us=
github.com/taubyte/go-interfaces v0.2.14-0.20230918143025-f035534c6a59/go.mod h1:4DHAcs77uhLXLGWnoRca6kK/vUw0BPHGr0iXcwnBTaE=
github.com/taubyte/go-project-schema v0.9.3 h1:2H0ClUZq7f97OgtL0FUe9tv2v12wOmnTAIiJGLei/gU=
github.com/taubyte/go-project-schema v0.9.3/go.mod h1:8Rt5zsVfj8qbYCT+7++oax/nFVKvVfAepzVkqXrNTs8=
github.com/taubyte/go-sdk v0.3.9 h1:mwwjiub/Jc987kfWvVfAcx63fMRAYB9cj4yhJq+CSyo=
Expand Down
1 change: 1 addition & 0 deletions pkgs/kvdb/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (f *factory) deleteDB(path string) {
delete(f.dbs, path)
}

// TODO: This should be Time.Duration
func (f *factory) New(logger logging.StandardLogger, path string, rebroadcastIntervalSec int) (kvdb.KVDB, error) {
cachedDB := f.getDB(path)
if cachedDB != nil {
Expand Down
114 changes: 0 additions & 114 deletions protocols/hoarder/api_hoarders.go

This file was deleted.

100 changes: 100 additions & 0 deletions protocols/hoarder/auction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package hoarder

import (
"context"
"crypto/rand"
"errors"
"fmt"
"time"
"unsafe"

pubsub "github.com/libp2p/go-libp2p-pubsub"
hoarderIface "github.com/taubyte/go-interfaces/services/hoarder"
)

func (srv *Service) auctionNew(ctx context.Context, auction *hoarderIface.Auction, msg *pubsub.Message) error {
srv.startAuction(ctx, auction)

// Check if we have that actionId stored with the action
if found := srv.checkValidAction(auction.Meta.Match, hoarderIface.AuctionNew, msg.ReceivedFrom.Pretty()); !found {
// Generate Lottery number and publish our intent to join the lottery
auction.Lottery.HoarderId = srv.node.ID().Pretty()

arr := make([]byte, 8)
if _, err := rand.Read(arr); err != nil {
return fmt.Errorf("auctionNew rand read failed with: %s", err)
}

num := *(*uint64)(unsafe.Pointer(&arr[0]))
auction.Lottery.Number = num

if err := srv.publishAction(ctx, auction, hoarderIface.AuctionIntent); err != nil {
return err
}
}

// Store the new action and register our entry inside the auction pool
srv.saveAction(auction)
return nil
}

func (srv *Service) startAuction(ctx context.Context, action *hoarderIface.Auction) {
// Start a countdown for the new action
go func() {
select {
case <-ctx.Done():
return

case <-time.After(maxWaitTime):
if err := srv.publishAction(ctx, action, hoarderIface.AuctionEnd); err != nil {
logger.Error("action publish failed with:", err.Error())
}
}
}()
}

func (srv *Service) auctionEnd(ctx context.Context, auction *hoarderIface.Auction, msg *pubsub.Message) error {
var winner *hoarderIface.Auction
var currentBiggest uint64
for _, lottery := range srv.lotteryPool[auction.Meta.ConfigId+auction.Meta.Match] {
if lottery.Lottery.Number > currentBiggest {
winner = lottery
currentBiggest = lottery.Lottery.Number
}
}

if winner == nil {
return errors.New("no winner was selected")
}

// Self evaluate to check if we won or not
if winner.Lottery.HoarderId == srv.node.Peer().ID().Pretty() {
err := srv.storeAuction(ctx, auction)
if err != nil {
return err
}
}

// Do I need to do this, probably not but just in case
srv.lotteryPool[auction.Meta.ConfigId+auction.Meta.Match] = nil
return nil
}

func (srv *Service) auctionIntent(auction *hoarderIface.Auction, msg *pubsub.Message) error {
// If we see that the node already reported intent to stash on action Id we ignore it
if found := srv.checkValidAction(auction.Meta.Match, hoarderIface.AuctionIntent, msg.ReceivedFrom.Pretty()); found {
return fmt.Errorf("%s already reported an intent", msg.ReceivedFrom.Pretty())
}

// Generate lottery pool
srv.regLock.Lock()
defer srv.regLock.Unlock()
pool, ok := srv.lotteryPool[auction.Meta.ConfigId+auction.Meta.Match]
if !ok {
return fmt.Errorf("did not find lottery pool for %s", auction.Meta.ConfigId+auction.Meta.Match)
}

pool = append(pool, auction)
srv.lotteryPool[auction.Meta.ConfigId+auction.Meta.Match] = pool
return nil
}
36 changes: 0 additions & 36 deletions protocols/hoarder/auction_end.go

This file was deleted.

27 changes: 0 additions & 27 deletions protocols/hoarder/auction_intent.go

This file was deleted.

Loading

0 comments on commit c27d989

Please sign in to comment.