sync

package
v0.16.1 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: Apache-2.0 Imports: 23 Imported by: 2

Documentation

Index

Constants

View Source
const (
	OpVerify = "verify"
	OpStore  = "store"
	OpFetch  = "fetch"
)

Variables

View Source
var ErrPreConfirmedBaseTxCountMismatch = errors.New("pre_confirmed base transaction count mismatch")

ErrPreConfirmedBaseTxCountMismatch is returned by ApplyUpdate when a Delta arrives whose base transaction count no longer matches the stored pre_confirmed. This happens when two polls race: both observe the same pre_confirmed, the first delta is applied, and the second delta — still computed against the pre-merge base — would duplicate transactions if appended positionally. Callers should drop the delta; the next poll will re-read the now-updated base and request a fresh delta.

Functions

func MakeEmptyPendingForParent deprecated added in v0.16.0

func MakeEmptyPendingForParent(
	bcReader blockchain.Reader,
	latestHeader *core.Header,
) (pending.Pending, error)

Deprecated: MakeEmptyPendingForParent constructs an empty pending.Pending placeholder used by rpc/v8 to serve "pending" block ID requests. Remove when rpc/v8 is deprecated.

func MakeEmptyPreConfirmedForParent added in v0.16.0

func MakeEmptyPreConfirmedForParent(
	bcReader blockchain.Reader,
	latestHeader *core.Header,
) (pending.PreConfirmed, error)

func PendingState added in v0.13.0

func PendingState(
	preConfirmed *pending.PreConfirmed,
	stateReader blockchain.Reader,
) (core.StateReader, blockchain.StateCloser, error)

PendingState is a convenience function that combines base state resolution with pending state creation

func PendingStateBeforeIndex added in v0.16.0

func PendingStateBeforeIndex(
	preConfirmed *pending.PreConfirmed,
	stateReader blockchain.Reader,
	index uint,
) (core.StateReader, blockchain.StateCloser, error)

PendingStateBeforeIndex is a convenience function that combines base state resolution with pending state before index creation

func ResolvePreConfirmedBaseState added in v0.16.0

func ResolvePreConfirmedBaseState(
	preConfirmed *pending.PreConfirmed,
	stateReader blockchain.Reader,
) (core.StateReader, blockchain.StateCloser, error)

ResolvePreConfirmedBaseState resolves the base state for pre-confirmed blocks

Types

type CommittedBlock added in v0.15.0

type CommittedBlock struct {
	Block       *core.Block
	StateUpdate *core.StateUpdate
	NewClasses  map[felt.Felt]core.ClassDefinition
	Persisted   chan struct{} // This is used to signal that the block has been persisted
}

type DataSource added in v0.15.0

type DataSource interface {
	BlockByNumber(ctx context.Context, blockNumber uint64) (CommittedBlock, error)
	BlockHeaderLatest(ctx context.Context) (*core.Header, error)
	BlockPreLatest(ctx context.Context) (pending.PreLatest, error)
	PreConfirmedBlockByNumber(
		ctx context.Context,
		blockNumber uint64,
		blockIdentifier string,
		knownTransactionCount uint64,
	) (starknet.PreConfirmedUpdate, error)
}

func NewFeederGatewayDataSource added in v0.15.0

func NewFeederGatewayDataSource(blockchain *blockchain.Blockchain, starknetData starknetdata.StarknetData) DataSource

type EventListener added in v0.7.0

type EventListener interface {
	OnSyncStepDone(op string, blockNum uint64, took time.Duration)
	OnReorg(blockNum uint64)
}

type NewHeadSubscription added in v0.13.2

type NewHeadSubscription struct {
	*feed.Subscription[*core.Block]
}

This is a work-around. mockgen chokes when the instantiated generic type is in the interface.

type NoopSynchronizer added in v0.10.0

type NoopSynchronizer struct{}

This is temporary and will be removed once the p2p synchronizer implements this interface.

func (*NoopSynchronizer) HighestBlockHeader added in v0.10.0

func (n *NoopSynchronizer) HighestBlockHeader() *core.Header

func (*NoopSynchronizer) PreConfirmed added in v0.16.0

func (n *NoopSynchronizer) PreConfirmed() (*pending.PreConfirmed, error)

func (*NoopSynchronizer) StartingBlockNumber added in v0.10.0

func (n *NoopSynchronizer) StartingBlockNumber() (uint64, error)

func (*NoopSynchronizer) SubscribeNewHeads added in v0.10.0

func (n *NoopSynchronizer) SubscribeNewHeads() NewHeadSubscription

