2021-11-21 20:52:36 +00:00
|
|
|
package calltracer
|
|
|
|
|
|
|
|
import (
|
2021-11-21 21:47:33 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"sort"
|
2021-11-21 20:52:36 +00:00
|
|
|
|
2022-11-30 01:31:39 +00:00
|
|
|
"github.com/holiman/uint256"
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2021-11-21 21:47:33 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common/length"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2023-01-13 18:12:18 +00:00
|
|
|
|
2021-11-21 20:52:36 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
2021-11-21 21:47:33 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
2021-11-21 20:52:36 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/vm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CallTracer struct {
|
2023-01-13 18:12:18 +00:00
|
|
|
froms map[libcommon.Address]struct{}
|
|
|
|
tos map[libcommon.Address]bool // address -> isCreated
|
2021-11-21 20:52:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 18:49:29 +00:00
|
|
|
func NewCallTracer() *CallTracer {
|
2021-11-21 20:52:36 +00:00
|
|
|
return &CallTracer{
|
2023-01-13 18:12:18 +00:00
|
|
|
froms: make(map[libcommon.Address]struct{}),
|
|
|
|
tos: make(map[libcommon.Address]bool),
|
2021-11-21 20:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-18 04:36:57 +00:00
|
|
|
func (ct *CallTracer) CaptureTxStart(gasLimit uint64) {}
|
|
|
|
func (ct *CallTracer) CaptureTxEnd(restGas uint64) {}
|
|
|
|
|
2022-12-26 04:56:39 +00:00
|
|
|
// CaptureStart and CaptureEnter also capture SELFDESTRUCT opcode invocations
|
2023-01-13 18:12:18 +00:00
|
|
|
func (ct *CallTracer) captureStartOrEnter(from, to libcommon.Address, create bool, code []byte) {
|
2021-11-21 21:47:33 +00:00
|
|
|
ct.froms[from] = struct{}{}
|
2021-11-21 20:52:36 +00:00
|
|
|
|
2021-11-21 21:47:33 +00:00
|
|
|
created, ok := ct.tos[to]
|
2021-11-21 20:52:36 +00:00
|
|
|
if !ok {
|
2021-11-21 21:47:33 +00:00
|
|
|
ct.tos[to] = false
|
2021-11-21 20:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !created && create {
|
2022-09-01 18:49:29 +00:00
|
|
|
if len(code) > 0 {
|
|
|
|
ct.tos[to] = true
|
2021-11-21 20:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 16:11:31 +00:00
|
|
|
|
2023-01-16 22:28:50 +00:00
|
|
|
func (ct *CallTracer) CaptureStart(env vm.VMInterface, from libcommon.Address, to libcommon.Address, precompile bool, create bool, input []byte, gas uint64, value *uint256.Int, code []byte) {
|
2022-12-18 16:11:31 +00:00
|
|
|
ct.captureStartOrEnter(from, to, create, code)
|
|
|
|
}
|
2023-01-13 18:12:18 +00:00
|
|
|
func (ct *CallTracer) CaptureEnter(typ vm.OpCode, from libcommon.Address, to libcommon.Address, precompile bool, create bool, input []byte, gas uint64, value *uint256.Int, code []byte) {
|
2022-12-18 16:11:31 +00:00
|
|
|
ct.captureStartOrEnter(from, to, create, code)
|
|
|
|
}
|
2022-12-18 17:06:40 +00:00
|
|
|
func (ct *CallTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, rData []byte, depth int, err error) {
|
2021-11-21 20:52:36 +00:00
|
|
|
}
|
2022-12-18 17:06:40 +00:00
|
|
|
func (ct *CallTracer) CaptureFault(pc uint64, op vm.OpCode, gas, cost uint64, scope *vm.ScopeContext, depth int, err error) {
|
2021-11-21 20:52:36 +00:00
|
|
|
}
|
2022-12-23 05:43:08 +00:00
|
|
|
func (ct *CallTracer) CaptureEnd(output []byte, usedGas uint64, err error) {
|
2022-12-18 16:11:31 +00:00
|
|
|
}
|
2022-12-23 05:43:08 +00:00
|
|
|
func (ct *CallTracer) CaptureExit(output []byte, usedGas uint64, err error) {
|
2021-11-21 20:52:36 +00:00
|
|
|
}
|
2021-11-21 21:47:33 +00:00
|
|
|
|
2022-04-04 07:11:59 +00:00
|
|
|
func (ct *CallTracer) WriteToDb(tx kv.StatelessWriteTx, block *types.Block, vmConfig vm.Config) error {
|
2021-11-21 21:47:33 +00:00
|
|
|
ct.tos[block.Coinbase()] = false
|
|
|
|
for _, uncle := range block.Uncles() {
|
|
|
|
ct.tos[uncle.Coinbase] = false
|
|
|
|
}
|
|
|
|
list := make(common.Addresses, len(ct.froms)+len(ct.tos))
|
|
|
|
i := 0
|
|
|
|
for addr := range ct.froms {
|
|
|
|
copy(list[i][:], addr[:])
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
for addr := range ct.tos {
|
|
|
|
copy(list[i][:], addr[:])
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
sort.Sort(list)
|
|
|
|
// List may contain duplicates
|
|
|
|
var blockNumEnc [8]byte
|
|
|
|
binary.BigEndian.PutUint64(blockNumEnc[:], block.Number().Uint64())
|
2023-01-13 18:12:18 +00:00
|
|
|
var prev libcommon.Address
|
2021-11-21 21:47:33 +00:00
|
|
|
for j, addr := range list {
|
|
|
|
if j > 0 && prev == addr {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var v [length.Addr + 1]byte
|
|
|
|
copy(v[:], addr[:])
|
|
|
|
if _, ok := ct.froms[addr]; ok {
|
|
|
|
v[length.Addr] |= 1
|
|
|
|
}
|
|
|
|
if _, ok := ct.tos[addr]; ok {
|
|
|
|
v[length.Addr] |= 2
|
|
|
|
}
|
|
|
|
if j == 0 {
|
|
|
|
if err := tx.Append(kv.CallTraceSet, blockNumEnc[:], v[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := tx.AppendDup(kv.CallTraceSet, blockNumEnc[:], v[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
copy(prev[:], addr[:])
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|