node

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2025 License: Apache-2.0 Imports: 54 Imported by: 1

README

internal

The packages in this top level internal are only for use by the main module packages and apps like kwild.

These will be part of the kwil-db main module. As such other kwil-db module cannot use them.

Any reusable code that we want to make publicly accessible may be moved out of internal. Doing so means it should be documented for public consumption and supported, while minimizing breaking changes.

Documentation

Index

Constants

View Source
const (
	TopicACKs     = "acks"
	TopicReset    = "reset"
	TopicDiscReq  = "discovery_request"
	TopicDiscResp = "discovery_response"
)
View Source
const (
	ProtocolIDDiscover = peers.ProtocolIDDiscover
	ProtocolIDCrawler  = peers.ProtocolIDCrawler

	ProtocolIDTx          protocol.ID = "/kwil/tx/1.0.0"
	ProtocolIDTxAnn       protocol.ID = "/kwil/txann/1.0.0"
	ProtocolIDBlockHeight protocol.ID = "/kwil/blkheight/1.1.0"
	ProtocolIDBlock       protocol.ID = "/kwil/blk/1.0.0"
	ProtocolIDBlkAnn      protocol.ID = "/kwil/blkann/1.0.0"

	ProtocolIDBlockPropose protocol.ID = "/kwil/blkprop/1.0.0"
)
View Source
const AppVersion = 1

AppVersion encompasses all aspects of the Kwil DB application. A new version indicates incompatible changes to the application, and nodes with different versions should not communicate (TODO).

Variables

View Source
var (
	ErrNotFound        = errors.New("resource not available")
	ErrTxNotFound      = types.ErrTxNotFound
	ErrTxAlreadyExists = types.ErrTxAlreadyExists
	ErrBlkNotFound     = types.ErrBlkNotFound
	ErrNoResponse      = types.ErrNoResponse
)
View Source
var (
	ErrNoSnapshotsDiscovered = errors.New("no snapshots discovered")
)

Functions

func ExpandPath

func ExpandPath(path string) (string, error)

TODO: this is WRONG considering paths like ~user. We should rewrite this correctly, for both ~/ and ~user/ and without assuming a platform separator.

func FormatPeerString

func FormatPeerString(rawPubKey []byte, keyType crypto.KeyType, ip string, port int) string

func NewKey

NewKey generates a new private key from a reader, which should provide random data.

func RestoreDB

func RestoreDB(ctx context.Context, reader io.Reader, db config.DBConfig, snapshotHash []byte, logger log.Logger) error

Types

type AckRes

type AckRes = types.AckRes

type BlockProcessor

type BlockProcessor interface {
	GetValidators() []*ktypes.Validator
	SubscribeValidators() <-chan []*ktypes.Validator
}

type Config

type Config struct {
	RootDir string
	ChainID string
	PrivKey crypto.PrivateKey
	DB      DB

	P2P       *config.PeerConfig
	DBConfig  *config.DBConfig
	Statesync *config.StateSyncConfig

	Mempool     types.MemPool
	BlockStore  types.BlockStore
	Consensus   ConsensusEngine
	Snapshotter SnapshotStore
	BlockProc   BlockProcessor
	Logger      log.Logger

	P2PService *P2PService
}

Config is the configuration for a Node instance.

type ConsensusEngine

