From 45adb24d43cd42bb4f060ed960729a00c434dee7 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Wed, 16 Jul 2014 13:15:18 -0700 Subject: [PATCH] TMROOT env variable. Panic less. --- config/config.go | 10 +++++++--- main.go | 2 +- p2p/addrbook.go | 27 ++++++++++++++++++--------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/config/config.go b/config/config.go index 715068c81..c1d31e044 100644 --- a/config/config.go +++ b/config/config.go @@ -16,7 +16,7 @@ import ( /* Global & initialization */ -var AppDir = os.Getenv("HOME") + "/.tendermint" +var RootDir string var Config Config_ func initFlags(printHelp *bool) { @@ -26,7 +26,11 @@ func initFlags(printHelp *bool) { } func init() { - configFile := AppDir + "/config.json" + RootDir = os.Getenv("TMROOT") + if RootDir == "" { + RootDir = os.Getenv("HOME") + "/.tendermint" + } + configFile := RootDir + "/config.json" // try to read configuration. if missing, write default configBytes, err := ioutil.ReadFile(configFile) @@ -67,7 +71,7 @@ var defaultConfig = Config_{ Seed: "", Db: DbConfig{ Type: "level", - Dir: AppDir + "/data", + Dir: RootDir + "/data", }, Twilio: TwilioConfig{}, } diff --git a/main.go b/main.go index 526a3c40b..70b77cc09 100644 --- a/main.go +++ b/main.go @@ -42,7 +42,7 @@ func NewNode() *Node { }, } sw := p2p.NewSwitch(chDescs) - book := p2p.NewAddrBook(config.AppDir + "/addrbook.json") + book := p2p.NewAddrBook(config.RootDir + "/addrbook.json") pmgr := p2p.NewPeerManager(sw, book) return &Node{ diff --git a/p2p/addrbook.go b/p2p/addrbook.go index d46c3ec6f..9e9c845c7 100644 --- a/p2p/addrbook.go +++ b/p2p/addrbook.go @@ -386,7 +386,8 @@ func (a *AddrBook) getBucket(bucketType byte, bucketIdx int) map[string]*knownAd func (a *AddrBook) addToNewBucket(ka *knownAddress, bucketIdx int) bool { // Sanity check if ka.isOld() { - panic("Cannot add address already in old bucket to a new bucket") + log.Warning("Cannot add address already in old bucket to a new bucket: %v", ka) + return false } addrStr := ka.Addr.String() @@ -419,10 +420,12 @@ func (a *AddrBook) addToNewBucket(ka *knownAddress, bucketIdx int) bool { func (a *AddrBook) addToOldBucket(ka *knownAddress, bucketIdx int) bool { // Sanity check if ka.isNew() { - panic("Cannot add new address to old bucket") + log.Warning("Cannot add new address to old bucket: %v", ka) + return false } if len(ka.Buckets) != 0 { - panic("Cannot add already old address to another old bucket") + log.Warning("Cannot add already old address to another old bucket: %v", ka) + return false } addrStr := ka.Addr.String() @@ -452,7 +455,8 @@ func (a *AddrBook) addToOldBucket(ka *knownAddress, bucketIdx int) bool { func (a *AddrBook) removeFromBucket(ka *knownAddress, bucketType byte, bucketIdx int) { if ka.BucketType != bucketType { - panic("Bucket type mismatch") + log.Warning("Bucket type mismatch: %v", ka) + return } bucket := a.getBucket(bucketType, bucketIdx) delete(bucket, ka.Addr.String()) @@ -493,7 +497,8 @@ func (a *AddrBook) pickOldest(bucketType byte, bucketIdx int) *knownAddress { func (a *AddrBook) addAddress(addr, src *NetAddress) { if !addr.Routable() { - panic("Cannot add non-routable address") + log.Warning("Cannot add non-routable address %v", addr) + return } ka := a.addrLookup[addr.String()] @@ -545,10 +550,12 @@ func (a *AddrBook) expireNew(bucketIdx int) { func (a *AddrBook) moveToOld(ka *knownAddress) { // Sanity check if ka.isOld() { - panic("Cannot promote address that is already old") + log.Warning("Cannot promote address that is already old %v", ka) + return } if len(ka.Buckets) == 0 { - panic("Cannot promote address that isn't in any new buckets") + log.Warning("Cannot promote address that isn't in any new buckets %v", ka) + return } // Remember one of the buckets in which ka is in. @@ -725,7 +732,8 @@ func (ka *knownAddress) markGood() { func (ka *knownAddress) addBucketRef(bucketIdx int) int { for _, bucket := range ka.Buckets { if bucket == bucketIdx { - panic("Bucket already exists in ka.Buckets") + log.Warning("Bucket already exists in ka.Buckets: %v", ka) + return -1 } } ka.Buckets = append(ka.Buckets, bucketIdx) @@ -740,7 +748,8 @@ func (ka *knownAddress) removeBucketRef(bucketIdx int) int { } } if len(buckets) != len(ka.Buckets)-1 { - panic("bucketIdx not found in ka.Buckets") + log.Warning("bucketIdx not found in ka.Buckets: %v", ka) + return -1 } ka.Buckets = buckets return len(ka.Buckets)