transition-test.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #!/bin/bash
  2. ticks="\`\`\`"
  3. function showjson(){
  4. echo "\`$1\`:"
  5. echo "${ticks}json"
  6. cat $1
  7. echo ""
  8. echo "$ticks"
  9. }
  10. function demo(){
  11. echo "$ticks"
  12. echo "$1"
  13. $1
  14. echo ""
  15. echo "$ticks"
  16. echo ""
  17. }
  18. function tick(){
  19. echo "$ticks"
  20. }
  21. cat << EOF
  22. ## EVM state transition tool
  23. The \`evm t8n\` tool is a stateless state transition utility. It is a utility
  24. which can
  25. 1. Take a prestate, including
  26. - Accounts,
  27. - Block context information,
  28. - Previous blockshashes (*optional)
  29. 2. Apply a set of transactions,
  30. 3. Apply a mining-reward (*optional),
  31. 4. And generate a post-state, including
  32. - State root, transaction root, receipt root,
  33. - Information about rejected transactions,
  34. - Optionally: a full or partial post-state dump
  35. ## Specification
  36. The idea is to specify the behaviour of this binary very _strict_, so that other
  37. node implementors can build replicas based on their own state-machines, and the
  38. state generators can swap between a \`geth\`-based implementation and a \`parityvm\`-based
  39. implementation.
  40. ### Command line params
  41. Command line params that has to be supported are
  42. $(tick)
  43. ` ./evm t8n -h | grep "trace\|output\|state\."`
  44. $(tick)
  45. ### Error codes and output
  46. All logging should happen against the \`stderr\`.
  47. There are a few (not many) errors that can occur, those are defined below.
  48. #### EVM-based errors (\`2\` to \`9\`)
  49. - Other EVM error. Exit code \`2\`
  50. - Failed configuration: when a non-supported or invalid fork was specified. Exit code \`3\`.
  51. - Block history is not supplied, but needed for a \`BLOCKHASH\` operation. If \`BLOCKHASH\`
  52. is invoked targeting a block which history has not been provided for, the program will
  53. exit with code \`4\`.
  54. #### IO errors (\`10\`-\`20\`)
  55. - Invalid input json: the supplied data could not be marshalled.
  56. The program will exit with code \`10\`
  57. - IO problems: failure to load or save files, the program will exit with code \`11\`
  58. EOF
  59. # This should exit with 3
  60. ./evm t8n --input.alloc=./testdata/1/alloc.json --input.txs=./testdata/1/txs.json --input.env=./testdata/1/env.json --state.fork=Frontier+1346 2>/dev/null
  61. if [ $? != 3 ]; then
  62. echo "Failed, exitcode should be 3"
  63. fi
  64. cat << EOF
  65. ## Examples
  66. ### Basic usage
  67. Invoking it with the provided example files
  68. EOF
  69. cmd="./evm t8n --input.alloc=./testdata/1/alloc.json --input.txs=./testdata/1/txs.json --input.env=./testdata/1/env.json"
  70. tick;echo "$cmd"; tick
  71. $cmd 2>/dev/null
  72. echo "Two resulting files:"
  73. echo ""
  74. showjson alloc.json
  75. showjson result.json
  76. echo ""
  77. echo "We can make them spit out the data to e.g. \`stdout\` like this:"
  78. cmd="./evm t8n --input.alloc=./testdata/1/alloc.json --input.txs=./testdata/1/txs.json --input.env=./testdata/1/env.json --output.result=stdout --output.alloc=stdout"
  79. tick;echo "$cmd"; tick
  80. output=`$cmd 2>/dev/null`
  81. echo "Output:"
  82. echo "${ticks}json"
  83. echo "$output"
  84. echo "$ticks"
  85. cat << EOF
  86. ## About Ommers
  87. Mining rewards and ommer rewards might need to be added. This is how those are applied:
  88. - \`block_reward\` is the block mining reward for the miner (\`0xaa\`), of a block at height \`N\`.
  89. - For each ommer (mined by \`0xbb\`), with blocknumber \`N-delta\`
  90. - (where \`delta\` is the difference between the current block and the ommer)
  91. - The account \`0xbb\` (ommer miner) is awarded \`(8-delta)/ 8 * block_reward\`
  92. - The account \`0xaa\` (block miner) is awarded \`block_reward / 32\`
  93. To make \`state_t8n\` apply these, the following inputs are required:
  94. - \`state.reward\`
  95. - For ethash, it is \`5000000000000000000\` \`wei\`,
  96. - If this is not defined, mining rewards are not applied,
  97. - A value of \`0\` is valid, and causes accounts to be 'touched'.
  98. - For each ommer, the tool needs to be given an \`address\` and a \`delta\`. This
  99. is done via the \`env\`.
  100. Note: the tool does not verify that e.g. the normal uncle rules apply,
  101. and allows e.g two uncles at the same height, or the uncle-distance. This means that
  102. the tool allows for negative uncle reward (distance > 8)
  103. Example:
  104. EOF
  105. showjson ./testdata/5/env.json
  106. echo "When applying this, using a reward of \`0x08\`"
  107. cmd="./evm t8n --input.alloc=./testdata/5/alloc.json -input.txs=./testdata/5/txs.json --input.env=./testdata/5/env.json --output.alloc=stdout --state.reward=0x80"
  108. output=`$cmd 2>/dev/null`
  109. echo "Output:"
  110. echo "${ticks}json"
  111. echo "$output"
  112. echo "$ticks"
  113. echo "### Future EIPS"
  114. echo ""
  115. echo "It is also possible to experiment with future eips that are not yet defined in a hard fork."
  116. echo "Example, putting EIP-1344 into Frontier: "
  117. cmd="./evm t8n --state.fork=Frontier+1344 --input.pre=./testdata/1/pre.json --input.txs=./testdata/1/txs.json --input.env=/testdata/1/env.json"
  118. tick;echo "$cmd"; tick
  119. echo ""
  120. echo "### Block history"
  121. echo ""
  122. echo "The \`BLOCKHASH\` opcode requires blockhashes to be provided by the caller, inside the \`env\`."
  123. echo "If a required blockhash is not provided, the exit code should be \`4\`:"
  124. echo "Example where blockhashes are provided: "
  125. demo "./evm --verbosity=1 t8n --input.alloc=./testdata/3/alloc.json --input.txs=./testdata/3/txs.json --input.env=./testdata/3/env.json --trace"
  126. cmd="cat trace-0-0x72fadbef39cd251a437eea619cfeda752271a5faaaa2147df012e112159ffb81.jsonl | grep BLOCKHASH -C2"
  127. tick && echo $cmd && tick
  128. echo "$ticks"
  129. cat trace-0-0x72fadbef39cd251a437eea619cfeda752271a5faaaa2147df012e112159ffb81.jsonl | grep BLOCKHASH -C2
  130. echo "$ticks"
  131. echo ""
  132. echo "In this example, the caller has not provided the required blockhash:"
  133. cmd="./evm t8n --input.alloc=./testdata/4/alloc.json --input.txs=./testdata/4/txs.json --input.env=./testdata/4/env.json --trace"
  134. tick && echo $cmd && $cmd
  135. errc=$?
  136. tick
  137. echo "Error code: $errc"
  138. echo ""
  139. echo "### Chaining"
  140. echo ""
  141. echo "Another thing that can be done, is to chain invocations:"
  142. cmd1="./evm t8n --input.alloc=./testdata/1/alloc.json --input.txs=./testdata/1/txs.json --input.env=./testdata/1/env.json --output.alloc=stdout"
  143. cmd2="./evm t8n --input.alloc=stdin --input.env=./testdata/1/env.json --input.txs=./testdata/1/txs.json"
  144. echo "$ticks"
  145. echo "$cmd1 | $cmd2"
  146. output=$($cmd1 | $cmd2 )
  147. echo $output
  148. echo "$ticks"
  149. echo "What happened here, is that we first applied two identical transactions, so the second one was rejected. "
  150. echo "Then, taking the poststate alloc as the input for the next state, we tried again to include"
  151. echo "the same two transactions: this time, both failed due to too low nonce."
  152. echo ""
  153. echo "In order to meaningfully chain invocations, one would need to provide meaningful new \`env\`, otherwise the"
  154. echo "actual blocknumber (exposed to the EVM) would not increase."
  155. echo ""
  156. echo "### Transactions in RLP form"
  157. echo ""
  158. echo "It is possible to provide already-signed transactions as input to, using an \`input.txs\` which ends with the \`rlp\` suffix."
  159. echo "The input format for RLP-form transactions is _identical_ to the _output_ format for block bodies. Therefore, it's fully possible"
  160. echo "to use the evm to go from \`json\` input to \`rlp\` input."
  161. echo ""
  162. echo "The following command takes **json** the transactions in \`./testdata/13/txs.json\` and signs them. After execution, they are output to \`signed_txs.rlp\`.:"
  163. demo "./evm t8n --state.fork=London --input.alloc=./testdata/13/alloc.json --input.txs=./testdata/13/txs.json --input.env=./testdata/13/env.json --output.result=alloc_jsontx.json --output.body=signed_txs.rlp"
  164. echo "The \`output.body\` is the rlp-list of transactions, encoded in hex and placed in a string a'la \`json\` encoding rules:"
  165. demo "cat signed_txs.rlp"
  166. echo "We can use \`rlpdump\` to check what the contents are: "
  167. echo "$ticks"
  168. echo "rlpdump -hex \$(cat signed_txs.rlp | jq -r )"
  169. rlpdump -hex $(cat signed_txs.rlp | jq -r )
  170. echo "$ticks"
  171. echo "Now, we can now use those (or any other already signed transactions), as input, like so: "
  172. demo "./evm t8n --state.fork=London --input.alloc=./testdata/13/alloc.json --input.txs=./signed_txs.rlp --input.env=./testdata/13/env.json --output.result=alloc_rlptx.json"
  173. echo "You might have noticed that the results from these two invocations were stored in two separate files. "
  174. echo "And we can now finally check that they match."
  175. echo "$ticks"
  176. echo "cat alloc_jsontx.json | jq .stateRoot && cat alloc_rlptx.json | jq .stateRoot"
  177. cat alloc_jsontx.json | jq .stateRoot && cat alloc_rlptx.json | jq .stateRoot
  178. echo "$ticks"