type ConsensusEngine interface {
	Status() *ktypes.NodeStatus // includes: role, inCatchup, consensus params, last commit info and block header

	Role() types.Role
	InCatchup() bool

	AcceptProposal(height int64, blkID, prevBlkID types.Hash, leaderSig []byte, timestamp int64) bool
	NotifyBlockProposal(blk *ktypes.Block, done func())

	AcceptCommit(height int64, blkID types.Hash, hdr *ktypes.BlockHeader, ci *ktypes.CommitInfo, leaderSig []byte) bool
	NotifyBlockCommit(blk *ktypes.Block, ci *ktypes.CommitInfo, blkID types.Hash, doneFn func())

	NotifyACK(validatorPK []byte, ack types.AckRes)

	NotifyResetState(height int64, txIDs []types.Hash, senderPubKey []byte)

	NotifyDiscoveryMessage(validatorPK []byte, height int64)

	Start(ctx context.Context, fns consensus.BroadcastFns, peerFns consensus.WhitelistFns) error

	QueueTx(ctx context.Context, tx *types.Tx) error
	BroadcastTx(ctx context.Context, tx *types.Tx, sync uint8) (ktypes.Hash, *ktypes.TxResult, error)

	ConsensusParams() *ktypes.NetworkParameters
	CancelBlockExecution(height int64, txIDs []types.Hash) error

	// PromoteLeader is used to promote a validator to leader starting from the specified height
	PromoteLeader(leader crypto.PublicKey, height int64) error
}

type ConsensusReset

type ConsensusReset = types.ConsensusReset

type DB

type DB interface {
	sql.ReadTxMaker
}

type ErrNotFoundWithBestHeight

type ErrNotFoundWithBestHeight struct {
	BestHeight int64
}

ErrNotFoundWithBestHeight is an error that contains a BestHeight field, which is used when a block is not found, but the the negative responses from peers contained their best height.

Use with errors.As. For example:

func heightFromErr(err error) int64 {
	be := new(ErrNotFoundWithBestHeight)
	if errors.As(err, &be) {
		return be.BestHeight
	}
	return -1
}

func (*ErrNotFoundWithBestHeight) Error

func (e *ErrNotFoundWithBestHeight) Error() string

type Node

type Node struct {
	// Base services
	P2PService
	// contains filtered or unexported fields
}

func NewNode

func NewNode(cfg *Config, opts ...Option) (*Node, error)

NewNode creates a new node. The config struct is for required configuration, and the functional options for optional settings, like dependency overrides.

func (*Node) AbortBlockExecution

func (n *Node) AbortBlockExecution(height int64, txIDs []types.Hash) error

func (*Node) Addrs

func (n *Node) Addrs() []string

func (*Node) BlockByHash

func (n *Node) BlockByHash(hash types.Hash) (*ktypes.Block, *ktypes.CommitInfo, error)

BlockByHash returns the block by block hash.

func (*Node) BlockByHeight

func (n *Node) BlockByHeight(height int64) (types.Hash, *ktypes.Block, *ktypes.CommitInfo, error)

BlockByHeight returns the block by height. If height <= 0, the latest block will be returned.

func (*Node) BlockHeight

func (n *Node) BlockHeight() int64

func (*Node) BlockResultByHash

func (n *Node) BlockResultByHash(hash types.Hash) ([]ktypes.TxResult, error)

BlockResultByHash returns the block result by block hash.

func (*Node) BroadcastTx

func (n *Node) BroadcastTx(ctx context.Context, tx *ktypes.Transaction, sync uint8) (ktypes.Hash, *ktypes.TxResult, error)

func (*Node) ChainTx

func (n *Node) ChainTx(hash types.Hash) (*chainTypes.Tx, error)

ChainTx return tx info that is used in Chain rpc.

func (*Node) ChainUnconfirmedTx

func (n *Node) ChainUnconfirmedTx(limit int) (int, []*types.Tx)

ChainUnconfirmedTx return unconfirmed tx info that is used in Chain rpc.

func (*Node) ConsensusParams

func (n *Node) ConsensusParams() *ktypes.NetworkParameters

func (*Node) Dir

func (n *Node) Dir() string

func (*Node) ID

func (n *Node) ID() string

func (*Node) InCatchup

func (n *Node) InCatchup() bool

func (*Node) MultiAddrs

func (n *Node) MultiAddrs() []string

func (*Node) Peers

func (n *Node) Peers(context.Context) ([]*adminTypes.PeerInfo, error)

func (*Node) PromoteLeader

func (n *Node) PromoteLeader(candidate crypto.PublicKey, height int64) error

func (*Node) RawBlockByHash

func (n *Node) RawBlockByHash(hash types.Hash) ([]byte, *ktypes.CommitInfo, error)

RawBlockByHash returns the block by block hash.

func (*Node) RawBlockByHeight

func (n *Node) RawBlockByHeight(height int64) (types.Hash, []byte, *ktypes.CommitInfo, error)

RawBlockByHeight returns the block by height. If height <= 0, the latest block will be returned.

func (*Node) Role

func (n *Node) Role() types.Role

func (*Node) Start

func (n *Node) Start(ctx context.Context) error

Start begins tx and block gossip, connects to any bootstrap peers, and begins peer discovery.

func (*Node) Status

func (n *Node) Status(ctx context.Context) (*adminTypes.Status, error)

Status returns the current status of the node.

func (*Node) TxQuery

func (n *Node) TxQuery(ctx context.Context, hash types.Hash, prove bool) (*ktypes.TxQueryResponse, error)

func (*Node) Whitelister

func (n *Node) Whitelister() *WhitelistMgr

Whitelister is a shim between the a Kwil consumer like RPC service and the p2p layer (PeerMan) which manages the persistent and effective white list in terms of libp2p types.

type Option

type Option func(*options) // NOTHING PRESENTLY!

type P2PService

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

func NewP2PService

func NewP2PService(ctx context.Context, cfg *P2PServiceConfig, host host.Host) (*P2PService, error)

func (*P2PService) Close

func (p *P2PService) Close() error

func (*P2PService) Discovery

func (p *P2PService) Discovery() discovery.Discovery

func (*P2PService) Host

func (p *P2PService) Host() host.Host

func (*P2PService) PEX

func (p *P2PService) PEX() bool

PEX indicates whether the peer manager is configured to use peer exchange (PEX).

func (*P2PService) Start

func (p *P2PService) Start(ctx context.Context, bootpeers ...string) error

Start launches the P2P service, registering the network Notifiee, and connecting to bootstrap peers. This method is NOT blocking. The context only affects the the connection process, and does not shutdown the service after this method has returned.

type P2PServiceConfig

type P2PServiceConfig struct {
	PrivKey crypto.PrivateKey
	RootDir string
	ChainID string
	KwilCfg *config.Config

	Logger log.Logger
}

type SnapshotStore

type SnapshotStore interface {
	Enabled() bool
	GetSnapshot(height uint64, format uint32) *snapshotter.Snapshot
	ListSnapshots() []*snapshotter.Snapshot
	LoadSnapshotChunk(height uint64, format uint32, chunk uint32) ([]byte, error)
}

type StateSyncService

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

func NewStateSyncService

func NewStateSyncService(ctx context.Context, cfg *StatesyncConfig) (*StateSyncService, error)

func (*StateSyncService) Bootstrap

func (s *StateSyncService) Bootstrap(ctx context.Context) error

func (*StateSyncService) DiscoverSnapshots

func (s *StateSyncService) DiscoverSnapshots(ctx context.Context) (int64, error)

DiscoverSnapshots discovers snapshot providers and their catalogs. It waits for responsesp from snapshot catalog providers for the duration of the discoveryTimeout. If the timeout is reached, the best snapshot is selected and snapshot chunks are requested. If no snapshots are discovered, it reenters the discovery phase after a delay, retrying up to maxRetries times. If discovery fails after maxRetries, the node will switch to block sync. If snapshots and their chunks are successfully fetched, the DB is restored from the snapshot and the application state is verified.

func (*StateSyncService) DoStatesync

func (ss *StateSyncService) DoStatesync(ctx context.Context) (bool, error)

DoStatesync attempts to perform statesync if the db is uninitialized. It also initializes the blockstore with the initial block data at the height of the discovered snapshot.

func (*StateSyncService) VerifySnapshot

func (ss *StateSyncService) VerifySnapshot(ctx context.Context, snap *snapshotMetadata) (bool, []byte)

verifySnapshot verifies the snapshot with the trusted provider and returns the app hash if the snapshot is valid.

type StatesyncConfig

