dummy

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package dummy implements a simple Babble application for testing and documentation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DummySocketClient

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

DummySocketClient is a socket implementation of the dummy app. Babble and the app run in separate processes and communicate through TCP sockets using a SocketBabbleProxy and a SocketAppProxy.

func NewDummySocketClient

func NewDummySocketClient(clientAddr string, nodeAddr string, logger *logrus.Entry) (*DummySocketClient, error)

NewDummySocketClient instantiates a DummySocketClient and starts the SocketBabbleProxy

func (*DummySocketClient) SubmitTx

func (c *DummySocketClient) SubmitTx(tx []byte) error

SubmitTx sends a transaction to Babble via the SocketProxy

type InmemDummyClient

type InmemDummyClient struct {
	*inmem.InmemProxy
	// contains filtered or unexported fields
}

InmemDummyClient is an in-memory implementation of the dummy app. It embeds an InmemProxy so automatically implements the AppProxy interface, and can be passed in the Babble constructor directly.

Example
// Start from default Babble configuration.
babbleConfig := config.NewDefaultConfig()

// Create dummy InmemProxy
dummy := NewInmemDummyClient(babbleConfig.Logger())

// Set the proxy in the Babble configuration.
babbleConfig.Proxy = dummy

// Instantiate Babble.
babble := babble.NewBabble(babbleConfig)

// Read in the configuration and initialise the node accordingly.
if err := babble.Init(); err != nil {
	babbleConfig.Logger().Error("Cannot initialize babble:", err)
	os.Exit(1)
}

// The application can submit transactions to Babble using the proxy's
// SubmitTx. Babble will broadcast the transactions to other nodes, run them
// through the consensus algorithm, and eventually call the callback methods
// implemented in the handler.
go func() {
	dummy.SubmitTx([]byte("the test transaction"))
}()

// Run the node aynchronously.
babble.Run()

// Babble reacts to SIGINT (Ctrl + c) and SIGTERM by calling the leave
// method to politely leave a Babble network, but it can also be called
// manually.
defer babble.Node.Leave()
Output:

func NewInmemDummyClient

func NewInmemDummyClient(logger *logrus.Entry) *InmemDummyClient

NewInmemDummyClient instantiates an InmemDummyClient.

func (*InmemDummyClient) GetCommittedTransactions

func (c *InmemDummyClient) GetCommittedTransactions() [][]byte

GetCommittedTransactions returns the state's list of transactions.

type State

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

State represents the state of our dummy application. It implements the ProxyHandler interface for use with an InmemProxy. It doesn't really do anything useful but save and logs block transactions. The state hash is computed by cumulatively hashing transactions together as they come in. Snapshots correspond to the state hash resulting from executing a block's transactions.

func NewState

func NewState(logger *logrus.Entry) *State

NewState creates a new dummy state.

func (*State) CommitHandler

func (a *State) CommitHandler(block hashgraph.Block) (proxy.CommitResponse, error)

CommitHandler implements the ProxyHandler interface. This callback is called Babble to commit a block to the application. Blocks contain transactions that represent commands or messages for the application, and internal transactions that are used internally by Babble to update the peer-set. The application can accept or refuse internal transactions based on custom rules. Here we accept all internal transactions. The commit response contains a state hash that represents the state of the application after applying all the transactions sequentially.

func (*State) GetCommittedTransactions

func (a *State) GetCommittedTransactions() [][]byte

GetCommittedTransactions returns the list of committed transactions

func (*State) RestoreHandler

func (a *State) RestoreHandler(snapshot []byte) ([]byte, error)

RestoreHandler implements the ProxyHandler interface. It is called by Babble to instruct the application to restore its state back to a given snapshot. This is only used when fast-sync is activated.

func (*State) SnapshotHandler

func (a *State) SnapshotHandler(blockIndex int) ([]byte, error)

SnapshotHandler implements the ProxyHandler interface. It is used by Babble to retrieve a snapshot of the application at a specific block index. It is left to the application to keep track of snapshots and to encode/decode state snapshots to and from raw bytes. This handler is only used when fast-sync is activated.

func (*State) StateChangeHandler

func (a *State) StateChangeHandler(state state.State) error

StateChangedHandler implements the ProxyHandler interface. It is called by Babble to notify the application that the node has entered a new state (ex Babbling, Joining, Suspended, etc.).

Jump to

Keyboard shortcuts

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