aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/blocktest/main.go
blob: 4a05b8bee8ab8e82fb2b010eb758f1e5a7afe3c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
    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"
    "crypto/ecdsa"
    "encoding/hex"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "math/big"
    "path"
    "runtime"
    "strconv"
    "strings"
    "time"

    "github.com/ethereum/go-ethereum/cmd/utils"
    types "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/eth"
    "github.com/ethereum/go-ethereum/ethutil"
    "github.com/ethereum/go-ethereum/logger"
    "github.com/ethereum/go-ethereum/p2p"
    "github.com/ethereum/go-ethereum/p2p/nat"
    "github.com/ethereum/go-ethereum/rlp"
)

const (
    ClientIdentifier = "Ethereum(G)"
    Version          = "0.8.6"
)

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
}

var (
    Identifier       string
    KeyRing          string
    DiffTool         bool
    DiffType         string
    KeyStore         string
    StartRpc         bool
    StartWebSockets  bool
    RpcListenAddress string
    RpcPort          int
    WsPort           int
    OutboundPort     string
    ShowGenesis      bool
    AddPeer          string
    MaxPeer          int
    GenAddr          bool
    BootNodes        string
    NodeKey          *ecdsa.PrivateKey
    NAT              nat.Interface
    SecretFile       string
    ExportDir        string
    NonInteractive   bool
    Datadir          string
    LogFile          string
    ConfigFile       string
    DebugFile        string
    LogLevel         int
    LogFormat        string
    Dump             bool
    DumpHash         string
    DumpNumber       int
    VmType           int
    ImportChain      string
    SHH              bool
    Dial             bool
    PrintVersion     bool
    MinerThreads     int
)

// flags specific to cli client
var (
    StartMining    bool
    StartJsConsole bool
    InputFile      string
)

func main() {
    init_vars()

    Init()

    if len(TestFile) < 1 {
        log.Fatal("Please specify test file")
    }
    blocks, err := loadBlocksFromTestFile(TestFile)
    if err != nil {
        panic(err)
    }

    runtime.GOMAXPROCS(runtime.NumCPU())

    defer func() {
        logger.Flush()
    }()

    utils.HandleInterrupt()

    utils.InitConfig(VmType, ConfigFile, Datadir, "ethblocktest")

    ethereum, err := eth.New(&eth.Config{
        Name:         p2p.MakeName(ClientIdentifier, Version),
        KeyStore:     KeyStore,
        DataDir:      Datadir,
        LogFile:      LogFile,
        LogLevel:     LogLevel,
        LogFormat:    LogFormat,
        MaxPeers:     MaxPeer,
        Port:         OutboundPort,
        NAT:          NAT,
        KeyRing:      KeyRing,
        Shh:          true,
        Dial:         Dial,
        BootNodes:    BootNodes,
        NodeKey:      NodeKey,
        MinerThreads: MinerThreads,
    })

    utils.StartRpc(ethereum, RpcListenAddress, RpcPort)
    utils.StartEthereum(ethereum)

    ethereum.ChainManager().ResetWithGenesisBlock(blocks[0])

    // fmt.Println("HURR: ", hex.EncodeToString(ethutil.Encode(blocks[0].RlpData())))

    go ethereum.ChainManager().InsertChain(types.Blocks{blocks[1]})
    fmt.Println("OK! ")
    ethereum.WaitForShutdown()
}

func loadBlocksFromTestFile(filePath string) (blocks types.Blocks, err error) {
    fileContent, err := ioutil.ReadFile(filePath)
    if err != nil {
        return
    }
    bt := *new(map[string]Test)
    err = json.Unmarshal(fileContent, &bt)
    if 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.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 init_vars() {
    VmType = 0
    Identifier = ""
    KeyRing = ""
    KeyStore = "db"
    RpcListenAddress = "127.0.0.1"
    RpcPort = 8545
    WsPort = 40404
    StartRpc = true
    StartWebSockets = false
    NonInteractive = false
    GenAddr = false
    SecretFile = ""
    ExportDir = ""
    LogFile = ""

    timeStr := strconv.FormatInt(time.Now().UnixNano(), 10)

    Datadir = path.Join(ethutil.DefaultDataDir(), timeStr)
    ConfigFile = path.Join(ethutil.DefaultDataDir(), timeStr, "conf.ini")

    DebugFile = ""
    LogLevel = 5
    LogFormat = "std"
    DiffTool = false
    DiffType = "all"
    ShowGenesis = false
    ImportChain = ""
    Dump = false
    DumpHash = ""
    DumpNumber = -1
    StartMining = false
    StartJsConsole = false
    PrintVersion = false
    MinerThreads = runtime.NumCPU()

    Dial = false
    OutboundPort = "30303"
    BootNodes = ""
    MaxPeer = 1

}

func hex_decode(s string) (res []byte, err error) {
    return hex.DecodeString(strings.TrimPrefix(s, "0x"))
}