Files
zeekayandHanzo Dev 37e2940ead mesh is the peer layer, network is the validator set
Two packages were named for the same word and meant different things. network/
dialed peers, exchanged keys, throttled and tracked them; nets/ tracked which
chains share a validator set and when they have all bootstrapped. One letter
apart, and neither name said which was which.

The peer layer is mesh — the word this estate already uses for it, in
lux-rs/node's own mesh.rs. That frees network for the thing that is actually a
network: a set of chains sharing a validator set. A network holds chains; a
chain belongs to one network.

github.com/luxfi/p2p stays what it is, the gossip library the VMs use. Three
concerns, three names, none of them borrowed from another.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-07 21:17:09 -07:00

94 lines
3.4 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package network
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/luxfi/ids"
"github.com/luxfi/math/set"
)
func TestNet(t *testing.T) {
require := require.New(t)
myNodeID := ids.GenerateTestNodeID()
chainID0 := ids.GenerateTestID()
chainID1 := ids.GenerateTestID()
chainID2 := ids.GenerateTestID()
s := New(myNodeID, Config{})
s.AddChain(chainID0)
require.False(s.IsBootstrapped(), "A net with one chain in bootstrapping shouldn't be considered bootstrapped")
s.Bootstrapped(chainID0)
require.True(s.IsBootstrapped(), "A net with only bootstrapped chains should be considered bootstrapped")
s.AddChain(chainID1)
require.False(s.IsBootstrapped(), "A net with one chain in bootstrapping shouldn't be considered bootstrapped")
s.AddChain(chainID2)
require.False(s.IsBootstrapped(), "A net with one chain in bootstrapping shouldn't be considered bootstrapped")
s.Bootstrapped(chainID1)
require.False(s.IsBootstrapped(), "A net with one chain in bootstrapping shouldn't be considered bootstrapped")
s.Bootstrapped(chainID2)
require.True(s.IsBootstrapped(), "A net with only bootstrapped chains should be considered bootstrapped")
}
func TestIsAllowed(t *testing.T) {
require := require.New(t)
myNodeID := ids.GenerateTestNodeID()
// Test with no rules
s := New(myNodeID, Config{})
require.True(s.IsAllowed(ids.GenerateTestNodeID(), true), "Validator should be allowed with no rules")
require.True(s.IsAllowed(ids.GenerateTestNodeID(), false), "Non-validator should be allowed with no rules")
// Test with validator only rules
s = New(myNodeID, Config{
ValidatorOnly: true,
})
require.True(s.IsAllowed(ids.GenerateTestNodeID(), true), "Validator should be allowed with validator only rules")
require.True(s.IsAllowed(myNodeID, false), "Self node should be allowed with validator only rules")
require.False(s.IsAllowed(ids.GenerateTestNodeID(), false), "Non-validator should not be allowed with validator only rules")
// Test with validator only rules and allowed nodes
allowedNodeID := ids.GenerateTestNodeID()
s = New(myNodeID, Config{
ValidatorOnly: true,
AllowedNodes: set.Of(allowedNodeID),
})
require.True(s.IsAllowed(allowedNodeID, true), "Validator should be allowed with validator only rules and allowed nodes")
require.True(s.IsAllowed(myNodeID, false), "Self node should be allowed with validator only rules")
require.False(s.IsAllowed(ids.GenerateTestNodeID(), false), "Non-validator should not be allowed with validator only rules and allowed nodes")
require.True(s.IsAllowed(allowedNodeID, true), "Non-validator allowed node should be allowed with validator only rules and allowed nodes")
}
// Bootstrapping must name the CHAINS still syncing. The net-level aggregate
// IsBootstrapped() only says "something here is unconverged"; only the chain
// IDs say what.
func TestNetBootstrappingNamesTheChains(t *testing.T) {
require := require.New(t)
chainID0 := ids.GenerateTestID()
chainID1 := ids.GenerateTestID()
s := New(ids.GenerateTestNodeID(), Config{})
require.Empty(s.Bootstrapping())
s.AddChain(chainID0)
s.AddChain(chainID1)
require.ElementsMatch([]ids.ID{chainID0, chainID1}, s.Bootstrapping())
s.Bootstrapped(chainID0)
require.Equal([]ids.ID{chainID1}, s.Bootstrapping())
s.Bootstrapped(chainID1)
require.Empty(s.Bootstrapping())
}