precompiles

package
v0.37.12-re-execute-block Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: AGPL-3.0 Imports: 9 Imported by: 1

Documentation

Index

Constants

View Source
const (
	FixedSizeUnitDataReadSize = 32
	Bytes4DataReadSize        = 4
	Bytes8DataReadSize        = 8
	Bytes32DataReadSize       = 32
	Uint64ByteSize            = 8

	EncodedBoolSize    = FixedSizeUnitDataReadSize
	EncodedAddressSize = FixedSizeUnitDataReadSize
	EncodedBytes32Size = FixedSizeUnitDataReadSize
	EncodedBytes4Size  = FixedSizeUnitDataReadSize
	EncodedBytes8Size  = FixedSizeUnitDataReadSize
	EncodedUint64Size  = FixedSizeUnitDataReadSize
	EncodedUint256Size = FixedSizeUnitDataReadSize
)

This package provides fast and efficient utilities needed for abi encoding and decoding encodings are mostly used for testing purpose if more complex encoding and decoding is needed please use the abi package and pass the ABIs, though that has a performance overhead.

View Source
const FunctionSelectorLength = 4
View Source
const InvalidMethodCallGasUsage = uint64(1)

InvalidMethodCallGasUsage captures how much gas we charge for invalid method call

Variables

View Source
var (
	FlowBlockHeightFuncSig = ComputeFunctionSelector("flowBlockHeight", nil)

	ProofVerifierFuncSig = ComputeFunctionSelector(
		"verifyCOAOwnershipProof",
		[]string{"address", "bytes32", "bytes"},
	)

	RandomSourceFuncSig = ComputeFunctionSelector("getRandomSource", []string{"uint64"})

	RevertibleRandomFuncSig = ComputeFunctionSelector("revertibleRandom", nil)

	// FlowBlockHeightFixedGas is set to match the `number` opCode (0x43)
	FlowBlockHeightFixedGas = uint64(2)
	// ProofVerifierBaseGas covers the cost of decoding, checking capability the resource
	// and the rest of operations excluding signature verification
	ProofVerifierBaseGas = uint64(1_000)
	// ProofVerifierGasMultiplerPerSignature is set to match `ECRECOVER`
	// but we might increase this in the future
	ProofVerifierGasMultiplerPerSignature = uint64(3_000)

	// RandomSourceGas covers the cost of obtaining random sournce bytes
	RandomSourceGas = uint64(1_000)

	// RevertibleRandomGas covers the cost of calculating a revertible random bytes
	RevertibleRandomGas = uint64(1_000)
)
View Source
var ErrBufferTooSmall = errors.New("buffer too small for encoding")
View Source
var ErrDataTooLarge = errors.New("input data is too large for encoding")
View Source
var ErrInputDataTooSmall = errors.New("input data is too small for decoding")
View Source
var ErrInvalidMethodCall = errors.New("invalid method call")

ErrInvalidMethodCall is returned when the method is not available on the contract

Functions

func ABIEncodeProof

func ABIEncodeProof(proof *types.COAOwnershipProofInContext) ([]byte, error)

func AggregatedPrecompiledCallsToPrecompiledContracts added in v0.36.3

func AggregatedPrecompiledCallsToPrecompiledContracts(apc types.AggregatedPrecompiledCalls) []types.PrecompiledContract

AggregatedPrecompiledCallsToPrecompiledContracts converts an aggregated set of precompile calls into a list of replayer precompiled contract

func ArchContract

func ArchContract(
	address types.Address,
	heightProvider func() (uint64, error),
	proofVer func(*types.COAOwnershipProofInContext) (bool, error),
	randomSourceProvider func(uint64) ([]byte, error),
	revertibleRandomGenerator func() (uint64, error),
) types.PrecompiledContract

ArchContract return a procompile for the Cadence Arch contract which facilitates access of Flow EVM environment into the Cadence environment. for more details see this Flip 223.

func DecodeABIEncodedProof

func DecodeABIEncodedProof(input []byte) (*types.COAOwnershipProofInContext, error)

func EncodeAddress

func EncodeAddress(address gethCommon.Address, buffer []byte, index int) error

EncodeAddress encodes the address and add it to the buffer at the index

func EncodeBool

func EncodeBool(bitSet bool, buffer []byte, index int) error

