github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/wire/msgpong.go (about) 1 // Copyright (c) 2013-2015 The btcsuite developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package wire 6 7 import ( 8 "fmt" 9 "io" 10 ) 11 12 // MsgPong implements the Message interface and represents a bitcoin pong 13 // message which is used primarily to confirm that a connection is still valid 14 // in response to a bitcoin ping message (MsgPing). 15 // 16 // This message was not added until protocol versions AFTER BIP0031Version. 17 type MsgPong struct { 18 // Unique value associated with message that is used to identify 19 // specific ping message. 20 Nonce uint64 21 } 22 23 // BtcDecode decodes r using the bitcoin protocol encoding into the receiver. 24 // This is part of the Message interface implementation. 25 func (msg *MsgPong) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error { 26 // NOTE: <= is not a mistake here. The BIP0031 was defined as AFTER 27 // the version unlike most others. 28 if pver <= BIP0031Version { 29 str := fmt.Sprintf("pong message invalid for protocol "+ 30 "version %d", pver) 31 return messageError("MsgPong.BtcDecode", str) 32 } 33 34 err := readElement(r, &msg.Nonce) 35 if err != nil { 36 return err 37 } 38 39 return nil 40 } 41 42 // BtcEncode encodes the receiver to w using the bitcoin protocol encoding. 43 // This is part of the Message interface implementation. 44 func (msg *MsgPong) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error { 45 // NOTE: <= is not a mistake here. The BIP0031 was defined as AFTER 46 // the version unlike most others. 47 if pver <= BIP0031Version { 48 str := fmt.Sprintf("pong message invalid for protocol "+ 49 "version %d", pver) 50 return messageError("MsgPong.BtcEncode", str) 51 } 52 53 err := writeElement(w, msg.Nonce) 54 if err != nil { 55 return err 56 } 57 58 return nil 59 } 60 61 // Command returns the protocol command string for the message. This is part 62 // of the Message interface implementation. 63 func (msg *MsgPong) Command() string { 64 return CmdPong 65 } 66 67 // MaxPayloadLength returns the maximum length the payload can be for the 68 // receiver. This is part of the Message interface implementation. 69 func (msg *MsgPong) MaxPayloadLength(pver uint32) uint32 { 70 plen := uint32(0) 71 // The pong message did not exist for BIP0031Version and earlier. 72 // NOTE: > is not a mistake here. The BIP0031 was defined as AFTER 73 // the version unlike most others. 74 if pver > BIP0031Version { 75 // Nonce 8 bytes. 76 plen += 8 77 } 78 79 return plen 80 } 81 82 // NewMsgPong returns a new bitcoin pong message that conforms to the Message 83 // interface. See MsgPong for details. 84 func NewMsgPong(nonce uint64) *MsgPong { 85 return &MsgPong{ 86 Nonce: nonce, 87 } 88 }