tendermint/peer/knownaddress.go

105 lines
2.8 KiB
Go
Raw Normal View History

2014-06-07 19:09:47 -07:00
package peer
import (
. "github.com/tendermint/tendermint/binary"
"time"
2014-06-08 17:17:17 -07:00
"io"
2014-06-07 19:09:47 -07:00
)
/*
KnownAddress
tracks information about a known network address that is used
to determine how viable an address is.
*/
type KnownAddress struct {
Addr *NetAddress
Src *NetAddress
Attempts UInt32
2014-06-18 20:48:32 -07:00
LastAttempt Time
LastSuccess Time
2014-06-07 19:09:47 -07:00
NewRefs UInt16
OldBucket Int16 // TODO init to -1
}
func NewKnownAddress(addr *NetAddress, src *NetAddress) *KnownAddress {
return &KnownAddress{
Addr: addr,
Src: src,
OldBucket: -1,
2014-06-18 20:48:32 -07:00
LastAttempt: Time{time.Now()},
2014-06-07 19:09:47 -07:00
Attempts: 0,
}
}
2014-06-08 17:17:17 -07:00
func ReadKnownAddress(r io.Reader) *KnownAddress {
return &KnownAddress{
Addr: ReadNetAddress(r),
Src: ReadNetAddress(r),
Attempts: ReadUInt32(r),
2014-06-18 20:48:32 -07:00
LastAttempt: ReadTime(r),
LastSuccess: ReadTime(r),
2014-06-08 17:17:17 -07:00
NewRefs: ReadUInt16(r),
OldBucket: ReadInt16(r),
}
}
func (ka *KnownAddress) WriteTo(w io.Writer) (n int64, err error) {
n, err = WriteOnto(ka.Addr, w, n, err)
n, err = WriteOnto(ka.Src, w, n, err)
n, err = WriteOnto(ka.Attempts, w, n, err)
n, err = WriteOnto(ka.LastAttempt, w, n, err)
n, err = WriteOnto(ka.LastSuccess, w, n, err)
n, err = WriteOnto(ka.NewRefs, w, n, err)
n, err = WriteOnto(ka.OldBucket, w, n, err)
return
}
2014-06-07 19:09:47 -07:00
func (ka *KnownAddress) MarkAttempt(success bool) {
2014-06-18 20:48:32 -07:00
now := Time{time.Now()}
2014-06-07 19:09:47 -07:00
ka.LastAttempt = now
if success {
ka.LastSuccess = now
ka.Attempts = 0
} else {
ka.Attempts += 1
}
}
/*
An address is bad if the address in question has not been tried in the last
minute and meets one of the following criteria:
1) It claims to be from the future
2) It hasn't been seen in over a month
3) It has failed at least three times and never succeeded
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) Bad() bool {
// Has been attempted in the last minute --> good
2014-06-18 20:48:32 -07:00
if ka.LastAttempt.Before(time.Now().Add(-1 * time.Minute)) {
2014-06-07 19:09:47 -07:00
return false
}
// Over a month old?
2014-06-18 20:48:32 -07:00
if ka.LastAttempt.After(time.Now().Add(-1 * numMissingDays * time.Hour * 24)) {
2014-06-07 19:09:47 -07:00
return true
}
// Never succeeded?
2014-06-18 20:48:32 -07:00
if ka.LastSuccess.IsZero() && ka.Attempts >= numRetries {
2014-06-07 19:09:47 -07:00
return true
}
// Hasn't succeeded in too long?
2014-06-18 20:48:32 -07:00
if ka.LastSuccess.Before(time.Now().Add(-1*minBadDays*time.Hour*24)) &&
2014-06-07 19:09:47 -07:00
ka.Attempts >= maxFailures {
return true
}
return false
}