Skip to content

Commit

Permalink
Merge pull request ethereum#58 from status-im/feature/ulc-config-refa…
Browse files Browse the repository at this point in the history
…ctoring-#52

config refactoring
  • Loading branch information
b00ris committed Jun 4, 2018
2 parents ab009b1 + f9380e2 commit a311a2b
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 110 deletions.
2 changes: 1 addition & 1 deletion cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
}

// Apply flags.
utils.SetULCFromFile(ctx, &cfg.Eth, &cfg.Node.P2P)
utils.SetULC(ctx, &cfg.Eth)
utils.SetNodeConfig(ctx, &cfg.Node)
stack, err := node.New(&cfg.Node)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ var (
utils.LightModeFlag,
utils.ULCModeConfigFlag,
utils.OnlyAnnounceModeFlag,
utils.ULCTrustedNodesFlag,
utils.ULCMinTrustedFractionFlag,
utils.SyncModeFlag,
utils.GCModeFlag,
utils.LightServFlag,
Expand Down
54 changes: 39 additions & 15 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,26 @@ var (
}
LightModeFlag = cli.BoolFlag{
Name: "light",
Usage: "Enable light client mode (replaced by --syncmode)",
Usage: "Enable light client mode(LES) (replaced by --syncmode)",
}
ULCModeConfigFlag = cli.StringFlag{
Name: "ulcconfig",
Name: "les.ulcconfig",
Usage: "Config file to use for ULC mode",
}
OnlyAnnounceModeFlag = cli.BoolFlag{
Name: "onlyannounce",
Usage: "Les server sends only announce",
Name: "les.onlyannounce",
Usage: "LES server sends only announce",
}
ULCMinTrustedFractionFlag = cli.IntFlag{
Name: "les.mintrustedfraction",
Usage: "LES server sends only announce",
Value: eth.DefaultUTCMinTrustedFraction,
}
ULCTrustedNodesFlag = cli.StringFlag{
Name: "les.trusted",
Usage: "Config file to use for ULC mode",
}

defaultSyncMode = eth.DefaultConfig.SyncMode
SyncModeFlag = TextMarshalerFlag{
Name: "syncmode",
Expand Down Expand Up @@ -737,24 +747,38 @@ func setIPC(ctx *cli.Context, cfg *node.Config) {
}
}

// SetULCFromFile setup ULC config from file if given.
func SetULCFromFile(ctx *cli.Context, cfg *eth.Config, p2pCfg *p2p.Config) {
path := ctx.GlobalString(ULCModeConfigFlag.Name)
if path == "" {
// SetULC setup ULC config from file if given.
func SetULC(ctx *cli.Context, cfg *eth.Config) {
// ULC config isn't loaded from global config and ULC config and ULC trusted nodes are not defined.
if cfg.ULC == nil && (ctx.GlobalIsSet(ULCModeConfigFlag.Name) || ctx.GlobalIsSet(ULCTrustedNodesFlag.Name)) == false {
return
}
cfg.ULC = &eth.ULCConfig{}

cfgData, err := ioutil.ReadFile(path)
if err != nil {
Fatalf("Failed to read ULC config file: %v", err)
path := ctx.GlobalString(ULCModeConfigFlag.Name)
if path != "" {
cfgData, err := ioutil.ReadFile(path)
if err != nil {
Fatalf("Failed to unmarshal ULC configuration: %v", err)
}

err = json.Unmarshal(cfgData, &cfg.ULC)
if err != nil {
Fatalf("Failed to unmarshal ULC configuration: %s", err.Error())
}
}

err = json.Unmarshal(cfgData, &cfg.ULC)
if err != nil {
Fatalf(err.Error())
if trustedNodes := ctx.GlobalString(ULCTrustedNodesFlag.Name); trustedNodes != "" {
cfg.ULC.TrustedServers = strings.Split(trustedNodes, ",")
}

eth.SetULC(cfg.ULC, p2pCfg)
if trustedFraction := ctx.GlobalInt(ULCMinTrustedFractionFlag.Name); trustedFraction > 0 {
cfg.ULC.MinTrustedFraction = trustedFraction
}
if cfg.ULC.MinTrustedFraction <= 0 && cfg.ULC.MinTrustedFraction > 100 {
log.Error("MinTrustedFraction is invalid", "MinTrustedFraction", cfg.ULC.MinTrustedFraction, "Changed to default", eth.DefaultUTCMinTrustedFraction)
cfg.ULC.MinTrustedFraction = eth.DefaultUTCMinTrustedFraction
}
}

// makeDatabaseHandles raises out the number of allowed file handles per process
Expand Down
2 changes: 1 addition & 1 deletion eth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type Config struct {
// Light client options
LightServ int `toml:",omitempty"` // Maximum percentage of time allowed for serving LES requests
LightPeers int `toml:",omitempty"` // Maximum number of LES client peers
OnlyAnnounce bool `toml:",omitempty"` // Maximum number of LES client peers
OnlyAnnounce bool // Maximum number of LES client peers

// Ultra Light client options
ULC *ULCConfig `toml:",omitempty"`
Expand Down
33 changes: 3 additions & 30 deletions eth/ulc_config.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,9 @@
package eth

import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
)

const defaultUTCMinTrustedFraction = 75
const DefaultUTCMinTrustedFraction = 75

// ULCConfig is a Ultra Light client options.
type ULCConfig struct {
TrustedNodes []string `toml:",omitempty"` // A list of trusted servers
TrustedServers []string `toml:",omitempty"` // A list of trusted servers
MinTrustedFraction int `toml:",omitempty"` // Minimum percentage of connected trusted servers to validate trusted (1-100)
}

// SetULC properties into eth and p2p configs.
func SetULC(ulc *ULCConfig, p2pCfg *p2p.Config) {
if ulc == nil || len(ulc.TrustedNodes) == 0 {
return
}

if ulc.MinTrustedFraction < 0 || ulc.MinTrustedFraction >= 100 {
ulc.MinTrustedFraction = defaultUTCMinTrustedFraction
}

p2pCfg.TrustedNodes = make([]*discover.Node, 0, len(ulc.TrustedNodes))
for _, url := range ulc.TrustedNodes {
node, err := discover.ParseNode(url)
if err != nil {
log.Error("Trusted node URL invalid", "enode", url, "err", err)
continue
}
p2pCfg.TrustedNodes = append(p2pCfg.TrustedNodes, node)
}
}
}
48 changes: 0 additions & 48 deletions eth/ulc_config_test.go

This file was deleted.

6 changes: 5 additions & 1 deletion les/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,12 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
bloomTrieIndexer: light.NewBloomTrieIndexer(chainDb, true),
}

var trustedNodes []string
if leth.config.ULC != nil {
trustedNodes = leth.config.ULC.TrustedServers
}
leth.relay = NewLesTxRelay(peers, leth.reqDist)
leth.serverPool = newServerPool(chainDb, quitSync, &leth.wg)
leth.serverPool = newServerPool(chainDb, quitSync, &leth.wg, trustedNodes)
leth.retriever = newRetrieveManager(peers, leth.reqDist, leth.serverPool)
leth.odr = NewLesOdr(chainDb, leth.chtIndexer, leth.bloomTrieIndexer, leth.bloomIndexer, leth.retriever)
if leth.blockchain, err = light.NewLightChain(leth.odr, leth.chainConfig, leth.engine); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion les/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ func (f *lightFetcher) isTrusted(hash common.Hash, minTrustedFraction int) bool
numAgreed = numAgreed + 1
}

return 100*numAgreed/numPeers > minTrustedFraction
return 100*numAgreed/numPeers >= minTrustedFraction
}

func (f *lightFetcher) newFetcherDistReqForSync(bestHash common.Hash) *distReq {
Expand Down
21 changes: 19 additions & 2 deletions les/serverpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type serverPool struct {
discNodes chan *discv5.Node
discLookups chan bool

trustedNodes []string
entries map[discover.NodeID]*poolEntry
lock sync.Mutex
timeout, enableRetry chan *poolEntry
Expand All @@ -116,7 +117,7 @@ type serverPool struct {
}

// newServerPool creates a new serverPool instance
func newServerPool(db ethdb.Database, quit chan struct{}, wg *sync.WaitGroup) *serverPool {
func newServerPool(db ethdb.Database, quit chan struct{}, wg *sync.WaitGroup, trustedNodes []string) *serverPool {
pool := &serverPool{
db: db,
quit: quit,
Expand All @@ -128,6 +129,7 @@ func newServerPool(db ethdb.Database, quit chan struct{}, wg *sync.WaitGroup) *s
knownSelect: newWeightedRandomSelect(),
newSelect: newWeightedRandomSelect(),
fastDiscover: true,
trustedNodes: trustedNodes,
}

pool.trustedQueue = newPoolEntryQueue(maxKnownEntries, nil)
Expand Down Expand Up @@ -401,7 +403,7 @@ func (pool *serverPool) loadNodes() {
pool.knownSelect.update((*knownEntry)(e))
}

for _, trusted := range pool.server.TrustedNodes {
for _, trusted := range pool.parseTrustedServers() {
e := pool.findOrNewNode(trusted.ID, trusted.IP, trusted.TCP)
e.trusted = true
e.dialed = &poolEntryAddress{ip: trusted.IP, port: trusted.TCP}
Expand All @@ -411,6 +413,21 @@ func (pool *serverPool) loadNodes() {

}

// parseTrustedServers returns valid and parsed by discovery enodes.
func (pool *serverPool) parseTrustedServers() []*discover.Node {
nodes := make([]*discover.Node, 0, len(pool.trustedNodes))

for _, enode := range pool.trustedNodes {
node, err := discover.ParseNode(enode)
if err != nil {
log.Warn("Trusted node URL invalid", "enode", enode, "err", err)
continue
}
nodes = append(nodes, node)
}
return nodes
}

// saveNodes saves known nodes and their statistics into the database. Nodes are
// ordered from least to most recently connected.
func (pool *serverPool) saveNodes() {
Expand Down
2 changes: 1 addition & 1 deletion les/serverpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestLoadTrustedNodes(t *testing.T) {

var wg sync.WaitGroup
q := make(chan struct{})
sp := newServerPool(&dbStub{}, q, &wg)
sp := newServerPool(&dbStub{}, q, &wg, []string{node.String()})
sp.server = &p2p.Server{}
sp.server.TrustedNodes = []*discover.Node{
&node,
Expand Down
4 changes: 2 additions & 2 deletions les/ulc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func newULC(ulcConfig *eth.ULCConfig) *ulc {
return nil
}

m := make(map[string]struct{}, len(ulcConfig.TrustedNodes))
for _, id := range ulcConfig.TrustedNodes {
m := make(map[string]struct{}, len(ulcConfig.TrustedServers))
for _, id := range ulcConfig.TrustedServers {
node, err := discover.ParseNode(id)
if err != nil {
continue
Expand Down
6 changes: 3 additions & 3 deletions les/ulc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestULCSyncWithOnePeer(t *testing.T) {
f := newFullPeerPair(t, 1, 4, testChainGen)
ulcConfig := &eth.ULCConfig{
MinTrustedFraction: 100,
TrustedNodes: []string{f.ID.String()},
TrustedServers: []string{f.ID.String()},
}

l := newLightPeer(t, ulcConfig)
Expand All @@ -45,7 +45,7 @@ func TestULCReceiveAnnounce(t *testing.T) {
f := newFullPeerPair(t, 1, 4, testChainGen)
ulcConfig := &eth.ULCConfig{
MinTrustedFraction: 100,
TrustedNodes: []string{f.ID.String()},
TrustedServers: []string{f.ID.String()},
}

key, err := crypto.GenerateKey()
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestULCShouldNotSyncWithTwoPeersOneHaveEmptyChain(t *testing.T) {
ulcConf.trustedKeys[f2.ID.String()] = struct{}{}
ulcConfig := &eth.ULCConfig{
MinTrustedFraction: 100,
TrustedNodes: []string{f1.ID.String(), f2.ID.String()},
TrustedServers: []string{f1.ID.String(), f2.ID.String()},
}
l := newLightPeer(t, ulcConfig)
l.PM.ulc.minTrustedFraction = 100
Expand Down
5 changes: 0 additions & 5 deletions mobile/geth.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,6 @@ func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) {
},
}

// setup ULC
if config.EthereumEnabled {
eth.SetULC(config.ULC, &nodeConf.P2P)
}

rawStack, err := node.New(nodeConf)
if err != nil {
return nil, err
Expand Down

0 comments on commit a311a2b

Please sign in to comment.