From f44d32bd2dff0566419fe35952050efa2d84cf8a Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 3 Jun 2025 17:01:05 -0700 Subject: [PATCH] v2transport: reduce size of module by shedding dep on wire package In this commit, we reduce the size of the v2transport module by shedding the dep on the wire package. This allows us to only depend on btcec/v2, which itself is a sub module, instead of the entire btcd module. --- peer/peer.go | 9 ++++++--- v2transport/go.mod | 1 - v2transport/go.sum | 8 -------- v2transport/transport.go | 16 +++++++++++----- v2transport/transport_test.go | 7 +++++-- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/peer/peer.go b/peer/peer.go index 2e5d6ff2..0dc129ce 100644 --- a/peer/peer.go +++ b/peer/peer.go @@ -2279,7 +2279,8 @@ func (p *Peer) negotiateInboundProtocol() error { if p.cfg.UsingV2Conn { garbageLen := rand.Intn(v2transport.MaxGarbageLen + 1) err := p.V2Transport.RespondV2Handshake( - garbageLen, p.cfg.ChainParams.Net, + garbageLen, + v2transport.BitcoinNet(p.cfg.ChainParams.Net), ) switch { case errors.Is(err, v2transport.ErrUseV1Protocol): @@ -2295,7 +2296,8 @@ func (p *Peer) negotiateInboundProtocol() error { default: err = p.V2Transport.CompleteHandshake( - false, nil, p.cfg.ChainParams.Net, + false, nil, + v2transport.BitcoinNet(p.cfg.ChainParams.Net), ) if err != nil { return err @@ -2356,7 +2358,8 @@ func (p *Peer) negotiateOutboundProtocol() error { } err = p.V2Transport.CompleteHandshake( - true, nil, p.cfg.ChainParams.Net, + true, nil, + v2transport.BitcoinNet(p.cfg.ChainParams.Net), ) if errors.Is(err, v2transport.ErrShouldDowngradeToV1) { log.Infof("Outbound v2 connection attempt to %s "+ diff --git a/v2transport/go.mod b/v2transport/go.mod index 8e616519..9373d00b 100644 --- a/v2transport/go.mod +++ b/v2transport/go.mod @@ -3,7 +3,6 @@ module v2transport go 1.23.2 require ( - github.com/btcsuite/btcd v0.24.2 github.com/btcsuite/btcd/btcec/v2 v2.3.4 github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f golang.org/x/crypto v0.25.0 diff --git a/v2transport/go.sum b/v2transport/go.sum index 14b1fbae..31fc7462 100644 --- a/v2transport/go.sum +++ b/v2transport/go.sum @@ -1,5 +1,3 @@ -github.com/btcsuite/btcd v0.24.2 h1:aLmxPguqxza+4ag8R1I2nnJjSu2iFn/kqtHTIImswcY= -github.com/btcsuite/btcd v0.24.2/go.mod h1:5C8ChTkl5ejr3WHj8tkQSCmydiMEPB0ZhQhehpq7Dgg= github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 h1:59Kx4K6lzOW5w6nFlA0v5+lk/6sjybR934QNHSJZPTQ= github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f h1:bAs4lUbRJpnnkd9VhRV3jjAVU7DJVjMaK+IsvSeZvFo= @@ -9,13 +7,7 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/v2transport/transport.go b/v2transport/transport.go index 0fcc5d7f..d9500581 100644 --- a/v2transport/transport.go +++ b/v2transport/transport.go @@ -13,7 +13,6 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcec/v2/ellswift" - "github.com/btcsuite/btcd/wire" ) // packetBit is a type used to represent the bits in the packet's header. @@ -25,6 +24,13 @@ const ( ignoreBitPos packetBit = 7 ) +// BitcoinNet is a type used to represent the Bitcoin network that we're +// connecting to. +// +// NOTE: This is identical to the wire.BitcoinNet type, but allows us to shed a +// large module dependency. +type BitcoinNet uint32 + const ( // garbageSize is the length in bytes of the garbage terminator that // each party sends. @@ -179,7 +185,7 @@ func NewPeer() *Peer { // createV2Ciphers constructs the packet-length and packet encryption ciphers. func (p *Peer) createV2Ciphers(ecdhSecret []byte, initiating bool, - net wire.BitcoinNet) error { + net BitcoinNet) error { log.Debugf("Creating v2 ciphers (initiating=%v, net=%v)", initiating, net) @@ -376,7 +382,7 @@ func (p *Peer) InitiateV2Handshake(garbageLen int) error { // wants to use the v2 protocol and if so returns our ElligatorSwift-encoded // public key followed by our garbage data over. If the initiator does not want // to use the v2 protocol, we'll instead revert to the v1 protocol. -func (p *Peer) RespondV2Handshake(garbageLen int, net wire.BitcoinNet) error { +func (p *Peer) RespondV2Handshake(garbageLen int, net BitcoinNet) error { v1Prefix := createV1Prefix(net) log.Debugf("Responding to v2 handshake (garbageLen=%d, net=%v)", @@ -474,7 +480,7 @@ func (p *Peer) generateKeyAndGarbage(garbageLen int) ([]byte, error) { // createV1Prefix is a helper function that returns the first 16 bytes of the // version message's header. -func createV1Prefix(net wire.BitcoinNet) []byte { +func createV1Prefix(net BitcoinNet) []byte { v1Prefix := make([]byte, 0, 4+12) // The v1 transport protocol uses the network's 4 magic bytes followed by @@ -493,7 +499,7 @@ func createV1Prefix(net wire.BitcoinNet) []byte { // CompleteHandshake finishes the v2 protocol negotiation and optionally sends // decoy packets after sending the garbage terminator. func (p *Peer) CompleteHandshake(initiating bool, decoyContentLens []int, - btcnet wire.BitcoinNet) error { + btcnet BitcoinNet) error { log.Debugf("Completing v2 handshake (initiating=%v, "+ "num_decoys=%d, net=%v)", initiating, len(decoyContentLens), diff --git a/v2transport/transport_test.go b/v2transport/transport_test.go index 54e384bf..97fa7a27 100644 --- a/v2transport/transport_test.go +++ b/v2transport/transport_test.go @@ -8,7 +8,6 @@ import ( "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcec/v2/ellswift" - "github.com/btcsuite/btcd/wire" ) func setHex(hexString string) *btcec.FieldVal { @@ -23,6 +22,10 @@ func setHex(hexString string) *btcec.FieldVal { return &f } +const ( + mainNet = 0xd9b4bef9 +) + func TestPacketEncodingVectors(t *testing.T) { tests := []struct { inIdx int @@ -366,7 +369,7 @@ func TestPacketEncodingVectors(t *testing.T) { buf := bytes.NewBuffer(nil) p.UseReadWriter(buf) - err = p.createV2Ciphers(midShared, inInitiating, wire.MainNet) + err = p.createV2Ciphers(midShared, inInitiating, mainNet) if err != nil { t.Fatalf("error initiating v2 transport") }