Address a few things found by golint.

This commit is contained in:
Dave Collins 2015-01-28 09:44:56 -06:00
parent 188cf2555f
commit ee403e2abd
5 changed files with 35 additions and 34 deletions

View file

@ -34,8 +34,8 @@ type AddrManager struct {
lookupFunc func(string) ([]net.IP, error) lookupFunc func(string) ([]net.IP, error)
rand *rand.Rand rand *rand.Rand
key [32]byte key [32]byte
addrIndex map[string]*knownAddress // address key to ka for all addrs. addrIndex map[string]*KnownAddress // address key to ka for all addrs.
addrNew [newBucketCount]map[string]*knownAddress addrNew [newBucketCount]map[string]*KnownAddress
addrTried [triedBucketCount]*list.List addrTried [triedBucketCount]*list.List
started int32 started int32
shutdown int32 shutdown int32
@ -208,7 +208,7 @@ func (a *AddrManager) updateAddress(netAddr, srcAddr *btcwire.NetAddress) {
// updated elsewhere in the addrmanager code and would otherwise // updated elsewhere in the addrmanager code and would otherwise
// change the actual netaddress on the peer. // change the actual netaddress on the peer.
netAddrCopy := *netAddr netAddrCopy := *netAddr
ka = &knownAddress{na: &netAddrCopy, srcAddr: srcAddr} ka = &KnownAddress{na: &netAddrCopy, srcAddr: srcAddr}
a.addrIndex[addr] = ka a.addrIndex[addr] = ka
a.nNew++ a.nNew++
// XXX time penalty? // XXX time penalty?
@ -243,7 +243,7 @@ func (a *AddrManager) expireNew(bucket int) {
// Bitcoind here chooses four random and just throws the oldest of // Bitcoind here chooses four random and just throws the oldest of
// those away, but we keep track of oldest in the initial traversal and // those away, but we keep track of oldest in the initial traversal and
// use that information instead. // use that information instead.
var oldest *knownAddress var oldest *KnownAddress
for k, v := range a.addrNew[bucket] { for k, v := range a.addrNew[bucket] {
if v.isBad() { if v.isBad() {
log.Tracef("expiring bad address %v", k) log.Tracef("expiring bad address %v", k)
@ -279,10 +279,10 @@ func (a *AddrManager) expireNew(bucket int) {
// We just choose the eldest. Bitcoind selects 4 random entries and throws away // We just choose the eldest. Bitcoind selects 4 random entries and throws away
// the older of them. // the older of them.
func (a *AddrManager) pickTried(bucket int) *list.Element { func (a *AddrManager) pickTried(bucket int) *list.Element {
var oldest *knownAddress var oldest *KnownAddress
var oldestElem *list.Element var oldestElem *list.Element
for e := a.addrTried[bucket].Front(); e != nil; e = e.Next() { for e := a.addrTried[bucket].Front(); e != nil; e = e.Next() {
ka := e.Value.(*knownAddress) ka := e.Value.(*KnownAddress)
if oldest == nil || oldest.na.Timestamp.After(ka.na.Timestamp) { if oldest == nil || oldest.na.Timestamp.After(ka.na.Timestamp) {
oldestElem = e oldestElem = e
oldest = ka oldest = ka
@ -393,7 +393,7 @@ func (a *AddrManager) savePeers() {
sam.TriedBuckets[i] = make([]string, a.addrTried[i].Len()) sam.TriedBuckets[i] = make([]string, a.addrTried[i].Len())
j := 0 j := 0
for e := a.addrTried[i].Front(); e != nil; e = e.Next() { for e := a.addrTried[i].Front(); e != nil; e = e.Next() {
ka := e.Value.(*knownAddress) ka := e.Value.(*KnownAddress)
sam.TriedBuckets[i][j] = NetAddressKey(ka.na) sam.TriedBuckets[i][j] = NetAddressKey(ka.na)
j++ j++
} }
@ -459,7 +459,7 @@ func (a *AddrManager) deserializePeers(filePath string) error {
copy(a.key[:], sam.Key[:]) copy(a.key[:], sam.Key[:])
for _, v := range sam.Addresses { for _, v := range sam.Addresses {
ka := new(knownAddress) ka := new(KnownAddress)
ka.na, err = a.DeserializeNetAddress(v.Addr) ka.na, err = a.DeserializeNetAddress(v.Addr)
if err != nil { if err != nil {
return fmt.Errorf("failed to deserialize netaddress "+ return fmt.Errorf("failed to deserialize netaddress "+
@ -673,12 +673,12 @@ func (a *AddrManager) AddressCache() []*btcwire.NetAddress {
// and allocating fresh empty bucket storage. // and allocating fresh empty bucket storage.
func (a *AddrManager) reset() { func (a *AddrManager) reset() {
a.addrIndex = make(map[string]*knownAddress) a.addrIndex = make(map[string]*KnownAddress)
// fill key with bytes from a good random source. // fill key with bytes from a good random source.
io.ReadFull(crand.Reader, a.key[:]) io.ReadFull(crand.Reader, a.key[:])
for i := range a.addrNew { for i := range a.addrNew {
a.addrNew[i] = make(map[string]*knownAddress) a.addrNew[i] = make(map[string]*KnownAddress)
} }
for i := range a.addrTried { for i := range a.addrTried {
a.addrTried[i] = list.New() a.addrTried[i] = list.New()
@ -741,7 +741,7 @@ func NetAddressKey(na *btcwire.NetAddress) string {
// random one from the possible addresses with preference given to ones that // random one from the possible addresses with preference given to ones that
// have not been used recently and should not pick 'close' addresses // have not been used recently and should not pick 'close' addresses
// consecutively. // consecutively.
func (a *AddrManager) GetAddress(class string, newBias int) *knownAddress { func (a *AddrManager) GetAddress(class string, newBias int) *KnownAddress {
// Protect concurrent access. // Protect concurrent access.
a.mtx.Lock() a.mtx.Lock()
defer a.mtx.Unlock() defer a.mtx.Unlock()
@ -780,7 +780,7 @@ func (a *AddrManager) GetAddress(class string, newBias int) *knownAddress {
a.rand.Int63n(int64(a.addrTried[bucket].Len())); i > 0; i-- { a.rand.Int63n(int64(a.addrTried[bucket].Len())); i > 0; i-- {
e = e.Next() e = e.Next()
} }
ka := e.Value.(*knownAddress) ka := e.Value.(*KnownAddress)
randval := a.rand.Intn(large) randval := a.rand.Intn(large)
if float64(randval) < (factor * ka.chance() * float64(large)) { if float64(randval) < (factor * ka.chance() * float64(large)) {
log.Tracef("Selected %v from tried bucket", log.Tracef("Selected %v from tried bucket",
@ -801,7 +801,7 @@ func (a *AddrManager) GetAddress(class string, newBias int) *knownAddress {
continue continue
} }
// Then, a random entry in it. // Then, a random entry in it.
var ka *knownAddress var ka *KnownAddress
nth := a.rand.Intn(len(a.addrNew[bucket])) nth := a.rand.Intn(len(a.addrNew[bucket]))
for _, value := range a.addrNew[bucket] { for _, value := range a.addrNew[bucket] {
if nth == 0 { if nth == 0 {
@ -820,7 +820,7 @@ func (a *AddrManager) GetAddress(class string, newBias int) *knownAddress {
} }
} }
func (a *AddrManager) find(addr *btcwire.NetAddress) *knownAddress { func (a *AddrManager) find(addr *btcwire.NetAddress) *KnownAddress {
return a.addrIndex[NetAddressKey(addr)] return a.addrIndex[NetAddressKey(addr)]
} }
@ -923,7 +923,7 @@ func (a *AddrManager) Good(addr *btcwire.NetAddress) {
// No room, we have to evict something else. // No room, we have to evict something else.
entry := a.pickTried(bucket) entry := a.pickTried(bucket)
rmka := entry.Value.(*knownAddress) rmka := entry.Value.(*KnownAddress)
// First bucket it would have been put in. // First bucket it would have been put in.
newBucket := a.getNewBucket(rmka.na, rmka.srcAddr) newBucket := a.getNewBucket(rmka.na, rmka.srcAddr)

View file

@ -10,9 +10,9 @@ import (
"github.com/btcsuite/btcwire" "github.com/btcsuite/btcwire"
) )
// knownAddress tracks information about a known network address that is used // KnownAddress tracks information about a known network address that is used
// to determine how viable an address is. // to determine how viable an address is.
type knownAddress struct { type KnownAddress struct {
na *btcwire.NetAddress na *btcwire.NetAddress
srcAddr *btcwire.NetAddress srcAddr *btcwire.NetAddress
attempts int attempts int
@ -24,19 +24,19 @@ type knownAddress struct {
// NetAddress returns the underlying btcwire.NetAddress associated with the // NetAddress returns the underlying btcwire.NetAddress associated with the
// known address. // known address.
func (ka *knownAddress) NetAddress() *btcwire.NetAddress { func (ka *KnownAddress) NetAddress() *btcwire.NetAddress {
return ka.na return ka.na
} }
// LastAttempt returns the last time the known address was attempted. // LastAttempt returns the last time the known address was attempted.
func (ka *knownAddress) LastAttempt() time.Time { func (ka *KnownAddress) LastAttempt() time.Time {
return ka.lastattempt return ka.lastattempt
} }
// chance returns the selection probability for a known address. The priority // chance returns the selection probability for a known address. The priority
// depends upon how recently the address has been seen, how recently it was last // depends upon how recently the address has been seen, how recently it was last
// attempted and how often attempts to connect to it have failed. // attempted and how often attempts to connect to it have failed.
func (ka *knownAddress) chance() float64 { func (ka *KnownAddress) chance() float64 {
now := time.Now() now := time.Now()
lastSeen := now.Sub(ka.na.Timestamp) lastSeen := now.Sub(ka.na.Timestamp)
lastAttempt := now.Sub(ka.lastattempt) lastAttempt := now.Sub(ka.lastattempt)
@ -71,7 +71,7 @@ func (ka *knownAddress) chance() float64 {
// 4) It has failed ten times in the last week // 4) It has failed ten times in the last week
// All addresses that meet these criteria are assumed to be worthless and not // All addresses that meet these criteria are assumed to be worthless and not
// worth keeping hold of. // worth keeping hold of.
func (ka *knownAddress) isBad() bool { func (ka *KnownAddress) isBad() bool {
if ka.lastattempt.After(time.Now().Add(-1 * time.Minute)) { if ka.lastattempt.After(time.Now().Add(-1 * time.Minute)) {
return false return false
} }

View file

@ -20,8 +20,6 @@ import (
flags "github.com/btcsuite/go-flags" flags "github.com/btcsuite/go-flags"
) )
type ShaHash btcwire.ShaHash
type config struct { type config struct {
DataDir string `short:"b" long:"datadir" description:"Directory to store data"` DataDir string `short:"b" long:"datadir" description:"Directory to store data"`
DbType string `long:"dbtype" description:"Database backend"` DbType string `long:"dbtype" description:"Database backend"`
@ -39,8 +37,8 @@ var (
) )
const ( const (
ArgSha = iota argSha = iota
ArgHeight argHeight
) )
// netName returns the name used when referring to a bitcoin network. At the // netName returns the name used when referring to a bitcoin network. At the
@ -147,9 +145,9 @@ func getSha(db database.Db, str string) (btcwire.ShaHash, error) {
} }
switch argtype { switch argtype {
case ArgSha: case argSha:
// nothing to do // nothing to do
case ArgHeight: case argHeight:
sha, err = db.FetchBlockShaByHeight(idx) sha, err = db.FetchBlockShaByHeight(idx)
if err != nil { if err != nil {
return btcwire.ShaHash{}, err return btcwire.ShaHash{}, err
@ -165,9 +163,9 @@ var ntxcnt int64
var txspendcnt int64 var txspendcnt int64
var txgivecnt int64 var txgivecnt int64
var ErrBadShaPrefix = errors.New("invalid prefix") var errBadShaPrefix = errors.New("invalid prefix")
var ErrBadShaLen = errors.New("invalid len") var errBadShaLen = errors.New("invalid len")
var ErrBadShaChar = errors.New("invalid character") var errBadShaChar = errors.New("invalid character")
func parsesha(argstr string) (argtype int, height int64, psha *btcwire.ShaHash, err error) { func parsesha(argstr string) (argtype int, height int64, psha *btcwire.ShaHash, err error) {
var sha btcwire.ShaHash var sha btcwire.ShaHash
@ -180,14 +178,14 @@ func parsesha(argstr string) (argtype int, height int64, psha *btcwire.ShaHash,
case 66: case 66:
if argstr[0:2] != "0x" { if argstr[0:2] != "0x" {
log.Infof("prefix is %v", argstr[0:2]) log.Infof("prefix is %v", argstr[0:2])
err = ErrBadShaPrefix err = errBadShaPrefix
return return
} }
hashbuf = argstr[2:] hashbuf = argstr[2:]
default: default:
if len(argstr) <= 16 { if len(argstr) <= 16 {
// assume value is height // assume value is height
argtype = ArgHeight argtype = argHeight
var h int var h int
h, err = strconv.Atoi(argstr) h, err = strconv.Atoi(argstr)
if err == nil { if err == nil {
@ -196,7 +194,7 @@ func parsesha(argstr string) (argtype int, height int64, psha *btcwire.ShaHash,
} }
log.Infof("Unable to parse height %v, err %v", height, err) log.Infof("Unable to parse height %v, err %v", height, err)
} }
err = ErrBadShaLen err = errBadShaLen
return return
} }
@ -212,7 +210,7 @@ func parsesha(argstr string) (argtype int, height int64, psha *btcwire.ShaHash,
case ch >= 'A' && ch <= 'F': case ch >= 'A' && ch <= 'F':
val = ch - 'A' + rune(10) val = ch - 'A' + rune(10)
default: default:
err = ErrBadShaChar err = errBadShaChar
return return
} }
b := buf[31-idx/2] b := buf[31-idx/2]

View file

@ -16,6 +16,8 @@ const (
fileLimitMin = 1024 fileLimitMin = 1024
) )
// SetLimits raises some process limits to values which allow btcd and
// associated utilities to run.
func SetLimits() error { func SetLimits() error {
var rLimit syscall.Rlimit var rLimit syscall.Rlimit

View file

@ -4,6 +4,7 @@
package limits package limits
// SetLimits is a no-op on Windows since it's not required there.
func SetLimits() error { func SetLimits() error {
return nil return nil
} }