Skip to content

Commit

Permalink
support for blockHash - 10
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Feb 26, 2024
1 parent 25ce749 commit bd6ec4f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 9 deletions.
62 changes: 61 additions & 1 deletion rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1462,12 +1462,72 @@ func (h *Handler) LegacySimulateTransactions(id BlockID, transactions []Broadcas
return res, err
}

func (h *Handler) getBlockHashMinus10(blockID BlockID) (*felt.Felt, *jsonrpc.Error) {
curBlockHashAndNumber, errRPC := h.BlockHashAndNumber()
if errRPC != nil {
return nil, errRPC
}
if blockID.Latest {
return curBlockHashAndNumber.Hash, nil
}

if blockID.Hash != nil {
if curBlockHashAndNumber.Number < 10 { //nolint:gomnd
return nil, jsonrpc.Err(jsonrpc.InvalidParams, "block number must be greater than 10")
}
blockIDMinus10 := BlockID{
Number: curBlockHashAndNumber.Number - 10,
}
header, err := h.blockHeaderByID(&blockIDMinus10)
if err != nil {
return nil, err
}
return header.Hash, nil
}

if blockID.Pending {
pending, err := h.bcReader.Pending()
if err != nil {
return nil, jsonrpc.Err(jsonrpc.InternalError, err.Error())
}
bNum := pending.Block.Header.Number
blockIDMinus10 := BlockID{
Number: bNum - 10,
}
header, errRPC := h.blockHeaderByID(&blockIDMinus10)
if errRPC != nil {
return nil, errRPC
}
return header.Hash, nil
}

if blockID.Number > 10 { //nolint:gomnd
blockIDMinus10 := BlockID{
Number: curBlockHashAndNumber.Number - 10,
}
header, err := h.blockHeaderByID(&blockIDMinus10)
if err != nil {
return nil, err
}
return header.Hash, nil
} else if blockID.Number < 10 { //nolint:gomnd
return nil, jsonrpc.Err(jsonrpc.InvalidParams, "block number must be greater than 10")
}

return nil, jsonrpc.Err(jsonrpc.InternalError, "unexpected error in GetBlockHashMinus10")
}

//nolint:gocyclo
func (h *Handler) simulateTransactions(id BlockID, transactions []BroadcastedTransaction,
simulationFlags []SimulationFlag, legacyTraceJSON, errOnRevert bool,
) ([]SimulatedTransaction, *jsonrpc.Error) {
skipFeeCharge := slices.Contains(simulationFlags, SkipFeeChargeFlag)
skipValidate := slices.Contains(simulationFlags, SkipValidateFlag)

blockHashMinus10, rpcErr := h.getBlockHashMinus10(id)
if rpcErr != nil {
return nil, rpcErr
}
state, closer, rpcErr := h.stateByBlockID(&id)
if rpcErr != nil {
return nil, rpcErr
Expand Down Expand Up @@ -1504,7 +1564,7 @@ func (h *Handler) simulateTransactions(id BlockID, transactions []BroadcastedTra
sequencerAddress = h.bcReader.Network().BlockHashMetaInfo.FallBackSequencerAddress
}
overallFees, traces, err := h.vm.Execute(txns, classes, header.Number, header.Timestamp, header.ProtocolVersion,
header.Hash, sequencerAddress, state, h.bcReader.Network(), paidFeesOnL1, skipFeeCharge, skipValidate,
blockHashMinus10, sequencerAddress, state, h.bcReader.Network(), paidFeesOnL1, skipFeeCharge, skipValidate,
errOnRevert, header.GasPrice, header.GasPriceSTRK, legacyTraceJSON, nil, nil, false)
if err != nil {
if errors.Is(err, utils.ErrResourceBusy) {
Expand Down
34 changes: 26 additions & 8 deletions rpc/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3065,6 +3065,8 @@ 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)
_, err := handler.EstimateMessageFee(msg, rpc.BlockID{Latest: true})
require.Equal(t, rpc.ErrBlockNotFound, err)
})
Expand Down Expand Up @@ -3147,6 +3149,8 @@ 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)

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,23 +3273,29 @@ func TestSimulateTransactions(t *testing.T) {
sequencerAddress := network.BlockHashMetaInfo.FallBackSequencerAddress

t.Run("ok with zero values, skip fee", func(t *testing.T) { //nolint:dupl
mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", nil, sequencerAddress, mockState, &network, []*felt.Felt{}, true, false, false, nil, nil, false, nil, nil, false).
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).
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
mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", nil, sequencerAddress, mockState, &network, []*felt.Felt{}, false, true, false, nil, nil, false, nil, nil, false).
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).
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) {
mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", nil, sequencerAddress, mockState, &network, []*felt.Felt{}, false, true, false, nil, nil, false, nil, nil, false).
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).
Return(nil, nil, vm.TransactionExecutionError{
Index: 44,
Cause: errors.New("oops"),
Expand All @@ -3297,7 +3307,7 @@ func TestSimulateTransactions(t *testing.T) {
ExecutionError: "oops",
}), err)

mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", nil, sequencerAddress, mockState, &network, []*felt.Felt{}, false, true, true, nil, nil, true, nil, nil, false).
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).
Return(nil, nil, vm.TransactionExecutionError{
Index: 44,
Cause: errors.New("oops"),
Expand Down Expand Up @@ -3757,6 +3767,8 @@ 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)
_, rpcErr := handler.SimulateTransactions(rpc.BlockID{Latest: true}, []rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{rpc.SkipFeeChargeFlag})
assert.Equal(t, throttledErr, rpcErr.Data)
})
Expand Down Expand Up @@ -3826,23 +3838,29 @@ func TestEstimateFee(t *testing.T) {
sequencerAddress := network.BlockHashMetaInfo.FallBackSequencerAddress

t.Run("ok with zero values", func(t *testing.T) {
mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", nil, sequencerAddress, mockState, &network, []*felt.Felt{}, true, false, true, nil, nil, false, nil, nil, false).
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).
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) {
mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", nil, sequencerAddress, mockState, &network, []*felt.Felt{}, true, true, true, nil, nil, false, nil, nil, false).
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).
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) {
mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", nil, sequencerAddress, mockState, &network, []*felt.Felt{}, true, true, true, nil, nil, false, nil, nil, false).
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).
Return(nil, nil, vm.TransactionExecutionError{
Index: 44,
Cause: errors.New("oops"),
Expand All @@ -3854,7 +3872,7 @@ func TestEstimateFee(t *testing.T) {
ExecutionError: "oops",
}), err)

mockVM.EXPECT().Execute(nil, nil, uint64(0), uint64(0), "", nil, sequencerAddress, mockState, &network, []*felt.Felt{}, true, false, true, nil, nil, true, nil, nil, false).
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).
Return(nil, nil, vm.TransactionExecutionError{
Index: 44,
Cause: errors.New("oops"),
Expand Down

0 comments on commit bd6ec4f

Please sign in to comment.