aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/puppeth/genesis.go
blob: 5f39a889d1b583543488bbe2ed88655a97a074ee (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright 2017 The go-ethereum Authors
// 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 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 General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

package main

import (
    "encoding/binary"
    "errors"
    "math"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/common/hexutil"
    "github.com/ethereum/go-ethereum/consensus/ethash"
    "github.com/ethereum/go-ethereum/core"
    "github.com/ethereum/go-ethereum/params"
)

// cppEthereumGenesisSpec represents the genesis specification format used by the
// C++ Ethereum implementation.
type cppEthereumGenesisSpec struct {
    SealEngine string `json:"sealEngine"`
    Params     struct {
        AccountStartNonce       hexutil.Uint64 `json:"accountStartNonce"`
        HomesteadForkBlock      hexutil.Uint64 `json:"homesteadForkBlock"`
        EIP150ForkBlock         hexutil.Uint64 `json:"EIP150ForkBlock"`
        EIP158ForkBlock         hexutil.Uint64 `json:"EIP158ForkBlock"`
        ByzantiumForkBlock      hexutil.Uint64 `json:"byzantiumForkBlock"`
        ConstantinopleForkBlock hexutil.Uint64 `json:"constantinopleForkBlock"`
        NetworkID               hexutil.Uint64 `json:"networkID"`
        ChainID                 hexutil.Uint64 `json:"chainID"`
        MaximumExtraDataSize    hexutil.Uint64 `json:"maximumExtraDataSize"`
        MinGasLimit             hexutil.Uint64 `json:"minGasLimit"`
        MaxGasLimit             hexutil.Uint64 `json:"maxGasLimit"`
        GasLimitBoundDivisor    hexutil.Uint64 `json:"gasLimitBoundDivisor"`
        MinimumDifficulty       *hexutil.Big   `json:"minimumDifficulty"`
        DifficultyBoundDivisor  *hexutil.Big   `json:"difficultyBoundDivisor"`
        DurationLimit           *hexutil.Big   `json:"durationLimit"`
        BlockReward             *hexutil.Big   `json:"blockReward"`
    } `json:"params"`

    Genesis struct {
        Nonce      hexutil.Bytes  `json:"nonce"`
        Difficulty *hexutil.Big   `json:"difficulty"`
        MixHash    common.Hash    `json:"mixHash"`
        Author     common.Address `json:"author"`
        Timestamp  hexutil.Uint64 `json:"timestamp"`
        ParentHash common.Hash    `json:"parentHash"`
        ExtraData  hexutil.Bytes  `json:"extraData"`
        GasLimit   hexutil.Uint64 `json:"gasLimit"`
    } `json:"genesis"`

    Accounts map[common.Address]*cppEthereumGenesisSpecAccount `json:"accounts"`
}

// cppEthereumGenesisSpecAccount is the prefunded genesis account and/or precompiled
// contract definition.
type cppEthereumGenesisSpecAccount struct {
    Balance     *hexutil.Big                   `json:"balance"`
    Nonce       uint64                         `json:"nonce,omitempty"`
    Precompiled *cppEthereumGenesisSpecBuiltin `json:"precompiled,omitempty"`
}

// cppEthereumGenesisSpecBuiltin is the precompiled contract definition.
type cppEthereumGenesisSpecBuiltin struct {
    Name          string                               `json:"name,omitempty"`
    StartingBlock hexutil.Uint64                       `json:"startingBlock,omitempty"`
    Linear        *cppEthereumGenesisSpecLinearPricing `json:"linear,omitempty"`
}

type cppEthereumGenesisSpecLinearPricing struct {
    Base uint64 `json:"base"`
    Word uint64 `json:"word"`
}

// newCppEthereumGenesisSpec converts a go-ethereum genesis block into a Parity specific
// chain specification format.
func newCppEthereumGenesisSpec(network string, genesis *core.Genesis) (*cppEthereumGenesisSpec, error) {
    // Only ethash is currently supported between go-ethereum and cpp-ethereum
    if genesis.Config.Ethash == nil {
        return nil, errors.New("unsupported consensus engine")
    }
    // Reconstruct the chain spec in Parity's format
    spec := &cppEthereumGenesisSpec{
        SealEngine: "Ethash",
    }
    spec.Params.AccountStartNonce = 0
    spec.Params.HomesteadForkBlock = (hexutil.Uint64)(genesis.Config.HomesteadBlock.Uint64())
    spec.Params.EIP150ForkBlock = (hexutil.Uint64)(genesis.Config.EIP150Block.Uint64())
    spec.Params.EIP158ForkBlock = (hexutil.Uint64)(genesis.Config.EIP158Block.Uint64())
    spec.Params.ByzantiumForkBlock = (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64())
    spec.Params.ConstantinopleForkBlock = (hexutil.Uint64)(math.MaxUint64)

    spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
    spec.Params.ChainID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())

    spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize)
    spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit)
    spec.Params.MaxGasLimit = (hexutil.Uint64)(math.MaxUint64)
    spec.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty)
    spec.Params.DifficultyBoundDivisor = (*hexutil.Big)(params.DifficultyBoundDivisor)
    spec.Params.GasLimitBoundDivisor = (hexutil.Uint64)(params.GasLimitBoundDivisor)
    spec.Params.DurationLimit = (*hexutil.Big)(params.DurationLimit)
    spec.Params.BlockReward = (*hexutil.Big)(ethash.FrontierBlockReward)

    spec.Genesis.Nonce = (hexutil.Bytes)(make([]byte, 8))
    binary.LittleEndian.PutUint64(spec.Genesis.Nonce[:], genesis.Nonce)

    spec.Genesis.MixHash = genesis.Mixhash
    spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty)
    spec.Genesis.Author = genesis.Coinbase
    spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp)
    spec.Genesis.ParentHash = genesis.ParentHash
    spec.Genesis.ExtraData = (hexutil.Bytes)(genesis.ExtraData)
    spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit)

    spec.Accounts = make(map[common.Address]*cppEthereumGenesisSpecAccount)
    for address, account := range genesis.Alloc {
        spec.Accounts[address] = &cppEthereumGenesisSpecAccount{
            Balance: (*hexutil.Big)(account.Balance),
            Nonce:   account.Nonce,
        }
    }
    spec.Accounts[common.BytesToAddress([]byte{1})].Precompiled = &cppEthereumGenesisSpecBuiltin{
        Name: "ecrecover", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 3000},
    }
    spec.Accounts[common.BytesToAddress([]byte{2})].Precompiled = &cppEthereumGenesisSpecBuiltin{
        Name: "sha256", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 60, Word: 12},
    }
    spec.Accounts[common.BytesToAddress([]byte{3})].Precompiled = &cppEthereumGenesisSpecBuiltin{
        Name: "ripemd160", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 600, Word: 120},
    }
    spec.Accounts[common.BytesToAddress([]byte{4})].Precompiled = &cppEthereumGenesisSpecBuiltin{
        Name: "identity", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 15, Word: 3},
    }
    if genesis.Config.ByzantiumBlock != nil {
        spec.Accounts[common.BytesToAddress([]byte{5})].Precompiled = &cppEthereumGenesisSpecBuiltin{
            Name: "modexp", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()),
        }
        spec.Accounts[common.BytesToAddress([]byte{6})].Precompiled = &cppEthereumGenesisSpecBuiltin{
            Name: "alt_bn128_G1_add", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()), Linear: &cppEthereumGenesisSpecLinearPricing{Base: 500},
        }
        spec.Accounts[common.BytesToAddress([]byte{7})].Precompiled = &cppEthereumGenesisSpecBuiltin{
            Name: "alt_bn128_G1_mul", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()), Linear: &cppEthereumGenesisSpecLinearPricing{Base: 40000},
        }
        spec.Accounts[common.BytesToAddress([]byte{8})].Precompiled = &cppEthereumGenesisSpecBuiltin{
            Name: "alt_bn128_pairing_product", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()),
        }
    }
    return spec, nil
}

