Skip to content

Commit

Permalink
Refactor 'txSnapshot.ApplyInitialSnapshot'.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickeskov committed Dec 17, 2023
1 parent f4bde2e commit 5f83ee5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
8 changes: 5 additions & 3 deletions pkg/state/appender.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ func calculateInitialSnapshotStateHash(
}

func (a *txAppender) appendBlock(params *appendBlockParams) error {
const validatingUTX = false // we're applying a block, so it's false
// Reset block complexity counter.
defer func() {
a.sc.resetComplexity()
Expand Down Expand Up @@ -695,7 +696,7 @@ func (a *txAppender) appendBlock(params *appendBlockParams) error {
// TODO validate before reset
a.diffStor.reset()

err = initialSnapshot.ApplyInitialSnapshot(a.txHandler.sa)
err = initialSnapshot.ApplyInitialSnapshot(a.txHandler.sa, validatingUTX)
if err != nil {
return errors.Wrap(err, "failed to apply an initial snapshot")
}
Expand Down Expand Up @@ -749,7 +750,7 @@ func (a *txAppender) appendBlock(params *appendBlockParams) error {
consensusImprovementsActivated: consensusImprovementsActivated,
blockRewardDistributionActivated: blockRewardDistributionActivated,
invokeExpressionActivated: invokeExpressionActivated,
validatingUtx: false,
validatingUtx: validatingUTX,
currentMinerPK: params.block.GeneratorPublicKey,
}
txSnapshots, errAppendTx := a.appendTx(tx, appendTxArgs)
Expand Down Expand Up @@ -969,6 +970,7 @@ func (a *txAppender) handleFallible(

// For UTX validation.
func (a *txAppender) validateNextTx(tx proto.Transaction, currentTimestamp, parentTimestamp uint64, version proto.BlockVersion, acceptFailed bool) error {
const validatingUTX = true // we're validating a tx in UTX, so it's true
// TODO: Doesn't work correctly if miner doesn't work in NG mode.
// In this case it returns the last block instead of what is being mined.
block, err := a.currentBlock()
Expand Down Expand Up @@ -1036,7 +1038,7 @@ func (a *txAppender) validateNextTx(tx proto.Transaction, currentTimestamp, pare
consensusImprovementsActivated: consensusImprovementsActivated,
blockRewardDistributionActivated: blockRewardDistributionActivated,
invokeExpressionActivated: invokeExpressionActivated,
validatingUtx: true,
validatingUtx: validatingUTX,
}
_, err = a.appendTx(tx, appendTxArgs)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkg/state/snapshot_applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
)

type txSnapshotContext struct {
initialized bool
validatingUTX bool
applyingTx proto.Transaction
validatingUTX bool
}

func (c txSnapshotContext) initialized() bool { return c.applyingTx != nil }

type blockSnapshotsApplier struct {
info *blockSnapshotsApplierInfo
stor snapshotApplierStorages
Expand All @@ -32,9 +33,8 @@ type blockSnapshotsApplier struct {

func (a *blockSnapshotsApplier) BeforeTxSnapshotApply(tx proto.Transaction, validatingUTX bool) error {
a.txSnapshotContext = txSnapshotContext{
initialized: true,
validatingUTX: validatingUTX,
applyingTx: tx,
validatingUTX: validatingUTX, // can be nil in case of initial snapshot applying
}
a.issuedAssets = []crypto.Digest{}
if len(a.scriptedAssets) != 0 {
Expand Down Expand Up @@ -335,7 +335,7 @@ func (a *blockSnapshotsApplier) ApplyCancelledLease(snapshot proto.CancelledLeas
}

func (a *blockSnapshotsApplier) ApplyTransactionsStatus(snapshot proto.TransactionStatusSnapshot) error {
if !a.txSnapshotContext.initialized { // sanity check
if !a.txSnapshotContext.initialized() { // sanity check
return errors.New("failed to apply transaction status snapshot: transaction is not set")
}
var (
Expand Down
18 changes: 6 additions & 12 deletions pkg/state/tx_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,13 @@ func (ts txSnapshot) Apply(a extendedSnapshotApplier, tx proto.Transaction, vali
return nil
}

func (ts txSnapshot) ApplyInitialSnapshot(a extendedSnapshotApplier) error {
// internal snapshots must be applied at the end
for _, rs := range ts.regular {
err := rs.Apply(a)
if err != nil {
return errors.Wrap(err, "failed to apply regular transaction snapshot")
}
func (ts txSnapshot) ApplyInitialSnapshot(a extendedSnapshotApplier, validatingUTX bool) error {
if validatingUTX { // sanity check
return errors.New("initial snapshot cannot be applied in validating UTX mode")
}
for _, is := range ts.internal {
err := is.ApplyInternal(a)
if err != nil {
return errors.Wrap(err, "failed to apply internal transaction snapshot")
}
var tx proto.Transaction // it's nil intentionally, because we're applying initial snapshot without TX
if err := ts.Apply(a, tx, validatingUTX); err != nil {
return errors.Wrap(err, "failed to apply initial block snapshot")
}
return nil
}

0 comments on commit 5f83ee5

Please sign in to comment.