MCPcopy Create free account
hub / github.com/DeAI-Artist/Linkis / Validate

Method Validate

p2p/node_info.go:122–174  ·  view source on GitHub ↗

Validate checks the self-reported DefaultNodeInfo is safe. It returns an error if there are too many Channels, if there are any duplicate Channels, if the ListenAddr is malformed, or if the ListenAddr is a host name that can not be resolved to some IP. TODO: constraints for Moniker/Other? Or is that

()

Source from the content-addressed store, hash-verified

120// url-encoding), and we just need to be careful with how we handle that in our
121// clients. (e.g. off by default).
122func (info DefaultNodeInfo) Validate() error {
123
124 // ID is already validated.
125
126 // Validate ListenAddr.
127 _, err := NewNetAddressString(IDAddressString(info.ID(), info.ListenAddr))
128 if err != nil {
129 return err
130 }
131
132 // Network is validated in CompatibleWith.
133
134 // Validate Version
135 if len(info.Version) > 0 &&
136 (!tmstrings.IsASCIIText(info.Version) || tmstrings.ASCIITrim(info.Version) == "") {
137
138 return fmt.Errorf("info.Version must be valid ASCII text without tabs, but got %v", info.Version)
139 }
140
141 // Validate Channels - ensure max and check for duplicates.
142 if len(info.Channels) > maxNumChannels {
143 return fmt.Errorf("info.Channels is too long (%v). Max is %v", len(info.Channels), maxNumChannels)
144 }
145 channels := make(map[byte]struct{})
146 for _, ch := range info.Channels {
147 _, ok := channels[ch]
148 if ok {
149 return fmt.Errorf("info.Channels contains duplicate channel id %v", ch)
150 }
151 channels[ch] = struct{}{}
152 }
153
154 // Validate Moniker.
155 if !tmstrings.IsASCIIText(info.Moniker) || tmstrings.ASCIITrim(info.Moniker) == "" {
156 return fmt.Errorf("info.Moniker must be valid non-empty ASCII text without tabs, but got %v", info.Moniker)
157 }
158
159 // Validate Other.
160 other := info.Other
161 txIndex := other.TxIndex
162 switch txIndex {
163 case "", "on", "off":
164 default:
165 return fmt.Errorf("info.Other.TxIndex should be either 'on', 'off', or empty string, got '%v'", txIndex)
166 }
167 // XXX: Should we be more strict about address formats?
168 rpcAddr := other.RPCAddress
169 if len(rpcAddr) > 0 && (!tmstrings.IsASCIIText(rpcAddr) || tmstrings.ASCIITrim(rpcAddr) == "") {
170 return fmt.Errorf("info.Other.RPCAddress=%v must be valid ASCII text without tabs", rpcAddr)
171 }
172
173 return nil
174}
175
176// CompatibleWith checks if two DefaultNodeInfo are compatible with eachother.
177// CONTRACT: two nodes are compatible if the Block version and network match

Callers 3

TestNodeInfoValidateFunction · 0.95
makeNodeInfoFunction · 0.95
makeNodeInfoFunction · 0.95

Calls 3

IDMethod · 0.95
NewNetAddressStringFunction · 0.85
IDAddressStringFunction · 0.85

Tested by 1

TestNodeInfoValidateFunction · 0.76