// parityChainSpec is the chain specification format used by Parity.
type parityChainSpec struct {
    Name   string `json:"name"`
    Engine struct {
        Ethash struct {
            Params struct {
                MinimumDifficulty      *hexutil.Big `json:"minimumDifficulty"`
                DifficultyBoundDivisor *hexutil.Big `json:"difficultyBoundDivisor"`
                DurationLimit          *hexutil.Big `json:"durationLimit"`
                BlockReward            *hexutil.Big `json:"blockReward"`
                HomesteadTransition    uint64       `json:"homesteadTransition"`
                EIP150Transition       uint64       `json:"eip150Transition"`
                EIP160Transition       uint64       `json:"eip160Transition"`
                EIP161abcTransition    uint64       `json:"eip161abcTransition"`
                EIP161dTransition      uint64       `json:"eip161dTransition"`
                EIP649Reward           *hexutil.Big `json:"eip649Reward"`
                EIP100bTransition      uint64       `json:"eip100bTransition"`
                EIP649Transition       uint64       `json:"eip649Transition"`
            } `json:"params"`
        } `json:"Ethash"`
    } `json:"engine"`

    Params struct {
        MaximumExtraDataSize hexutil.Uint64 `json:"maximumExtraDataSize"`
        MinGasLimit          hexutil.Uint64 `json:"minGasLimit"`
        GasLimitBoundDivisor hexutil.Uint64 `json:"gasLimitBoundDivisor"`
        NetworkID            hexutil.Uint64 `json:"networkID"`
        MaxCodeSize          uint64         `json:"maxCodeSize"`
        EIP155Transition     uint64         `json:"eip155Transition"`
        EIP98Transition      uint64         `json:"eip98Transition"`
        EIP86Transition      uint64         `json:"eip86Transition"`
        EIP140Transition     uint64         `json:"eip140Transition"`
        EIP211Transition     uint64         `json:"eip211Transition"`
        EIP214Transition     uint64         `json:"eip214Transition"`
        EIP658Transition     uint64         `json:"eip658Transition"`
    } `json:"params"`

    Genesis struct {
        Seal struct {
            Ethereum struct {
                Nonce   hexutil.Bytes `json:"nonce"`
                MixHash hexutil.Bytes `json:"mixHash"`
            } `json:"ethereum"`
        } `json:"seal"`

        Difficulty *hexutil.Big   `json:"difficulty"`
        Author     common.Address `json:"author"`
        Timestamp  hexutil.Uint64 `json:"timestamp"`
        ParentHash common.Hash    `json:"parentHash"`
        ExtraData  hexutil.Bytes  `json:"extraData"`
        GasLimit   hexutil.Uint64 `json:"gasLimit"`
    } `json:"genesis"`

    Nodes    []string                                   `json:"nodes"`
    Accounts map[common.Address]*parityChainSpecAccount `json:"accounts"`
}

