Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brendoncarroll committed Dec 4, 2022
1 parent 4464819 commit 2ea40cc
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 310 deletions.
3 changes: 2 additions & 1 deletion networks/forrestnet/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (

"github.com/brendoncarroll/go-p2p"
"github.com/brendoncarroll/go-tai64"
"github.com/inet256/inet256/pkg/inet256"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"

"github.com/inet256/inet256/pkg/inet256"
)

type Timestamp = tai64.TAI64N
Expand Down
4 changes: 2 additions & 2 deletions networks/forrestnet/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package forrestnet
import (
"time"

"github.com/inet256/inet256/networks"
"github.com/inet256/inet256/pkg/inet256"
"github.com/inet256/inet256/pkg/mesh256"
)

const (
Expand All @@ -17,6 +17,6 @@ var DefaultRoots = []inet256.Addr{
allOnes(),
}

func Factory(params networks.Params) networks.Network {
func Factory(params mesh256.NetworkParams) mesh256.Network {
return New(params, DefaultRoots)
}
16 changes: 0 additions & 16 deletions networks/forrestnet/dht.go

This file was deleted.

2 changes: 1 addition & 1 deletion networks/forrestnet/forreststate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewForrestState(privateKey inet256.PrivateKey, optimums ...inet256.Addr) *F
}
}

// Deliver is called with a Beacon, and a list of peers potentiall downstream from the beacon
// Deliver is called with a Beacon, and a list of peers potential downstream from the beacon
// Deliver returns a slice of new beacons to be sent out.
// This will either be:
// - an ammended version of b sent to all downstream peers
Expand Down
11 changes: 8 additions & 3 deletions networks/forrestnet/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package forrestnet

import (
"sync"
"time"

"github.com/brendoncarroll/go-p2p/p/kademlia"

"github.com/inet256/inet256/pkg/inet256"
)

Expand Down Expand Up @@ -41,18 +43,20 @@ type Locator struct {
type InfoStore struct {
mu sync.RWMutex
cache *kademlia.Cache[*NodeInfo]
now func() time.Time
}

func NewInfoStore(localID inet256.Addr, size int) *InfoStore {
return &InfoStore{
cache: kademlia.NewCache[*NodeInfo](localID[:], size, 1),
now: time.Now,
}
}

func (s *InfoStore) Get(x inet256.Addr) *NodeInfo {
s.mu.RLock()
defer s.mu.RUnlock()
ni, _ := s.cache.Get(x[:])
ni, _ := s.cache.Get(x[:], s.now())
return ni
}

Expand All @@ -73,9 +77,10 @@ func (s *InfoStore) Put(treeRoot inet256.Addr, pubKey inet256.PublicKey, loc Loc
}

func (s *InfoStore) update(addr inet256.Addr, fn func(x *NodeInfo) *NodeInfo) {
x, _ := s.cache.Get(addr[:])
now := s.now()
x, _ := s.cache.Get(addr[:], now)
y := fn(x)
s.cache.Put(addr[:], y)
s.cache.Put(addr[:], y, now, now.Add(100*time.Second))
}

// Find returns the PeerInfo closest to x.
Expand Down
3 changes: 2 additions & 1 deletion networks/forrestnet/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"encoding/binary"

"github.com/brendoncarroll/go-p2p"
"github.com/inet256/inet256/pkg/inet256"
"github.com/pkg/errors"

"github.com/inet256/inet256/pkg/inet256"
)

