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/ethereum.go b/ethereum.go index 3a7202d53..d9281cd57 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) { @@ -236,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 @@ -303,12 +306,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/ethminer/miner.go b/ethminer/miner.go index 9396d33f9..19ff5dd9e 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' @@ -148,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/ethpub/pub.go b/ethpub/pub.go index 5a9401d0d..a9a962f14 100644 --- a/ethpub/pub.go +++ b/ethpub/pub.go @@ -4,6 +4,7 @@ import ( "encoding/hex" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethutil" + "math/big" "strings" ) @@ -95,13 +96,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 @@ -122,29 +139,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) { 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 { 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()...) } 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 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]) diff --git a/peer.go b/peer.go index d613bf6ff..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" @@ -440,7 +439,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{}{})) @@ -450,9 +449,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()) @@ -521,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 {