Skip to content

Commit 76e4e23

Browse files
committed
Merge branch 'poc-9' of github.com-obscure:ethereum/go-ethereum into poc-9
2 parents d44fe4e + 3b30765 commit 76e4e23

4 files changed

Lines changed: 377 additions & 1 deletion

File tree

‎cmd/blocktest/flags.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
This file is part of go-ethereum
3+
4+
go-ethereum is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
go-ethereum is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
/**
18+
* @authors
19+
* Gustav Simonsson <gustav.simonsson@gmail.com>
20+
*/
21+
package main
22+
23+
import (
24+
"flag"
25+
"fmt"
26+
"os"
27+
)
28+
29+
var (
30+
TestFile string
31+
)
32+
33+
func Init() {
34+
flag.Usage = func() {
35+
fmt.Fprintf(os.Stderr, "%s <testfile>\n", os.Args[0])
36+
flag.PrintDefaults()
37+
}
38+
flag.Parse()
39+
40+
TestFile = flag.Arg(0)
41+
}

‎cmd/blocktest/main.go‎

Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,320 @@
1+
/*
2+
This file is part of go-ethereum
3+
4+
go-ethereum is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU Lesser General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
go-ethereum is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public License
15+
along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
/**
18+
* @authors
19+
* Gustav Simonsson <gustav.simonsson@gmail.com>
20+
* @date 2015
21+
*
22+
*/
23+
24+
package main
25+
26+
import (
27+
"bytes"
28+
"crypto/ecdsa"
29+
"encoding/hex"
30+
"encoding/json"
31+
"fmt"
32+
"io/ioutil"
33+
"log"
34+
"math/big"
35+
"path"
36+
"runtime"
37+
"strconv"
38+
"strings"
39+
"time"
40+
41+
"github.com/ethereum/go-ethereum/cmd/utils"
42+
types "github.com/ethereum/go-ethereum/core/types"
43+
"github.com/ethereum/go-ethereum/eth"
44+
"github.com/ethereum/go-ethereum/ethutil"
45+
"github.com/ethereum/go-ethereum/logger"
46+
"github.com/ethereum/go-ethereum/p2p"
47+
"github.com/ethereum/go-ethereum/p2p/nat"
48+
"github.com/ethereum/go-ethereum/rlp"
49+
)
50+
51+
const (
52+
ClientIdentifier = "Ethereum(G)"
53+
Version = "0.8.6"
54+
)
55+
56+
type Account struct {
57+
Balance string
58+
Code string
59+
Nonce string
60+
Storage map[string]string
61+
}
62+
63+
type BlockHeader struct {
64+
Bloom string
65+
Coinbase string
66+
Difficulty string
67+
ExtraData string
68+
GasLimit string
69+
GasUsed string
70+
MixHash string
71+
Nonce string
72+
Number string
73+
ParentHash string
74+
ReceiptTrie string
75+
SeedHash string
76+
StateRoot string
77+
Timestamp string
78+
TransactionsTrie string
79+
UncleHash string
80+
}
81+
type Tx struct {
82+
Data string
83+
GasLimit string
84+
GasPrice string
85+
Nonce string
86+
R string
87+
S string
88+
To string
89+
V string
90+
Value string
91+
}
92+
93+
type Block struct {
94+
BlockHeader BlockHeader
95+
Rlp string
96+
Transactions []Tx
97+
UncleHeaders []string
98+
}
99+
100+
type Test struct {
101+
Blocks []Block
102+
GenesisBlockHeader BlockHeader
103+
Pre map[string]Account
104+
}
105+
106+
var (
107+
Identifier string
108+
KeyRing string
109+
DiffTool bool
110+
DiffType string
111+
KeyStore string
112+
StartRpc bool
113+
StartWebSockets bool
114+
RpcListenAddress string
115+
RpcPort int
116+
WsPort int
117+
OutboundPort string
118+
ShowGenesis bool
119+
AddPeer string
120+
MaxPeer int
121+
GenAddr bool
122+
BootNodes string
123+
NodeKey *ecdsa.PrivateKey
124+
NAT nat.Interface
125+
SecretFile string
126+
ExportDir string
127+
NonInteractive bool
128+
Datadir string
129+
LogFile string
130+
ConfigFile string
131+
DebugFile string
132+
LogLevel int
133+
LogFormat string
134+
Dump bool
135+
DumpHash string
136+
DumpNumber int
137+
VmType int
138+
ImportChain string
139+
SHH bool
140+
Dial bool
141+
PrintVersion bool
142+
MinerThreads int
143+
)
144+
145+
// flags specific to cli client
146+
var (
147+
StartMining bool
148+
StartJsConsole bool
149+
InputFile string
150+
)
151+
152+
func main() {
153+
init_vars()
154+
155+
Init()
156+
157+
if len(TestFile) < 1 {
158+
log.Fatal("Please specify test file")
159+
}
160+
blocks, err := loadBlocksFromTestFile(TestFile)
161+
if err != nil {
162+
panic(err)
163+
}
164+
165+
runtime.GOMAXPROCS(runtime.NumCPU())
166+
167+
defer func() {
168+
logger.Flush()
169+
}()
170+
171+
utils.HandleInterrupt()
172+
173+
utils.InitConfig(VmType, ConfigFile, Datadir, "ethblocktest")
174+
175+
ethereum, err := eth.New(&eth.Config{
176+
Name: p2p.MakeName(ClientIdentifier, Version),
177+
KeyStore: KeyStore,
178+
DataDir: Datadir,
179+
LogFile: LogFile,
180+
LogLevel: LogLevel,
181+
LogFormat: LogFormat,
182+
MaxPeers: MaxPeer,
183+
Port: OutboundPort,
184+
NAT: NAT,
185+
KeyRing: KeyRing,
186+
Shh: true,
187+
Dial: Dial,
188+
BootNodes: BootNodes,
189+
NodeKey: NodeKey,
190+
MinerThreads: MinerThreads,
191+
})
192+
193+
utils.StartRpc(ethereum, RpcListenAddress, RpcPort)
194+
utils.StartEthereum(ethereum)
195+
196+
ethereum.ChainManager().ResetWithGenesisBlock(blocks[0])
197+
198+
// fmt.Println("HURR: ", hex.EncodeToString(ethutil.Encode(blocks[0].RlpData())))
199+
200+
go ethereum.ChainManager().InsertChain(types.Blocks{blocks[1]})
201+
fmt.Println("OK! ")
202+
ethereum.WaitForShutdown()
203+
}
204+
205+
func loadBlocksFromTestFile(filePath string) (blocks types.Blocks, err error) {
206+
fileContent, err := ioutil.ReadFile(filePath)
207+
if err != nil {
208+
return
209+
}
210+
bt := *new(map[string]Test)
211+
err = json.Unmarshal(fileContent, &bt)
212+
if err != nil {
213+
return
214+
}
215+
216+
// TODO: support multiple blocks; loop over all blocks
217+
gbh := new(types.Header)
218+
219+
// Let's use slighlty different namings for the same things, because that's awesome.
220+
gbh.ParentHash, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.ParentHash)
221+
gbh.UncleHash, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.UncleHash)
222+
gbh.Coinbase, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.Coinbase)
223+
gbh.Root, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.StateRoot)
224+
gbh.TxHash, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.TransactionsTrie)
225+
gbh.ReceiptHash, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.ReceiptTrie)
226+
gbh.Bloom, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.Bloom)
227+
228+
gbh.MixDigest, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.MixHash)
229+
gbh.SeedHash, err = hex_decode(bt["SimpleTx"].GenesisBlockHeader.SeedHash)
230+
231+
d, _ := new(big.Int).SetString(bt["SimpleTx"].GenesisBlockHeader.Difficulty, 10)
232+
gbh.Difficulty = d
233+
234+
n, _ := new(big.Int).SetString(bt["SimpleTx"].GenesisBlockHeader.Number, 10)
235+
gbh.Number = n
236+
237+
gl, _ := new(big.Int).SetString(bt["SimpleTx"].GenesisBlockHeader.GasLimit, 10)
238+
gbh.GasLimit = gl
239+
240+
gu, _ := new(big.Int).SetString(bt["SimpleTx"].GenesisBlockHeader.GasUsed, 10)
241+
gbh.GasUsed = gu
242+
243+
ts, _ := new(big.Int).SetString(bt["SimpleTx"].GenesisBlockHeader.Timestamp, 0)
244+
gbh.Time = ts.Uint64()
245+
246+
extra, err := hex_decode(bt["SimpleTx"].GenesisBlockHeader.ExtraData)
247+
gbh.Extra = string(extra) // TODO: change ExtraData to byte array
248+
249+
nonce, _ := hex_decode(bt["SimpleTx"].GenesisBlockHeader.Nonce)
250+
gbh.Nonce = nonce
251+
252+
if err != nil {
253+
return
254+
}
255+
256+
gb := types.NewBlockWithHeader(gbh)
257+
gb.Reward = new(big.Int)
258+
259+
testBlock := new(types.Block)
260+
261+
rlpBytes, err := hex_decode(bt["SimpleTx"].Blocks[0].Rlp)
262+
err = rlp.Decode(bytes.NewReader(rlpBytes), &testBlock)
263+
if err != nil {
264+
return
265+
}
266+
267+
blocks = types.Blocks{
268+
gb,
269+
testBlock,
270+
}
271+
272+
return
273+
}
274+
275+
func init_vars() {
276+
VmType = 0
277+
Identifier = ""
278+
KeyRing = ""
279+
KeyStore = "db"
280+
RpcListenAddress = "127.0.0.1"
281+
RpcPort = 8545
282+
WsPort = 40404
283+
StartRpc = true
284+
StartWebSockets = false
285+
NonInteractive = false
286+
GenAddr = false
287+
SecretFile = ""
288+
ExportDir = ""
289+
LogFile = ""
290+
291+
timeStr := strconv.FormatInt(time.Now().UnixNano(), 10)
292+
293+
Datadir = path.Join(ethutil.DefaultDataDir(), timeStr)
294+
ConfigFile = path.Join(ethutil.DefaultDataDir(), timeStr, "conf.ini")
295+
296+
DebugFile = ""
297+
LogLevel = 5
298+
LogFormat = "std"
299+
DiffTool = false
300+
DiffType = "all"
301+
ShowGenesis = false
302+
ImportChain = ""
303+
Dump = false
304+
DumpHash = ""
305+
DumpNumber = -1
306+
StartMining = false
307+
StartJsConsole = false
308+
PrintVersion = false
309+
MinerThreads = runtime.NumCPU()
310+
311+
Dial = false
312+
OutboundPort = "30303"
313+
BootNodes = ""
314+
MaxPeer = 1
315+
316+
}
317+
318+
func hex_decode(s string) (res []byte, err error) {
319+
return hex.DecodeString(strings.TrimPrefix(s, "0x"))
320+
}

