network

package
v0.0.0-...-9b6ef68 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2025 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFromCache

func GetFromCache(domain string) (string, bool)

Get retrieves a domain from the cache

func NodesCleanup

func NodesCleanup(nodes []*Node)

func ResolveDomain

func ResolveDomain(domain string, n *Node) (string, error)

ResolveDomain queries the BDNS blockchain for a domain

func SetToCache

func SetToCache(domain, ip string)

Set stores a domain in the cache with a default TTL of 300 seconds

func StartDNSServer

func StartDNSServer(port string, node *Node)

StartDNSServer initializes and starts a UDP-based DNS server

Types

type BDNSRequest

type BDNSRequest struct {
	DomainName string
}

type BDNSResponse

type BDNSResponse struct {
	Timestamp  int64
	DomainName string
	IP         string
	TTL        int64
	OwnerKey   []byte
	Signature  []byte
}

type DNSRecord

type DNSRecord struct {
	DomainName string     `json:"domain"`    // The domain being registered
	RecordType RecordType `json:"type"`      // The type of DNS record
	Value      string     `json:"value"`     // IP, alias, mail server, or other value
	TTL        int64      `json:"ttl"`       // Time-To-Live in seconds
	Priority   int        `json:"priority"`  // Used for MX/SRV records
	Port       int        `json:"port"`      // Used for SRV records
	OwnerKey   string     `json:"ownerKey"`  // Owner's public key
	Signature  string     `json:"signature"` // Signature to verify authenticity
	Timestamp  int64      `json:"timestamp"` // Record creation time
}

DNSRecord represents a BDNS record stored in the blockchain

type GossipMessage

type GossipMessage struct {
	Type    MessageType
	Sender  string // Peer ID
	Content json.RawMessage
	Metrics *metrics.DNSMetrics
}

GossipMessage structure

type Message

type Message struct {
	Sender string      `json:"sender"`
	Type   MessageType `json:"type"`
	Data   []byte      `json:"data"`
}

Message represents a generic network message

func DecodeMessage

func DecodeMessage(data []byte) (*Message, error)

Decode message from JSON

func (*Message) Encode

func (m *Message) Encode() []byte

Encode message to JSON

type MessageType

type MessageType string

MessageType defines various message types in the P2P network

const (
	DNSRequest       MessageType = "DNS_REQUEST"
	DNSResponse      MessageType = "DNS_RESPONSE"
	MsgTransaction   MessageType = "TRANSACTION"
	MsgBlock         MessageType = "BLOCK"
	MsgChainRequest  MessageType = "CHAIN_REQUEST"
	MsgChainResponse MessageType = "CHAIN_RESPONSE"
	MsgPeerRequest   MessageType = "PEER_REQUEST"
	MsgPeerResponse  MessageType = "PEER_RESPONSE"
	MsgRandomNumber  MessageType = "RANDOM_NUMBER"
	MsgInv           MessageType = "INV"
	MsgGetBlock      MessageType = "GET_BLOCK"
	MsgGetData       MessageType = "GET_DATA"
	MsgGetMerkle     MessageType = "GET_MERKLE"
)

type Node

type Node struct {
	Address         string
	Port            int
	Config          NodeConfig
	P2PNetwork      *P2PNetwork
	KeyPair         *blockchain.KeyPair
	RegistryKeys    [][]byte
	SlotLeaders     map[int64][]byte // epoch to slot leader
	SlotMutex       sync.Mutex
	TransactionPool map[int]*blockchain.Transaction
	TxMutex         sync.Mutex
	IndexManager    *index.IndexManager
	Blockchain      *blockchain.Blockchain
	BcMutex         sync.Mutex
	RandomNumber    []byte
	RandomMutex     sync.Mutex
	EpochRandoms    map[int64]map[string]consensus.SecretValues
	IsFullNode      bool // full vs light node
	PeerID          string
	KnownFullPeers  []string
}

Node represents a blockchain peer

func InitializeP2PNodes

func InitializeP2PNodes(numNodes int, slotInterval int, slotsPerEpoch int, seed int) []*Node

func NewNode

func NewNode(ctx context.Context, addr string, topicName string, isFullNode bool) (*Node, error)

NewNode initializes a blockchain node

func (*Node) AddBlock

func (n *Node) AddBlock(block *blockchain.Block)

func (*Node) AddTransaction

func (n *Node) AddTransaction(tx *blockchain.Transaction)

