Skip to content

Commit

Permalink
fix: gossip in hive test
Browse files Browse the repository at this point in the history
  • Loading branch information
fearlessfe authored and GrapeBaBa committed Apr 18, 2024
1 parent c2bef98 commit e97bca6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
10 changes: 2 additions & 8 deletions p2p/discover/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,7 @@ func (p *PortalProtocolAPI) AddEnr(enr string) (bool, error) {
if err != nil {
return false, err
}

wn := wrapNode(n)
wn.livenessChecks++
p.portalProtocol.table.addVerifiedNode(wn)
p.portalProtocol.AddEnr(n)
return true, nil
}

Expand All @@ -258,10 +255,7 @@ func (p *PortalProtocolAPI) AddEnrs(enrs []string) bool {
if err != nil {
continue
}

wn := wrapNode(n)
wn.livenessChecks++
p.portalProtocol.table.addVerifiedNode(wn)
p.portalProtocol.AddEnr(n)
}

return true
Expand Down
8 changes: 8 additions & 0 deletions p2p/discover/portal_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ var ErrNilContentKey = errors.New("content key cannot be nil")

var ContentNotFound = storage.ErrContentNotFound

var MaxDistance = hexutil.MustDecode("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")

type ContentElement struct {
Node enode.ID
ContentKeys [][]byte
Expand Down Expand Up @@ -265,6 +267,12 @@ func (p *PortalProtocol) RoutingTableInfo() [][]string {
return nodes
}

func (p *PortalProtocol) AddEnr(n *enode.Node) {
p.setJustSeen(n)
id := n.ID().String()
p.radiusCache.Set([]byte(id), MaxDistance)
}

func (p *PortalProtocol) setupUDPListening() error {
laddr := p.conn.LocalAddr().(*net.UDPAddr)
p.localNode.SetFallbackUDP(laddr.Port)
Expand Down
55 changes: 55 additions & 0 deletions portalnetwork/beacon/beacon_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/portalnetwork/storage"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)

func setupBeaconNetwork(addr string, bootNodes []*enode.Node) (*BeaconNetwork, error) {
Expand Down Expand Up @@ -218,3 +220,56 @@ func TestBeaconNetworkContent(t *testing.T) {
assert.Equal(t, vBytes3, get3)
}
}

type Entry struct {
ContentKey string `yaml:"content_key"`
ContentValue string `yaml:"content_value"`
}

func TestGossipTwoNodes(t *testing.T) {
file, err := os.ReadFile("./testdata/hive/gossip.yaml")
require.NoError(t, err)
entries := make([]Entry, 0)
err = yaml.Unmarshal(file, &entries)
require.NoError(t, err)

keys := make([][]byte, 0)
values := make([][]byte, 0)

for _, entry := range entries {
keys = append(keys, hexutil.MustDecode(entry.ContentKey))
values = append(values, hexutil.MustDecode(entry.ContentValue))
}

logger := testlog.Logger(t, log.LvlTrace)
node1, err := setupBeaconNetwork(":6998", nil)
assert.NoError(t, err)
node1.log = logger
node1.portalProtocol.Log = logger
err = node1.Start()
assert.NoError(t, err)

node2, err := setupBeaconNetwork(":6999", nil)
assert.NoError(t, err)
node2.log = logger
node2.portalProtocol.Log = logger
err = node2.Start()
assert.NoError(t, err)

node2.portalProtocol.AddEnr(node1.portalProtocol.Self())

id := node2.portalProtocol.Self().ID()

num, err := node2.portalProtocol.Gossip(&id, keys, values)
require.NoError(t, err)
require.Equal(t, num, 1)

time.Sleep(time.Second * 10)

for i, key := range keys {
val := values[i]
res, err := node1.portalProtocol.Get(key, node1.portalProtocol.ToContentId(key))
require.NoError(t, err)
require.Equal(t, res, val)
}
}
18 changes: 18 additions & 0 deletions portalnetwork/beacon/testdata/hive/gossip.yaml

Large diffs are not rendered by default.

0 comments on commit e97bca6

Please sign in to comment.