retesteth.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. // Copyright 2019 The go-ethereum Authors
  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. package main
  17. import (
  18. "bytes"
  19. "context"
  20. "fmt"
  21. "math/big"
  22. "os"
  23. "os/signal"
  24. "strings"
  25. "time"
  26. "github.com/ethereum/go-ethereum/cmd/utils"
  27. "github.com/ethereum/go-ethereum/common"
  28. "github.com/ethereum/go-ethereum/common/hexutil"
  29. "github.com/ethereum/go-ethereum/common/math"
  30. "github.com/ethereum/go-ethereum/consensus"
  31. "github.com/ethereum/go-ethereum/consensus/ethash"
  32. "github.com/ethereum/go-ethereum/consensus/misc"
  33. "github.com/ethereum/go-ethereum/core"
  34. "github.com/ethereum/go-ethereum/core/rawdb"
  35. "github.com/ethereum/go-ethereum/core/state"
  36. "github.com/ethereum/go-ethereum/core/types"
  37. "github.com/ethereum/go-ethereum/core/vm"
  38. "github.com/ethereum/go-ethereum/crypto"
  39. "github.com/ethereum/go-ethereum/ethdb"
  40. "github.com/ethereum/go-ethereum/log"
  41. "github.com/ethereum/go-ethereum/node"
  42. "github.com/ethereum/go-ethereum/params"
  43. "github.com/ethereum/go-ethereum/rlp"
  44. "github.com/ethereum/go-ethereum/rpc"
  45. "github.com/ethereum/go-ethereum/trie"
  46. cli "gopkg.in/urfave/cli.v1"
  47. )
  48. var (
  49. rpcPortFlag = cli.IntFlag{
  50. Name: "rpcport",
  51. Usage: "HTTP-RPC server listening port",
  52. Value: node.DefaultHTTPPort,
  53. }
  54. retestethCommand = cli.Command{
  55. Action: utils.MigrateFlags(retesteth),
  56. Name: "retesteth",
  57. Usage: "Launches geth in retesteth mode",
  58. ArgsUsage: "",
  59. Flags: []cli.Flag{rpcPortFlag},
  60. Category: "MISCELLANEOUS COMMANDS",
  61. Description: `Launches geth in retesteth mode (no database, no network, only retesteth RPC interface)`,
  62. }
  63. )
  64. type RetestethTestAPI interface {
  65. SetChainParams(ctx context.Context, chainParams ChainParams) (bool, error)
  66. MineBlocks(ctx context.Context, number uint64) (bool, error)
  67. ModifyTimestamp(ctx context.Context, interval uint64) (bool, error)
  68. ImportRawBlock(ctx context.Context, rawBlock hexutil.Bytes) (common.Hash, error)
  69. RewindToBlock(ctx context.Context, number uint64) (bool, error)
  70. GetLogHash(ctx context.Context, txHash common.Hash) (common.Hash, error)
  71. }
  72. type RetestethEthAPI interface {
  73. SendRawTransaction(ctx context.Context, rawTx hexutil.Bytes) (common.Hash, error)
  74. BlockNumber(ctx context.Context) (uint64, error)
  75. GetBlockByNumber(ctx context.Context, blockNr math.HexOrDecimal64, fullTx bool) (map[string]interface{}, error)
  76. GetBalance(ctx context.Context, address common.Address, blockNr math.HexOrDecimal64) (*math.HexOrDecimal256, error)
  77. GetCode(ctx context.Context, address common.Address, blockNr math.HexOrDecimal64) (hexutil.Bytes, error)
  78. GetTransactionCount(ctx context.Context, address common.Address, blockNr math.HexOrDecimal64) (uint64, error)
  79. }
  80. type RetestethDebugAPI interface {
  81. AccountRangeAt(ctx context.Context,
  82. blockHashOrNumber *math.HexOrDecimal256, txIndex uint64,
  83. addressHash *math.HexOrDecimal256, maxResults uint64,
  84. ) (AccountRangeResult, error)
  85. StorageRangeAt(ctx context.Context,
  86. blockHashOrNumber *math.HexOrDecimal256, txIndex uint64,
  87. address common.Address,
  88. begin *math.HexOrDecimal256, maxResults uint64,
  89. ) (StorageRangeResult, error)
  90. }
  91. type RetestWeb3API interface {
  92. ClientVersion(ctx context.Context) (string, error)
  93. }
  94. type RetestethAPI struct {
  95. ethDb ethdb.Database
  96. db state.Database
  97. chainConfig *params.ChainConfig
  98. author common.Address
  99. extraData []byte
  100. genesisHash common.Hash
  101. engine *NoRewardEngine
  102. blockchain *core.BlockChain
  103. blockNumber uint64
  104. txMap map[common.Address]map[uint64]*types.Transaction // Sender -> Nonce -> Transaction
  105. txSenders map[common.Address]struct{} // Set of transaction senders
  106. blockInterval uint64
  107. }
  108. type ChainParams struct {
  109. SealEngine string `json:"sealEngine"`
  110. Params CParamsParams `json:"params"`
  111. Genesis CParamsGenesis `json:"genesis"`
  112. Accounts map[common.Address]CParamsAccount `json:"accounts"`
  113. }
  114. type CParamsParams struct {
  115. AccountStartNonce math.HexOrDecimal64 `json:"accountStartNonce"`
  116. HomesteadForkBlock *math.HexOrDecimal64 `json:"homesteadForkBlock"`
  117. EIP150ForkBlock *math.HexOrDecimal64 `json:"EIP150ForkBlock"`
  118. EIP158ForkBlock *math.HexOrDecimal64 `json:"EIP158ForkBlock"`
  119. DaoHardforkBlock *math.HexOrDecimal64 `json:"daoHardforkBlock"`
  120. ByzantiumForkBlock *math.HexOrDecimal64 `json:"byzantiumForkBlock"`
  121. ConstantinopleForkBlock *math.HexOrDecimal64 `json:"constantinopleForkBlock"`
  122. ConstantinopleFixForkBlock *math.HexOrDecimal64 `json:"constantinopleFixForkBlock"`
  123. ChainID *math.HexOrDecimal256 `json:"chainID"`
  124. MaximumExtraDataSize math.HexOrDecimal64 `json:"maximumExtraDataSize"`
  125. TieBreakingGas bool `json:"tieBreakingGas"`
  126. MinGasLimit math.HexOrDecimal64 `json:"minGasLimit"`
  127. MaxGasLimit math.HexOrDecimal64 `json:"maxGasLimit"`
  128. GasLimitBoundDivisor math.HexOrDecimal64 `json:"gasLimitBoundDivisor"`
  129. MinimumDifficulty math.HexOrDecimal256 `json:"minimumDifficulty"`
  130. DifficultyBoundDivisor math.HexOrDecimal256 `json:"difficultyBoundDivisor"`
  131. DurationLimit math.HexOrDecimal256 `json:"durationLimit"`
  132. BlockReward math.HexOrDecimal256 `json:"blockReward"`
  133. NetworkID math.HexOrDecimal256 `json:"networkID"`
  134. }
  135. type CParamsGenesis struct {
  136. Nonce math.HexOrDecimal64 `json:"nonce"`
  137. Difficulty *math.HexOrDecimal256 `json:"difficulty"`
  138. MixHash *math.HexOrDecimal256 `json:"mixHash"`
  139. Author common.Address `json:"author"`
  140. Timestamp math.HexOrDecimal64 `json:"timestamp"`
  141. ParentHash common.Hash `json:"parentHash"`
  142. ExtraData hexutil.Bytes `json:"extraData"`
  143. GasLimit math.HexOrDecimal64 `json:"gasLimit"`
  144. }
  145. type CParamsAccount struct {
  146. Balance *math.HexOrDecimal256 `json:"balance"`
  147. Precompiled *CPAccountPrecompiled `json:"precompiled"`
  148. Code hexutil.Bytes `json:"code"`
  149. Storage map[string]string `json:"storage"`
  150. Nonce *math.HexOrDecimal64 `json:"nonce"`
  151. }
  152. type CPAccountPrecompiled struct {
  153. Name string `json:"name"`
  154. StartingBlock math.HexOrDecimal64 `json:"startingBlock"`
  155. Linear *CPAPrecompiledLinear `json:"linear"`
  156. }
  157. type CPAPrecompiledLinear struct {
  158. Base uint64 `json:"base"`
  159. Word uint64 `json:"word"`
  160. }
  161. type AccountRangeResult struct {
  162. AddressMap map[common.Hash]common.Address `json:"addressMap"`
  163. NextKey common.Hash `json:"nextKey"`
  164. }
  165. type StorageRangeResult struct {
  166. Complete bool `json:"complete"`
  167. Storage map[common.Hash]SRItem `json:"storage"`
  168. }
  169. type SRItem struct {
  170. Key string `json:"key"`
  171. Value string `json:"value"`
  172. }
  173. type NoRewardEngine struct {
  174. inner consensus.Engine
  175. rewardsOn bool
  176. }
  177. func (e *NoRewardEngine) Author(header *types.Header) (common.Address, error) {
  178. return e.inner.Author(header)
  179. }
  180. func (e *NoRewardEngine) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error {
  181. return e.inner.VerifyHeader(chain, header, seal)
  182. }
  183. func (e *NoRewardEngine) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) {
  184. return e.inner.VerifyHeaders(chain, headers, seals)
  185. }
  186. func (e *NoRewardEngine) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
  187. return e.inner.VerifyUncles(chain, block)
  188. }
  189. func (e *NoRewardEngine) VerifySeal(chain consensus.ChainReader, header *types.Header) error {
  190. return e.inner.VerifySeal(chain, header)
  191. }
  192. func (e *NoRewardEngine) Prepare(chain consensus.ChainReader, header *types.Header) error {
  193. return e.inner.Prepare(chain, header)
  194. }
  195. func (e *NoRewardEngine) accumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) {
  196. // Simply touch miner and uncle coinbase accounts
  197. reward := big.NewInt(0)
  198. for _, uncle := range uncles {
  199. state.AddBalance(uncle.Coinbase, reward)
  200. }
  201. state.AddBalance(header.Coinbase, reward)
  202. }
  203. func (e *NoRewardEngine) Finalize(chain consensus.ChainReader, header *types.Header, statedb *state.StateDB, txs []*types.Transaction,
  204. uncles []*types.Header) {
  205. if e.rewardsOn {
  206. e.inner.Finalize(chain, header, statedb, txs, uncles)
  207. } else {
  208. e.accumulateRewards(chain.Config(), statedb, header, uncles)
  209. header.Root = statedb.IntermediateRoot(chain.Config().IsEIP158(header.Number))
  210. }
  211. }
  212. func (e *NoRewardEngine) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Header, statedb *state.StateDB, txs []*types.Transaction,
  213. uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
  214. if e.rewardsOn {
  215. return e.inner.FinalizeAndAssemble(chain, header, statedb, txs, uncles, receipts)
  216. } else {
  217. e.accumulateRewards(chain.Config(), statedb, header, uncles)
  218. header.Root = statedb.IntermediateRoot(chain.Config().IsEIP158(header.Number))
  219. // Header seems complete, assemble into a block and return
  220. return types.NewBlock(header, txs, uncles, receipts), nil
  221. }
  222. }
  223. func (e *NoRewardEngine) Seal(chain consensus.ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
  224. return e.inner.Seal(chain, block, results, stop)
  225. }
  226. func (e *NoRewardEngine) SealHash(header *types.Header) common.Hash {
  227. return e.inner.SealHash(header)
  228. }
  229. func (e *NoRewardEngine) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int {
  230. return e.inner.CalcDifficulty(chain, time, parent)
  231. }
  232. func (e *NoRewardEngine) APIs(chain consensus.ChainReader) []rpc.API {
  233. return e.inner.APIs(chain)
  234. }
  235. func (e *NoRewardEngine) Close() error {
  236. return e.inner.Close()
  237. }
  238. func (api *RetestethAPI) SetChainParams(ctx context.Context, chainParams ChainParams) (bool, error) {
  239. // Clean up
  240. if api.blockchain != nil {
  241. api.blockchain.Stop()
  242. }
  243. if api.engine != nil {
  244. api.engine.Close()
  245. }
  246. if api.ethDb != nil {
  247. api.ethDb.Close()
  248. }
  249. ethDb := rawdb.NewMemoryDatabase()
  250. accounts := make(core.GenesisAlloc)
  251. for address, account := range chainParams.Accounts {
  252. balance := big.NewInt(0)
  253. if account.Balance != nil {
  254. balance.Set((*big.Int)(account.Balance))
  255. }
  256. var nonce uint64
  257. if account.Nonce != nil {
  258. nonce = uint64(*account.Nonce)
  259. }
  260. if account.Precompiled == nil || account.Balance != nil {
  261. storage := make(map[common.Hash]common.Hash)
  262. for k, v := range account.Storage {
  263. storage[common.HexToHash(k)] = common.HexToHash(v)
  264. }
  265. accounts[address] = core.GenesisAccount{
  266. Balance: balance,
  267. Code: account.Code,
  268. Nonce: nonce,
  269. Storage: storage,
  270. }
  271. }
  272. }
  273. chainId := big.NewInt(1)
  274. if chainParams.Params.ChainID != nil {
  275. chainId.Set((*big.Int)(chainParams.Params.ChainID))
  276. }
  277. var (
  278. homesteadBlock *big.Int
  279. daoForkBlock *big.Int
  280. eip150Block *big.Int
  281. eip155Block *big.Int
  282. eip158Block *big.Int
  283. byzantiumBlock *big.Int
  284. constantinopleBlock *big.Int
  285. petersburgBlock *big.Int
  286. )
  287. if chainParams.Params.HomesteadForkBlock != nil {
  288. homesteadBlock = big.NewInt(int64(*chainParams.Params.HomesteadForkBlock))
  289. }
  290. if chainParams.Params.DaoHardforkBlock != nil {
  291. daoForkBlock = big.NewInt(int64(*chainParams.Params.DaoHardforkBlock))
  292. }
  293. if chainParams.Params.EIP150ForkBlock != nil {
  294. eip150Block = big.NewInt(int64(*chainParams.Params.EIP150ForkBlock))
  295. }
  296. if chainParams.Params.EIP158ForkBlock != nil {
  297. eip158Block = big.NewInt(int64(*chainParams.Params.EIP158ForkBlock))
  298. eip155Block = eip158Block
  299. }
  300. if chainParams.Params.ByzantiumForkBlock != nil {
  301. byzantiumBlock = big.NewInt(int64(*chainParams.Params.ByzantiumForkBlock))
  302. }
  303. if chainParams.Params.ConstantinopleForkBlock != nil {
  304. constantinopleBlock = big.NewInt(int64(*chainParams.Params.ConstantinopleForkBlock))
  305. }
  306. if chainParams.Params.ConstantinopleFixForkBlock != nil {
  307. petersburgBlock = big.NewInt(int64(*chainParams.Params.ConstantinopleFixForkBlock))
  308. }
  309. if constantinopleBlock != nil && petersburgBlock == nil {
  310. petersburgBlock = big.NewInt(100000000000)
  311. }
  312. genesis := &core.Genesis{
  313. Config: &params.ChainConfig{
  314. ChainID: chainId,
  315. HomesteadBlock: homesteadBlock,
  316. DAOForkBlock: daoForkBlock,
  317. DAOForkSupport: false,
  318. EIP150Block: eip150Block,
  319. EIP155Block: eip155Block,
  320. EIP158Block: eip158Block,
  321. ByzantiumBlock: byzantiumBlock,
  322. ConstantinopleBlock: constantinopleBlock,
  323. PetersburgBlock: petersburgBlock,
  324. },
  325. Nonce: uint64(chainParams.Genesis.Nonce),
  326. Timestamp: uint64(chainParams.Genesis.Timestamp),
  327. ExtraData: chainParams.Genesis.ExtraData,
  328. GasLimit: uint64(chainParams.Genesis.GasLimit),
  329. Difficulty: big.NewInt(0).Set((*big.Int)(chainParams.Genesis.Difficulty)),
  330. Mixhash: common.BigToHash((*big.Int)(chainParams.Genesis.MixHash)),
  331. Coinbase: chainParams.Genesis.Author,
  332. ParentHash: chainParams.Genesis.ParentHash,
  333. Alloc: accounts,
  334. }
  335. chainConfig, genesisHash, err := core.SetupGenesisBlock(ethDb, genesis)
  336. if err != nil {
  337. return false, err
  338. }
  339. fmt.Printf("Chain config: %v\n", chainConfig)
  340. var inner consensus.Engine
  341. switch chainParams.SealEngine {
  342. case "NoProof", "NoReward":
  343. inner = ethash.NewFaker()
  344. case "Ethash":
  345. inner = ethash.New(ethash.Config{
  346. CacheDir: "ethash",
  347. CachesInMem: 2,
  348. CachesOnDisk: 3,
  349. DatasetsInMem: 1,
  350. DatasetsOnDisk: 2,
  351. }, nil, false)
  352. default:
  353. return false, fmt.Errorf("unrecognised seal engine: %s", chainParams.SealEngine)
  354. }
  355. engine := &NoRewardEngine{inner: inner, rewardsOn: chainParams.SealEngine != "NoReward"}
  356. blockchain, err := core.NewBlockChain(ethDb, nil, chainConfig, engine, vm.Config{}, nil)
  357. if err != nil {
  358. return false, err
  359. }
  360. api.chainConfig = chainConfig
  361. api.genesisHash = genesisHash
  362. api.author = chainParams.Genesis.Author
  363. api.extraData = chainParams.Genesis.ExtraData
  364. api.ethDb = ethDb
  365. api.engine = engine
  366. api.blockchain = blockchain
  367. api.db = state.NewDatabase(api.ethDb)
  368. api.blockNumber = 0
  369. api.txMap = make(map[common.Address]map[uint64]*types.Transaction)
  370. api.txSenders = make(map[common.Address]struct{})
  371. api.blockInterval = 0
  372. return true, nil
  373. }
  374. func (api *RetestethAPI) SendRawTransaction(ctx context.Context, rawTx hexutil.Bytes) (common.Hash, error) {
  375. tx := new(types.Transaction)
  376. if err := rlp.DecodeBytes(rawTx, tx); err != nil {
  377. // Return nil is not by mistake - some tests include sending transaction where gasLimit overflows uint64
  378. return common.Hash{}, nil
  379. }
  380. signer := types.MakeSigner(api.chainConfig, big.NewInt(int64(api.blockNumber)))
  381. sender, err := types.Sender(signer, tx)
  382. if err != nil {
  383. return common.Hash{}, err
  384. }
  385. if nonceMap, ok := api.txMap[sender]; ok {
  386. nonceMap[tx.Nonce()] = tx
  387. } else {
  388. nonceMap = make(map[uint64]*types.Transaction)
  389. nonceMap[tx.Nonce()] = tx
  390. api.txMap[sender] = nonceMap
  391. }
  392. api.txSenders[sender] = struct{}{}
  393. return tx.Hash(), nil
  394. }
  395. func (api *RetestethAPI) MineBlocks(ctx context.Context, number uint64) (bool, error) {
  396. for i := 0; i < int(number); i++ {
  397. if err := api.mineBlock(); err != nil {
  398. return false, err
  399. }
  400. }
  401. fmt.Printf("Mined %d blocks\n", number)
  402. return true, nil
  403. }
  404. func (api *RetestethAPI) mineBlock() error {
  405. parentHash := rawdb.ReadCanonicalHash(api.ethDb, api.blockNumber)
  406. parent := rawdb.ReadBlock(api.ethDb, parentHash, api.blockNumber)
  407. var timestamp uint64
  408. if api.blockInterval == 0 {
  409. timestamp = uint64(time.Now().Unix())
  410. } else {
  411. timestamp = parent.Time() + api.blockInterval
  412. }
  413. gasLimit := core.CalcGasLimit(parent, 9223372036854775807, 9223372036854775807)
  414. header := &types.Header{
  415. ParentHash: parent.Hash(),
  416. Number: big.NewInt(int64(api.blockNumber + 1)),
  417. GasLimit: gasLimit,
  418. Extra: api.extraData,
  419. Time: timestamp,
  420. }
  421. header.Coinbase = api.author
  422. if api.engine != nil {
  423. api.engine.Prepare(api.blockchain, header)
  424. }
  425. // If we are care about TheDAO hard-fork check whether to override the extra-data or not
  426. if daoBlock := api.chainConfig.DAOForkBlock; daoBlock != nil {
  427. // Check whether the block is among the fork extra-override range
  428. limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange)
  429. if header.Number.Cmp(daoBlock) >= 0 && header.Number.Cmp(limit) < 0 {
  430. // Depending whether we support or oppose the fork, override differently
  431. if api.chainConfig.DAOForkSupport {
  432. header.Extra = common.CopyBytes(params.DAOForkBlockExtra)
  433. } else if bytes.Equal(header.Extra, params.DAOForkBlockExtra) {
  434. header.Extra = []byte{} // If miner opposes, don't let it use the reserved extra-data
  435. }
  436. }
  437. }
  438. statedb, err := api.blockchain.StateAt(parent.Root())
  439. if err != nil {
  440. return err
  441. }
  442. if api.chainConfig.DAOForkSupport && api.chainConfig.DAOForkBlock != nil && api.chainConfig.DAOForkBlock.Cmp(header.Number) == 0 {
  443. misc.ApplyDAOHardFork(statedb)
  444. }
  445. gasPool := new(core.GasPool).AddGas(header.GasLimit)
  446. txCount := 0
  447. var txs []*types.Transaction
  448. var receipts []*types.Receipt
  449. var coalescedLogs []*types.Log
  450. var blockFull = gasPool.Gas() < params.TxGas
  451. for address := range api.txSenders {
  452. if blockFull {
  453. break
  454. }
  455. m := api.txMap[address]
  456. for nonce := statedb.GetNonce(address); ; nonce++ {
  457. if tx, ok := m[nonce]; ok {
  458. // Try to apply transactions to the state
  459. statedb.Prepare(tx.Hash(), common.Hash{}, txCount)
  460. snap := statedb.Snapshot()
  461. receipt, _, err := core.ApplyTransaction(
  462. api.chainConfig,
  463. api.blockchain,
  464. &api.author,
  465. gasPool,
  466. statedb,
  467. header, tx, &header.GasUsed, *api.blockchain.GetVMConfig(),
  468. )
  469. if err != nil {
  470. statedb.RevertToSnapshot(snap)
  471. break
  472. }
  473. txs = append(txs, tx)
  474. receipts = append(receipts, receipt)
  475. coalescedLogs = append(coalescedLogs, receipt.Logs...)
  476. delete(m, nonce)
  477. if len(m) == 0 {
  478. // Last tx for the sender
  479. delete(api.txMap, address)
  480. delete(api.txSenders, address)
  481. }
  482. txCount++
  483. if gasPool.Gas() < params.TxGas {
  484. blockFull = true
  485. break
  486. }
  487. } else {
  488. break // Gap in the nonces
  489. }
  490. }
  491. }
  492. block, err := api.engine.FinalizeAndAssemble(api.blockchain, header, statedb, txs, []*types.Header{}, receipts)
  493. return api.importBlock(block)
  494. }
  495. func (api *RetestethAPI) importBlock(block *types.Block) error {
  496. if _, err := api.blockchain.InsertChain([]*types.Block{block}); err != nil {
  497. return err
  498. }
  499. api.blockNumber = block.NumberU64()
  500. fmt.Printf("Imported block %d\n", block.NumberU64())
  501. return nil
  502. }
  503. func (api *RetestethAPI) ModifyTimestamp(ctx context.Context, interval uint64) (bool, error) {
  504. api.blockInterval = interval
  505. return true, nil
  506. }
  507. func (api *RetestethAPI) ImportRawBlock(ctx context.Context, rawBlock hexutil.Bytes) (common.Hash, error) {
  508. block := new(types.Block)
  509. if err := rlp.DecodeBytes(rawBlock, block); err != nil {
  510. return common.Hash{}, err
  511. }
  512. fmt.Printf("Importing block %d with parent hash: %x, genesisHash: %x\n", block.NumberU64(), block.ParentHash(), api.genesisHash)
  513. if err := api.importBlock(block); err != nil {
  514. return common.Hash{}, err
  515. }
  516. return block.Hash(), nil
  517. }
  518. func (api *RetestethAPI) RewindToBlock(ctx context.Context, newHead uint64) (bool, error) {
  519. if err := api.blockchain.SetHead(newHead); err != nil {
  520. return false, err
  521. }
  522. api.blockNumber = newHead
  523. return true, nil
  524. }
  525. var emptyListHash common.Hash = common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")
  526. func (api *RetestethAPI) GetLogHash(ctx context.Context, txHash common.Hash) (common.Hash, error) {
  527. receipt, _, _, _ := rawdb.ReadReceipt(api.ethDb, txHash, api.chainConfig)
  528. if receipt == nil {
  529. return emptyListHash, nil
  530. } else {
  531. if logListRlp, err := rlp.EncodeToBytes(receipt.Logs); err != nil {
  532. return common.Hash{}, err
  533. } else {
  534. return common.BytesToHash(crypto.Keccak256(logListRlp)), nil
  535. }
  536. }
  537. }
  538. func (api *RetestethAPI) BlockNumber(ctx context.Context) (uint64, error) {
  539. //fmt.Printf("BlockNumber, response: %d\n", api.blockNumber)
  540. return api.blockNumber, nil
  541. }
  542. func (api *RetestethAPI) GetBlockByNumber(ctx context.Context, blockNr math.HexOrDecimal64, fullTx bool) (map[string]interface{}, error) {
  543. block := api.blockchain.GetBlockByNumber(uint64(blockNr))
  544. if block != nil {
  545. response, err := RPCMarshalBlock(block, true, fullTx)
  546. if err != nil {
  547. return nil, err
  548. }
  549. response["author"] = response["miner"]
  550. response["totalDifficulty"] = (*hexutil.Big)(api.blockchain.GetTd(block.Hash(), uint64(blockNr)))
  551. return response, err
  552. }
  553. return nil, fmt.Errorf("block %d not found", blockNr)
  554. }
  555. func (api *RetestethAPI) AccountRangeAt(ctx context.Context,
  556. blockHashOrNumber *math.HexOrDecimal256, txIndex uint64,
  557. addressHash *math.HexOrDecimal256, maxResults uint64,
  558. ) (AccountRangeResult, error) {
  559. var (
  560. header *types.Header
  561. block *types.Block
  562. )
  563. if (*big.Int)(blockHashOrNumber).Cmp(big.NewInt(math.MaxInt64)) > 0 {
  564. blockHash := common.BigToHash((*big.Int)(blockHashOrNumber))
  565. header = api.blockchain.GetHeaderByHash(blockHash)
  566. block = api.blockchain.GetBlockByHash(blockHash)
  567. //fmt.Printf("Account range: %x, txIndex %d, start: %x, maxResults: %d\n", blockHash, txIndex, common.BigToHash((*big.Int)(addressHash)), maxResults)
  568. } else {
  569. blockNumber := (*big.Int)(blockHashOrNumber).Uint64()
  570. header = api.blockchain.GetHeaderByNumber(blockNumber)
  571. block = api.blockchain.GetBlockByNumber(blockNumber)
  572. //fmt.Printf("Account range: %d, txIndex %d, start: %x, maxResults: %d\n", blockNumber, txIndex, common.BigToHash((*big.Int)(addressHash)), maxResults)
  573. }
  574. parentHeader := api.blockchain.GetHeaderByHash(header.ParentHash)
  575. var root common.Hash
  576. var statedb *state.StateDB
  577. var err error
  578. if parentHeader == nil || int(txIndex) >= len(block.Transactions()) {
  579. root = header.Root
  580. statedb, err = api.blockchain.StateAt(root)
  581. if err != nil {
  582. return AccountRangeResult{}, err
  583. }
  584. } else {
  585. root = parentHeader.Root
  586. statedb, err = api.blockchain.StateAt(root)
  587. if err != nil {
  588. return AccountRangeResult{}, err
  589. }
  590. // Recompute transactions up to the target index.
  591. signer := types.MakeSigner(api.blockchain.Config(), block.Number())
  592. for idx, tx := range block.Transactions() {
  593. // Assemble the transaction call message and return if the requested offset
  594. msg, _ := tx.AsMessage(signer)
  595. context := core.NewEVMContext(msg, block.Header(), api.blockchain, nil)
  596. // Not yet the searched for transaction, execute on top of the current state
  597. vmenv := vm.NewEVM(context, statedb, api.blockchain.Config(), vm.Config{})
  598. if _, _, _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil {
  599. return AccountRangeResult{}, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err)
  600. }
  601. // Ensure any modifications are committed to the state
  602. // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
  603. root = statedb.IntermediateRoot(vmenv.ChainConfig().IsEIP158(block.Number()))
  604. if idx == int(txIndex) {
  605. // This is to make sure root can be opened by OpenTrie
  606. root, err = statedb.Commit(api.chainConfig.IsEIP158(block.Number()))
  607. if err != nil {
  608. return AccountRangeResult{}, err
  609. }
  610. break
  611. }
  612. }
  613. }
  614. accountTrie, err := statedb.Database().OpenTrie(root)
  615. if err != nil {
  616. return AccountRangeResult{}, err
  617. }
  618. it := trie.NewIterator(accountTrie.NodeIterator(common.BigToHash((*big.Int)(addressHash)).Bytes()))
  619. result := AccountRangeResult{AddressMap: make(map[common.Hash]common.Address)}
  620. for i := 0; /*i < int(maxResults) && */ it.Next(); i++ {
  621. if preimage := accountTrie.GetKey(it.Key); preimage != nil {
  622. result.AddressMap[common.BytesToHash(it.Key)] = common.BytesToAddress(preimage)
  623. //fmt.Printf("%x: %x\n", it.Key, preimage)
  624. } else {
  625. //fmt.Printf("could not find preimage for %x\n", it.Key)
  626. }
  627. }
  628. //fmt.Printf("Number of entries returned: %d\n", len(result.AddressMap))
  629. // Add the 'next key' so clients can continue downloading.
  630. if it.Next() {
  631. next := common.BytesToHash(it.Key)
  632. result.NextKey = next
  633. }
  634. return result, nil
  635. }
  636. func (api *RetestethAPI) GetBalance(ctx context.Context, address common.Address, blockNr math.HexOrDecimal64) (*math.HexOrDecimal256, error) {
  637. //fmt.Printf("GetBalance %x, block %d\n", address, blockNr)
  638. header := api.blockchain.GetHeaderByNumber(uint64(blockNr))
  639. statedb, err := api.blockchain.StateAt(header.Root)
  640. if err != nil {
  641. return nil, err
  642. }
  643. return (*math.HexOrDecimal256)(statedb.GetBalance(address)), nil
  644. }
  645. func (api *RetestethAPI) GetCode(ctx context.Context, address common.Address, blockNr math.HexOrDecimal64) (hexutil.Bytes, error) {
  646. header := api.blockchain.GetHeaderByNumber(uint64(blockNr))
  647. statedb, err := api.blockchain.StateAt(header.Root)
  648. if err != nil {
  649. return nil, err
  650. }
  651. return statedb.GetCode(address), nil
  652. }
  653. func (api *RetestethAPI) GetTransactionCount(ctx context.Context, address common.Address, blockNr math.HexOrDecimal64) (uint64, error) {
  654. header := api.blockchain.GetHeaderByNumber(uint64(blockNr))
  655. statedb, err := api.blockchain.StateAt(header.Root)
  656. if err != nil {
  657. return 0, err
  658. }
  659. return statedb.GetNonce(address), nil
  660. }
  661. func (api *RetestethAPI) StorageRangeAt(ctx context.Context,
  662. blockHashOrNumber *math.HexOrDecimal256, txIndex uint64,
  663. address common.Address,
  664. begin *math.HexOrDecimal256, maxResults uint64,
  665. ) (StorageRangeResult, error) {
  666. var (
  667. header *types.Header
  668. block *types.Block
  669. )
  670. if (*big.Int)(blockHashOrNumber).Cmp(big.NewInt(math.MaxInt64)) > 0 {
  671. blockHash := common.BigToHash((*big.Int)(blockHashOrNumber))
  672. header = api.blockchain.GetHeaderByHash(blockHash)
  673. block = api.blockchain.GetBlockByHash(blockHash)
  674. //fmt.Printf("Storage range: %x, txIndex %d, addr: %x, start: %x, maxResults: %d\n",
  675. // blockHash, txIndex, address, common.BigToHash((*big.Int)(begin)), maxResults)
  676. } else {
  677. blockNumber := (*big.Int)(blockHashOrNumber).Uint64()
  678. header = api.blockchain.GetHeaderByNumber(blockNumber)
  679. block = api.blockchain.GetBlockByNumber(blockNumber)
  680. //fmt.Printf("Storage range: %d, txIndex %d, addr: %x, start: %x, maxResults: %d\n",
  681. // blockNumber, txIndex, address, common.BigToHash((*big.Int)(begin)), maxResults)
  682. }
  683. parentHeader := api.blockchain.GetHeaderByHash(header.ParentHash)
  684. var root common.Hash
  685. var statedb *state.StateDB
  686. var err error
  687. if parentHeader == nil || int(txIndex) >= len(block.Transactions()) {
  688. root = header.Root
  689. statedb, err = api.blockchain.StateAt(root)
  690. if err != nil {
  691. return StorageRangeResult{}, err
  692. }
  693. } else {
  694. root = parentHeader.Root
  695. statedb, err = api.blockchain.StateAt(root)
  696. if err != nil {
  697. return StorageRangeResult{}, err
  698. }
  699. // Recompute transactions up to the target index.
  700. signer := types.MakeSigner(api.blockchain.Config(), block.Number())
  701. for idx, tx := range block.Transactions() {
  702. // Assemble the transaction call message and return if the requested offset
  703. msg, _ := tx.AsMessage(signer)
  704. context := core.NewEVMContext(msg, block.Header(), api.blockchain, nil)
  705. // Not yet the searched for transaction, execute on top of the current state
  706. vmenv := vm.NewEVM(context, statedb, api.blockchain.Config(), vm.Config{})
  707. if _, _, _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil {
  708. return StorageRangeResult{}, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err)
  709. }
  710. // Ensure any modifications are committed to the state
  711. // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
  712. root = statedb.IntermediateRoot(vmenv.ChainConfig().IsEIP158(block.Number()))
  713. if idx == int(txIndex) {
  714. // This is to make sure root can be opened by OpenTrie
  715. root, err = statedb.Commit(vmenv.ChainConfig().IsEIP158(block.Number()))
  716. if err != nil {
  717. return StorageRangeResult{}, err
  718. }
  719. }
  720. }
  721. }
  722. storageTrie := statedb.StorageTrie(address)
  723. it := trie.NewIterator(storageTrie.NodeIterator(common.BigToHash((*big.Int)(begin)).Bytes()))
  724. result := StorageRangeResult{Storage: make(map[common.Hash]SRItem)}
  725. for i := 0; /*i < int(maxResults) && */ it.Next(); i++ {
  726. if preimage := storageTrie.GetKey(it.Key); preimage != nil {
  727. key := (*math.HexOrDecimal256)(big.NewInt(0).SetBytes(preimage))
  728. v, _, err := rlp.SplitString(it.Value)
  729. if err != nil {
  730. return StorageRangeResult{}, err
  731. }
  732. value := (*math.HexOrDecimal256)(big.NewInt(0).SetBytes(v))
  733. ks, _ := key.MarshalText()
  734. vs, _ := value.MarshalText()
  735. if len(ks)%2 != 0 {
  736. ks = append(append(append([]byte{}, ks[:2]...), byte('0')), ks[2:]...)
  737. }
  738. if len(vs)%2 != 0 {
  739. vs = append(append(append([]byte{}, vs[:2]...), byte('0')), vs[2:]...)
  740. }
  741. result.Storage[common.BytesToHash(it.Key)] = SRItem{
  742. Key: string(ks),
  743. Value: string(vs),
  744. }
  745. //fmt.Printf("Key: %s, Value: %s\n", ks, vs)
  746. } else {
  747. //fmt.Printf("Did not find preimage for %x\n", it.Key)
  748. }
  749. }
  750. if it.Next() {
  751. result.Complete = false
  752. } else {
  753. result.Complete = true
  754. }
  755. return result, nil
  756. }
  757. func (api *RetestethAPI) ClientVersion(ctx context.Context) (string, error) {
  758. return "Geth-" + params.VersionWithCommit(gitCommit, gitDate), nil
  759. }
  760. // splitAndTrim splits input separated by a comma
  761. // and trims excessive white space from the substrings.
  762. func splitAndTrim(input string) []string {
  763. result := strings.Split(input, ",")
  764. for i, r := range result {
  765. result[i] = strings.TrimSpace(r)
  766. }
  767. return result
  768. }
  769. func retesteth(ctx *cli.Context) error {
  770. log.Info("Welcome to retesteth!")
  771. // register signer API with server
  772. var (
  773. extapiURL = "n/a"
  774. )
  775. apiImpl := &RetestethAPI{}
  776. var testApi RetestethTestAPI = apiImpl
  777. var ethApi RetestethEthAPI = apiImpl
  778. var debugApi RetestethDebugAPI = apiImpl
  779. var web3Api RetestWeb3API = apiImpl
  780. rpcAPI := []rpc.API{
  781. {
  782. Namespace: "test",
  783. Public: true,
  784. Service: testApi,
  785. Version: "1.0",
  786. },
  787. {
  788. Namespace: "eth",
  789. Public: true,
  790. Service: ethApi,
  791. Version: "1.0",
  792. },
  793. {
  794. Namespace: "debug",
  795. Public: true,
  796. Service: debugApi,
  797. Version: "1.0",
  798. },
  799. {
  800. Namespace: "web3",
  801. Public: true,
  802. Service: web3Api,
  803. Version: "1.0",
  804. },
  805. }
  806. vhosts := splitAndTrim(ctx.GlobalString(utils.RPCVirtualHostsFlag.Name))
  807. cors := splitAndTrim(ctx.GlobalString(utils.RPCCORSDomainFlag.Name))
  808. // start http server
  809. httpEndpoint := fmt.Sprintf("%s:%d", ctx.GlobalString(utils.RPCListenAddrFlag.Name), ctx.Int(rpcPortFlag.Name))
  810. listener, _, err := rpc.StartHTTPEndpoint(httpEndpoint, rpcAPI, []string{"test", "eth", "debug", "web3"}, cors, vhosts, rpc.DefaultHTTPTimeouts)
  811. if err != nil {
  812. utils.Fatalf("Could not start RPC api: %v", err)
  813. }
  814. extapiURL = fmt.Sprintf("http://%s", httpEndpoint)
  815. log.Info("HTTP endpoint opened", "url", extapiURL)
  816. defer func() {
  817. listener.Close()
  818. log.Info("HTTP endpoint closed", "url", httpEndpoint)
  819. }()
  820. abortChan := make(chan os.Signal)
  821. signal.Notify(abortChan, os.Interrupt)
  822. sig := <-abortChan
  823. log.Info("Exiting...", "signal", sig)
  824. return nil
  825. }