config: Add user agent comments flag --uacomment

Just like Core's -uacomment, this flag allows to specify user agent
comments like defined in BIP 14.
This commit is contained in:
Steven Roose 2017-04-14 03:08:05 +02:00
parent 40f4997b95
commit 53f55a4634
6 changed files with 53 additions and 25 deletions

View file

@ -141,6 +141,7 @@ type config struct {
BlockMinSize uint32 `long:"blockminsize" description:"Mininum block size in bytes to be used when creating a block"` BlockMinSize uint32 `long:"blockminsize" description:"Mininum block size in bytes to be used when creating a block"`
BlockMaxSize uint32 `long:"blockmaxsize" description:"Maximum block size in bytes to be used when creating a block"` BlockMaxSize uint32 `long:"blockmaxsize" description:"Maximum block size in bytes to be used when creating a block"`
BlockPrioritySize uint32 `long:"blockprioritysize" description:"Size in bytes for high-priority/low-fee transactions when creating a block"` BlockPrioritySize uint32 `long:"blockprioritysize" description:"Size in bytes for high-priority/low-fee transactions when creating a block"`
UserAgentComments []string `long:"uacomment" description:"Comment to add to the user agent -- See BIP 14 for more information."`
NoPeerBloomFilters bool `long:"nopeerbloomfilters" description:"Disable bloom filtering support"` NoPeerBloomFilters bool `long:"nopeerbloomfilters" description:"Disable bloom filtering support"`
SigCacheMaxSize uint `long:"sigcachemaxsize" description:"The maximum number of entries in the signature verification cache"` SigCacheMaxSize uint `long:"sigcachemaxsize" description:"The maximum number of entries in the signature verification cache"`
BlocksOnly bool `long:"blocksonly" description:"Do not accept transactions from remote peers."` BlocksOnly bool `long:"blocksonly" description:"Do not accept transactions from remote peers."`
@ -736,6 +737,18 @@ func loadConfig() (*config, []string, error) {
cfg.BlockPrioritySize = minUint32(cfg.BlockPrioritySize, cfg.BlockMaxSize) cfg.BlockPrioritySize = minUint32(cfg.BlockPrioritySize, cfg.BlockMaxSize)
cfg.BlockMinSize = minUint32(cfg.BlockMinSize, cfg.BlockMaxSize) cfg.BlockMinSize = minUint32(cfg.BlockMinSize, cfg.BlockMaxSize)
// Look for illegal characters in the user agent comments.
for _, uaComment := range cfg.UserAgentComments {
if strings.ContainsAny(uaComment, "/:()") {
err := fmt.Errorf("%s: The following characters must not "+
"appear in user agent comments: '/', ':', '(', ')'",
funcName)
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err
}
}
// --txindex and --droptxindex do not mix. // --txindex and --droptxindex do not mix.
if cfg.TxIndex && cfg.DropTxIndex { if cfg.TxIndex && cfg.DropTxIndex {
err := fmt.Errorf("%s: the --txindex and --droptxindex "+ err := fmt.Errorf("%s: the --txindex and --droptxindex "+

2
doc.go
View file

@ -77,6 +77,8 @@ Application Options:
--addcheckpoint= Add a custom checkpoint. Format: '<height>:<hash>' --addcheckpoint= Add a custom checkpoint. Format: '<height>:<hash>'
--nocheckpoints Disable built-in checkpoints. Don't do this unless --nocheckpoints Disable built-in checkpoints. Don't do this unless
you know what you're doing. you know what you're doing.
--uacomment= Comment to add to the user agent --
See BIP 14 for more information.
--dbtype= Database backend to use for the Block Chain (ffldb) --dbtype= Database backend to use for the Block Chain (ffldb)
--profile= Enable HTTP profiling on given port -- NOTE port --profile= Enable HTTP profiling on given port -- NOTE port
must be between 1024 and 65536 must be between 1024 and 65536

View file

@ -214,6 +214,11 @@ type Config struct {
// form "major.minor.revision" e.g. "2.6.41". // form "major.minor.revision" e.g. "2.6.41".
UserAgentVersion string UserAgentVersion string
// UserAgentComments specify the user agent comments to advertise. These
// values must not contain the illegal characters specified in BIP 14:
// '/', ':', '(', ')'.
UserAgentComments []string
// ChainParams identifies which chain parameters the peer is associated // ChainParams identifies which chain parameters the peer is associated
// with. It is highly recommended to specify this field, however it can // with. It is highly recommended to specify this field, however it can
// be omitted in which case the test network will be used. // be omitted in which case the test network will be used.
@ -799,7 +804,8 @@ func (p *Peer) localVersionMsg() (*wire.MsgVersion, error) {
// Version message. // Version message.
msg := wire.NewMsgVersion(ourNA, theirNA, nonce, blockNum) msg := wire.NewMsgVersion(ourNA, theirNA, nonce, blockNum)
msg.AddUserAgent(p.cfg.UserAgentName, p.cfg.UserAgentVersion) msg.AddUserAgent(p.cfg.UserAgentName, p.cfg.UserAgentVersion,
p.cfg.UserAgentComments...)
// XXX: bitcoind appears to always enable the full node services flag // XXX: bitcoind appears to always enable the full node services flag
// of the remote peer netaddress field in the version message regardless // of the remote peer netaddress field in the version message regardless

View file

@ -218,13 +218,14 @@ func TestPeerConnection(t *testing.T) {
} }
}, },
}, },
UserAgentName: "peer", UserAgentName: "peer",
UserAgentVersion: "1.0", UserAgentVersion: "1.0",
ChainParams: &chaincfg.MainNetParams, UserAgentComments: []string{"comment"},
Services: 0, ChainParams: &chaincfg.MainNetParams,
Services: 0,
} }
wantStats := peerStats{ wantStats := peerStats{
wantUserAgent: wire.DefaultUserAgent + "peer:1.0/", wantUserAgent: wire.DefaultUserAgent + "peer:1.0(comment)/",
wantServices: 0, wantServices: 0,
wantProtocolVersion: peer.MaxProtocolVersion, wantProtocolVersion: peer.MaxProtocolVersion,
wantConnected: true, wantConnected: true,
@ -234,8 +235,8 @@ func TestPeerConnection(t *testing.T) {
wantLastPingNonce: uint64(0), wantLastPingNonce: uint64(0),
wantLastPingMicros: int64(0), wantLastPingMicros: int64(0),
wantTimeOffset: int64(0), wantTimeOffset: int64(0),
wantBytesSent: 158, // 134 version + 24 verack wantBytesSent: 167, // 143 version + 24 verack
wantBytesReceived: 158, wantBytesReceived: 167,
} }
tests := []struct { tests := []struct {
name string name string
@ -387,10 +388,11 @@ func TestPeerListeners(t *testing.T) {
ok <- msg ok <- msg
}, },
}, },
UserAgentName: "peer", UserAgentName: "peer",
UserAgentVersion: "1.0", UserAgentVersion: "1.0",
ChainParams: &chaincfg.MainNetParams, UserAgentComments: []string{"comment"},
Services: wire.SFNodeBloom, ChainParams: &chaincfg.MainNetParams,
Services: wire.SFNodeBloom,
} }
inConn, outConn := pipe( inConn, outConn := pipe(
&conn{raddr: "10.0.0.1:8333"}, &conn{raddr: "10.0.0.1:8333"},
@ -535,10 +537,11 @@ func TestOutboundPeer(t *testing.T) {
NewestBlock: func() (*chainhash.Hash, int32, error) { NewestBlock: func() (*chainhash.Hash, int32, error) {
return nil, 0, errors.New("newest block not found") return nil, 0, errors.New("newest block not found")
}, },
UserAgentName: "peer", UserAgentName: "peer",
UserAgentVersion: "1.0", UserAgentVersion: "1.0",
ChainParams: &chaincfg.MainNetParams, UserAgentComments: []string{"comment"},
Services: 0, ChainParams: &chaincfg.MainNetParams,
Services: 0,
} }
r, w := io.Pipe() r, w := io.Pipe()

View file

@ -156,6 +156,9 @@
; Add additional checkpoints. Format: '<height>:<hash>' ; Add additional checkpoints. Format: '<height>:<hash>'
; addcheckpoint=<height>:<hash> ; addcheckpoint=<height>:<hash>
; Add comments to the user agent that is advertised to peers.
; Must not include characters '/', ':', '(' and ')'.
; uacomment=
; ------------------------------------------------------------------------------ ; ------------------------------------------------------------------------------
; RPC server options - The following options control the built-in RPC server ; RPC server options - The following options control the built-in RPC server

View file

@ -1617,15 +1617,16 @@ func newPeerConfig(sp *serverPeer) *peer.Config {
// other implementations' alert messages, we will not relay theirs. // other implementations' alert messages, we will not relay theirs.
OnAlert: nil, OnAlert: nil,
}, },
NewestBlock: sp.newestBlock, NewestBlock: sp.newestBlock,
HostToNetAddress: sp.server.addrManager.HostToNetAddress, HostToNetAddress: sp.server.addrManager.HostToNetAddress,
Proxy: cfg.Proxy, Proxy: cfg.Proxy,
UserAgentName: userAgentName, UserAgentName: userAgentName,
UserAgentVersion: userAgentVersion, UserAgentVersion: userAgentVersion,
ChainParams: sp.server.chainParams, UserAgentComments: cfg.UserAgentComments,
Services: sp.server.services, ChainParams: sp.server.chainParams,
DisableRelayTx: cfg.BlocksOnly, Services: sp.server.services,
ProtocolVersion: wire.FeeFilterVersion, DisableRelayTx: cfg.BlocksOnly,
ProtocolVersion: wire.FeeFilterVersion,
} }
} }