mock

package
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package counter implements buffered position-based editing of byte slices.

Package mock provides lightweight contract call tracing and logging

Index

Constants

This section is empty.

Variables

View Source
var (
	GasImportPath      = "github.com/govm-net/vm/mock"
	GasPackageNickName = ""
	GasPackageName     = "mock"
	GasConsumeGasFunc  = "ConsumeGas"
)

Functions

func AddGasConsumption

func AddGasConsumption(packageName string, code []byte) ([]byte, error)

AddGasConsumption adds gas consumption tracking to the code

func AddMockEnterExit added in v0.1.4

func AddMockEnterExit(packageName string, code []byte) ([]byte, error)

AddMockEnterExit adds mock.Enter/Exit to exported functions

func ConsumeGas

func ConsumeGas(amount int64)

ConsumeGas consumes gas

func Enter

func Enter(contract string, function string)

Enter records function entry by pushing the contract address onto the call stack

func Exit

func Exit(contract string, function string)

Exit records function exit by popping the top contract address from the call stack

func GetCaller

func GetCaller() core.Address

GetCaller returns the address of the contract that called the current contract This correctly handles the case where a contract calls its own functions Returns an empty address if there's no caller (e.g., top-level call)

func GetCurrentContract

func GetCurrentContract() core.Address

GetCurrentContract returns the address of the currently executing contract Returns an empty address if the call stack is empty

func GetGas

func GetGas() int64

GetGas gets remaining gas

func GetUsedGas

func GetUsedGas() int64

GetUsedGas gets consumed gas

func InitGas

func InitGas(initialGas int64)

InitGas initializes gas

func RefundGas

func RefundGas(amount int64)

RefundGas refunds gas

func ResetGas

func ResetGas(initialGas int64)

ResetGas resets gas

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

A Buffer is a queue of edits to apply to a given byte slice.

func NewBuffer

func NewBuffer(data []byte) *Buffer

NewBuffer returns a new buffer to accumulate changes to an initial data slice. The returned buffer maintains a reference to the data, so the caller must ensure the data is not modified until after the Buffer is done being used.

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes returns a new byte slice containing the original data with the queued edits applied.

func (*Buffer) Delete

func (b *Buffer) Delete(start, end int)

Delete delete

func (*Buffer) Insert

func (b *Buffer) Insert(pos int, new string)

Insert insert

func (*Buffer) Replace

func (b *Buffer) Replace(start, end int, new string)

Replace replace

func (*Buffer) String

func (b *Buffer) String() string

String returns a string containing the original data with the queued edits applied.

type CallTraceGenerator added in v0.1.5

type CallTraceGenerator func(packageName, funcName string) []ast.Stmt
var DefaultTraceGenerator CallTraceGenerator = func(packageName, funcName string) []ast.Stmt {
	enterStmt := &ast.ExprStmt{
		X: &ast.CallExpr{
			Fun: &ast.SelectorExpr{
				X:   ast.NewIdent(GasPackageName),
				Sel: ast.NewIdent("Enter"),
			},
			Args: []ast.Expr{
				&ast.BasicLit{
					Kind:  token.STRING,
					Value: `"` + packageName + `"`,
				},
				&ast.BasicLit{
					Kind:  token.STRING,
					Value: `"` + funcName + `"`,
				},
			},
		},
	}
	exitStmt := &ast.DeferStmt{
		Call: &ast.CallExpr{
			Fun: &ast.SelectorExpr{
				X:   ast.NewIdent(GasPackageName),
				Sel: ast.NewIdent("Exit"),
			},
			Args: []ast.Expr{
				&ast.BasicLit{
					Kind:  token.STRING,
					Value: `"` + packageName + `"`,
				},
				&ast.BasicLit{
					Kind:  token.STRING,
					Value: `"` + funcName + `"`,
				},
			},
		},
	}
	return []ast.Stmt{enterStmt, exitStmt}
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL