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

View file

@ -10,9 +10,9 @@ import (
"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.
type knownAddress struct {
type KnownAddress struct {
na *btcwire.NetAddress
srcAddr *btcwire.NetAddress
attempts int
@ -24,19 +24,19 @@ type knownAddress struct {
// NetAddress returns the underlying btcwire.NetAddress associated with the
// known address.
func (ka *knownAddress) NetAddress() *btcwire.NetAddress {
func (ka *KnownAddress) NetAddress() *btcwire.NetAddress {
return ka.na
}
// 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
}
// 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
// attempted and how often attempts to connect to it have failed.
func (ka *knownAddress) chance() float64 {
func (ka *KnownAddress) chance() float64 {
now := time.Now()
lastSeen := now.Sub(ka.na.Timestamp)
lastAttempt := now.Sub(ka.lastattempt)
@ -71,7 +71,7 @@ func (ka *knownAddress) chance() float64 {
// 4) It has failed ten times in the last week
// All addresses that meet these criteria are assumed to be worthless and not
// worth keeping hold of.
func (ka *knownAddress) isBad() bool {
func (ka *KnownAddress) isBad() bool {
if ka.lastattempt.After(time.Now().Add(-1 * time.Minute)) {
return false
}

View file

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

View file

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

View file

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