From a2d01d6af8cace4ea0ef87662727659eaa1ff791 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 29 May 2014 02:05:57 +0200 Subject: [PATCH 01/11] Removed comments --- ethpub/pub.go | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/ethpub/pub.go b/ethpub/pub.go index 5a9401d0d..6dd7798ae 100644 --- a/ethpub/pub.go +++ b/ethpub/pub.go @@ -122,29 +122,6 @@ func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, sc var tx *ethchain.Transaction // Compile and assemble the given data if contractCreation { - /* - var initScript, mainScript []byte - var err error - if ethutil.IsHex(initStr) { - initScript = ethutil.FromHex(initStr[2:]) - } else { - initScript, err = ethutil.Compile(initStr) - if err != nil { - return nil, err - } - } - - if ethutil.IsHex(scriptStr) { - mainScript = ethutil.FromHex(scriptStr[2:]) - } else { - mainScript, err = ethutil.Compile(scriptStr) - if err != nil { - return nil, err - } - } - - script := ethchain.AppendScript(initScript, mainScript) - */ var script []byte var err error if ethutil.IsHex(scriptStr) { From e7097641e3987420429fe47efdb678c9e4cd9ba9 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 29 May 2014 11:49:41 +0200 Subject: [PATCH 02/11] Support for namereg --- ethpub/pub.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/ethpub/pub.go b/ethpub/pub.go index 6dd7798ae..6beab5cf9 100644 --- a/ethpub/pub.go +++ b/ethpub/pub.go @@ -2,8 +2,10 @@ package ethpub import ( "encoding/hex" + "fmt" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethutil" + "math/big" "strings" ) @@ -95,13 +97,29 @@ func (lib *PEthereum) Create(key, valueStr, gasStr, gasPriceStr, script string) return lib.createTx(key, "", valueStr, gasStr, gasPriceStr, script) } +var namereg = ethutil.FromHex("bb5f186604d057c1c5240ca2ae0f6430138ac010") + +func GetAddressFromNameReg(stateManager *ethchain.StateManager, name string) []byte { + recp := new(big.Int).SetBytes([]byte(name)) + object := stateManager.CurrentState().GetStateObject(namereg) + reg := object.GetStorage(recp) + + return reg.Bytes() +} + func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, scriptStr string) (*PReceipt, error) { var hash []byte var contractCreation bool if len(recipient) == 0 { contractCreation = true } else { - hash = ethutil.FromHex(recipient) + // Check if an address is stored by this address + addr := GetAddressFromNameReg(lib.stateManager, recipient) + if len(addr) > 0 { + hash = addr + } else { + hash = ethutil.FromHex(recipient) + } } var keyPair *ethutil.KeyPair From 9bb7633254f5ded891f1162783bc06c1b4d131a0 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 29 May 2014 11:50:36 +0200 Subject: [PATCH 03/11] Removed fmt --- ethpub/pub.go | 1 - 1 file changed, 1 deletion(-) diff --git a/ethpub/pub.go b/ethpub/pub.go index 6beab5cf9..a9a962f14 100644 --- a/ethpub/pub.go +++ b/ethpub/pub.go @@ -2,7 +2,6 @@ package ethpub import ( "encoding/hex" - "fmt" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethutil" "math/big" From 8fcba0eb1e947061aadeea1059830dbcdfd2ef44 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 29 May 2014 23:54:48 +0200 Subject: [PATCH 04/11] fixed test --- ethchain/state_test.go | 1 - ethchain/vm_test.go | 2 +- peer.go | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ethchain/state_test.go b/ethchain/state_test.go index 4cc3fdf75..292129953 100644 --- a/ethchain/state_test.go +++ b/ethchain/state_test.go @@ -1,7 +1,6 @@ package ethchain import ( - "fmt" "github.com/ethereum/eth-go/ethdb" "github.com/ethereum/eth-go/ethutil" "testing" diff --git a/ethchain/vm_test.go b/ethchain/vm_test.go index 520f9a2ed..518a88766 100644 --- a/ethchain/vm_test.go +++ b/ethchain/vm_test.go @@ -62,7 +62,7 @@ func TestRun4(t *testing.T) { Diff: big.NewInt(256), }) var ret []byte - ret, e = callerClosure.Call(vm, nil, nil) + ret, _, e = callerClosure.Call(vm, nil, nil) if e != nil { fmt.Println("error", e) } diff --git a/peer.go b/peer.go index d613bf6ff..3d140608b 100644 --- a/peer.go +++ b/peer.go @@ -440,7 +440,7 @@ func (p *Peer) HandleInbound() { ethutil.Config.Log.Debugf("[PEER] Found canonical block, returning chain from: %x ", parent.Hash()) chain := p.ethereum.BlockChain().GetChainFromHash(parent.Hash(), amountOfBlocks) if len(chain) > 0 { - ethutil.Config.Log.Debugf("[PEER] Returning %d blocks: %x ", len(chain), parent.Hash()) + //ethutil.Config.Log.Debugf("[PEER] Returning %d blocks: %x ", len(chain), parent.Hash()) p.QueueMessage(ethwire.NewMessage(ethwire.MsgBlockTy, chain)) } else { p.QueueMessage(ethwire.NewMessage(ethwire.MsgBlockTy, []interface{}{})) From 99797858a692520b47c2ca767b433ca425637d2a Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 30 May 2014 11:47:23 +0200 Subject: [PATCH 05/11] Added coin base to pub block --- ethpub/types.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ethpub/types.go b/ethpub/types.go index de1149a26..4e7c44ed4 100644 --- a/ethpub/types.go +++ b/ethpub/types.go @@ -16,6 +16,7 @@ type PBlock struct { Hash string `json:"hash"` Transactions string `json:"transactions"` Time int64 `json:"time"` + Coinbase string `json:"coinbase"` } // Creates a new QML Block from a chain block @@ -34,7 +35,7 @@ func NewPBlock(block *ethchain.Block) *PBlock { return nil } - return &PBlock{ref: block, Number: int(block.Number.Uint64()), Hash: ethutil.Hex(block.Hash()), Transactions: string(txJson), Time: block.Time} + return &PBlock{ref: block, Number: int(block.Number.Uint64()), Hash: ethutil.Hex(block.Hash()), Transactions: string(txJson), Time: block.Time, Coinbase: ethutil.Hex(block.Coinbase)} } func (self *PBlock) ToString() string { From 15e0093e13dde98fb9ff3251203313ab4f0eacd4 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 30 May 2014 11:48:23 +0200 Subject: [PATCH 06/11] Fixed issue where the client could crash when sending malformed data --- peer.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/peer.go b/peer.go index 3d140608b..60f2de711 100644 --- a/peer.go +++ b/peer.go @@ -450,9 +450,11 @@ func (p *Peer) HandleInbound() { //ethutil.Config.Log.Debugf("[PEER] Could not find a similar block") // If no blocks are found we send back a reply with msg not in chain // and the last hash from get chain - lastHash := msg.Data.Get(l - 1) - //log.Printf("Sending not in chain with hash %x\n", lastHash.AsRaw()) - p.QueueMessage(ethwire.NewMessage(ethwire.MsgNotInChainTy, []interface{}{lastHash.Raw()})) + if l > 0 { + lastHash := msg.Data.Get(l - 1) + //log.Printf("Sending not in chain with hash %x\n", lastHash.AsRaw()) + p.QueueMessage(ethwire.NewMessage(ethwire.MsgNotInChainTy, []interface{}{lastHash.Raw()})) + } } case ethwire.MsgNotInChainTy: ethutil.Config.Log.Debugf("Not in chain: %x\n", msg.Data.Get(0).Bytes()) From 6c91ffcfbe1a7b469dc6a8a2676959f0000c925a Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 30 May 2014 11:48:37 +0200 Subject: [PATCH 07/11] Do not panic, but return nil instead --- ethutil/value.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethutil/value.go b/ethutil/value.go index 83600abc2..c86c24a7a 100644 --- a/ethutil/value.go +++ b/ethutil/value.go @@ -176,7 +176,7 @@ func (val *Value) Get(idx int) *Value { } if idx < 0 { - panic("negative idx for Value Get") + return NewValue(nil) } return NewValue(d[idx]) From f382221b28ab9e886263e37b1eab9c7924a6a0dc Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 30 May 2014 13:04:08 +0200 Subject: [PATCH 08/11] Broadcast "peerList" event upon removing or adding peers --- ethereum.go | 22 +++++++++++++++++++--- peer.go | 9 +-------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/ethereum.go b/ethereum.go index 3a7202d53..6be989a10 100644 --- a/ethereum.go +++ b/ethereum.go @@ -165,6 +165,8 @@ func (s *Ethereum) AddPeer(conn net.Conn) { ethutil.Config.Log.Debugf("[SERV] Max connected peers reached. Not adding incoming peer.") } } + + s.reactor.Post("peerList", s.peers) } func (s *Ethereum) ProcessPeerList(addrs []string) { @@ -303,12 +305,26 @@ func (s *Ethereum) Peers() *list.List { } func (s *Ethereum) reapPeers() { + eachPeer(s.peers, func(p *Peer, e *list.Element) { + if atomic.LoadInt32(&p.disconnect) == 1 || (p.inbound && (time.Now().Unix()-p.lastPong) > int64(5*time.Minute)) { + s.removePeerElement(e) + } + }) +} + +func (s *Ethereum) removePeerElement(e *list.Element) { s.peerMut.Lock() defer s.peerMut.Unlock() - eachPeer(s.peers, func(p *Peer, e *list.Element) { - if atomic.LoadInt32(&p.disconnect) == 1 || (p.inbound && (time.Now().Unix()-p.lastPong) > int64(5*time.Minute)) { - s.peers.Remove(e) + s.peers.Remove(e) + + s.reactor.Post("peerList", s.peers) +} + +func (s *Ethereum) RemovePeer(p *Peer) { + eachPeer(s.peers, func(peer *Peer, e *list.Element) { + if peer == p { + s.removePeerElement(e) } }) } diff --git a/peer.go b/peer.go index 60f2de711..6853a949d 100644 --- a/peer.go +++ b/peer.go @@ -2,7 +2,6 @@ package eth import ( "bytes" - "container/list" "fmt" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethutil" @@ -523,13 +522,7 @@ func (p *Peer) Stop() { } // Pre-emptively remove the peer; don't wait for reaping. We already know it's dead if we are here - p.ethereum.peerMut.Lock() - defer p.ethereum.peerMut.Unlock() - eachPeer(p.ethereum.peers, func(peer *Peer, e *list.Element) { - if peer == p { - p.ethereum.peers.Remove(e) - } - }) + p.ethereum.RemovePeer(p) } func (p *Peer) pushHandshake() error { From e0b6a31613bc48bc5785f2bea655f832848392d8 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 30 May 2014 13:27:56 +0200 Subject: [PATCH 09/11] Buffered channel to fix not ready (blocking) --- ethminer/miner.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ethminer/miner.go b/ethminer/miner.go index 9396d33f9..e7237bae2 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -25,6 +25,7 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { reactChan := make(chan ethutil.React, 1) // This is the channel that receives 'updates' when ever a new transaction or block comes in powChan := make(chan []byte, 1) // This is the channel that receives valid sha hases for a given block powQuitChan := make(chan ethutil.React, 1) // This is the channel that can exit the miner thread + quitChan := make(chan bool, 1) ethereum.Reactor().Subscribe("newBlock", reactChan) ethereum.Reactor().Subscribe("newTx:pre", reactChan) @@ -44,7 +45,7 @@ func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner { reactChan: reactChan, powChan: powChan, powQuitChan: powQuitChan, - quitChan: make(chan bool), + quitChan: quitChan, } // Insert initial TXs in our little miner 'pool' From b15e03acd7d6184ad920292a10d9a2bbf2b59f00 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 30 May 2014 16:57:40 +0200 Subject: [PATCH 10/11] Fixed issue with casting to smaller byte array --- ethutil/big.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ethutil/big.go b/ethutil/big.go index 891d476ad..1c25a4784 100644 --- a/ethutil/big.go +++ b/ethutil/big.go @@ -49,6 +49,10 @@ func BigD(data []byte) *big.Int { func BigToBytes(num *big.Int, base int) []byte { ret := make([]byte, base/8) + if len(num.Bytes()) > base/8 { + return num.Bytes() + } + return append(ret[:len(ret)-len(num.Bytes())], num.Bytes()...) } From 17c825f53a2676ffe17fd7731f8f550aebcb56b0 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 30 May 2014 16:57:58 +0200 Subject: [PATCH 11/11] Peer changes broadcasting and minor miner fix --- ethereum.go | 1 + ethminer/miner.go | 2 +- ethutil/config.go | 54 +++++++++++++++++++++++++++++++++++------------ 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/ethereum.go b/ethereum.go index 6be989a10..d9281cd57 100644 --- a/ethereum.go +++ b/ethereum.go @@ -238,6 +238,7 @@ func (s *Ethereum) ConnectToPeer(addr string) error { s.peers.PushBack(peer) ethutil.Config.Log.Infof("[SERV] Adding peer (%s) %d / %d\n", addr, s.peers.Len(), s.MaxPeers) + s.reactor.Post("peerList", s.peers) } return nil diff --git a/ethminer/miner.go b/ethminer/miner.go index e7237bae2..19ff5dd9e 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -149,7 +149,7 @@ func (self *Miner) mineNewBlock() { // Find a valid nonce self.block.Nonce = self.pow.Search(self.block, self.powQuitChan) if self.block.Nonce != nil { - err := self.ethereum.StateManager().Process(self.block, true) + err := self.ethereum.StateManager().Process(self.block, false) if err != nil { ethutil.Config.Log.Infoln(err) } else { diff --git a/ethutil/config.go b/ethutil/config.go index fb270ce72..916b0d186 100644 --- a/ethutil/config.go +++ b/ethutil/config.go @@ -22,26 +22,54 @@ type config struct { Identifier string } +const defaultConf = ` +id = "" +port = 30303 +upnp = true +maxpeer = 10 +rpc = false +rpcport = 8080 +` + var Config *config +func ApplicationFolder(base string) string { + usr, _ := user.Current() + p := path.Join(usr.HomeDir, base) + + if len(base) > 0 { + //Check if the logging directory already exists, create it if not + _, err := os.Stat(p) + if err != nil { + if os.IsNotExist(err) { + log.Printf("Debug logging directory %s doesn't exist, creating it\n", p) + os.Mkdir(p, 0777) + + } + } + + iniFilePath := path.Join(p, "conf.ini") + _, err = os.Stat(iniFilePath) + if err != nil && os.IsNotExist(err) { + file, err := os.Create(iniFilePath) + if err != nil { + fmt.Println(err) + } else { + assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "ethereal", "assets") + file.Write([]byte(defaultConf + "\nasset_path = " + assetPath)) + } + } + } + + return p +} + // Read config // // Initialize the global Config variable with default settings func ReadConfig(base string, logTypes LoggerType, id string) *config { if Config == nil { - usr, _ := user.Current() - path := path.Join(usr.HomeDir, base) - - if len(base) > 0 { - //Check if the logging directory already exists, create it if not - _, err := os.Stat(path) - if err != nil { - if os.IsNotExist(err) { - log.Printf("Debug logging directory %s doesn't exist, creating it\n", path) - os.Mkdir(path, 0777) - } - } - } + path := ApplicationFolder(base) Config = &config{ExecPath: path, Debug: true, Ver: "0.5.0 RC11"} Config.Identifier = id