Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Feb 27, 2024
1 parent 2494e47 commit 8bbf9e3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
2 changes: 2 additions & 0 deletions rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
64 changes: 35 additions & 29 deletions rpc/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -3269,33 +3271,35 @@ 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})
require.Nil(t, err)
})

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})
require.Nil(t, err)
})

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"),
Expand All @@ -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"),
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -3834,33 +3838,35 @@ 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})
require.Nil(t, err)
})

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})
require.Nil(t, err)
})

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"),
Expand All @@ -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"),
Expand Down

0 comments on commit 8bbf9e3

Please sign in to comment.