EncodeBool encodes a boolean into fixed size unit of encoded data

func EncodeBytes

func EncodeBytes(data []byte, buffer []byte, headerIndex, payloadIndex int) error

EncodeBytes encodes the data into the buffer at index and append payload to the end of buffer

func EncodeBytes32

func EncodeBytes32(data []byte, buffer []byte, index int) error

EncodeBytes32 encodes data into a bytes 32

func EncodeUint64

func EncodeUint64(inp uint64, buffer []byte, index int) error

EncodeUint64 encodes a uint64 into fixed size unit of encoded data (zero-padded on the left side)

func MultiFunctionPrecompiledContract added in v0.35.17

func MultiFunctionPrecompiledContract(
	address types.Address,
	functions []Function,
) types.PrecompiledContract

MultiFunctionPrecompiledContract constructs a multi-function precompile smart contract

func ReadAddress

func ReadAddress(buffer []byte, index int) (gethCommon.Address, error)

ReadAddress reads an address from the buffer at index

func ReadBool

func ReadBool(buffer []byte, index int) (bool, error)

ReadBool reads a boolean from the buffer at the index

func ReadBytes

func ReadBytes(buffer []byte, index int) ([]byte, error)

ReadBytes reads a variable length bytes from the buffer

func ReadBytes32

func ReadBytes32(buffer []byte, index int) ([]byte, error)

ReadBytes32 reads a 32 byte slice from the buffer at index

func ReadBytes4

func ReadBytes4(buffer []byte, index int) ([]byte, error)

ReadBytes4 reads a 4 byte slice from the buffer at index

func ReadBytes8

func ReadBytes8(buffer []byte, index int) ([]byte, error)

ReadBytes8 reads a 8 byte slice from the buffer at index

func ReadUint256

func ReadUint256(buffer []byte, index int) (*big.Int, error)

ReadUint256 reads an address from the buffer at index

func ReadUint64

func ReadUint64(buffer []byte, index int) (uint64, error)

ReadUint64 reads a uint64 from the buffer at index

func SizeNeededForBytesEncoding

func SizeNeededForBytesEncoding(data []byte) int

SizeNeededForBytesEncoding computes the number of bytes needed for bytes encoding

Types

type Function

type Function interface {
	// FunctionSelector returns the function selector bytes for this function
	FunctionSelector() FunctionSelector

	// ComputeGas computes the gas needed for the given input
	ComputeGas(input []byte) uint64

	// Run runs the function on the given data
	Run(input []byte) ([]byte, error)
}

Function is an interface for a function in a multi-function precompile contract

type FunctionSelector

type FunctionSelector [FunctionSelectorLength]byte

This is derived as the first 4 bytes of the Keccak hash of the ASCII form of the signature of the method

func ComputeFunctionSelector

func ComputeFunctionSelector(name string, args []string) FunctionSelector

ComputeFunctionSelector computes the function selector given the canonical name of function and args. for example the canonical format for int is int256

func SplitFunctionSelector

func SplitFunctionSelector(input []byte) (FunctionSelector, []byte)

SplitFunctionSelector splits the function signature from input data and returns the rest of the data

func (FunctionSelector) Bytes

func (fs FunctionSelector) Bytes() []byte

type ReplayerPrecompiledContract added in v0.36.3

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

ReplayerPrecompiledContract is a precompiled contract that replay the outputs based on the input

func NewReplayerPrecompiledContract added in v0.36.3

func NewReplayerPrecompiledContract(
	expectedCalls *types.PrecompiledCalls,
) *ReplayerPrecompiledContract

NewReplayerPrecompiledContract constructs a ReplayerPrecompiledContract

func (*ReplayerPrecompiledContract) Address added in v0.36.3

func (*ReplayerPrecompiledContract) HasReplayedAll added in v0.36.3

func (p *ReplayerPrecompiledContract) HasReplayedAll() bool

func (*ReplayerPrecompiledContract) RequiredGas added in v0.36.3

func (p *ReplayerPrecompiledContract) RequiredGas(input []byte) (output uint64)

func (*ReplayerPrecompiledContract) Run added in v0.36.3

func (p *ReplayerPrecompiledContract) Run(input []byte) (output []byte, err error)

Jump to

Keyboard shortcuts

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