The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/core/vm/logger_json.go

92 lines
2.9 KiB

// Copyright 2017 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
package vm
import (
"encoding/json"
"io"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
)
// JSONLogger ...
type JSONLogger struct {
encoder *json.Encoder
cfg *LogConfig
}
// NewJSONLogger creates a new EVM tracer that prints execution steps as JSON objects
// into the provided stream.
func NewJSONLogger(cfg *LogConfig, writer io.Writer) *JSONLogger {
l := &JSONLogger{json.NewEncoder(writer), cfg}
if l.cfg == nil {
l.cfg = &LogConfig{}
}
return l
}
// CaptureStart ...
func (l *JSONLogger) CaptureStart(env *EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) error {
return nil
}
// CaptureState outputs state information on the logger.
func (l *JSONLogger) CaptureState(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) (HookAfter, error) {
log := StructLog{
[Rosetta] Track internal transactions (#3475) * [rosetta] Refactor operations & prep for internal tx exposure * Remove gas op relation for tx operations. Gas is for submission & processing the tx, thus not really related to the amount being transferred * Make optional starting op a ptr to a uint to keep consistent * Reorg file for consistency of fn placement * Rename functions for clarity * Make getContractCreationNativeOperations consume getBasicTransferOperations for consistency * Remove invariant doc as it does not apply anymore Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add framework for parsing traced txs * Define ContractInfo struct for FormatTransaction * Add tx trace helper function defs & propagate type defs * Add a GetTransactionStatus helper fn for Operation formatting/creation * Add wrapper function, getContractTransferNativeOperations, to get internal operations Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement transaction tracer Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [tracer] Add CallerAddress & CodeAddress to tracer logs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [tracer] Remove ptr to slice & map in StructLogRes Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Implement getContractInternalTransferNativeOperations * Add ContractAddress to ContractInfo for future usages (i.e: erc20 parsing) * Only check for contract address if there is tx data in BlockTransaction Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix status report for contract related txs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [tracer] Expose contract address instead of code address Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add trace cache & update TODO * Trace any PLAIN transaction instead of only transactions with data. This is to account for fall back contract fn calls & ignore staking data. Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Make internal tx formatter not return err on nil exec result Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * Fix lint Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix tests Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Add internal tx unit tests * Fix tx data len check for contract related transfers as a transaction with len 0 data can happen for a contract (and fail) due to fall back operations. Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Update invariant comment Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Expose mutually exclusive ops + update docs & tests Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu> * [rosetta] Fix docs and err msgs Signed-off-by: Daniel Van Der Maden <dvandermaden0@berkeley.edu>
4 years ago
Pc: pc,
Op: op,
ContractAddress: contract.Address(),
CallerAddress: contract.CallerAddress,
Gas: gas,
GasCost: cost,
MemorySize: memory.Len(),
Storage: nil,
Depth: depth,
RefundCounter: env.StateDB.GetRefund(),
Err: err,
}
if !l.cfg.DisableMemory {
log.Memory = memory.Data()
}
if !l.cfg.DisableStack {
log.Stack = stack.Data()
}
return nil, l.encoder.Encode(log)
}
// CaptureFault outputs state information on the logger.
func (l *JSONLogger) CaptureFault(env *EVM, pc uint64, op OpCode, gas, cost uint64, memory *Memory, stack *Stack, contract *Contract, depth int, err error) error {
return nil
}
// CaptureEnd is triggered at end of execution.
func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, t time.Duration, err error) error {
type endLog struct {
Output string `json:"output"`
GasUsed math.HexOrDecimal64 `json:"gasUsed"`
Time time.Duration `json:"time"`
Err string `json:"error,omitempty"`
}
if err != nil {
return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, err.Error()})
}
return l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), t, ""})
}