mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 01:43:47 +00:00
Merge pull request #2 from ShyftNetwork/store-transactions
Store transactions
This commit is contained in:
commit
830c48b630
5 changed files with 72 additions and 55 deletions
|
|
@ -20,8 +20,8 @@ package core
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
//"bytes"
|
||||
//"encoding/gob"
|
||||
"io"
|
||||
"math/big"
|
||||
mrand "math/rand"
|
||||
|
|
@ -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
|
||||
}
|
||||
result := 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
|
||||
|
|
@ -920,13 +920,13 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
|
|||
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)
|
||||
//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 {
|
||||
|
|
|
|||
|
|
@ -448,15 +448,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.Hash())
|
||||
fmt.Println("TX HASH")
|
||||
fmt.Println(tx.To().Hex())
|
||||
}
|
||||
}
|
||||
if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -5,33 +5,15 @@ import (
|
|||
"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 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"}
|
||||
|
|
@ -39,9 +21,55 @@ func WriteBlock(db *leveldb.DB, block *types.Block) error {
|
|||
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)
|
||||
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)
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
|
@ -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],
|
||||
|
|
|
|||
Loading…
Reference in a new issue