func (*NoopSynchronizer) SubscribePreConfirmed added in v0.16.0

func (n *NoopSynchronizer) SubscribePreConfirmed() PreConfirmedDataSubscription

func (*NoopSynchronizer) SubscribePreLatest added in v0.15.9

func (n *NoopSynchronizer) SubscribePreLatest() PreLatestDataSubscription

func (*NoopSynchronizer) SubscribeReorg added in v0.13.0

func (n *NoopSynchronizer) SubscribeReorg() ReorgSubscription

type PendingTxSubscription added in v0.13.0

type PendingTxSubscription struct {
	*feed.Subscription[[]core.Transaction]
}

type PreConfirmedDataSubscription added in v0.16.0

type PreConfirmedDataSubscription struct {
	*feed.Subscription[*pending.PreConfirmed]
}

type PreConfirmedStorage added in v0.16.1

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

PreConfirmedStorage owns the atomically-stored pre_confirmed block and the rules for evolving it under a wire-side update.

The stored block is also the single source of truth for the poll target: pollPreConfirmed reads `inner` once per tick and derives all three poll parameters (number, identifier, txCount) from that single snapshot, so they are necessarily coherent. This means the poll loop only moves to the next height after an empty pre_confirmed is stored for it. If that write is skipped, the poller has no target and stays idle.

func NewPreConfirmedStorage added in v0.16.1

func NewPreConfirmedStorage() *PreConfirmedStorage

func (*PreConfirmedStorage) ApplyUpdate added in v0.16.1

func (s *PreConfirmedStorage) ApplyUpdate(
	update starknet.PreConfirmedUpdate,
	blockNumber uint64,
	baseTxCount uint64,
	head *core.Header,
	preLatest *pending.PreLatest,
) (*pending.PreConfirmed, error)

ApplyUpdate atomically evolves the stored pre_confirmed from a wire-side update, attaching the given pre_latest, under the preserve-if-richer rule. A starknet.PreConfirmedBlock update bootstraps the store when nothing is yet stored; starknet.PreConfirmedDeltaUpdate and starknet.PreConfirmedNoChange are no-ops in that case (Delta needs a baseline to merge into). Returns the resulting pre_confirmed if the store was replaced; nil otherwise.

baseTxCount is the transaction count the wire-side caller sent to the server as knownTransactionCount and is consulted only for the Delta case: if the stored pre_confirmed has drifted away from that count between poll dispatch and apply, the delta is rejected with ErrPreConfirmedBaseTxCountMismatch.

head MUST be the canonical chain head from blockchain.HeadsHeader(), or nil at genesis. It is forwarded to StorePreConfirmedForHead for the validation gate — see that method for the contract.

func (*PreConfirmedStorage) ReadPreConfirmedForHead added in v0.16.1

func (s *PreConfirmedStorage) ReadPreConfirmedForHead(head *core.Header) *pending.PreConfirmed

ReadPreConfirmedForHead returns the stored pre_confirmed if it is a valid successor to head (head+1, or head+2 with a valid PreLatest in between). Returns nil if nothing is stored or the stored pre_confirmed is stale.

head MUST be the canonical chain head as returned by blockchain.HeadsHeader(), or nil when the chain is empty (genesis).

func (*PreConfirmedStorage) ReadUnsafe added in v0.16.1

func (s *PreConfirmedStorage) ReadUnsafe() *pending.PreConfirmed

ReadUnsafe returns the currently stored pre_confirmed pointer without any validation against the canonical head. The result may be nil, stale, or inconsistent with the chain — callers that need a consumer-safe view should use ReadPreConfirmedForHead instead.

func (*PreConfirmedStorage) StorePreConfirmedForHead added in v0.16.1

func (s *PreConfirmedStorage) StorePreConfirmedForHead(
	p *pending.PreConfirmed,
	head *core.Header,
) (bool, error)

StorePreConfirmedForHead atomically stores a fully-constructed pre_confirmed. The protocol version must be supported and the block must validate against head. The store-vs-preserve decision then follows the rules below; returns true if the stored pointer was replaced.

Replacement happens when:

  • the incoming block is at a higher number than the existing one, or
  • same number but a different BlockIdentifier (new round) — replaces even if the new block has fewer txs, EXCEPT when the incoming carries the blank PreConfirmedBlankIdentifier (an internal placeholder), in which case the existing real round is preserved, or
  • same number and identifier but the incoming block is strictly richer (more transactions).

