Skip to content

Commit

Permalink
Store empty pending block and state
Browse files Browse the repository at this point in the history
In one of the previous commits empty pending block and state was removed
as the spec allowed returning block not found error if pending didn't
exist. However, `statknet.js` tests always expect a block or state to be
returned when a pending block or state is requested; therefore, it is
re-added.
  • Loading branch information
IronGauntlets committed Jan 14, 2025
1 parent 27b6968 commit efe2165
Showing 1 changed file with 80 additions and 3 deletions.
83 changes: 80 additions & 3 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ func (s *Synchronizer) handlePluginRevertBlock() {
}
}

//nolint:gocyclo
func (s *Synchronizer) verifierTask(ctx context.Context, block *core.Block, stateUpdate *core.StateUpdate,
newClasses map[felt.Felt]core.Class, resetStreams context.CancelFunc,
) stream.Callback {
Expand Down Expand Up @@ -318,13 +319,30 @@ func (s *Synchronizer) verifierTask(ctx context.Context, block *core.Block, stat
s.handlePluginRevertBlock()
}
s.revertHead(block)

// The previous head has been reverted, hence, get the current head and store empty pending block
head, err := s.blockchain.HeadsHeader()
if err != nil {
s.log.Errorw("Failed to retrieve the head header", "err", err)
}

if head != nil {
if err := s.storeEmptyPending(head); err != nil {
s.log.Errorw("Failed to store empty pending block", "number", block.Number)
}

Check warning on line 332 in sync/sync.go

View check run for this annotation

Codecov / codecov/patch

sync/sync.go#L331-L332

Added lines #L331 - L332 were not covered by tests
}
} else {
s.log.Warnw("Failed storing Block", "number", block.Number,
"hash", block.Hash.ShortString(), "err", err)
}
resetStreams()
return
}

if err := s.storeEmptyPending(block.Header); err != nil {
s.log.Errorw("Failed to store empty pending block", "number", block.Number)
}

Check warning on line 344 in sync/sync.go

View check run for this annotation

Codecov / codecov/patch

sync/sync.go#L343-L344

Added lines #L343 - L344 were not covered by tests

s.listener.OnSyncStepDone(OpStore, block.Number, time.Since(storeTimer))

highestBlockHeader := s.highestBlockHeader.Load()
Expand Down Expand Up @@ -628,9 +646,6 @@ func (s *Synchronizer) Pending() (*Pending, error) {
return p, nil
}

// Since the pending block in the cache is outdated remove it
s.pending.Store(nil)

return nil, ErrPendingBlockNotFound
}

Expand All @@ -656,3 +671,65 @@ func (s *Synchronizer) PendingState() (core.StateReader, func() error, error) {

return NewPendingState(pending.StateUpdate.StateDiff, pending.NewClasses, core.NewState(txn)), txn.Discard, nil
}

func (s *Synchronizer) storeEmptyPending(latestHeader *core.Header) error {
receipts := make([]*core.TransactionReceipt, 0)
pendingBlock := &core.Block{
Header: &core.Header{
ParentHash: latestHeader.Hash,
SequencerAddress: latestHeader.SequencerAddress,
Number: latestHeader.Number + 1,
Timestamp: uint64(time.Now().Unix()),
ProtocolVersion: latestHeader.ProtocolVersion,
EventsBloom: core.EventsBloom(receipts),
GasPrice: latestHeader.GasPrice,
GasPriceSTRK: latestHeader.GasPriceSTRK,
},
Transactions: make([]core.Transaction, 0),
Receipts: receipts,
}

stateDiff, err := makeStateDiffForEmptyBlock(s.blockchain, latestHeader.Number+1)
if err != nil {
return err
}

Check warning on line 695 in sync/sync.go

View check run for this annotation

Codecov / codecov/patch

sync/sync.go#L694-L695

Added lines #L694 - L695 were not covered by tests

emptyPending := &Pending{
Block: pendingBlock,
StateUpdate: &core.StateUpdate{
OldRoot: latestHeader.GlobalStateRoot,
StateDiff: stateDiff,
},
NewClasses: make(map[felt.Felt]core.Class, 0),
}

s.pending.Store(emptyPending)
return nil
}

func makeStateDiffForEmptyBlock(bc blockchain.Reader, blockNumber uint64) (*core.StateDiff, error) {
stateDiff := &core.StateDiff{
StorageDiffs: make(map[felt.Felt]map[felt.Felt]*felt.Felt),
Nonces: make(map[felt.Felt]*felt.Felt),
DeployedContracts: make(map[felt.Felt]*felt.Felt),
DeclaredV0Classes: make([]*felt.Felt, 0),
DeclaredV1Classes: make(map[felt.Felt]*felt.Felt),
ReplacedClasses: make(map[felt.Felt]*felt.Felt),
}

const blockHashLag = 10
if blockNumber < blockHashLag {
return stateDiff, nil
}

header, err := bc.BlockHeaderByNumber(blockNumber - blockHashLag)
if err != nil {
return nil, err
}

Check warning on line 728 in sync/sync.go

View check run for this annotation

Codecov / codecov/patch

sync/sync.go#L725-L728

Added lines #L725 - L728 were not covered by tests

blockHashStorageContract := new(felt.Felt).SetUint64(1)
stateDiff.StorageDiffs[*blockHashStorageContract] = map[felt.Felt]*felt.Felt{
*new(felt.Felt).SetUint64(header.Number): header.Hash,
}
return stateDiff, nil

Check warning on line 734 in sync/sync.go

View check run for this annotation

Codecov / codecov/patch

sync/sync.go#L730-L734

Added lines #L730 - L734 were not covered by tests
}

0 comments on commit efe2165

Please sign in to comment.