worldmock

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2021 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const BuiltInFunctionESDTTransfer = "ESDTTransfer"

BuiltInFunctionESDTTransfer is the key for the elrond standard digital token transfer built-in function

Variables

View Source
var ErrInsufficientFunds = errors.New("insufficient funds")

ErrInsufficientFunds signals the funds are insufficient for the move balance operation but the transaction fee is covered by the current balance

Functions

func GenerateMockAddress

func GenerateMockAddress(creatorAddress []byte, creatorNonce uint64) []byte

/ GenerateMockAddress simulates creation of a new address by the protocol.

Types

type Account

type Account struct {
	Exists          bool
	Address         []byte
	Nonce           uint64
	Balance         *big.Int
	BalanceDelta    *big.Int
	Storage         map[string][]byte
	Code            []byte
	CodeHash        []byte
	CodeMetadata    []byte
	AsyncCallData   string
	OwnerAddress    []byte
	Username        []byte
	ShardID         uint32
	IsSmartContract bool
	ESDTData        map[string]*ESDTData
}

Account holds the account info

func (*Account) AddressBytes

func (a *Account) AddressBytes() []byte

AddressBytes -

func (*Account) GetBalance

func (a *Account) GetBalance() *big.Int

GetBalance -

func (*Account) GetCode

func (a *Account) GetCode() []byte

GetCode -

func (*Account) GetCodeHash

func (a *Account) GetCodeHash() []byte

GetCodeHash -

func (*Account) GetCodeMetadata

func (a *Account) GetCodeMetadata() []byte

GetCodeMetadata -

func (*Account) GetDeveloperReward

func (a *Account) GetDeveloperReward() *big.Int

GetDeveloperReward -

func (*Account) GetNonce

func (a *Account) GetNonce() uint64

GetNonce -

func (*Account) GetOwnerAddress

func (a *Account) GetOwnerAddress() []byte

GetOwnerAddress -

func (*Account) GetRootHash

func (a *Account) GetRootHash() []byte

GetRootHash -

func (*Account) GetUserName

func (a *Account) GetUserName() []byte

GetUserName -

func (*Account) IsInterfaceNil

func (a *Account) IsInterfaceNil() bool

IsInterfaceNil -

func (*Account) SetBalance added in v1.1.2

func (a *Account) SetBalance(balance int64)

SetBalance -

func (*Account) SetCode added in v1.1.2

func (a *Account) SetCode(code []byte, codeMetadata *vmcommon.CodeMetadata)

SetCode changes the account code, as well as all fields depending on it: CodeHash, IsSmartContract, CodeMetadata. The code metadata must be given explicitly.

func (*Account) StorageValue

func (a *Account) StorageValue(key string) []byte

StorageValue yields the storage value for key, default 0

type AccountMap

type AccountMap map[string]*Account

AccountMap is a map from address to account

func NewAccountMap

func NewAccountMap() AccountMap

NewAccountMap creates a new AccountMap instance.

func (AccountMap) CreateAccount added in v1.1.2

func (am AccountMap) CreateAccount(address []byte) *Account

CreateAccount instantiates an empty account for the given address.

func (AccountMap) CreateSmartContractAccount added in v1.1.2

func (am AccountMap) CreateSmartContractAccount(owner []byte, address []byte, code []byte) *Account

CreateSmartContractAccount instantiates an account for a smart contract with the given address and WASM bytecode.

func (AccountMap) DeleteAccount

func (am AccountMap) DeleteAccount(address []byte)

DeleteAccount removes account based on address

func (AccountMap) GetAccount

func (am AccountMap) GetAccount(address []byte) *Account

GetAccount retrieves account based on address

func (AccountMap) PutAccount

func (am AccountMap) PutAccount(account *Account)

PutAccount inserts account based on address.

func (AccountMap) PutAccounts

func (am AccountMap) PutAccounts(accounts []*Account)

PutAccounts inserts multiple accounts based on address.

type BlockInfo

type BlockInfo struct {
	BlockTimestamp uint64
	BlockNonce     uint64
	BlockRound     uint64
	BlockEpoch     uint32
	RandomSeed     *[48]byte
}

BlockInfo contains metadata about a mocked block

type ESDTData

type ESDTData struct {
	Balance      *big.Int
	BalanceDelta *big.Int
	Frozen       bool
}

ESDTData models an account holding an ESDT token

type MockWorld

type MockWorld struct {
	AcctMap                    AccountMap
	PreviousBlockInfo          *BlockInfo
	CurrentBlockInfo           *BlockInfo
	Blockhashes                [][]byte
	NewAddressMocks            []*NewAddressMock
	StateRootHash              []byte
	Err                        error
	LastCreatedContractAddress []byte
	CompiledCode               map[string][]byte
}

MockWorld provides a mock representation of the blockchain to be used in VM tests.

func NewMockWorld

func NewMockWorld() *MockWorld

NewMockWorld creates a new mock instance

func (*MockWorld) Clear

func (b *MockWorld) Clear()

Clear resets all mock data between tests.

func (*MockWorld) ClearCompiledCodes

func (b *MockWorld) ClearCompiledCodes()

func (*MockWorld) CurrentEpoch

func (b *MockWorld) CurrentEpoch() uint32

CurrentEpoch returns the current epoch

func (*MockWorld) CurrentNonce

func (b *MockWorld) CurrentNonce() uint64

CurrentNonce returns the nonce from the current block

func (*MockWorld) CurrentRandomSeed

func (b *MockWorld) CurrentRandomSeed() []byte

CurrentRandomSeed returns the random seed from the current header

func (*MockWorld) CurrentRound

func (b *MockWorld) CurrentRound() uint64

CurrentRound returns the round from the current block

func (*MockWorld) CurrentTimeStamp