‎core/chain_manager.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,21 @@ func (bc *ChainManager) Reset() {
234234
bc.setTotalDifficulty(ethutil.Big("0"))
235235
}
236236

237+
func (bc *ChainManager) ResetWithGenesisBlock(gb *types.Block) {
238+
bc.mu.Lock()
239+
defer bc.mu.Unlock()
240+
241+
for block := bc.currentBlock; block != nil; block = bc.GetBlock(block.Header().ParentHash) {
242+
bc.db.Delete(block.Hash())
243+
}
244+
245+
// Prepare the genesis block
246+
bc.genesisBlock = gb
247+
bc.write(bc.genesisBlock)
248+
bc.insert(bc.genesisBlock)
249+
bc.currentBlock = bc.genesisBlock
250+
}
251+
237252
func (self *ChainManager) Export() []byte {
238253
self.mu.RLock()
239254
defer self.mu.RUnlock()

‎core/error.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (err *ParentErr) Error() string {
2222
}
2323

2424
func ParentError(hash []byte) error {
25-
return &ParentErr{Message: fmt.Sprintf("Block's parent unkown %x", hash)}
25+
return &ParentErr{Message: fmt.Sprintf("Block's parent unknown %x", hash)}
2626
}
2727

2828
func IsParentErr(err error) bool {

0 commit comments

Comments
 (0)