Merge branch 'ethersphere-feature/clientid' into develop

This commit is contained in:
obscuren 2014-07-04 00:13:42 +02:00
commit d3d043dba0
6 changed files with 122 additions and 38 deletions

View File

@ -42,6 +42,7 @@ type EthManager interface {
IsListening() bool IsListening() bool
Peers() *list.List Peers() *list.List
KeyManager() *ethcrypto.KeyManager KeyManager() *ethcrypto.KeyManager
ClientIdentity() ethwire.ClientIdentity
} }
type StateManager struct { type StateManager struct {

View File

@ -76,9 +76,11 @@ type Ethereum struct {
RpcServer *ethrpc.JsonRpcServer RpcServer *ethrpc.JsonRpcServer
keyManager *ethcrypto.KeyManager keyManager *ethcrypto.KeyManager
clientIdentity ethwire.ClientIdentity
} }
func New(db ethutil.Database, keyManager *ethcrypto.KeyManager, caps Caps, usePnp bool) (*Ethereum, error) { func New(db ethutil.Database, clientIdentity ethwire.ClientIdentity, keyManager *ethcrypto.KeyManager, caps Caps, usePnp bool) (*Ethereum, error) {
var err error var err error
var nat NAT var nat NAT
@ -94,14 +96,15 @@ func New(db ethutil.Database, keyManager *ethcrypto.KeyManager, caps Caps, usePn
nonce, _ := ethutil.RandomUint64() nonce, _ := ethutil.RandomUint64()
ethereum := &Ethereum{ ethereum := &Ethereum{
shutdownChan: make(chan bool), shutdownChan: make(chan bool),
quit: make(chan bool), quit: make(chan bool),
db: db, db: db,
peers: list.New(), peers: list.New(),
Nonce: nonce, Nonce: nonce,
serverCaps: caps, serverCaps: caps,
nat: nat, nat: nat,
keyManager: keyManager, keyManager: keyManager,
clientIdentity: clientIdentity,
} }
ethereum.reactor = ethutil.NewReactorEngine() ethereum.reactor = ethutil.NewReactorEngine()
@ -123,6 +126,10 @@ func (s *Ethereum) KeyManager() *ethcrypto.KeyManager {
return s.keyManager return s.keyManager
} }
func (s *Ethereum) ClientIdentity() ethwire.ClientIdentity {
return s.clientIdentity
}
func (s *Ethereum) BlockChain() *ethchain.BlockChain { func (s *Ethereum) BlockChain() *ethchain.BlockChain {
return s.blockChain return s.blockChain
} }

View File

@ -5,29 +5,25 @@ import (
"fmt" "fmt"
"github.com/rakyll/globalconf" "github.com/rakyll/globalconf"
"os" "os"
"runtime"
) )
// Config struct // Config struct
type config struct { type ConfigManager struct {
Db Database Db Database
ExecPath string ExecPath string
Debug bool Debug bool
Paranoia bool Paranoia bool
Ver string
ClientString string
Identifier string
conf *globalconf.GlobalConf conf *globalconf.GlobalConf
} }
var Config *config var Config *ConfigManager
// Read config // Read config
// //
// Initialize Config from Config File // Initialize Config from Config File
func ReadConfig(ConfigFile string, Datadir string, Identifier string, EnvPrefix string) *config { 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) _, err := os.Stat(ConfigFile)
@ -44,34 +40,30 @@ func ReadConfig(ConfigFile string, Datadir string, Identifier string, EnvPrefix
} else { } else {
g.ParseAll() g.ParseAll()
} }
Config = &config{ExecPath: Datadir, Debug: true, Ver: "0.5.16", conf: g, Identifier: Identifier, Paranoia: true} Config = &ConfigManager{ExecPath: Datadir, Debug: true, conf: g, Paranoia: true}
Config.SetClientString("Ethereum(G)")
} }
return Config return Config
} }
// Set client string
//
func (c *config) SetClientString(str string) {
os := runtime.GOOS
cust := c.Identifier
Config.ClientString = fmt.Sprintf("%s/v%s/%s/%s/Go", str, c.Ver, cust, os)
}
func (c *config) SetIdentifier(id string) {
c.Identifier = id
c.Set("id", id)
}
// provides persistence for flags // provides persistence for flags
func (c *config) Set(key, value string) { func (c *ConfigManager) Save(key string, value interface{}) {
f := &flag.Flag{Name: key, Value: &confValue{value}} f := &flag.Flag{Name: key, Value: newConfValue(value)}
c.conf.Set("", f) c.conf.Set("", f)
} }
func (c *ConfigManager) Delete(key string) {
c.conf.Delete("", key)
}
// private type implementing flag.Value
type confValue struct { type confValue struct {
value string value string
} }
// generic constructor to allow persising non-string values directly
func newConfValue(value interface{}) *confValue {
return &confValue{fmt.Sprintf("%v", value)}
}
func (self confValue) String() string { return self.value } func (self confValue) String() string { return self.value }
func (self confValue) Set(s string) error { self.value = s; return nil } func (self confValue) Set(s string) error { self.value = s; return nil }

