|
| 1 | +package net.nandgr.debugger.transformers; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import net.nandgr.debugger.cfg.CFGCreatorDefault; |
| 6 | +import net.nandgr.debugger.cfg.beans.BytecodeChunk; |
| 7 | +import net.nandgr.debugger.cfg.beans.ContractBytecode; |
| 8 | +import net.nandgr.debugger.cfg.beans.OpcodeSource; |
| 9 | +import net.nandgr.debugger.cfg.graphviz.GraphVizCreator; |
| 10 | +import net.nandgr.debugger.disassembler.DisassemblerException; |
| 11 | +import net.nandgr.debugger.disassembler.LinkedDisassembler; |
| 12 | +import net.nandgr.debugger.node.NodeService; |
| 13 | +import net.nandgr.debugger.node.response.json.DebugTraceTransactionLog; |
| 14 | +import net.nandgr.debugger.node.response.json.GetCodeResponse; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +public class ByteCodeTransformer implements Transformer { |
| 23 | + |
| 24 | + private final String nodeUrl; |
| 25 | + private final String txHash; |
| 26 | + private final String address; |
| 27 | + |
| 28 | + public ByteCodeTransformer(String nodeUrl, String txHash, String address) { |
| 29 | + this.nodeUrl = nodeUrl; |
| 30 | + this.txHash = txHash; |
| 31 | + this.address = address; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public List<ContractObject> loadContracts() throws TransformException { |
| 36 | + |
| 37 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 38 | + NodeService nodeService = new NodeService(nodeUrl); |
| 39 | + Map<String, Map<Integer, DebugTraceTransactionLog>> traceData = null; |
| 40 | + |
| 41 | + try { |
| 42 | + nodeService.populateTraceDataResponse(address, txHash); |
| 43 | + traceData = nodeService.getAddressTrace(); |
| 44 | + } catch (IOException e) { |
| 45 | + throw new TransformException("Failed getting debug trace for hash: " + txHash, e); |
| 46 | + } |
| 47 | + |
| 48 | + List<ContractObject> contracts = new ArrayList<>(); |
| 49 | + for (Map.Entry<String, Map<Integer, DebugTraceTransactionLog>> stringMapEntry : traceData.entrySet()) { |
| 50 | + String contractAddress = stringMapEntry.getKey(); |
| 51 | + String cName = stringMapEntry.getKey(); |
| 52 | + List<OpcodeSource> opcodeSources = null; |
| 53 | + String code; |
| 54 | + try { |
| 55 | + GetCodeResponse contractCodeFromChain = nodeService.getContractCode(contractAddress); |
| 56 | + code = contractCodeFromChain.getResult(); |
| 57 | + LinkedDisassembler disassembler = new LinkedDisassembler(code); |
| 58 | + opcodeSources = disassembler.getOpcodeSources(Collections.emptyList()); |
| 59 | + } catch (IOException e) { |
| 60 | + throw new TransformException("Failed when getting code for contract: " + contractAddress + ", txHash: " + txHash, e); |
| 61 | + } catch (DisassemblerException e) { |
| 62 | + throw new TransformException("Failed when disassembling: " + contractAddress + ", txHash: " + txHash, e); |
| 63 | + } |
| 64 | + Map<Integer, DebugTraceTransactionLog> traceDataContract = traceData.get(contractAddress); |
| 65 | + String traceMapJson = null; |
| 66 | + try { |
| 67 | + traceMapJson = objectMapper.writeValueAsString(traceDataContract); |
| 68 | + } catch (JsonProcessingException e) { |
| 69 | + throw new TransformException("Failed when mapping trace data", e); |
| 70 | + } |
| 71 | + |
| 72 | + CFGCreatorDefault cfgCreatorDefault = new CFGCreatorDefault(); |
| 73 | + |
| 74 | + ContractBytecode contractBytecode = cfgCreatorDefault.createContractBytecode(opcodeSources, traceDataContract); |
| 75 | + Map<Integer, BytecodeChunk> runtimeChunks = contractBytecode.getRuntime().getChunks(); |
| 76 | + |
| 77 | + GraphVizCreator graphVizCreator = new GraphVizCreator(runtimeChunks, traceDataContract, cName); |
| 78 | + String graph = graphVizCreator.buildStringGraph(); |
| 79 | + |
| 80 | + ContractObject contractObject = new ContractObject(cName, "", traceDataContract, contractAddress, code, |
| 81 | + traceMapJson, opcodeSources, null, graph); |
| 82 | + contracts.add(contractObject); |
| 83 | + } |
| 84 | + return contracts; |
| 85 | + } |
| 86 | +} |
0 commit comments