forked from cerc-io/plugeth
Merge commit 'da6cdaf63' into merge/geth-v1.13.6
This commit is contained in:
+15
-3
@@ -164,6 +164,7 @@ type TraceCallConfig struct {
|
||||
TraceConfig
|
||||
StateOverrides *ethapi.StateOverride
|
||||
BlockOverrides *ethapi.BlockOverrides
|
||||
TxIndex *hexutil.Uint
|
||||
}
|
||||
|
||||
// StdTraceConfig holds extra parameters to standard-json trace functions.
|
||||
@@ -863,11 +864,17 @@ func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config *
|
||||
// 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.
|
||||
// If no transaction index is specified, the trace will be conducted on the state
|
||||
// after executing the specified block. However, if a transaction index is provided,
|
||||
// the trace will be conducted on the state after executing the specified transaction
|
||||
// within the specified block.
|
||||
func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, config *TraceCallConfig) (interface{}, error) {
|
||||
// Try to retrieve the specified block
|
||||
var (
|
||||
err error
|
||||
block *types.Block
|
||||
err error
|
||||
block *types.Block
|
||||
statedb *state.StateDB
|
||||
release StateReleaseFunc
|
||||
)
|
||||
if hash, ok := blockNrOrHash.Hash(); ok {
|
||||
block, err = api.blockByHash(ctx, hash)
|
||||
@@ -892,7 +899,12 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc
|
||||
if config != nil && config.Reexec != nil {
|
||||
reexec = *config.Reexec
|
||||
}
|
||||
statedb, release, err := api.backend.StateAtBlock(ctx, block, reexec, nil, true, false)
|
||||
|
||||
if config != nil && config.TxIndex != nil {
|
||||
_, _, statedb, release, err = api.backend.StateAtTransaction(ctx, block, int(*config.TxIndex), reexec)
|
||||
} else {
|
||||
statedb, release, err = api.backend.StateAtBlock(ctx, block, reexec, nil, true, false)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
+110
-6
@@ -200,13 +200,51 @@ func TestTraceCall(t *testing.T) {
|
||||
}
|
||||
genBlocks := 10
|
||||
signer := types.HomesteadSigner{}
|
||||
nonce := uint64(0)
|
||||
backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) {
|
||||
// Transfer from account[0] to account[1]
|
||||
// value: 1000 wei
|
||||
// fee: 0 wei
|
||||
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
|
||||
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
|
||||
Nonce: nonce,
|
||||
To: &accounts[1].addr,
|
||||
Value: big.NewInt(1000),
|
||||
Gas: params.TxGas,
|
||||
GasPrice: b.BaseFee(),
|
||||
Data: nil}),
|
||||
signer, accounts[0].key)
|
||||
b.AddTx(tx)
|
||||
nonce++
|
||||
|
||||
if i == genBlocks-2 {
|
||||
// Transfer from account[0] to account[2]
|
||||
tx, _ = types.SignTx(types.NewTx(&types.LegacyTx{
|
||||
Nonce: nonce,
|
||||
To: &accounts[2].addr,
|
||||
Value: big.NewInt(1000),
|
||||
Gas: params.TxGas,
|
||||
GasPrice: b.BaseFee(),
|
||||
Data: nil}),
|
||||
signer, accounts[0].key)
|
||||
b.AddTx(tx)
|
||||
nonce++
|
||||
|
||||
// Transfer from account[0] to account[1] again
|
||||
tx, _ = types.SignTx(types.NewTx(&types.LegacyTx{
|
||||
Nonce: nonce,
|
||||
To: &accounts[1].addr,
|
||||
Value: big.NewInt(1000),
|
||||
Gas: params.TxGas,
|
||||
GasPrice: b.BaseFee(),
|
||||
Data: nil}),
|
||||
signer, accounts[0].key)
|
||||
b.AddTx(tx)
|
||||
nonce++
|
||||
}
|
||||
})
|
||||
|
||||
uintPtr := func(i int) *hexutil.Uint { x := hexutil.Uint(i); return &x }
|
||||
|
||||
defer backend.teardown()
|
||||
api := NewAPI(backend)
|
||||
var testSuite = []struct {
|
||||
@@ -240,6 +278,51 @@ func TestTraceCall(t *testing.T) {
|
||||
expectErr: nil,
|
||||
expect: `{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}`,
|
||||
},
|
||||
// Upon the last state, default to the post block's state
|
||||
{
|
||||
blockNumber: rpc.BlockNumber(genBlocks - 1),
|
||||
call: ethapi.TransactionArgs{
|
||||
From: &accounts[2].addr,
|
||||
To: &accounts[0].addr,
|
||||
Value: (*hexutil.Big)(new(big.Int).Add(big.NewInt(params.Ether), big.NewInt(100))),
|
||||
},
|
||||
config: nil,
|
||||
expect: `{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}`,
|
||||
},
|
||||
// Before the first transaction, should be failed
|
||||
{
|
||||
blockNumber: rpc.BlockNumber(genBlocks - 1),
|
||||
call: ethapi.TransactionArgs{
|
||||
From: &accounts[2].addr,
|
||||
To: &accounts[0].addr,
|
||||
Value: (*hexutil.Big)(new(big.Int).Add(big.NewInt(params.Ether), big.NewInt(100))),
|
||||
},
|
||||
config: &TraceCallConfig{TxIndex: uintPtr(0)},
|
||||
expectErr: fmt.Errorf("tracing failed: insufficient funds for gas * price + value: address %s have 1000000000000000000 want 1000000000000000100", accounts[2].addr),
|
||||
},
|
||||
// Before the target transaction, should be failed
|
||||
{
|
||||
blockNumber: rpc.BlockNumber(genBlocks - 1),
|
||||
call: ethapi.TransactionArgs{
|
||||
From: &accounts[2].addr,
|
||||
To: &accounts[0].addr,
|
||||
Value: (*hexutil.Big)(new(big.Int).Add(big.NewInt(params.Ether), big.NewInt(100))),
|
||||
},
|
||||
config: &TraceCallConfig{TxIndex: uintPtr(1)},
|
||||
expectErr: fmt.Errorf("tracing failed: insufficient funds for gas * price + value: address %s have 1000000000000000000 want 1000000000000000100", accounts[2].addr),
|
||||
},
|
||||
// After the target transaction, should be succeed
|
||||
{
|
||||
blockNumber: rpc.BlockNumber(genBlocks - 1),
|
||||
call: ethapi.TransactionArgs{
|
||||
From: &accounts[2].addr,
|
||||
To: &accounts[0].addr,
|
||||
Value: (*hexutil.Big)(new(big.Int).Add(big.NewInt(params.Ether), big.NewInt(100))),
|
||||
},
|
||||
config: &TraceCallConfig{TxIndex: uintPtr(2)},
|
||||
expectErr: nil,
|
||||
expect: `{"gas":21000,"failed":false,"returnValue":"","structLogs":[]}`,
|
||||
},
|
||||
// Standard JSON trace upon the non-existent block, error expects
|
||||
{
|
||||
blockNumber: rpc.BlockNumber(genBlocks + 1),
|
||||
@@ -297,8 +380,8 @@ func TestTraceCall(t *testing.T) {
|
||||
t.Errorf("test %d: expect error %v, got nothing", i, testspec.expectErr)
|
||||
continue
|
||||
}
|
||||
if !reflect.DeepEqual(err, testspec.expectErr) {
|
||||
t.Errorf("test %d: error mismatch, want %v, git %v", i, testspec.expectErr, err)
|
||||
if !reflect.DeepEqual(err.Error(), testspec.expectErr.Error()) {
|
||||
t.Errorf("test %d: error mismatch, want '%v', got '%v'", i, testspec.expectErr, err)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
@@ -338,7 +421,14 @@ func TestTraceTransaction(t *testing.T) {
|
||||
// Transfer from account[0] to account[1]
|
||||
// value: 1000 wei
|
||||
// fee: 0 wei
|
||||
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
|
||||
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
|
||||
Nonce: uint64(i),
|
||||
To: &accounts[1].addr,
|
||||
Value: big.NewInt(1000),
|
||||
Gas: params.TxGas,
|
||||
GasPrice: b.BaseFee(),
|
||||
Data: nil}),
|
||||
signer, accounts[0].key)
|
||||
b.AddTx(tx)
|
||||
target = tx.Hash()
|
||||
})
|
||||
@@ -388,7 +478,14 @@ func TestTraceBlock(t *testing.T) {
|
||||
// Transfer from account[0] to account[1]
|
||||
// value: 1000 wei
|
||||
// fee: 0 wei
|
||||
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
|
||||
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
|
||||
Nonce: uint64(i),
|
||||
To: &accounts[1].addr,
|
||||
Value: big.NewInt(1000),
|
||||
Gas: params.TxGas,
|
||||
GasPrice: b.BaseFee(),
|
||||
Data: nil}),
|
||||
signer, accounts[0].key)
|
||||
b.AddTx(tx)
|
||||
txHash = tx.Hash()
|
||||
})
|
||||
@@ -478,7 +575,14 @@ func TestTracingWithOverrides(t *testing.T) {
|
||||
// Transfer from account[0] to account[1]
|
||||
// value: 1000 wei
|
||||
// fee: 0 wei
|
||||
tx, _ := types.SignTx(types.NewTransaction(uint64(i), accounts[1].addr, big.NewInt(1000), params.TxGas, b.BaseFee(), nil), signer, accounts[0].key)
|
||||
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
|
||||
Nonce: uint64(i),
|
||||
To: &accounts[1].addr,
|
||||
Value: big.NewInt(1000),
|
||||
Gas: params.TxGas,
|
||||
GasPrice: b.BaseFee(),
|
||||
Data: nil}),
|
||||
signer, accounts[0].key)
|
||||
b.AddTx(tx)
|
||||
})
|
||||
defer backend.chain.Stop()
|
||||
|
||||
+46
-12
@@ -142,19 +142,29 @@ func newJsTracer(code string, ctx *tracers.Context, cfg json.RawMessage) (tracer
|
||||
vm: vm,
|
||||
ctx: make(map[string]goja.Value),
|
||||
}
|
||||
|
||||
t.setTypeConverters()
|
||||
t.setBuiltinFunctions()
|
||||
|
||||
if ctx == nil {
|
||||
ctx = new(tracers.Context)
|
||||
}
|
||||
if ctx.BlockHash != (common.Hash{}) {
|
||||
t.ctx["blockHash"] = vm.ToValue(ctx.BlockHash.Bytes())
|
||||
blockHash, err := t.toBuf(vm, ctx.BlockHash.Bytes())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t.ctx["blockHash"] = blockHash
|
||||
if ctx.TxHash != (common.Hash{}) {
|
||||
t.ctx["txIndex"] = vm.ToValue(ctx.TxIndex)
|
||||
t.ctx["txHash"] = vm.ToValue(ctx.TxHash.Bytes())
|
||||
txHash, err := t.toBuf(vm, ctx.TxHash.Bytes())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t.ctx["txHash"] = txHash
|
||||
}
|
||||
}
|
||||
|
||||
t.setTypeConverters()
|
||||
t.setBuiltinFunctions()
|
||||
ret, err := vm.RunString("(" + code + ")")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -224,6 +234,10 @@ func (t *jsTracer) CaptureTxEnd(restGas uint64) {
|
||||
|
||||
// CaptureStart implements the Tracer interface to initialize the tracing operation.
|
||||
func (t *jsTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Address, create bool, input []byte, gas uint64, value *big.Int) {
|
||||
cancel := func(err error) {
|
||||
t.err = err
|
||||
t.env.Cancel()
|
||||
}
|
||||
t.env = env
|
||||
db := &dbObj{db: env.StateDB, vm: t.vm, toBig: t.toBig, toBuf: t.toBuf, fromBuf: t.fromBuf}
|
||||
t.dbValue = db.setupObject()
|
||||
@@ -232,19 +246,34 @@ func (t *jsTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Addr
|
||||
} else {
|
||||
t.ctx["type"] = t.vm.ToValue("CALL")
|
||||
}
|
||||
t.ctx["from"] = t.vm.ToValue(from.Bytes())
|
||||
t.ctx["to"] = t.vm.ToValue(to.Bytes())
|
||||
t.ctx["input"] = t.vm.ToValue(input)
|
||||
fromVal, err := t.toBuf(t.vm, from.Bytes())
|
||||
if err != nil {
|
||||
cancel(err)
|
||||
return
|
||||
}
|
||||
t.ctx["from"] = fromVal
|
||||
toVal, err := t.toBuf(t.vm, to.Bytes())
|
||||
if err != nil {
|
||||
cancel(err)
|
||||
return
|
||||
}
|
||||
t.ctx["to"] = toVal
|
||||
inputVal, err := t.toBuf(t.vm, input)
|
||||
if err != nil {
|
||||
cancel(err)
|
||||
return
|
||||
}
|
||||
t.ctx["input"] = inputVal
|
||||
t.ctx["gas"] = t.vm.ToValue(t.gasLimit)
|
||||
gasPriceBig, err := t.toBig(t.vm, env.TxContext.GasPrice.String())
|
||||
if err != nil {
|
||||
t.err = err
|
||||
cancel(err)
|
||||
return
|
||||
}
|
||||
t.ctx["gasPrice"] = gasPriceBig
|
||||
valueBig, err := t.toBig(t.vm, value.String())
|
||||
if err != nil {
|
||||
t.err = err
|
||||
cancel(err)
|
||||
return
|
||||
}
|
||||
t.ctx["value"] = valueBig
|
||||
@@ -293,10 +322,15 @@ func (t *jsTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope
|
||||
|
||||
// CaptureEnd is called after the call finishes to finalize the tracing.
|
||||
func (t *jsTracer) CaptureEnd(output []byte, gasUsed uint64, err error) {
|
||||
t.ctx["output"] = t.vm.ToValue(output)
|
||||
if err != nil {
|
||||
t.ctx["error"] = t.vm.ToValue(err.Error())
|
||||
}
|
||||
outputVal, err := t.toBuf(t.vm, output)
|
||||
if err != nil {
|
||||
t.err = err
|
||||
return
|
||||
}
|
||||
t.ctx["output"] = outputVal
|
||||
}
|
||||
|
||||
// CaptureEnter is called when EVM enters a new scope (via call, create or selfdestruct).
|
||||
@@ -465,13 +499,13 @@ func (t *jsTracer) setBuiltinFunctions() {
|
||||
}
|
||||
return false
|
||||
})
|
||||
vm.Set("slice", func(slice goja.Value, start, end int) goja.Value {
|
||||
vm.Set("slice", func(slice goja.Value, start, end int64) goja.Value {
|
||||
b, err := t.fromBuf(vm, slice, false)
|
||||
if err != nil {
|
||||
vm.Interrupt(err)
|
||||
return nil
|
||||
}
|
||||
if start < 0 || start > end || end > len(b) {
|
||||
if start < 0 || start > end || end > int64(len(b)) {
|
||||
vm.Interrupt(fmt.Sprintf("Tracer accessed out of bound memory: available %d, offset %d, size %d", len(b), start, end-start))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ func (s StructLog) MarshalJSON() ([]byte, error) {
|
||||
GasCost math.HexOrDecimal64 `json:"gasCost"`
|
||||
Memory hexutil.Bytes `json:"memory,omitempty"`
|
||||
MemorySize int `json:"memSize"`
|
||||
Stack []uint256.Int `json:"stack"`
|
||||
Stack []hexutil.U256 `json:"stack"`
|
||||
ReturnData hexutil.Bytes `json:"returnData,omitempty"`
|
||||
Storage map[common.Hash]common.Hash `json:"-"`
|
||||
Depth int `json:"depth"`
|
||||
@@ -39,7 +39,12 @@ func (s StructLog) MarshalJSON() ([]byte, error) {
|
||||
enc.GasCost = math.HexOrDecimal64(s.GasCost)
|
||||
enc.Memory = s.Memory
|
||||
enc.MemorySize = s.MemorySize
|
||||
enc.Stack = s.Stack
|
||||
if s.Stack != nil {
|
||||
enc.Stack = make([]hexutil.U256, len(s.Stack))
|
||||
for k, v := range s.Stack {
|
||||
enc.Stack[k] = hexutil.U256(v)
|
||||
}
|
||||
}
|
||||
enc.ReturnData = s.ReturnData
|
||||
enc.Storage = s.Storage
|
||||
enc.Depth = s.Depth
|
||||
@@ -59,7 +64,7 @@ func (s *StructLog) UnmarshalJSON(input []byte) error {
|
||||
GasCost *math.HexOrDecimal64 `json:"gasCost"`
|
||||
Memory *hexutil.Bytes `json:"memory,omitempty"`
|
||||
MemorySize *int `json:"memSize"`
|
||||
Stack []uint256.Int `json:"stack"`
|
||||
Stack []hexutil.U256 `json:"stack"`
|
||||
ReturnData *hexutil.Bytes `json:"returnData,omitempty"`
|
||||
Storage map[common.Hash]common.Hash `json:"-"`
|
||||
Depth *int `json:"depth"`
|
||||
@@ -89,7 +94,10 @@ func (s *StructLog) UnmarshalJSON(input []byte) error {
|
||||
s.MemorySize = *dec.MemorySize
|
||||
}
|
||||
if dec.Stack != nil {
|
||||
s.Stack = dec.Stack
|
||||
s.Stack = make([]uint256.Int, len(dec.Stack))
|
||||
for k, v := range dec.Stack {
|
||||
s.Stack[k] = uint256.Int(v)
|
||||
}
|
||||
}
|
||||
if dec.ReturnData != nil {
|
||||
s.ReturnData = *dec.ReturnData
|
||||
|
||||
@@ -83,6 +83,7 @@ type structLogMarshaling struct {
|
||||
GasCost math.HexOrDecimal64
|
||||
Memory hexutil.Bytes
|
||||
ReturnData hexutil.Bytes
|
||||
Stack []hexutil.U256
|
||||
OpName string `json:"opName"` // adds call to OpName() in MarshalJSON
|
||||
ErrorString string `json:"error,omitempty"` // adds call to ErrorString() in MarshalJSON
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user