From bd6ec4f2e08b901ceefe4dc9b9767aedd4229213 Mon Sep 17 00:00:00 2001 From: rianhughes Date: Mon, 26 Feb 2024 16:53:24 +0200 Subject: [PATCH] support for blockHash - 10 --- rpc/handlers.go | 62 +++++++++++++++++++++++++++++++++++++++++++- rpc/handlers_test.go | 34 ++++++++++++++++++------ 2 files changed, 87 insertions(+), 9 deletions(-) diff --git a/rpc/handlers.go b/rpc/handlers.go index 5fecb3181f..83d44a811b 100644 --- a/rpc/handlers.go +++ b/rpc/handlers.go @@ -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 @@ -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) { diff --git a/rpc/handlers_test.go b/rpc/handlers_test.go index 741aebfc9e..7db69e4502 100644 --- a/rpc/handlers_test.go +++ b/rpc/handlers_test.go @@ -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) }) @@ -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(), @@ -3269,7 +3273,9 @@ 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}) @@ -3277,7 +3283,9 @@ func TestSimulateTransactions(t *testing.T) { }) 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}) @@ -3285,7 +3293,9 @@ func TestSimulateTransactions(t *testing.T) { }) 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"), @@ -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"), @@ -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) }) @@ -3826,7 +3838,9 @@ 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}) @@ -3834,7 +3848,9 @@ func TestEstimateFee(t *testing.T) { }) 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}) @@ -3842,7 +3858,9 @@ func TestEstimateFee(t *testing.T) { }) 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"), @@ -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"),