const (
Expand Down
12 changes: 6 additions & 6 deletions networks/forrestnet/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ package forrestnet
import (
"time"

"github.com/inet256/inet256/networks"
"github.com/inet256/inet256/networks/nettmpl1"
"github.com/inet256/inet256/networks/neteng"
"github.com/inet256/inet256/pkg/inet256"
"github.com/inet256/inet256/pkg/mesh256"
)

var _ networks.Network = &Network{}
var _ mesh256.Network = &Network{}

type Addr = inet256.Addr

type Network struct {
*nettmpl1.Network
*neteng.Network
router *Router
}

func New(params networks.Params, roots []inet256.ID) *Network {
func New(params mesh256.NetworkParams, roots []inet256.ID) *Network {
r := NewRouter(params.Logger)
nwk := nettmpl1.New(params, r, time.Second)
nwk := neteng.New(params, r, time.Second)
return &Network{
Network: nwk,
router: r,
Expand Down
50 changes: 30 additions & 20 deletions networks/forrestnet/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,64 @@ import (
"time"

"github.com/brendoncarroll/go-p2p"
"github.com/brendoncarroll/go-p2p/p/kademlia"
"github.com/brendoncarroll/go-tai64"
"github.com/golang/protobuf/proto"
"github.com/inet256/inet256/networks"
"github.com/inet256/inet256/networks/nettmpl1"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"

"github.com/inet256/inet256/networks/neteng"
"github.com/inet256/inet256/pkg/inet256"
"github.com/inet256/inet256/pkg/linkmap"
"github.com/pkg/errors"
"github.com/inet256/inet256/pkg/mesh256"
)

var _ nettmpl1.Router = &Router{}
var _ neteng.Router = &Router{}

type Router struct {
log networks.Logger
log mesh256.Logger
privateKey inet256.PrivateKey
publicKey inet256.PublicKey
localID inet256.ID
oneHop networks.PeerSet
oneHop mesh256.PeerSet
getPublicKey func(inet256.ID) inet256.PublicKey

linkMap *linkmap.LinkMap
state *ForrestState
infoStore *InfoStore
dhtNode *kademlia.DHTNode
}

func NewRouter(log networks.Logger) *Router {
func NewRouter(log mesh256.Logger) *Router {
return &Router{log: log}
}

func (r *Router) Reset(privateKey inet256.PrivateKey, peers networks.PeerSet, getPublicKey nettmpl1.PublicKeyFunc, now time.Time) {
func (r *Router) Reset(privateKey inet256.PrivateKey, peers mesh256.PeerSet, getPublicKey neteng.PublicKeyFunc, now time.Time) {
r.oneHop = peers
r.privateKey = privateKey
r.publicKey = r.privateKey.Public()
r.localID = inet256.NewAddr(r.publicKey)
r.linkMap = linkmap.New(r.localID)
r.getPublicKey = getPublicKey

r.state = NewForrestState(privateKey)
r.linkMap = linkmap.New(r.localID)
r.infoStore = NewInfoStore(r.localID, 128)
r.dhtNode = kademlia.NewDHTNode(kademlia.DHTNodeParams{
LocalID: p2p.PeerID(r.localID),
PeerCacheSize: 128,
DataCacheSize: 128,
})
}

func (r *Router) HandleAbove(dst inet256.Addr, data p2p.IOVec, send nettmpl1.SendFunc) bool {
func (r *Router) HandleAbove(dst inet256.Addr, data p2p.IOVec, send neteng.SendFunc) bool {
return false
}

func (r *Router) HandleBelow(from inet256.Addr, data []byte, send nettmpl1.SendFunc, deliver nettmpl1.DeliverFunc, info nettmpl1.InfoFunc) {
func (r *Router) HandleBelow(from inet256.Addr, data []byte, send neteng.SendFunc, deliver neteng.DeliverFunc, info neteng.InfoFunc) {
msg, err := ParseMessage(data)
if err != nil {
r.log.Warn("error parsing message from ", from, err)
return
}
if err := func() error {
mtype := msg.GetType()
Expand All @@ -72,22 +82,22 @@ func (r *Router) HandleBelow(from inet256.Addr, data []byte, send nettmpl1.SendF
}
}

func (r *Router) Heartbeat(now time.Time, send nettmpl1.SendFunc) {
func (r *Router) Heartbeat(now time.Time, send neteng.SendFunc) {
bcasts := r.state.Heartbeat(tai64.FromGoTime(now), r.listOutgoing(inet256.ID{}))
for _, b := range bcasts {
r.sendBeacon(send, getLeafAddr(b), b)
}
}

func (r *Router) FindAddr(send nettmpl1.SendFunc, info nettmpl1.InfoFunc, prefix []byte, nbits int) {
func (r *Router) FindAddr(send neteng.SendFunc, info neteng.InfoFunc, prefix []byte, nbits int) {

}

func (r *Router) LookupPublicKey(send nettmpl1.SendFunc, info nettmpl1.InfoFunc, target inet256.ID) {
func (r *Router) LookupPublicKey(send neteng.SendFunc, info neteng.InfoFunc, target inet256.ID) {

}

func (r *Router) handleData(send nettmpl1.SendFunc, deliver nettmpl1.DeliverFunc, from inet256.Addr, msg Message) error {
func (r *Router) handleData(send neteng.SendFunc, deliver neteng.DeliverFunc, from inet256.Addr, msg Message) error {
var rt RoutingTag
if err := proto.Unmarshal(msg.GetMetadata(), &rt); err != nil {
return err
Expand All @@ -105,7 +115,7 @@ func (r *Router) handleData(send nettmpl1.SendFunc, deliver nettmpl1.DeliverFunc
return nil
}

func (r *Router) handleBeacon(send nettmpl1.SendFunc, from inet256.Addr, msg Message) error {
func (r *Router) handleBeacon(send neteng.SendFunc, from inet256.Addr, msg Message) error {
b, err := parseBeacon(msg.GetBody())
if err != nil {
return err
Expand All @@ -129,23 +139,23 @@ func (r *Router) handleBeacon(send nettmpl1.SendFunc, from inet256.Addr, msg Mes
return nil
}

func (r *Router) handleFindNodeReq(send nettmpl1.SendFunc, dst inet256.Addr, msg Message) error {
func (r *Router) handleFindNodeReq(send neteng.SendFunc, dst inet256.Addr, msg Message) error {
return nil
}

func (r *Router) handleFindNodeRes(send nettmpl1.SendFunc, dst inet256.Addr, msg Message) error {
func (r *Router) handleFindNodeRes(send neteng.SendFunc, dst inet256.Addr, msg Message) error {
return nil
}

func (r *Router) sendBeacon(send nettmpl1.SendFunc, dst inet256.Addr, b *Beacon) {
func (r *Router) sendBeacon(send neteng.SendFunc, dst inet256.Addr, b *Beacon) {
data, err := proto.Marshal(b)
if err != nil {
panic(err)
}
send(dst, p2p.IOVec{data})
}

// func (r *Router) broadcastBeacon(send nettmpl1.SendFunc, exclude inet256.Addr, b *Beacon) {
// func (r *Router) broadcastBeacon(send neteng.SendFunc, exclude inet256.Addr, b *Beacon) {
// data, err := proto.Marshal(b)
// if err != nil {
// panic(err)
Expand Down
10 changes: 3 additions & 7 deletions networks/forrestnet/treestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package forrestnet

import (
"bytes"
sync "sync"
"sync"

"github.com/brendoncarroll/go-p2p/p/kademlia"
"github.com/brendoncarroll/go-tai64"

"github.com/inet256/inet256/pkg/inet256"
)

Expand Down Expand Up @@ -115,15 +116,10 @@ func (ts *TreeState) Heartbeat(now Timestamp, peers []Peer) (beacons []*Beacon)
return beacons
}

// LocationClaim returns a location claim for the current position
// in the tree. If the local node is the root of the tree, then nil
// is returned.
// LocationClaim returns a location claim for the current position in the tree.
func (ts *TreeState) LocationClaim() *LocationClaim {
ts.mu.Lock()
defer ts.mu.Unlock()
if ts.currentRoot == ts.localID {
return nil
}
return CreateLocationClaim(ts.privateKey, ts.beacon)
}

Expand Down

0 comments on commit 2ea40cc

Please sign in to comment.