swarm: Added lightnode flag (#17291)

* swarm: Added lightnode flag

Added --lightnode command line parameter
Added LightNode to Handshake message

* swarm/config: Fixed variable naming

* cmd/swarm: Changed BoolTFlag to BoolFlag for SwarmLightNodeEnabled

* swarm/network: Changed logging

* swarm/network: Changed protocol version testing

* swarm/network: Renamed DefaultNetworkID variable to TestProtocolNetworkID

* swarm/network: Bumped protocol version

* swarm/network: Changed LightNode handhsake test to table driven

* swarm/network: Changed back TestProtocolVersion to 5 for now

* swarm/network: Moved the test configuration inside the test function scope
This commit is contained in:
Attila Gazso 2018-08-07 15:34:11 +02:00 committed by Balint Gabor
parent 8461fea44b
commit 9df16f3468
6 changed files with 95 additions and 17 deletions

View File

@ -68,6 +68,7 @@ const (
SWARM_ENV_SWAP_API = "SWARM_SWAP_API" SWARM_ENV_SWAP_API = "SWARM_SWAP_API"
SWARM_ENV_SYNC_DISABLE = "SWARM_SYNC_DISABLE" SWARM_ENV_SYNC_DISABLE = "SWARM_SYNC_DISABLE"
SWARM_ENV_SYNC_UPDATE_DELAY = "SWARM_ENV_SYNC_UPDATE_DELAY" SWARM_ENV_SYNC_UPDATE_DELAY = "SWARM_ENV_SYNC_UPDATE_DELAY"
SWARM_ENV_LIGHT_NODE_ENABLE = "SWARM_LIGHT_NODE_ENABLE"
SWARM_ENV_DELIVERY_SKIP_CHECK = "SWARM_DELIVERY_SKIP_CHECK" SWARM_ENV_DELIVERY_SKIP_CHECK = "SWARM_DELIVERY_SKIP_CHECK"
SWARM_ENV_ENS_API = "SWARM_ENS_API" SWARM_ENV_ENS_API = "SWARM_ENS_API"
SWARM_ENV_ENS_ADDR = "SWARM_ENS_ADDR" SWARM_ENV_ENS_ADDR = "SWARM_ENS_ADDR"
@ -204,6 +205,10 @@ func cmdLineOverride(currentConfig *bzzapi.Config, ctx *cli.Context) *bzzapi.Con
currentConfig.SyncUpdateDelay = d currentConfig.SyncUpdateDelay = d
} }
if ctx.GlobalIsSet(SwarmLightNodeEnabled.Name) {
currentConfig.LightNodeEnabled = true
}
if ctx.GlobalIsSet(SwarmDeliverySkipCheckFlag.Name) { if ctx.GlobalIsSet(SwarmDeliverySkipCheckFlag.Name) {
currentConfig.DeliverySkipCheck = true currentConfig.DeliverySkipCheck = true
} }
@ -301,6 +306,12 @@ func envVarsOverride(currentConfig *bzzapi.Config) (config *bzzapi.Config) {
} }
} }
if lne := os.Getenv(SWARM_ENV_LIGHT_NODE_ENABLE); lne != "" {
if lightnode, err := strconv.ParseBool(lne); err != nil {
currentConfig.LightNodeEnabled = lightnode
}
}
if swapapi := os.Getenv(SWARM_ENV_SWAP_API); swapapi != "" { if swapapi := os.Getenv(SWARM_ENV_SWAP_API); swapapi != "" {
currentConfig.SwapAPI = swapapi currentConfig.SwapAPI = swapapi
} }

View File

