mock

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: Apache-2.0 Imports: 45 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLostBlock is returned if a block was removed from the cache.
	ErrLostBlock = errors.New("lost block")
)

Functions

func RequireStreams added in v0.1.9

func RequireStreams[T any, S StreamFetcher[T]](t *testing.T, manager S, expectedNumStreams int,
) []*T

RequireStreams ensures that there are a specified number of active streams.

func RequireStreamsWithEndpoints added in v0.1.9

func RequireStreamsWithEndpoints[T any, S StreamFetcher[T]](
	t *testing.T, manager S, expectedNumStreams int, endpoints ...string,
) []*T

RequireStreamsWithEndpoints ensures that there are a specified number of active streams via a specified endpoint.

func StartMockCoordinatorServiceFromServerConfig added in v0.1.8

func StartMockCoordinatorServiceFromServerConfig(
	t *testing.T,
	coordService *Coordinator,
	sc *connection.ServerConfig,
) *test.GrpcServers

StartMockCoordinatorServiceFromServerConfig starts a mock coordinator service using the given config.

func StartMockVCServiceFromServerConfig added in v0.1.8

func StartMockVCServiceFromServerConfig(
	t *testing.T, vc *VcService, sc ...*connection.ServerConfig,
) *test.GrpcServers

StartMockVCServiceFromServerConfig starts a specified number of mock vc service.

func StartMockVerifierServiceFromServerConfig added in v0.1.8

func StartMockVerifierServiceFromServerConfig(
	t *testing.T, verifier *Verifier, sc ...*connection.ServerConfig,
) *test.GrpcServers

StartMockVerifierServiceFromServerConfig starts a specified number of mock verifier service.

Types

type BlockWithConsenters added in v0.1.9

type BlockWithConsenters struct {
	Block             *common.Block
	ConsenterMetadata []byte
	ConsenterSigners  []msp.SigningIdentity
}

BlockWithConsenters is used to submit a new config block with its consenters.

type Coordinator

type Coordinator struct {
	servicepb.CoordinatorServer
	// contains filtered or unexported fields
}

Coordinator is a mock implementation of servicepb.CoordinatorServer.

func NewMockCoordinator

func NewMockCoordinator() *Coordinator

NewMockCoordinator creates a new mock coordinator.

func StartMockCoordinatorService

func StartMockCoordinatorService(t *testing.T, p test.StartServerParameters) (
	*Coordinator, *test.GrpcServers,
)

StartMockCoordinatorService starts a mock coordinator service and registers cancellation.

func (*Coordinator) BlockProcessing

func (c *Coordinator) BlockProcessing(stream servicepb.Coordinator_BlockProcessingServer) error

BlockProcessing processes a block.

func (*Coordinator) GetNextBlockNumberToCommit added in v0.1.8

func (c *Coordinator) GetNextBlockNumberToCommit(
	context.Context,
	*emptypb.Empty,
) (*servicepb.BlockRef, error)

GetNextBlockNumberToCommit returns the next expected block number to be received by the coordinator.

func (*Coordinator) GetTransactionsStatus

func (c *Coordinator) GetTransactionsStatus(
	_ context.Context,
	q *committerpb.TxIDsBatch,
) (*committerpb.TxStatusBatch, error)

GetTransactionsStatus returns the status of given set of transaction identifiers.

func (*Coordinator) IsStreamActive

func (c *Coordinator) IsStreamActive() bool

IsStreamActive returns true if the stream from the sidecar is active.

func (*Coordinator) NumberOfWaitingTransactionsForStatus

func (c *Coordinator) NumberOfWaitingTransactionsForStatus(
	context.Context,
	*emptypb.Empty,
) (*servicepb.WaitingTransactions, error)

NumberOfWaitingTransactionsForStatus returns the number of transactions waiting to get the final status.

func (*Coordinator) RegisterService added in v0.1.5

func (c *Coordinator) RegisterService(server *grpc.Server)

RegisterService registers for the coordinator's GRPC services.

func (*Coordinator) SetDelay

func (c *Coordinator) SetDelay(d time.Duration)

SetDelay sets the duration to wait before sending statuses.

func (*Coordinator) SetLastCommittedBlockNumber

func (c *Coordinator) SetLastCommittedBlockNumber(
	_ context.Context, lastBlock *servicepb.BlockRef,
) (*emptypb.Empty, error)

SetLastCommittedBlockNumber sets the last committed block number.

func (*Coordinator) SetWaitingTxsCount

func (c *Coordinator) SetWaitingTxsCount(count int32)

SetWaitingTxsCount sets the waiting transactions count. The purpose of this method is to set the count manually for testing purpose.

type Orderer

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

Orderer supports running multiple mock-orderer services which mocks a consortium.

func NewMockOrderer

func NewMockOrderer(config *OrdererConfig) (*Orderer, error)

NewMockOrderer creates multiple orderer instances.

func (*Orderer) Broadcast

func (o *Orderer) Broadcast(stream ab.AtomicBroadcast_BroadcastServer) error

Broadcast receives TXs and returns ACKs.

func (*Orderer) CutBlock

func (o *Orderer) CutBlock(ctx context.Context) bool

CutBlock allows forcing block cut for testing other packages.

func (*Orderer) Deliver

func (o *Orderer) Deliver(stream ab.AtomicBroadcast_DeliverServer) error

Deliver receives a seek request and returns a stream of the orderered blocks.

func (*Orderer) GetBlock

func (o *Orderer) GetBlock(ctx context.Context, blockNum uint64) (*common.Block, error)

GetBlock allows fetching blocks directly for testing other packages.

func (*Orderer) RegisterPartyState added in v0.1.9

func (o *Orderer) RegisterPartyState(address string, partyState *PartyState)

RegisterPartyState registered a persistent party state for the server. It allows configuring a shared, persistent party state between multiple streams according to the stream's server endpoint. This allows uniform behavior for each mock server.

func (*Orderer) RegisterService added in v0.1.5

func (o *Orderer) RegisterService(server *grpc.Server)

RegisterService registers for the orderer's GRPC services.

func (*Orderer) Run

func (o *Orderer) Run(ctx context.Context) error

Run collects the envelopes, cuts the blocks, and store them to the block cache.

func (*Orderer) StreamsStates added in v0.1.9

func (s *Orderer) StreamsStates() []*T

StreamsStates returns the current active streams in the orderer they were created.

func (*Orderer) StreamsStatesByServerEndpoints added in v0.1.9

func (s *Orderer) StreamsStatesByServerEndpoints(endpoints ...string) []*T

StreamsStatesByServerEndpoints returns the current active streams that was accessed using one of the specific endpoints, in the orderer they were created.

func (*Orderer) SubmitBlock

func (o *Orderer) SubmitBlock(ctx context.Context, b *common.Block) error

SubmitBlock allows submitting blocks directly for testing other packages. The block header will be replaced with a generated header.

func (*Orderer) SubmitBlockWithConsenters added in v0.1.9

func (o *Orderer) SubmitBlockWithConsenters(ctx context.Context, newConfig *BlockWithConsenters) error

SubmitBlockWithConsenters allows submitting config-blocks (with crypto) directly for testing other packages. The block header will be replaced with a generated header.

func (*Orderer) SubmitEnv

func (o *Orderer) SubmitEnv(ctx context.Context, e *common.Envelope) bool

SubmitEnv allows submitting envelops directly for testing other packages. It blocks until the envelope is collected by the Run goroutine.

func (*Orderer) WaitForReady

func (*Orderer) WaitForReady(context.Context) bool

WaitForReady returns true when we are ready to process GRPC requests.

type OrdererConfig

type OrdererConfig struct {
	// Server and ServerConfigs sets the used serving endpoints.
	// We support both for compatibility with other services.
	Server           *connection.ServerConfig   `mapstructure:"server"`
	ServerConfigs    []*connection.ServerConfig `mapstructure:"servers"`
	BlockSize        int                        `mapstructure:"block-size"`
	BlockTimeout     time.Duration              `mapstructure:"block-timeout"`
	OutBlockCapacity int                        `mapstructure:"out-block-capacity"`
	PayloadCacheSize int                        `mapstructure:"payload-cache-size"`
	ArtifactsPath    string                     `mapstructure:"artifacts-path"`
	SendGenesisBlock bool                       `mapstructure:"send-genesis-block"`

	// TestServerParameters is only used for internal testing.
	TestServerParameters test.StartServerParameters
}

OrdererConfig configuration for the mock orderer.

type OrdererStreamState added in v0.1.9

type OrdererStreamState struct {
	StreamInfo
	*PartyState
	DataBlockStream bool
}

OrdererStreamState holds the streams state.

func (*OrdererStreamState) String added in v0.1.9

func (s *OrdererStreamState) String() string

String outputs a human-readable identifier for this stream.

type OrdererTestEnv

type OrdererTestEnv struct {
	OrdererTestParameters
	OrdererConnConfig ordererdial.Config
	Orderer           *Orderer
	PrevBlock         *common.Block
	PartyStates       map[uint32]*PartyState
	AllServerConfig   []*connection.ServerConfig
	AllServers        []*grpc.Server
	AllEndpoints      []*commontypes.OrdererEndpoint
}

OrdererTestEnv prepares and controls the environment for testing the operation with the orderer.

func NewOrdererTestEnv

func NewOrdererTestEnv(t *testing.T, p *OrdererTestParameters) *OrdererTestEnv

NewOrdererTestEnv creates and starts a new OrdererTestEnv.

func (*OrdererTestEnv) EndpointsOfID added in v0.2.0

func (e *OrdererTestEnv) EndpointsOfID(id uint32) []*commontypes.OrdererEndpoint

EndpointsOfID returns the OrdererEndpoint of a party ID.

func (*OrdererTestEnv) GetBlock added in v0.2.0

func (e *OrdererTestEnv) GetBlock(t *testing.T, blockNum uint64) *common.Block

GetBlock returns a block number.

func (*OrdererTestEnv) StartServers added in v0.2.0

func (e *OrdererTestEnv) StartServers(t *testing.T)

StartServers starts the servers for the orderer and maps them to their respective IDs.

func (*OrdererTestEnv) StopServers added in v0.2.0

func (e *OrdererTestEnv) StopServers()

StopServers stops the servers and closes their pre allocated listeners.

func (*OrdererTestEnv) StopServersOfID added in v0.2.0

func (e *OrdererTestEnv) StopServersOfID(id uint32)

StopServersOfID stop the servers of a party ID and closes their pre allocated listeners.

func (*OrdererTestEnv) Submit added in v0.2.0

func (e *OrdererTestEnv) Submit(
	t *testing.T, outputBlocks <-chan *common.Block,
) *common.Block

Submit submits a block and verify its delivery.

func (*OrdererTestEnv) SubmitConfigBlock

SubmitConfigBlock creates and submits a config block.

func (*OrdererTestEnv) WaitForBlock added in v0.2.0

func (e *OrdererTestEnv) WaitForBlock(t *testing.T, outputBlocks <-chan *common.Block) *common.Block

WaitForBlock waits for a block on the channel.

type OrdererTestParameters added in v0.2.0

type OrdererTestParameters struct {
	ArtifactsPath         string
	ChanID                string
	NumIDs                uint32
	ServerPerID           int
	PeerOrganizationCount uint32
	OrdererConfig         *OrdererConfig
	ServerTLSConfig       connection.TLSConfig
	ClientTLSConfig       connection.TLSConfig

	// NewOrdererTestEnv prepares the environment for the entire test.
	// However, the test might choose to start with a subset of the IDs, then
	// add the others as the test progresses.
	// We want to allow this without having to create servers in flight.
	// InitialNumIDs determines the initial number of IDs, to choose for the genesis block.
	// If not initialized, the default is all the IDs.
	InitialNumIDs uint32
}

OrdererTestParameters describes the parameters for OrdererTestEnv. Some fields are required, while others have default values if not set. See NewOrdererTestEnv for details.

func (*OrdererTestParameters) ConfigBlockPath added in v0.2.0

func (p *OrdererTestParameters) ConfigBlockPath() string

ConfigBlockPath is a convenient way to get the config block path.

type PartyState added in v0.1.9

type PartyState struct {
	PartyID       uint32
	HoldFromBlock atomic.Uint64
	ReplaceBlock  utils.SyncMap[uint64, *common.Block]
}

PartyState holds the shared state of all streams of a party. HoldFromBlock will hold the blocks starting from this number. If HoldFromBlock is 0, no blocks will be held. ReplaceBlock holds the blocks to replace, with the block number as the key. This behavior is permanent until the entry is removed from the ReplaceBlock map.

type StreamFetcher added in v0.1.9

type StreamFetcher[T any] interface {
	StreamsStates() []*T
	StreamsStatesByServerEndpoints(endpoint ...string) []*T
}

StreamFetcher is used by RequireStreams/RequireStreamsWithEndpoints.

type StreamInfo added in v0.1.9

type StreamInfo struct {
	Index          uint64
	ServerEndpoint string
	ClientEndpoint string
}

StreamInfo holds the information of a mock stream.

type VCStreamState added in v0.1.9

type VCStreamState struct {
	StreamInfo
	// contains filtered or unexported fields
}

VCStreamState holds the stream's batch queue.

type VcService

type VcService struct {
	servicepb.ValidationAndCommitServiceServer

	// NumBatchesReceived is the number of batches received by VcService.
	NumBatchesReceived atomic.Uint32
	// MockFaultyNodeDropSize allows mocking a faulty node by dropping some TXs.
	MockFaultyNodeDropSize int
	// contains filtered or unexported fields
}

VcService is a mock implementation of servicepb.ValidationAndCommitServiceServer. It is used for testing the client which is the coordinator service.

func NewMockVcService

func NewMockVcService() *VcService

NewMockVcService returns a new VcService.

func StartMockVCService

func StartMockVCService(t *testing.T, p test.StartServerParameters) (*VcService, *test.GrpcServers)

StartMockVCService starts a specified number of mock VC service using the same shared instance. It is used for testing when multiple VC services are required to share the same state.

func (*VcService) GetConfigTransaction

func (*VcService) GetConfigTransaction(
	context.Context,
	*emptypb.Empty,
) (*applicationpb.ConfigTransaction, error)

GetConfigTransaction is a mock implementation of the protovcservice.GetConfigTransaction.

func (*VcService) GetNamespacePolicies

func (*VcService) GetNamespacePolicies(
	context.Context,
	*emptypb.Empty,
) (*applicationpb.NamespacePolicies, error)

GetNamespacePolicies is a mock implementation of the protovcservice.GetNamespacePolicies.

func (*VcService) GetNextBlockNumberToCommit added in v0.1.8

func (v *VcService) GetNextBlockNumberToCommit(
	context.Context,
	*emptypb.Empty,
) (*servicepb.BlockRef, error)

GetNextBlockNumberToCommit get the next expected block number in the database/ledger.

func (*VcService) GetTransactionsStatus

func (v *VcService) GetTransactionsStatus(
	_ context.Context,
	query *committerpb.TxIDsBatch,
) (*committerpb.TxStatusBatch, error)

GetTransactionsStatus get the status for a given set of transactions IDs.

func (*VcService) RegisterService added in v0.1.5

func (v *VcService) RegisterService(server *grpc.Server)

RegisterService registers for the validator-committer's GRPC services.

func (*VcService) SetLastCommittedBlockNumber

func (v *VcService) SetLastCommittedBlockNumber(
	_ context.Context,
	lastBlock *servicepb.BlockRef,
) (*emptypb.Empty, error)

SetLastCommittedBlockNumber set the last committed block number in the database/ledger.

func (*VcService) SetupSystemTablesAndNamespaces

func (*VcService) SetupSystemTablesAndNamespaces(
	context.Context,
	*emptypb.Empty,
) (*emptypb.Empty, error)

SetupSystemTablesAndNamespaces creates the required system tables and namespaces.

func (*VcService) StartValidateAndCommitStream

StartValidateAndCommitStream is the mock implementation of the [protovcservice.ValidationAndCommitServiceServer] interface.

func (*VcService) StreamsStates added in v0.1.9

func (s *VcService) StreamsStates() []*T

StreamsStates returns the current active streams in the orderer they were created.

func (*VcService) StreamsStatesByServerEndpoints added in v0.1.9

func (s *VcService) StreamsStatesByServerEndpoints(endpoints ...string) []*T

StreamsStatesByServerEndpoints returns the current active streams that was accessed using one of the specific endpoints, in the orderer they were created.

func (*VcService) SubmitTransactions

func (v *VcService) SubmitTransactions(ctx context.Context, txsBatch *servicepb.VcBatch) error

SubmitTransactions enqueues the given transactions to a queue read by status sending goroutine. This method helps the test code to bypass the stream to submit transactions to the mock vcservice.

type Verifier added in v0.1.8

type Verifier struct {
	servicepb.UnimplementedVerifierServer
	// contains filtered or unexported fields
}

Verifier is a mock implementation of servicepb.VerifierServer. Verifier marks valid and invalid flag as follows: - when the tx has empty signature, it is invalid. - when the tx has non-empty signature, it is valid.

func NewMockSigVerifier

func NewMockSigVerifier() *Verifier

NewMockSigVerifier returns a new mock verifier.

func StartMockVerifierService added in v0.1.8

func StartMockVerifierService(t *testing.T, p test.StartServerParameters) (
	*Verifier, *test.GrpcServers,
)

StartMockVerifierService starts a specified number of mock verifier service and register cancellation.

func (*Verifier) RegisterService added in v0.1.8

func (m *Verifier) RegisterService(server *grpc.Server)

RegisterService registers for the verifier's GRPC services.

func (*Verifier) StartStream added in v0.1.8

func (m *Verifier) StartStream(stream servicepb.Verifier_StartStreamServer) error

StartStream is a mock implementation of the [protosignverifierservice.VerifierServer].

func (*Verifier) StreamsStates added in v0.1.9

func (s *Verifier) StreamsStates() []*T

StreamsStates returns the current active streams in the orderer they were created.

func (*Verifier) StreamsStatesByServerEndpoints added in v0.1.9

func (s *Verifier) StreamsStatesByServerEndpoints(endpoints ...string) []*T

StreamsStatesByServerEndpoints returns the current active streams that was accessed using one of the specific endpoints, in the orderer they were created.

type VerifierStreamState added in v0.1.8

type VerifierStreamState struct {
	StreamInfo

	// Updates contains all the updates received.
	Updates atomic.Pointer[[]*servicepb.VerifierUpdates]
	// NumBlocksReceived is the number of blocks received by the mock verifier.
	NumBlocksReceived atomic.Uint64
	// PolicyUpdateCounter is the number of policy updates, used to check progress.
	PolicyUpdateCounter atomic.Uint64
	// ReturnErrForUpdatePolicies configures the Verifier to return an error during policy updates.
	// When true, the verifier will signal an error during the update policies process.
	// It is a one time event, after which the flag will return to false.
	ReturnErrForUpdatePolicies atomic.Bool
	// MockFaultyNodeDropSize allows mocking a faulty node by dropping some TXs.
	MockFaultyNodeDropSize atomic.Uint64
	// contains filtered or unexported fields
}

VerifierStreamState holds the state of a verifier stream.

Jump to

Keyboard shortcuts

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