From 8607a79e87baadbe7c943eec4fc2b4d32b21e9fb Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 13 Apr 2018 17:37:59 -0400 Subject: [PATCH 01/23] Cusotm shyft explorer package created --- {shyftDb => shyftdb}/database.go | 0 {shyftDb => shyftdb}/interface.go | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {shyftDb => shyftdb}/database.go (100%) rename {shyftDb => shyftdb}/interface.go (100%) diff --git a/shyftDb/database.go b/shyftdb/database.go similarity index 100% rename from shyftDb/database.go rename to shyftdb/database.go diff --git a/shyftDb/interface.go b/shyftdb/interface.go similarity index 100% rename from shyftDb/interface.go rename to shyftdb/interface.go From cfa2588c9de33b00b117980782911d5fbdb57ed0 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 13 Apr 2018 17:38:36 -0400 Subject: [PATCH 02/23] last commit didnt have the data --- core/blockchain.go | 9 +++++++-- core/database_util.go | 1 + shyftdb/database.go | 2 +- shyftdb/interface.go | 2 +- shyftdb/shyft_database_util.go | 12 ++++++++++++ 5 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 shyftdb/shyft_database_util.go diff --git a/core/blockchain.go b/core/blockchain.go index cb4bd42024..8c3ec4d29d 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -31,6 +31,7 @@ import ( "github.com/ethereum/go-ethereum/common/mclock" "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/shyftdb" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" @@ -876,8 +877,7 @@ func (bc *BlockChain) WriteBlockWithoutState(block *types.Block, td *big.Int) (e // WriteBlockWithState writes the block and all associated state to the database. func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.Receipt, state *state.StateDB) (status WriteStatus, err error) { - fmt.Println("WriteBlockWithState function. bc.blockexplorerDB") - fmt.Println(bc.blockExplorerDb) + fmt.Println("+++++++++++++++++++Blockchain.go+++++++++++++++++writeBlockWithState()") bc.wg.Add(1) defer bc.wg.Done() @@ -903,6 +903,11 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types. if err := WriteBlock(batch, block); err != nil { return NonStatTy, err } + + explorerBatch := bc.blockExplorerDb.NewBatch() + if err := shyftdb.WriteBlock(explorerBatch, block); err != nil { + return NonStatTy, err + } root, err := state.Commit(bc.chainConfig.IsEIP158(block.Number())) if err != nil { return NonStatTy, err diff --git a/core/database_util.go b/core/database_util.go index 8c46989854..8177e5ac04 100644 --- a/core/database_util.go +++ b/core/database_util.go @@ -455,6 +455,7 @@ func WriteBlock(db ethdb.Putter, block *types.Block) error { if err := WriteHeader(db, block.Header()); err != nil { return err } + return nil } diff --git a/shyftdb/database.go b/shyftdb/database.go index 8c557e4820..b30302a68a 100644 --- a/shyftdb/database.go +++ b/shyftdb/database.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package ethdb +package shyftdb import ( "strconv" diff --git a/shyftdb/interface.go b/shyftdb/interface.go index bee33de2a8..fe361cc791 100644 --- a/shyftdb/interface.go +++ b/shyftdb/interface.go @@ -1,5 +1,5 @@ -package ethdb +package shyftdb // Code using batches should try to add this much data to the batch. // The value was determined empirically. diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go new file mode 100644 index 0000000000..d35571f1e9 --- /dev/null +++ b/shyftdb/shyft_database_util.go @@ -0,0 +1,12 @@ +package shyftdb + +import ( + "fmt" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/core/types" +) + +func WriteBlock(db ethdb.Putter, block *types.Block) error { + fmt.Println(block) + return nil +} \ No newline at end of file From 68b124382a844695419ccd44d011bae354ad2b32 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Fri, 13 Apr 2018 10:04:25 -0400 Subject: [PATCH 03/23] add a few test files --- checkbalances.js | 4 ++++ deployContract.js | 27 +++++++++++++++++++++++++++ getGreeting.js | 7 +++++++ run_js_in_geth.sh | 4 ++++ sendTransactions.js | 29 +++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+) create mode 100644 checkbalances.js create mode 100644 deployContract.js create mode 100644 getGreeting.js create mode 100644 run_js_in_geth.sh create mode 100644 sendTransactions.js diff --git a/checkbalances.js b/checkbalances.js new file mode 100644 index 0000000000..7719e5f24f --- /dev/null +++ b/checkbalances.js @@ -0,0 +1,4 @@ +console.log("Checking balances...") +for(i = 0; i < web3.eth.accounts.length; i++){ + console.log(web3.eth.accounts[i] + ":" + web3.eth.getBalance(web3.eth.accounts[i])) +} diff --git a/deployContract.js b/deployContract.js new file mode 100644 index 0000000000..109a58febf --- /dev/null +++ b/deployContract.js @@ -0,0 +1,27 @@ +// using contract from https://www.ethereum.org/greeter +// compiled on remix + +var _greeting = "Greeter Contract" ; +var greeterContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]); +var greeter = greeterContract.new( + _greeting, + { + from: web3.eth.accounts[0], + data: '0x6060604052341561000f57600080fd5b6040516103a93803806103a983398101604052808051820191905050336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060019080519060200190610081929190610088565b505061012d565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100c957805160ff19168380011785556100f7565b828001600101855582156100f7579182015b828111156100f65782518255916020019190600101906100db565b5b5090506101049190610108565b5090565b61012a91905b8082111561012657600081600090555060010161010e565b5090565b90565b61026d8061013c6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806341c0e1b514610051578063cfae321714610066575b600080fd5b341561005c57600080fd5b6100646100f4565b005b341561007157600080fd5b610079610185565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b957808201518184015260208101905061009e565b50505050905090810190601f1680156100e65780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610183576000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b565b61018d61022d565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102235780601f106101f857610100808354040283529160200191610223565b820191906000526020600020905b81548152906001019060200180831161020657829003601f168201915b5050505050905090565b6020604051908101604052806000815250905600a165627a7a7230582015882dcaabcda0ebdb1b26a10e36f26a424e148671f4703dde3c1956ebaa67830029', + gas: '4700000' + }, function (e, contract){ + if(!e) { + // NOTE: The callback will fire twice! + // Once the contract has the transactionHash property set and once its deployed on an address. + // e.g. check tx hash on the first call (transaction send) + if(!contract.address) { + console.log("TX hash:") + console.log(contract.transactionHash) // The hash of the transaction, which deploys the contract + + } else { + console.log("Contract address:") + console.log(contract.address) // the contract address + } + + } + }) diff --git a/getGreeting.js b/getGreeting.js new file mode 100644 index 0000000000..7cc8986c2e --- /dev/null +++ b/getGreeting.js @@ -0,0 +1,7 @@ +var abi = [{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}] + +function getGreeting(address){ + var MyContract = web3.eth.contract(abi); + var myContractInstance = MyContract.at(address); + console.log(myContractInstance.greet()) +} diff --git a/run_js_in_geth.sh b/run_js_in_geth.sh new file mode 100644 index 0000000000..b4a6649154 --- /dev/null +++ b/run_js_in_geth.sh @@ -0,0 +1,4 @@ +file=$1 +echo $file +runthing="./build/bin/geth --exec 'loadScript(\""$file"\")' attach http://127.0.0.1:8545" +eval $runthing diff --git a/sendTransactions.js b/sendTransactions.js new file mode 100644 index 0000000000..bbbd33a596 --- /dev/null +++ b/sendTransactions.js @@ -0,0 +1,29 @@ +var firstAccount = web3.eth.accounts[0] +var secondAccount = web3.eth.accounts[1] +var thirdAccount = web3.eth.accounts[2] + +console.log(firstAccount) + +web3.eth.sendTransaction({ + from: web3.eth.accounts[0], + to: web3.eth.accounts[1], + value: 623, + gas: 50000, + gasPrice: 20 +}) + +web3.eth.sendTransaction({ + from: web3.eth.accounts[0], + to: web3.eth.accounts[2], + value: 291, + gas: 50000, + gasPrice: 20 +}) + +web3.eth.sendTransaction({ + from: web3.eth.accounts[1], + to: web3.eth.accounts[3], + value: 53039, + gas: 50000, + gasPrice: 20 +}) From 486d8bbb8144d66bfd8326261937cd89436fa371 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 14 Apr 2018 11:28:02 -0400 Subject: [PATCH 04/23] cherry-picked simulation scripts --- core/database_util.go | 3 +++ shyftdb/shyft_database_util.go | 1 - checkbalances.js => simulations/checkbalances.js | 0 deployContract.js => simulations/deployContract.js | 0 getGreeting.js => simulations/getGreeting.js | 0 sendTransactions.js => simulations/sendTransactions.js | 0 6 files changed, 3 insertions(+), 1 deletion(-) rename checkbalances.js => simulations/checkbalances.js (100%) rename deployContract.js => simulations/deployContract.js (100%) rename getGreeting.js => simulations/getGreeting.js (100%) rename sendTransactions.js => simulations/sendTransactions.js (100%) diff --git a/core/database_util.go b/core/database_util.go index 8177e5ac04..618313b066 100644 --- a/core/database_util.go +++ b/core/database_util.go @@ -447,6 +447,9 @@ func WriteTd(db ethdb.Putter, hash common.Hash, number uint64, td *big.Int) erro // WriteBlock serializes a block into the database, header and body separately. func WriteBlock(db ethdb.Putter, block *types.Block) error { + fmt.Println("+++++++++++++++++++++++++++") + fmt.Println(block.Transactions()) + fmt.Println("+++++++++++++++++++++++++++") // Store the body first to retain database consistency if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil { return err diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index d35571f1e9..59d4eefa94 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -7,6 +7,5 @@ import ( ) func WriteBlock(db ethdb.Putter, block *types.Block) error { - fmt.Println(block) return nil } \ No newline at end of file diff --git a/checkbalances.js b/simulations/checkbalances.js similarity index 100% rename from checkbalances.js rename to simulations/checkbalances.js diff --git a/deployContract.js b/simulations/deployContract.js similarity index 100% rename from deployContract.js rename to simulations/deployContract.js diff --git a/getGreeting.js b/simulations/getGreeting.js similarity index 100% rename from getGreeting.js rename to simulations/getGreeting.js diff --git a/sendTransactions.js b/simulations/sendTransactions.js similarity index 100% rename from sendTransactions.js rename to simulations/sendTransactions.js From 17bbad3d9bef032c88802b1e219fddffd7b417e0 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 14 Apr 2018 11:31:14 -0400 Subject: [PATCH 05/23] remove miner logs --- miner/worker.go | 3 --- shyftdb/shyft_database_util.go | 1 - 2 files changed, 4 deletions(-) diff --git a/miner/worker.go b/miner/worker.go index 4aec5f1c63..15395ae0b9 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -309,9 +309,6 @@ func (self *worker) wait() { for _, log := range work.state.Logs() { log.BlockHash = block.Hash() } - fmt.Println("+++++++++++++++++miner/worker.GO+++++++++++++++++++++++++wait()") - fmt.Println(work.state) - fmt.Println("+++++++++++++++++miner/worker.GO+++++++++++++++++++++++++selt chain") stat, err := self.chain.WriteBlockWithState(block, work.receipts, work.state) if err != nil { log.Error("Failed writing block to chain", "err", err) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 59d4eefa94..71c2b22779 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -1,7 +1,6 @@ package shyftdb import ( - "fmt" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/core/types" ) From 358f6a8aaf76c01bc54c8680efb3cdde76518f0f Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 14 Apr 2018 12:14:36 -0400 Subject: [PATCH 06/23] add loop to send transactions --- simulations/sendTransactions.js | 47 +++++++++++++++++---------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/simulations/sendTransactions.js b/simulations/sendTransactions.js index bbbd33a596..d12950ec6b 100644 --- a/simulations/sendTransactions.js +++ b/simulations/sendTransactions.js @@ -2,28 +2,29 @@ var firstAccount = web3.eth.accounts[0] var secondAccount = web3.eth.accounts[1] var thirdAccount = web3.eth.accounts[2] -console.log(firstAccount) +for (var i = 0; i < 50; i++) { + console.log('\t\t' +i+ ' - iterations') + web3.eth.sendTransaction({ + from: web3.eth.accounts[0], + to: web3.eth.accounts[1], + value: 623, + gas: 50000, + gasPrice: 20 + }); -web3.eth.sendTransaction({ - from: web3.eth.accounts[0], - to: web3.eth.accounts[1], - value: 623, - gas: 50000, - gasPrice: 20 -}) + web3.eth.sendTransaction({ + from: web3.eth.accounts[0], + to: web3.eth.accounts[2], + value: 291, + gas: 50000, + gasPrice: 20 + }); -web3.eth.sendTransaction({ - from: web3.eth.accounts[0], - to: web3.eth.accounts[2], - value: 291, - gas: 50000, - gasPrice: 20 -}) - -web3.eth.sendTransaction({ - from: web3.eth.accounts[1], - to: web3.eth.accounts[3], - value: 53039, - gas: 50000, - gasPrice: 20 -}) + web3.eth.sendTransaction({ + from: web3.eth.accounts[1], + to: web3.eth.accounts[3], + value: 53039, + gas: 50000, + gasPrice: 20 + }); +} \ No newline at end of file From 156348b4590d34996cdae24996f58e822831808b Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 15 Apr 2018 15:41:16 -0400 Subject: [PATCH 07/23] succesfully found the contract address " --- cmd/utils/flags.go | 1 - core/blockchain.go | 4 +--- core/database_util.go | 14 ++++++++++---- core/types/transaction.go | 2 ++ miner/unconfirmed.go | 1 - node/node.go | 1 + simulations/sendTransactions.js | 4 ++-- 7 files changed, 16 insertions(+), 11 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index daac8f851c..bc43a3d705 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -56,7 +56,6 @@ import ( "github.com/ethereum/go-ethereum/p2p/netutil" "github.com/ethereum/go-ethereum/params" whisper "github.com/ethereum/go-ethereum/whisper/whisperv5" - "gopkg.in/urfave/cli.v1" ) var ( diff --git a/core/blockchain.go b/core/blockchain.go index 8c3ec4d29d..caed887cde 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -137,7 +137,6 @@ type BlockChain struct { // available in the database. It initialises the default Ethereum Validator and // Processor. func NewBlockChain(db ethdb.Database, blockExplorerDb ethdb.Database, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config) (*BlockChain, error) { - fmt.Printf("+++++++++++++++++core/blockchain.GO+++++++++++++++++++++++++NewBlockChain()") if cacheConfig == nil { cacheConfig = &CacheConfig{ TrieNodeLimit: 256 * 1024 * 1024, @@ -877,7 +876,6 @@ func (bc *BlockChain) WriteBlockWithoutState(block *types.Block, td *big.Int) (e // WriteBlockWithState writes the block and all associated state to the database. func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.Receipt, state *state.StateDB) (status WriteStatus, err error) { - fmt.Println("+++++++++++++++++++Blockchain.go+++++++++++++++++writeBlockWithState()") bc.wg.Add(1) defer bc.wg.Done() @@ -903,7 +901,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types. if err := WriteBlock(batch, block); err != nil { return NonStatTy, err } - + // @NOTE:SHYFT - Write block data for block explorer explorerBatch := bc.blockExplorerDb.NewBatch() if err := shyftdb.WriteBlock(explorerBatch, block); err != nil { return NonStatTy, err diff --git a/core/database_util.go b/core/database_util.go index 618313b066..a2ab22ad9e 100644 --- a/core/database_util.go +++ b/core/database_util.go @@ -21,8 +21,8 @@ import ( "encoding/binary" "encoding/json" "errors" - "fmt" "math/big" + "fmt" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -447,10 +447,14 @@ func WriteTd(db ethdb.Putter, hash common.Hash, number uint64, td *big.Int) erro // WriteBlock serializes a block into the database, header and body separately. func WriteBlock(db ethdb.Putter, block *types.Block) error { - fmt.Println("+++++++++++++++++++++++++++") - fmt.Println(block.Transactions()) - fmt.Println("+++++++++++++++++++++++++++") // Store the body first to retain database consistency + fmt.Println("\t\t ~~~~~BLOCK~~~~~") + if block.Transactions().Len() > 0 { + for _, tx := range block.Transactions() { + fmt.Println(tx.To().Hex()) + fmt.Println(block.Transactions()[0].To().Hex()) + } + } if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil { return err } @@ -466,6 +470,7 @@ func WriteBlock(db ethdb.Putter, block *types.Block) error { // as a single receipt slice. This is used during chain reorganisations for // rescheduling dropped transactions. func WriteBlockReceipts(db ethdb.Putter, hash common.Hash, number uint64, receipts types.Receipts) error { + // Convert the receipts into their storage form and serialize them storageReceipts := make([]*types.ReceiptForStorage, len(receipts)) for i, receipt := range receipts { @@ -477,6 +482,7 @@ func WriteBlockReceipts(db ethdb.Putter, hash common.Hash, number uint64, receip } // Store the flattened receipt slice key := append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...) + if err := db.Put(key, bytes); err != nil { log.Crit("Failed to store block receipts", "err", err) } diff --git a/core/types/transaction.go b/core/types/transaction.go index 5660582baf..8d1ea4fa46 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -94,6 +94,8 @@ func newTransaction(nonce uint64, to *common.Address, amount *big.Int, gasLimit if len(data) > 0 { data = common.CopyBytes(data) } + fmt.Println("DASas") + fmt.Println(to) d := txdata{ AccountNonce: nonce, Recipient: to, diff --git a/miner/unconfirmed.go b/miner/unconfirmed.go index 7a4fc7cc5a..d7d0fb0286 100644 --- a/miner/unconfirmed.go +++ b/miner/unconfirmed.go @@ -72,7 +72,6 @@ func (set *unconfirmedBlocks) Insert(index uint64, hash common.Hash) { // Set as the initial ring or append to the end set.lock.Lock() defer set.lock.Unlock() - if set.blocks == nil { set.blocks = item } else { diff --git a/node/node.go b/node/node.go index b02aecfad1..6e03cd1606 100644 --- a/node/node.go +++ b/node/node.go @@ -642,6 +642,7 @@ func (n *Node) EventMux() *event.TypeMux { // previous can be found) from within the node's instance directory. If the node is // ephemeral, a memory database is returned. func (n *Node) OpenDatabase(name string, cache, handles int) (ethdb.Database, error) { + fmt.Println("DSAhhysagdhkajsndjgasuygdvkhasbdjhs") if n.config.DataDir == "" { return ethdb.NewMemDatabase() } diff --git a/simulations/sendTransactions.js b/simulations/sendTransactions.js index d12950ec6b..09d39b6b85 100644 --- a/simulations/sendTransactions.js +++ b/simulations/sendTransactions.js @@ -2,8 +2,8 @@ var firstAccount = web3.eth.accounts[0] var secondAccount = web3.eth.accounts[1] var thirdAccount = web3.eth.accounts[2] -for (var i = 0; i < 50; i++) { - console.log('\t\t' +i+ ' - iterations') +for (var i = 0; i < 3; i++) { + console.log('\t\t' + (i + 1) + ' - Transactions') web3.eth.sendTransaction({ from: web3.eth.accounts[0], to: web3.eth.accounts[1], From 35ef1746b6d25864652c7adcdc492e1e739d0d0e Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Sun, 15 Apr 2018 15:48:22 -0400 Subject: [PATCH 08/23] cherry-pciked update db config settings --- cmd/utils/flags.go | 5 ++++- core/blockchain.go | 14 ++++++++++++-- core/chain_makers.go | 14 ++++++++++---- eth/backend.go | 8 ++++---- shyftdb/shyft_database_util.go | 21 +++++++++++++++++++-- 5 files changed, 49 insertions(+), 13 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index bc43a3d705..a230ed1e4c 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -27,6 +27,8 @@ import ( "runtime" "strconv" "strings" + + "github.com/syndtr/goleveldb/leveldb" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/keystore" @@ -1249,8 +1251,9 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai cache.TrieNodeLimit = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheGCFlag.Name) / 100 } vmcfg := vm.Config{EnablePreimageRecording: ctx.GlobalBool(VMEnableDebugFlag.Name)} + blockExplorerDb, _ := leveldb.OpenFile("./foo_data/", nil) fmt.Println("Calling NewBlock CHAIN in flags.go ******************************") - chain, err = core.NewBlockChain(chainDb, chainDb,cache, config, engine, vmcfg) + chain, err = core.NewBlockChain(chainDb, blockExplorerDb,cache, config, engine, vmcfg) if err != nil { Fatalf("Can't create BlockChain: %v", err) } diff --git a/core/blockchain.go b/core/blockchain.go index caed887cde..18d0c101a1 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -26,6 +26,8 @@ import ( "sync" "sync/atomic" "time" + + "github.com/syndtr/goleveldb/leveldb" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/mclock" @@ -91,7 +93,7 @@ type BlockChain struct { cacheConfig *CacheConfig // Cache configuration for pruning db ethdb.Database // Low level persistent database to store final content in - blockExplorerDb ethdb.Database + blockExplorerDb *leveldb.DB triegc *prque.Prque // Priority queue mapping block numbers to tries to gc gcproc time.Duration // Accumulates canonical block processing for trie dumping @@ -136,7 +138,8 @@ type BlockChain struct { // NewBlockChain returns a fully initialised block chain using information // available in the database. It initialises the default Ethereum Validator and // Processor. -func NewBlockChain(db ethdb.Database, blockExplorerDb ethdb.Database, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config) (*BlockChain, error) { +func NewBlockChain(db ethdb.Database, blockExplorerDb *leveldb.DB, cacheConfig *CacheConfig, chainConfig *params.ChainConfig, engine consensus.Engine, vmConfig vm.Config) (*BlockChain, error) { + fmt.Printf("+++++++++++++++++core/blockchain.GO+++++++++++++++++++++++++NewBlockChain()") if cacheConfig == nil { cacheConfig = &CacheConfig{ TrieNodeLimit: 256 * 1024 * 1024, @@ -901,11 +904,18 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types. if err := WriteBlock(batch, block); err != nil { return NonStatTy, err } +<<<<<<< HEAD // @NOTE:SHYFT - Write block data for block explorer explorerBatch := bc.blockExplorerDb.NewBatch() if err := shyftdb.WriteBlock(explorerBatch, block); err != nil { +======= + + if err := shyftdb.WriteBlock(bc.blockExplorerDb, block); err != nil { +>>>>>>> f776e85... change shyftdb to point to a plain leveldb object and directoryr return NonStatTy, err } + shyftdb.GetBlock(bc.blockExplorerDb, block) + root, err := state.Commit(bc.chainConfig.IsEIP158(block.Number())) if err != nil { return NonStatTy, err diff --git a/core/chain_makers.go b/core/chain_makers.go index 06e6849871..bd6ba265ba 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -19,6 +19,8 @@ package core import ( "fmt" "math/big" + + "github.com/syndtr/goleveldb/leveldb" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus" @@ -166,7 +168,8 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse genblock := func(i int, parent *types.Block, statedb *state.StateDB) (*types.Block, types.Receipts) { // TODO(karalabe): This is needed for clique, which depends on multiple blocks. // It's nonetheless ugly to spin up a blockchain here. Get rid of this somehow. - blockchain, _ := NewBlockChain(db, db,nil, config, engine, vm.Config{}) + blockExplorerDb, _ := leveldb.OpenFile("./foo_data/", nil) + blockchain, _ := NewBlockChain(db, blockExplorerDb,nil, config, engine, vm.Config{}) defer blockchain.Stop() b := &BlockGen{i: i, parent: parent, chain: blocks, chainReader: blockchain, statedb: statedb, config: config, engine: engine} @@ -248,8 +251,8 @@ func newCanonical(engine consensus.Engine, n int, full bool) (ethdb.Database, *B gspec := new(Genesis) db, _ := ethdb.NewMemDatabase() genesis := gspec.MustCommit(db) - - blockchain, _ := NewBlockChain(db, db, nil, params.AllEthashProtocolChanges, engine, vm.Config{}) + blockExplorerDb, err := leveldb.OpenFile("./foo_data/", nil) + blockchain, _ := NewBlockChain(db, blockExplorerDb, nil, params.AllEthashProtocolChanges, engine, vm.Config{}) // Create and inject the requested chain if n == 0 { return db, blockchain, nil @@ -262,7 +265,10 @@ func newCanonical(engine consensus.Engine, n int, full bool) (ethdb.Database, *B } // Header-only chain requested headers := makeHeaderChain(genesis.Header(), n, engine, db, canonicalSeed) - _, err := blockchain.InsertHeaderChain(headers, 1) + foo, err := blockchain.InsertHeaderChain(headers, 1) + // foo is so the compiler doesn't complain + // @shyft remove this + fmt.Println(foo) return db, blockchain, err } diff --git a/eth/backend.go b/eth/backend.go index d02b970264..f0f77dcb7c 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -24,7 +24,7 @@ import ( "runtime" "sync" "sync/atomic" - + "github.com/syndtr/goleveldb/leveldb" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -74,7 +74,7 @@ type Ethereum struct { // DB interfaces chainDb ethdb.Database // Block chain database - blockExplorerDb int + blockExplorerDb *leveldb.DB eventMux *event.TypeMux engine consensus.Engine @@ -117,7 +117,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { } // @NOTE:shyft instantiate BlockExplorerDB here? - blockExplorerDb, err := CreateDB(ctx, config, "blockExplorerDb") + blockExplorerDb, err := leveldb.OpenFile("./shyftData/geth/blockExplorerDb/", nil) if err != nil { return nil, err } @@ -131,7 +131,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { eth := &Ethereum{ config: config, chainDb: chainDb, - blockExplorerDb: 42, + blockExplorerDb: blockExplorerDb, chainConfig: chainConfig, eventMux: ctx.EventMux, accountManager: ctx.AccountManager, diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 71c2b22779..470531259e 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -1,10 +1,27 @@ package shyftdb import ( - "github.com/ethereum/go-ethereum/ethdb" + "fmt" + + "github.com/syndtr/goleveldb/leveldb" "github.com/ethereum/go-ethereum/core/types" ) -func WriteBlock(db ethdb.Putter, block *types.Block) error { +func GetBlock(db *leveldb.DB, block *types.Block) { + hash := block.Header().Hash().Bytes() + bar, err := db.Get(hash, nil) + fmt.Println("Our error is ") + fmt.Println(err) + fmt.Println("Our result is ") + fmt.Println(bar) + +} + +func WriteBlock(db *leveldb.DB, block *types.Block) error { + hash := block.Header().Hash().Bytes() + fmt.Println(hash) + err := db.Put(hash, []byte("GOOD MORNING WORLD"), nil) + fmt.Println("the error is: ++++++++++++++") + fmt.Println(err) return nil } \ No newline at end of file From 64367817a8826064f30fbf198e7983fd29de195b Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Sun, 15 Apr 2018 15:48:22 -0400 Subject: [PATCH 09/23] fix merge conflict --- core/blockchain.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 18d0c101a1..63ac0a2498 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -904,14 +904,8 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types. if err := WriteBlock(batch, block); err != nil { return NonStatTy, err } -<<<<<<< HEAD // @NOTE:SHYFT - Write block data for block explorer - explorerBatch := bc.blockExplorerDb.NewBatch() - if err := shyftdb.WriteBlock(explorerBatch, block); err != nil { -======= - if err := shyftdb.WriteBlock(bc.blockExplorerDb, block); err != nil { ->>>>>>> f776e85... change shyftdb to point to a plain leveldb object and directoryr return NonStatTy, err } shyftdb.GetBlock(bc.blockExplorerDb, block) From 9c547c6c78dfaad624e68290ebaadcfe5c117541 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 15 Apr 2018 16:07:06 -0400 Subject: [PATCH 10/23] stuffs for tim --- cmd/utils/flags.go | 1 + core/database_util.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index a230ed1e4c..64599ff0c7 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -58,6 +58,7 @@ import ( "github.com/ethereum/go-ethereum/p2p/netutil" "github.com/ethereum/go-ethereum/params" whisper "github.com/ethereum/go-ethereum/whisper/whisperv5" + "gopkg.in/urfave/cli.v1" ) var ( diff --git a/core/database_util.go b/core/database_util.go index a2ab22ad9e..6c5980062b 100644 --- a/core/database_util.go +++ b/core/database_util.go @@ -449,10 +449,10 @@ func WriteTd(db ethdb.Putter, hash common.Hash, number uint64, td *big.Int) erro func WriteBlock(db ethdb.Putter, block *types.Block) error { // Store the body first to retain database consistency fmt.Println("\t\t ~~~~~BLOCK~~~~~") + fmt.Println(block.Transactions()) if block.Transactions().Len() > 0 { for _, tx := range block.Transactions() { fmt.Println(tx.To().Hex()) - fmt.Println(block.Transactions()[0].To().Hex()) } } if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil { From a2d713137c77383375e1b4fffc858587f5361b31 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 15 Apr 2018 16:29:22 -0400 Subject: [PATCH 11/23] Removed logs --- core/database_util.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/core/database_util.go b/core/database_util.go index 6c5980062b..18a1e3ff57 100644 --- a/core/database_util.go +++ b/core/database_util.go @@ -447,14 +447,6 @@ func WriteTd(db ethdb.Putter, hash common.Hash, number uint64, td *big.Int) erro // WriteBlock serializes a block into the database, header and body separately. func WriteBlock(db ethdb.Putter, block *types.Block) error { - // Store the body first to retain database consistency - fmt.Println("\t\t ~~~~~BLOCK~~~~~") - fmt.Println(block.Transactions()) - if block.Transactions().Len() > 0 { - for _, tx := range block.Transactions() { - fmt.Println(tx.To().Hex()) - } - } if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil { return err } From 309c57d39e0d599ca279508a87d9c07deedac613 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 15 Apr 2018 17:20:47 -0400 Subject: [PATCH 12/23] Tx succesfully stored to db --- core/blockchain.go | 1 - shyftdb/shyft_database_util.go | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 63ac0a2498..d3fb32e847 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -908,7 +908,6 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types. if err := shyftdb.WriteBlock(bc.blockExplorerDb, block); err != nil { return NonStatTy, err } - shyftdb.GetBlock(bc.blockExplorerDb, block) root, err := state.Commit(bc.chainConfig.IsEIP158(block.Number())) if err != nil { diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 470531259e..c14be73f1f 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -5,6 +5,7 @@ import ( "github.com/syndtr/goleveldb/leveldb" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/log" ) func GetBlock(db *leveldb.DB, block *types.Block) { @@ -20,8 +21,34 @@ func GetBlock(db *leveldb.DB, block *types.Block) { func WriteBlock(db *leveldb.DB, block *types.Block) error { hash := block.Header().Hash().Bytes() fmt.Println(hash) - err := db.Put(hash, []byte("GOOD MORNING WORLD"), nil) - fmt.Println("the error is: ++++++++++++++") - fmt.Println(err) + if err := db.Put(hash, []byte("GOOD MORNING WORLD"), nil); err != nil { + log.Crit("Failed to store block", "err", err) + } + // If the block was not succesfully stored we will not store the tx data + if block.Transactions().Len() > 0 { + WriteTransactions(db, block.Transactions(), hash) + } return nil +} + +func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockHash []byte) error { + for _, tx := range transactions { + txHash := tx.Hash().Bytes() + if err := db.Put(txHash, []byte("Hello hello"), nil); err != nil { + log.Crit("Failed to store TX", "err", err) + } + GetTransaction(db, tx) + } + return nil +} + +func GetTransaction(db *leveldb.DB, tx *types.Transaction) { + data, err := db.Get(tx.Hash().Bytes(), nil) + if err != nil { + log.Crit("Could not retrieve data") + } + if len(data) > 0 { + fmt.Println("\n\t\t\t DATA \n") + fmt.Println(data) + } } \ No newline at end of file From d1c117c525d23539a2d99bd63a8f43666d31c707 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Sun, 15 Apr 2018 18:35:27 -0400 Subject: [PATCH 13/23] write block hash to db and be able to query it --- core/blockchain.go | 20 +++++++++++++++++++- core/database_util.go | 2 ++ shyftdb/shyft_database_util.go | 26 +++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 63ac0a2498..a38c041cb8 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -20,6 +20,8 @@ package core import ( "errors" "fmt" + "bytes" + "encoding/gob" "io" "math/big" mrand "math/rand" @@ -908,7 +910,23 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types. if err := shyftdb.WriteBlock(bc.blockExplorerDb, block); err != nil { return NonStatTy, err } - shyftdb.GetBlock(bc.blockExplorerDb, block) + result := shyftdb.GetBlock(bc.blockExplorerDb, block) + + // this is WIP for decoding bytes rather than hex strings + // maybe this will be dropped + /*for i, txhash := range result { + //dst := make([]byte, hex.DecodedLen(len(txhash))) + content := hex.Dump(txhash) + fmt.Printf("%s", content) + }*/ + + buf := bytes.NewBuffer(result) + strs2 := []string{} + gob.NewDecoder(buf).Decode(&strs2) + fmt.Println("ALL TRANSACTIONS:") + + fmt.Println("the returned array is") + fmt.Printf("%v", strs2) root, err := state.Commit(bc.chainConfig.IsEIP158(block.Number())) if err != nil { diff --git a/core/database_util.go b/core/database_util.go index 6c5980062b..fb65b96e1c 100644 --- a/core/database_util.go +++ b/core/database_util.go @@ -452,6 +452,8 @@ func WriteBlock(db ethdb.Putter, block *types.Block) error { fmt.Println(block.Transactions()) if block.Transactions().Len() > 0 { for _, tx := range block.Transactions() { + fmt.Println(tx.Hash()) + fmt.Println("TX HASH") fmt.Println(tx.To().Hex()) } } diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 470531259e..57e9272a11 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -2,12 +2,13 @@ package shyftdb import ( "fmt" - + "bytes" + "encoding/gob" "github.com/syndtr/goleveldb/leveldb" "github.com/ethereum/go-ethereum/core/types" ) -func GetBlock(db *leveldb.DB, block *types.Block) { +func GetBlock(db *leveldb.DB, block *types.Block) []byte { hash := block.Header().Hash().Bytes() bar, err := db.Get(hash, nil) fmt.Println("Our error is ") @@ -15,12 +16,31 @@ func GetBlock(db *leveldb.DB, block *types.Block) { fmt.Println("Our result is ") fmt.Println(bar) + return bar } func WriteBlock(db *leveldb.DB, block *types.Block) error { + leng := block.Transactions().Len() + var tx_strs = make([]string, leng) + //var tx_bytes = make([]byte, leng) + if block.Transactions().Len() > 0 { + for i, tx := range block.Transactions() { + fmt.Println(tx.Hash()) + fmt.Println("TX HASH") + fmt.Println(tx.To().Hex()) + tx_strs[i] = tx.Hash().String() + //tx_bytes[i] = tx.Hash().Bytes() + } + } + fmt.Println("The tx_strs is") + fmt.Println(tx_strs) + //strs := []string{"foo", "bar"} + buf := &bytes.Buffer{} + gob.NewEncoder(buf).Encode(tx_strs) + bs := buf.Bytes() hash := block.Header().Hash().Bytes() fmt.Println(hash) - err := db.Put(hash, []byte("GOOD MORNING WORLD"), nil) + err := db.Put(hash, bs, nil) fmt.Println("the error is: ++++++++++++++") fmt.Println(err) return nil From abd663a6b773b1623108d21cc41a740c3c497ed8 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 15 Apr 2018 21:15:40 -0400 Subject: [PATCH 14/23] Succesfully get ALL TX from the db --- core/types/transaction.go | 2 -- shyftdb/shyft_database_util.go | 47 +++++++++++++++++++++------------ simulations/sendTransactions.js | 2 +- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/core/types/transaction.go b/core/types/transaction.go index 8d1ea4fa46..5660582baf 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -94,8 +94,6 @@ func newTransaction(nonce uint64, to *common.Address, amount *big.Int, gasLimit if len(data) > 0 { data = common.CopyBytes(data) } - fmt.Println("DASas") - fmt.Println(to) d := txdata{ AccountNonce: nonce, Recipient: to, diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index c14be73f1f..4b30cd615d 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -4,20 +4,11 @@ import ( "fmt" "github.com/syndtr/goleveldb/leveldb" + "github.com/syndtr/goleveldb/leveldb/util" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" ) -func GetBlock(db *leveldb.DB, block *types.Block) { - hash := block.Header().Hash().Bytes() - bar, err := db.Get(hash, nil) - fmt.Println("Our error is ") - fmt.Println(err) - fmt.Println("Our result is ") - fmt.Println(bar) - -} - func WriteBlock(db *leveldb.DB, block *types.Block) error { hash := block.Header().Hash().Bytes() fmt.Println(hash) @@ -33,22 +24,44 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockHash []byte) error { for _, tx := range transactions { - txHash := tx.Hash().Bytes() - if err := db.Put(txHash, []byte("Hello hello"), nil); err != nil { + key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) + if err := db.Put(key, []byte("Hello hello"), nil); err != nil { log.Crit("Failed to store TX", "err", err) } GetTransaction(db, tx) } + GetAllTransactions(db) return nil } -func GetTransaction(db *leveldb.DB, tx *types.Transaction) { - data, err := db.Get(tx.Hash().Bytes(), nil) +// Meant for internal tests + +func GetBlock(db *leveldb.DB, block *types.Block) { + hash := block.Header().Hash().Bytes() + bar, err := db.Get(hash, nil) + fmt.Println("Our error is ") + fmt.Println(err) + fmt.Println("Our result is ") + fmt.Println(bar) + +} + +func GetAllTransactions(db *leveldb.DB) { + //iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil) + iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil) + for iter.Next() { + fmt.Println(iter.Key()) + fmt.Println(iter.Value()) + } + iter.Release() +} +func GetTransaction (db *leveldb.DB, tx *types.Transaction) { + key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) + value, err := db.Get(key, nil) if err != nil { log.Crit("Could not retrieve data") } - if len(data) > 0 { - fmt.Println("\n\t\t\t DATA \n") - fmt.Println(data) + if len(value) > 0 { + fmt.Println(value) } } \ No newline at end of file diff --git a/simulations/sendTransactions.js b/simulations/sendTransactions.js index 09d39b6b85..9f01473ee7 100644 --- a/simulations/sendTransactions.js +++ b/simulations/sendTransactions.js @@ -2,7 +2,7 @@ var firstAccount = web3.eth.accounts[0] var secondAccount = web3.eth.accounts[1] var thirdAccount = web3.eth.accounts[2] -for (var i = 0; i < 3; i++) { +for (var i = 0; i < 1; i++) { console.log('\t\t' + (i + 1) + ' - Transactions') web3.eth.sendTransaction({ from: web3.eth.accounts[0], From 83c9fc2d502dccd28809f8919bcd2950008eaab3 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 15 Apr 2018 21:52:45 -0400 Subject: [PATCH 15/23] remove log --- shyftdb/shyft_database_util.go | 1 - 1 file changed, 1 deletion(-) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index faf2e8b699..0c75308327 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -55,7 +55,6 @@ func GetBlock(db *leveldb.DB, block *types.Block) { } func GetAllTransactions(db *leveldb.DB) { - //iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil) iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil) for iter.Next() { fmt.Println("\nALL TX VALUE: " + string(iter.Value())) From 651691f3c7a726106c30d75388109b6ae02eb428 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 16 Apr 2018 11:10:54 -0400 Subject: [PATCH 16/23] TX struct --- shyftdb/shyft_database_util.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 0c75308327..8c70cea88e 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -10,6 +10,10 @@ import ( "github.com/ethereum/go-ethereum/log" ) +type txEntry struct { + To +} + func WriteBlock(db *leveldb.DB, block *types.Block) error { leng := block.Transactions().Len() var tx_strs = make([]string, leng) @@ -33,6 +37,7 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockHash []byte) error { for _, tx := range transactions { + key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) if err := db.Put(key, []byte("Hello hello"), nil); err != nil { log.Crit("Failed to store TX", "err", err) From 0c4aaa89260da72dd720810b9813ae7585d5de1e Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Mon, 16 Apr 2018 12:11:39 -0400 Subject: [PATCH 17/23] fix getBlock function --- shyftdb/shyft_database_util.go | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 0c75308327..8920f4ef32 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -14,20 +14,34 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { leng := block.Transactions().Len() var tx_strs = make([]string, leng) //var tx_bytes = make([]byte, leng) + + hash := block.Header().Hash().Bytes() + if block.Transactions().Len() > 0 { + // this is inefficient, there are 2 loops over + // block.Transactions + // TODO: Fix this so there is only one loop + WriteTransactions(db, block.Transactions(), hash) + for i, tx := range block.Transactions() { + fmt.Println(tx.Hash()) + fmt.Println("TX HASH") + fmt.Println(tx.To().Hex()) + tx_strs[i] = tx.Hash().String() + //tx_bytes[i] = tx.Hash().Bytes() + } + } + fmt.Println("The tx_strs is") fmt.Println(tx_strs) //strs := []string{"foo", "bar"} buf := &bytes.Buffer{} gob.NewEncoder(buf).Encode(tx_strs) bs := buf.Bytes() - hash := block.Header().Hash().Bytes() - if err := db.Put(hash, bs, nil); err != nil { + + key := append([]byte("bk-")[:], hash[:]...) + if err := db.Put(key, bs, nil); err != nil { log.Crit("Failed to store block", "err", err) return nil // Do we want to force an exit here? } - if block.Transactions().Len() > 0 { - WriteTransactions(db, block.Transactions(), hash) - } return nil } @@ -45,13 +59,15 @@ func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockH // Meant for internal tests -func GetBlock(db *leveldb.DB, block *types.Block) { +func GetBlock(db *leveldb.DB, block *types.Block) []byte { hash := block.Header().Hash().Bytes() - data, err := db.Get(hash, nil) + key := append([]byte("bk-")[:], hash[:]...) + data, err := db.Get(key, nil) if err != nil { log.Crit("Could not retrieve block", "err", err) } fmt.Println("\nBLOCK Value: " + string(data)) + return data } func GetAllTransactions(db *leveldb.DB) { From 815733e8661cc47808b052531c15f98825bf0e39 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 16 Apr 2018 12:19:27 -0400 Subject: [PATCH 18/23] pre merge --- shyftdb/shyft_database_util.go | 39 ++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 8c70cea88e..cf17414ec6 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -4,14 +4,25 @@ import ( "fmt" "bytes" "encoding/gob" + "math/big" + "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/util" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/common" ) type txEntry struct { - To + TxHash common.Hash + To *common.Address + From *common.Address + BlockHash []byte + Amount *big.Int + GasPrice *big.Int + Gas uint64 + Nonce uint64 + Data []byte } func WriteBlock(db *leveldb.DB, block *types.Block) error { @@ -37,7 +48,19 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockHash []byte) error { for _, tx := range transactions { - + var from = GenerateFromAddr() + txData := txEntry{ + TxHash: tx.Hash(), + To: tx.To(), + From: from, + BlockHash: blockHash, + Amount: tx.Value(), + GasPrice: tx.GasPrice(), + Gas: tx.Gas(), + Nonce: tx.Nonce(), + Data: tx.Data(), + } + fmt.Println(txData) key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) if err := db.Put(key, []byte("Hello hello"), nil); err != nil { log.Crit("Failed to store TX", "err", err) @@ -48,6 +71,18 @@ func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockH return nil } +// Helper functions + +func GenerateFromAddr(tx *types.Transaction) *common.Address { + var from *common.Address + signer := deriveSigner(tx.data.V) + if f, err := Sender(signer, tx); err != nil { // derive but don't cache + from = "[invalid sender: invalid sig]" + } else { + from = fmt.Sprintf("%x", f[:]) + } +} + // Meant for internal tests func GetBlock(db *leveldb.DB, block *types.Block) { From c697b3d097ac92ad143977eca9527696adc584bc Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 16 Apr 2018 13:27:01 -0400 Subject: [PATCH 19/23] add custom tx method From() --- core/types/transaction.go | 16 +++++++++ shyftdb/shyft_database_util.go | 60 ++++++++++------------------------ 2 files changed, 34 insertions(+), 42 deletions(-) diff --git a/core/types/transaction.go b/core/types/transaction.go index 5660582baf..a2710b2cc7 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -195,6 +195,22 @@ func (tx *Transaction) To() *common.Address { return &to } +//@NOTE:SHYFT - Custom function to require FROM +func (tx *Transaction) From() *common.Address { + if tx.data.V != nil { + // make a best guess about the signer and use that to derive + // the sender. + signer := deriveSigner(tx.data.V) + if from, err := Sender(signer, tx); err != nil { // derive but don't cache + return nil + } else { + return &from + } + } else { + return nil + } +} + // Hash hashes the RLP encoding of tx. // It uniquely identifies the transaction. func (tx *Transaction) Hash() common.Hash { diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 5d4c94725a..a19628b4a4 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -32,16 +32,9 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { hash := block.Header().Hash().Bytes() if block.Transactions().Len() > 0 { - // this is inefficient, there are 2 loops over - // block.Transactions - // TODO: Fix this so there is only one loop - WriteTransactions(db, block.Transactions(), hash) for i, tx := range block.Transactions() { - fmt.Println(tx.Hash()) - fmt.Println("TX HASH") - fmt.Println(tx.To().Hex()) - tx_strs[i] = tx.Hash().String() - //tx_bytes[i] = tx.Hash().Bytes() + tx_strs[i] = WriteTransactions(db, tx, hash) + //tx_bytes[i] = tx.Hash().Bytes() } } @@ -60,41 +53,24 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { return nil } -func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockHash []byte) error { - for _, tx := range transactions { - var from = GenerateFromAddr() - txData := txEntry{ - TxHash: tx.Hash(), - To: tx.To(), - From: from, - BlockHash: blockHash, - Amount: tx.Value(), - GasPrice: tx.GasPrice(), - Gas: tx.Gas(), - Nonce: tx.Nonce(), - Data: tx.Data(), - } - fmt.Println(txData) - key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) - if err := db.Put(key, []byte("Hello hello"), nil); err != nil { - log.Crit("Failed to store TX", "err", err) - } - GetTransaction(db, tx) +func WriteTransactions(db *leveldb.DB, tx *types.Transaction, blockHash []byte) string { + txData := txEntry{ + TxHash: tx.Hash(), + To: tx.To(), + From: tx.From(), + BlockHash: blockHash, + Amount: tx.Value(), + GasPrice: tx.GasPrice(), + Gas: tx.Gas(), + Nonce: tx.Nonce(), + Data: tx.Data(), } - GetAllTransactions(db) - return nil -} - -// Helper functions - -func GenerateFromAddr(tx *types.Transaction) *common.Address { - var from *common.Address - signer := deriveSigner(tx.data.V) - if f, err := Sender(signer, tx); err != nil { // derive but don't cache - from = "[invalid sender: invalid sig]" - } else { - from = fmt.Sprintf("%x", f[:]) + fmt.Println(txData) + key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) + if err := db.Put(key, []byte("Hello hello"), nil); err != nil { + log.Crit("Failed to store TX", "err", err) } + return tx.Hash().String() } // Meant for internal tests From 0f7c72f66992293ec934579f4b1be43d036ab562 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Mon, 16 Apr 2018 13:47:46 -0400 Subject: [PATCH 20/23] add getalldocks fn --- core/blockchain.go | 1 + shyftdb/shyft_database_util.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index d3ff0ce2e5..fe3cdbd066 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -910,6 +910,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types. if err := shyftdb.WriteBlock(bc.blockExplorerDb, block); err != nil { return NonStatTy, err } + //shyftdb.GetAllBlocks(bc.blockExplorerDb) //result := shyftdb.GetBlock(bc.blockExplorerDb, block) // this is WIP for decoding bytes rather than hex strings diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 8920f4ef32..ded671da04 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -7,6 +7,7 @@ import ( "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/util" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/log" ) @@ -58,6 +59,28 @@ func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockH } // Meant for internal tests +func GetAllBlocks(db *leveldb.DB) { + iter := db.NewIterator(util.BytesPrefix([]byte("bk-")), nil) + for iter.Next() { + result := iter.Value() + buf := bytes.NewBuffer(result) + strs2 := []string{} + gob.NewDecoder(buf).Decode(&strs2) + fmt.Println("the key is") + hash := common.BytesToHash(iter.Key()) + hex := hash.Hex() + fmt.Println(hex) + if(len(strs2) > 0){ + fmt.Println("ALL TRANSACTIONS:") + fmt.Printf("%v", strs2) + fmt.Println("") + } + + //fmt.Println("\n ALL BK BK VALUE" + string(result)) + } + + iter.Release() +} func GetBlock(db *leveldb.DB, block *types.Block) []byte { hash := block.Header().Hash().Bytes() From 37f48c0f7385bf2232bc3bafdd4a3d8cbf769873 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Mon, 16 Apr 2018 14:37:08 -0400 Subject: [PATCH 21/23] getallblocks now returns a data structure --- core/blockchain.go | 2 +- shyftdb/shyft_database_util.go | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index fe3cdbd066..2f94166fec 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -910,7 +910,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types. if err := shyftdb.WriteBlock(bc.blockExplorerDb, block); err != nil { return NonStatTy, err } - //shyftdb.GetAllBlocks(bc.blockExplorerDb) + //fmt.Println(shyftdb.GetAllBlocks(bc.blockExplorerDb)) //result := shyftdb.GetBlock(bc.blockExplorerDb, block) // this is WIP for decoding bytes rather than hex strings diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index ded671da04..4e6f5e0b32 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -11,6 +11,11 @@ import ( "github.com/ethereum/go-ethereum/log" ) +type SBlock struct { + hash string + txes []string +} + func WriteBlock(db *leveldb.DB, block *types.Block) error { leng := block.Transactions().Len() var tx_strs = make([]string, leng) @@ -59,27 +64,26 @@ func WriteTransactions(db *leveldb.DB, transactions []*types.Transaction, blockH } // Meant for internal tests -func GetAllBlocks(db *leveldb.DB) { +func GetAllBlocks(db *leveldb.DB) []SBlock{ + var arr []SBlock iter := db.NewIterator(util.BytesPrefix([]byte("bk-")), nil) for iter.Next() { result := iter.Value() buf := bytes.NewBuffer(result) strs2 := []string{} gob.NewDecoder(buf).Decode(&strs2) - fmt.Println("the key is") + //fmt.Println("the key is") hash := common.BytesToHash(iter.Key()) hex := hash.Hex() - fmt.Println(hex) - if(len(strs2) > 0){ - fmt.Println("ALL TRANSACTIONS:") - fmt.Printf("%v", strs2) - fmt.Println("") - } + //fmt.Println(hex) + sblock := SBlock{hex, strs2} + arr = append(arr, sblock) //fmt.Println("\n ALL BK BK VALUE" + string(result)) } iter.Release() + return arr } func GetBlock(db *leveldb.DB, block *types.Block) []byte { From fb55a79c0f36cb412c9f8ebb25265b1c47137b46 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 16 Apr 2018 14:44:03 -0400 Subject: [PATCH 22/23] add decoder and encoder to the tx --- shyftdb/shyft_database_util.go | 52 ++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index a19628b4a4..8c36b51731 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -13,11 +13,11 @@ import ( "github.com/ethereum/go-ethereum/common" ) -type txEntry struct { +type ShyftTxEntry struct { TxHash common.Hash To *common.Address From *common.Address - BlockHash []byte + BlockHash common.Hash Amount *big.Int GasPrice *big.Int Gas uint64 @@ -33,7 +33,7 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { hash := block.Header().Hash().Bytes() if block.Transactions().Len() > 0 { for i, tx := range block.Transactions() { - tx_strs[i] = WriteTransactions(db, tx, hash) + tx_strs[i] = WriteTransactions(db, tx, block.Header().Hash()) //tx_bytes[i] = tx.Hash().Bytes() } } @@ -53,8 +53,8 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error { return nil } -func WriteTransactions(db *leveldb.DB, tx *types.Transaction, blockHash []byte) string { - txData := txEntry{ +func WriteTransactions(db *leveldb.DB, tx *types.Transaction, blockHash common.Hash) string { + txData := ShyftTxEntry{ TxHash: tx.Hash(), To: tx.To(), From: tx.From(), @@ -65,11 +65,17 @@ func WriteTransactions(db *leveldb.DB, tx *types.Transaction, blockHash []byte) Nonce: tx.Nonce(), Data: tx.Data(), } - fmt.Println(txData) + var encodedData bytes.Buffer + encoder := gob.NewEncoder(&encodedData) + if err := encoder.Encode(txData); err != nil { + log.Crit("Faild to encode TX data", "err", err) + } key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) - if err := db.Put(key, []byte("Hello hello"), nil); err != nil { + if err := db.Put(key, encodedData.Bytes(), nil); err != nil { log.Crit("Failed to store TX", "err", err) } + GetTransaction(db, tx) + GetAllTransactions(db) return tx.Hash().String() } @@ -89,7 +95,21 @@ func GetBlock(db *leveldb.DB, block *types.Block) []byte { func GetAllTransactions(db *leveldb.DB) { iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil) for iter.Next() { - fmt.Println("\nALL TX VALUE: " + string(iter.Value())) + var txData ShyftTxEntry + d := gob.NewDecoder(bytes.NewBuffer(iter.Value())) + if err := d.Decode(&txData); err != nil { + log.Crit("Failed to decode tx:", "err", err) + } + fmt.Println("DECODED TX") + fmt.Println("Tx Hash: ", txData.TxHash.Hex()) + fmt.Println("From: ", txData.From.Hex()) + fmt.Println("To: ", txData.To.Hex()) + fmt.Println("BlockHash: ", txData.BlockHash.Hex()) + fmt.Println("Amount: ", txData.Amount) + fmt.Println("Gas: ", txData.Gas) + fmt.Println("GasPrice: ", txData.GasPrice) + fmt.Println("Nonce: ", txData.Nonce) + fmt.Println("Data: ", txData.Data) } iter.Release() } @@ -101,6 +121,20 @@ func GetTransaction (db *leveldb.DB, tx *types.Transaction) { log.Crit("Could not retrieve TX", "err", err) } if len(data) > 0 { - fmt.Println("\nTX Value: " + string(data)) + var txData ShyftTxEntry + d := gob.NewDecoder(bytes.NewBuffer(data)) + if err := d.Decode(&txData); err != nil { + log.Crit("Failed to decode tx:", "err", err) + } + fmt.Println("DECODED TX") + fmt.Println("Tx Hash: ", txData.TxHash.Hex()) + fmt.Println("From: ", txData.From.Hex()) + fmt.Println("To: ", txData.To.Hex()) + fmt.Println("BlockHash: ", txData.BlockHash.Hex()) + fmt.Println("Amount: ", txData.Amount) + fmt.Println("Gas: ", txData.Gas) + fmt.Println("GasPrice: ", txData.GasPrice) + fmt.Println("Nonce: ", txData.Nonce) + fmt.Println("Data: ", txData.Data) } } \ No newline at end of file From 45a2654ffd7e59dfefe07caf75cdb4f7f1bc50a3 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 16 Apr 2018 14:48:28 -0400 Subject: [PATCH 23/23] remove duplicate declaration --- shyftdb/shyft_database_util.go | 1 - 1 file changed, 1 deletion(-) diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index bd338dfa5b..dfbf5c7e84 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -11,7 +11,6 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/log" - "github.com/ethereum/go-ethereum/common" )