@ -123,6 +123,11 @@ var (
Usage: "Duration for sync subscriptions update after no new peers are added (default 15s)", Usage: "Duration for sync subscriptions update after no new peers are added (default 15s)",
EnvVar: SWARM_ENV_SYNC_UPDATE_DELAY, EnvVar: SWARM_ENV_SYNC_UPDATE_DELAY,
} }
SwarmLightNodeEnabled = cli.BoolFlag{
Name: "lightnode",
Usage: "Enable Swarm LightNode (default false)",
EnvVar: SWARM_ENV_LIGHT_NODE_ENABLE,
}
SwarmDeliverySkipCheckFlag = cli.BoolFlag{ SwarmDeliverySkipCheckFlag = cli.BoolFlag{
Name: "delivery-skip-check", Name: "delivery-skip-check",
Usage: "Skip chunk delivery check (default false)", Usage: "Skip chunk delivery check (default false)",
@ -464,6 +469,7 @@ pv(1) tool to get a progress bar:
SwarmSwapAPIFlag, SwarmSwapAPIFlag,
SwarmSyncDisabledFlag, SwarmSyncDisabledFlag,
SwarmSyncUpdateDelay, SwarmSyncUpdateDelay,
SwarmLightNodeEnabled,
SwarmDeliverySkipCheckFlag, SwarmDeliverySkipCheckFlag,
SwarmListenAddrFlag, SwarmListenAddrFlag,
SwarmPortFlag, SwarmPortFlag,

View File

@ -63,6 +63,7 @@ type Config struct {
SwapEnabled bool SwapEnabled bool
SyncEnabled bool SyncEnabled bool
DeliverySkipCheck bool DeliverySkipCheck bool
LightNodeEnabled bool
SyncUpdateDelay time.Duration SyncUpdateDelay time.Duration
SwapAPI string SwapAPI string
Cors string Cors string

View File

@ -94,12 +94,14 @@ type BzzConfig struct {
UnderlayAddr []byte // node's underlay address UnderlayAddr []byte // node's underlay address
HiveParams *HiveParams HiveParams *HiveParams
NetworkID uint64 NetworkID uint64
LightNode bool
} }
// Bzz is the swarm protocol bundle // Bzz is the swarm protocol bundle
type Bzz struct { type Bzz struct {
*Hive *Hive
NetworkID uint64 NetworkID uint64
LightNode bool
localAddr *BzzAddr localAddr *BzzAddr
mtx sync.Mutex mtx sync.Mutex
handshakes map[discover.NodeID]*HandshakeMsg handshakes map[discover.NodeID]*HandshakeMsg
@ -116,6 +118,7 @@ func NewBzz(config *BzzConfig, kad Overlay, store state.Store, streamerSpec *pro
return &Bzz{ return &Bzz{
Hive: NewHive(config.HiveParams, kad, store), Hive: NewHive(config.HiveParams, kad, store),
NetworkID: config.NetworkID, NetworkID: config.NetworkID,
LightNode: config.LightNode,
localAddr: &BzzAddr{config.OverlayAddr, config.UnderlayAddr}, localAddr: &BzzAddr{config.OverlayAddr, config.UnderlayAddr},
handshakes: make(map[discover.NodeID]*HandshakeMsg), handshakes: make(map[discover.NodeID]*HandshakeMsg),
streamerRun: streamerRun, streamerRun: streamerRun,
@ -209,7 +212,11 @@ func (b *Bzz) RunProtocol(spec *protocols.Spec, run func(*BzzPeer) error) func(*
localAddr: b.localAddr, localAddr: b.localAddr,
BzzAddr: handshake.peerAddr, BzzAddr: handshake.peerAddr,
lastActive: time.Now(), lastActive: time.Now(),
LightNode: handshake.LightNode,
} }
log.Debug("peer created", "addr", handshake.peerAddr.String())
return run(peer) return run(peer)
} }
} }
@ -228,6 +235,7 @@ func (b *Bzz) performHandshake(p *protocols.Peer, handshake *HandshakeMsg) error
return err return err
} }
handshake.peerAddr = rsh.(*HandshakeMsg).Addr handshake.peerAddr = rsh.(*HandshakeMsg).Addr
handshake.LightNode = rsh.(*HandshakeMsg).LightNode
return nil return nil
} }
@ -263,6 +271,7 @@ type BzzPeer struct {
localAddr *BzzAddr // local Peers address localAddr *BzzAddr // local Peers address
*BzzAddr // remote address -> implements Addr interface = protocols.Peer *BzzAddr // remote address -> implements Addr interface = protocols.Peer
lastActive time.Time // time is updated whenever mutexes are releasing lastActive time.Time // time is updated whenever mutexes are releasing
LightNode bool
} }
func NewBzzTestPeer(p *protocols.Peer, addr *BzzAddr) *BzzPeer { func NewBzzTestPeer(p *protocols.Peer, addr *BzzAddr) *BzzPeer {
@ -294,6 +303,7 @@ type HandshakeMsg struct {
Version uint64 Version uint64
NetworkID uint64 NetworkID uint64
Addr *BzzAddr Addr *BzzAddr
LightNode bool
// peerAddr is the address received in the peer handshake // peerAddr is the address received in the peer handshake
peerAddr *BzzAddr peerAddr *BzzAddr
@ -305,7 +315,7 @@ type HandshakeMsg struct {
// String pretty prints the handshake // String pretty prints the handshake
func (bh *HandshakeMsg) String() string { func (bh *HandshakeMsg) String() string {
return fmt.Sprintf("Handshake: Version: %v, NetworkID: %v, Addr: %v", bh.Version, bh.NetworkID, bh.Addr) return fmt.Sprintf("Handshake: Version: %v, NetworkID: %v, Addr: %v, LightNode: %v, peerAddr: %v", bh.Version, bh.NetworkID, bh.Addr, bh.LightNode, bh.peerAddr)
} }
// Perform initiates the handshake and validates the remote handshake message // Perform initiates the handshake and validates the remote handshake message
@ -338,6 +348,7 @@ func (b *Bzz) GetHandshake(peerID discover.NodeID) (*HandshakeMsg, bool) {
Version: uint64(BzzSpec.Version), Version: uint64(BzzSpec.Version),
NetworkID: b.NetworkID, NetworkID: b.NetworkID,
Addr: b.localAddr, Addr: b.localAddr,
LightNode: b.LightNode,
init: make(chan bool, 1), init: make(chan bool, 1),
done: make(chan struct{}), done: make(chan struct{}),
} }

View File

@ -30,6 +30,11 @@ import (
p2ptest "github.com/ethereum/go-ethereum/p2p/testing" p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
) )
const (
TestProtocolVersion = 5
TestProtocolNetworkID = 3
)
var ( var (
loglevel = flag.Int("loglevel", 2, "verbosity of logs") loglevel = flag.Int("loglevel", 2, "verbosity of logs")
) )
@ -127,23 +132,30 @@ type bzzTester struct {
*p2ptest.ProtocolTester *p2ptest.ProtocolTester
addr *BzzAddr addr *BzzAddr
cs map[string]chan bool cs map[string]chan bool
bzz *Bzz
} }
func newBzzHandshakeTester(t *testing.T, n int, addr *BzzAddr) *bzzTester { func newBzz(addr *BzzAddr, lightNode bool) *Bzz {
config := &BzzConfig{ config := &BzzConfig{
OverlayAddr: addr.Over(), OverlayAddr: addr.Over(),
UnderlayAddr: addr.Under(), UnderlayAddr: addr.Under(),
HiveParams: NewHiveParams(), HiveParams: NewHiveParams(),
NetworkID: DefaultNetworkID, NetworkID: DefaultNetworkID,
LightNode: lightNode,
} }
kad := NewKademlia(addr.OAddr, NewKadParams()) kad := NewKademlia(addr.OAddr, NewKadParams())
bzz := NewBzz(config, kad, nil, nil, nil) bzz := NewBzz(config, kad, nil, nil, nil)
return bzz
}
s := p2ptest.NewProtocolTester(t, NewNodeIDFromAddr(addr), 1, bzz.runBzz) func newBzzHandshakeTester(t *testing.T, n int, addr *BzzAddr, lightNode bool) *bzzTester {
bzz := newBzz(addr, lightNode)
pt := p2ptest.NewProtocolTester(t, NewNodeIDFromAddr(addr), n, bzz.runBzz)
return &bzzTester{ return &bzzTester{
addr: addr, addr: addr,
ProtocolTester: s, ProtocolTester: pt,
bzz: bzz,
} }
} }
@ -184,22 +196,24 @@ func (s *bzzTester) testHandshake(lhs, rhs *HandshakeMsg, disconnects ...*p2ptes
return nil return nil
} }
func correctBzzHandshake(addr *BzzAddr) *HandshakeMsg { func correctBzzHandshake(addr *BzzAddr, lightNode bool) *HandshakeMsg {
return &HandshakeMsg{ return &HandshakeMsg{
Version: 5, Version: TestProtocolVersion,
NetworkID: DefaultNetworkID, NetworkID: TestProtocolNetworkID,
Addr: addr, Addr: addr,
LightNode: lightNode,
} }
} }
func TestBzzHandshakeNetworkIDMismatch(t *testing.T) { func TestBzzHandshakeNetworkIDMismatch(t *testing.T) {
lightNode := false
addr := RandomAddr() addr := RandomAddr()
s := newBzzHandshakeTester(t, 1, addr) s := newBzzHandshakeTester(t, 1, addr, lightNode)
id := s.IDs[0] id := s.IDs[0]
err := s.testHandshake( err := s.testHandshake(
correctBzzHandshake(addr), correctBzzHandshake(addr, lightNode),
&HandshakeMsg{Version: 5, NetworkID: 321, Addr: NewAddrFromNodeID(id)}, &HandshakeMsg{Version: TestProtocolVersion, NetworkID: 321, Addr: NewAddrFromNodeID(id)},
&p2ptest.Disconnect{Peer: id, Error: fmt.Errorf("Handshake error: Message handler error: (msg code 0): network id mismatch 321 (!= 3)")}, &p2ptest.Disconnect{Peer: id, Error: fmt.Errorf("Handshake error: Message handler error: (msg code 0): network id mismatch 321 (!= 3)")},
) )
@ -209,14 +223,15 @@ func TestBzzHandshakeNetworkIDMismatch(t *testing.T) {
} }
func TestBzzHandshakeVersionMismatch(t *testing.T) { func TestBzzHandshakeVersionMismatch(t *testing.T) {
lightNode := false
addr := RandomAddr() addr := RandomAddr()
s := newBzzHandshakeTester(t, 1, addr) s := newBzzHandshakeTester(t, 1, addr, lightNode)
id := s.IDs[0] id := s.IDs[0]
err := s.testHandshake( err := s.testHandshake(
correctBzzHandshake(addr), correctBzzHandshake(addr, lightNode),
&HandshakeMsg{Version: 0, NetworkID: 3, Addr: NewAddrFromNodeID(id)}, &HandshakeMsg{Version: 0, NetworkID: TestProtocolNetworkID, Addr: NewAddrFromNodeID(id)},
&p2ptest.Disconnect{Peer: id, Error: fmt.Errorf("Handshake error: Message handler error: (msg code 0): version mismatch 0 (!= 5)")}, &p2ptest.Disconnect{Peer: id, Error: fmt.Errorf("Handshake error: Message handler error: (msg code 0): version mismatch 0 (!= %d)", TestProtocolVersion)},
) )
if err != nil { if err != nil {
@ -225,16 +240,49 @@ func TestBzzHandshakeVersionMismatch(t *testing.T) {
} }
func TestBzzHandshakeSuccess(t *testing.T) { func TestBzzHandshakeSuccess(t *testing.T) {
lightNode := false
addr := RandomAddr() addr := RandomAddr()
s := newBzzHandshakeTester(t, 1, addr) s := newBzzHandshakeTester(t, 1, addr, lightNode)
id := s.IDs[0] id := s.IDs[0]
err := s.testHandshake( err := s.testHandshake(
correctBzzHandshake(addr), correctBzzHandshake(addr, lightNode),
&HandshakeMsg{Version: 5, NetworkID: 3, Addr: NewAddrFromNodeID(id)}, &HandshakeMsg{Version: TestProtocolVersion, NetworkID: TestProtocolNetworkID, Addr: NewAddrFromNodeID(id)},
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestBzzHandshakeLightNode(t *testing.T) {
var lightNodeTests = []struct {
name string
lightNode bool
}{
{"on", true},
{"off", false},
}
for _, test := range lightNodeTests {
t.Run(test.name, func(t *testing.T) {
randomAddr := RandomAddr()
pt := newBzzHandshakeTester(t, 1, randomAddr, false)
id := pt.IDs[0]
addr := NewAddrFromNodeID(id)
err := pt.testHandshake(
correctBzzHandshake(randomAddr, false),
&HandshakeMsg{Version: TestProtocolVersion, NetworkID: TestProtocolNetworkID, Addr: addr, LightNode: test.lightNode},
)
if err != nil {
t.Fatal(err)
}
if pt.bzz.handshakes[id].LightNode != test.lightNode {
t.Fatalf("peer LightNode flag is %v, should be %v", pt.bzz.handshakes[id].LightNode, test.lightNode)
}
})
}
}

View File

@ -143,6 +143,7 @@ func NewSwarm(config *api.Config, mockStore *mock.NodeStore) (self *Swarm, err e
OverlayAddr: addr.OAddr, OverlayAddr: addr.OAddr,
UnderlayAddr: addr.UAddr, UnderlayAddr: addr.UAddr,
HiveParams: config.HiveParams, HiveParams: config.HiveParams,
LightNode: config.LightNodeEnabled,
} }
stateStore, err := state.NewDBStore(filepath.Join(config.Path, "state-store.db")) stateStore, err := state.NewDBStore(filepath.Join(config.Path, "state-store.db"))