Merge branch 'develop' into conversion
This commit is contained in:
@ -38,7 +38,10 @@ var (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Name string
|
||||
Name string
|
||||
ProtocolVersion int
|
||||
NetworkId int
|
||||
|
||||
DataDir string
|
||||
LogFile string
|
||||
LogLevel int
|
||||
@ -135,14 +138,16 @@ type Ethereum struct {
|
||||
|
||||
logger logger.LogSystem
|
||||
|
||||
Mining bool
|
||||
DataDir string
|
||||
version string
|
||||
Mining bool
|
||||
DataDir string
|
||||
version string
|
||||
ProtocolVersion int
|
||||
NetworkId int
|
||||
}
|
||||
|
||||
func New(config *Config) (*Ethereum, error) {
|
||||
// Boostrap database
|
||||
servlogger := logger.New(config.DataDir, config.LogFile, config.LogLevel, config.LogFormat)
|
||||
servlogsystem := logger.New(config.DataDir, config.LogFile, config.LogLevel, config.LogFormat)
|
||||
|
||||
newdb := config.NewDB
|
||||
if newdb == nil {
|
||||
@ -159,13 +164,14 @@ func New(config *Config) (*Ethereum, error) {
|
||||
extraDb, err := ethdb.NewLDBDatabase(path.Join(config.DataDir, "extra"))
|
||||
|
||||
// Perform database sanity checks
|
||||
d, _ := blockDb.Get([]byte("ProtocolVersion"))
|
||||
protov := common.NewValue(d).Uint()
|
||||
if protov != ProtocolVersion && protov != 0 {
|
||||
d, _ := extraDb.Get([]byte("ProtocolVersion"))
|
||||
protov := int(common.NewValue(d).Uint())
|
||||
if protov != config.ProtocolVersion && protov != 0 {
|
||||
path := path.Join(config.DataDir, "blockchain")
|
||||
return nil, fmt.Errorf("Database version mismatch. Protocol(%d / %d). `rm -rf %s`", protov, ProtocolVersion, path)
|
||||
return nil, fmt.Errorf("Database version mismatch. Protocol(%d / %d). `rm -rf %s`", protov, config.ProtocolVersion, path)
|
||||
}
|
||||
saveProtocolVersion(extraDb)
|
||||
saveProtocolVersion(extraDb, config.ProtocolVersion)
|
||||
servlogger.Infof("Protocol Version: %v, Network Id: %v", config.ProtocolVersion, config.NetworkId)
|
||||
|
||||
eth := &Ethereum{
|
||||
shutdownChan: make(chan bool),
|
||||
@ -173,7 +179,7 @@ func New(config *Config) (*Ethereum, error) {
|
||||
stateDb: stateDb,
|
||||
extraDb: extraDb,
|
||||
eventMux: &event.TypeMux{},
|
||||
logger: servlogger,
|
||||
logger: servlogsystem,
|
||||
accountManager: config.AccountManager,
|
||||
DataDir: config.DataDir,
|
||||
version: config.Name, // TODO should separate from Name
|
||||
@ -195,7 +201,8 @@ func New(config *Config) (*Ethereum, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ethProto := EthProtocol(eth.txPool, eth.chainManager, eth.blockPool)
|
||||
|
||||
ethProto := EthProtocol(config.ProtocolVersion, config.NetworkId, eth.txPool, eth.chainManager, eth.blockPool)
|
||||
protocols := []p2p.Protocol{ethProto}
|
||||
if config.Shh {
|
||||
protocols = append(protocols, eth.whisper.Protocol())
|
||||
@ -309,7 +316,6 @@ func (s *Ethereum) StateDb() common.Database { return s.stateDb }
|
||||
func (s *Ethereum) ExtraDb() common.Database { return s.extraDb }
|
||||
func (s *Ethereum) IsListening() bool { return true } // Always listening
|
||||
func (s *Ethereum) PeerCount() int { return s.net.PeerCount() }
|
||||
func (s *Ethereum) PeerInfo() int { return s.net.PeerCount() }
|
||||
func (s *Ethereum) Peers() []*p2p.Peer { return s.net.Peers() }
|
||||
func (s *Ethereum) MaxPeers() int { return s.net.MaxPeers }
|
||||
func (s *Ethereum) Version() string { return s.version }
|
||||
@ -413,11 +419,11 @@ func (self *Ethereum) blockBroadcastLoop() {
|
||||
}
|
||||
}
|
||||
|
||||
func saveProtocolVersion(db common.Database) {
|
||||
func saveProtocolVersion(db common.Database, protov int) {
|
||||
d, _ := db.Get([]byte("ProtocolVersion"))
|
||||
protocolVersion := common.NewValue(d).Uint()
|
||||
|
||||
if protocolVersion == 0 {
|
||||
db.Put([]byte("ProtocolVersion"), common.NewValue(ProtocolVersion).Bytes())
|
||||
db.Put([]byte("ProtocolVersion"), common.NewValue(protov).Bytes())
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +0,0 @@
|
||||
package eth
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
)
|
||||
|
||||
func WritePeers(path string, addresses []string) {
|
||||
if len(addresses) > 0 {
|
||||
data, _ := json.MarshalIndent(addresses, "", " ")
|
||||
common.WriteFile(path, data)
|
||||
}
|
||||
}
|
||||
|
||||
func ReadPeers(path string) (ips []string, err error) {
|
||||
var data string
|
||||
data, err = common.ReadAllFile(path)
|
||||
if err != nil {
|
||||
json.Unmarshal([]byte(data), &ips)
|
||||
}
|
||||
return
|
||||
}
|
@ -58,13 +58,15 @@ var errorToString = map[int]string{
|
||||
// ethProtocol represents the ethereum wire protocol
|
||||
// instance is running on each peer
|
||||
type ethProtocol struct {
|
||||
txPool txPool
|
||||
chainManager chainManager
|
||||
blockPool blockPool
|
||||
peer *p2p.Peer
|
||||
id string
|
||||
rw p2p.MsgReadWriter
|
||||
errors *errs.Errors
|
||||
txPool txPool
|
||||
chainManager chainManager
|
||||
blockPool blockPool
|
||||
peer *p2p.Peer
|
||||
id string
|
||||
rw p2p.MsgReadWriter
|
||||
errors *errs.Errors
|
||||
protocolVersion int
|
||||
networkId int
|
||||
}
|
||||
|
||||
// backend is the interface the ethereum protocol backend should implement
|
||||
@ -101,27 +103,29 @@ type getBlockHashesMsgData struct {
|
||||
// main entrypoint, wrappers starting a server running the eth protocol
|
||||
// use this constructor to attach the protocol ("class") to server caps
|
||||
// the Dev p2p layer then runs the protocol instance on each peer
|
||||
func EthProtocol(txPool txPool, chainManager chainManager, blockPool blockPool) p2p.Protocol {
|
||||
func EthProtocol(protocolVersion, networkId int, txPool txPool, chainManager chainManager, blockPool blockPool) p2p.Protocol {
|
||||
return p2p.Protocol{
|
||||
Name: "eth",
|
||||
Version: ProtocolVersion,
|
||||
Version: uint(protocolVersion),
|
||||
Length: ProtocolLength,
|
||||
Run: func(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
|
||||
return runEthProtocol(txPool, chainManager, blockPool, peer, rw)
|
||||
return runEthProtocol(protocolVersion, networkId, txPool, chainManager, blockPool, peer, rw)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// the main loop that handles incoming messages
|
||||
// note RemovePeer in the post-disconnect hook
|
||||
func runEthProtocol(txPool txPool, chainManager chainManager, blockPool blockPool, peer *p2p.Peer, rw p2p.MsgReadWriter) (err error) {
|
||||
func runEthProtocol(protocolVersion, networkId int, txPool txPool, chainManager chainManager, blockPool blockPool, peer *p2p.Peer, rw p2p.MsgReadWriter) (err error) {
|
||||
id := peer.ID()
|
||||
self := ðProtocol{
|
||||
txPool: txPool,
|
||||
chainManager: chainManager,
|
||||
blockPool: blockPool,
|
||||
rw: rw,
|
||||
peer: peer,
|
||||
txPool: txPool,
|
||||
chainManager: chainManager,
|
||||
blockPool: blockPool,
|
||||
rw: rw,
|
||||
peer: peer,
|
||||
protocolVersion: protocolVersion,
|
||||
networkId: networkId,
|
||||
errors: &errs.Errors{
|
||||
Package: "ETH",
|
||||
Errors: errorToString,
|
||||
@ -291,8 +295,8 @@ func (self *ethProtocol) statusMsg() p2p.Msg {
|
||||
td, currentBlock, genesisBlock := self.chainManager.Status()
|
||||
|
||||
return p2p.NewMsg(StatusMsg,
|
||||
uint32(ProtocolVersion),
|
||||
uint32(NetworkId),
|
||||
uint32(self.protocolVersion),
|
||||
uint32(self.networkId),
|
||||
td,
|
||||
currentBlock,
|
||||
genesisBlock,
|
||||
@ -330,12 +334,12 @@ func (self *ethProtocol) handleStatus() error {
|
||||
return self.protoError(ErrGenesisBlockMismatch, "%x (!= %x)", status.GenesisBlock, genesisBlock)
|
||||
}
|
||||
|
||||
if status.NetworkId != NetworkId {
|
||||
return self.protoError(ErrNetworkIdMismatch, "%d (!= %d)", status.NetworkId, NetworkId)
|
||||
if int(status.NetworkId) != self.networkId {
|
||||
return self.protoError(ErrNetworkIdMismatch, "%d (!= %d)", status.NetworkId, self.networkId)
|
||||
}
|
||||
|
||||
if ProtocolVersion != status.ProtocolVersion {
|
||||
return self.protoError(ErrProtocolVersionMismatch, "%d (!= %d)", status.ProtocolVersion, ProtocolVersion)
|
||||
if int(status.ProtocolVersion) != self.protocolVersion {
|
||||
return self.protoError(ErrProtocolVersionMismatch, "%d (!= %d)", status.ProtocolVersion, self.protocolVersion)
|
||||
}
|
||||
|
||||
self.peer.Infof("Peer is [eth] capable (%d/%d). TD=%v H=%x\n", status.ProtocolVersion, status.NetworkId, status.TD, status.CurrentBlock[:4])
|
||||
|
@ -9,10 +9,10 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/errs"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
ethlogger "github.com/ethereum/go-ethereum/logger"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/p2p/discover"
|
||||
@ -216,7 +216,7 @@ func (self *ethProtocolTester) checkMsg(i int, code uint64, val interface{}) (ms
|
||||
}
|
||||
|
||||
func (self *ethProtocolTester) run() {
|
||||
err := runEthProtocol(self.txPool, self.chainManager, self.blockPool, testPeer(), self.rw)
|
||||
err := runEthProtocol(ProtocolVersion, NetworkId, self.txPool, self.chainManager, self.blockPool, testPeer(), self.rw)
|
||||
self.quit <- err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user