Skip to content

Commit 2a4e5b8

Browse files
committed
Small refactor - overflow in state UI
1 parent 6042daa commit 2a4e5b8

File tree

7 files changed

+217
-145
lines changed

7 files changed

+217
-145
lines changed

src/main/java/net/nandgr/debugger/Main.java

Lines changed: 10 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,14 @@
11
package net.nandgr.debugger;
22

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.ContractObject;
7-
import net.nandgr.debugger.cfg.beans.BytecodeChunk;
8-
import net.nandgr.debugger.cfg.beans.ContractBytecode;
9-
import net.nandgr.debugger.cfg.beans.OpcodeSource;
10-
import net.nandgr.debugger.cfg.graphviz.GraphVizCreator;
11-
import net.nandgr.debugger.disassembler.DisassemblerException;
12-
import net.nandgr.debugger.disassembler.LinkedDisassembler;
13-
import net.nandgr.debugger.node.response.json.GetCodeResponse;
143
import net.nandgr.debugger.report.Report;
154
import net.nandgr.debugger.report.ReportException;
165
import net.nandgr.debugger.solc.Solc;
17-
import net.nandgr.debugger.solc.solcjson.Code;
18-
import net.nandgr.debugger.solc.solcjson.Contract;
19-
import net.nandgr.debugger.solc.solcjson.SolcOutput;
20-
import net.nandgr.debugger.node.NodeService;
21-
import net.nandgr.debugger.node.response.json.DebugTraceTransactionLog;
22-
6+
import net.nandgr.debugger.transformers.ContractObject;
7+
import net.nandgr.debugger.transformers.SolidityTransformer;
8+
import net.nandgr.debugger.transformers.TransformException;
9+
import net.nandgr.debugger.transformers.Transformer;
2310
import java.io.File;
24-
import java.io.IOException;
25-
import java.nio.file.Files;
26-
import java.nio.file.Path;
27-
import java.nio.file.Paths;
28-
import java.util.ArrayList;
2911
import java.util.List;
30-
import java.util.Map;
3112