// parityChainSpecAccount is the prefunded genesis account and/or precompiled
// contract definition.
type parityChainSpecAccount struct {
    Balance *hexutil.Big            `json:"balance"`
    Nonce   uint64                  `json:"nonce,omitempty"`
    Builtin *parityChainSpecBuiltin `json:"builtin,omitempty"`
}

// parityChainSpecBuiltin is the precompiled contract definition.
type parityChainSpecBuiltin struct {
    Name       string                  `json:"name,omitempty"`
    ActivateAt uint64                  `json:"activate_at,omitempty"`
    Pricing    *parityChainSpecPricing `json:"pricing,omitempty"`
}

// parityChainSpecPricing represents the different pricing models that builtin
// contracts might advertise using.
type parityChainSpecPricing struct {
    Linear       *parityChainSpecLinearPricing       `json:"linear,omitempty"`
    ModExp       *parityChainSpecModExpPricing       `json:"modexp,omitempty"`
    AltBnPairing *parityChainSpecAltBnPairingPricing `json:"alt_bn128_pairing,omitempty"`
}

type parityChainSpecLinearPricing struct {
    Base uint64 `json:"base"`
    Word uint64 `json:"word"`
}

type parityChainSpecModExpPricing struct {
    Divisor uint64 `json:"divisor"`
}

