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

View File

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

View File

@ -3,12 +3,6 @@ package eth
import ( import (
"container/list" "container/list"
"fmt" "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" "io/ioutil"
"math/rand" "math/rand"
"net" "net"
@ -18,6 +12,13 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "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" const seedTextFileUri string = "http://www.ethereum.org/servers.poc3.txt"

View File

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

View File

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

View File

@ -1,6 +1,8 @@
package ethutil package ethutil
import ( import (
"io/ioutil"
"os"
"os/user" "os/user"
"strings" "strings"
) )
@ -18,3 +20,41 @@ func ExpandHomePath(p string) (path string) {
return 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
}

View File

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

15
peer.go
View File

@ -4,15 +4,16 @@ import (
"bytes" "bytes"
"container/list" "container/list"
"fmt" "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" "net"
"strconv" "strconv"
"strings" "strings"
"sync/atomic" "sync/atomic"
"time" "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") 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) { 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) conn, err = net.DialTimeout("tcp", addr, 10*time.Second)
if err != nil { 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 continue
} }