From 2d39e463a5e6c2ee129eeb048dc4035608e68581 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Wed, 9 Jul 2014 14:27:32 -0700 Subject: [PATCH] cleanup --- p2p/addrbook.go | 28 ++++++++++++++-------------- p2p/connection.go | 5 ++++- p2p/log.go | 19 +++++++++++++++++++ p2p/util.go | 4 ++-- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/p2p/addrbook.go b/p2p/addrbook.go index 1fc867c73..d15120486 100644 --- a/p2p/addrbook.go +++ b/p2p/addrbook.go @@ -120,7 +120,7 @@ func (a *AddrBook) Start() { log.Trace("Starting address manager") a.loadFromFile(a.filePath) a.wg.Add(1) - go a.addressHandler() + go a.saveHandler() } func (a *AddrBook) Stop() { @@ -344,7 +344,7 @@ func (a *AddrBook) loadFromFile(filePath string) { /* Private methods */ -func (a *AddrBook) addressHandler() { +func (a *AddrBook) saveHandler() { dumpAddressTicker := time.NewTicker(dumpAddressInterval) out: for { @@ -384,7 +384,7 @@ func (a *AddrBook) addAddress(addr, src *NetAddress) { return } } else { - ka = NewknownAddress(addr, src) + ka = newKnownAddress(addr, src) a.addrIndex[key] = ka a.nNew++ } @@ -517,19 +517,19 @@ func (a *AddrBook) pickOld(bucket int) int { func (a *AddrBook) getNewBucket(addr, src *NetAddress) int { data1 := []byte{} data1 = append(data1, a.key[:]...) - data1 = append(data1, []byte(GroupKey(addr))...) - data1 = append(data1, []byte(GroupKey(src))...) - hash1 := DoubleSha256(data1) + data1 = append(data1, []byte(groupKey(addr))...) + data1 = append(data1, []byte(groupKey(src))...) + hash1 := doubleSha256(data1) hash64 := binary.LittleEndian.Uint64(hash1) hash64 %= newBucketsPerGroup var hashbuf [8]byte binary.LittleEndian.PutUint64(hashbuf[:], hash64) data2 := []byte{} data2 = append(data2, a.key[:]...) - data2 = append(data2, GroupKey(src)...) + data2 = append(data2, groupKey(src)...) data2 = append(data2, hashbuf[:]...) - hash2 := DoubleSha256(data2) + hash2 := doubleSha256(data2) return int(binary.LittleEndian.Uint64(hash2) % newBucketCount) } @@ -538,17 +538,17 @@ func (a *AddrBook) getOldBucket(addr *NetAddress) int { data1 := []byte{} data1 = append(data1, a.key[:]...) data1 = append(data1, []byte(addr.String())...) - hash1 := DoubleSha256(data1) + hash1 := doubleSha256(data1) hash64 := binary.LittleEndian.Uint64(hash1) hash64 %= oldBucketsPerGroup var hashbuf [8]byte binary.LittleEndian.PutUint64(hashbuf[:], hash64) data2 := []byte{} data2 = append(data2, a.key[:]...) - data2 = append(data2, GroupKey(addr)...) + data2 = append(data2, groupKey(addr)...) data2 = append(data2, hashbuf[:]...) - hash2 := DoubleSha256(data2) + hash2 := doubleSha256(data2) return int(binary.LittleEndian.Uint64(hash2) % oldBucketCount) } @@ -556,7 +556,7 @@ func (a *AddrBook) getOldBucket(addr *NetAddress) int { // This is the /16 for IPv6, the /32 (/36 for he.net) for IPv6, the string // "local" for a local address and the string "unroutable for an unroutable // address. -func GroupKey(na *NetAddress) string { +func groupKey(na *NetAddress) string { if na.Local() { return "local" } @@ -617,7 +617,7 @@ type knownAddress struct { OldBucket Int16 } -func NewknownAddress(addr *NetAddress, src *NetAddress) *knownAddress { +func newKnownAddress(addr *NetAddress, src *NetAddress) *knownAddress { return &knownAddress{ Addr: addr, Src: src, @@ -627,7 +627,7 @@ func NewknownAddress(addr *NetAddress, src *NetAddress) *knownAddress { } } -func ReadknownAddress(r io.Reader) *knownAddress { +func readKnownAddress(r io.Reader) *knownAddress { return &knownAddress{ Addr: ReadNetAddress(r), Src: ReadNetAddress(r), diff --git a/p2p/connection.go b/p2p/connection.go index 27ba7c3f8..3279b8324 100644 --- a/p2p/connection.go +++ b/p2p/connection.go @@ -43,7 +43,7 @@ type Connection struct { errored uint32 } -var ( +const ( PacketTypePing = UInt8(0x00) PacketTypePong = UInt8(0x01) PacketTypeMessage = UInt8(0x10) @@ -158,9 +158,11 @@ FOR_LOOP: c.flush() case <-c.pingRepeatTimer.Ch: _, err = PacketTypePing.WriteTo(c.bufWriter) + log.Debugf("[%v] Sending Ping", c) c.flush() case <-c.pong: _, err = PacketTypePong.WriteTo(c.bufWriter) + log.Debugf("[%v] Sending Pong", c) c.flush() case <-c.quit: break FOR_LOOP @@ -206,6 +208,7 @@ FOR_LOOP: c.pong <- struct{}{} case PacketTypePong: // do nothing + log.Debugf("[%v] Received Pong", c) case PacketTypeMessage: pkt, err := ReadPacketSafe(c.bufReader) if err != nil { diff --git a/p2p/log.go b/p2p/log.go index 0a0ab2766..d8698b898 100644 --- a/p2p/log.go +++ b/p2p/log.go @@ -6,6 +6,25 @@ import ( var log seelog.LoggerInterface +func init() { + config := ` + + + + + + + + +` + + var err error + log, err = seelog.LoggerFromConfigAsBytes([]byte(config)) + if err != nil { + panic(err) + } +} + func SetLogger(l seelog.LoggerInterface) { log = l } diff --git a/p2p/util.go b/p2p/util.go index eeb0020d7..2be320263 100644 --- a/p2p/util.go +++ b/p2p/util.go @@ -4,8 +4,8 @@ import ( "crypto/sha256" ) -// DoubleSha256 calculates sha256(sha256(b)) and returns the resulting bytes. -func DoubleSha256(b []byte) []byte { +// doubleSha256 calculates sha256(sha256(b)) and returns the resulting bytes. +func doubleSha256(b []byte) []byte { hasher := sha256.New() hasher.Write(b) sum := hasher.Sum(nil)