Otherwise the existing block is preserved; if the incoming carries a fresh PreLatest attachment we refresh that in-place via CAS without swapping the pre_confirmed pointer.

head MUST be the canonical chain head as returned by blockchain.HeadsHeader(), or nil when the chain is empty (genesis).

func (*PreConfirmedStorage) UpdatePreLatestAttachment added in v0.16.1

func (s *PreConfirmedStorage) UpdatePreLatestAttachment(
	blockNumber uint64,
	preLatest *pending.PreLatest,
) bool

UpdatePreLatestAttachment swaps in a new PreLatest attachment for the stored pre_confirmed at the given block number. Returns true on a CAS swap.

type PreLatestDataSubscription added in v0.15.9

type PreLatestDataSubscription struct {
	*feed.Subscription[*pending.PreLatest]
}

type Reader added in v0.6.2

type Reader interface {
	StartingBlockNumber() (uint64, error)
	HighestBlockHeader() *core.Header
	SubscribeNewHeads() NewHeadSubscription
	SubscribeReorg() ReorgSubscription
	SubscribePreConfirmed() PreConfirmedDataSubscription
	SubscribePreLatest() PreLatestDataSubscription
	PreConfirmed() (*pending.PreConfirmed, error)
}

Todo: Since this is also going to be implemented by p2p package we should move this interface to node package

type ReorgBlockRange added in v0.13.0

type ReorgBlockRange struct {
	// StartBlockHash is the hash of the first known block of the orphaned chain
	StartBlockHash *felt.Felt
	// StartBlockNum is the number of the first known block of the orphaned chain
	StartBlockNum uint64
	// The last known block of the orphaned chain
	EndBlockHash *felt.Felt
	// Number of the last known block of the orphaned chain
	EndBlockNum uint64
}

ReorgBlockRange represents data about reorganised blocks, starting and ending block number and hash

type ReorgSubscription added in v0.13.0

type ReorgSubscription struct {
	*feed.Subscription[*ReorgBlockRange]
}

type SelectiveListener added in v0.7.0

type SelectiveListener struct {
	OnSyncStepDoneCb func(op string, blockNum uint64, took time.Duration)
	OnReorgCb        func(blockNum uint64)
}

func (*SelectiveListener) OnReorg added in v0.7.0

func (l *SelectiveListener) OnReorg(blockNum uint64)

func (*SelectiveListener) OnSyncStepDone added in v0.7.0

func (l *SelectiveListener) OnSyncStepDone(op string, blockNum uint64, took time.Duration)

type Synchronizer

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

Synchronizer manages a list of StarknetData to fetch the latest blockchain updates

func New added in v0.2.1

func New(
	bc *blockchain.Blockchain,
	dataSource DataSource,
	logger log.StructuredLogger,
	preLatestPollInterval,
	preConfirmedPollInterval time.Duration,
	readOnlyBlockchain bool,
	database db.KeyValueStore,
) *Synchronizer

func (*Synchronizer) HighestBlockHeader added in v0.3.0

func (s *Synchronizer) HighestBlockHeader() *core.Header

func (*Synchronizer) PreConfirmed added in v0.16.0

func (s *Synchronizer) PreConfirmed() (*pending.PreConfirmed, error)

func (*Synchronizer) Run

func (s *Synchronizer) Run(ctx context.Context) error

Run starts the Synchronizer, returns an error if the loop is already running

func (*Synchronizer) StartingBlockNumber added in v0.3.0

func (s *Synchronizer) StartingBlockNumber() (uint64, error)

func (*Synchronizer) SubscribeNewHeads added in v0.7.4

func (s *Synchronizer) SubscribeNewHeads() NewHeadSubscription

func (*Synchronizer) SubscribePreConfirmed added in v0.16.0

func (s *Synchronizer) SubscribePreConfirmed() PreConfirmedDataSubscription

func (*Synchronizer) SubscribePreLatest added in v0.15.9

func (s *Synchronizer) SubscribePreLatest() PreLatestDataSubscription

func (*Synchronizer) SubscribeReorg added in v0.13.0

func (s *Synchronizer) SubscribeReorg() ReorgSubscription

func (*Synchronizer) WithListener added in v0.7.0

func (s *Synchronizer) WithListener(listener EventListener) *Synchronizer

WithListener registers an EventListener

func (*Synchronizer) WithPlugin added in v0.12.3

func (s *Synchronizer) WithPlugin(plugin junoplugin.JunoPlugin) *Synchronizer

WithPlugin registers an plugin

Jump to

Keyboard shortcuts

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