Browse Source

msg.Version

master
Loki Verloren 6 months ago
parent
commit
decfee4c8f

+ 1
- 1
node/serverpeer.go View File

@@ -166,7 +166,7 @@ func (sp *ServerPeer) AddBanScore(ncfg *cnf.Config, persistent, transient uint32
166 166
 // OnVersion is invoked when a peer receives a version bitcoin message
167 167
 // and is used to negotiate the protocol version details as well as kick start
168 168
 // the communications.
169
-func (sp *ServerPeer) OnVersion(ncfg *cnf.Config, _ *peer.Peer, m *msg.MsgVersion) *msg.Reject {
169
+func (sp *ServerPeer) OnVersion(ncfg *cnf.Config, _ *peer.Peer, m *msg.Version) *msg.Reject {
170 170
 	// Update the address manager with the advertised services for outbound
171 171
 	// connections in case they have changed.  This is not done for inbound
172 172
 	// connections to help prevent malicious behavior and is skipped when

+ 1
- 1
peer/example_test.go View File

@@ -77,7 +77,7 @@ func Example_newOutboundPeer() {
77 77
 		Services:         0,
78 78
 		TrickleInterval:  time.Second * 10,
79 79
 		Listeners: peer.MessageListeners{
80
-			OnVersion: func(ncfg *cnf.Config, p *peer.Peer, msg *msg.MsgVersion) *msg.Reject {
80
+			OnVersion: func(ncfg *cnf.Config, p *peer.Peer, msg *msg.Version) *msg.Reject {
81 81
 				fmt.Println("outbound: received version")
82 82
 				return nil
83 83
 			},

+ 1
- 1
peer/log.go View File

@@ -149,7 +149,7 @@ func sanitizeString(str string, maxLength uint) string {
149 149
 // Not all messages have or need a summary.  This is used for debug logging.
150 150
 func messageSummary(mm msg.Message) string {
151 151
 	switch m := mm.(type) {
152
-	case *msg.MsgVersion:
152
+	case *msg.Version:
153 153
 		return fmt.Sprintf("agent %s, pver %d, block %d",
154 154
 			m.UserAgent, m.ProtocolVersion, m.LastBlock)
155 155
 

+ 4
- 4
peer/peer.go View File

@@ -199,7 +199,7 @@ type MessageListeners struct {
199 199
 	// OnVersion is invoked when a peer receives a version bitcoin message.
200 200
 	// The caller may return a reject message in which case the message will
201 201
 	// be sent to the peer and the peer will be disconnected.
202
-	OnVersion func(ncfg *cnf.Config, p *Peer, mm *msg.MsgVersion) *msg.Reject
202
+	OnVersion func(ncfg *cnf.Config, p *Peer, mm *msg.Version) *msg.Reject
203 203
 
204 204
 	// OnVerAck is invoked when a peer receives a verack bitcoin message.
205 205
 	OnVerAck func(ncfg *cnf.Config, p *Peer, mm *msg.VerAck)
@@ -1380,7 +1380,7 @@ out:
1380 1380
 		// Handle each supported message type.
1381 1381
 		p.stallControl <- stallControlMsg{sccHandlerStart, rmsg}
1382 1382
 		switch m := rmsg.(type) {
1383
-		case *msg.MsgVersion:
1383
+		case *msg.Version:
1384 1384
 			// Limit to one version message per peer.
1385 1385
 			p.PushRejectMsg(m.Command(), reject.Duplicate,
1386 1386
 				"duplicate version message", nil, true)
@@ -1899,7 +1899,7 @@ func (p *Peer) readRemoteVersionMsg(ncfg *cnf.Config) error {
1899 1899
 
1900 1900
 	// Notify and disconnect clients if the first message is not a version
1901 1901
 	// message.
1902
-	m, ok := remoteMsg.(*msg.MsgVersion)
1902
+	m, ok := remoteMsg.(*msg.Version)
1903 1903
 	if !ok {
1904 1904
 		reason := "a version message must precede all others"
1905 1905
 		rejectMsg := msg.NewMsgReject(m.Command(), reject.Malformed,
@@ -1986,7 +1986,7 @@ func (p *Peer) readRemoteVersionMsg(ncfg *cnf.Config) error {
1986 1986
 
1987 1987
 // localVersionMsg creates a version message that can be used to send to the
1988 1988
 // remote peer.
1989
-func (p *Peer) localVersionMsg(ncfg *cnf.Config) (*msg.MsgVersion, error) {
1989
+func (p *Peer) localVersionMsg(ncfg *cnf.Config) (*msg.Version, error) {
1990 1990
 	var blockNum int32
1991 1991
 	if p.cfg.NewestBlock != nil {
1992 1992
 		var err error

+ 3
- 3
peer/peer_test.go View File

@@ -444,7 +444,7 @@ func TestPeerListeners(t *testing.T) {
444 444
 			OnMerkleBlock: func(ncfg *cnf.Config, p *peer.Peer, mm *msg.MerkleBlock) {
445 445
 				ok <- mm
446 446
 			},
447
-			OnVersion: func(ncfg *cnf.Config, p *peer.Peer, mm *msg.MsgVersion) *msg.Reject {
447
+			OnVersion: func(ncfg *cnf.Config, p *peer.Peer, mm *msg.Version) *msg.Reject {
448 448
 				ok <- mm
449 449
 				return nil
450 450
 			},
@@ -827,7 +827,7 @@ func TestUnsupportedVersionPeer(t *testing.T) {
827 827
 	// Read version message sent to remote peer
828 828
 	select {
829 829
 	case mm := <-outboundMessages:
830
-		if _, ok := mm.(*msg.MsgVersion); !ok {
830
+		if _, ok := mm.(*msg.Version); !ok {
831 831
 			t.Fatalf("Expected version message, got [%s]", mm.Command())
832 832
 		}
833 833
 	case <-time.After(time.Second):
@@ -914,7 +914,7 @@ func TestDuplicateVersionMsg(t *testing.T) {
914 914
 	// Queue a duplicate version message from the outbound peer and wait until
915 915
 	// it is sent.
916 916
 	done := make(chan struct{})
917
-	outPeer.QueueMessage(&msg.MsgVersion{}, done)
917
+	outPeer.QueueMessage(&msg.Version{}, done)
918 918
 	select {
919 919
 	case <-done:
920 920
 	case <-time.After(time.Second):

+ 2
- 2
wire/msg/message.go View File

@@ -47,7 +47,7 @@ func MakeEmpty(command string) (Message, error) {
47 47
 	var mm Message
48 48
 	switch command {
49 49
 	case CmdVersion:
50
-		mm = &MsgVersion{}
50
+		mm = &Version{}
51 51
 
52 52
 	case CmdVerAck:
53 53
 		mm = &VerAck{}
@@ -358,7 +358,7 @@ func ReadWithEncodingN(r io.Reader, pver uint32, btcnet service.Net,
358 358
 	}
359 359
 
360 360
 	// Unmarshal message.  NOTE: This must be a *bytes.Buffer since the
361
-	// MsgVersion BtcDecode function requires it.
361
+	// Version BtcDecode function requires it.
362 362
 	pr := bytes.NewBuffer(payload)
363 363
 	err = mm.BtcDecode(pr, pver, enc)
364 364
 	if err != nil {

+ 1
- 1
wire/msg/message_test.go View File

@@ -48,7 +48,7 @@ func TestMessage(t *testing.T) {
48 48
 
49 49
 	// Create the various types of messages to test.
50 50
 
51
-	// MsgVersion.
51
+	// Version.
52 52
 	addrYou := &net.TCPAddr{IP: net.ParseIP("192.168.0.1"), Port: 8333}
53 53
 	you := na.NewNetAddress(addrYou, service.NodeNetwork)
54 54
 	you.Timestamp = time.Time{} // Version message has zero value timestamp.

+ 1
- 1
wire/msg/msgverack.go View File

@@ -9,7 +9,7 @@ import (
9 9
 )
10 10
 
11 11
 // VerAck defines a bitcoin verack message which is used for a peer to
12
-// acknowledge a version message (MsgVersion) after it has used the information
12
+// acknowledge a version message (Version) after it has used the information
13 13
 // to negotiate parameters.  It implements the Message interface.
14 14
 //
15 15
 // This message has no payload.

+ 14
- 14
wire/msg/msgversion.go View File

@@ -18,20 +18,20 @@ import (
18 18
 )
19 19
 
20 20
 // MaxUserAgentLen is the maximum allowed length for the user agent field in a
21
-// version message (MsgVersion).
21
+// version message (Version).
22 22
 const MaxUserAgentLen = 256
23 23
 
24 24
 // DefaultUserAgent for wire in the stack
25 25
 const DefaultUserAgent = "/btcwire:0.5.0/"
26 26
 
27
-// MsgVersion implements the Message interface and represents a bitcoin version
27
+// Version implements the Message interface and represents a bitcoin version
28 28
 // message.  It is used for a peer to advertise itself as soon as an outbound
29 29
 // connection is made.  The remote peer then uses this information along with
30 30
 // its own to negotiate.  The remote peer must then respond with a version
31 31
 // message of its own containing the negotiated values followed by a verack
32 32
 // message (VerAck).  This exchange must take place before any further
33 33
 // communication is allowed to proceed.
34
-type MsgVersion struct {
34
+type Version struct {
35 35
 	// Version of the protocol the node is using.
36 36
 	ProtocolVersion int32
37 37
 
@@ -64,13 +64,13 @@ type MsgVersion struct {
64 64
 
65 65
 // HasService returns whether the specified service is supported by the peer
66 66
 // that generated the message.
67
-func (m *MsgVersion) HasService(ss service.Flag) bool {
67
+func (m *Version) HasService(ss service.Flag) bool {
68 68
 	return m.Services&ss == ss
69 69
 }
70 70
 
71 71
 // AddService adds service as a supported service by the peer generating the
72 72
 // message.
73
-func (m *MsgVersion) AddService(ss service.Flag) {
73
+func (m *Version) AddService(ss service.Flag) {
74 74
 	m.Services |= ss
75 75
 }
76 76
 
@@ -81,10 +81,10 @@ func (m *MsgVersion) AddService(ss service.Flag) {
81 81
 // *bytes.Buffer so the number of remaining bytes can be ascertained.
82 82
 //
83 83
 // This is part of the Message interface implementation.
84
-func (m *MsgVersion) BtcDecode(r io.Reader, pver uint32, enc Encoding) error {
84
+func (m *Version) BtcDecode(r io.Reader, pver uint32, enc Encoding) error {
85 85
 	buf, ok := r.(*bytes.Buffer)
86 86
 	if !ok {
87
-		return fmt.Errorf("MsgVersion.BtcDecode reader is not a " +
87
+		return fmt.Errorf("Version.BtcDecode reader is not a " +
88 88
 			"*bytes.Buffer")
89 89
 	}
90 90
 
@@ -154,7 +154,7 @@ func (m *MsgVersion) BtcDecode(r io.Reader, pver uint32, enc Encoding) error {
154 154
 
155 155
 // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
156 156
 // This is part of the Message interface implementation.
157
-func (m *MsgVersion) BtcEncode(w io.Writer, pver uint32, enc Encoding) error {
157
+func (m *Version) BtcEncode(w io.Writer, pver uint32, enc Encoding) error {
158 158
 	err := validateUserAgent(m.UserAgent)
159 159
 	if err != nil {
160 160
 		return err
@@ -205,13 +205,13 @@ func (m *MsgVersion) BtcEncode(w io.Writer, pver uint32, enc Encoding) error {
205 205
 
206 206
 // Command returns the protocol command string for the message.  This is part
207 207
 // of the Message interface implementation.
208
-func (m *MsgVersion) Command() string {
208
+func (m *Version) Command() string {
209 209
 	return CmdVersion
210 210
 }
211 211
 
212 212
 // MaxPayloadLength returns the maximum length the payload can be for the
213 213
 // receiver.  This is part of the Message interface implementation.
214
-func (m *MsgVersion) MaxPayloadLength(pver uint32) uint32 {
214
+func (m *Version) MaxPayloadLength(pver uint32) uint32 {
215 215
 	// XXX: <= 106 different
216 216
 
217 217
 	// Protocol version 4 bytes + services 8 bytes + timestamp 8 bytes +
@@ -226,11 +226,11 @@ func (m *MsgVersion) MaxPayloadLength(pver uint32) uint32 {
226 226
 // Message interface using the passed parameters and defaults for the remaining
227 227
 // fields.
228 228
 func NewMsgVersion(me *na.NetAddress, you *na.NetAddress, nonce uint64,
229
-	lastBlock int32) *MsgVersion {
229
+	lastBlock int32) *Version {
230 230
 
231 231
 	// Limit the timestamp to one second precision since the protocol
232 232
 	// doesn't support better.
233
-	return &MsgVersion{
233
+	return &Version{
234 234
 		ProtocolVersion: int32(version.Protocol),
235 235
 		Services:        0,
236 236
 		Timestamp:       time.Unix(time.Now().Unix(), 0),
@@ -248,7 +248,7 @@ func validateUserAgent(userAgent string) error {
248 248
 	if len(userAgent) > MaxUserAgentLen {
249 249
 		str := fmt.Sprintf("user agent too long [len %v, max %v]",
250 250
 			len(userAgent), MaxUserAgentLen)
251
-		return NewError("MsgVersion", str)
251
+		return NewError("Version", str)
252 252
 	}
253 253
 	return nil
254 254
 }
@@ -256,7 +256,7 @@ func validateUserAgent(userAgent string) error {
256 256
 // AddUserAgent adds a user agent to the user agent string for the version
257 257
 // message.  The version string is not defined to any strict format, although
258 258
 // it is recommended to use the form "major.minor.revision" e.g. "2.6.41".
259
-func (m *MsgVersion) AddUserAgent(name string, version string,
259
+func (m *Version) AddUserAgent(name string, version string,
260 260
 	comments ...string) error {
261 261
 
262 262
 	newUserAgent := fmt.Sprintf("%s:%s", name, version)

+ 16
- 16
wire/msg/msgversion_test.go View File

@@ -23,7 +23,7 @@ import (
23 23
 	"github.com/davecgh/go-spew/spew"
24 24
 )
25 25
 
26
-// TestVersion tests the MsgVersion API.
26
+// TestVersion tests the Version API.
27 27
 func TestVersion(t *testing.T) {
28 28
 	pver := version.Protocol
29 29
 
@@ -133,7 +133,7 @@ func TestVersion(t *testing.T) {
133 133
 	}
134 134
 }
135 135
 
136
-// TestVersionWire tests the MsgVersion wire encode and decode for various
136
+// TestVersionWire tests the Version wire encode and decode for various
137 137
 // protocol versions.
138 138
 func TestVersionWire(t *testing.T) {
139 139
 	// verRelayTxFalse and verRelayTxFalseEncoded is a version message as of
@@ -146,8 +146,8 @@ func TestVersionWire(t *testing.T) {
146 146
 	verRelayTxFalseEncoded[len(verRelayTxFalseEncoded)-1] = 0
147 147
 
148 148
 	tests := []struct {
149
-		in   *MsgVersion // Message to encode
150
-		out  *MsgVersion // Expected decoded message
149
+		in   *Version // Message to encode
150
+		out  *Version // Expected decoded message
151 151
 		buf  []byte      // Wire encoding
152 152
 		pver uint32      // Protocol version for wire encoding
153 153
 		enc  Encoding    // Message encoding format
@@ -234,7 +234,7 @@ func TestVersionWire(t *testing.T) {
234 234
 		}
235 235
 
236 236
 		// Decode the message from wire format.
237
-		var mm MsgVersion
237
+		var mm Version
238 238
 		rbuf := bytes.NewBuffer(test.buf)
239 239
 		err = mm.BtcDecode(rbuf, test.pver, test.enc)
240 240
 		if err != nil {
@@ -259,12 +259,12 @@ func TestVersionWireErrors(t *testing.T) {
259 259
 	enc := Base
260 260
 	wireErr := &Error{}
261 261
 
262
-	// Ensure calling MsgVersion.BtcDecode with a non *bytes.Buffer returns
262
+	// Ensure calling Version.BtcDecode with a non *bytes.Buffer returns
263 263
 	// error.
264 264
 	fr := fixed.NewReader(0, []byte{})
265 265
 	if err := baseVersion.BtcDecode(fr, pver, enc); err == nil {
266 266
 		t.Errorf("Did not received error when calling " +
267
-			"MsgVersion.BtcDecode with non *bytes.Buffer")
267
+			"Version.BtcDecode with non *bytes.Buffer")
268 268
 	}
269 269
 
270 270
 	// Copy the base version and change the user agent to exceed max limits.
@@ -292,7 +292,7 @@ func TestVersionWireErrors(t *testing.T) {
292 292
 	copy(exceedUAVerEncoded[83+len(newUA):], baseVersionEncoded[97:100])
293 293
 
294 294
 	tests := []struct {
295
-		in       *MsgVersion // Value to encode
295
+		in       *Version // Value to encode
296 296
 		buf      []byte      // Wire encoding
297 297
 		pver     uint32      // Protocol version for wire encoding
298 298
 		enc      Encoding    // Message encoding format
@@ -350,7 +350,7 @@ func TestVersionWireErrors(t *testing.T) {
350 350
 		}
351 351
 
352 352
 		// Decode from wire format.
353
-		var mm MsgVersion
353
+		var mm Version
354 354
 		buf := bytes.NewBuffer(test.buf[0:test.max])
355 355
 		err = mm.BtcDecode(buf, test.pver, test.enc)
356 356
 		if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
@@ -376,7 +376,7 @@ func TestVersionWireErrors(t *testing.T) {
376 376
 func TestVersionOptionalFields(t *testing.T) {
377 377
 	// onlyRequiredVersion is a version message that only contains the
378 378
 	// required versions and all other values set to their default values.
379
-	onlyRequiredVersion := MsgVersion{
379
+	onlyRequiredVersion := Version{
380 380
 		ProtocolVersion: 60002,
381 381
 		Services:        service.NodeNetwork,
382 382
 		Timestamp:       time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST)
@@ -424,7 +424,7 @@ func TestVersionOptionalFields(t *testing.T) {
424 424
 	copy(lastBlockVersionEncoded, baseVersionEncoded)
425 425
 
426 426
 	tests := []struct {
427
-		msg  *MsgVersion // Expected message
427
+		msg  *Version // Expected message
428 428
 		buf  []byte      // Wire encoding
429 429
 		pver uint32      // Protocol version for wire encoding
430 430
 		enc  Encoding    // Message encoding format
@@ -463,7 +463,7 @@ func TestVersionOptionalFields(t *testing.T) {
463 463
 
464 464
 	for i, test := range tests {
465 465
 		// Decode the message from wire format.
466
-		var mm MsgVersion
466
+		var mm Version
467 467
 		rbuf := bytes.NewBuffer(test.buf)
468 468
 		err := mm.BtcDecode(rbuf, test.pver, test.enc)
469 469
 		if err != nil {
@@ -478,8 +478,8 @@ func TestVersionOptionalFields(t *testing.T) {
478 478
 	}
479 479
 }
480 480
 
481
-// baseVersion is used in the various tests as a baseline MsgVersion.
482
-var baseVersion = &MsgVersion{
481
+// baseVersion is used in the various tests as a baseline Version.
482
+var baseVersion = &Version{
483 483
 	ProtocolVersion: 60002,
484 484
 	Services:        service.NodeNetwork,
485 485
 	Timestamp:       time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST)
@@ -523,9 +523,9 @@ var baseVersionEncoded = []byte{
523 523
 	0xfa, 0x92, 0x03, 0x00, // Last block
524 524
 }
525 525
 
526
-// baseVersionBIP0037 is used in the various tests as a baseline MsgVersion for
526
+// baseVersionBIP0037 is used in the various tests as a baseline Version for
527 527
 // BIP0037.
528
-var baseVersionBIP0037 = &MsgVersion{
528
+var baseVersionBIP0037 = &Version{
529 529
 	ProtocolVersion: 70001,
530 530
 	Services:        service.NodeNetwork,
531 531
 	Timestamp:       time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST)

Loading…
Cancel
Save