forked from dominant-strategies/go-quai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add foundational files to support gossipsub
- Loading branch information
1 parent
5f58738
commit 6716a7a
Showing
7 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package gossipsub | ||
|
||
import ( | ||
"time" | ||
|
||
lru "github.com/hashicorp/golang-lru" | ||
) | ||
|
||
const cacheSize = 1000 | ||
|
||
// Gossipsub message cache. | ||
// Mostly used for duplicate messages and handling of recently seen messages. | ||
var msgCache *lru.Cache | ||
|
||
func init() { | ||
msgCache, _ = lru.NewWithEvict(cacheSize, onEvicted) | ||
} | ||
|
||
func AddMessageToCache(msgID string, msgData interface{}) { | ||
msgCache.Add(msgID, msgData) | ||
} | ||
|
||
func GetMessageFromCache(msgID string) (interface{}, bool) { | ||
return msgCache.Get(msgID) | ||
} | ||
|
||
func onEvicted(key interface{}, value interface{}) { | ||
// TODO: Handle any logic needed when a message is evicted from cache | ||
} | ||
|
||
func CleanupCache(tickDuration time.Duration) { | ||
ticker := time.NewTicker(tickDuration) | ||
defer ticker.Stop() | ||
|
||
for range ticker.C { | ||
// TODO: Add cleanup logic | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package gossipsub | ||
|
||
import ( | ||
gossip "github.com/libp2p/go-libp2p-pubsub" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
) | ||
|
||
// Handle incoming Gossipsub message | ||
func HandleIncomingMessage(msg *gossip.Message) { | ||
// TODO: implement | ||
} | ||
|
||
// Handle logic when a peer joins the network | ||
func HandlePeerJoin(peerID peer.ID) { | ||
// TODO: implement | ||
} | ||
|
||
// Handle logic when a peer leaves the network | ||
func HandlePeerLeave(peerID peer.ID) { | ||
// TODO: implement | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package gossipsub | ||
|
||
import ( | ||
"context" | ||
|
||
gossip "github.com/libp2p/go-libp2p-pubsub" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
) | ||
|
||
// Register a validator for a topic. | ||
// Validators help in ensuring that incoming Gossipsub messages meet certain criteria | ||
// before they are processed or propagated. | ||
func RegisterValidators(ps *gossip.PubSub) { | ||
// TODO: implement | ||
ps.RegisterTopicValidator("example-topic", exampleTopicValidator) | ||
} | ||
|
||
func exampleTopicValidator(ctx context.Context, peerID peer.ID, msg *gossip.Message) bool { | ||
// TODO: implement | ||
// Validate the message for the "example-topic" | ||
// Return true if valid, false otherwise | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package pubsub | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/dominant-strategies/go-quai/log" | ||
gossip "github.com/libp2p/go-libp2p-pubsub" | ||
"github.com/libp2p/go-libp2p/core/host" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
) | ||
|
||
// Base instance for the pub-sub service. | ||
// It manages the Gossipsub instance and provides utility methods | ||
type PubSubManager struct { | ||
ps *gossip.PubSub | ||
} | ||
|
||
func NewPubSubManager(ctx context.Context, h host.Host, opts []gossip.Option) (*PubSubManager, error) { | ||
ps, err := gossip.NewGossipSub(ctx, h, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &PubSubManager{ps: ps}, nil | ||
} | ||
|
||
// Join a topic | ||
func (manager *PubSubManager) Join(topic string) (*gossip.Topic, error) { | ||
return manager.ps.Join(topic) | ||
} | ||
|
||
// Join a topic and subscribe to it | ||
func (manager *PubSubManager) Subscribe(topic string) (*gossip.Subscription, error) { | ||
topicHandle, err := manager.ps.Join(topic) | ||
if err != nil { | ||
log.Errorf("error joining topic: %s", err) | ||
return nil, err | ||
} | ||
return topicHandle.Subscribe() | ||
} | ||
|
||
// Publish a message to a topic | ||
func (manager *PubSubManager) Publish(topic string, data []byte) error { | ||
topicHandle, err := manager.ps.Join(topic) | ||
if err != nil { | ||
log.Errorf("error joining topic: %s", err) | ||
return err | ||
} | ||
return topicHandle.Publish(context.Background(), data) | ||
} | ||
|
||
func (manager *PubSubManager) ListPeers(topic string) []peer.ID { | ||
return manager.ps.ListPeers(topic) | ||
} |