type parityChainSpecAltBnPairingPricing struct {
    Base uint64 `json:"base"`
    Pair uint64 `json:"pair"`
}

// newParityChainSpec converts a go-ethereum genesis block into a Parity specific
// chain specification format.
func newParityChainSpec(network string, genesis *core.Genesis, bootnodes []string) (*parityChainSpec, error) {
    // Only ethash is currently supported between go-ethereum and Parity
    if genesis.Config.Ethash == nil {
        return nil, errors.New("unsupported consensus engine")
    }
    // Reconstruct the chain spec in Parity's format
    spec := &parityChainSpec{
        Name:  network,
        Nodes: bootnodes,
    }
    spec.Engine.Ethash.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty)
    spec.Engine.Ethash.Params.DifficultyBoundDivisor = (*hexutil.Big)(params.DifficultyBoundDivisor)
    spec.Engine.Ethash.Params.DurationLimit = (*hexutil.Big)(params.DurationLimit)
    spec.Engine.Ethash.Params.BlockReward = (*hexutil.Big)(ethash.FrontierBlockReward)
    spec.Engine.Ethash.Params.HomesteadTransition = genesis.Config.HomesteadBlock.Uint64()
    spec.Engine.Ethash.Params.EIP150Transition = genesis.Config.EIP150Block.Uint64()
    spec.Engine.Ethash.Params.EIP160Transition = genesis.Config.EIP155Block.Uint64()
    spec.Engine.Ethash.Params.EIP161abcTransition = genesis.Config.EIP158Block.Uint64()
    spec.Engine.Ethash.Params.EIP161dTransition = genesis.Config.EIP158Block.Uint64()
    spec.Engine.Ethash.Params.EIP649Reward = (*hexutil.Big)(ethash.ByzantiumBlockReward)
    spec.Engine.Ethash.Params.EIP100bTransition = genesis.Config.ByzantiumBlock.Uint64()
    spec.Engine.Ethash.Params.EIP649Transition = genesis.Config.ByzantiumBlock.Uint64()

    spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize)
    spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit)
    spec.Params.GasLimitBoundDivisor = (hexutil.Uint64)(params.GasLimitBoundDivisor)
    spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64())
    spec.Params.MaxCodeSize = params.MaxCodeSize
    spec.Params.EIP155Transition = genesis.Config.EIP155Block.Uint64()
    spec.Params.EIP98Transition = math.MaxUint64
    spec.Params.EIP86Transition = math.MaxUint64
    spec.Params.EIP140Transition = genesis.Config.ByzantiumBlock.Uint64()
    spec.Params.EIP211Transition = genesis.Config.ByzantiumBlock.Uint64()
    spec.Params.EIP214Transition = genesis.Config.ByzantiumBlock.Uint64()
    spec.Params.EIP658Transition = genesis.Config.ByzantiumBlock.Uint64()

    spec.Genesis.Seal.Ethereum.Nonce = (hexutil.Bytes)(make([]byte, 8))
    binary.LittleEndian.PutUint64(spec.Genesis.Seal.Ethereum.Nonce[:], genesis.Nonce)

    spec.Genesis.Seal.Ethereum.MixHash = (hexutil.Bytes)(genesis.Mixhash[:])
    spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty)
    spec.Genesis.Author = genesis.Coinbase
    spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp)
    spec.Genesis.ParentHash = genesis.ParentHash
    spec.Genesis.ExtraData = (hexutil.Bytes)(genesis.ExtraData)
    spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit)

    spec.Accounts = make(map[common.Address]*parityChainSpecAccount)
    for address, account := range genesis.Alloc {
        spec.Accounts[address] = &parityChainSpecAccount{
            Balance: (*hexutil.Big)(account.Balance),
            Nonce:   account.Nonce,
        }
    }
    spec.Accounts[common.BytesToAddress([]byte{1})].Builtin = &parityChainSpecBuiltin{
        Name: "ecrecover", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 3000}},
    }
    spec.Accounts[common.BytesToAddress([]byte{2})].Builtin = &parityChainSpecBuiltin{
        Name: "sha256", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 60, Word: 12}},
    }
    spec.Accounts[common.BytesToAddress([]byte{3})].Builtin = &parityChainSpecBuiltin{
        Name: "ripemd160", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 600, Word: 120}},
    }
    spec.Accounts[common.BytesToAddress([]byte{4})].Builtin = &parityChainSpecBuiltin{
        Name: "identity", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 15, Word: 3}},
    }
    if genesis.Config.ByzantiumBlock != nil {
        spec.Accounts[common.BytesToAddress([]byte{5})].Builtin = &parityChainSpecBuiltin{
            Name: "modexp", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{ModExp: &parityChainSpecModExpPricing{Divisor: 20}},
        }
        spec.Accounts[common.BytesToAddress([]byte{6})].Builtin = &parityChainSpecBuiltin{
            Name: "alt_bn128_add", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 500}},
        }
        spec.Accounts[common.BytesToAddress([]byte{7})].Builtin = &parityChainSpecBuiltin{
            Name: "alt_bn128_mul", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 40000}},
        }
        spec.Accounts[common.BytesToAddress([]byte{8})].Builtin = &parityChainSpecBuiltin{
            Name: "alt_bn128_pairing", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{AltBnPairing: &parityChainSpecAltBnPairingPricing{Base: 100000, Pair: 80000}},
        }
    }
    return spec, nil
}

