storage

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: Apache-2.0 Imports: 15 Imported by: 3

Documentation

Overview

Package storage provides tools for interacting with smart contract storages on the Ethereum based networks. It includes functionalities such as querying contract storage, decoding contracts, and reading contract storage variables.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Descriptor

type Descriptor struct {
	Detector *detector.Detector `json:"-"`

	Address           common.Address         `json:"address"`
	Block             *big.Int               `json:"block"`
	StateVariables    map[string][]*Variable `json:"-"`
	TargetVariables   map[string][]*Variable `json:"-"`
	ConstantVariables map[string][]*Variable `json:"-"`
	StorageLayout     *StorageLayout         `json:"storage_layout"`
	// contains filtered or unexported fields
}

Descriptor holds information about a smart contract's state at a specific block. It includes details like state variables, target variables, constant variables, and storage layouts.

func (*Descriptor) GetAST

func (s *Descriptor) GetAST() *ast.ASTBuilder

GetAST retrieves the abstract syntax tree (AST) builder for the contract. It returns an error if the AST builder is not available due to parsing failures or initialization issues.

func (*Descriptor) GetBlock

func (s *Descriptor) GetBlock() *big.Int

GetBlock returns the block number at which the contract's state is described.

func (*Descriptor) GetCFG

func (s *Descriptor) GetCFG() *cfg.Builder

GetCFG retrieves the control flow graph (CFG) builder of the contract. It returns an error if the CFG builder is not set, indicating parsing or initialization issues.

func (*Descriptor) GetConstantStorageSlotVariables

func (s *Descriptor) GetConstantStorageSlotVariables() map[string][]*Variable

GetConstantStorageSlotVariables returns a map of constant variables associated with the contract.

func (*Descriptor) GetDetector

func (s *Descriptor) GetDetector() *detector.Detector

GetDetector retrieves the contract's detector, which is essential for contract analysis. It returns an error if the contract or its detector is not properly set up.

func (*Descriptor) GetIR

func (s *Descriptor) GetIR() *ir.Builder

GetIR retrieves the intermediate representation (IR) builder of the contract. It returns an error if the IR builder is not set, indicating parsing or initialization issues.

func (*Descriptor) GetSlots

func (s *Descriptor) GetSlots() []*SlotDescriptor

GetSlots returns slots descriptor by its declaration line.

func (*Descriptor) GetSortedSlots

func (s *Descriptor) GetSortedSlots() []*SlotDescriptor

GetSortedSlots returns a slice of slot descriptors sorted by their declaration line. It aggregates and sorts slot descriptors from all storage layouts.

func (*Descriptor) GetStateVariables

func (s *Descriptor) GetStateVariables() map[string][]*Variable

GetStateVariables returns a map of state variables associated with the contract.

func (*Descriptor) GetStorageLayout

func (s *Descriptor) GetStorageLayout() *StorageLayout

GetStorageLayout returns all storage layouts associated with the contract.

func (*Descriptor) GetTargetVariables

func (s *Descriptor) GetTargetVariables() map[string][]*Variable

GetTargetVariables returns a map of target variables associated with the contract.

type Options

type Options struct {
	// UseSimulator indicates whether to use the simulator for storage operations.
	// When set to true, storage operations are simulated. This is useful for testing
	// or development purposes where actual storage resources are not required.
	// The default value is false.
	UseSimulator bool `json:"use_simulator"`
}

Options defines the configuration parameters for the storage system. It includes settings to determine the use of a simulator for storage operations.

func NewDefaultOptions

func NewDefaultOptions() *Options

NewDefaultOptions creates and returns a new instance of Options with default settings. By default, the UseSimulator field is set to false, indicating that the simulator is not used, and actual storage operations are performed.

func NewSimulatorOptions

func NewSimulatorOptions() *Options

NewSimulatorOptions creates and returns a new instance of Options with the UseSimulator field set to true. This configuration is useful for scenarios where storage operations need to be simulated, such as during testing or when actual storage resources are not available or necessary.

type Reader

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

Reader is responsible for reading and interpreting storage-related information of a smart contract. It uses a Descriptor to understand the contract's storage layout and variables.

func NewReader

func NewReader(ctx context.Context, s *Storage, d *Descriptor) (*Reader, error)

NewReader creates a new instance of Reader with the given context, Storage, and Descriptor. It returns a pointer to the created Reader and any error encountered during its creation.

func (*Reader) CalculateStorageLayout

func (r *Reader) CalculateStorageLayout() error

CalculateStorageLayout calculates and sets the storage layout of the smart contract in the Descriptor. It determines the slot and offset for each storage variable and organizes them accordingly.

func (*Reader) DiscoverStorageVariables

func (r *Reader) DiscoverStorageVariables() error

DiscoverStorageVariables analyzes the smart contract to discover and categorize storage variables. It differentiates between constant and non-constant variables and organizes them accordingly.

func (*Reader) GetDescriptor

func (r *Reader) GetDescriptor() *Descriptor

GetDescriptor returns the Descriptor associated with the Reader.

type SlotDescriptor

type SlotDescriptor struct {
	// DeclarationId is a unique identifier for the variable declaration associated with this slot.
	DeclarationId int64 `json:"declaration_id"`

	// Variable represents the variable metadata and is not serialized into JSON.
	Variable *Variable `json:"-"`

	// Contract points to the IR representation of the contract containing this slot.
	// It is not serialized into JSON.
	Contract *ir.Contract `json:"-"`

	// BlockNumber specifies the block at which the slot's state is considered.
	BlockNumber *big.Int `json:"block_number"`

	// Name is the name of the variable in the smart contract.
	Name string `json:"name"`

	// Type is the high-level type of the variable (e.g., "uint256").
	Type string `json:"type"`

	// TypeDescription provides a detailed AST-based type description of the variable.
	TypeDescription *ast.TypeDescription `json:"type_description"`

	// Slot is the index of the storage slot in the contract.
	Slot int64 `json:"slot"`

	// Size indicates the size of the variable in bytes.
	Size int64 `json:"size"`

	// Offset represents the byte offset of the variable within the storage slot.
	Offset int64 `json:"offset"`

	// RawValue is the raw Ethereum storage slot value at the specified block number.
	RawValue common.Hash `json:"raw_value"`

	// Value is the interpreted value of the variable, with its type depending on the variable's type.
	Value interface{} `json:"value"`
}

SlotDescriptor provides detailed information about a storage slot in an Ethereum smart contract. It includes metadata about the variable stored in the slot, the contract it belongs to, and the slot's state at a specific block number.

type Storage

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

Storage is a struct that encapsulates various components required to interact with Ethereum smart contracts.

func NewStorage

func NewStorage(
	ctx context.Context,
	network utils.Network,
	pool *clients.ClientPool,

	opts *Options,
) (*Storage, error)

NewStorage creates a new Storage instance. It requires context, network configuration, and various components such as a client pool, simulator, etherscan provider, compiler, and binding manager. It returns an initialized Storage object or an error if the initialization fails.

func (*Storage) Describe

func (s *Storage) Describe(ctx context.Context, addr common.Address, detector *detector.Detector, cfgBuilder *cfg.Builder, atBlock *big.Int) (*Reader, error)

Describe queries and returns detailed information about a smart contract at a specific address. It requires context, the contract address, detector and the block number for the query. It returns a Reader instance containing contract details or an error if the query fails.

func (*Storage) DescribeLayout added in v0.3.5

func (s *Storage) DescribeLayout(ctx context.Context, addr common.Address, detector *detector.Detector, cfgBuilder *cfg.Builder, atBlock *big.Int) (*Reader, error)

func (*Storage) ReadStorageSlot

func (s *Storage) ReadStorageSlot(ctx context.Context, contractAddress common.Address, slot int64, blockNumber *big.Int) ([]byte, error)

ReadStorageSlot reads the storage at a given slot for a specific contract.

type StorageLayout

type StorageLayout struct {
	Slots []*SlotDescriptor `json:"slots"` // Slots is a slice of SlotDescriptor pointers.
}

