testutils

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 45 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultHostname is the default hostname used for the application API.
	DefaultHostname = "indexer.sia.tech"
)
View Source
const (
	// TestQuotaName is the name of the quota used in tests.
	TestQuotaName = "testing"
)

Variables

View Source
var (
	// MaintenanceSettings are the default maintenance settings used in testing.
	MaintenanceSettings = contracts.MaintenanceSettings{
		Enabled:         true,
		Period:          144,
		RenewWindow:     72,
		WantedContracts: 30,
	}
)
View Source
var TestQuotaFundTargetBytes uint64 = 1 << 30 // 1 GiB

TestQuotaFundTargetBytes is the fund target for the testing quota.

Functions

func NewWallet

func NewWallet(t testing.TB, c *ConsensusNode, walletKey types.PrivateKey) *wallet.SingleAddressWallet

NewWallet creates a new SingleAddressWallet for testing which is connected to all the other components created via the ConsensusNode.

Types

type Cluster

type Cluster struct {
	ConsensusNode *ConsensusNode
	Hosts         []*Host
	Indexer       *Indexer
	// contains filtered or unexported fields
}

Cluster is a test cluster that contains an indexer, hosts and other helper types as needed for integration testing.

func NewCluster

func NewCluster(t testing.TB, opts ...ClusterOpt) *Cluster

NewCluster creates a cluster for testing. A cluster contains an indexer and multiple hosts.

func (*Cluster) AddAccount

func (c *Cluster) AddAccount(t testing.TB) types.PrivateKey

AddAccount adds a new account to the indexer and returns the private key.

func (*Cluster) AddHosts

func (c *Cluster) AddHosts(ctx context.Context, t testing.TB, hosts ...*Host)

AddHosts adds the given hosts to the cluster.

func (*Cluster) AnnounceHosts

func (c *Cluster) AnnounceHosts(ctx context.Context, t testing.TB, hosts ...*Host)

AnnounceHosts announces the hosts and blocks until they are indexed.

func (*Cluster) FundHosts

func (c *Cluster) FundHosts(ctx context.Context, t testing.TB, hosts ...*Host)

FundHosts funds the hosts with multiple blocks, then waits for the funds to mature.

func (*Cluster) NewHosts

func (c *Cluster) NewHosts(t testing.TB, n int) []*Host

NewHosts creates n new hosts using the cluster's network and genesis block.

func (*Cluster) WaitForAccountFunding

func (c *Cluster) WaitForAccountFunding(t *testing.T, pk proto.Account)

WaitForAccountFunding waits until the given account is funded on every host in the cluster.

func (*Cluster) WaitForContracts

func (c *Cluster) WaitForContracts(t *testing.T)

WaitForContracts waits until all contracts are formed

type ClusterOpt

type ClusterOpt func(*clusterCfg)

ClusterOpt is a functional option for configuring a cluster for testing

func WithHosts

func WithHosts(n int) ClusterOpt

WithHosts allows for overriding the default number of hosts in the cluster

func WithIndexer

func WithIndexer(opts ...IndexerOpt) ClusterOpt

WithIndexer allows for passing options to the indexer when creating the cluster. This is useful for configuring slab options, logger, etc.

func WithLogger

func WithLogger(logger *zap.Logger) ClusterOpt

WithLogger allows for attaching a custom logger to the cluster for debugging if necessary

type ConsensusNode

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

ConsensusNode is a helper type that serves as the source of truth for anything consensus-related during testing. It can be used to create test types such as the Indexer or Wallet and will guarantee that a call to `MineBlock` won't return before all created components are done subscribing to the newly mined blocks.

func NewConsensusNode

func NewConsensusNode(t testing.TB, log *zap.Logger) *ConsensusNode

NewConsensusNode creates a new node ready for testing. It will mine enough blocks to reach the V2 require height before returning.

func (*ConsensusNode) MineBlocks

func (c *ConsensusNode) MineBlocks(t testing.TB, addr types.Address, n uint64)

MineBlocks is a helper to mine blocks and broadcast the headers

func (*ConsensusNode) Network

func (c *ConsensusNode) Network() *consensus.Network

Network returns the used network configuration of the ConsensusNode

func (*ConsensusNode) NewHost

func (c *ConsensusNode) NewHost(t testing.TB, pk types.PrivateKey, log *zap.Logger) *Host

NewHost creates a new host.

type Explorer

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

Explorer is a mock implementation of an explorer.

func NewExplorer

func NewExplorer() *Explorer

NewExplorer creates a new instance of Explorer with a default exchange rates for usd.

func (*Explorer) BaseURL

func (e *Explorer) BaseURL() string

BaseURL returns the base URL of the explorer.

func (*Explorer) SiacoinExchangeRate

func (e *Explorer) SiacoinExchangeRate(ctx context.Context, currency string) (rate float64, err error)

SiacoinExchangeRate returns the exchange rate for a given currency.

type Host

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

A Host is an ephemeral host that can be used for testing.

func (*Host) Addr

func (h *Host) Addr() string

Addr returns the host's address.

func (*Host) Announce

func (h *Host) Announce() error

Announce announces the host on the network.

func (*Host) Connect

func (h *Host) Connect(ctx context.Context, addr string) error

Connect connects the host's syncer with the given peer.

func (*Host) DropSector

func (h *Host) DropSector(root types.Hash256)

DropSector simulates the host losing a sector by removing it from the sector store.

func (*Host) PublicKey

func (h *Host) PublicKey() types.PublicKey

PublicKey returns the host's public key.

func (*Host) QUICAddr added in v0.2.0

func (h *Host) QUICAddr() string

QUICAddr returns the host's QUIC address.

func (*Host) WalletAddress

func (h *Host) WalletAddress() types.Address

WalletAddress returns the host's wallet address.

type Indexer

type Indexer struct {
	AdminURL      string
	AdminPassword string
	AppURL        string

	Admin *admin.Client
	App   *app.Client
	// contains filtered or unexported fields
}

Indexer is a test utility combining an indexer, an http client for the indexer and useful helpers for testing.

func NewIndexer

func NewIndexer(t testing.TB, c *ConsensusNode, log *zap.Logger, opts ...IndexerOpt) *Indexer

NewIndexer creates a new indexer for testing that is automatically closed up after the test is finished.

func (*Indexer) Accounts

func (i *Indexer) Accounts() *accounts.AccountManager

Accounts returns the account manager for the indexer.

func (*Indexer) AddTestAccount

func (idx *Indexer) AddTestAccount(t *testing.T, pk types.PublicKey)

AddTestAccount adds a test account to the indexer and funds it.

func (*Indexer) Alerter

func (idx *Indexer) Alerter() *alerts.Manager

Alerter returns the underlying alert manager.

func (*Indexer) ChainManager

func (idx *Indexer) ChainManager() *chain.Manager

ChainManager returns the underlying chain manager.

func (*Indexer) Client

func (idx *Indexer) Client() *client.Client

Client returns the underlying client/v2 client.

func (*Indexer) Contracts

func (i *Indexer) Contracts() *contracts.ContractManager

Contracts returns the contract manager for the indexer.

func (*Indexer) Hosts

func (i *Indexer) Hosts() *hosts.HostManager

Hosts returns the host manager for the indexer.

func (*Indexer) Signer

func (idx *Indexer) Signer() rhp.FormContractSigner

Signer returns the contract signer.

func (*Indexer) Store

func (idx *Indexer) Store() TestStore

Store returns the underlying store.

func (*Indexer) Tip

func (idx *Indexer) Tip() (types.ChainIndex, error)

Tip returns the current tip of the chain.

func (*Indexer) WalletAddr

func (idx *Indexer) WalletAddr() types.Address

WalletAddr returns the address of the wallet.

type IndexerOpt

type IndexerOpt func(*indexerCfg)

IndexerOpt is a functional option for configuring an indexer for testing

func WithAdvertiseURL

func WithAdvertiseURL(url string) IndexerOpt

WithAdvertiseURL sets the advertise URL for the indexer.

func WithContractOptions

func WithContractOptions(opts ...contracts.ContractManagerOpt) IndexerOpt

WithContractOptions allows for passing contract options to the indexer

func WithMaintenanceSettings

func WithMaintenanceSettings(ms contracts.MaintenanceSettings) IndexerOpt

WithMaintenanceSettings allows for passing maintenance settings to the indexer

func WithSlabOptions

func WithSlabOptions(opts ...slabs.Option) IndexerOpt

WithSlabOptions allows for passing slab options to the indexer

type Syncer

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

Syncer is a wrapper around the syncer.Syncer that doesn't fail broadcasts when no peers are available.

func NewSyncer

func NewSyncer(t testing.TB, genesis types.BlockID, cm *chain.Manager) *Syncer

NewSyncer creates a new Syncer for testing.

func (*Syncer) Addr

func (s *Syncer) Addr() string

Addr returns the address of the Syncer.

func (*Syncer) BroadcastV2TransactionSet

func (s *Syncer) BroadcastV2TransactionSet(index types.ChainIndex, txns []types.V2Transaction) error

BroadcastV2TransactionSet broadcasts a v2 transaction set to all peers.

func (*Syncer) Close

func (s *Syncer) Close() error

Close closes the Syncer's net.Listener.

func (*Syncer) Connect

func (s *Syncer) Connect(ctx context.Context, addr string) (*syncer.Peer, error)

Connect forms an outbound connection to a peer.

func (*Syncer) Peers

func (s *Syncer) Peers() []*syncer.Peer

Peers returns the set of currently-connected peers.

type TestStore

type TestStore struct {
	*postgres.Store
	// contains filtered or unexported fields
}

TestStore represents a postgres database for testing with some helpers for common actions in testing like adding accounts and running SQL queries.

func NewDB

func NewDB(t testing.TB, maintenanceSettings contracts.MaintenanceSettings, log *zap.Logger) TestStore

NewDB creates a new postgres database for testing.

func (TestStore) AddTestAccount

func (ts TestStore) AddTestAccount(t testing.TB, ak types.PublicKey)

AddTestAccount adds an account to the database for testing.

func (TestStore) AddTestHost

func (ts TestStore) AddTestHost(t testing.TB, host hosts.Host)

AddTestHost adds a host to the database for testing.

func (TestStore) Exec

func (ts TestStore) Exec(ctx context.Context, query string, args ...any) (pgconn.CommandTag, error)

Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (TestStore) Query

func (ts TestStore) Query(ctx context.Context, query string, args ...any) (pgx.Rows, error)

Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (TestStore) QueryRow

func (ts TestStore) QueryRow(ctx context.Context, query string, args ...any) pgx.Row

QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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