Clean up and util methods

This commit is contained in:
obscuren 2014-07-26 11:24:44 +02:00
parent 92ffc1cc4c
commit 41bd38147c
7 changed files with 67 additions and 26 deletions

@ -3,13 +3,14 @@ package ethchain
import (
"bytes"
"fmt"
"math/big"
_ "strconv"
"time"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethstate"
"github.com/ethereum/eth-go/ethtrie"
"github.com/ethereum/eth-go/ethutil"
"math/big"
_ "strconv"
"time"
)
type BlockInfo struct {
@ -63,12 +64,6 @@ type Block struct {
TxSha []byte
}
// New block takes a raw encoded string
// XXX DEPRICATED
func NewBlockFromData(raw []byte) *Block {
return NewBlockFromBytes(raw)
}
func NewBlockFromBytes(raw []byte) *Block {
block := &Block{}
block.RlpDecode(raw)

@ -3,12 +3,6 @@ package eth
import (
"container/list"
"fmt"
"github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethrpc"
"github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/eth-go/ethwire"
"io/ioutil"
"math/rand"
"net"
@ -18,6 +12,13 @@ import (
"sync"
"sync/atomic"
"time"
"github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethrpc"
"github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/eth-go/ethwire"
)
const seedTextFileUri string = "http://www.ethereum.org/servers.poc3.txt"

@ -98,6 +98,7 @@ func Bytes2Hex(d []byte) string {
func Hex2Bytes(str string) []byte {
h, _ := hex.DecodeString(str)
return h
}

@ -3,8 +3,9 @@ package ethutil
import (
"flag"
"fmt"
"github.com/rakyll/globalconf"
"os"
"github.com/rakyll/globalconf"
)
// Config struct
@ -28,8 +29,7 @@ var Config *ConfigManager
func ReadConfig(ConfigFile string, Datadir string, EnvPrefix string) *ConfigManager {
if Config == nil {
// create ConfigFile if does not exist, otherwise globalconf panic when trying to persist flags
_, err := os.Stat(ConfigFile)
if err != nil && os.IsNotExist(err) {
if !FileExist(ConfigFile) {
fmt.Printf("config file '%s' doesn't exist, creating it\n", ConfigFile)
os.Create(ConfigFile)
}

@ -1,6 +1,8 @@
package ethutil
import (
"io/ioutil"
"os"
"os/user"
"strings"
)
@ -18,3 +20,41 @@ func ExpandHomePath(p string) (path string) {
return
}
func FileExist(filePath string) bool {
_, err := os.Stat(filePath)
if err != nil && os.IsNotExist(err) {
return false
}
return true
}
func ReadAllFile(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
data, err := ioutil.ReadAll(file)
if err != nil {
return "", err
}
return string(data), nil
}
func WriteFile(filePath string, content []byte) error {
fh, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}
defer fh.Close()
_, err = fh.Write(content)
if err != nil {
return err
}
return nil
}

@ -2,11 +2,12 @@ package ethvm
import (
"fmt"
"math"
"math/big"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethstate"
"github.com/ethereum/eth-go/ethutil"
"math"
"math/big"
)
type Debugger interface {

15
peer.go

@ -4,15 +4,16 @@ import (
"bytes"
"container/list"
"fmt"
"github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/eth-go/ethwire"
"net"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/eth-go/ethwire"
)
var peerlogger = ethlog.NewLogger("PEER")
@ -197,10 +198,12 @@ func NewOutboundPeer(addr string, ethereum *Ethereum, caps Caps) *Peer {
}
func (self *Peer) Connect(addr string) (conn net.Conn, err error) {
for attempts := 0; attempts < 5; attempts++ {
const maxTries = 3
for attempts := 0; attempts < maxTries; attempts++ {
conn, err = net.DialTimeout("tcp", addr, 10*time.Second)
if err != nil {
peerlogger.Debugf("Peer connection failed. Retrying (%d/5)\n", attempts+1)
//peerlogger.Debugf("Peer connection failed. Retrying (%d/%d) (%s)\n", attempts+1, maxTries, addr)
time.Sleep(time.Duration(attempts*20) * time.Second)
continue
}