Skip to content
This repository has been archived by the owner on Sep 15, 2020. It is now read-only.

Commit

Permalink
addPeer now gets the PubKey from originator when confrm true
Browse files Browse the repository at this point in the history
  • Loading branch information
zippy committed Apr 1, 2018
1 parent ee7fce2 commit ecbe18c
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 26 deletions.
12 changes: 4 additions & 8 deletions action.go
Expand Up @@ -505,16 +505,12 @@ func (a *APIFnVerifySignature) Args() []Arg {

func (a *APIFnVerifySignature) Call(h *Holochain) (response interface{}, err error) {
var b bool
var pubKeyIC ic.PubKey
var pubKey ic.PubKey
sig := SignatureFromB58String(a.b58signature)
var pubKeyBytes []byte
pubKeyBytes = b58.Decode(a.b58pubKey)
pubKeyIC, err = ic.UnmarshalPublicKey(pubKeyBytes)
if err != nil {
return
}

b, err = h.VerifySignature(sig, a.data, pubKeyIC)
pubKey, err = DecodePubKey(a.b58pubKey)

b, err = h.VerifySignature(sig, a.data, pubKey)
if err != nil {
return
}
Expand Down
7 changes: 7 additions & 0 deletions agent.go
Expand Up @@ -79,6 +79,13 @@ func (a *LibP2PAgent) EncodePubKey() (b58pk string, err error) {
return
}

func DecodePubKey(b58pk string) (pubKey ic.PubKey, err error) {
var pubKeyBytes []byte
pubKeyBytes = b58.Decode(b58pk)
pubKey, err = ic.UnmarshalPublicKey(pubKeyBytes)
return
}

func (a *LibP2PAgent) GenKeys(seed io.Reader) (err error) {
var priv ic.PrivKey
if seed == nil {
Expand Down
22 changes: 14 additions & 8 deletions dht.go
Expand Up @@ -142,12 +142,19 @@ const (

// NewDHT creates a new DHT structure
func NewDHT(h *Holochain) *DHT {
dht := DHT{
h: h,
glog: &h.Config.Loggers.Gossip,
dlog: &h.Config.Loggers.DHT,
config: &h.Nucleus().DNA().DHTConfig,
}
dht := DHT{}
dht.Open(h)
return &dht
}

// Open sets up the DHTs data structures and store
func (dht *DHT) Open(options interface{}) (err error) {
h := options.(*Holochain)
dht.h = h
dht.glog = &h.Config.Loggers.Gossip
dht.dlog = &h.Config.Loggers.DHT
dht.config = &h.Nucleus().DNA().DHTConfig

dht.ht = &BuntHT{}
dht.ht.Open(filepath.Join(h.DBPath(), DHTStoreFileName))
dht.retryQueue = make(chan *retry, 100)
Expand All @@ -156,8 +163,7 @@ func NewDHT(h *Holochain) *DHT {
// dht.fingerprints = make(map[string]bool)
dht.gchan = make(Channel, GossipWithQueueSize)
dht.gossipPuts = make(Channel, GossipPutQueueSize)

return &dht
return
}

// putKey implements the special case for adding the KeyEntry system type to the DHT
Expand Down
2 changes: 1 addition & 1 deletion holochain.go
Expand Up @@ -311,7 +311,7 @@ func (h *Holochain) Prepare() (err error) {
h.dht = NewDHT(h)
h.nucleus.h = h

h.world = NewWorld(h.node.HashAddr)
h.world = NewWorld(h.node.HashAddr, h.dht)

var peerList PeerList
peerList, err = h.dht.getList(BlockedList)
Expand Down
2 changes: 1 addition & 1 deletion ht_test.go
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
)

func Test(t *testing.T) {
func TestHT(t *testing.T) {
Convey("", t, func() {
})
}
39 changes: 38 additions & 1 deletion node.go
Expand Up @@ -16,6 +16,7 @@ import (
. "github.com/holochain/holochain-proto/hash"
goprocess "github.com/jbenet/goprocess"
goprocessctx "github.com/jbenet/goprocess/context"
ic "github.com/libp2p/go-libp2p-crypto"
nat "github.com/libp2p/go-libp2p-nat"
net "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
Expand Down Expand Up @@ -176,6 +177,30 @@ func (h *Holochain) HandlePeerFound(pi pstore.PeerInfo) {
}
}

func (h *Holochain) getNodePubKey(ID peer.ID) (pubKey ic.PubKey, err error) {
req := GetReq{H: HashFromPeerID(ID), GetMask: GetMaskEntry}
var rsp interface{}
rsp, err = callGet(h, req, &GetOptions{GetMask: req.GetMask})
if err != nil {
return
}
e := rsp.(GetResp).Entry
pubKey, err = DecodePubKey(e.Content().(string))
if err != nil {
return
}
var pkID peer.ID
pkID, err = peer.IDFromPublicKey(pubKey)
if err != nil {
return
}
if pkID != ID {
err = errors.New("Public Key doesn't match Node ID!")
return
}
return
}

func (h *Holochain) addPeer(pi pstore.PeerInfo, confirm bool) (err error) {
// add the peer into the peerstore
h.node.peerstore.AddAddrs(pi.ID, pi.Addrs, PeerTTL)
Expand All @@ -193,11 +218,23 @@ func (h *Holochain) addPeer(pi pstore.PeerInfo, confirm bool) (err error) {
h.dht.dlog.Logf("Adding Peer: %v\n", pi.ID)
h.node.routingTable.Update(pi.ID)
err = h.dht.AddGossiper(pi.ID)
if err != nil {
return
}
if bootstrap {
RoutingRefreshTask(h)
}
h.world.AddNode(pi)

var pubKey ic.PubKey
if confirm {
pubKey, err = h.getNodePubKey(pi.ID)
if err != nil {
return
}
}
h.world.AddNode(pi, pubKey)
}

return
}

Expand Down
9 changes: 8 additions & 1 deletion node_test.go
Expand Up @@ -372,9 +372,11 @@ func TestAddPeer(t *testing.T) {
h := nodes[0]
somePeer := nodes[1].node.HashAddr
pi := pstore.PeerInfo{ID: somePeer, Addrs: []ma.Multiaddr{nodes[1].node.NetAddr}}
Convey("it should add a peer to the peer store and the gossip list", t, func() {
Convey("it should add a peer to the peer store and the gossip list with public key", t, func() {
So(h.node.routingTable.Size(), ShouldEqual, 0)
So(len(h.node.peerstore.Peers()), ShouldEqual, 1)
So(len(h.world.nodes), ShouldEqual, 0)

err := h.AddPeer(pi)
So(err, ShouldBeNil)
So(len(h.node.peerstore.Peers()), ShouldEqual, 2)
Expand All @@ -383,6 +385,11 @@ func TestAddPeer(t *testing.T) {
So(len(glist), ShouldEqual, 1)
So(glist[0], ShouldEqual, somePeer)
So(h.node.routingTable.Size(), ShouldEqual, 1)

So(len(h.world.nodes), ShouldEqual, 1)
node1ID, _, _ := nodes[1].agent.NodeID()
So(h.world.nodes[node1ID].PubKey.Equals(nodes[1].agent.PubKey()), ShouldBeTrue)

})

Convey("it should not add a blocked peer", t, func() {
Expand Down
10 changes: 7 additions & 3 deletions world.go
Expand Up @@ -7,27 +7,31 @@ package holochain

import (
. "github.com/holochain/holochain-proto/hash"
ic "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
)

// NodeRecord stores the necessary information about other nodes in the world model
type NodeRecord struct {
PeerInfo pstore.PeerInfo
PubKey ic.PubKey
}

// World holds the data of a nodes' world model
type World struct {
me peer.ID
nodes map[peer.ID]*NodeRecord
responsible map[Hash][]peer.ID
ht HashTable
}

// NewWorld creates and empty world model
func NewWorld(me peer.ID) *World {
func NewWorld(me peer.ID, ht HashTable) *World {
world := World{me: me}
world.nodes = make(map[peer.ID]*NodeRecord)
world.responsible = make(map[Hash][]peer.ID)
world.ht = ht
return &world
}

Expand All @@ -44,8 +48,8 @@ func (world *World) AllNodes() (nodes []peer.ID, err error) {
}

// AddNode adds a node to the world model
func (world *World) AddNode(pi pstore.PeerInfo) (err error) {
rec := NodeRecord{PeerInfo: pi}
func (world *World) AddNode(pi pstore.PeerInfo, pubKey ic.PubKey) (err error) {
rec := NodeRecord{PeerInfo: pi, PubKey: pubKey}
world.nodes[pi.ID] = &rec
return
}
Expand Down
9 changes: 6 additions & 3 deletions world_test.go
Expand Up @@ -12,7 +12,7 @@ import (

func testAddNodeToWorld(world *World, ID peer.ID, addr ma.Multiaddr) {
pi := pstore.PeerInfo{ID: ID, Addrs: []ma.Multiaddr{addr}}
err := world.AddNode(pi)
err := world.AddNode(pi, nil)
if err != nil {
panic(err)
}
Expand All @@ -33,7 +33,9 @@ func testAddNodesToWorld(world *World, start, count int) (nodes []*Node) {
func TestWorldNodes(t *testing.T) {
b58 := "QmY8Mzg9F69e5P9AoQPYat655HEhc1TVGs11tmfNSzkqh2"
peer, _ := peer.IDB58Decode(b58)
world := NewWorld(peer)

ht := BuntHT{}
world := NewWorld(peer, &ht)

Convey("to start with I should know about nobody", t, func() {
nodes, err := world.AllNodes()
Expand Down Expand Up @@ -69,7 +71,8 @@ func TestWorldUpdateResponsible(t *testing.T) {
var p1, p2, p3, p4, p5 peer.ID
var hash1, hash2, hash4 Hash
p1, _ = peer.IDB58Decode(b58)
world := NewWorld(p1)
ht := BuntHT{}
world := NewWorld(p1, &ht)
var addr ma.Multiaddr
var err error
var responsible bool
Expand Down

0 comments on commit ecbe18c

Please sign in to comment.