diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index daac8f851c..64599ff0c7 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" @@ -1250,8 +1252,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 8c3ec4d29d..a38c041cb8 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -20,12 +20,16 @@ package core import ( "errors" "fmt" + "bytes" + "encoding/gob" "io" "math/big" mrand "math/rand" "sync" "sync/atomic" "time" + + "github.com/syndtr/goleveldb/leveldb" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/mclock" @@ -91,7 +95,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 +140,7 @@ 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{ @@ -877,7 +881,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,11 +906,28 @@ 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 { + // @NOTE:SHYFT - Write block data for block explorer + if err := shyftdb.WriteBlock(bc.blockExplorerDb, block); err != nil { return NonStatTy, err } + 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 { 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/core/database_util.go b/core/database_util.go index 618313b066..fb65b96e1c 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,16 @@ 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~~~~~") + 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()) + } + } if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil { return err } @@ -466,6 +472,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 +484,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/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/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/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 71c2b22779..57e9272a11 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -1,10 +1,47 @@ package shyftdb import ( - "github.com/ethereum/go-ethereum/ethdb" + "fmt" + "bytes" + "encoding/gob" + "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) []byte { + 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) + + 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, bs, nil) + fmt.Println("the error is: ++++++++++++++") + fmt.Println(err) return nil } \ No newline at end of file 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],