chainservice

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2023 License: Apache-2.0, MIT Imports: 27 Imported by: 0

Documentation

Overview

Package chainservice is a chain service responsible for submitting blockchain transactions and relaying blockchain events.

Index

Constants

View Source
const (
	MIN_BACKOFF_TIME = 1 * time.Second
	MAX_BACKOFF_TIME = 5 * time.Minute
)
View Source
const MAX_EPOCHS = 60480

MAX_EPOCHS is the maximum range of old epochs we can query with a single "FilterLogs" request This is a restriction enforced by the rpc provider

View Source
const MAX_QUERY_BLOCK_RANGE = 2000

MAX_QUERY_BLOCK_RANGE is the maximum range of blocks we query for events at once. Most json-rpc nodes restrict the amount of blocks you can search. For example Wallaby supports a maximum range of 2880 See https://github.com/Zondax/rosetta-filecoin/blob/b395b3e04401be26c6cdf6a419e14ce85e2f7331/tools/wallaby/files/config.toml#L243

View Source
const REQUIRED_BLOCK_CONFIRMATIONS = 2

REQUIRED_BLOCK_CONFIRMATIONS is how many blocks must be mined before an emitted event is processed

View Source
const RESUB_INTERVAL = 15 * time.Second

RESUB_INTERVAL is how often we resubscribe to log events. We do this to avoid https://github.com/ethereum/go-ethereum/issues/23845 We use 2.5 minutes as the default filter timeout is 5 minutes. See https://github.com/ethereum/go-ethereum/blob/e14164d516600e9ac66f9060892e078f5c076229/eth/filters/filter_system.go#L43 This has been reduced to 15 seconds to support local devnets with much shorter timeouts.

View Source
const TEST_CHAIN_ID = 1337

This is the chain id used by the simulated backend as well as hardhat

Variables

View Source
var ErrUnableToAssignBigInt = errors.New("simulated_backend_chainservice: unable to assign BigInt")

Functions

func NewEventTracker added in v0.1.1

func NewEventTracker(startBlock uint64) *eventTracker

func SetupSimulatedBackend

func SetupSimulatedBackend(numAccounts uint64) (SimulatedChain, Bindings, []*bind.TransactOpts, error)

SetupSimulatedBackend creates a new SimulatedBackend with the supplied number of transacting accounts, deploys the Nitro Adjudicator and returns both.

Types

type AllocationUpdatedEvent

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

AllocationUpdated is an internal representation of the AllocationUpdated blockchain event The event includes the token address and amount at the block that generated the event

func NewAllocationUpdatedEvent

func NewAllocationUpdatedEvent(channelId types.Destination, blockNum uint64, txIndex uint, assetAddress common.Address, assetAmount *big.Int) AllocationUpdatedEvent

func (AllocationUpdatedEvent) BlockNum

func (ce AllocationUpdatedEvent) BlockNum() uint64

func (AllocationUpdatedEvent) ChannelID

func (ce AllocationUpdatedEvent) ChannelID() types.Destination

func (AllocationUpdatedEvent) String

func (aue AllocationUpdatedEvent) String() string

func (AllocationUpdatedEvent) TxIndex added in v0.1.1

func (ce AllocationUpdatedEvent) TxIndex() uint

type BackendWrapper

type BackendWrapper struct {
	*backends.SimulatedBackend
}

This is used to wrap the simulated backend so that we can provide a ChainID function like a real eth client

func (*BackendWrapper) ChainID

func (b *BackendWrapper) ChainID(ctx context.Context) (*big.Int, error)

type Bindings

type Bindings struct {
	Adjudicator       binding[NitroAdjudicator.NitroAdjudicator]
	Token             binding[Token.Token]
	ConsensusApp      binding[ConsensusApp.ConsensusApp]
	VirtualPaymentApp binding[VirtualPaymentApp.VirtualPaymentApp]
}

type ChainEventHandler

type ChainEventHandler interface {
	UpdateWithChainEvent(event Event) (protocols.Objective, error)
}

ChainEventHandler describes an objective that can handle chain events

type ChainOpts added in v0.1.2

type ChainOpts struct {
	ChainUrl        string
	ChainStartBlock uint64
	ChainAuthToken  string
	ChainPk         string
	NaAddress       common.Address
	VpaAddress      common.Address
	CaAddress       common.Address
}

type ChainService

type ChainService interface {
	// EventFeed returns a chan for receiving events from the chain service.
	EventFeed() <-chan Event
	// SendTransaction is for sending transactions with the chain service
	SendTransaction(protocols.ChainTransaction) error
	// GetConsensusAppAddress returns the address of a deployed ConsensusApp (for ledger channels)
	GetConsensusAppAddress() types.Address
	// GetVirtualPaymentAppAddress returns the address of a deployed VirtualPaymentApp
	GetVirtualPaymentAppAddress() types.Address
	// GetChainId returns the id of the chain the service is connected to
	GetChainId() (*big.Int, error)
	// GetLastConfirmedBlockNum returns the highest blockNum that satisfies the chainservice's REQUIRED_BLOCK_CONFIRMATIONS
	GetLastConfirmedBlockNum() uint64
	// Close closes the ChainService
	Close() error
}

func NewEthChainService

func NewEthChainService(chainOpts ChainOpts) (ChainService, error)

NewEthChainService is a convenient wrapper around newEthChainService, which provides a simpler API

func NewSimulatedBackendChainService

func NewSimulatedBackendChainService(sim SimulatedChain, bindings Bindings,
	txSigner *bind.TransactOpts,
) (ChainService, error)

NewSimulatedBackendChainService constructs a chain service that submits transactions to a NitroAdjudicator and listens to events from an eventSource

type ChallengeRegisteredEvent

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

func NewChallengeRegisteredEvent

func NewChallengeRegisteredEvent(
	channelId types.Destination,
	blockNum uint64,
	txIndex uint,
	variablePart state.VariablePart,
	sigs []state.Signature,
) ChallengeRegisteredEvent

NewChallengeRegisteredEvent constructs a ChallengeRegisteredEvent

func (ChallengeRegisteredEvent) BlockNum

func (ce ChallengeRegisteredEvent) BlockNum() uint64

func (ChallengeRegisteredEvent) ChannelID

func (ce ChallengeRegisteredEvent) ChannelID() types.Destination

func (ChallengeRegisteredEvent) Outcome

func (cr ChallengeRegisteredEvent) Outcome() outcome.Exit

Outcome returns the outcome which will have been stored on chain in the adjudicator after the ChallengeRegistered Event fires.

func (ChallengeRegisteredEvent) SignedState

SignedState returns the signed state which will have been stored on chain in the adjudicator after the ChallengeRegistered Event fires.

func (ChallengeRegisteredEvent) StateHash

StateHash returns the statehash stored on chain at the time of the ChallengeRegistered Event firing.

func (ChallengeRegisteredEvent) String

func (cr ChallengeRegisteredEvent) String() string

func (ChallengeRegisteredEvent) TxIndex added in v0.1.1

func (ce ChallengeRegisteredEvent) TxIndex() uint

type ConcludedEvent

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

ConcludedEvent is an internal representation of the Concluded blockchain event

func (ConcludedEvent) BlockNum

func (ce ConcludedEvent) BlockNum() uint64

func (ConcludedEvent) ChannelID

func (ce ConcludedEvent) ChannelID() types.Destination

func (ConcludedEvent) String

func (ce ConcludedEvent) String() string

func (ConcludedEvent) TxIndex added in v0.1.1

func (ce ConcludedEvent) TxIndex() uint

type DepositedEvent

type DepositedEvent struct {
	Asset   types.Address
	NowHeld *big.Int
	// contains filtered or unexported fields
}

DepositedEvent is an internal representation of the deposited blockchain event

func NewDepositedEvent

func NewDepositedEvent(channelId types.Destination, blockNum uint64, txIndex uint, assetAddress common.Address, nowHeld *big.Int) DepositedEvent

func (DepositedEvent) BlockNum

func (ce DepositedEvent) BlockNum() uint64

func (DepositedEvent) ChannelID

func (ce DepositedEvent) ChannelID() types.Destination

func (DepositedEvent) String

func (de DepositedEvent) String() string

func (DepositedEvent) TxIndex added in v0.1.1

func (ce DepositedEvent) TxIndex() uint

type EthChainService

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

eventTracker holds on to events in memory and dispatches an event after required number of confirmations

func (*EthChainService) Close

func (ecs *EthChainService) Close() error

func (*EthChainService) EventFeed

func (ecs *EthChainService) EventFeed() <-chan Event

EventFeed returns the out chan, and narrows the type so that external consumers may only receive on it.

func (*EthChainService) GetChainId

func (ecs *EthChainService) GetChainId() (*big.Int, error)

func (*EthChainService) GetConsensusAppAddress

func (ecs *EthChainService) GetConsensusAppAddress() types.Address

func (*EthChainService) GetLastConfirmedBlockNum added in v0.1.1

func (ecs *EthChainService) GetLastConfirmedBlockNum() uint64

func (*EthChainService) GetVirtualPaymentAppAddress

func (ecs *EthChainService) GetVirtualPaymentAppAddress() types.Address

func (*EthChainService) SendTransaction

func (ecs *EthChainService) SendTransaction(tx protocols.ChainTransaction) error

SendTransaction sends the transaction and blocks until it has been submitted.

type Event

type Event interface {
	ChannelID() types.Destination
	BlockNum() uint64
	TxIndex() uint
}

Event dictates which methods all chain events must implement

type MockChain

type MockChain struct {
	BlockNum uint64
	// contains filtered or unexported fields
}

MockChain mimics the Ethereum blockchain by keeping track of block numbers and account balances in memory. MockChain accepts transactions and broadcasts events.

func NewMockChain

func NewMockChain() *MockChain

NewMockChain creates a new MockChain

func (*MockChain) Close

func (mc *MockChain) Close() error

func (*MockChain) SubmitTransaction

func (mc *MockChain) SubmitTransaction(tx protocols.ChainTransaction) error

SubmitTransaction updates internal state and broadcasts events unlike an ethereum blockchain, MockChain accepts go-nitro protocols.ChainTransaction

func (*MockChain) SubscribeToEvents

func (mc *MockChain) SubscribeToEvents(a types.Address) <-chan Event

SubscribeToEvents creates, stores, and returns a new Event channel that produces all chain Events

type MockChainService

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

MockChainService adheres to the ChainService interface. The constructor accepts a MockChain, which allows multiple clients to share the same, in-memory chain.

func NewMockChainService

func NewMockChainService(chain *MockChain, address common.Address) *MockChainService

NewMockChainService returns a new MockChainService.

func (*MockChainService) Close

func (mc *MockChainService) Close() error

func (*MockChainService) EventFeed

func (mc *MockChainService) EventFeed() <-chan Event

func (*MockChainService) GetChainId

func (mc *MockChainService) GetChainId() (*big.Int, error)

func (*MockChainService) GetConsensusAppAddress

func (mc *MockChainService) GetConsensusAppAddress() types.Address

GetConsensusAppAddress returns the zero address, since the mock chain will not run any application logic.

func (*MockChainService) GetLastConfirmedBlockNum added in v0.1.1

func (mc *MockChainService) GetLastConfirmedBlockNum() uint64

func (*MockChainService) GetVirtualPaymentAppAddress

func (mc *MockChainService) GetVirtualPaymentAppAddress() types.Address

GetVirtualPaymentAppAddress returns the zero address, since the mock chain will not run any application logic.

func (*MockChainService) SendTransaction

func (mc *MockChainService) SendTransaction(tx protocols.ChainTransaction) error

SendTransaction responds to the given tx.

type SimulatedBackendChainService

type SimulatedBackendChainService struct {
	*EthChainService
	// contains filtered or unexported fields
}

SimulatedBackendChainService extends EthChainService to automatically mine a block for every transaction

func (*SimulatedBackendChainService) GetConsensusAppAddress

func (sbcs *SimulatedBackendChainService) GetConsensusAppAddress() types.Address

func (*SimulatedBackendChainService) GetVirtualPaymentAppAddress

func (sbcs *SimulatedBackendChainService) GetVirtualPaymentAppAddress() types.Address

GetVirtualPaymentAppAddress returns the address of a deployed VirtualPaymentApp

func (*SimulatedBackendChainService) SendTransaction

SendTransaction sends the transaction and blocks until it has been mined.

type SimulatedChain

type SimulatedChain interface {
	Commit() common.Hash
	Close() error
	// contains filtered or unexported methods
}

Directories

Path Synopsis
package NitroAdjudicator contains bindings generated from the Nitro Adjudicator solidity contract, as well as utilities for converting client types to on-chain types.
package NitroAdjudicator contains bindings generated from the Nitro Adjudicator solidity contract, as well as utilities for converting client types to on-chain types.

Jump to

Keyboard shortcuts

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