• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 14193549836

01 Apr 2025 10:40AM UTC coverage: 69.046% (+0.007%) from 69.039%
14193549836

Pull #9665

github

web-flow
Merge e8825f209 into b01f4e514
Pull Request #9665: kvdb: bump etcd libs to v3.5.12

133439 of 193262 relevant lines covered (69.05%)

22119.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/watchtower/wtwire/init.go
1
package wtwire
2

3
import (
4
        "fmt"
5
        "io"
6

7
        "github.com/btcsuite/btcd/chaincfg/chainhash"
8
        "github.com/lightningnetwork/lnd/feature"
9
        "github.com/lightningnetwork/lnd/lnwire"
10
)
11

12
// Init is the first message sent over the watchtower wire protocol, and
13
// specifies connection features bits and level of requiredness maintained by
14
// the sending node. The Init message also sends the chain hash identifying the
15
// network that the sender is on.
16
type Init struct {
17
        // ConnFeatures are the feature bits being advertised for the duration
18
        // of a single connection with a peer.
19
        ConnFeatures *lnwire.RawFeatureVector
20

21
        // ChainHash is the genesis hash of the chain that the advertiser claims
22
        // to be on.
23
        ChainHash chainhash.Hash
24
}
25

26
// NewInitMessage generates a new Init message from a raw connection feature
27
// vector and chain hash.
28
func NewInitMessage(connFeatures *lnwire.RawFeatureVector,
29
        chainHash chainhash.Hash) *Init {
346✔
30

346✔
31
        return &Init{
346✔
32
                ConnFeatures: connFeatures,
346✔
33
                ChainHash:    chainHash,
346✔
34
        }
346✔
35
}
346✔
36

37
// Encode serializes the target Init into the passed io.Writer observing the
38
// protocol version specified.
39
//
40
// This is part of the wtwire.Message interface.
41
func (msg *Init) Encode(w io.Writer, pver uint32) error {
1,056✔
42
        return WriteElements(w,
1,056✔
43
                msg.ConnFeatures,
1,056✔
44
                msg.ChainHash,
1,056✔
45
        )
1,056✔
46
}
1,056✔
47

48
// Decode deserializes a serialized Init message stored in the passed io.Reader
49
// observing the specified protocol version.
50
//
51
// This is part of the wtwire.Message interface.
52
func (msg *Init) Decode(r io.Reader, pver uint32) error {
1,083✔
53
        return ReadElements(r,
1,083✔
54
                &msg.ConnFeatures,
1,083✔
55
                &msg.ChainHash,
1,083✔
56
        )
1,083✔
57
}
1,083✔
58

59
// MsgType returns the integer uniquely identifying this message type on the
60
// wire.
61
//
62
// This is part of the wtwire.Message interface.
63
func (msg *Init) MsgType() MessageType {
2,890✔
64
        return MsgInit
2,890✔
65
}
2,890✔
66

67
// MaxPayloadLength returns the maximum allowed payload size for an Init
68
// complete message observing the specified protocol version.
69
//
70
// This is part of the wtwire.Message interface.
71
func (msg *Init) MaxPayloadLength(uint32) uint32 {
1,185✔
72
        return MaxMessagePayload
1,185✔
73
}
1,185✔
74

75
// A compile-time constraint to ensure Init implements the Message interface.
76
var _ Message = (*Init)(nil)
77

78
// CheckRemoteInit performs basic validation of the remote party's Init message.
79
// This method checks that the remote Init's chain hash matches our advertised
80
// chain hash and that the remote Init does not contain any required feature
81
// bits that we don't understand.
82
func (msg *Init) CheckRemoteInit(remoteInit *Init,
83
        featureNames map[lnwire.FeatureBit]string) error {
910✔
84

910✔
85
        // Check that the remote peer is on the same chain.
910✔
86
        if msg.ChainHash != remoteInit.ChainHash {
912✔
87
                return NewErrUnknownChainHash(remoteInit.ChainHash)
2✔
88
        }
2✔
89

90
        remoteConnFeatures := lnwire.NewFeatureVector(
908✔
91
                remoteInit.ConnFeatures, featureNames,
908✔
92
        )
908✔
93

908✔
94
        // Check that the remote peer doesn't have any required connection
908✔
95
        // feature bits that we ourselves are unaware of.
908✔
96
        return feature.ValidateRequired(remoteConnFeatures)
908✔
97
}
98

99
// ErrUnknownChainHash signals that the remote Init has a different chain hash
100
// from the one we advertised.
101
type ErrUnknownChainHash struct {
102
        hash chainhash.Hash
103
}
104

105
// NewErrUnknownChainHash creates an ErrUnknownChainHash using the remote Init's
106
// chain hash.
107
func NewErrUnknownChainHash(hash chainhash.Hash) *ErrUnknownChainHash {
4✔
108
        return &ErrUnknownChainHash{hash}
4✔
109
}
4✔
110

111
// Error returns a human-readable error displaying the unknown chain hash.
112
func (e *ErrUnknownChainHash) Error() string {
4✔
113
        return fmt.Sprintf("remote init has unknown chain hash: %s", e.hash)
4✔
114
}
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc