diff --git a/rpc/handlers.go b/rpc/handlers.go index f9ee867ea2..bb4fb0559c 100644 --- a/rpc/handlers.go +++ b/rpc/handlers.go @@ -493,6 +493,7 @@ func (h *Handler) blockByID(id *BlockID) (*core.Block, *jsonrpc.Error) { func (h *Handler) blockHeaderByID(id *BlockID) (*core.Header, *jsonrpc.Error) { var header *core.Header var err error + switch { case id.Latest: header, err = h.bcReader.HeadsHeader() @@ -1503,6 +1504,7 @@ func (h *Handler) simulateTransactions(id BlockID, transactions []BroadcastedTra if rpcErr != nil { return nil, rpcErr } + state, closer, rpcErr := h.stateByBlockID(&id) if rpcErr != nil { return nil, rpcErr diff --git a/rpc/handlers_test.go b/rpc/handlers_test.go index f9823dee86..8e62fe82f3 100644 --- a/rpc/handlers_test.go +++ b/rpc/handlers_test.go @@ -3065,8 +3065,9 @@ func TestEstimateMessageFee(t *testing.T) { t.Run("block not found", func(t *testing.T) { mockReader.EXPECT().HeadState().Return(nil, nil, db.ErrKeyNotFound) - blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1)} - mockReader.EXPECT().Head().Return(&core.Block{Header: &blockHeader}, nil).Times(2) + blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1), Number: 123} + mockReader.EXPECT().HeadsHeader().Return(&blockHeader, nil).Times(2) + mockReader.EXPECT().BlockHeaderByNumber(uint64(113)).Return(&blockHeader, nil).Times(2) _, err := handler.EstimateMessageFee(msg, rpc.BlockID{Latest: true}) require.Equal(t, rpc.ErrBlockNotFound, err) }) @@ -3149,8 +3150,9 @@ func TestLegacyEstimateMessageFee(t *testing.T) { mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil) mockReader.EXPECT().HeadsHeader().Return(latestHeader, nil) - blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1)} - mockReader.EXPECT().Head().Return(&core.Block{Header: &blockHeader}, nil).Times(1) + latestHeaderMinus10 := core.Header{Hash: new(felt.Felt).SetUint64(1), Number: 113} + mockReader.EXPECT().HeadsHeader().Return(latestHeader, nil) + mockReader.EXPECT().BlockHeaderByNumber(uint64(113)).Return(&latestHeaderMinus10, nil) expectedGasConsumed := new(felt.Felt).SetUint64(37) mockVM.EXPECT().Execute(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), @@ -3269,13 +3271,13 @@ func TestSimulateTransactions(t *testing.T) { mockState := mocks.NewMockStateHistoryReader(mockCtrl) mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil).AnyTimes() - mockReader.EXPECT().HeadsHeader().Return(&core.Header{}, nil).AnyTimes() sequencerAddress := network.BlockHashMetaInfo.FallBackSequencerAddress t.Run("ok with zero values, skip fee", func(t *testing.T) { //nolint:dupl - blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1)} - mockReader.EXPECT().Head().Return(&core.Block{Header: &blockHeader}, nil) - mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, true, false, false, nil, nil, false, nil, nil, false). + blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1), Number: 123} + mockReader.EXPECT().HeadsHeader().Return(&blockHeader, nil).AnyTimes() + mockReader.EXPECT().BlockHeaderByNumber(uint64(113)).Return(&blockHeader, nil) + mockVM.EXPECT().Execute(nil, nil, uint64(123), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, true, false, false, nil, nil, false, nil, nil, false). Return([]*felt.Felt{}, []vm.TransactionTrace{}, nil) _, err := handler.SimulateTransactions(rpc.BlockID{Latest: true}, []rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{rpc.SkipFeeChargeFlag}) @@ -3283,9 +3285,10 @@ func TestSimulateTransactions(t *testing.T) { }) t.Run("ok with zero values, skip validate", func(t *testing.T) { //nolint:dupl - blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1)} - mockReader.EXPECT().Head().Return(&core.Block{Header: &blockHeader}, nil) - mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, false, true, false, nil, nil, false, nil, nil, false). + blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1), Number: 123} + mockReader.EXPECT().HeadsHeader().Return(&blockHeader, nil).AnyTimes() + mockReader.EXPECT().BlockHeaderByNumber(uint64(113)).Return(&blockHeader, nil) + mockVM.EXPECT().Execute(nil, nil, uint64(123), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, false, true, false, nil, nil, false, nil, nil, false). Return([]*felt.Felt{}, []vm.TransactionTrace{}, nil) _, err := handler.SimulateTransactions(rpc.BlockID{Latest: true}, []rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{rpc.SkipValidateFlag}) @@ -3293,9 +3296,10 @@ func TestSimulateTransactions(t *testing.T) { }) t.Run("transaction execution error", func(t *testing.T) { - blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1)} - mockReader.EXPECT().Head().Return(&core.Block{Header: &blockHeader}, nil).Times(2) - mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, false, true, false, nil, nil, false, nil, nil, false). + blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1), Number: 123} + mockReader.EXPECT().HeadsHeader().Return(&blockHeader, nil).AnyTimes() + mockReader.EXPECT().BlockHeaderByNumber(uint64(113)).Return(&blockHeader, nil).AnyTimes() + mockVM.EXPECT().Execute(nil, nil, uint64(123), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, false, true, false, nil, nil, false, nil, nil, false). Return(nil, nil, vm.TransactionExecutionError{ Index: 44, Cause: errors.New("oops"), @@ -3307,7 +3311,7 @@ func TestSimulateTransactions(t *testing.T) { ExecutionError: "oops", }), err) - mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, false, true, true, nil, nil, true, nil, nil, false). + mockVM.EXPECT().Execute(nil, nil, uint64(123), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, false, true, true, nil, nil, true, nil, nil, false). Return(nil, nil, vm.TransactionExecutionError{ Index: 44, Cause: errors.New("oops"), @@ -3766,9 +3770,9 @@ func TestThrottledVMError(t *testing.T) { t.Run("simulate", func(t *testing.T) { mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil) - mockReader.EXPECT().HeadsHeader().Return(&core.Header{}, nil) - blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1)} - mockReader.EXPECT().Head().Return(&core.Block{Header: &blockHeader}, nil) + blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1), Number: 123} + mockReader.EXPECT().HeadsHeader().Return(&blockHeader, nil).AnyTimes() + mockReader.EXPECT().BlockHeaderByNumber(uint64(113)).Return(&blockHeader, nil) _, rpcErr := handler.SimulateTransactions(rpc.BlockID{Latest: true}, []rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{rpc.SkipFeeChargeFlag}) assert.Equal(t, throttledErr, rpcErr.Data) }) @@ -3834,13 +3838,13 @@ func TestEstimateFee(t *testing.T) { mockState := mocks.NewMockStateHistoryReader(mockCtrl) mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil).AnyTimes() - mockReader.EXPECT().HeadsHeader().Return(&core.Header{}, nil).AnyTimes() sequencerAddress := network.BlockHashMetaInfo.FallBackSequencerAddress t.Run("ok with zero values", func(t *testing.T) { - blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1)} - mockReader.EXPECT().Head().Return(&core.Block{Header: &blockHeader}, nil).Times(1) - mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, true, false, true, nil, nil, false, nil, nil, false). + blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1), Number: 123} + mockReader.EXPECT().HeadsHeader().Return(&blockHeader, nil).AnyTimes() + mockReader.EXPECT().BlockHeaderByNumber(uint64(113)).Return(&blockHeader, nil) + mockVM.EXPECT().Execute(nil, nil, uint64(123), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, true, false, true, nil, nil, false, nil, nil, false). Return([]*felt.Felt{}, []vm.TransactionTrace{}, nil) _, err := handler.EstimateFee([]rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{}, rpc.BlockID{Latest: true}) @@ -3848,9 +3852,10 @@ func TestEstimateFee(t *testing.T) { }) t.Run("ok with zero values, skip validate", func(t *testing.T) { - blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1)} - mockReader.EXPECT().Head().Return(&core.Block{Header: &blockHeader}, nil).Times(1) - mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, true, true, true, nil, nil, false, nil, nil, false). + blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1), Number: 123} + mockReader.EXPECT().HeadsHeader().Return(&blockHeader, nil).AnyTimes() + mockReader.EXPECT().BlockHeaderByNumber(uint64(113)).Return(&blockHeader, nil) + mockVM.EXPECT().Execute(nil, nil, uint64(123), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, true, true, true, nil, nil, false, nil, nil, false). Return([]*felt.Felt{}, []vm.TransactionTrace{}, nil) _, err := handler.EstimateFee([]rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{rpc.SkipValidateFlag}, rpc.BlockID{Latest: true}) @@ -3858,9 +3863,10 @@ func TestEstimateFee(t *testing.T) { }) t.Run("transaction execution error", func(t *testing.T) { - blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1)} - mockReader.EXPECT().Head().Return(&core.Block{Header: &blockHeader}, nil).Times(2) - mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, true, true, true, nil, nil, false, nil, nil, false). + blockHeader := core.Header{Hash: new(felt.Felt).SetUint64(1), Number: 123} + mockReader.EXPECT().HeadsHeader().Return(&blockHeader, nil).AnyTimes() + mockReader.EXPECT().BlockHeaderByNumber(uint64(113)).Return(&blockHeader, nil).AnyTimes() + mockVM.EXPECT().Execute(nil, nil, uint64(123), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, true, true, true, nil, nil, false, nil, nil, false). Return(nil, nil, vm.TransactionExecutionError{ Index: 44, Cause: errors.New("oops"), @@ -3872,7 +3878,7 @@ func TestEstimateFee(t *testing.T) { ExecutionError: "oops", }), err) - mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, true, false, true, nil, nil, true, nil, nil, false). + mockVM.EXPECT().Execute(nil, nil, uint64(123), uint64(0), "", blockHeader.Hash, sequencerAddress, mockState, &network, []*felt.Felt{}, true, false, true, nil, nil, true, nil, nil, false). Return(nil, nil, vm.TransactionExecutionError{ Index: 44, Cause: errors.New("oops"),