2019-01-15 05:06:46 +00:00
|
|
|
import
|
2019-01-23 12:59:26 +00:00
|
|
|
json, strutils, os,
|
2019-02-05 19:15:50 +00:00
|
|
|
stint, chronicles, eth/common,
|
2019-02-26 07:04:12 +00:00
|
|
|
../nimbus/transaction, ../nimbus/launcher,
|
2019-01-23 12:59:26 +00:00
|
|
|
./js_tracer, ./parser, ./downloader
|
2019-01-14 11:49:18 +00:00
|
|
|
|
|
|
|
proc fakeAlloc(n: JsonNode) =
|
|
|
|
const
|
|
|
|
chunk = repeat('0', 64)
|
|
|
|
|
|
|
|
for i in 1 ..< n.len:
|
|
|
|
if not n[i].hasKey("memory"): return
|
|
|
|
let
|
|
|
|
prevMem = n[i-1]["memory"]
|
|
|
|
currMem = n[i]["memory"]
|
2019-02-21 08:17:43 +00:00
|
|
|
prevPc = n[i-1]["pc"].getInt()
|
|
|
|
currPc = n[i]["pc"].getInt()
|
2021-06-27 04:19:43 +00:00
|
|
|
|
2019-02-21 08:17:43 +00:00
|
|
|
if currMem.len > prevMem.len and prevPc == currPc - 1:
|
2019-01-14 11:49:18 +00:00
|
|
|
let diff = currMem.len - prevMem.len
|
|
|
|
for _ in 0 ..< diff:
|
|
|
|
prevMem.add %chunk
|
|
|
|
|
|
|
|
proc updateAccount*(a, b: JsonNode) =
|
|
|
|
if b.hasKey("name"):
|
|
|
|
a["name"] = newJString(b["name"].getStr)
|
|
|
|
a["balance"] = newJString(b["balance"].getStr)
|
|
|
|
a["nonce"] = newJString(b["nonce"].getStr)
|
|
|
|
a["code"] = newJString(b["code"].getStr)
|
|
|
|
var storage = a["storage"]
|
|
|
|
for k, v in b["storage"]:
|
|
|
|
storage[k] = newJString(v.getStr)
|
|
|
|
a["storageRoot"] = newJString(b["storageRoot"].getStr)
|
|
|
|
a["codeHash"] = newJString(b["codeHash"].getStr)
|
|
|
|
|
2019-01-15 05:06:46 +00:00
|
|
|
proc copyAccount*(acc: JsonNode): JsonNode =
|
|
|
|
result = newJObject()
|
|
|
|
result["storage"] = newJObject()
|
|
|
|
updateAccount(result, acc)
|
|
|
|
|
|
|
|
proc removePostStateDup*(postState: JsonNode): JsonNode =
|
2019-01-14 11:49:18 +00:00
|
|
|
var accounts = newJObject()
|
|
|
|
for acc in postState:
|
|
|
|
let address = acc["address"].getStr
|
|
|
|
if accounts.hasKey(address):
|
|
|
|
updateAccount(accounts[address], acc)
|
|
|
|
else:
|
|
|
|
accounts[address] = copyAccount(acc)
|
2019-01-15 05:06:46 +00:00
|
|
|
accounts
|
2019-01-14 11:49:18 +00:00
|
|
|
|
|
|
|
proc processNimbusData*(nimbus: JsonNode) =
|
|
|
|
# remove duplicate accounts with same address
|
|
|
|
# and only take newest one
|
2019-01-15 05:06:46 +00:00
|
|
|
let postState = nimbus["stateDump"]["after"]
|
|
|
|
nimbus["stateDump"]["after"] = removePostStateDup(postState)
|
2019-01-14 11:49:18 +00:00
|
|
|
|
|
|
|
let txTraces = nimbus["txTraces"]
|
|
|
|
|
|
|
|
for trace in txTraces:
|
|
|
|
trace["structLogs"].fakeAlloc()
|
|
|
|
|
|
|
|
proc generatePremixData*(nimbus, geth: JsonNode) =
|
|
|
|
var premixData = %{
|
|
|
|
"nimbus": nimbus,
|
|
|
|
"geth": geth
|
|
|
|
}
|
|
|
|
|
|
|
|
var data = "var premixData = " & premixData.pretty & "\n"
|
2019-01-23 12:59:26 +00:00
|
|
|
writeFile(getFileDir("index.html") / "premixData.js", data)
|
2019-01-15 05:06:46 +00:00
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
proc hasInternalTx(tx: Transaction, blockNumber: UInt256, sender: EthAddress): bool =
|
2019-01-15 05:06:46 +00:00
|
|
|
let
|
|
|
|
number = %(blockNumber.prefixHex)
|
2021-05-04 06:27:18 +00:00
|
|
|
recipient = tx.getRecipient(sender)
|
|
|
|
code = request("eth_getCode", %[%recipient.prefixHex, number])
|
2019-01-15 05:06:46 +00:00
|
|
|
recipientHasCode = code.getStr.len > 2 # "0x"
|
|
|
|
|
2021-06-27 04:19:43 +00:00
|
|
|
if tx.contractCreation:
|
2019-01-15 05:06:46 +00:00
|
|
|
return recipientHasCode or tx.payload.len > 0
|
|
|
|
|
|
|
|
recipientHasCode
|
|
|
|
|
|
|
|
proc jsonTracer(tracer: string): JsonNode =
|
|
|
|
result = %{ "tracer": %tracer }
|
|
|
|
|
|
|
|
proc requestInternalTx(txHash, tracer: JsonNode): JsonNode =
|
|
|
|
let txTrace = request("debug_traceTransaction", %[txHash, tracer])
|
|
|
|
if txTrace.kind == JNull:
|
|
|
|
error "requested postState not available", txHash=txHash
|
|
|
|
raise newException(ValueError, "Error when retrieving transaction postState")
|
|
|
|
result = txTrace
|
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
proc requestAccount*(premix: JsonNode, blockNumber: UInt256, address: EthAddress) =
|
2019-01-15 05:06:46 +00:00
|
|
|
let
|
|
|
|
number = %(blockNumber.prefixHex)
|
|
|
|
address = address.prefixHex
|
|
|
|
proof = request("eth_getProof", %[%address, %[], number])
|
|
|
|
|
|
|
|
let account = %{
|
|
|
|
"address": %address,
|
|
|
|
"codeHash": proof["codeHash"],
|
|
|
|
"storageRoot": proof["storageHash"],
|
|
|
|
"balance": proof["balance"],
|
|
|
|
"nonce": proof["nonce"],
|
|
|
|
"code": newJString("0x"),
|
2019-01-15 08:55:39 +00:00
|
|
|
"storage": newJObject(),
|
|
|
|
"accountProof": proof["accountProof"],
|
|
|
|
"storageProof": proof["storageProof"]
|
2019-01-15 05:06:46 +00:00
|
|
|
}
|
2019-01-15 08:55:39 +00:00
|
|
|
premix.add account
|
2019-01-15 05:06:46 +00:00
|
|
|
|
|
|
|
proc padding(x: string): JsonNode =
|
|
|
|
let val = x.substr(2)
|
|
|
|
let pad = repeat('0', 64 - val.len)
|
|
|
|
result = newJString("0x" & pad & val)
|
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
proc updateAccount*(address: string, account: JsonNode, blockNumber: UInt256) =
|
2019-01-15 05:06:46 +00:00
|
|
|
let number = %(blockNumber.prefixHex)
|
|
|
|
|
|
|
|
var storage = newJArray()
|
|
|
|
for k, _ in account["storage"]:
|
|
|
|
storage.add %k
|
|
|
|
|
|
|
|
let proof = request("eth_getProof", %[%address, storage, number])
|
|
|
|
account["address"] = %address
|
|
|
|
account["codeHash"] = proof["codeHash"]
|
|
|
|
account["storageRoot"] = proof["storageHash"]
|
|
|
|
account["nonce"] = proof["nonce"]
|
|
|
|
account["balance"] = proof["balance"]
|
2019-01-15 08:55:39 +00:00
|
|
|
account["accountProof"]= proof["accountProof"]
|
|
|
|
account["storageProof"]= proof["storageProof"]
|
2019-01-15 05:06:46 +00:00
|
|
|
for x in proof["storageProof"]:
|
|
|
|
x["value"] = padding(x["value"].getStr())
|
|
|
|
account["storage"][x["key"].getStr] = x["value"]
|
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
proc requestPostState*(premix, n: JsonNode, blockNumber: UInt256) =
|
2019-01-15 09:18:27 +00:00
|
|
|
type
|
|
|
|
TxKind {.pure.} = enum
|
|
|
|
Regular
|
|
|
|
ContractCreation
|
|
|
|
ContractCall
|
|
|
|
|
2019-01-15 05:06:46 +00:00
|
|
|
let txs = n["transactions"]
|
|
|
|
if txs.len == 0: return
|
|
|
|
|
|
|
|
let tracer = jsonTracer(postStateTracer)
|
|
|
|
for t in txs:
|
2019-01-15 09:18:27 +00:00
|
|
|
var txKind = TxKind.Regular
|
2019-01-15 05:06:46 +00:00
|
|
|
let tx = parseTransaction(t)
|
2021-05-04 06:27:18 +00:00
|
|
|
let sender = tx.getSender
|
2021-06-27 04:19:43 +00:00
|
|
|
if tx.contractCreation: txKind = TxKind.ContractCreation
|
2021-05-04 06:27:18 +00:00
|
|
|
if hasInternalTx(tx, blockNumber, sender):
|
2019-01-15 05:06:46 +00:00
|
|
|
let txTrace = requestInternalTx(t["hash"], tracer)
|
|
|
|
for address, account in txTrace:
|
2019-01-15 08:55:39 +00:00
|
|
|
updateAccount(address, account, blockNumber)
|
|
|
|
premix.add account
|
2021-06-27 04:19:43 +00:00
|
|
|
if not tx.contractCreation: txKind = TxKind.ContractCall
|
|
|
|
else:
|
2021-05-04 06:27:18 +00:00
|
|
|
premix.requestAccount(blockNumber, tx.getRecipient(sender))
|
|
|
|
premix.requestAccount(blockNumber, sender)
|
2019-01-15 08:55:39 +00:00
|
|
|
|
2019-01-15 09:18:27 +00:00
|
|
|
t["txKind"] = %($txKind)
|
|
|
|
|
2019-01-15 08:55:39 +00:00
|
|
|
proc requestPostState*(thisBlock: Block): JsonNode =
|
|
|
|
let blockNumber = thisBlock.header.blockNumber
|
|
|
|
var premix = newJArray()
|
|
|
|
|
|
|
|
premix.requestPostState(thisBlock.jsonData, blockNumber)
|
|
|
|
premix.requestAccount(blockNumber, thisBlock.header.coinbase)
|
|
|
|
for uncle in thisBlock.body.uncles:
|
|
|
|
premix.requestAccount(blockNumber, uncle.coinbase)
|
|
|
|
|
|
|
|
removePostStateDup(premix)
|