// pyEthereumGenesisSpec represents the genesis specification format used by the
// Python Ethereum implementation.
type pyEthereumGenesisSpec struct {
    Nonce      hexutil.Bytes     `json:"nonce"`
    Timestamp  hexutil.Uint64    `json:"timestamp"`
    ExtraData  hexutil.Bytes     `json:"extraData"`
    GasLimit   hexutil.Uint64    `json:"gasLimit"`
    Difficulty *hexutil.Big      `json:"difficulty"`
    Mixhash    common.Hash       `json:"mixhash"`
    Coinbase   common.Address    `json:"coinbase"`
    Alloc      core.GenesisAlloc `json:"alloc"`
    ParentHash common.Hash       `json:"parentHash"`
}

// newPyEthereumGenesisSpec converts a go-ethereum genesis block into a Parity specific
// chain specification format.
func newPyEthereumGenesisSpec(network string, genesis *core.Genesis) (*pyEthereumGenesisSpec, error) {
    // Only ethash is currently supported between go-ethereum and pyethereum
    if genesis.Config.Ethash == nil {
        return nil, errors.New("unsupported consensus engine")
    }
    spec := &pyEthereumGenesisSpec{
        Timestamp:  (hexutil.Uint64)(genesis.Timestamp),
        ExtraData:  genesis.ExtraData,
        GasLimit:   (hexutil.Uint64)(genesis.GasLimit),
        Difficulty: (*hexutil.Big)(genesis.Difficulty),
        Mixhash:    genesis.Mixhash,
        Coinbase:   genesis.Coinbase,
        Alloc:      genesis.Alloc,
        ParentHash: genesis.ParentHash,
    }
    spec.Nonce = (hexutil.Bytes)(make([]byte, 8))
    binary.LittleEndian.PutUint64(spec.Nonce[:], genesis.Nonce)

    return spec, nil
}