3213
public class Main {
3314

@@ -43,124 +24,24 @@ public static void main(String[] args){
4324
System.exit(0);
4425
}
4526

46-
String solidityFile = args[0];
27+
String sourceCodeFile = args[0];
4728
String nodeUrl = args[1];
4829
String txHash = args[2];
4930

50-
ObjectMapper objectMapper = new ObjectMapper();
51-
5231
if (!Solc.checkSolcInClasspath()) {
5332
System.out.println("solc was not found in classpath");
5433
System.exit(0);
5534
}
5635

57-
NodeService nodeService = new NodeService(nodeUrl);
58-
59-
Map<String, Map<Integer, DebugTraceTransactionLog>> traceDataResponse = null;
60-
61-
try {
62-
nodeService.populateTraceDataResponse(txHash);
63-
traceDataResponse = nodeService.getAddressTrace();
64-
} catch (IOException e) {
65-
e.printStackTrace();
66-
System.exit(0);
67-
}
68-
69-
Path path = Paths.get(solidityFile);
70-
String fileName = path.getFileName().toString();
71-
String contractName = fileName.substring(0, fileName.lastIndexOf("."));
72-
73-
Solc solc = new Solc(solidityFile);
74-
SolcOutput solcOutput = null;
36+
// for now only solidity is supported
37+
Transformer solidityTransformer = new SolidityTransformer(nodeUrl, txHash);
38+
List<ContractObject> contracts = null;
7539
try {
76-
solcOutput = solc.compile();
77-
} catch (IOException | InterruptedException e) {
78-
System.out.println("Failed when compiling source");
40+
contracts = solidityTransformer.loadContracts(sourceCodeFile);
41+
} catch (TransformException e) {
7942
e.printStackTrace();
8043
System.exit(0);
8144
}
82-
Map<String, Contract> solcContracts = solcOutput.getContracts();
83-
84-
List<ContractObject> contracts = new ArrayList<>();
85-
for (Map.Entry<String, Map<Integer, DebugTraceTransactionLog>> stringMapEntry : traceDataResponse.entrySet()) {
86-
String contractAddress = stringMapEntry.getKey();
87-
Map<Integer, DebugTraceTransactionLog> contractTrace = stringMapEntry.getValue();
88-
String contractPath = "";
89-
String cName = "";
90-
if (contractAddress.equals(NodeService.EMPTY_ADDRESS)) {
91-
contractPath = solidityFile;
92-
cName = contractName;
93-
} else {
94-
GetCodeResponse contractCodeFromChain = null;
95-
try {
96-
contractCodeFromChain = nodeService.getContractCode(contractAddress);
97-
for (Map.Entry<String, Contract> stringContractEntry : solcContracts.entrySet()) {
98-
String asmRuntime = LinkedDisassembler.cleanData(stringContractEntry.getValue().getBinRuntime())[0];
99-
String chainRuntime = LinkedDisassembler.cleanData(contractCodeFromChain.getResult())[0];
100-
if (asmRuntime.equals(chainRuntime)) {
101-
System.out.println(contractAddress);
102-
System.out.println(stringContractEntry.getKey());
103-
String key = stringContractEntry.getKey();
104-
String[] split = key.split(":");
105-
contractPath = split[0];
106-
cName = split[1];
107-
break;
108-
}
109-
}
110-
} catch (IOException e) {
111-
e.printStackTrace();
112-
}
113-
114-
}
115-
116-
Contract contract = solcContracts.get(contractPath + ":" + cName);
117-
List<Code> asmCode = contract.getAsm().getData().get("0").getCode();
118-
String code = contract.getBinRuntime();
119-
120-
121-
LinkedDisassembler disassembler = new LinkedDisassembler(code);
122-
List<OpcodeSource> opcodeSources = null;
123-
try {
124-
opcodeSources = disassembler.getOpcodeSources(asmCode);
125-
} catch (DisassemblerException e) {
126-
System.out.println("Failed when disassembling: " + contractAddress + " - " + contractPath);
127-
e.printStackTrace();
128-
System.exit(0);
129-
}
130-
path = Paths.get(contractPath);
131-
Map<Integer, DebugTraceTransactionLog> traceData = traceDataResponse.get(contractAddress);
132-
String sourceCode = null;
133-
try {
134-
sourceCode = new String(Files.readAllBytes(path));
135-
} catch (IOException e) {
136-
System.out.println("Failed when reading source code from source file");
137-
e.printStackTrace();
138-
System.exit(0);
139-
}
140-
141-
String traceMapJson = null;
142-
try {
143-
traceMapJson = objectMapper.writeValueAsString(traceData);
144-
} catch (JsonProcessingException e) {
145-
System.out.println("Failed when mapping trace data");
146-
e.printStackTrace();
147-
System.exit(0);
148-
}
149-
150-
CFGCreatorDefault cfgCreatorDefault = new CFGCreatorDefault();
151-
152-
ContractBytecode contractBytecode = cfgCreatorDefault.createContractBytecode(opcodeSources);
153-
Map<Integer, BytecodeChunk> runtimeChunks = contractBytecode.getRuntime().getChunks();
154-
155-
GraphVizCreator graphVizCreator = new GraphVizCreator(runtimeChunks, traceData, cName);
156-
String graph = graphVizCreator.buildStringGraph();
157-
158-
ContractObject contractObject = new ContractObject(cName, contractPath, traceData, contractAddress, code,
159-
traceMapJson, opcodeSources, sourceCode, graph);
160-
contracts.add(contractObject);
161-
}
162-
163-
16445

16546
Report report = new Report(contracts, txHash);
16647
String reportName = null;

src/main/java/net/nandgr/debugger/report/Report.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package net.nandgr.debugger.report;
22

3-
import net.nandgr.debugger.cfg.ContractObject;
3+
import net.nandgr.debugger.transformers.ContractObject;
44
import org.apache.velocity.Template;
55
import org.apache.velocity.VelocityContext;
66
import org.apache.velocity.app.VelocityEngine;

src/main/java/net/nandgr/debugger/cfg/ContractObject.java renamed to src/main/java/net/nandgr/debugger/transformers/ContractObject.java

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.nandgr.debugger.cfg;
1+
package net.nandgr.debugger.transformers;
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -9,14 +9,14 @@
99

1010
public class ContractObject {
1111

12-
private final String contractName;
13-
private final String filePath;
14-
private final Map<Integer, DebugTraceTransactionLog> trace;
15-
private final String traceMapJson;
16-
private final String address;
17-
private final List<OpcodeSource> opcodes;
18-
private final String sourceCode;
19-
private final String graph;
12+
private String contractName;
13+
private String filePath;
14+
private Map<Integer, DebugTraceTransactionLog> trace;
15+
private String traceMapJson;
16+
private String address;
17+
private List<OpcodeSource> opcodes;
18+
private String sourceCode;
19+
private String graph;
2020

2121
public ContractObject(String contractName, String filePath, Map<Integer, DebugTraceTransactionLog> trace, String address, String bytecode, String traceMapJson, List<OpcodeSource> opcodes, String sourceCode, String graph) {
2222
this.contractName = contractName;
@@ -29,38 +29,73 @@ public ContractObject(String contractName, String filePath, Map<Integer, DebugTr
2929
this.graph = graph;
3030
}
3131

32+
public ContractObject() {
33+
}
34+
3235
public String getContractName() {
3336
return contractName;
3437
}
3538

39+
public void setContractName(String contractName) {
40+
this.contractName = contractName;
41+
}
42+
3643
public String getFilePath() {
3744
return filePath;
3845
}
3946

47+
public void setFilePath(String filePath) {
48+
this.filePath = filePath;
49+
}
50+
4051
public Map<Integer, DebugTraceTransactionLog> getTrace() {
4152
return trace;
4253
}
4354

55+
public void setTrace(Map<Integer, DebugTraceTransactionLog> trace) {
56+
this.trace = trace;
57+
}
58+
4459
public String getTraceMapJson() {
4560
return traceMapJson;
4661
}
4762

63+
public void setTraceMapJson(String traceMapJson) {
64+
this.traceMapJson = traceMapJson;
65+
}
66+
4867
public String getAddress() {
4968
return address;
5069
}
5170

52-
public String getSourceCode() {
53-
return sourceCode;
71+
public void setAddress(String address) {
72+
this.address = address;
5473
}
5574

5675
public List<OpcodeSource> getOpcodes() {
5776
return opcodes;
5877
}
5978

79+
public void setOpcodes(List<OpcodeSource> opcodes) {
80+
this.opcodes = opcodes;
81+
}
82+
83+
public String getSourceCode() {
84+
return sourceCode;
85+
}
86+
87+
public void setSourceCode(String sourceCode) {
88+
this.sourceCode = sourceCode;
89+
}
90+
6091
public String getGraph() {
6192
return graph;
6293
}
6394

95+
public void setGraph(String graph) {
96+
this.graph = graph;
97+
}
98+
6499
@Override
65100
public String toString() {
66101
return "ContractObject{" +

0 commit comments

Comments
 (0)