github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/puppeth/genesis.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2017 Go Ethereum作者 10 //此文件是Go以太坊的一部分。 11 // 12 //Go以太坊是免费软件:您可以重新发布和/或修改它 13 //根据GNU通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊的分布希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU通用公共许可证了解更多详细信息。 21 // 22 //你应该已经收到一份GNU通用公共许可证的副本 23 //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package main 26 27 import ( 28 "encoding/binary" 29 "errors" 30 "math" 31 32 "github.com/ethereum/go-ethereum/common" 33 "github.com/ethereum/go-ethereum/common/hexutil" 34 "github.com/ethereum/go-ethereum/consensus/ethash" 35 "github.com/ethereum/go-ethereum/core" 36 "github.com/ethereum/go-ethereum/params" 37 ) 38 39 //cppetereumgeneisspec表示 40 //C++ EthUUM实现。 41 type cppEthereumGenesisSpec struct { 42 SealEngine string `json:"sealEngine"` 43 Params struct { 44 AccountStartNonce hexutil.Uint64 `json:"accountStartNonce"` 45 HomesteadForkBlock hexutil.Uint64 `json:"homesteadForkBlock"` 46 EIP150ForkBlock hexutil.Uint64 `json:"EIP150ForkBlock"` 47 EIP158ForkBlock hexutil.Uint64 `json:"EIP158ForkBlock"` 48 ByzantiumForkBlock hexutil.Uint64 `json:"byzantiumForkBlock"` 49 ConstantinopleForkBlock hexutil.Uint64 `json:"constantinopleForkBlock"` 50 NetworkID hexutil.Uint64 `json:"networkID"` 51 ChainID hexutil.Uint64 `json:"chainID"` 52 MaximumExtraDataSize hexutil.Uint64 `json:"maximumExtraDataSize"` 53 MinGasLimit hexutil.Uint64 `json:"minGasLimit"` 54 MaxGasLimit hexutil.Uint64 `json:"maxGasLimit"` 55 GasLimitBoundDivisor hexutil.Uint64 `json:"gasLimitBoundDivisor"` 56 MinimumDifficulty *hexutil.Big `json:"minimumDifficulty"` 57 DifficultyBoundDivisor *hexutil.Big `json:"difficultyBoundDivisor"` 58 DurationLimit *hexutil.Big `json:"durationLimit"` 59 BlockReward *hexutil.Big `json:"blockReward"` 60 } `json:"params"` 61 62 Genesis struct { 63 Nonce hexutil.Bytes `json:"nonce"` 64 Difficulty *hexutil.Big `json:"difficulty"` 65 MixHash common.Hash `json:"mixHash"` 66 Author common.Address `json:"author"` 67 Timestamp hexutil.Uint64 `json:"timestamp"` 68 ParentHash common.Hash `json:"parentHash"` 69 ExtraData hexutil.Bytes `json:"extraData"` 70 GasLimit hexutil.Uint64 `json:"gasLimit"` 71 } `json:"genesis"` 72 73 Accounts map[common.Address]*cppEthereumGenesisSpecAccount `json:"accounts"` 74 } 75 76 //cppetereumgeneisspeccount是预先准备好的Genesis帐户和/或预编译的 77 //合同定义。 78 type cppEthereumGenesisSpecAccount struct { 79 Balance *hexutil.Big `json:"balance"` 80 Nonce uint64 `json:"nonce,omitempty"` 81 Precompiled *cppEthereumGenesisSpecBuiltin `json:"precompiled,omitempty"` 82 } 83 84 //cppetereumgeneisspecbuiltin是预编译的合同定义。 85 type cppEthereumGenesisSpecBuiltin struct { 86 Name string `json:"name,omitempty"` 87 StartingBlock hexutil.Uint64 `json:"startingBlock,omitempty"` 88 Linear *cppEthereumGenesisSpecLinearPricing `json:"linear,omitempty"` 89 } 90 91 type cppEthereumGenesisSpecLinearPricing struct { 92 Base uint64 `json:"base"` 93 Word uint64 `json:"word"` 94 } 95 96 //newcppetereumgenesspec将go-ethereum genesis块转换为一个特定于奇偶校验的块。 97 //链规范格式。 98 func newCppEthereumGenesisSpec(network string, genesis *core.Genesis) (*cppEthereumGenesisSpec, error) { 99 //Go以太坊和CPP以太坊目前仅支持ethash 100 if genesis.Config.Ethash == nil { 101 return nil, errors.New("unsupported consensus engine") 102 } 103 //以奇偶校验格式重新构造链规范 104 spec := &cppEthereumGenesisSpec{ 105 SealEngine: "Ethash", 106 } 107 spec.Params.AccountStartNonce = 0 108 spec.Params.HomesteadForkBlock = (hexutil.Uint64)(genesis.Config.HomesteadBlock.Uint64()) 109 spec.Params.EIP150ForkBlock = (hexutil.Uint64)(genesis.Config.EIP150Block.Uint64()) 110 spec.Params.EIP158ForkBlock = (hexutil.Uint64)(genesis.Config.EIP158Block.Uint64()) 111 spec.Params.ByzantiumForkBlock = (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()) 112 spec.Params.ConstantinopleForkBlock = (hexutil.Uint64)(math.MaxUint64) 113 114 spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64()) 115 spec.Params.ChainID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64()) 116 117 spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize) 118 spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit) 119 spec.Params.MaxGasLimit = (hexutil.Uint64)(math.MaxUint64) 120 spec.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty) 121 spec.Params.DifficultyBoundDivisor = (*hexutil.Big)(params.DifficultyBoundDivisor) 122 spec.Params.GasLimitBoundDivisor = (hexutil.Uint64)(params.GasLimitBoundDivisor) 123 spec.Params.DurationLimit = (*hexutil.Big)(params.DurationLimit) 124 spec.Params.BlockReward = (*hexutil.Big)(ethash.FrontierBlockReward) 125 126 spec.Genesis.Nonce = (hexutil.Bytes)(make([]byte, 8)) 127 binary.LittleEndian.PutUint64(spec.Genesis.Nonce[:], genesis.Nonce) 128 129 spec.Genesis.MixHash = genesis.Mixhash 130 spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty) 131 spec.Genesis.Author = genesis.Coinbase 132 spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp) 133 spec.Genesis.ParentHash = genesis.ParentHash 134 spec.Genesis.ExtraData = (hexutil.Bytes)(genesis.ExtraData) 135 spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit) 136 137 spec.Accounts = make(map[common.Address]*cppEthereumGenesisSpecAccount) 138 for address, account := range genesis.Alloc { 139 spec.Accounts[address] = &cppEthereumGenesisSpecAccount{ 140 Balance: (*hexutil.Big)(account.Balance), 141 Nonce: account.Nonce, 142 } 143 } 144 spec.Accounts[common.BytesToAddress([]byte{1})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 145 Name: "ecrecover", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 3000}, 146 } 147 spec.Accounts[common.BytesToAddress([]byte{2})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 148 Name: "sha256", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 60, Word: 12}, 149 } 150 spec.Accounts[common.BytesToAddress([]byte{3})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 151 Name: "ripemd160", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 600, Word: 120}, 152 } 153 spec.Accounts[common.BytesToAddress([]byte{4})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 154 Name: "identity", Linear: &cppEthereumGenesisSpecLinearPricing{Base: 15, Word: 3}, 155 } 156 if genesis.Config.ByzantiumBlock != nil { 157 spec.Accounts[common.BytesToAddress([]byte{5})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 158 Name: "modexp", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()), 159 } 160 spec.Accounts[common.BytesToAddress([]byte{6})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 161 Name: "alt_bn128_G1_add", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()), Linear: &cppEthereumGenesisSpecLinearPricing{Base: 500}, 162 } 163 spec.Accounts[common.BytesToAddress([]byte{7})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 164 Name: "alt_bn128_G1_mul", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()), Linear: &cppEthereumGenesisSpecLinearPricing{Base: 40000}, 165 } 166 spec.Accounts[common.BytesToAddress([]byte{8})].Precompiled = &cppEthereumGenesisSpecBuiltin{ 167 Name: "alt_bn128_pairing_product", StartingBlock: (hexutil.Uint64)(genesis.Config.ByzantiumBlock.Uint64()), 168 } 169 } 170 return spec, nil 171 } 172 173 //paritychainspec是奇偶校验使用的链规范格式。 174 type parityChainSpec struct { 175 Name string `json:"name"` 176 Engine struct { 177 Ethash struct { 178 Params struct { 179 MinimumDifficulty *hexutil.Big `json:"minimumDifficulty"` 180 DifficultyBoundDivisor *hexutil.Big `json:"difficultyBoundDivisor"` 181 DurationLimit *hexutil.Big `json:"durationLimit"` 182 BlockReward *hexutil.Big `json:"blockReward"` 183 HomesteadTransition uint64 `json:"homesteadTransition"` 184 EIP150Transition uint64 `json:"eip150Transition"` 185 EIP160Transition uint64 `json:"eip160Transition"` 186 EIP161abcTransition uint64 `json:"eip161abcTransition"` 187 EIP161dTransition uint64 `json:"eip161dTransition"` 188 EIP649Reward *hexutil.Big `json:"eip649Reward"` 189 EIP100bTransition uint64 `json:"eip100bTransition"` 190 EIP649Transition uint64 `json:"eip649Transition"` 191 } `json:"params"` 192 } `json:"Ethash"` 193 } `json:"engine"` 194 195 Params struct { 196 MaximumExtraDataSize hexutil.Uint64 `json:"maximumExtraDataSize"` 197 MinGasLimit hexutil.Uint64 `json:"minGasLimit"` 198 GasLimitBoundDivisor hexutil.Uint64 `json:"gasLimitBoundDivisor"` 199 NetworkID hexutil.Uint64 `json:"networkID"` 200 MaxCodeSize uint64 `json:"maxCodeSize"` 201 EIP155Transition uint64 `json:"eip155Transition"` 202 EIP98Transition uint64 `json:"eip98Transition"` 203 EIP86Transition uint64 `json:"eip86Transition"` 204 EIP140Transition uint64 `json:"eip140Transition"` 205 EIP211Transition uint64 `json:"eip211Transition"` 206 EIP214Transition uint64 `json:"eip214Transition"` 207 EIP658Transition uint64 `json:"eip658Transition"` 208 } `json:"params"` 209 210 Genesis struct { 211 Seal struct { 212 Ethereum struct { 213 Nonce hexutil.Bytes `json:"nonce"` 214 MixHash hexutil.Bytes `json:"mixHash"` 215 } `json:"ethereum"` 216 } `json:"seal"` 217 218 Difficulty *hexutil.Big `json:"difficulty"` 219 Author common.Address `json:"author"` 220 Timestamp hexutil.Uint64 `json:"timestamp"` 221 ParentHash common.Hash `json:"parentHash"` 222 ExtraData hexutil.Bytes `json:"extraData"` 223 GasLimit hexutil.Uint64 `json:"gasLimit"` 224 } `json:"genesis"` 225 226 Nodes []string `json:"nodes"` 227 Accounts map[common.Address]*parityChainSpecAccount `json:"accounts"` 228 } 229 230 //ParityChainspeccount是预先准备好的Genesis帐户和/或预编译的 231 //合同定义。 232 type parityChainSpecAccount struct { 233 Balance *hexutil.Big `json:"balance"` 234 Nonce uint64 `json:"nonce,omitempty"` 235 Builtin *parityChainSpecBuiltin `json:"builtin,omitempty"` 236 } 237 238 //paritychainspeccuiltin是预编译的合同定义。 239 type parityChainSpecBuiltin struct { 240 Name string `json:"name,omitempty"` 241 ActivateAt uint64 `json:"activate_at,omitempty"` 242 Pricing *parityChainSpecPricing `json:"pricing,omitempty"` 243 } 244 245 //ParityChainSecPricing表示内置的不同定价模型 246 //合同可能会宣传使用。 247 type parityChainSpecPricing struct { 248 Linear *parityChainSpecLinearPricing `json:"linear,omitempty"` 249 ModExp *parityChainSpecModExpPricing `json:"modexp,omitempty"` 250 AltBnPairing *parityChainSpecAltBnPairingPricing `json:"alt_bn128_pairing,omitempty"` 251 } 252 253 type parityChainSpecLinearPricing struct { 254 Base uint64 `json:"base"` 255 Word uint64 `json:"word"` 256 } 257 258 type parityChainSpecModExpPricing struct { 259 Divisor uint64 `json:"divisor"` 260 } 261 262 type parityChainSpecAltBnPairingPricing struct { 263 Base uint64 `json:"base"` 264 Pair uint64 `json:"pair"` 265 } 266 267 //NewParityChainspec将Go-Ethereum Genesis块转换为特定于奇偶校验的块。 268 //链规范格式。 269 func newParityChainSpec(network string, genesis *core.Genesis, bootnodes []string) (*parityChainSpec, error) { 270 //在go-ethereum和parity之间,目前只支持ethash。 271 if genesis.Config.Ethash == nil { 272 return nil, errors.New("unsupported consensus engine") 273 } 274 //以奇偶校验格式重新构造链规范 275 spec := &parityChainSpec{ 276 Name: network, 277 Nodes: bootnodes, 278 } 279 spec.Engine.Ethash.Params.MinimumDifficulty = (*hexutil.Big)(params.MinimumDifficulty) 280 spec.Engine.Ethash.Params.DifficultyBoundDivisor = (*hexutil.Big)(params.DifficultyBoundDivisor) 281 spec.Engine.Ethash.Params.DurationLimit = (*hexutil.Big)(params.DurationLimit) 282 spec.Engine.Ethash.Params.BlockReward = (*hexutil.Big)(ethash.FrontierBlockReward) 283 spec.Engine.Ethash.Params.HomesteadTransition = genesis.Config.HomesteadBlock.Uint64() 284 spec.Engine.Ethash.Params.EIP150Transition = genesis.Config.EIP150Block.Uint64() 285 spec.Engine.Ethash.Params.EIP160Transition = genesis.Config.EIP155Block.Uint64() 286 spec.Engine.Ethash.Params.EIP161abcTransition = genesis.Config.EIP158Block.Uint64() 287 spec.Engine.Ethash.Params.EIP161dTransition = genesis.Config.EIP158Block.Uint64() 288 spec.Engine.Ethash.Params.EIP649Reward = (*hexutil.Big)(ethash.ByzantiumBlockReward) 289 spec.Engine.Ethash.Params.EIP100bTransition = genesis.Config.ByzantiumBlock.Uint64() 290 spec.Engine.Ethash.Params.EIP649Transition = genesis.Config.ByzantiumBlock.Uint64() 291 292 spec.Params.MaximumExtraDataSize = (hexutil.Uint64)(params.MaximumExtraDataSize) 293 spec.Params.MinGasLimit = (hexutil.Uint64)(params.MinGasLimit) 294 spec.Params.GasLimitBoundDivisor = (hexutil.Uint64)(params.GasLimitBoundDivisor) 295 spec.Params.NetworkID = (hexutil.Uint64)(genesis.Config.ChainID.Uint64()) 296 spec.Params.MaxCodeSize = params.MaxCodeSize 297 spec.Params.EIP155Transition = genesis.Config.EIP155Block.Uint64() 298 spec.Params.EIP98Transition = math.MaxUint64 299 spec.Params.EIP86Transition = math.MaxUint64 300 spec.Params.EIP140Transition = genesis.Config.ByzantiumBlock.Uint64() 301 spec.Params.EIP211Transition = genesis.Config.ByzantiumBlock.Uint64() 302 spec.Params.EIP214Transition = genesis.Config.ByzantiumBlock.Uint64() 303 spec.Params.EIP658Transition = genesis.Config.ByzantiumBlock.Uint64() 304 305 spec.Genesis.Seal.Ethereum.Nonce = (hexutil.Bytes)(make([]byte, 8)) 306 binary.LittleEndian.PutUint64(spec.Genesis.Seal.Ethereum.Nonce[:], genesis.Nonce) 307 308 spec.Genesis.Seal.Ethereum.MixHash = (hexutil.Bytes)(genesis.Mixhash[:]) 309 spec.Genesis.Difficulty = (*hexutil.Big)(genesis.Difficulty) 310 spec.Genesis.Author = genesis.Coinbase 311 spec.Genesis.Timestamp = (hexutil.Uint64)(genesis.Timestamp) 312 spec.Genesis.ParentHash = genesis.ParentHash 313 spec.Genesis.ExtraData = (hexutil.Bytes)(genesis.ExtraData) 314 spec.Genesis.GasLimit = (hexutil.Uint64)(genesis.GasLimit) 315 316 spec.Accounts = make(map[common.Address]*parityChainSpecAccount) 317 for address, account := range genesis.Alloc { 318 spec.Accounts[address] = &parityChainSpecAccount{ 319 Balance: (*hexutil.Big)(account.Balance), 320 Nonce: account.Nonce, 321 } 322 } 323 spec.Accounts[common.BytesToAddress([]byte{1})].Builtin = &parityChainSpecBuiltin{ 324 Name: "ecrecover", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 3000}}, 325 } 326 spec.Accounts[common.BytesToAddress([]byte{2})].Builtin = &parityChainSpecBuiltin{ 327 Name: "sha256", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 60, Word: 12}}, 328 } 329 spec.Accounts[common.BytesToAddress([]byte{3})].Builtin = &parityChainSpecBuiltin{ 330 Name: "ripemd160", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 600, Word: 120}}, 331 } 332 spec.Accounts[common.BytesToAddress([]byte{4})].Builtin = &parityChainSpecBuiltin{ 333 Name: "identity", Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 15, Word: 3}}, 334 } 335 if genesis.Config.ByzantiumBlock != nil { 336 spec.Accounts[common.BytesToAddress([]byte{5})].Builtin = &parityChainSpecBuiltin{ 337 Name: "modexp", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{ModExp: &parityChainSpecModExpPricing{Divisor: 20}}, 338 } 339 spec.Accounts[common.BytesToAddress([]byte{6})].Builtin = &parityChainSpecBuiltin{ 340 Name: "alt_bn128_add", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 500}}, 341 } 342 spec.Accounts[common.BytesToAddress([]byte{7})].Builtin = &parityChainSpecBuiltin{ 343 Name: "alt_bn128_mul", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{Linear: &parityChainSpecLinearPricing{Base: 40000}}, 344 } 345 spec.Accounts[common.BytesToAddress([]byte{8})].Builtin = &parityChainSpecBuiltin{ 346 Name: "alt_bn128_pairing", ActivateAt: genesis.Config.ByzantiumBlock.Uint64(), Pricing: &parityChainSpecPricing{AltBnPairing: &parityChainSpecAltBnPairingPricing{Base: 100000, Pair: 80000}}, 347 } 348 } 349 return spec, nil 350 } 351 352 // 353 //python-ethereum实现。 354 type pyEthereumGenesisSpec struct { 355 Nonce hexutil.Bytes `json:"nonce"` 356 Timestamp hexutil.Uint64 `json:"timestamp"` 357 ExtraData hexutil.Bytes `json:"extraData"` 358 GasLimit hexutil.Uint64 `json:"gasLimit"` 359 Difficulty *hexutil.Big `json:"difficulty"` 360 Mixhash common.Hash `json:"mixhash"` 361 Coinbase common.Address `json:"coinbase"` 362 Alloc core.GenesisAlloc `json:"alloc"` 363 ParentHash common.Hash `json:"parentHash"` 364 } 365 366 //Newpyethereumgeneisspec将Go-Ethereum Genesis块转换为特定于奇偶校验的块。 367 //链规范格式。 368 func newPyEthereumGenesisSpec(network string, genesis *core.Genesis) (*pyEthereumGenesisSpec, error) { 369 //Go以太坊和Pyetereum目前仅支持ethash 370 if genesis.Config.Ethash == nil { 371 return nil, errors.New("unsupported consensus engine") 372 } 373 spec := &pyEthereumGenesisSpec{ 374 Timestamp: (hexutil.Uint64)(genesis.Timestamp), 375 ExtraData: genesis.ExtraData, 376 GasLimit: (hexutil.Uint64)(genesis.GasLimit), 377 Difficulty: (*hexutil.Big)(genesis.Difficulty), 378 Mixhash: genesis.Mixhash, 379 Coinbase: genesis.Coinbase, 380 Alloc: genesis.Alloc, 381 ParentHash: genesis.ParentHash, 382 } 383 spec.Nonce = (hexutil.Bytes)(make([]byte, 8)) 384 binary.LittleEndian.PutUint64(spec.Nonce[:], genesis.Nonce) 385 386 return spec, nil 387 }