11package 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 ;
143import net .nandgr .debugger .report .Report ;
154import net .nandgr .debugger .report .ReportException ;
165import 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 ;
2310import 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 ;
2911import java .util .List ;
30- import java .util .Map ;
3112
3213public 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 ;
0 commit comments