Merge pull request #1 from ShyftNetwork/write-block-hash-to-db

Write block hash to db
This commit is contained in:
Tim Williams 2018-04-15 18:43:48 -04:00 committed by GitHub
commit 950a95bb5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 100 additions and 24 deletions

View file

@ -28,6 +28,8 @@ import (
"strconv"
"strings"
"github.com/syndtr/goleveldb/leveldb"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
@ -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)
}

View file

@ -20,6 +20,8 @@ package core
import (
"errors"
"fmt"
"bytes"
"encoding/gob"
"io"
"math/big"
mrand "math/rand"
@ -27,6 +29,8 @@ import (
"sync/atomic"
"time"
"github.com/syndtr/goleveldb/leveldb"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/consensus"
@ -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

View file

@ -20,6 +20,8 @@ import (
"fmt"
"math/big"
"github.com/syndtr/goleveldb/leveldb"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/misc"
@ -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
}

View file

@ -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)
}

View file

@ -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,

View file

@ -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,

View file

@ -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 {

View file

@ -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()
}

View file

@ -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
}

View file

@ -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],