func (b *MockWorld) CurrentTimeStamp() uint64

CurrentTimeStamp return the timestamp from the current block

func (*MockWorld) GetAllState

func (b *MockWorld) GetAllState(accountAddress []byte) (map[string][]byte, error)

GetAllState simply returns the storage as-is.

func (*MockWorld) GetBlockhash

func (b *MockWorld) GetBlockhash(nonce uint64) ([]byte, error)

GetBlockhash should return the hash of the nth previous blockchain. Offset specifies how many blocks we need to look back.

func (*MockWorld) GetBuiltinFunctionNames

func (b *MockWorld) GetBuiltinFunctionNames() vmcommon.FunctionNames

GetBuiltinFunctionNames -

func (*MockWorld) GetCode added in v1.0.2

func (b *MockWorld) GetCode(acc vmcommon.UserAccountHandler) []byte

GetCode retrieves the code from the given account, or nil if not found

func (*MockWorld) GetCompiledCode

func (b *MockWorld) GetCompiledCode(codeHash []byte) (bool, []byte)

func (*MockWorld) GetESDTToken added in v1.2.0

func (b *MockWorld) GetESDTToken(_ []byte, _ []byte, _ uint64) (*esdt.ESDigitalToken, error)

GetESDTToken -

func (*MockWorld) GetShardOfAddress

func (b *MockWorld) GetShardOfAddress(address []byte) uint32

GetShardOfAddress -

func (*MockWorld) GetStateRootHash

func (b *MockWorld) GetStateRootHash() []byte

GetStateRootHash returns the state root hash from the last committed block

func (*MockWorld) GetStorageData

func (b *MockWorld) GetStorageData(accountAddress []byte, key []byte) ([]byte, error)

GetStorageData yields the storage value for a certain account and storage key. Should return an empty byte array if the key is missing from the account storage

func (*MockWorld) GetUserAccount

func (b *MockWorld) GetUserAccount(address []byte) (vmcommon.UserAccountHandler, error)

GetUserAccount retrieves account info from map, or error if not found.

func (*MockWorld) IsInterfaceNil

func (b *MockWorld) IsInterfaceNil() bool

IsInterfaceNil returns true if underlying implementation is nil

func (*MockWorld) IsPayable

func (b *MockWorld) IsPayable(address []byte) (bool, error)

func (*MockWorld) IsSmartContract

func (b *MockWorld) IsSmartContract(address []byte) bool

IsSmartContract -

func (*MockWorld) LastEpoch

func (b *MockWorld) LastEpoch() uint32

LastEpoch returns the epoch from the last committed block

func (*MockWorld) LastNonce

func (b *MockWorld) LastNonce() uint64

LastNonce returns the nonce from from the last committed block

func (*MockWorld) LastRandomSeed

func (b *MockWorld) LastRandomSeed() []byte

LastRandomSeed returns the random seed from the last committed block

func (*MockWorld) LastRound

func (b *MockWorld) LastRound() uint64

LastRound returns the round from the last committed block

func (*MockWorld) LastTimeStamp

func (b *MockWorld) LastTimeStamp() uint64

LastTimeStamp returns the timeStamp from the last committed block

func (*MockWorld) NewAddress

func (b *MockWorld) NewAddress(creatorAddress []byte, creatorNonce uint64, _ []byte) ([]byte, error)

NewAddress provides the address for a new account. It looks up the explicit new address mocks, if none found generates one using a fake but realistic algorithm.

func (*MockWorld) ProcessBuiltInFunction

func (b *MockWorld) ProcessBuiltInFunction(input *vmcommon.ContractCallInput) (*vmcommon.VMOutput, error)

ProcessBuiltInFunction -

func (*MockWorld) RollbackChanges

func (b *MockWorld) RollbackChanges()

RollbackChanges should be called after the VM test has run, if the tx has failed

func (*MockWorld) SaveCompiledCode

func (b *MockWorld) SaveCompiledCode(codeHash []byte, code []byte)

func (*MockWorld) SetCurrentBlockHash

func (b *MockWorld) SetCurrentBlockHash(blockHash []byte)

func (*MockWorld) StartTransferESDT

func (b *MockWorld) StartTransferESDT(from, to []byte, tokenName string, amount *big.Int) (bool, error)

StartTransferESDT updates ESDT balance deltas, without changing the actual balances. The deltas need to be committed afterwards.

func (*MockWorld) UpdateAccountFromOutputAccount added in v1.1.2

func (b *MockWorld) UpdateAccountFromOutputAccount(modAcct *vmcommon.OutputAccount)

UpdateAccountFromOutputAccount updates a single account from a transaction output.

func (*MockWorld) UpdateAccounts

func (b *MockWorld) UpdateAccounts(
	outputAccounts map[string]*vmcommon.OutputAccount,
	accountsToDelete [][]byte) error

UpdateAccounts should be called after the VM test has run, to update world state

func (*MockWorld) UpdateBalance

func (b *MockWorld) UpdateBalance(address []byte, newBalance *big.Int) error

UpdateBalance sets a new balance to an account

func (*MockWorld) UpdateBalanceWithDelta

func (b *MockWorld) UpdateBalanceWithDelta(address []byte, balanceDelta *big.Int) error

UpdateBalanceWithDelta changes balance of an account by a given amount

func (*MockWorld) UpdateWorldStateBefore

func (b *MockWorld) UpdateWorldStateBefore(
	fromAddr []byte,
	gasLimit uint64,
	gasPrice uint64) error

UpdateWorldStateBefore performs gas payment, before transaction

type NewAddressMock

type NewAddressMock struct {
	CreatorAddress []byte
	CreatorNonce   uint64
	NewAddress     []byte
}

NewAddressMock allows tests to specify what new addresses to generate

Jump to

Keyboard shortcuts

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