aboutsummaryrefslogtreecommitdiffstats
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/blocktest/main.go213
-rw-r--r--cmd/ethereum/admin.go9
-rw-r--r--cmd/ethereum/main.go2
-rw-r--r--cmd/ethtest/main.go68
-rw-r--r--cmd/utils/cmd.go36
5 files changed, 69 insertions, 259 deletions
diff --git a/cmd/blocktest/main.go b/cmd/blocktest/main.go
deleted file mode 100644
index acbadee77..000000000
--- a/cmd/blocktest/main.go
+++ /dev/null
@@ -1,213 +0,0 @@
-/*
- This file is part of go-ethereum
-
- go-ethereum is free software: you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- go-ethereum is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
-*/
-/**
- * @authors
- * Gustav Simonsson <gustav.simonsson@gmail.com>
- * @date 2015
- *
- */
-
-package main
-
-import (
- "bytes"
- "encoding/hex"
- "encoding/json"
- "flag"
- "fmt"
- "io/ioutil"
- "log"
- "math/big"
- "os"
- "runtime"
- "strings"
-
- "github.com/ethereum/go-ethereum/cmd/utils"
- "github.com/ethereum/go-ethereum/core"
- types "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/ethdb"
- "github.com/ethereum/go-ethereum/event"
- "github.com/ethereum/go-ethereum/logger"
- "github.com/ethereum/go-ethereum/rlp"
-)
-
-type Account struct {
- Balance string
- Code string
- Nonce string
- Storage map[string]string
-}
-
-type BlockHeader struct {
- Bloom string
- Coinbase string
- Difficulty string
- ExtraData string
- GasLimit string
- GasUsed string
- MixHash string
- Nonce string
- Number string
- ParentHash string
- ReceiptTrie string
- SeedHash string
- StateRoot string
- Timestamp string
- TransactionsTrie string
- UncleHash string
-}
-
-type Tx struct {
- Data string
- GasLimit string
- GasPrice string
- Nonce string
- R string
- S string
- To string
- V string
- Value string
-}
-
-type Block struct {
- BlockHeader BlockHeader
- Rlp string
- Transactions []Tx
- UncleHeaders []string
-}
-
-type Test struct {
- Blocks []Block
- GenesisBlockHeader BlockHeader
- Pre map[string]Account
-}
-
-func main() {
- flag.Usage = func() {
- fmt.Fprintf(os.Stderr, "%s <testfile>\n", os.Args[0])
- flag.PrintDefaults()
- }
- flag.Parse()
-
- runtime.GOMAXPROCS(runtime.NumCPU())
- logger.AddLogSystem(logger.NewStdLogSystem(os.Stderr, log.LstdFlags, logger.DebugDetailLevel))
- defer func() { logger.Flush() }()
-
- if len(os.Args) < 2 {
- utils.Fatalf("Please specify a test file as the first argument.")
- }
- blocks, err := loadBlocksFromTestFile(os.Args[1])
- if err != nil {
- utils.Fatalf("Could not load blocks: %v", err)
- }
-
- chain := memchain()
- chain.ResetWithGenesisBlock(blocks[0])
- if err = chain.InsertChain(types.Blocks{blocks[1]}); err != nil {
- utils.Fatalf("Error: %v", err)
- } else {
- fmt.Println("PASS")
- }
-}
-
-func memchain() *core.ChainManager {
- blockdb, err := ethdb.NewMemDatabase()
- if err != nil {
- utils.Fatalf("Could not create in-memory database: %v", err)
- }
- statedb, err := ethdb.NewMemDatabase()
- if err != nil {
- utils.Fatalf("Could not create in-memory database: %v", err)
- }
- return core.NewChainManager(blockdb, statedb, new(event.TypeMux))
-}
-
-func loadBlocksFromTestFile(filePath string) (blocks types.Blocks, err error) {
- fileContent, err := ioutil.ReadFile(filePath)
- if err != nil {
- return
- }
- bt := make(map[string]Test)
- if err = json.Unmarshal(fileContent, &bt); err != nil {
- return
- }
-
- // TODO: support multiple blocks; loop over all blocks
- gbh := new(types.Header)
-
- // Let's use slighlty different namings for the same things, because that's awesome.
- gbh.ParentHash, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.ParentHash)
- gbh.UncleHash, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.UncleHash)
- gbh.Coinbase, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.Coinbase)
- gbh.Root, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.StateRoot)
- gbh.TxHash, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.TransactionsTrie)
- gbh.ReceiptHash, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.ReceiptTrie)
- gbh.Bloom, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.Bloom)
-
- gbh.MixDigest, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.MixHash)
- //gbh.SeedHash, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.SeedHash)
-
- d, _ := new(big.Int).SetString(bt["SimpleTx"].GenesisBlockHeader.Difficulty, 10)
- gbh.Difficulty = d
-
- n, _ := new(big.Int).SetString(bt["SimpleTx"].GenesisBlockHeader.Number, 10)
- gbh.Number = n
-
- gl, _ := new(big.Int).SetString(bt["SimpleTx"].GenesisBlockHeader.GasLimit, 10)
- gbh.GasLimit = gl
-
- gu, _ := new(big.Int).SetString(bt["SimpleTx"].GenesisBlockHeader.GasUsed, 10)
- gbh.GasUsed = gu
-
- ts, _ := new(big.Int).SetString(bt["SimpleTx"].GenesisBlockHeader.Timestamp, 0)
- gbh.Time = ts.Uint64()
-
- extra, err := hex_decode(bt["SimpleTx"].GenesisBlockHeader.ExtraData)
- gbh.Extra = string(extra) // TODO: change ExtraData to byte array
-
- nonce, _ := hex_decode(bt["SimpleTx"].GenesisBlockHeader.Nonce)
- gbh.Nonce = nonce
-
- if err != nil {
- return
- }
-
- gb := types.NewBlockWithHeader(gbh)
- //gb.uncles = *new([]*types.Header)
- //gb.transactions = *new(types.Transactions)
- gb.Td = new(big.Int)
- gb.Reward = new(big.Int)
-
- testBlock := new(types.Block)
-
- rlpBytes, err := hex_decode(bt["SimpleTx"].Blocks[0].Rlp)
- err = rlp.Decode(bytes.NewReader(rlpBytes), &testBlock)
- if err != nil {
- return
- }
-
- blocks = types.Blocks{
- gb,
- testBlock,
- }
-
- return
-}
-
-func hex_decode(s string) (res []byte, err error) {
- return hex.DecodeString(strings.TrimPrefix(s, "0x"))
-}
diff --git a/cmd/ethereum/admin.go b/cmd/ethereum/admin.go
index 880a22c22..41aaf46d8 100644
--- a/cmd/ethereum/admin.go
+++ b/cmd/ethereum/admin.go
@@ -8,8 +8,8 @@ import (
"time"
"github.com/ethereum/go-ethereum/cmd/utils"
- "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/state"
@@ -221,13 +221,10 @@ func (js *jsre) exportChain(call otto.FunctionCall) otto.Value {
fmt.Println(err)
return otto.FalseValue()
}
-
- data := js.ethereum.ChainManager().Export()
- if err := common.WriteFile(fn, data); err != nil {
+ if err := utils.ExportChain(js.ethereum.ChainManager(), fn); err != nil {
fmt.Println(err)
return otto.FalseValue()
}
-
return otto.TrueValue()
}
@@ -239,7 +236,7 @@ func (js *jsre) dumpBlock(call otto.FunctionCall) otto.Value {
block = js.ethereum.ChainManager().GetBlockByNumber(uint64(num))
} else if call.Argument(0).IsString() {
hash, _ := call.Argument(0).ToString()
- block = js.ethereum.ChainManager().GetBlock(common.Hex2Bytes(hash))
+ block = js.ethereum.ChainManager().GetBlock(common.HexToHash(hash))
} else {
fmt.Println("invalid argument for dump. Either hex string or number")
}
diff --git a/cmd/ethereum/main.go b/cmd/ethereum/main.go
index 87f588de8..7d5854230 100644
--- a/cmd/ethereum/main.go
+++ b/cmd/ethereum/main.go
@@ -314,7 +314,7 @@ func dump(ctx *cli.Context) {
for _, arg := range ctx.Args() {
var block *types.Block
if hashish(arg) {
- block = chainmgr.GetBlock(common.Hex2Bytes(arg))
+ block = chainmgr.GetBlock(common.HexToHash(arg))
} else {
num, _ := strconv.Atoi(arg)
block = chainmgr.GetBlockByNumber(uint64(num))
diff --git a/cmd/ethtest/main.go b/cmd/ethtest/main.go
index cf1ec6dfa..f2f7d27f3 100644
--- a/cmd/ethtest/main.go
+++ b/cmd/ethtest/main.go
@@ -33,6 +33,7 @@ import (
"strings"
"github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/state"
@@ -66,7 +67,7 @@ type Account struct {
}
func StateObjectFromAccount(db common.Database, addr string, account Account) *state.StateObject {
- obj := state.NewStateObject(common.Hex2Bytes(addr), db)
+ obj := state.NewStateObject(common.HexToAddress(addr), db)
obj.SetBalance(common.Big(account.Balance))
if common.IsHex(account.Code) {
@@ -112,7 +113,7 @@ func RunVmTest(r io.Reader) (failed int) {
for name, test := range tests {
db, _ := ethdb.NewMemDatabase()
- statedb := state.New(nil, db)
+ statedb := state.New(common.Hash{}, db)
for addr, account := range test.Pre {
obj := StateObjectFromAccount(db, addr, account)
statedb.SetStateObject(obj)
@@ -135,63 +136,82 @@ func RunVmTest(r io.Reader) (failed int) {
rexp := helper.FromHex(test.Out)
if bytes.Compare(rexp, ret) != 0 {
- helper.Log.Infof("FAIL: %s's return failed. Expected %x, got %x\n", name, rexp, ret)
+ helper.Log.Infof("%s's return failed. Expected %x, got %x\n", name, rexp, ret)
failed = 1
}
for addr, account := range test.Post {
- obj := statedb.GetStateObject(helper.FromHex(addr))
+ obj := statedb.GetStateObject(common.HexToAddress(addr))
if obj == nil {
continue
}
if len(test.Exec) == 0 {
if obj.Balance().Cmp(common.Big(account.Balance)) != 0 {
- helper.Log.Infof("FAIL: %s's : (%x) balance failed. Expected %v, got %v => %v\n",
- name,
- obj.Address()[:4],
- account.Balance,
- obj.Balance(),
- new(big.Int).Sub(common.Big(account.Balance), obj.Balance()),
- )
+ helper.Log.Infof("%s's : (%x) balance failed. Expected %v, got %v => %v\n", name, obj.Address().Bytes()[:4], account.Balance, obj.Balance(), new(big.Int).Sub(common.Big(account.Balance), obj.Balance()))
failed = 1
}
}
for addr, value := range account.Storage {
- v := obj.GetState(helper.FromHex(addr)).Bytes()
+ v := obj.GetState(common.HexToHash(addr)).Bytes()
vexp := helper.FromHex(value)
if bytes.Compare(v, vexp) != 0 {
- helper.Log.Infof("FAIL: %s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address()[0:4], addr, vexp, v, common.BigD(vexp), common.BigD(v))
+ helper.Log.Infof("%s's : (%x: %s) storage failed. Expected %x, got %x (%v %v)\n", name, obj.Address().Bytes()[0:4], addr, vexp, v, common.BigD(vexp), common.BigD(v))
failed = 1
}
}
}
- if !bytes.Equal(common.Hex2Bytes(test.PostStateRoot), statedb.Root()) {
- helper.Log.Infof("FAIL: %s's : Post state root error. Expected %s, got %x\n", name, test.PostStateRoot, statedb.Root())
+ statedb.Sync()
+ //if !bytes.Equal(common.Hex2Bytes(test.PostStateRoot), statedb.Root()) {
+ if common.HexToHash(test.PostStateRoot) != statedb.Root() {
+ helper.Log.Infof("%s's : Post state root failed. Expected %s, got %x", name, test.PostStateRoot, statedb.Root())
failed = 1
}
if len(test.Logs) > 0 {
if len(test.Logs) != len(logs) {
- helper.Log.Infof("FAIL: log length mismatch. Expected %d, got %d", len(test.Logs), len(logs))
+ helper.Log.Infof("log length failed. Expected %d, got %d", len(test.Logs), len(logs))
failed = 1
} else {
- /*
- fmt.Println("A", test.Logs)
- fmt.Println("B", logs)
- for i, log := range test.Logs {
- genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256)
- if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
- t.Errorf("bloom mismatch")
+ for i, log := range test.Logs {
+ if common.HexToAddress(log.AddressF) != logs[i].Address() {
+ helper.Log.Infof("'%s' log address failed. Expected %v got %x", name, log.AddressF, logs[i].Address())
+ failed = 1
+ }
+
+ if !bytes.Equal(logs[i].Data(), helper.FromHex(log.DataF)) {
+ helper.Log.Infof("'%s' log data failed. Expected %v got %x", name, log.DataF, logs[i].Data())
+ failed = 1
+ }
+
+ if len(log.TopicsF) != len(logs[i].Topics()) {
+ helper.Log.Infof("'%s' log topics length failed. Expected %d got %d", name, len(log.TopicsF), logs[i].Topics())
+ failed = 1
+ } else {
+ for j, topic := range log.TopicsF {
+ if common.HexToHash(topic) != logs[i].Topics()[j] {
+ helper.Log.Infof("'%s' log topic[%d] failed. Expected %v got %x", name, j, topic, logs[i].Topics()[j])
+ failed = 1
}
}
- */
+ }
+ genBloom := common.LeftPadBytes(types.LogsBloom(state.Logs{logs[i]}).Bytes(), 256)
+
+ if !bytes.Equal(genBloom, common.Hex2Bytes(log.BloomF)) {
+ helper.Log.Infof("'%s' bloom failed.", name)
+ failed = 1
+ }
+ }
}
}
+ if failed == 1 {
+ helper.Log.Infoln(string(statedb.Dump()))
+ }
+
logger.Flush()
}
diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go
index 74fd5334e..feea29d64 100644
--- a/cmd/utils/cmd.go
+++ b/cmd/utils/cmd.go
@@ -23,14 +23,15 @@ package utils
import (
"fmt"
+ "io"
"os"
"os/signal"
"regexp"
+ "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth"
- "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/rlp"
)
@@ -152,29 +153,34 @@ func ImportChain(chainmgr *core.ChainManager, fn string) error {
}
defer fh.Close()
- var blocks types.Blocks
- if err := rlp.Decode(fh, &blocks); err != nil {
- return err
- }
-
chainmgr.Reset()
- if err := chainmgr.InsertChain(blocks); err != nil {
- return err
+ stream := rlp.NewStream(fh)
+ var i int
+ for ; ; i++ {
+ var b types.Block
+ if err := stream.Decode(&b); err == io.EOF {
+ break
+ } else if err != nil {
+ return fmt.Errorf("at block %d: %v", i, err)
+ }
+ if err := chainmgr.InsertChain(types.Blocks{&b}); err != nil {
+ return fmt.Errorf("invalid block %d: %v", i, err)
+ }
}
- fmt.Printf("imported %d blocks\n", len(blocks))
-
+ fmt.Printf("imported %d blocks\n", i)
return nil
}
func ExportChain(chainmgr *core.ChainManager, fn string) error {
fmt.Printf("exporting blockchain '%s'\n", fn)
-
- data := chainmgr.Export()
-
- if err := common.WriteFile(fn, data); err != nil {
+ fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
+ if err != nil {
+ return err
+ }
+ defer fh.Close()
+ if err := chainmgr.Export(fh); err != nil {
return err
}
fmt.Printf("exported blockchain\n")
-
return nil
}