mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-11 06:23:47 +00:00
fix: gossip in hive test
This commit is contained in:
parent
c2bef98ad5
commit
e97bca61ec
4 changed files with 83 additions and 8 deletions
|
|
@ -244,10 +244,7 @@ func (p *PortalProtocolAPI) AddEnr(enr string) (bool, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
p.portalProtocol.AddEnr(n)
|
||||||
wn := wrapNode(n)
|
|
||||||
wn.livenessChecks++
|
|
||||||
p.portalProtocol.table.addVerifiedNode(wn)
|
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -258,10 +255,7 @@ func (p *PortalProtocolAPI) AddEnrs(enrs []string) bool {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
p.portalProtocol.AddEnr(n)
|
||||||
wn := wrapNode(n)
|
|
||||||
wn.livenessChecks++
|
|
||||||
p.portalProtocol.table.addVerifiedNode(wn)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,8 @@ var ErrNilContentKey = errors.New("content key cannot be nil")
|
||||||
|
|
||||||
var ContentNotFound = storage.ErrContentNotFound
|
var ContentNotFound = storage.ErrContentNotFound
|
||||||
|
|
||||||
|
var MaxDistance = hexutil.MustDecode("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
||||||
|
|
||||||
type ContentElement struct {
|
type ContentElement struct {
|
||||||
Node enode.ID
|
Node enode.ID
|
||||||
ContentKeys [][]byte
|
ContentKeys [][]byte
|
||||||
|
|
@ -265,6 +267,12 @@ func (p *PortalProtocol) RoutingTableInfo() [][]string {
|
||||||
return nodes
|
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 {
|
func (p *PortalProtocol) setupUDPListening() error {
|
||||||
laddr := p.conn.LocalAddr().(*net.UDPAddr)
|
laddr := p.conn.LocalAddr().(*net.UDPAddr)
|
||||||
p.localNode.SetFallbackUDP(laddr.Port)
|
p.localNode.SetFallbackUDP(laddr.Port)
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
"github.com/ethereum/go-ethereum/portalnetwork/storage"
|
"github.com/ethereum/go-ethereum/portalnetwork/storage"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func setupBeaconNetwork(addr string, bootNodes []*enode.Node) (*BeaconNetwork, error) {
|
func setupBeaconNetwork(addr string, bootNodes []*enode.Node) (*BeaconNetwork, error) {
|
||||||
|
|
@ -218,3 +220,56 @@ func TestBeaconNetworkContent(t *testing.T) {
|
||||||
assert.Equal(t, vBytes3, get3)
|
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
portalnetwork/beacon/testdata/hive/gossip.yaml
vendored
Normal file
18
portalnetwork/beacon/testdata/hive/gossip.yaml
vendored
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue