-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
232 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright (C) 2018 The ontology Authors | ||
* This file is part of The ontology library. | ||
* | ||
* The ontology is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The ontology is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with The ontology. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package debug | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/rpc" | ||
"github.com/ontio/ontology/core/ledger" | ||
"github.com/ontio/ontology/http/ethrpc/eth" | ||
types2 "github.com/ontio/ontology/http/ethrpc/types" | ||
"github.com/ontio/ontology/vm/evm" | ||
"github.com/ontio/ontology/vm/evm/tracers" | ||
) | ||
|
||
// DebugAPI is the collection of tracing APIs exposed over the private debugging endpoint. | ||
type DebugAPI struct { | ||
} | ||
|
||
// NewDebugAPI creates a new DebugAPI definition for the tracing methods of the Ethereum service. | ||
func NewDebugAPI() *DebugAPI { | ||
return &DebugAPI{} | ||
} | ||
|
||
// TraceConfig holds extra parameters to trace functions. | ||
type TraceConfig struct { | ||
*evm.LogConfig | ||
Tracer *string | ||
Timeout *string | ||
Reexec *uint64 | ||
} | ||
|
||
// TraceCallConfig is the config for traceCall DebugAPI. It holds one more | ||
// field to override the state for tracing. | ||
type TraceCallConfig struct { | ||
*evm.LogConfig | ||
Tracer *string | ||
Timeout *string | ||
Reexec *uint64 | ||
//StateOverrides *ethapi.StateOverride | ||
} | ||
|
||
// TraceCall lets you trace a given eth_call. It collects the structured logs | ||
// created during the execution of EVM if the given transaction was added on | ||
// top of the provided block and returns them as a JSON object. | ||
// You can provide -2 as a block number to trace on top of the pending block. | ||
func (api *DebugAPI) TraceCall(args types2.CallArgs, blockNrOrHash rpc.BlockNumberOrHash, config *TraceCallConfig) (interface{}, error) { | ||
// Execute the trace | ||
msg := args.AsMessage(eth.RPCGasCap) | ||
|
||
var traceConfig *TraceConfig | ||
if config != nil { | ||
traceConfig = &TraceConfig{ | ||
LogConfig: config.LogConfig, | ||
Tracer: config.Tracer, | ||
Timeout: config.Timeout, | ||
Reexec: config.Reexec, | ||
} | ||
} | ||
return api.traceTx(msg, traceConfig) | ||
} | ||
|
||
// traceTx configures a new tracer according to the provided configuration, and | ||
// executes the given message in the provided environment. The return value will | ||
// be tracer dependent. | ||
func (api *DebugAPI) traceTx(message types.Message, config *TraceConfig) (interface{}, error) { | ||
// Assemble the structured logger or the JavaScript tracer | ||
var ( | ||
tracer evm.Tracer | ||
err error | ||
) | ||
switch { | ||
case config == nil: | ||
tracer = evm.NewStructLogger(nil) | ||
case config.Tracer != nil: | ||
switch *config.Tracer { | ||
case "callTracer": | ||
tracer = tracers.NewCallTracer() | ||
default: | ||
return nil, fmt.Errorf("unkown tracer type: %s", *config.Tracer) | ||
} | ||
default: | ||
tracer = evm.NewStructLogger(config.LogConfig) | ||
} | ||
|
||
result, err := ledger.DefLedger.TraceEip155Tx(message, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("tracing failed: %w", err) | ||
} | ||
|
||
// Depending on the tracer type, format and return the output. | ||
switch tracer := tracer.(type) { | ||
case *evm.StructLogger: | ||
// If the result contains a revert reason, return it. | ||
returnVal := fmt.Sprintf("%x", result.Return()) | ||
if len(result.Revert()) > 0 { | ||
returnVal = fmt.Sprintf("%x", result.Revert()) | ||
} | ||
return &ExecutionResult{ | ||
Gas: result.UsedGas, | ||
Failed: result.Failed(), | ||
ReturnValue: returnVal, | ||
StructLogs: FormatLogs(tracer.StructLogs()), | ||
}, nil | ||
|
||
case *tracers.CallTracer: | ||
return tracer.GetResult() | ||
|
||
default: | ||
panic(fmt.Sprintf("bad tracer type %T", tracer)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (C) 2018 The ontology Authors | ||
* This file is part of The ontology library. | ||
* | ||
* The ontology is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The ontology is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with The ontology. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package debug | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ethereum/go-ethereum/common/math" | ||
"github.com/ontio/ontology/vm/evm" | ||
) | ||
|
||
// ExecutionResult groups all structured logs emitted by the EVM | ||
// while replaying a transaction in debug mode as well as transaction | ||
// execution status, the amount of gas used and the return value | ||
type ExecutionResult struct { | ||
Gas uint64 `json:"gas"` | ||
Failed bool `json:"failed"` | ||
ReturnValue string `json:"returnValue"` | ||
StructLogs []StructLogRes `json:"structLogs"` | ||
} | ||
|
||
// StructLogRes stores a structured log emitted by the EVM while replaying a | ||
// transaction in debug mode | ||
type StructLogRes struct { | ||
Pc uint64 `json:"pc"` | ||
Op string `json:"op"` | ||
Gas uint64 `json:"gas"` | ||
GasCost uint64 `json:"gasCost"` | ||
Depth int `json:"depth"` | ||
Error error `json:"error,omitempty"` | ||
Stack *[]string `json:"stack,omitempty"` | ||
Memory *[]string `json:"memory,omitempty"` | ||
Storage *map[string]string `json:"storage,omitempty"` | ||
} | ||
|
||
// FormatLogs formats EVM returned structured logs for json output | ||
func FormatLogs(logs []evm.StructLog) []StructLogRes { | ||
formatted := make([]StructLogRes, len(logs)) | ||
for index, trace := range logs { | ||
formatted[index] = StructLogRes{ | ||
Pc: trace.Pc, | ||
Op: trace.Op.String(), | ||
Gas: trace.Gas, | ||
GasCost: trace.GasCost, | ||
Depth: trace.Depth, | ||
Error: trace.Err, | ||
} | ||
if trace.Stack != nil { | ||
stack := make([]string, len(trace.Stack)) | ||
for i, stackValue := range trace.Stack { | ||
stack[i] = fmt.Sprintf("%x", math.PaddedBigBytes(stackValue, 32)) | ||
} | ||
formatted[index].Stack = &stack | ||
} | ||
if trace.Memory != nil { | ||
memory := make([]string, 0, (len(trace.Memory)+31)/32) | ||
for i := 0; i+32 <= len(trace.Memory); i += 32 { | ||
memory = append(memory, fmt.Sprintf("%x", trace.Memory[i:i+32])) | ||
} | ||
formatted[index].Memory = &memory | ||
} | ||
if trace.Storage != nil { | ||
storage := make(map[string]string) | ||
for i, storageValue := range trace.Storage { | ||
storage[fmt.Sprintf("%x", i)] = fmt.Sprintf("%x", storageValue) | ||
} | ||
formatted[index].Storage = &storage | ||
} | ||
} | ||
return formatted | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters