github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/cmd/u2u/launcher/config_custom.go (about) 1 package launcher 2 3 import ( 4 "fmt" 5 6 "github.com/naoina/toml" 7 "github.com/naoina/toml/ast" 8 "github.com/unicornultrafoundation/go-u2u/p2p/enode" 9 ) 10 11 // asDefault is slice with one empty element 12 // which indicates that network default bootnodes should be substituted 13 var asDefault = []*enode.Node{{}} 14 15 func needDefaultBootnodes(nn []*enode.Node) bool { 16 return len(nn) == len(asDefault) && nn[0] == asDefault[0] 17 } 18 19 func isBootstrapNodesDefault(root *ast.Table) ( 20 bootstrapNodes bool, 21 bootstrapNodesV5 bool, 22 ) { 23 table := root 24 for _, path := range []string{"Node", "P2P"} { 25 val, ok := table.Fields[path] 26 if !ok { 27 return 28 } 29 table = val.(*ast.Table) 30 } 31 32 emptyNode := fmt.Sprintf("\"%s\"", asDefault[0]) 33 34 var res = map[string]bool{ 35 "BootstrapNodes": false, 36 "BootstrapNodesV5": false, 37 } 38 for name := range res { 39 if val, ok := table.Fields[name]; ok { 40 kv := val.(*ast.KeyValue) 41 arr := kv.Value.(*ast.Array) 42 if len(arr.Value) == len(asDefault) && arr.Value[0].Source() == emptyNode { 43 res[name] = true 44 delete(table.Fields, name) 45 } 46 } 47 } 48 bootstrapNodes = res["BootstrapNodes"] 49 bootstrapNodesV5 = res["BootstrapNodesV5"] 50 51 return 52 } 53 54 // UnmarshalTOML implements toml.Unmarshaler. 55 func (c *config) UnmarshalTOML(input []byte) error { 56 ast, err := toml.Parse(input) 57 if err != nil { 58 return err 59 } 60 61 defaultBootstrapNodes, defaultBootstrapNodesV5 := isBootstrapNodesDefault(ast) 62 63 type rawCfg config 64 var raw = rawCfg(*c) 65 err = toml.UnmarshalTable(ast, &raw) 66 if err != nil { 67 return err 68 } 69 *c = config(raw) 70 71 if defaultBootstrapNodes { 72 c.Node.P2P.BootstrapNodes = asDefault 73 } 74 if defaultBootstrapNodesV5 { 75 c.Node.P2P.BootstrapNodesV5 = asDefault 76 } 77 78 return nil 79 }