Skip to content

Commit

Permalink
Change Stack to conform with Tenderly stack interface
Browse files Browse the repository at this point in the history
  • Loading branch information
NebojsaHorvat committed Dec 13, 2023
1 parent 2f2bc35 commit ea71f7b
Show file tree
Hide file tree
Showing 31 changed files with 90 additions and 81 deletions.
2 changes: 1 addition & 1 deletion accounts/abi/bind/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1770,7 +1770,7 @@ var bindTests = []struct {
iter2, _ := c.FilterFallback(nil)
defer iter2.Close()
for iter2.Next() {
if !bytes.Equal(iter2.Event.Data, calldata) {
if !bytes.Equal(iter2.Event.StackData, calldata) {
t.Fatal("calldata mismatch")
}
gotEvent = true
Expand Down
2 changes: 1 addition & 1 deletion cl/beacon/handler/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (r *beaconResponse) withVersion(version clparams.StateVersion) (out *beacon
// }
//
// if isSSZ && supportSSZ {
// data := resp.Data
// data := resp.StackData
// // SSZ encoding
// encoded, err := data.(ssz.Marshaler).EncodeSSZ(nil)
// if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cl/cltypes/beacon_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type BeaconBlock struct {
type BeaconBody struct {
// A byte array used for randomness in the beacon chain
RandaoReveal libcommon.Bytes96 `json:"randao_reveal"`
// Data related to the Ethereum 1.0 chain
// StackData related to the Ethereum 1.0 chain
Eth1Data *Eth1Data `json:"eth1_data"`
// A byte array used to customize validators' behavior
Graffiti libcommon.Hash `json:"graffit"`
Expand All @@ -54,7 +54,7 @@ type BeaconBody struct {
VoluntaryExits *solid.ListSSZ[*SignedVoluntaryExit] `json:"voluntary_exits"`
// A summary of the current state of the beacon chain
SyncAggregate *SyncAggregate `json:"sync_aggregate,omitempty"`
// Data related to crosslink records and executing operations on the Ethereum 2.0 chain
// StackData related to crosslink records and executing operations on the Ethereum 2.0 chain
ExecutionPayload *Eth1Block `json:"execution_payload,omitempty"`
// Withdrawals Diffs for Execution Layer
ExecutionChanges *solid.ListSSZ[*SignedBLSToExecutionChange] `json:"execution_changes,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion cmd/devnet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
var (
DataDirFlag = flags.DirectoryFlag{
Name: "datadir",
Usage: "Data directory for the devnet",
Usage: "StackData directory for the devnet",
Value: flags.DirectoryString(""),
Required: true,
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/devnet/services/polygon/statesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ func (h *Heimdall) handleStateSynced(event *contracts.TestStateSenderStateSynced
)

if event.Raw.BlockNumber > h.getSpanOverrideHeight() && len(event.Data) > MaxStateSyncSize {
h.logger.Info(`Data is too large to process, Resetting to ""`, "data", hex.EncodeToString(event.Data))
h.logger.Info(`StackData is too large to process, Resetting to ""`, "data", hex.EncodeToString(event.Data))
event.Data = []byte{}
} else if len(event.Data) > LegacyMaxStateSyncSize {
h.logger.Info(`Data is too large to process, Resetting to ""`, "data", hex.EncodeToString(event.Data))
h.logger.Info(`StackData is too large to process, Resetting to ""`, "data", hex.EncodeToString(event.Data))
event.Data = []byte{}
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/state/commands/opcode_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (ot *opcodeTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64,
}

// prepare the opcode's stack for saving
//stackTop := &stack.Stack{Data: make([]uint256.Int, 0, 7)}//stack.New()
//stackTop := &stack.Stack{StackData: make([]uint256.Int, 0, 7)}//stack.New()
// the most stack positions consumed by any opcode is 7
//for i:= min(7, st.Len()-1); i>=0; i-- {
// stackTop.Push(st.Back(i))
Expand All @@ -303,8 +303,8 @@ func (ot *opcodeTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint64,
//sl := st.Len()
//minl := min(7, sl)
//startcopy := sl-minl
//stackTop := &stack.Stack{Data: make([]uint256.Int, minl, minl)}//stack.New()
//copy(stackTop.Data, st.Data[startcopy:sl])
//stackTop := &stack.Stack{StackData: make([]uint256.Int, minl, minl)}//stack.New()
//copy(stackTop.StackData, st.StackData[startcopy:sl])

//sanity check
if currentEntry.OpcodeFault != "" {
Expand Down
4 changes: 2 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ var (
// General settings
DataDirFlag = flags.DirectoryFlag{
Name: "datadir",
Usage: "Data directory for the databases",
Usage: "StackData directory for the databases",
Value: flags.DirectoryString(paths.DefaultDataDir()),
}

AncientFlag = flags.DirectoryFlag{
Name: "datadir.ancient",
Usage: "Data directory for ancient chain segments (default = inside chaindata)",
Usage: "StackData directory for ancient chain segments (default = inside chaindata)",
}
MinFreeDiskSpaceFlag = flags.DirectoryFlag{
Name: "datadir.minfreedisk",
Expand Down
4 changes: 2 additions & 2 deletions common/debugprint/receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ func Receipts(rs1, rs2 types.Receipts) {
if l2 == nil {
fmt.Printf(" Logs[%d].Address: %x\n", j, l1.Address)
fmt.Printf(" Logs[%d].Topic: %x\n", j, l1.Topics)
fmt.Printf(" Logs[%d].Data: %x\n", j, l1.Data)
fmt.Printf(" Logs[%d].StackData: %x\n", j, l1.Data)
continue
}
fmt.Printf(" Logs[%d].Address: %x, %x\n", j, l1.Address, l2.Address)
fmt.Printf(" Logs[%d].Topic: %x, %x\n", j, l1.Topics, l2.Topics)
fmt.Printf(" Logs[%d].Data: %x, %x\n", j, l1.Data, l2.Data)
fmt.Printf(" Logs[%d].StackData: %x, %x\n", j, l1.Data, l2.Data)
}
//fmt.Printf(" Bloom: %x, %x\n", r1.Bloom, r1.Bloom)
}
Expand Down
2 changes: 1 addition & 1 deletion core/state/plain_state_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
var _ StateReader = (*PlainStateReader)(nil)

// PlainStateReader reads data from so called "plain state".
// Data in the plain state is stored using un-hashed account/storage items
// StackData in the plain state is stored using un-hashed account/storage items
// as opposed to the "normal" state that uses hashes of merkle paths to store items.
type PlainStateReader struct {
db kv.Getter
Expand Down
6 changes: 3 additions & 3 deletions core/types/access_list_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (tx AccessListTx) payloadSize() (payloadSize int, nonceLen, gasLen, accessL
// size of Value
payloadSize++
payloadSize += rlp.Uint256LenExcludingHead(tx.Value)
// size of Data
// size of StackData
payloadSize += rlp2.StringLen(tx.Data)
// size of AccessList
accessListLen = accessListSize(tx.AccessList)
Expand Down Expand Up @@ -256,7 +256,7 @@ func (tx AccessListTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLe
if err := tx.Value.EncodeRLP(w); err != nil {
return err
}
// encode Data
// encode StackData
if err := rlp.EncodeString(tx.Data, w, b); err != nil {
return err
}
Expand Down Expand Up @@ -390,7 +390,7 @@ func (tx *AccessListTx) DecodeRLP(s *rlp.Stream) error {
}
tx.Value = new(uint256.Int).SetBytes(b)
if tx.Data, err = s.Bytes(); err != nil {
return fmt.Errorf("read Data: %w", err)
return fmt.Errorf("read StackData: %w", err)
}
// decode AccessList
tx.AccessList = types2.AccessList{}
Expand Down
2 changes: 1 addition & 1 deletion core/types/blob_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (stx BlobTx) encodePayload(w io.Writer, b []byte, payloadSize, nonceLen, ga
if err := stx.Value.EncodeRLP(w); err != nil {
return err
}
// encode Data
// encode StackData
if err := rlp.EncodeString(stx.Data, w, b); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions core/types/dynamic_fee_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (tx DynamicFeeTransaction) payloadSize() (payloadSize int, nonceLen, gasLen
// size of Value
payloadSize++
payloadSize += rlp.Uint256LenExcludingHead(tx.Value)
// size of Data
// size of StackData
payloadSize += rlp2.StringLen(tx.Data)
// size of AccessList
accessListLen = accessListSize(tx.AccessList)
Expand Down Expand Up @@ -245,7 +245,7 @@ func (tx DynamicFeeTransaction) encodePayload(w io.Writer, b []byte, payloadSize
if err := tx.Value.EncodeRLP(w); err != nil {
return err
}
// encode Data
// encode StackData
if err := rlp.EncodeString(tx.Data, w, b); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions core/types/legacy_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (tx LegacyTx) payloadSize() (payloadSize int, nonceLen, gasLen int) {
}
payloadSize++
payloadSize += rlp.Uint256LenExcludingHead(tx.Value)
// size of Data
// size of StackData
payloadSize += rlp2.StringLen(tx.Data)
// size of V
payloadSize++
Expand Down Expand Up @@ -328,7 +328,7 @@ func (tx *LegacyTx) DecodeRLP(s *rlp.Stream, encodingSize uint64) error {
}
tx.Value = new(uint256.Int).SetBytes(b)
if tx.Data, err = s.Bytes(); err != nil {
return fmt.Errorf("read Data: %w", err)
return fmt.Errorf("read StackData: %w", err)
}
if b, err = s.Uint256Bytes(); err != nil {
return fmt.Errorf("read V: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (r *Receipt) decodePayload(s *rlp.Stream) error {
return fmt.Errorf("close Topics: %w", err)
}
if log.Data, err = s.Bytes(); err != nil {
return fmt.Errorf("read Data: %w", err)
return fmt.Errorf("read StackData: %w", err)
}
// end of Log
if err = s.ListEnd(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion core/types/receipt_codecgen_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ func printSTX(stx *BlobTx) {
fmt.Printf("Gas: %v\n", stx.Gas)
fmt.Printf("To: %v\n", stx.To)
fmt.Printf("Value: %v\n", stx.Value)
fmt.Printf("Data: %v\n", stx.Data)
fmt.Printf("StackData: %v\n", stx.Data)
fmt.Printf("AccessList: %v\n", stx.AccessList)
fmt.Printf("MaxFeePerBlobGas: %v\n", stx.MaxFeePerBlobGas)
fmt.Printf("BlobVersionedHashes: %v\n", stx.BlobVersionedHashes)
Expand Down
4 changes: 2 additions & 2 deletions core/vm/gas_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func gasCreate2Eip3860(_ *EVM, contract *Contract, stack *stack.Stack, mem *Memo
}

func gasExpFrontier(_ *EVM, contract *Contract, stack *stack.Stack, mem *Memory, memorySize uint64) (uint64, error) {
expByteLen := uint64(libcommon.BitLenToByteLen(stack.Data[stack.Len()-2].BitLen()))
expByteLen := uint64(libcommon.BitLenToByteLen(stack.StackData[stack.Len()-2].BitLen()))

var (
gas = expByteLen * params.ExpByteFrontier // no overflow check required. Max is 256 * ExpByte gas
Expand All @@ -362,7 +362,7 @@ func gasExpFrontier(_ *EVM, contract *Contract, stack *stack.Stack, mem *Memory,
}

func gasExpEIP160(_ *EVM, contract *Contract, stack *stack.Stack, mem *Memory, memorySize uint64) (uint64, error) {
expByteLen := uint64(libcommon.BitLenToByteLen(stack.Data[stack.Len()-2].BitLen()))
expByteLen := uint64(libcommon.BitLenToByteLen(stack.StackData[stack.Len()-2].BitLen()))

var (
gas = expByteLen * params.ExpByteEIP160 // no overflow check required. Max is 256 * ExpByte gas
Expand Down
4 changes: 2 additions & 2 deletions core/vm/instructions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ func testTwoOperandOp(t *testing.T, tests []TwoOperandTestcase, opFn executionFu
stack.Push(x)
stack.Push(y)
opFn(&pc, evmInterpreter, &ScopeContext{nil, stack, nil})
if len(stack.Data) != 1 {
t.Errorf("Expected one item on stack after %v, got %d: ", name, len(stack.Data))
if len(stack.StackData) != 1 {
t.Errorf("Expected one item on stack after %v, got %d: ", name, len(stack.StackData))
}
actual := stack.Pop()

Expand Down
41 changes: 25 additions & 16 deletions core/vm/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ import (

var stackPool = sync.Pool{
New: func() interface{} {
return &Stack{Data: make([]uint256.Int, 0, 16)}
return &Stack{StackData: make([]uint256.Int, 0, 16)}
},
}

// Stack is an object for basic stack operations. Items popped to the stack are
// expected to be changed and modified. stack does not take care of adding newly
// initialised objects.
type Stack struct {
Data []uint256.Int
StackData []uint256.Int
}

func New() *Stack {
Expand All @@ -45,56 +45,65 @@ func New() *Stack {
return stack
}

// Data returns the underlying uint256.Int array.
func (st *Stack) Data() []uint256.Int {
return st.StackData
}

func (st *Stack) Push(d *uint256.Int) {
// NOTE push limit (1024) is checked in baseCheck
st.Data = append(st.Data, *d)
st.StackData = append(st.StackData, *d)
}

func (st *Stack) PushN(ds ...uint256.Int) {
// FIXME: Is there a way to pass args by pointers.
st.Data = append(st.Data, ds...)
st.StackData = append(st.StackData, ds...)
}

func (st *Stack) Pop() (ret uint256.Int) {
ret = st.Data[len(st.Data)-1]
st.Data = st.Data[:len(st.Data)-1]
ret = st.StackData[len(st.StackData)-1]
st.StackData = st.StackData[:len(st.StackData)-1]
return
}

func (st *Stack) Cap() int {
return cap(st.Data)
return cap(st.StackData)
}

func (st *Stack) Swap(n int) {
st.Data[len(st.Data)-n], st.Data[len(st.Data)-1] = st.Data[len(st.Data)-1], st.Data[len(st.Data)-n]
st.StackData[len(st.StackData)-n], st.StackData[len(st.StackData)-1] = st.StackData[len(st.StackData)-1], st.StackData[len(st.StackData)-n]
}

func (st *Stack) Dup(n int) {
st.Data = append(st.Data, st.Data[len(st.Data)-n])
st.StackData = append(st.StackData, st.StackData[len(st.StackData)-n])
}

func (st *Stack) Peek() *uint256.Int {
return &st.Data[len(st.Data)-1]
return &st.StackData[len(st.StackData)-1]
}

func (st *Stack) Peek(position int) *uint256.Int {
return &st.StackData[position]
}

// Back returns the n'th item in stack
func (st *Stack) Back(n int) *uint256.Int {
return &st.Data[len(st.Data)-n-1]
return &st.StackData[len(st.StackData)-n-1]
}

func (st *Stack) Reset() {
st.Data = st.Data[:0]
st.StackData = st.StackData[:0]
}

func (st *Stack) Len() int {
return len(st.Data)
return len(st.StackData)
}

// Print dumps the content of the stack
func (st *Stack) Print() {
fmt.Println("### stack ###")
if len(st.Data) > 0 {
for i, val := range st.Data {
if len(st.StackData) > 0 {
for i, val := range st.StackData {
fmt.Printf("%-3d %v\n", i, val)
}
} else {
Expand All @@ -104,6 +113,6 @@ func (st *Stack) Print() {
}

func ReturnNormalStack(s *Stack) {
s.Data = s.Data[:0]
s.StackData = s.StackData[:0]
stackPool.Put(s)
}
2 changes: 1 addition & 1 deletion eth/stagedsync/stage_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func SpawnExecuteBlocksStage(s *StageState, u Unwinder, tx kv.RwTx, toBlock uint
var readAhead chan uint64
if initialCycle {
// snapshots are often stored on chaper drives. don't expect low-read-latency and manually read-ahead.
// can't use OS-level ReadAhead - because Data >> RAM
// can't use OS-level ReadAhead - because StackData >> RAM
// it also warmsup state a bit - by touching senders/coninbase accounts and code
var clean func()
readAhead, clean = blocksReadAhead(ctx, &cfg, 4)
Expand Down
6 changes: 3 additions & 3 deletions eth/tracers/js/goja.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,14 +635,14 @@ func (s *stackObj) Peek(idx int) goja.Value {

// peek returns the nth-from-the-top element of the stack.
func (s *stackObj) peek(idx int) (*big.Int, error) {
if len(s.stack.Data) <= idx || idx < 0 {
return nil, fmt.Errorf("tracer accessed out of bound stack: size %d, index %d", len(s.stack.Data), idx)
if len(s.stack.StackData) <= idx || idx < 0 {
return nil, fmt.Errorf("tracer accessed out of bound stack: size %d, index %d", len(s.stack.StackData), idx)
}
return s.stack.Back(idx).ToBig(), nil
}

func (s *stackObj) Length() int {
return len(s.stack.Data)
return len(s.stack.StackData)
}

func (s *stackObj) setupObject() *goja.Object {
Expand Down
2 changes: 1 addition & 1 deletion eth/tracers/logger/access_list_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (a *AccessListTracer) CaptureState(pc uint64, op vm.OpCode, gas, cost uint6
contract := scope.Contract
caller := contract.Address()

stackData := stack.Data
stackData := stack.StackData
stackLen := len(stackData)
if (op == vm.SLOAD || op == vm.SSTORE) && stackLen >= 1 {
addr := contract.Address()
Expand Down
Loading

0 comments on commit ea71f7b

Please sign in to comment.