func (*Node) BroadcastRandomNumber

func (n *Node) BroadcastRandomNumber(epoch int64)

func (*Node) BroadcastTransaction

func (n *Node) BroadcastTransaction(tx blockchain.Transaction)

func (*Node) ChooseTxFromPool

func (n *Node) ChooseTxFromPool(limit int) []blockchain.Transaction

func (*Node) CreateBlockIfLeader

func (n *Node) CreateBlockIfLeader()

func (*Node) DNSRequestHandler

func (n *Node) DNSRequestHandler(req BDNSRequest, reqSender string, metrics *metrics.DNSMetrics)

func (*Node) DNSResponseHandler

func (n *Node) DNSResponseHandler(res BDNSResponse)

func (*Node) GenerateRandomNumber

func (n *Node) GenerateRandomNumber() []byte

func (*Node) GetSlotLeader

func (n *Node) GetSlotLeader(epoch int64) []byte

func (*Node) HandleGetBlock

func (n *Node) HandleGetBlock(sender string)

HandleGetBlock responds with full blockchain blocks starting from a given height

func (*Node) HandleGetData

func (n *Node) HandleGetData(sender string)

HandleGetData responds with transactions in the mempool

func (*Node) HandleGossip

func (n *Node) HandleGossip()

HandleGossip listens for messages from the gossip network

func (*Node) HandleINV

func (n *Node) HandleINV(sender string)

HandleINV processes inventory message and requests missing blocks

func (*Node) HandleMerkleRequest

func (n *Node) HandleMerkleRequest(sender string)

HandleMerkleRequest sends Merkle proof path for a record

func (*Node) HandleMsgGivenType

func (n *Node) HandleMsgGivenType(msg GossipMessage, _ *metrics.DNSMetrics)

func (*Node) InitializeNodeAsync

func (n *Node) InitializeNodeAsync(chainID string, registryKeys [][]byte, initialTimestamp int64, slotInterval int64, slotsPerEpoch int64, seed float64)

func (*Node) ListenForDirectMessages

func (n *Node) ListenForDirectMessages()

Handles direct peer-to-peer messages

func (*Node) MakeDNSRequest

func (n *Node) MakeDNSRequest(domainName string, metrics *metrics.DNSMetrics)

func (*Node) RandomNumberHandler

func (n *Node) RandomNumberHandler(epoch int64, sender string, secretValue int, randomValue int)

type NodeConfig

type NodeConfig struct {
	InitialTimestamp int64
	SlotInterval     int64
	SlotsPerEpoch    int64
	Seed             float64
}

Node Config

type P2PNetwork

type P2PNetwork struct {
	Host    host.Host
	PubSub  *pubsub.PubSub
	Topic   *pubsub.Topic
	Sub     *pubsub.Subscription
	MsgChan chan GossipMessage
}

P2PNetwork struct manages libp2p host and pubsub

func NewP2PNetwork

func NewP2PNetwork(ctx context.Context, addr string, topicName string) (*P2PNetwork, error)

NewP2PNetwork initializes a libp2p node with pubsub gossip

func (*P2PNetwork) BroadcastMessage

func (p *P2PNetwork) BroadcastMessage(msgType MessageType, content interface{}, metrics *metrics.DNSMetrics)

BroadcastMessage publishes a message to the gossip network

func (*P2PNetwork) Close

func (p *P2PNetwork) Close()

Close shuts down the network

func (*P2PNetwork) ConnectToPeer

func (p *P2PNetwork) ConnectToPeer(peerAddr string) error

ConnectToPeer connects to another peer via multiaddress

func (*P2PNetwork) DirectMessage

func (p *P2PNetwork) DirectMessage(msgType MessageType, content interface{}, peerIDStr string)

DirectMessage sends a message to a specific peer

func (*P2PNetwork) ListenForGossip

func (p *P2PNetwork) ListenForGossip()

ListenForGossip listens for gossip messages

type RandomNumberMsg

type RandomNumberMsg struct {
	Epoch       int64
	SecretValue int    // u_i value
	RandomValue int    // r_i value
	Sender      []byte // Registry's public key
}

type RecordType

type RecordType string

RecordType defines supported BDNS record types

const (
	A     RecordType = "A"     // IPv4 Address
	AAAA  RecordType = "AAAA"  // IPv6 Address
	CNAME RecordType = "CNAME" // Alias for another domain
	MX    RecordType = "MX"    // Mail Exchange
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL