Browse Source

TMROOT env variable. Panic less.

pull/9/head
Jae Kwon 11 years ago
parent
commit
45adb24d43
3 changed files with 26 additions and 13 deletions
  1. +7
    -3
      config/config.go
  2. +1
    -1
      main.go
  3. +18
    -9
      p2p/addrbook.go

+ 7
- 3
config/config.go View File

@ -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{},
}


+ 1
- 1
main.go View File

@ -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{


+ 18
- 9
p2p/addrbook.go View File

@ -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)


Loading…
Cancel
Save