Skip to content

Commit

Permalink
fix TestBuildBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Sep 21, 2024
1 parent 3512493 commit 93c9b15
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
53 changes: 29 additions & 24 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,23 @@ func NewShadow(privKey *ecdsa.PrivateKey, ownAddr *felt.Felt, bc *blockchain.Blo
blockTime time.Duration, pool *mempool.Pool, log utils.Logger, starknetData starknetdata.StarknetData,
) *Builder {
return &Builder{
ownAddress: *ownAddr,
privKey: privKey,
blockTime: blockTime,
log: log,
listener: &SelectiveListener{},
chanFinaliseBlock: make(chan struct{}, 1),
chanFinalised: make(chan struct{}, 1),
ownAddress: *ownAddr,
privKey: privKey,
blockTime: blockTime,
log: log,
listener: &SelectiveListener{},

chanFinaliseBlock: make(chan struct{}, 1),
chanFinalised: make(chan struct{}, 1),
chanNumTxnsToShadow: make(chan int, 1),

bc: bc,
pool: pool,
vm: builderVM,
newHeads: feed.New[*core.Header](),

shadowMode: true,
starknetData: starknetData,
chanNumTxnsToShadow: make(chan int, 1),
shadowMode: true,
starknetData: starknetData,
}
}

Expand Down Expand Up @@ -470,7 +471,6 @@ func (b *Builder) listenPool(ctx context.Context) error {
return err
}
}

select {
case <-ctx.Done():
return nil
Expand Down Expand Up @@ -503,7 +503,6 @@ func (b *Builder) depletePool(ctx context.Context) error {
<-b.chanNumTxnsToShadow
}
}

select {
case <-ctx.Done():
return nil
Expand Down Expand Up @@ -557,9 +556,11 @@ func (b *Builder) runTxn(txn *mempool.BroadcastedTransaction) error {
}

receipt := Receipt(fee[0], feeUnit, txn.Transaction.Hash(), &trace[0], &txnReceipts[0])
err = b.overrideTraces(receipt)
if err != nil {
return err
if b.shadowBlock != nil {
err = b.overrideTraces(receipt)
if err != nil {
return err
}
}
if b.junoEndpoint != "" {
seqTrace := vm2core.AdaptStateDiff(trace[0].StateDiff)
Expand Down Expand Up @@ -676,15 +677,7 @@ func (b *Builder) shadowTxns(ctx context.Context) error {

b.shadowStateUpdate = su
b.shadowBlock = block
b.pendingBlock.Block.Transactions = nil
b.pendingBlock.Block.Number = nextBlockToSequence
b.pendingBlock.Block.SequencerAddress = block.SequencerAddress // Affects post 0.13.2 block hash
b.pendingBlock.Block.Timestamp = block.Timestamp // Affects post 0.13.2 block hash
b.pendingBlock.Block.Header.ProtocolVersion = block.ProtocolVersion // Affects post 0.13.2 block hash
b.pendingBlock.Block.Header.GasPrice = block.GasPrice // Affects post 0.13.2 block hash
b.pendingBlock.Block.Header.GasPriceSTRK = block.GasPriceSTRK // Affects post 0.13.2 block hash
b.pendingBlock.Block.Header.L1DataGasPrice = block.L1DataGasPrice // Affects post 0.13.2 block hash
b.pendingBlock.Block.Header.L1DAMode = block.L1DAMode // Affects data_availability
b.setPendingHeader(block, nextBlockToSequence)
blockHashStorage := b.pendingBlock.StateUpdate.StateDiff.StorageDiffs[*new(felt.Felt).SetUint64(1)]
for _, blockHash := range blockHashStorage {
b.blockHashToBeRevealed = blockHash // Affects execution
Expand All @@ -710,6 +703,18 @@ func (b *Builder) shadowTxns(ctx context.Context) error {
}
}

func (b *Builder) setPendingHeader(refBlock *core.Block, nextBlockToSequence uint64) {
b.pendingBlock.Block.Transactions = nil
b.pendingBlock.Block.Number = nextBlockToSequence
b.pendingBlock.Block.SequencerAddress = refBlock.SequencerAddress // Affects post 0.13.2 block hash
b.pendingBlock.Block.Timestamp = refBlock.Timestamp // Affects post 0.13.2 block hash
b.pendingBlock.Block.Header.ProtocolVersion = refBlock.ProtocolVersion // Affects post 0.13.2 block hash
b.pendingBlock.Block.Header.GasPrice = refBlock.GasPrice // Affects post 0.13.2 block hash
b.pendingBlock.Block.Header.GasPriceSTRK = refBlock.GasPriceSTRK // Affects post 0.13.2 block hash
b.pendingBlock.Block.Header.L1DataGasPrice = refBlock.L1DataGasPrice // Affects post 0.13.2 block hash
b.pendingBlock.Block.Header.L1DAMode = refBlock.L1DAMode // Affects data_availability
}

// syncStore pulls blocks, classes and state-updates directly from the FGW and stores them in the
// blockchain. This is needed when a block can not be sequenced, eg Sepolia block0 uses deprecated
// transactions to bootstrap the network, etc.
Expand Down
9 changes: 5 additions & 4 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ func TestBuildBlocks(t *testing.T) {
privKey, err := ecdsa.GenerateKey(rand.Reader)
require.NoError(t, err)
p := mempool.New(pebble.NewMemTest(t))
testBuilder := builder.New(privKey, seqAddr, bc, mockVM, time.Millisecond, p, utils.NewNopZapLogger())
blockTime := time.Millisecond
testBuilder := builder.New(privKey, seqAddr, bc, mockVM, blockTime, p, utils.NewNopZapLogger())

txnHashes := []*felt.Felt{}
for i := uint64(0); i < 100; i++ {
Expand All @@ -360,8 +361,8 @@ func TestBuildBlocks(t *testing.T) {
}

mockVM.EXPECT().Execute([]core.Transaction{invokeTxn}, gomock.Any(), gomock.Any(), gomock.Any(),
gomock.Any(), gomock.Any(), false, false, false, false).Return(
[]*felt.Felt{&felt.Zero}, []*felt.Felt{}, []vm.TransactionTrace{{StateDiff: &vm.StateDiff{}}}, executionErr,
gomock.Any(), gomock.Any(), false, false, false, true).Return(
[]*felt.Felt{&felt.Zero}, []core.GasConsumed{}, []vm.TransactionTrace{{StateDiff: &vm.StateDiff{}}}, []vm.TransactionReceipt{{}}, uint64(0), executionErr,
)
}

Expand All @@ -371,7 +372,6 @@ func TestBuildBlocks(t *testing.T) {
cancel()
}()
require.NoError(t, testBuilder.Run(ctx))

var totalTxns uint64
height, err := bc.Height()
require.NoError(t, err)
Expand Down Expand Up @@ -531,6 +531,7 @@ func TestShadowSepolia(t *testing.T) {
}
for _, hash := range classHashes {
classHash := utils.HexToFelt(t, hash)
fmt.Println("classHash", classHash.String())
class, err2 := gw.Class(context.Background(), classHash)
require.NoError(t, err2)
snData.EXPECT().Class(context.Background(), classHash).Return(class, nil)
Expand Down

0 comments on commit 93c9b15

Please sign in to comment.