type StatesyncConfig struct {
	StateSyncCfg *config.StateSyncConfig
	DBConfig     config.DBConfig
	RcvdSnapsDir string
	P2PService   *P2PService

	DB            DB
	SnapshotStore SnapshotStore
	BlockStore    blockStore
	Logger        log.Logger
}

type Streamer

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

Utility to stream chunks of a snapshot

func NewStreamer

func NewStreamer(numChunks uint32, chunkDir string, logger log.Logger) *Streamer

func (*Streamer) Close

func (s *Streamer) Close() error

func (*Streamer) Next

func (s *Streamer) Next() error

Next opens the next chunk file for streaming

func (*Streamer) Read

func (s *Streamer) Read(p []byte) (n int, err error)

Read reads from the current chunk file If the current chunk is exhausted, it opens the next chunk file until all chunks are read

type WhitelistMgr

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

func (*WhitelistMgr) AddPeer

func (wl *WhitelistMgr) AddPeer(nodeID string) error

func (*WhitelistMgr) List

func (wl *WhitelistMgr) List() []string

func (*WhitelistMgr) RemovePeer

func (wl *WhitelistMgr) RemovePeer(nodeID string) error

Directories

Path Synopsis
package adminclient provides a client for the Kwil admin service.
package adminclient provides a client for the Kwil admin service.
interpreter
package interpreter provides a basic interpreter for Kuneiform procedures.
package interpreter provides a basic interpreter for Kuneiform procedures.
parse
package parse contains logic for parsing SQL, DDL, and Actions, and SQL.
package parse contains logic for parsing SQL, DDL, and Actions, and SQL.
pg_generate
pggenerate package is responsible for generating the Postgres-compatible SQL from the AST.
pggenerate package is responsible for generating the Postgres-compatible SQL from the AST.
exts
erc20-bridge/erc20
package erc20reward implements a meta extension that manages all rewards on a Kwil network.
package erc20reward implements a meta extension that manages all rewards on a Kwil network.
erc20-bridge/signersvc
Package signersvc implements the SignerSvc of the Kwil reward system.
Package signersvc implements the SignerSvc of the Kwil reward system.
evm-sync/chains
package chains tracks the EVM chains that are supported by the node.
package chains tracks the EVM chains that are supported by the node.
ordered-sync
package orderedsync is a general purpose extension that synchronizes data from systems where absolute order is guaranteed (e.g.
package orderedsync is a general purpose extension that synchronizes data from systems where absolute order is guaranteed (e.g.
poll
package poll implements a basic polling mechanism for Kwil event listeners
package poll implements a basic polling mechanism for Kwil event listeners
Package meta defines a chain metadata store for the ABCI application.
Package meta defines a chain metadata store for the ABCI application.
package migrations implements a long-running migrations protocol for Kwil.
package migrations implements a long-running migrations protocol for Kwil.
sec
pg
Package pg defines the primary PostgreSQL-powered DB and Pool types used to support Kwil DB.
Package pg defines the primary PostgreSQL-powered DB and Pool types used to support Kwil DB.
services
_v1
_v2
memstore
Package memstore provides a memory-backed block store, which is only suitable for testing where a disk-based store or third party dependencies are not desired.
Package memstore provides a memory-backed block store, which is only suitable for testing where a disk-based store or third party dependencies are not desired.
package tx_router routes transactions to the appropriate module(s)
package tx_router routes transactions to the appropriate module(s)
sql
Package sql defines common type required by SQL database implementations and consumers.
Package sql defines common type required by SQL database implementations and consumers.
utils
syncmap
Package syncmap provides a map that is safe for concurrent use.
Package syncmap provides a map that is safe for concurrent use.
url
package url provides url fuctionalities to provide consistent parsing for Kwil clients.
package url provides url fuctionalities to provide consistent parsing for Kwil clients.
package versioning provides standard schema versioning for Kwil databases.
package versioning provides standard schema versioning for Kwil databases.
package events is used to track events that need to be included in a Kwil block.
package events is used to track events that need to be included in a Kwil block.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