good morning!!!!

Skip to content
Snippets Groups Projects
Commit 9e5257b8 authored by Jeffrey Wilcke's avatar Jeffrey Wilcke
Browse files

Chain importer

parent 4cd79d8d
Branches
Tags
No related merge requests found
...@@ -58,6 +58,7 @@ var ( ...@@ -58,6 +58,7 @@ var (
DumpHash string DumpHash string
DumpNumber int DumpNumber int
VmType int VmType int
ImportChain string
) )
// flags specific to cli client // flags specific to cli client
...@@ -104,6 +105,7 @@ func Init() { ...@@ -104,6 +105,7 @@ func Init() {
flag.BoolVar(&DiffTool, "difftool", false, "creates output for diff'ing. Sets LogLevel=0") flag.BoolVar(&DiffTool, "difftool", false, "creates output for diff'ing. Sets LogLevel=0")
flag.StringVar(&DiffType, "diff", "all", "sets the level of diff output [vm, all]. Has no effect if difftool=false") flag.StringVar(&DiffType, "diff", "all", "sets the level of diff output [vm, all]. Has no effect if difftool=false")
flag.BoolVar(&ShowGenesis, "genesis", false, "Dump the genesis block") flag.BoolVar(&ShowGenesis, "genesis", false, "Dump the genesis block")
flag.StringVar(&ImportChain, "chain", "", "Imports fiven chain")
flag.BoolVar(&Dump, "dump", false, "output the ethereum state in JSON format. Sub args [number, hash]") flag.BoolVar(&Dump, "dump", false, "output the ethereum state in JSON format. Sub args [number, hash]")
flag.StringVar(&DumpHash, "hash", "", "specify arg in hex") flag.StringVar(&DumpHash, "hash", "", "specify arg in hex")
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"os" "os"
"runtime" "runtime"
...@@ -26,6 +27,7 @@ import ( ...@@ -26,6 +27,7 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/rlp"
) )
const ( const (
...@@ -38,6 +40,10 @@ var clilogger = logger.NewLogger("CLI") ...@@ -38,6 +40,10 @@ var clilogger = logger.NewLogger("CLI")
func main() { func main() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
defer func() {
logger.Flush()
}()
utils.HandleInterrupt() utils.HandleInterrupt()
// precedence: code-internal flag default < config file < environment variables < command line // precedence: code-internal flag default < config file < environment variables < command line
...@@ -112,6 +118,27 @@ func main() { ...@@ -112,6 +118,27 @@ func main() {
utils.StartMining(ethereum) utils.StartMining(ethereum)
} }
if len(ImportChain) > 0 {
clilogger.Infof("importing chain '%s'\n", ImportChain)
c, err := ethutil.ReadAllFile(ImportChain)
if err != nil {
clilogger.Infoln(err)
return
}
var chain types.Blocks
if err := rlp.Decode(bytes.NewReader([]byte(c)), &chain); err != nil {
clilogger.Infoln(err)
return
}
ethereum.ChainManager().Reset()
if err := ethereum.ChainManager().InsertChain(chain); err != nil {
clilogger.Infoln(err)
return
}
clilogger.Infof("imported %d blocks\n", len(chain))
}
// better reworked as cases // better reworked as cases
if StartJsConsole { if StartJsConsole {
InitJsConsole(ethereum) InitJsConsole(ethereum)
...@@ -131,5 +158,4 @@ func main() { ...@@ -131,5 +158,4 @@ func main() {
// this blocks the thread // this blocks the thread
ethereum.WaitForShutdown() ethereum.WaitForShutdown()
logger.Flush()
} }
...@@ -304,7 +304,7 @@ func (sm *BlockManager) AccumelateRewards(statedb *state.StateDB, block, parent ...@@ -304,7 +304,7 @@ func (sm *BlockManager) AccumelateRewards(statedb *state.StateDB, block, parent
knownUncles := set.New() knownUncles := set.New()
for _, uncle := range parent.Uncles() { for _, uncle := range parent.Uncles() {
knownUncles.Add(uncle.Hash()) knownUncles.Add(string(uncle.Hash()))
} }
nonces := ethutil.NewSet(block.Header().Nonce) nonces := ethutil.NewSet(block.Header().Nonce)
...@@ -323,7 +323,7 @@ func (sm *BlockManager) AccumelateRewards(statedb *state.StateDB, block, parent ...@@ -323,7 +323,7 @@ func (sm *BlockManager) AccumelateRewards(statedb *state.StateDB, block, parent
return UncleError("Uncle too old") return UncleError("Uncle too old")
} }
if knownUncles.Has(uncle.Hash()) { if knownUncles.Has(string(uncle.Hash())) {
return UncleError("Uncle in chain") return UncleError("Uncle in chain")
} }
......
...@@ -204,9 +204,6 @@ func (bc *ChainManager) Reset() { ...@@ -204,9 +204,6 @@ func (bc *ChainManager) Reset() {
bc.currentBlock = bc.genesisBlock bc.currentBlock = bc.genesisBlock
bc.setTotalDifficulty(ethutil.Big("0")) bc.setTotalDifficulty(ethutil.Big("0"))
// Set the last know difficulty (might be 0x0 as initial value, Genesis)
bc.td = ethutil.BigD(ethutil.Config.Db.LastKnownTD())
} }
func (self *ChainManager) Export() []byte { func (self *ChainManager) Export() []byte {
...@@ -219,9 +216,7 @@ func (self *ChainManager) Export() []byte { ...@@ -219,9 +216,7 @@ func (self *ChainManager) Export() []byte {
for block := self.currentBlock; block != nil; block = self.GetBlock(block.Header().ParentHash) { for block := self.currentBlock; block != nil; block = self.GetBlock(block.Header().ParentHash) {
blocks[block.NumberU64()] = block blocks[block.NumberU64()] = block
} }
//fmt.Println(blocks)
return nil
return ethutil.Encode(blocks) return ethutil.Encode(blocks)
} }
......
...@@ -209,15 +209,21 @@ func (self *Block) HashNoNonce() []byte { ...@@ -209,15 +209,21 @@ func (self *Block) HashNoNonce() []byte {
func (self *Block) String() string { func (self *Block) String() string {
return fmt.Sprintf(`BLOCK(%x): Size: %v { return fmt.Sprintf(`BLOCK(%x): Size: %v {
Header:
[
%v %v
]
Transactions:
%v %v
Uncles:
%v %v
} }
`, self.header.Hash(), self.Size(), self.header, self.uncles, self.transactions) `, self.header.Hash(), self.Size(), self.header, self.transactions, self.uncles)
} }
func (self *Header) String() string { func (self *Header) String() string {
return fmt.Sprintf(`ParentHash: %x return fmt.Sprintf(`
ParentHash: %x
UncleHash: %x UncleHash: %x
Coinbase: %x Coinbase: %x
Root: %x Root: %x
......
...@@ -17,10 +17,10 @@ func IsContractAddr(addr []byte) bool { ...@@ -17,10 +17,10 @@ func IsContractAddr(addr []byte) bool {
type Transaction struct { type Transaction struct {
AccountNonce uint64 AccountNonce uint64
Price *big.Int
GasLimit *big.Int
Recipient []byte Recipient []byte
Amount *big.Int Amount *big.Int
GasAmount *big.Int
Price *big.Int
Payload []byte Payload []byte
V uint64 V uint64
R, S []byte R, S []byte
...@@ -31,7 +31,7 @@ func NewContractCreationTx(Amount, gasAmount, price *big.Int, data []byte) *Tran ...@@ -31,7 +31,7 @@ func NewContractCreationTx(Amount, gasAmount, price *big.Int, data []byte) *Tran
} }
func NewTransactionMessage(to []byte, Amount, gasAmount, price *big.Int, data []byte) *Transaction { func NewTransactionMessage(to []byte, Amount, gasAmount, price *big.Int, data []byte) *Transaction {
return &Transaction{Recipient: to, Amount: Amount, Price: price, GasAmount: gasAmount, Payload: data} return &Transaction{Recipient: to, Amount: Amount, Price: price, GasLimit: gasAmount, Payload: data}
} }
func NewTransactionFromBytes(data []byte) *Transaction { func NewTransactionFromBytes(data []byte) *Transaction {
...@@ -49,7 +49,7 @@ func NewTransactionFromAmount(val *ethutil.Value) *Transaction { ...@@ -49,7 +49,7 @@ func NewTransactionFromAmount(val *ethutil.Value) *Transaction {
} }
func (tx *Transaction) Hash() []byte { func (tx *Transaction) Hash() []byte {
data := []interface{}{tx.AccountNonce, tx.Price, tx.GasAmount, tx.Recipient, tx.Amount, tx.Payload} data := []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload}
return crypto.Sha3(ethutil.Encode(data)) return crypto.Sha3(ethutil.Encode(data))
} }
...@@ -59,7 +59,7 @@ func (self *Transaction) Data() []byte { ...@@ -59,7 +59,7 @@ func (self *Transaction) Data() []byte {
} }
func (self *Transaction) Gas() *big.Int { func (self *Transaction) Gas() *big.Int {
return self.GasAmount return self.GasLimit
} }
func (self *Transaction) GasPrice() *big.Int { func (self *Transaction) GasPrice() *big.Int {
...@@ -140,7 +140,7 @@ func (tx *Transaction) Sign(privk []byte) error { ...@@ -140,7 +140,7 @@ func (tx *Transaction) Sign(privk []byte) error {
} }
func (tx *Transaction) RlpData() interface{} { func (tx *Transaction) RlpData() interface{} {
data := []interface{}{tx.AccountNonce, tx.Price, tx.GasAmount, tx.Recipient, tx.Amount, tx.Payload} data := []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload}
return append(data, tx.V, new(big.Int).SetBytes(tx.R).Bytes(), new(big.Int).SetBytes(tx.S).Bytes()) return append(data, tx.V, new(big.Int).SetBytes(tx.R).Bytes(), new(big.Int).SetBytes(tx.S).Bytes())
} }
...@@ -156,7 +156,7 @@ func (tx *Transaction) RlpDecode(data []byte) { ...@@ -156,7 +156,7 @@ func (tx *Transaction) RlpDecode(data []byte) {
func (tx *Transaction) RlpValueDecode(decoder *ethutil.Value) { func (tx *Transaction) RlpValueDecode(decoder *ethutil.Value) {
tx.AccountNonce = decoder.Get(0).Uint() tx.AccountNonce = decoder.Get(0).Uint()
tx.Price = decoder.Get(1).BigInt() tx.Price = decoder.Get(1).BigInt()
tx.GasAmount = decoder.Get(2).BigInt() tx.GasLimit = decoder.Get(2).BigInt()
tx.Recipient = decoder.Get(3).Bytes() tx.Recipient = decoder.Get(3).Bytes()
tx.Amount = decoder.Get(4).BigInt() tx.Amount = decoder.Get(4).BigInt()
tx.Payload = decoder.Get(5).Bytes() tx.Payload = decoder.Get(5).Bytes()
...@@ -171,10 +171,10 @@ func (tx *Transaction) String() string { ...@@ -171,10 +171,10 @@ func (tx *Transaction) String() string {
Contract: %v Contract: %v
From: %x From: %x
To: %x To: %x
AccountNonce: %v Nonce: %v
GasAmountPrice: %v GasPrice: %v
GasAmount: %v GasLimit %v
Amount: %v Value: %v
Data: 0x%x Data: 0x%x
V: 0x%x V: 0x%x
R: 0x%x R: 0x%x
...@@ -184,10 +184,10 @@ func (tx *Transaction) String() string { ...@@ -184,10 +184,10 @@ func (tx *Transaction) String() string {
tx.Hash(), tx.Hash(),
len(tx.Recipient) == 0, len(tx.Recipient) == 0,
tx.From(), tx.From(),
tx.Recipient, tx.To(),
tx.AccountNonce, tx.AccountNonce,
tx.Price, tx.Price,
tx.GasAmount, tx.GasLimit,
tx.Amount, tx.Amount,
tx.Payload, tx.Payload,
tx.V, tx.V,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment