11'use strict'
22
3- const StorageViewer = require ( './storage/storageViewer' )
4- const StorageResolver = require ( './storage/storageResolver' )
5-
6- const SolidityDecoder = require ( './solidity-decoder' )
7- const SolidityProxy = SolidityDecoder . SolidityProxy
8- const stateDecoder = SolidityDecoder . stateDecoder
9- const localDecoder = SolidityDecoder . localDecoder
10- const InternalCallTree = SolidityDecoder . InternalCallTree
11-
123const remixLib = require ( '@remix-project/remix-lib' )
134const TraceManager = remixLib . trace . TraceManager
145const CodeManager = remixLib . code . CodeManager
156const traceHelper = remixLib . helpers . trace
167const EventManager = remixLib . EventManager
178
9+ const { SolidityProxy, stateDecoder, localDecoder, InternalCallTree} = require ( './solidity-decoder' )
10+
11+ const StorageViewer = require ( './storage/storageViewer' )
12+ const StorageResolver = require ( './storage/storageResolver' )
13+
1814/**
1915 * Ethdebugger is a wrapper around a few classes that helps debugging a transaction
2016 *
@@ -58,23 +54,15 @@ Ethdebugger.prototype.resolveStep = function (index) {
5854}
5955
6056Ethdebugger . prototype . setCompilationResult = function ( compilationResult ) {
61- if ( compilationResult && compilationResult . data ) {
62- this . solidityProxy . reset ( compilationResult . data )
63- } else {
64- this . solidityProxy . reset ( { } )
65- }
57+ this . solidityProxy . reset ( ( compilationResult && compilationResult . data ) || { } )
6658}
6759
68- Ethdebugger . prototype . sourceLocationFromVMTraceIndex = function ( address , stepIndex , callback ) {
69- this . callTree . sourceLocationTracker . getSourceLocationFromVMTraceIndex ( address , stepIndex , this . solidityProxy . contracts ) . then ( ( rawLocation ) => {
70- callback ( null , rawLocation )
71- } ) . catch ( callback )
60+ Ethdebugger . prototype . sourceLocationFromVMTraceIndex = async function ( address , stepIndex ) {
61+ return this . callTree . sourceLocationTracker . getSourceLocationFromVMTraceIndex ( address , stepIndex , this . solidityProxy . contracts )
7262}
7363
74- Ethdebugger . prototype . sourceLocationFromInstructionIndex = function ( address , instIndex , callback ) {
75- this . callTree . sourceLocationTracker . getSourceLocationFromInstructionIndex ( address , instIndex , this . solidityProxy . contracts ) . then ( ( rawLocation ) => {
76- callback ( null , rawLocation )
77- } ) . catch ( callback )
64+ Ethdebugger . prototype . sourceLocationFromInstructionIndex = async function ( address , instIndex , callback ) {
65+ return this . callTree . sourceLocationTracker . getSourceLocationFromInstructionIndex ( address , instIndex , this . solidityProxy . contracts )
7866}
7967
8068/* breakpoint */
@@ -83,98 +71,48 @@ Ethdebugger.prototype.setBreakpointManager = function (breakpointManager) {
8371}
8472
8573/* decode locals */
86- Ethdebugger . prototype . extractLocalsAt = function ( step , callback ) {
87- callback ( null , this . callTree . findScope ( step ) )
74+ Ethdebugger . prototype . extractLocalsAt = function ( step ) {
75+ return this . callTree . findScope ( step )
8876}
8977
90- Ethdebugger . prototype . decodeLocalsAt = function ( step , sourceLocation , callback ) {
91- const self = this
92- this . traceManager . waterfall ( [
93- function getStackAt ( stepIndex , callback ) {
94- try {
95- const result = self . traceManager . getStackAt ( stepIndex )
96- callback ( null , result )
97- } catch ( error ) {
98- callback ( error )
99- }
100- } ,
101- function getMemoryAt ( stepIndex , callback ) {
102- try {
103- const result = self . traceManager . getMemoryAt ( stepIndex )
104- callback ( null , result )
105- } catch ( error ) {
106- callback ( error )
107- }
108- } ,
109-
110- function getCurrentCalledAddressAt ( stepIndex , next ) {
111- try {
112- const address = self . traceManager . getCurrentCalledAddressAt ( stepIndex )
113- next ( null , address )
114- } catch ( error ) {
115- next ( error )
116- }
117- } ] ,
118- step ,
119- ( error , result ) => {
120- if ( ! error ) {
121- const stack = result [ 0 ] . value
122- const memory = result [ 1 ] . value
123- try {
124- const storageViewer = new StorageViewer ( {
125- stepIndex : step ,
126- tx : this . tx ,
127- address : result [ 2 ] . value
128- } , this . storageResolver , this . traceManager )
129- localDecoder . solidityLocals ( step , this . callTree , stack , memory , storageViewer , sourceLocation ) . then ( ( locals ) => {
130- if ( ! locals . error ) {
131- callback ( null , locals )
132- } else {
133- callback ( locals . error )
134- }
135- } )
136- } catch ( e ) {
137- callback ( e . message )
138- }
139- } else {
140- callback ( error )
78+ Ethdebugger . prototype . decodeLocalsAt = async function ( step , sourceLocation , callback ) {
79+ try {
80+ const stack = this . traceManager . getStackAt ( step )
81+ const memory = this . traceManager . getMemoryAt ( step )
82+ const address = this . traceManager . getCurrentCalledAddressAt ( step )
83+ try {
84+ const storageViewer = new StorageViewer ( { stepIndex : step , tx : this . tx , address : address } , this . storageResolver , this . traceManager )
85+ const locals = await localDecoder . solidityLocals ( step , this . callTree , stack , memory , storageViewer , sourceLocation )
86+ if ( locals . error ) {
87+ return callback ( locals . error )
14188 }
142- } )
89+ return callback ( null , locals )
90+ } catch ( e ) {
91+ callback ( e . message )
92+ }
93+ } catch ( error ) {
94+ callback ( error )
95+ }
14396}
14497
14598/* decode state */
146- Ethdebugger . prototype . extractStateAt = function ( step , callback ) {
147- this . solidityProxy . extractStateVariablesAt ( step ) . then ( ( stateVars ) => {
148- callback ( null , stateVars )
149- } ) . catch ( callback )
99+ Ethdebugger . prototype . extractStateAt = async function ( step ) {
100+ return this . solidityProxy . extractStateVariablesAt ( step )
150101}
151102
152- Ethdebugger . prototype . decodeStateAt = function ( step , stateVars , callback ) {
103+ Ethdebugger . prototype . decodeStateAt = async function ( step , stateVars , callback ) {
153104 try {
154105 const address = this . traceManager . getCurrentCalledAddressAt ( step )
155- const storageViewer = new StorageViewer ( {
156- stepIndex : step ,
157- tx : this . tx ,
158- address : address
159- } , this . storageResolver , this . traceManager )
160- stateDecoder . decodeState ( stateVars , storageViewer ) . then ( ( result ) => {
161- if ( ! result . error ) {
162- callback ( null , result )
163- } else {
164- callback ( result . error )
165- }
166- } )
106+ const storageViewer = new StorageViewer ( { stepIndex : step , tx : this . tx , address : address } , this . storageResolver , this . traceManager )
107+ const result = await stateDecoder . decodeState ( stateVars , storageViewer )
108+ return result
167109 } catch ( error ) {
168110 callback ( error )
169111 }
170112}
171113
172114Ethdebugger . prototype . storageViewAt = function ( step , address ) {
173- return new StorageViewer ( {
174- stepIndex : step ,
175- tx : this . tx ,
176- address : address
177- } , this . storageResolver , this . traceManager )
115+ return new StorageViewer ( { stepIndex : step , tx : this . tx , address : address } , this . storageResolver , this . traceManager )
178116}
179117
180118Ethdebugger . prototype . updateWeb3 = function ( web3 ) {
@@ -192,21 +130,18 @@ Ethdebugger.prototype.debug = function (tx) {
192130 if ( this . traceManager . isLoading ) {
193131 return
194132 }
195- if ( ! tx . to ) {
196- tx . to = traceHelper . contractCreationToken ( '0' )
197- }
133+ tx . to = tx . to || traceHelper . contractCreationToken ( '0' )
198134 this . tx = tx
199- this . traceManager . resolveTrace ( tx , async ( error , result ) => {
200- if ( result ) {
201- this . setCompilationResult ( await this . compilationResult ( tx . to ) )
202- this . event . trigger ( 'newTraceLoaded' , [ this . traceManager . trace ] )
203- if ( this . breakpointManager && this . breakpointManager . hasBreakpoint ( ) ) {
204- this . breakpointManager . jumpNextBreakpoint ( false )
205- }
206- this . storageResolver = new StorageResolver ( { web3 : this . traceManager . web3 } )
207- } else {
208- this . statusMessage = error ? error . message : 'Trace not loaded'
135+
136+ this . traceManager . resolveTrace ( tx ) . then ( async ( result ) => {
137+ this . setCompilationResult ( await this . compilationResult ( tx . to ) )
138+ this . event . trigger ( 'newTraceLoaded' , [ this . traceManager . trace ] )
139+ if ( this . breakpointManager && this . breakpointManager . hasBreakpoint ( ) ) {
140+ this . breakpointManager . jumpNextBreakpoint ( false )
209141 }
142+ this . storageResolver = new StorageResolver ( { web3 : this . traceManager . web3 } )
143+ } ) . catch ( ( error ) => {
144+ this . statusMessage = error ? error . message : 'Trace not loaded'
210145 } )
211146}
212147
0 commit comments