View File

@ -0,0 +1,54 @@
package ethwire
import (
"fmt"
"runtime"
)
// should be used in Peer handleHandshake, incorporate Caps, ProtocolVersion, Pubkey etc.
type ClientIdentity interface {
String() string
}
type SimpleClientIdentity struct {
clientString string
clientIdentifier string
version string
customIdentifier string
os string
implementation string
}
func NewSimpleClientIdentity(clientIdentifier string, version string, customIdentifier string) *SimpleClientIdentity {
clientIdentity := &SimpleClientIdentity{
clientIdentifier: clientIdentifier,
version: version,
customIdentifier: customIdentifier,
os: runtime.GOOS,
implementation: "Go",
}
clientIdentity.init()
return clientIdentity
}
func (c *SimpleClientIdentity) init() {
c.clientString = fmt.Sprintf("%s/v%s/%s/%s/%s",
c.clientIdentifier,
c.version,
c.customIdentifier,
c.os,
c.implementation)
}
func (c *SimpleClientIdentity) String() string {
return c.clientString
}
func (c *SimpleClientIdentity) SetCustomIdentifier(customIdentifier string) {
c.customIdentifier = customIdentifier
c.init()
}
func (c *SimpleClientIdentity) GetCustomIdentifier() string {
return c.customIdentifier
}

View File

@ -0,0 +1,30 @@
package ethwire
import (
"fmt"
"runtime"
"testing"
)
func TestClientIdentity(t *testing.T) {
clientIdentity := NewSimpleClientIdentity("Ethereum(G)", "0.5.16", "test")
clientString := clientIdentity.String()
expected := fmt.Sprintf("Ethereum(G)/v0.5.16/test/%s/Go", runtime.GOOS)
if clientString != expected {
t.Error("Expected clientIdentity to be %v, got %v", expected, clientString)
}
customIdentifier := clientIdentity.GetCustomIdentifier()
if customIdentifier != "test" {
t.Error("Expected clientIdentity.GetCustomIdentifier() to be 'test', got %v", customIdentifier)
}
clientIdentity.SetCustomIdentifier("test2")
customIdentifier = clientIdentity.GetCustomIdentifier()
if customIdentifier != "test2" {
t.Error("Expected clientIdentity.GetCustomIdentifier() to be 'test2', got %v", customIdentifier)
}
clientString = clientIdentity.String()
expected = fmt.Sprintf("Ethereum(G)/v0.5.16/test2/%s/Go", runtime.GOOS)
if clientString != expected {
t.Error("Expected clientIdentity to be %v, got %v", expected, clientString)
}
}

View File

@ -162,7 +162,7 @@ func NewPeer(conn net.Conn, ethereum *Ethereum, inbound bool) *Peer {
pubkey: pubkey, pubkey: pubkey,
blocksRequested: 10, blocksRequested: 10,
caps: ethereum.ServerCaps(), caps: ethereum.ServerCaps(),
version: ethutil.Config.ClientString, version: ethereum.ClientIdentity().String(),
} }
} }
@ -175,7 +175,7 @@ func NewOutboundPeer(addr string, ethereum *Ethereum, caps Caps) *Peer {
connected: 0, connected: 0,
disconnect: 0, disconnect: 0,
caps: caps, caps: caps,
version: ethutil.Config.ClientString, version: ethereum.ClientIdentity().String(),
} }
// Set up the connection in another goroutine so we don't block the main thread // Set up the connection in another goroutine so we don't block the main thread