Skip to content

Commit

Permalink
tests seem to be fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Dec 16, 2024
1 parent 7de947a commit 8c47609
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 51 deletions.
1 change: 0 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,6 @@ func (b *Blockchain) validateStateDiff(shadowStateUpdate, pendingStateUpdate *co
_, diffFound := pendingStateUpdate.StateDiff.Diff(shadowStateUpdate.StateDiff, "sequencer", "sepolia")
if diffFound {
// Todo: make format nicely
// fmt.Println(diffString)
return fmt.Errorf("state diff validation failed")
}
return nil
Expand Down
64 changes: 15 additions & 49 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func (b *Builder) Run(ctx context.Context) error {

func (b *Builder) ClearPending() error {
b.pendingBlock.Store(&sync.Pending{})

if b.headState != nil {
if err := b.headCloser(); err != nil {
return err
Expand Down Expand Up @@ -258,11 +259,7 @@ func (b *Builder) runShadowMode(ctx context.Context) error {
<-doneListen
return nil
case <-b.chanFinaliseBlock:
pending, err := b.Pending()
if err != nil {
return err
}
err = b.cleanStorageDiff(pending.StateUpdate.StateDiff)
err := b.cleanStorageDiff()
if err != nil {
return err
}
Expand All @@ -276,17 +273,24 @@ func (b *Builder) runShadowMode(ctx context.Context) error {
}
}

func (b *Builder) cleanStorageDiff(sd *core.StateDiff) error {
func (b *Builder) cleanStorageDiff() error {
b.log.Debugw("Removing values in the storage diff that don't affect state")
state, closer, err := b.bc.HeadState()
pending, err := b.Pending()
if err != nil {
return err
}
sd := pending.StateUpdate.StateDiff
state, stateCloser, err := b.PendingState()
if err != nil {
return err
}

defer func() {
if closeErr := closer(); closeErr != nil {
b.log.Errorw("Failed to close the state", "error", closeErr)
if err = stateCloser(); err != nil {
b.log.Errorw("closing state in cleanStorageDiff", "err", err)
}
}()

for addr, storage := range sd.StorageDiffs {
for k, v := range storage {
previousValue, err := state.ContractStorage(&addr, &k)
Expand Down Expand Up @@ -329,6 +333,7 @@ func (b *Builder) cleanStorageDiff(sd *core.StateDiff) error {
}
}
sd.DeclaredV0Classes = result
b.pendingBlock.Store(pending)
return nil
}

Expand Down Expand Up @@ -371,7 +376,6 @@ func (b *Builder) Finalise(signFunc blockchain.BlockSignFunc) error {
if err != nil {
return err
}
fmt.Println("pending", pending.Block.Number, pending.StateUpdate)
if err := b.bc.Finalise(pending.Block, pending.StateUpdate, pending.NewClasses, b.Sign, b.shadowStateUpdate, b.shadowBlock); err != nil {
return err
}
Expand All @@ -391,43 +395,6 @@ func (b *Builder) Finalise(signFunc blockchain.BlockSignFunc) error {
return b.InitPendingBlock()
}

// ValidateAgainstPendingState validates a user transaction against the pending state
// only hard-failures result in an error, reverts are not reported back to caller
func (b *Builder) ValidateAgainstPendingState(userTxn *mempool.BroadcastedTransaction) error {
declaredClasses := []core.Class{}
if userTxn.DeclaredClass != nil {
declaredClasses = []core.Class{userTxn.DeclaredClass}
}
pendingBlock, err := b.Pending()
if err != nil {
return err
}

state, stateCloser, err := b.PendingState()
if err != nil {
return err
}

defer func() {
if err = stateCloser(); err != nil {
b.log.Errorw("closing state in ValidateAgainstPendingState", "err", err)
}
}()

blockInfo := &vm.BlockInfo{
Header: &core.Header{
Number: pendingBlock.Block.Number,
Timestamp: pendingBlock.Block.Timestamp,
SequencerAddress: &b.ownAddress,
GasPrice: pendingBlock.Block.GasPrice,
GasPriceSTRK: pendingBlock.Block.GasPriceSTRK,
},
}
_, _, _, _, _, err = b.vm.Execute([]core.Transaction{userTxn.Transaction}, declaredClasses, //nolint:dogsled
[]*felt.Felt{}, blockInfo, state, b.bc.Network(), false, false, false)
return err
}

func (b *Builder) StartingBlockNumber() (uint64, error) {
return 0, nil
}
Expand Down Expand Up @@ -724,7 +691,6 @@ func (b *Builder) setPendingHeader(refBlock *core.Block, nextBlockToSequence uin
if err != nil {
return err
}

pending.Block.Transactions = nil
pending.Block.Number = nextBlockToSequence
pending.Block.SequencerAddress = refBlock.SequencerAddress // Affects post 0.13.2 block hash
Expand Down Expand Up @@ -777,7 +743,7 @@ func (b *Builder) syncStore() error {
return err
}
}
return nil
return b.ClearPending()
}

func (b *Builder) getSyncData(blockNumber uint64) (*core.Block, *core.StateUpdate,
Expand Down
2 changes: 1 addition & 1 deletion builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func TestBuildBlocks(t *testing.T) {

ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(10 * time.Millisecond)
waitForTxns(t, bc, time.Second, txnHashes)
cancel()
}()
require.NoError(t, testBuilder.Run(ctx))
Expand Down

0 comments on commit 8c47609

Please sign in to comment.