First Starting debug

This commit is contained in:
mr
2026-02-02 09:05:58 +01:00
parent 562d86125e
commit c3352499fa
13 changed files with 74 additions and 29 deletions

View File

@@ -27,7 +27,7 @@ type Event struct {
}
func NewEvent(name string, from string, dt *tools.DataType, user string, payload []byte) *Event {
priv, err := LoadKeyFromFile(false) // your node private key
priv, err := LoadKeyFromFilePrivate() // your node private key
if err != nil {
return nil
}

View File

@@ -246,7 +246,7 @@ func ConnectToIndexers(h host.Host, minIndexer int, maxIndexer int, myPID pp.ID)
}
}
if len(StaticIndexers) == 0 {
panic("can't run a node with no indexers")
logger.Err(errors.New("you run a node without indexers... your gonna be isolated."))
}
if len(StaticIndexers) < minIndexer {

View File

@@ -2,7 +2,10 @@ package common
import (
"bytes"
"crypto/ed25519"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"oc-discovery/conf"
@@ -44,36 +47,47 @@ func Verify(pub crypto.PubKey, data, sig []byte) (bool, error) {
return pub.Verify(data, sig)
}
func LoadKeyFromFile(isPublic bool) (crypto.PrivKey, error) {
func LoadKeyFromFilePrivate() (crypto.PrivKey, error) {
path := conf.GetConfig().PrivateKeyPath
if isPublic {
path = conf.GetConfig().PublicKeyPath
}
fmt.Println("extract " + path)
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
// Try to unmarshal as libp2p private key (supports ed25519, rsa, etc.)
priv, err := crypto.UnmarshalPrivateKey(data)
fmt.Println(data)
block, _ := pem.Decode(data)
fmt.Println(block.Bytes)
keyAny, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return priv, nil
edKey, ok := keyAny.(ed25519.PrivateKey)
if !ok {
return nil, fmt.Errorf("not an ed25519 key")
}
return crypto.UnmarshalEd25519PrivateKey(edKey)
}
func VerifyPubWithPriv() bool {
priv, err := LoadKeyFromFile(false)
func LoadKeyFromFilePublic() (crypto.PubKey, error) {
path := conf.GetConfig().PublicKeyPath
fmt.Println("extract " + path)
data, err := os.ReadFile(path)
if err != nil {
fmt.Println(err)
return false
return nil, err
}
pub, err := LoadKeyFromFile(true)
block, _ := pem.Decode(data)
keyAny, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
fmt.Println(err)
return false
return nil, err
}
return priv.GetPublic().Equals(pub)
edKey, ok := keyAny.(ed25519.PublicKey)
if !ok {
return nil, fmt.Errorf("not an ed25519 key")
}
// Try to unmarshal as libp2p private key (supports ed25519, rsa, etc.)
return crypto.UnmarshalEd25519PublicKey(edKey)
}
func LoadPSKFromFile() (pnet.PSK, error) {
@@ -82,11 +96,12 @@ func LoadPSKFromFile() (pnet.PSK, error) {
if err != nil {
return nil, err
}
// Try to unmarshal as libp2p private key (supports ed25519, rsa, etc.)
fmt.Println("sqqdsqsqd")
psk, err := pnet.DecodeV1PSK(bytes.NewReader(data))
if err != nil {
return nil, err
}
fmt.Println("PSK found.")
return psk, nil
}

View File

@@ -29,7 +29,7 @@ type PeerRecord struct {
}
func (p *PeerRecord) Sign() error {
priv, err := common.LoadKeyFromFile(false)
priv, err := common.LoadKeyFromFilePrivate()
if err != nil {
return err
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"oc-discovery/daemons/node/common"
oclib "cloud.o-forge.io/core/oc-lib"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/host"
)
@@ -17,6 +18,8 @@ type IndexerService struct {
// if a pubsub is given... indexer is also an active oc-node. If not... your a strict indexer
func NewIndexerService(h host.Host, ps *pubsub.PubSub, maxNode int) *IndexerService {
logger := oclib.GetLogger()
logger.Info().Msg("open indexer mode...")
var err error
ix := &IndexerService{
LongLivedStreamRecordedService: common.NewStreamRecordedService[PeerRecord](h, maxNode, false),
@@ -31,8 +34,11 @@ func NewIndexerService(h host.Host, ps *pubsub.PubSub, maxNode int) *IndexerServ
ix.PS = ps
// later TODO : all indexer laucnh a private replica of them self. DEV OPS
if ix.isStrictIndexer {
logger.Info().Msg("connect to indexers as strict indexer...")
common.ConnectToIndexers(h, 0, 5, ix.Host.ID()) // TODO : make var to change how many indexers are allowed.
ix.SubscribeToNodeActivity(ix.PS) // now we subscribe to a long run topic named node-activity, to relay message.
logger.Info().Msg("subscribe to node activity as strict indexer...")
ix.SubscribeToNodeActivity(ix.PS) // now we subscribe to a long run topic named node-activity, to relay message.
logger.Info().Msg("subscribe to decentralized search flow as strict indexer...")
ix.SubscribeToSearch(ix.PS)
}
ix.initNodeHandler() // then listen up on every protocol expected

View File

@@ -27,6 +27,7 @@ type Node struct {
PubSubService *pubsub.PubSubService
StreamService *stream.StreamService
PeerID pp.ID
isIndexer bool
}
func InitNode(isNode bool, isIndexer bool) (*Node, error) {
@@ -35,7 +36,7 @@ func InitNode(isNode bool, isIndexer bool) (*Node, error) {
}
logger := oclib.GetLogger()
logger.Info().Msg("retrieving private key...")
priv, err := common.LoadKeyFromFile(false) // your node private key
priv, err := common.LoadKeyFromFilePrivate() // your node private key
if err != nil {
return nil, err
}
@@ -57,6 +58,7 @@ func InitNode(isNode bool, isIndexer bool) (*Node, error) {
}
node := &Node{
PeerID: h.ID(),
isIndexer: isIndexer,
LongLivedStreamRecordedService: common.NewStreamRecordedService[interface{}](h, 1000, false),
}
var ps *pubsubs.PubSub
@@ -67,10 +69,15 @@ func InitNode(isNode bool, isIndexer bool) (*Node, error) {
panic(err) // can't run your node without a propalgation pubsub, of state of node.
}
node.PS = ps
logger.Info().Msg("connect to indexers...")
common.ConnectToIndexers(node.Host, 0, 5, node.PeerID) // TODO : make var to change how many indexers are allowed.
logger.Info().Msg("claims my node...")
node.claimInfo(conf.GetConfig().Name, conf.GetConfig().Hostname)
logger.Info().Msg("subscribe to node activity...")
node.SubscribeToNodeActivity(node.PS) // now we subscribe to a long run topic named node-activity, to relay message.
logger.Info().Msg("subscribe to decentralized search flow...")
node.SubscribeToSearch(node.PS)
logger.Info().Msg("run garbage collector...")
node.StartGC(30 * time.Second)
if node.StreamService, err = stream.InitStream(context.Background(), node.Host, node.PeerID, 1000, node); err != nil {
@@ -85,11 +92,14 @@ func InitNode(isNode bool, isIndexer bool) (*Node, error) {
logger.Info().Msg("generate opencloud indexer...")
node.IndexerService = indexer.NewIndexerService(node.Host, ps, 5)
}
logger.Info().Msg("Node is actually running.")
return node, nil
}
func (d *Node) Close() {
d.IndexerService.Close()
if d.isIndexer {
d.IndexerService.Close()
}
d.PubSubService.Close()
d.StreamService.Close()
d.Host.Close()
@@ -98,7 +108,7 @@ func (d *Node) Close() {
func (d *Node) publishPeerRecord(
rec *indexer.PeerRecord,
) error {
priv, err := common.LoadKeyFromFile(false) // your node private key
priv, err := common.LoadKeyFromFilePrivate() // your node private key
if err != nil {
return err
}
@@ -185,12 +195,12 @@ func (d *Node) claimInfo(
return nil, err
}
priv, err := common.LoadKeyFromFile(false)
priv, err := common.LoadKeyFromFilePrivate()
if err != nil {
return nil, err
}
pub, err := common.LoadKeyFromFile(true)
pub, err := common.LoadKeyFromFilePublic()
if err != nil {
return nil, err
}

View File

@@ -55,7 +55,7 @@ func (ps *PubSubService) publishEvent(
if err != nil {
return err
}
priv, err := common.LoadKeyFromFile(false)
priv, err := common.LoadKeyFromFilePrivate()
if err != nil {
return err
}

View File

@@ -7,6 +7,7 @@ import (
"strings"
"sync"
oclib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/tools"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/host"
@@ -28,7 +29,8 @@ func InitPubSub(ctx context.Context, h host.Host, ps *pubsub.PubSub, node common
StreamService: streamService,
PS: ps,
}
logger := oclib.GetLogger()
logger.Info().Msg("subscribe to events...")
service.initSubscribeEvents(ctx)
return service, nil
}

View File

@@ -43,6 +43,7 @@ type StreamService struct {
}
func InitStream(ctx context.Context, h host.Host, key pp.ID, maxNode int, node common.DiscoveryPeer) (*StreamService, error) {
logger := oclib.GetLogger()
service := &StreamService{
Key: key,
Node: node,
@@ -50,7 +51,9 @@ func InitStream(ctx context.Context, h host.Host, key pp.ID, maxNode int, node c
Streams: common.ProtocolStream{},
maxNodesConn: maxNode,
}
logger.Info().Msg("handle to partner heartbeat protocol...")
service.Host.SetStreamHandler(ProtocolHeartbeatPartner, service.HandlePartnerHeartbeat)
logger.Info().Msg("connect to partners...")
service.connectToPartners() // we set up a stream
go service.StartGC(30 * time.Second)
return service, nil