Skip to content

Commit

Permalink
restore decompile
Browse files Browse the repository at this point in the history
  • Loading branch information
arnetheduck committed Jun 21, 2024
1 parent caa0044 commit e141783
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
7 changes: 4 additions & 3 deletions examples/decompile_smart_contract.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Nimbus
# Copyright (c) 2018-2023 Status Research & Development GmbH
# Copyright (c) 2018-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http://www.apache.org/licenses/LICENSE-2.0)
Expand All @@ -8,9 +8,10 @@
# at your option. This file may not be copied, modified, or distributed except
# according to those terms.

import ../nimbus/vm/code_bytes, strformat
import ../nimbus/evm/code_stream, strformat

var c = CodeBytes.fromHex("0x6003600202600055").expect("valid code")
var c =
CodeStream.init(CodeBytesRef.fromHex("0x6003600202600055").expect("valid code"))

let opcodes = c.decompile()
for op in opcodes:
Expand Down
2 changes: 1 addition & 1 deletion nimbus/evm/code_bytes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init*(T: type CodeBytesRef, bytes: openArray[byte]): CodeBytesRef =
func init*(T: type CodeBytesRef, bytes: openArray[char]): CodeBytesRef =
CodeBytesRef.init(bytes.toOpenArrayByte(0, bytes.high()))

func fromHex*(T: type CodeBytesRef, hex: openArray[char]): Opt[CodeBytesRef] =
func fromHex*(T: type CodeBytesRef, hex: string): Opt[CodeBytesRef] =
try:
Opt.some(CodeBytesRef.init(hexToSeqByte(hex)))
except ValueError:
Expand Down
22 changes: 21 additions & 1 deletion nimbus/evm/code_stream.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except according to those terms.

import chronicles, eth/common, ./interpreter/op_codes, ./code_bytes
import
std/[sequtils, strutils], chronicles, eth/common, ./interpreter/op_codes, ./code_bytes

export code_bytes

Expand Down Expand Up @@ -79,5 +80,24 @@ func isValidOpcode*(c: CodeStream, position: int): bool =
func bytes*(c: CodeStream): lent seq[byte] =
c.code.bytes()

proc decompile*(original: CodeStream): seq[(int, Op, string)] =
# behave as https://etherscan.io/opcode-tool
var c = CodeStream.init(original.bytes)
while true:
var op = c.next
if op >= Push1 and op <= Push32:
result.add(
(
c.pc - 1,
op,
"0x" & c.read(op.int - 95).mapIt($(it.BiggestInt.toHex(2))).join(""),
)
)
elif op != Op.Stop:
result.add((c.pc - 1, op, ""))
else:
result.add((-1, Op.Stop, ""))
break

func atEnd*(c: CodeStream): bool =
c.pc >= c.code.bytes.len
2 changes: 1 addition & 1 deletion vendor/nim-evmc

0 comments on commit e141783

Please sign in to comment.