Merge branch 'shyft/greg' of github.com:ShyftNetwork/shyft_go-ethereum into shyftBlockExp

This commit is contained in:
Dustin Brickwood 2018-04-16 10:04:17 -04:00
commit 636cba146f
16 changed files with 200 additions and 20 deletions

View file

@ -28,6 +28,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/syndtr/goleveldb/leveldb"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common" "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 cache.TrieNodeLimit = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheGCFlag.Name) / 100
} }
vmcfg := vm.Config{EnablePreimageRecording: ctx.GlobalBool(VMEnableDebugFlag.Name)} vmcfg := vm.Config{EnablePreimageRecording: ctx.GlobalBool(VMEnableDebugFlag.Name)}
blockExplorerDb, _ := leveldb.OpenFile("./foo_data/", nil)
fmt.Println("Calling NewBlock CHAIN in flags.go ******************************") 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 { if err != nil {
Fatalf("Can't create BlockChain: %v", err) Fatalf("Can't create BlockChain: %v", err)
} }

View file

@ -20,6 +20,8 @@ package core
import ( import (
"errors" "errors"
"fmt" "fmt"
//"bytes"
//"encoding/gob"
"io" "io"
"math/big" "math/big"
mrand "math/rand" mrand "math/rand"
@ -27,10 +29,13 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/syndtr/goleveldb/leveldb"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/mclock" "github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/core/state" "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/types"
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
@ -90,7 +95,7 @@ type BlockChain struct {
cacheConfig *CacheConfig // Cache configuration for pruning cacheConfig *CacheConfig // Cache configuration for pruning
db ethdb.Database // Low level persistent database to store final content in 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 triegc *prque.Prque // Priority queue mapping block numbers to tries to gc
gcproc time.Duration // Accumulates canonical block processing for trie dumping gcproc time.Duration // Accumulates canonical block processing for trie dumping
@ -135,7 +140,7 @@ type BlockChain struct {
// NewBlockChain returns a fully initialised block chain using information // NewBlockChain returns a fully initialised block chain using information
// available in the database. It initialises the default Ethereum Validator and // available in the database. It initialises the default Ethereum Validator and
// Processor. // 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()") fmt.Printf("+++++++++++++++++core/blockchain.GO+++++++++++++++++++++++++NewBlockChain()")
if cacheConfig == nil { if cacheConfig == nil {
cacheConfig = &CacheConfig{ cacheConfig = &CacheConfig{
@ -876,8 +881,6 @@ func (bc *BlockChain) WriteBlockWithoutState(block *types.Block, td *big.Int) (e
// WriteBlockWithState writes the block and all associated state to the database. // 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) { 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)
bc.wg.Add(1) bc.wg.Add(1)
defer bc.wg.Done() defer bc.wg.Done()
@ -903,6 +906,28 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
if err := WriteBlock(batch, block); err != nil { if err := WriteBlock(batch, block); err != nil {
return NonStatTy, err return NonStatTy, err
} }
// @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())) root, err := state.Commit(bc.chainConfig.IsEIP158(block.Number()))
if err != nil { if err != nil {
return NonStatTy, err return NonStatTy, err

View file

@ -20,6 +20,8 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"github.com/syndtr/goleveldb/leveldb"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/misc" "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) { 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. // 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. // 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() defer blockchain.Stop()
b := &BlockGen{i: i, parent: parent, chain: blocks, chainReader: blockchain, statedb: statedb, config: config, engine: engine} 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) gspec := new(Genesis)
db, _ := ethdb.NewMemDatabase() db, _ := ethdb.NewMemDatabase()
genesis := gspec.MustCommit(db) genesis := gspec.MustCommit(db)
blockExplorerDb, err := leveldb.OpenFile("./foo_data/", nil)
blockchain, _ := NewBlockChain(db, db, nil, params.AllEthashProtocolChanges, engine, vm.Config{}) blockchain, _ := NewBlockChain(db, blockExplorerDb, nil, params.AllEthashProtocolChanges, engine, vm.Config{})
// Create and inject the requested chain // Create and inject the requested chain
if n == 0 { if n == 0 {
return db, blockchain, nil return db, blockchain, nil
@ -262,7 +265,10 @@ func newCanonical(engine consensus.Engine, n int, full bool) (ethdb.Database, *B
} }
// Header-only chain requested // Header-only chain requested
headers := makeHeaderChain(genesis.Header(), n, engine, db, canonicalSeed) 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 return db, blockchain, err
} }

View file

@ -21,8 +21,8 @@ import (
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"math/big" "math/big"
"fmt"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
@ -455,6 +455,7 @@ func WriteBlock(db ethdb.Putter, block *types.Block) error {
if err := WriteHeader(db, block.Header()); err != nil { if err := WriteHeader(db, block.Header()); err != nil {
return err return err
} }
return nil return nil
} }
@ -462,6 +463,7 @@ func WriteBlock(db ethdb.Putter, block *types.Block) error {
// as a single receipt slice. This is used during chain reorganisations for // as a single receipt slice. This is used during chain reorganisations for
// rescheduling dropped transactions. // rescheduling dropped transactions.
func WriteBlockReceipts(db ethdb.Putter, hash common.Hash, number uint64, receipts types.Receipts) error { func WriteBlockReceipts(db ethdb.Putter, hash common.Hash, number uint64, receipts types.Receipts) error {
// Convert the receipts into their storage form and serialize them // Convert the receipts into their storage form and serialize them
storageReceipts := make([]*types.ReceiptForStorage, len(receipts)) storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
for i, receipt := range receipts { for i, receipt := range receipts {
@ -473,6 +475,7 @@ func WriteBlockReceipts(db ethdb.Putter, hash common.Hash, number uint64, receip
} }
// Store the flattened receipt slice // Store the flattened receipt slice
key := append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...) key := append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
if err := db.Put(key, bytes); err != nil { if err := db.Put(key, bytes); err != nil {
log.Crit("Failed to store block receipts", "err", err) log.Crit("Failed to store block receipts", "err", err)
} }

View file

@ -24,7 +24,7 @@ import (
"runtime" "runtime"
"sync" "sync"
"sync/atomic" "sync/atomic"
"github.com/syndtr/goleveldb/leveldb"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
@ -74,7 +74,7 @@ type Ethereum struct {
// DB interfaces // DB interfaces
chainDb ethdb.Database // Block chain database chainDb ethdb.Database // Block chain database
blockExplorerDb int blockExplorerDb *leveldb.DB
eventMux *event.TypeMux eventMux *event.TypeMux
engine consensus.Engine engine consensus.Engine
@ -117,7 +117,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
} }
// @NOTE:shyft instantiate BlockExplorerDB here? // @NOTE:shyft instantiate BlockExplorerDB here?
blockExplorerDb, err := CreateDB(ctx, config, "blockExplorerDb") blockExplorerDb, err := leveldb.OpenFile("./shyftData/geth/blockExplorerDb/", nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -131,7 +131,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
eth := &Ethereum{ eth := &Ethereum{
config: config, config: config,
chainDb: chainDb, chainDb: chainDb,
blockExplorerDb: 42, blockExplorerDb: blockExplorerDb,
chainConfig: chainConfig, chainConfig: chainConfig,
eventMux: ctx.EventMux, eventMux: ctx.EventMux,
accountManager: ctx.AccountManager, 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 as the initial ring or append to the end
set.lock.Lock() set.lock.Lock()
defer set.lock.Unlock() defer set.lock.Unlock()
if set.blocks == nil { if set.blocks == nil {
set.blocks = item set.blocks = item
} else { } else {

View file

@ -309,9 +309,6 @@ func (self *worker) wait() {
for _, log := range work.state.Logs() { for _, log := range work.state.Logs() {
log.BlockHash = block.Hash() 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) stat, err := self.chain.WriteBlockWithState(block, work.receipts, work.state)
if err != nil { if err != nil {
log.Error("Failed writing block to chain", "err", err) log.Error("Failed writing block to chain", "err", err)

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 // previous can be found) from within the node's instance directory. If the node is
// ephemeral, a memory database is returned. // ephemeral, a memory database is returned.
func (n *Node) OpenDatabase(name string, cache, handles int) (ethdb.Database, error) { func (n *Node) OpenDatabase(name string, cache, handles int) (ethdb.Database, error) {
fmt.Println("DSAhhysagdhkajsndjgasuygdvkhasbdjhs")
if n.config.DataDir == "" { if n.config.DataDir == "" {
return ethdb.NewMemDatabase() return ethdb.NewMemDatabase()
} }

4
run_js_in_geth.sh Normal file
View file

@ -0,0 +1,4 @@
file=$1
echo $file
runthing="./build/bin/geth --exec 'loadScript(\""$file"\")' attach http://127.0.0.1:8545"
eval $runthing

View file

@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License // You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package ethdb package shyftdb
import ( import (
"strconv" "strconv"

View file

@ -1,5 +1,5 @@
package ethdb package shyftdb
// Code using batches should try to add this much data to the batch. // Code using batches should try to add this much data to the batch.
// The value was determined empirically. // The value was determined empirically.

View file

@ -0,0 +1,74 @@
package shyftdb
import (
"fmt"
"bytes"
"encoding/gob"
"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 WriteBlock(db *leveldb.DB, block *types.Block) error {
leng := block.Transactions().Len()
var tx_strs = make([]string, leng)
//var tx_bytes = make([]byte, leng)
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 {
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
}
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)
}
GetTransaction(db, tx)
}
GetAllTransactions(db)
return nil
}
// Meant for internal tests
func GetBlock(db *leveldb.DB, block *types.Block) {
hash := block.Header().Hash().Bytes()
data, err := db.Get(hash, nil)
if err != nil {
log.Crit("Could not retrieve block", "err", err)
}
fmt.Println("\nBLOCK Value: " + string(data))
}
func GetAllTransactions(db *leveldb.DB) {
iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil)
for iter.Next() {
fmt.Println("\nALL TX VALUE: " + string(iter.Value()))
}
iter.Release()
}
func GetTransaction (db *leveldb.DB, tx *types.Transaction) {
key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...)
data, err := db.Get(key, nil)
if err != nil {
log.Crit("Could not retrieve TX", "err", err)
}
if len(data) > 0 {
fmt.Println("\nTX Value: " + string(data))
}
}

View file

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

View file

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

View file

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

View file

@ -0,0 +1,30 @@
var firstAccount = web3.eth.accounts[0]
var secondAccount = web3.eth.accounts[1]
var thirdAccount = web3.eth.accounts[2]
for (var i = 0; i < 1; i++) {
console.log('\t\t' + (i + 1) + ' - Transactions')
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
});
}