mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
swarm/pss: Fix comments, minor cleanup
This commit is contained in:
parent
38141389b0
commit
be084fc4f4
2 changed files with 25 additions and 19 deletions
|
|
@ -332,7 +332,7 @@ func (p *Pss) deregister(topic *Topic, hndlr *handler) {
|
||||||
handlers := p.handlers[*topic]
|
handlers := p.handlers[*topic]
|
||||||
if len(handlers) == 1 {
|
if len(handlers) == 1 {
|
||||||
delete(p.handlers, *topic)
|
delete(p.handlers, *topic)
|
||||||
// check if we still have a prox handler on this topic
|
// topic caps might have changed now that a handler is gone
|
||||||
var caps byte
|
var caps byte
|
||||||
for h := range handlers {
|
for h := range handlers {
|
||||||
caps |= h.caps
|
caps |= h.caps
|
||||||
|
|
@ -380,7 +380,6 @@ func (p *Pss) handlePssMsg(ctx context.Context, msg interface{}) error {
|
||||||
if pssmsg.isRaw() {
|
if pssmsg.isRaw() {
|
||||||
if p.topicHandlerCaps[psstopic]&handlerCapRaw == 0 {
|
if p.topicHandlerCaps[psstopic]&handlerCapRaw == 0 {
|
||||||
log.Debug("No handler for raw message", "topic", psstopic)
|
log.Debug("No handler for raw message", "topic", psstopic)
|
||||||
//return errors.New("No handler for raw message")
|
|
||||||
}
|
}
|
||||||
isRaw = true
|
isRaw = true
|
||||||
}
|
}
|
||||||
|
|
@ -496,11 +495,11 @@ func (p *Pss) isSelfPossibleRecipient(msg *PssMsg, prox bool) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
minProx := p.Kademlia.NeighbourhoodDepth()
|
minProx := p.Kademlia.NeighbourhoodDepth()
|
||||||
depth, eq := p.Kademlia.Pof(p.Kademlia.BaseAddr(), msg.To, 0)
|
depth, _ := p.Kademlia.Pof(p.Kademlia.BaseAddr(), msg.To, 0)
|
||||||
log.Trace("selfpossible", "minprox", minProx, "depth", depth)
|
log.Trace("selfpossible", "minprox", minProx, "depth", depth)
|
||||||
|
|
||||||
log.Debug("here")
|
log.Debug("here")
|
||||||
if eq || minProx <= depth {
|
if minProx <= depth {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|
|
||||||
|
|
@ -329,20 +329,14 @@ func TestAddressMatchProx(t *testing.T) {
|
||||||
peerCount := kad.MinBinSize + 2
|
peerCount := kad.MinBinSize + 2
|
||||||
|
|
||||||
// set up pss
|
// set up pss
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
privKey, err := crypto.GenerateKey()
|
||||||
defer cancel()
|
pssp := NewPssParams().WithPrivateKey(privKey)
|
||||||
keys, err := wapi.NewKeyPair(ctx)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Could not generate private key: %v", err)
|
|
||||||
}
|
|
||||||
privkey, err := w.GetPrivateKey(keys)
|
|
||||||
pssp := NewPssParams().WithPrivateKey(privkey)
|
|
||||||
ps, err := NewPss(kad, pssp)
|
ps, err := NewPss(kad, pssp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err.Error())
|
t.Fatal(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// create kademlia peers, so we have peers outside minprox
|
// create kademlia peers, so we have peers both inside and outside minproxlimit
|
||||||
var peers []*network.Peer
|
var peers []*network.Peer
|
||||||
for i := 0; i < peerCount; i++ {
|
for i := 0; i < peerCount; i++ {
|
||||||
rw := &p2p.MsgPipeRW{}
|
rw := &p2p.MsgPipeRW{}
|
||||||
|
|
@ -362,7 +356,7 @@ func TestAddressMatchProx(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: create a test in the network package to make a table with n peers where n-m are proxpeers
|
// TODO: create a test in the network package to make a table with n peers where n-m are proxpeers
|
||||||
// meanwhile test regression for kademlia since we the params are generated outside the package
|
// meanwhile test regression for kademlia since we are compiling the test parameters from different packages
|
||||||
var proxes int
|
var proxes int
|
||||||
var conns int
|
var conns int
|
||||||
kad.EachConn(nil, peerCount, func(p *network.Peer, po int, prox bool) bool {
|
kad.EachConn(nil, peerCount, func(p *network.Peer, po int, prox bool) bool {
|
||||||
|
|
@ -387,7 +381,13 @@ func TestAddressMatchProx(t *testing.T) {
|
||||||
kad.MinBinSize - 1,
|
kad.MinBinSize - 1,
|
||||||
0,
|
0,
|
||||||
}
|
}
|
||||||
expects := []bool{true, true, true, false, false}
|
expects := []bool{
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
}
|
||||||
|
|
||||||
// first the unit test on the method that calculates possible receipient using prox
|
// first the unit test on the method that calculates possible receipient using prox
|
||||||
for i, distance := range remoteDistances {
|
for i, distance := range remoteDistances {
|
||||||
|
|
@ -608,8 +608,6 @@ func TestMessageProcessing(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// outbox full should return error
|
// outbox full should return error
|
||||||
return
|
|
||||||
|
|
||||||
msg.Expire = uint32(time.Now().Add(time.Second * 60).Unix())
|
msg.Expire = uint32(time.Now().Add(time.Second * 60).Unix())
|
||||||
for i := 0; i < defaultOutboxCapacity; i++ {
|
for i := 0; i < defaultOutboxCapacity; i++ {
|
||||||
ps.outbox <- msg
|
ps.outbox <- msg
|
||||||
|
|
@ -807,6 +805,7 @@ func TestPeerCapabilityMismatch(t *testing.T) {
|
||||||
// verifies that message handlers for raw messages only are invoked when minimum one handler for the topic exists in which raw messages are explicitly allowed
|
// verifies that message handlers for raw messages only are invoked when minimum one handler for the topic exists in which raw messages are explicitly allowed
|
||||||
func TestRawAllow(t *testing.T) {
|
func TestRawAllow(t *testing.T) {
|
||||||
|
|
||||||
|
// set up pss like so many times before
|
||||||
privKey, err := crypto.GenerateKey()
|
privKey, err := crypto.GenerateKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
@ -816,6 +815,7 @@ func TestRawAllow(t *testing.T) {
|
||||||
ps := newTestPss(privKey, kad, nil)
|
ps := newTestPss(privKey, kad, nil)
|
||||||
topic := BytesToTopic([]byte{0x2a})
|
topic := BytesToTopic([]byte{0x2a})
|
||||||
|
|
||||||
|
// create handler innards that increments every time a message hits it
|
||||||
var receives int
|
var receives int
|
||||||
rawHandlerFunc := func(msg []byte, p *p2p.Peer, asymmetric bool, keyid string) error {
|
rawHandlerFunc := func(msg []byte, p *p2p.Peer, asymmetric bool, keyid string) error {
|
||||||
log.Trace("in allowraw handler")
|
log.Trace("in allowraw handler")
|
||||||
|
|
@ -823,11 +823,13 @@ func TestRawAllow(t *testing.T) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// wrap this handler function with a handler without raw capability and register it
|
||||||
hndlrNoRaw := &handler{
|
hndlrNoRaw := &handler{
|
||||||
f: rawHandlerFunc,
|
f: rawHandlerFunc,
|
||||||
}
|
}
|
||||||
ps.Register(&topic, hndlrNoRaw)
|
ps.Register(&topic, hndlrNoRaw)
|
||||||
|
|
||||||
|
// test it with a raw message, should be poo-poo
|
||||||
pssMsg := newPssMsg(&msgParams{
|
pssMsg := newPssMsg(&msgParams{
|
||||||
raw: true,
|
raw: true,
|
||||||
})
|
})
|
||||||
|
|
@ -841,21 +843,26 @@ func TestRawAllow(t *testing.T) {
|
||||||
t.Fatalf("Expected handler not to be executed with raw cap off")
|
t.Fatalf("Expected handler not to be executed with raw cap off")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// now wrap the same handler function with raw capabilities and register it
|
||||||
hndlrRaw := &handler{
|
hndlrRaw := &handler{
|
||||||
f: rawHandlerFunc,
|
f: rawHandlerFunc,
|
||||||
caps: handlerCapRaw,
|
caps: handlerCapRaw,
|
||||||
}
|
}
|
||||||
deregRawHandler := ps.Register(&topic, hndlrRaw)
|
deregRawHandler := ps.Register(&topic, hndlrRaw)
|
||||||
pssMsg.Payload.Data = []byte("raw deal")
|
|
||||||
|
// should work now
|
||||||
|
pssMsg.Payload.Data = []byte("Raw Deal")
|
||||||
ps.handlePssMsg(context.TODO(), pssMsg)
|
ps.handlePssMsg(context.TODO(), pssMsg)
|
||||||
if receives == 0 {
|
if receives == 0 {
|
||||||
t.Fatalf("Expected handler to be executed with raw cap on")
|
t.Fatalf("Expected handler to be executed with raw cap on")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// now deregister the raw capable handler
|
||||||
prevReceives := receives
|
prevReceives := receives
|
||||||
deregRawHandler()
|
deregRawHandler()
|
||||||
|
|
||||||
pssMsg.Payload.Data = []byte("raw trump")
|
// check that raw messages fail again
|
||||||
|
pssMsg.Payload.Data = []byte("Raw Trump")
|
||||||
ps.handlePssMsg(context.TODO(), pssMsg)
|
ps.handlePssMsg(context.TODO(), pssMsg)
|
||||||
if receives != prevReceives {
|
if receives != prevReceives {
|
||||||
t.Fatalf("Expected handler not to be executed when raw handler is retracted")
|
t.Fatalf("Expected handler not to be executed when raw handler is retracted")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue