client

package
v1.0.0-mx.1 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

Client

This package provides a client contract for the multiplex library.

An Acceptor instance is injected in a Client to perform pre-committing verification of transactions data. If the acceptor method returns an error, the transactions data must be discarded entirely.

A [Client] instance may be used to perform on-demand consensus instances using a predefined list of active relays running a cometbft network. Transactions that are broadcast will be first broadcast to other relays, then verified, before they are committed to a network.

Interfaces

  • Transaction: A transaction consists of an object with Data and Fingerprint.
  • Acceptor: Defines the contract for client-side transactions verification.
  • Client: Defines the contract for multiplex client implementations.

BroadcastTx

  1. It is expected that any transaction batch forwarded to Client#BroadcastTx must have been previously accepted using Acceptor#AcceptBroadcastTx locally.

  2. Transaction batches may concern one or more than one network, which must all exist by the time the transaction gets full acceptance of the relays.

  3. A broadcast operation is built around a list of predefined relays, of which a majority must be healthy, a user address and a transactions batch.

  4. A complete consensus instance must succeed before a transaction batch may be committed, such that all relays effectively agree to persist the batch.

Testing

You can test the client package using the following unit test suite:

go test github.com/ice-blockchain/cometbft/multiplex/client -test.v -count=1

Note that this test suite is apart from the client package and implemented in a client_test package instead.

Documentation

Overview

This package provides a client contract for the multiplex library.

An Acceptor instance is injected in a Client to perform pre-committing verification of transactions data. If the acceptor method returns an error, the transactions data must be discarded entirely.

A Client instance may be used to perform on-demand consensus instances using a predefined list of active relays running a cometbft network. Transactions that are broadcast will be first broadcast to other relays, then verified, before they are committed to a network.

Interfaces

  • Transaction: A transaction consists of an object with Data and Fingerprint.
  • Acceptor: Defines the contract for client-side transactions verification.
  • Client: Defines the contract for multiplex client implementations.

BroadcastTx

1. It is expected that any transaction batch forwarded to [Client#BroadcastTx] must have been previously accepted using [Acceptor#AcceptBroadcastTx] locally.

2. Transaction batches may concern one or more than one network, which must all exist by the time the transaction gets full acceptance of the relays.

3. A broadcast operation is built around a list of predefined relays, of which a majority must be healthy, a user address and a transactions batch.

4. A complete consensus instance must succeed before a transaction batch may be committed, such that all relays effectively agree to persist the batch.

Testing

You can test the client package using the following unit test suite:

```bash go test github.com/ice-blockchain/cometbft/multiplex/client -test.v -count=1 ```

Note that this test suite is apart from the `client` package and implemented in a `client_test` package instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetChainID

func GetChainID(userAddress string, fingerprint string) string

GetChainID returns ChainID from a user address and fingerprint.

func GetFingerprint

func GetFingerprint(plaintext string) string

GetFingerprint returns a hash which consists of the first 8-bytes of a SHA-256 of the plaintext input.

func PubKeyToAddress

func PubKeyToAddress(masterPubKey string) (string, error)

PubKeyToAddress parses a master public key (ed25519) and returns the resulting compressed address format (20 bytes).

An error is returned if the master public key cannot be parsed as a crypto.PubKey instance. Note that we forcefully use `ed25519` public keys to permit batch verifications down the line.

func TransactionToRawTx

func TransactionToRawTx(transaction Transaction) types.Tx

TransactionToRawTx converts a Transaction to a cometbft transaction base type `types.Tx` which consists of the raw transaction with its hash.

Types

type Acceptor

type Acceptor interface {
	// AcceptBroadcastTx returns an error if any of the transactions
	// should not be accepted, or if the batch must not be broadcast.
	AcceptBroadcastTx(
		ctx context.Context,
		userAddress string,
		transactions ...Transaction,
	) error

	// AcceptBroadcastTxRemoval returns an error if any of the transactions
	// should not be accepted, or if the batch must not be broadcast.
	AcceptBroadcastTxRemoval(
		ctx context.Context,
		userAddress string,
		transactions ...Transaction,
	) error

	// RollbackTx should execute custom business logic such as removing data
	// previously committed for a transaction batch that is being rollbacked.
	RollbackTx(
		ctx context.Context,
		userAddress string,
		transactions ...Transaction,
	) error

	// RollbackTxRemoval should execute custom business logic such as removing
	// data previously committed for a removal operation that is rollbacked.
	RollbackTxRemoval(
		ctx context.Context,
		userAddress string,
		transactions ...Transaction,
	) error
}

Acceptor defines the contract for client-side transactions verification.

An acceptor instance is injected in a Client to perform pre-committing verification of transactions data. If the acceptor method returns an error, the transactions data must be discarded entirely.

The acceptor shall always receive a batch of transactions which may concern one or more than one independent cometbft network.

See also: Client.

type BroadcastStatus

type BroadcastStatus struct {
	Error    error
	TxHashes [][]byte
}

BroadcastStatus defines a wrapper for transaction broadcast status which may contain an error and metadata about the current step.

type Client

type Client interface {
	// BroadcastTx sends an error to a notifier if any of the transactions
	// fails basic verification, or if we fail to get a majority approval
	// for the broadcast operation from healthy relays.
	//
	// This method should broadcast the transactions to all other relays and
	// iff the calls to [Acceptor#AcceptBroadcastTx] by relays are successful,
	// it should commit the transactions data.
	// If commitment does not succeed for any reason outside of the scope of
	// acceptance, e.g. hd failure, this method calls [Acceptor#RollbackTx]
	// and broadcasts a RollbackTxs message to other relays' mempool reactor.
	//
	// If any error happens during the broadcast process, the transactions
	// data must be discarded.
	// Otherwise, it should eventually persist the transactions data using
	// active relays of one or many networks. The ChainID used for persisting
	// data must be built using the userAddress and the [Transaction#Fingerprint].
	BroadcastTx(
		ctx context.Context,
		userAddress string,
		relays []string,
		notifier chan<- BroadcastStatus,
		transactions ...Transaction,
	)

	// BroadcastTxRemoval sends an error to a notifier if any of the removal
	// operations fail verification, or if we fail to get a majority approval
	// for the broadcast operation from healthy relays.
	//
	// This method should broadcast the remove operations to all other relays
	// and iff the calls to [Acceptor#AcceptBroadcastTxRemoval] by relays are
	// successful, it should completely remove the persisted data.
	//
	// If any error happens during the broadcast process or during verification,
	// the removal operations data must be discarded.
	// Otherwise, it should eventually persist the removal operations data using
	// active relays of so-called *removal history networks*. The ChainID used
	// for persisting removal operations must be built using the userAddress
	// and the [Transaction#Fingerprint] will be prefixed with `delete_`.
	BroadcastTxRemoval(
		ctx context.Context,
		userAddress string,
		relays []string,
		notifier chan<- BroadcastStatus,
		transactions ...Transaction,
	)
}

Client defines the contract for multiplex client implementations.

A client instance may be used to perform on-demand consensus instances using a predefined list of active relays running a cometbft network.

Transactions that are broadcast using a client instance will be broadcast to other relays, then verified, before they are committed to a network.

See also: Acceptor.

type ContextKey

type ContextKey string

ContextKey defines a string-based context value key.

const (
	KeyAddress ContextKey = "Address"
	KeyChainID ContextKey = "ChainID"
)

We will attach a user address and ChainID inside the context.

type DefaultAcceptor

type DefaultAcceptor struct{}

func (DefaultAcceptor) AcceptBroadcastTx

func (DefaultAcceptor) AcceptBroadcastTx(
	_ context.Context,
	_ string,
	_ ...Transaction,
) error

AcceptBroadcastTx returns an error if any of the transactions should not be accepted, or if the batch must not be broadcast.

func (DefaultAcceptor) AcceptBroadcastTxRemoval

func (DefaultAcceptor) AcceptBroadcastTxRemoval(
	_ context.Context,
	_ string,
	_ ...Transaction,
) error

AcceptBroadcastTxRemoval returns an error if any of the transactions should not be accepted, or if the batch must not be broadcast.

func (DefaultAcceptor) RollbackTx

func (DefaultAcceptor) RollbackTx(
	_ context.Context,
	_ string,
	_ ...Transaction,
) error

RollbackTx should execute custom business logic such as removing data previously committed for a transaction batch that is being rollbacked.

func (DefaultAcceptor) RollbackTxRemoval

func (DefaultAcceptor) RollbackTxRemoval(
	_ context.Context,
	_ string,
	_ ...Transaction,
) error

RollbackTxRemoval should execute custom business logic such as removing data previously committed for a removal operation that is rollbacked.

type DefaultClient

type DefaultClient struct{}

func (DefaultClient) BroadcastTx

func (DefaultClient) BroadcastTx(
	_ context.Context,
	_ string,
	_ []string,
	_ chan<- BroadcastStatus,
	_ ...Transaction,
)

BroadcastTx sends a status to a notifier if any of the transactions fails basic verification, or if we fail to get a majority approval for the broadcast operation from healthy relays.

func (DefaultClient) BroadcastTxRemoval

func (DefaultClient) BroadcastTxRemoval(
	_ context.Context,
	_ string,
	_ []string,
	_ chan<- BroadcastStatus,
	_ ...Transaction,
)

BroadcastTxRemoval sends a status to a notifier if any of the removal operations fail verification, or if we fail to get a majority approval for the broadcast operation from healthy relays.

type DefaultServer

type DefaultServer struct{}

func (DefaultServer) Close

func (DefaultServer) Close() error

Close implements io.Closer

func (DefaultServer) GetAcceptor

func (DefaultServer) GetAcceptor() Acceptor

GetAcceptor returns the injected Acceptor implementation.

func (DefaultServer) MustStart

func (DefaultServer) MustStart()

MustStart must start a replication backend or return an error.

type Server

type Server interface {
	io.Closer

	// GetAcceptor returns the injected [Acceptor] implementation.
	GetAcceptor() Acceptor

	// MustStart executes a replication backend.
	MustStart()
}

Server defines the contract for replication backend implementations.

A server instance must be started before replication can happen and before broadcast operations can be forwarded to the Client.

type Transaction

type Transaction struct {
	Data        []byte
	Fingerprint string
}

Transaction defines a wrapper for data attached to a fingerprint.

Note that transaction fingerprints *may* hold plaintext scope names, e.g. "posts", "likes". Privacy shall not be a concern here because the ChainID is built using the user address and a *fingerprint hash*.

func RawTxToTransaction

func RawTxToTransaction(transaction types.Tx) Transaction

RawTxToTransaction converts a cometbft transaction to a Transaction.

Jump to

Keyboard shortcuts

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