StorageLayout represents the layout of storage with multiple slots. It holds a slice of SlotDescriptor pointers, each representing a storage slot.

func (*StorageLayout) AppendSlot

func (s *StorageLayout) AppendSlot(index int64, slot *SlotDescriptor) bool

AppendSlot adds a new SlotDescriptor to the Slots slice at a specified index. The function returns true if the slot is successfully added. It returns false if the slot already exists. After adding, the slots are sorted based on their declaration order using the DeclarationId field.

func (*StorageLayout) GetSlot

func (s *StorageLayout) GetSlot(slot int64) *SlotDescriptor

GetSlot retrieves a SlotDescriptor based on a given slot ID. Returns nil if no slot with the given ID exists.

func (*StorageLayout) GetSlotByName

func (s *StorageLayout) GetSlotByName(name string) *SlotDescriptor

GetSlotByName searches for and returns a SlotDescriptor based on a slot name. Returns nil if no slot with the given name is found.

func (*StorageLayout) GetSlotByOffset

func (s *StorageLayout) GetSlotByOffset(offset int64) *SlotDescriptor

GetSlotByOffset finds and returns a SlotDescriptor based on a slot offset. Returns nil if no slot with the given offset is found.

func (*StorageLayout) GetSlotByType

func (s *StorageLayout) GetSlotByType(t string) *SlotDescriptor

GetSlotByType finds and returns a SlotDescriptor based on a slot type. Returns nil if no slot of the given type is found.

func (*StorageLayout) GetSlots

func (s *StorageLayout) GetSlots() []*SlotDescriptor

GetSlots returns a slice of pointers to SlotDescriptor representing all slots.

func (*StorageLayout) SlotExists

func (s *StorageLayout) SlotExists(slot int64) bool

SlotExists checks if a slot with the given ID already exists in the layout. Returns true if the slot exists, and false otherwise.

type Variable

type Variable struct {
	*ir.StateVariable `json:"state_variable"` // StateVariable is the underlying state variable from the IR.
	Contract          *ir.Contract            `json:"contract"`       // Contract is the contract to which this variable belongs.
	EntryContract     bool                    `json:"entry_contract"` // EntryContract indicates if this variable is part of the entry contract.
}

Variable represents a state variable within a smart contract. It embeds ir.StateVariable and provides additional methods to determine the characteristics of the variable.

func (*Variable) GetArrayLength

func (v *Variable) GetArrayLength() (int64, error)

GetArrayLength returns the length of an array variable. For a variable representing an array, it parses and returns the array length. Returns 0 for non-array variables or if the length is not explicitly defined.

func (*Variable) GetStructMembers

func (v *Variable) GetStructMembers() []*Variable

GetStructMembers returns a slice of Variables representing the members of a struct. For a variable representing a struct, it parses and returns the struct members. Returns an empty slice for non-struct variables or if no members are defined.

func (*Variable) IsAddressType

func (v *Variable) IsAddressType() bool

IsAddressType checks if the variable is of an address type. Returns true if the variable type is prefixed with "address".

func (*Variable) IsArrayType

func (v *Variable) IsArrayType() bool

IsArrayType checks if the variable is of an array type. Returns true if the variable type includes square brackets, indicating an array.

func (*Variable) IsContractType

func (v *Variable) IsContractType() bool

IsContractType checks if the variable is of a contract type. Returns true if the variable type starts with "contract".

func (*Variable) IsDynamicArray

func (v *Variable) IsDynamicArray() bool

IsDynamicArray checks if the variable is a dynamic array. Currently, returns false and needs implementation for dynamic array checking.

func (*Variable) IsMappingType

func (v *Variable) IsMappingType() bool

IsMappingType checks if the variable is of a mapping type. Returns true if the variable type starts with "mapping".

func (*Variable) IsStructType

func (v *Variable) IsStructType() bool

IsStructType checks if the variable is of a struct type. Returns true if the variable type starts with "struct".

Jump to

Keyboard shortcuts

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