From 96fcc1da323ae37489721f58d04f6ebf1c14ff91 Mon Sep 17 00:00:00 2001 From: Maran Date: Mon, 10 Mar 2014 11:53:31 +0100 Subject: [PATCH 1/6] Initial smart-miner stuff --- ethereum.go | 87 ++++++++++++++++++++++++++++++++++++++++++---------- ui/ui_lib.go | 2 +- 2 files changed, 72 insertions(+), 17 deletions(-) diff --git a/ethereum.go b/ethereum.go index 36cd75e47..ed14a7feb 100644 --- a/ethereum.go +++ b/ethereum.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "fmt" "github.com/ethereum/eth-go" "github.com/ethereum/eth-go/ethchain" @@ -172,6 +173,16 @@ func main() { RegisterInterupts(ethereum) ethereum.Start() + minerChan := make(chan ethutil.React, 5) + ethereum.Reactor().Subscribe("newBlock", minerChan) + ethereum.Reactor().Subscribe("newTx", minerChan) + + minerChan2 := make(chan ethutil.React, 5) + ethereum.Reactor().Subscribe("newBlock", minerChan2) + ethereum.Reactor().Subscribe("newTx", minerChan2) + + ethereum.StateManager().PrepareMiningState() + if StartMining { log.Printf("Miner started\n") @@ -181,26 +192,70 @@ func main() { data, _ := ethutil.Config.Db.Get([]byte("KeyRing")) keyRing := ethutil.NewValueFromBytes(data) addr := keyRing.Get(1).Bytes() + txs := ethereum.TxPool().Flush() + block := ethereum.BlockChain().NewBlock(addr, txs) for { - txs := ethereum.TxPool().Flush() - // Create a new block which we're going to mine - block := ethereum.BlockChain().NewBlock(addr, txs) - log.Println("Mining on new block. Includes", len(block.Transactions()), "transactions") - // Apply all transactions to the block - ethereum.StateManager().ApplyTransactions(block, block.Transactions()) + select { + case chanMessage := <-minerChan: + log.Println("REACTOR: Got new block") - ethereum.StateManager().AccumelateRewards(block, block) + if block, ok := chanMessage.Resource.(*ethchain.Block); ok { + if bytes.Compare(ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 { + // TODO: Perhaps continue mining to get some uncle rewards + log.Println("New top block found resetting state") + // Reapplies the latest block to the mining state, thus resetting + ethereum.StateManager().PrepareMiningState() + block = ethereum.BlockChain().NewBlock(addr, txs) + log.Println("Block set") + } else { + if bytes.Compare(block.PrevHash, ethereum.BlockChain().CurrentBlock.PrevHash) == 0 { + log.Println("HELLO UNCLE") + // TODO: Add uncle to block + } + } + } - // Search the nonce - block.Nonce = pow.Search(block) - ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{block.Value().Val}) - err := ethereum.StateManager().ProcessBlock(block) - if err != nil { - log.Println(err) - } else { - //log.Println("\n+++++++ MINED BLK +++++++\n", ethereum.BlockChain().CurrentBlock) - log.Printf("🔨 Mined block %x\n", block.Hash()) + if tx, ok := chanMessage.Resource.(*ethchain.Transaction); ok { + log.Println("REACTOR: Got new transaction", tx) + found := false + for _, ctx := range txs { + if found = bytes.Compare(ctx.Hash(), tx.Hash()) == 0; found { + break + } + + } + if found == false { + log.Println("We did not know about this transaction, adding") + txs = append(txs, tx) + } else { + log.Println("We already had this transaction, ignoring") + } + } + log.Println("Sending block reset") + // Start mining over + log.Println("Block reset done") + default: + // Create a new block which we're going to mine + log.Println("Mining on block. Includes", len(txs), "transactions") + + // Apply all transactions to the block + ethereum.StateManager().ApplyTransactions(block, txs) + ethereum.StateManager().AccumelateRewards(block, block) + + // Search the nonce + block.Nonce = pow.Search(block, minerChan2) + if block.Nonce != nil { + ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{block.Value().Val}) + err := ethereum.StateManager().ProcessBlock(block) + if err != nil { + log.Println(err) + } else { + //log.Println("\n+++++++ MINED BLK +++++++\n", ethereum.BlockChain().CurrentBlock) + log.Printf("🔨 Mined block %x\n", block.Hash()) + block = ethereum.BlockChain().NewBlock(addr, txs) + } + } } } }() diff --git a/ui/ui_lib.go b/ui/ui_lib.go index 2a1abee23..fb5957cc3 100644 --- a/ui/ui_lib.go +++ b/ui/ui_lib.go @@ -54,7 +54,7 @@ func AssetPath(p string) string { // Get Binary Directory exedir, _ := osext.ExecutableFolder() base = filepath.Join(exedir, "../Resources") - base = "/Users/jeffrey/go/src/github.com/ethereum/go-ethereum" + base = "/Users/maranhidskes/projects/go/src/github.com/ethereum/go-ethereum" case "linux": base = "/usr/share/ethereal" case "window": From 1c983ed80cc71e7451c7223bae7e30fc41e07b20 Mon Sep 17 00:00:00 2001 From: Maran Date: Tue, 11 Mar 2014 15:17:23 +0100 Subject: [PATCH 2/6] More mining stuff --- ethereum.go | 16 +++++++++++----- ui/library.go | 1 + 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/ethereum.go b/ethereum.go index ed14a7feb..4d1a74ed1 100644 --- a/ethereum.go +++ b/ethereum.go @@ -173,11 +173,11 @@ func main() { RegisterInterupts(ethereum) ethereum.Start() - minerChan := make(chan ethutil.React, 5) + minerChan := make(chan ethutil.React, 1) ethereum.Reactor().Subscribe("newBlock", minerChan) ethereum.Reactor().Subscribe("newTx", minerChan) - minerChan2 := make(chan ethutil.React, 5) + minerChan2 := make(chan ethutil.React, 1) ethereum.Reactor().Subscribe("newBlock", minerChan2) ethereum.Reactor().Subscribe("newTx", minerChan2) @@ -186,7 +186,6 @@ func main() { if StartMining { log.Printf("Miner started\n") - // Fake block mining. It broadcasts a new block every 5 seconds go func() { pow := ðchain.EasyPow{} data, _ := ethutil.Config.Db.Get([]byte("KeyRing")) @@ -194,6 +193,7 @@ func main() { addr := keyRing.Get(1).Bytes() txs := ethereum.TxPool().Flush() block := ethereum.BlockChain().NewBlock(addr, txs) + var uncles []*ethchain.Block for { select { @@ -204,6 +204,7 @@ func main() { if bytes.Compare(ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 { // TODO: Perhaps continue mining to get some uncle rewards log.Println("New top block found resetting state") + // TODO: We probably want to skip this if it's our own block // Reapplies the latest block to the mining state, thus resetting ethereum.StateManager().PrepareMiningState() block = ethereum.BlockChain().NewBlock(addr, txs) @@ -212,6 +213,7 @@ func main() { if bytes.Compare(block.PrevHash, ethereum.BlockChain().CurrentBlock.PrevHash) == 0 { log.Println("HELLO UNCLE") // TODO: Add uncle to block + uncles = append(uncles, block) } } } @@ -227,7 +229,7 @@ func main() { } if found == false { log.Println("We did not know about this transaction, adding") - txs = append(txs, tx) + txs = ethereum.TxPool().Flush() } else { log.Println("We already had this transaction, ignoring") } @@ -239,6 +241,11 @@ func main() { // Create a new block which we're going to mine log.Println("Mining on block. Includes", len(txs), "transactions") + // Apply uncles + if len(uncles) > 0 { + block.SetUncles(uncles) + } + // Apply all transactions to the block ethereum.StateManager().ApplyTransactions(block, txs) ethereum.StateManager().AccumelateRewards(block, block) @@ -253,7 +260,6 @@ func main() { } else { //log.Println("\n+++++++ MINED BLK +++++++\n", ethereum.BlockChain().CurrentBlock) log.Printf("🔨 Mined block %x\n", block.Hash()) - block = ethereum.BlockChain().NewBlock(addr, txs) } } } diff --git a/ui/library.go b/ui/library.go index 8dda0a89e..8412a8d6c 100644 --- a/ui/library.go +++ b/ui/library.go @@ -35,6 +35,7 @@ func (lib *EthLib) CreateTx(receiver, a, data string) string { tx.Nonce = lib.stateManager.GetAddrState(keyRing.Get(1).Bytes()).Nonce tx.Sign(keyRing.Get(0).Bytes()) + ethutil.Config.Log.Infof("nonce: %x", tx.Nonce) lib.txPool.QueueTransaction(tx) From 3002570085c6823da4b8e12015eafa4bd87177fb Mon Sep 17 00:00:00 2001 From: Maran Date: Thu, 20 Mar 2014 11:20:10 +0100 Subject: [PATCH 3/6] Mining rework --- ethereum.go | 87 ++------------------------------------------------- ui/library.go | 7 +++-- 2 files changed, 7 insertions(+), 87 deletions(-) diff --git a/ethereum.go b/ethereum.go index 4d1a74ed1..556a33851 100644 --- a/ethereum.go +++ b/ethereum.go @@ -1,12 +1,11 @@ package main import ( - "bytes" "fmt" "github.com/ethereum/eth-go" "github.com/ethereum/eth-go/ethchain" + "github.com/ethereum/eth-go/ethminer" "github.com/ethereum/eth-go/ethutil" - "github.com/ethereum/eth-go/ethwire" "github.com/ethereum/go-ethereum/ui" "github.com/niemeyer/qml" "github.com/obscuren/secp256k1-go" @@ -173,97 +172,17 @@ func main() { RegisterInterupts(ethereum) ethereum.Start() - minerChan := make(chan ethutil.React, 1) - ethereum.Reactor().Subscribe("newBlock", minerChan) - ethereum.Reactor().Subscribe("newTx", minerChan) - - minerChan2 := make(chan ethutil.React, 1) - ethereum.Reactor().Subscribe("newBlock", minerChan2) - ethereum.Reactor().Subscribe("newTx", minerChan2) - - ethereum.StateManager().PrepareMiningState() - if StartMining { log.Printf("Miner started\n") go func() { - pow := ðchain.EasyPow{} data, _ := ethutil.Config.Db.Get([]byte("KeyRing")) keyRing := ethutil.NewValueFromBytes(data) addr := keyRing.Get(1).Bytes() - txs := ethereum.TxPool().Flush() - block := ethereum.BlockChain().NewBlock(addr, txs) - var uncles []*ethchain.Block - for { - select { - case chanMessage := <-minerChan: - log.Println("REACTOR: Got new block") + miner := ethminer.NewDefaultMiner(addr, ethereum) + miner.Start() - if block, ok := chanMessage.Resource.(*ethchain.Block); ok { - if bytes.Compare(ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 { - // TODO: Perhaps continue mining to get some uncle rewards - log.Println("New top block found resetting state") - // TODO: We probably want to skip this if it's our own block - // Reapplies the latest block to the mining state, thus resetting - ethereum.StateManager().PrepareMiningState() - block = ethereum.BlockChain().NewBlock(addr, txs) - log.Println("Block set") - } else { - if bytes.Compare(block.PrevHash, ethereum.BlockChain().CurrentBlock.PrevHash) == 0 { - log.Println("HELLO UNCLE") - // TODO: Add uncle to block - uncles = append(uncles, block) - } - } - } - - if tx, ok := chanMessage.Resource.(*ethchain.Transaction); ok { - log.Println("REACTOR: Got new transaction", tx) - found := false - for _, ctx := range txs { - if found = bytes.Compare(ctx.Hash(), tx.Hash()) == 0; found { - break - } - - } - if found == false { - log.Println("We did not know about this transaction, adding") - txs = ethereum.TxPool().Flush() - } else { - log.Println("We already had this transaction, ignoring") - } - } - log.Println("Sending block reset") - // Start mining over - log.Println("Block reset done") - default: - // Create a new block which we're going to mine - log.Println("Mining on block. Includes", len(txs), "transactions") - - // Apply uncles - if len(uncles) > 0 { - block.SetUncles(uncles) - } - - // Apply all transactions to the block - ethereum.StateManager().ApplyTransactions(block, txs) - ethereum.StateManager().AccumelateRewards(block, block) - - // Search the nonce - block.Nonce = pow.Search(block, minerChan2) - if block.Nonce != nil { - ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{block.Value().Val}) - err := ethereum.StateManager().ProcessBlock(block) - if err != nil { - log.Println(err) - } else { - //log.Println("\n+++++++ MINED BLK +++++++\n", ethereum.BlockChain().CurrentBlock) - log.Printf("🔨 Mined block %x\n", block.Hash()) - } - } - } - } }() } diff --git a/ui/library.go b/ui/library.go index 8412a8d6c..d6ce94b75 100644 --- a/ui/library.go +++ b/ui/library.go @@ -27,14 +27,15 @@ func (lib *EthLib) CreateTx(receiver, a, data string) string { } k, _ := ethutil.Config.Db.Get([]byte("KeyRing")) - keyRing := ethutil.NewValueFromBytes(k) + keyPair := ethutil.NewKeyFromBytes(k) amount := ethutil.Big(a) code := ethchain.Compile(strings.Split(data, "\n")) tx := ethchain.NewTransaction(hash, amount, code) - tx.Nonce = lib.stateManager.GetAddrState(keyRing.Get(1).Bytes()).Nonce + tx.Nonce = lib.stateManager.GetAddrState(keyPair.Address()).Nonce + + tx.Sign(keyPair.PrivateKey) - tx.Sign(keyRing.Get(0).Bytes()) ethutil.Config.Log.Infof("nonce: %x", tx.Nonce) lib.txPool.QueueTransaction(tx) From a30f5730b384bf99d23f6e83b356e27a14f961d1 Mon Sep 17 00:00:00 2001 From: Maran Date: Mon, 24 Mar 2014 10:56:42 +0100 Subject: [PATCH 4/6] Reimplement new miner creation --- ethereum/ethereum.go | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/ethereum/ethereum.go b/ethereum/ethereum.go index 3f5e4a8f5..c82e7dcd8 100644 --- a/ethereum/ethereum.go +++ b/ethereum/ethereum.go @@ -4,8 +4,8 @@ import ( "fmt" "github.com/ethereum/eth-go" "github.com/ethereum/eth-go/ethchain" + "github.com/ethereum/eth-go/ethminer" "github.com/ethereum/eth-go/ethutil" - "github.com/ethereum/eth-go/ethwire" "github.com/ethereum/go-ethereum/utils" "log" "os" @@ -121,36 +121,22 @@ func main() { // Fake block mining. It broadcasts a new block every 5 seconds go func() { - pow := ðchain.EasyPow{} - data, _ := ethutil.Config.Db.Get([]byte("KeyRing")) - keyRing := ethutil.NewValueFromBytes(data) - addr := keyRing.Get(1).Bytes() - for { - txs := ethereum.TxPool().Flush() - // Create a new block which we're going to mine - block := ethereum.BlockChain().NewBlock(addr, txs) - log.Println("Mining on new block. Includes", len(block.Transactions()), "transactions") - // Apply all transactions to the block - ethereum.StateManager().ApplyTransactions(block, block.Transactions()) + if StartMining { + log.Printf("Miner started\n") - ethereum.StateManager().Prepare(block.State(), block.State()) - ethereum.StateManager().AccumelateRewards(block) + go func() { + data, _ := ethutil.Config.Db.Get([]byte("KeyRing")) + keyRing := ethutil.NewValueFromBytes(data) + addr := keyRing.Get(1).Bytes() - // Search the nonce - block.Nonce = pow.Search(block) - ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{block.Value().Val}) + miner := ethminer.NewDefaultMiner(addr, ethereum) + miner.Start() - ethereum.StateManager().PrepareDefault(block) - err := ethereum.StateManager().ProcessBlock(block) - if err != nil { - log.Println(err) - } else { - log.Println("\n+++++++ MINED BLK +++++++\n", ethereum.BlockChain().CurrentBlock) - log.Printf("🔨 Mined block %x\n", block.Hash()) - } + }() } }() + } // Wait for shutdown From 5660d598df3d86f892975fcf9628133529598221 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 1 Apr 2014 10:40:34 +0200 Subject: [PATCH 5/6] Added tx output --- ethereal/assets/qml/wallet.qml | 17 +++++++++++++++-- ethereal/ui/library.go | 8 ++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/ethereal/assets/qml/wallet.qml b/ethereal/assets/qml/wallet.qml index 9093e3ca8..b22e82f9a 100644 --- a/ethereal/assets/qml/wallet.qml +++ b/ethereal/assets/qml/wallet.qml @@ -199,9 +199,22 @@ ApplicationWindow { text: "Send" onClicked: { //this.enabled = false - console.log(eth.createTx(txRecipient.text, txValue.text, txGas.text, txGasPrice.text, codeView.text)) + var res = eth.createTx(txRecipient.text, txValue.text, txGas.text, txGasPrice.text, codeView.text) + if(res[1]) { + txOutput.text = "Output:\n" + res[1].error() + } else { + txOutput.text = "Output:\n" + res[0] + } + txOutput.visible = true } } + TextArea { + id: txOutput + visible: false + Layout.fillWidth: true + height: 40 + anchors.bottom: parent.bottom + } } } @@ -391,7 +404,7 @@ ApplicationWindow { anchors.left: aboutIcon.right anchors.leftMargin: 10 font.pointSize: 12 - text: "

Ethereum(Go)


Development

Jeffrey Wilcke

Binary Distribution

Jarrad Hope
" + text: "

Ethereal


Development

Jeffrey Wilcke
Maran Hidskes

Binary Distribution

Jarrad Hope
" } } diff --git a/ethereal/ui/library.go b/ethereal/ui/library.go index 6f8cb6f65..7a8939bf1 100644 --- a/ethereal/ui/library.go +++ b/ethereal/ui/library.go @@ -15,7 +15,7 @@ type EthLib struct { txPool *ethchain.TxPool } -func (lib *EthLib) CreateTx(recipient, valueStr, gasStr, gasPriceStr, data string) string { +func (lib *EthLib) CreateTx(recipient, valueStr, gasStr, gasPriceStr, data string) (string, error) { var hash []byte var contractCreation bool if len(recipient) == 0 { @@ -24,7 +24,7 @@ func (lib *EthLib) CreateTx(recipient, valueStr, gasStr, gasPriceStr, data strin var err error hash, err = hex.DecodeString(recipient) if err != nil { - return err.Error() + return "", err } } @@ -37,7 +37,7 @@ func (lib *EthLib) CreateTx(recipient, valueStr, gasStr, gasPriceStr, data strin if contractCreation { asm, err := mutan.Compile(strings.NewReader(data), false) if err != nil { - return err.Error() + return "", err } code := ethutil.Assemble(asm...) @@ -55,7 +55,7 @@ func (lib *EthLib) CreateTx(recipient, valueStr, gasStr, gasPriceStr, data strin ethutil.Config.Log.Infof("Tx hash %x", tx.Hash()) } - return ethutil.Hex(tx.Hash()) + return ethutil.Hex(tx.Hash()), nil } /* From 1e94cb5286067da80c3227861a836c611f01e32b Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 9 Apr 2014 16:01:11 +0200 Subject: [PATCH 6/6] Nonce handling --- ethereal/ui/library.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ethereal/ui/library.go b/ethereal/ui/library.go index 7a8939bf1..9a7469426 100644 --- a/ethereal/ui/library.go +++ b/ethereal/ui/library.go @@ -45,7 +45,9 @@ func (lib *EthLib) CreateTx(recipient, valueStr, gasStr, gasPriceStr, data strin } else { tx = ethchain.NewTransactionMessage(hash, value, gasPrice, gas, []string{}) } - tx.Nonce = lib.stateManager.GetAddrState(keyPair.Address()).Nonce + acc := lib.stateManager.GetAddrState(keyPair.Address()) + tx.Nonce = acc.Nonce + //acc.Nonce++ tx.Sign(keyPair.PrivateKey) lib.txPool.QueueTransaction(tx)