Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove v0.6 endpoints, added v0.8 endpoint #2229

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions mocks/mock_vm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen
return nil, err
}
jsonrpcServerLegacy := jsonrpc.NewServer(maxGoroutines, log).WithValidator(validator.Validator())
legacyMethods, legacyPath := rpcHandler.MethodsV0_6()
legacyMethods, legacyPath := rpcHandler.MethodsV0_7()
if err = jsonrpcServerLegacy.RegisterMethods(legacyMethods...); err != nil {
return nil, err
}
Expand Down
9 changes: 4 additions & 5 deletions node/throttled_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@
}

func (tvm *ThrottledVM) Call(callInfo *vm.CallInfo, blockInfo *vm.BlockInfo, state core.StateReader,
network *utils.Network, maxSteps uint64, useBlobData bool,
network *utils.Network, maxSteps uint64,
) ([]*felt.Felt, error) {
var ret []*felt.Felt
return ret, tvm.Do(func(vm *vm.VM) error {
var err error
ret, err = (*vm).Call(callInfo, blockInfo, state, network, maxSteps, useBlobData)
ret, err = (*vm).Call(callInfo, blockInfo, state, network, maxSteps)

Check warning on line 28 in node/throttled_vm.go

View check run for this annotation

Codecov / codecov/patch

node/throttled_vm.go#L28

Added line #L28 was not covered by tests
return err
})
}

func (tvm *ThrottledVM) Execute(txns []core.Transaction, declaredClasses []core.Class, paidFeesOnL1 []*felt.Felt,
blockInfo *vm.BlockInfo, state core.StateReader, network *utils.Network, skipChargeFee, skipValidate, errOnRevert,
useBlobData bool,
blockInfo *vm.BlockInfo, state core.StateReader, network *utils.Network, skipChargeFee, skipValidate, errOnRevert bool,
) ([]*felt.Felt, []core.GasConsumed, []vm.TransactionTrace, uint64, error) {
var ret []*felt.Felt
var traces []vm.TransactionTrace
Expand All @@ -41,7 +40,7 @@
return ret, daGas, traces, numSteps, tvm.Do(func(vm *vm.VM) error {
var err error
ret, daGas, traces, numSteps, err = (*vm).Execute(txns, declaredClasses, paidFeesOnL1, blockInfo, state, network,
skipChargeFee, skipValidate, errOnRevert, useBlobData)
skipChargeFee, skipValidate, errOnRevert)

Check warning on line 43 in node/throttled_vm.go

View check run for this annotation

Codecov / codecov/patch

node/throttled_vm.go#L43

Added line #L43 was not covered by tests
return err
})
}
24 changes: 1 addition & 23 deletions rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,6 @@ func (h *Handler) BlockWithTxHashes(id BlockID) (*BlockWithTxHashes, *jsonrpc.Er
}, nil
}

func (h *Handler) BlockWithTxHashesV0_6(id BlockID) (*BlockWithTxHashes, *jsonrpc.Error) {
resp, err := h.BlockWithTxHashes(id)
if err != nil {
return nil, err
}

resp.L1DAMode = nil
resp.L1DataGasPrice = nil
return resp, nil
}

// BlockTransactionCount returns the number of transactions in a block
// identified by the given BlockID.
//
Expand Down Expand Up @@ -240,7 +229,7 @@ func (h *Handler) BlockWithReceipts(id BlockID) (*BlockWithReceipts, *jsonrpc.Er
txsWithReceipts[index] = TransactionWithReceipt{
Transaction: AdaptTransaction(txn),
// block_hash, block_number are optional in BlockWithReceipts response
Receipt: AdaptReceipt(r, txn, finalityStatus, nil, 0, false),
Receipt: AdaptReceipt(r, txn, finalityStatus, nil, 0),
}
}

Expand Down Expand Up @@ -278,17 +267,6 @@ func (h *Handler) BlockWithTxs(id BlockID) (*BlockWithTxs, *jsonrpc.Error) {
}, nil
}

func (h *Handler) BlockWithTxsV0_6(id BlockID) (*BlockWithTxs, *jsonrpc.Error) {
resp, err := h.BlockWithTxs(id)
if err != nil {
return nil, err
}

resp.L1DAMode = nil
resp.L1DataGasPrice = nil
return resp, nil
}

func (h *Handler) blockStatus(id BlockID, block *core.Block) (BlockStatus, *jsonrpc.Error) {
l1H, jsonErr := h.l1Head()
if jsonErr != nil {
Expand Down
11 changes: 8 additions & 3 deletions rpc/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func TestBlockWithTxHashesV013(t *testing.T) {

mockReader.EXPECT().BlockByNumber(gomock.Any()).Return(coreBlock, nil)
mockReader.EXPECT().L1Head().Return(&core.L1Head{}, nil)
got, rpcErr := handler.BlockWithTxsV0_6(rpc.BlockID{Number: blockNumber})
got, rpcErr := handler.BlockWithTxs(rpc.BlockID{Number: blockNumber})
require.Nil(t, rpcErr)
got.Transactions = got.Transactions[:1]

Expand All @@ -512,10 +512,15 @@ func TestBlockWithTxHashesV013(t *testing.T) {
NewRoot: coreBlock.GlobalStateRoot,
Number: &coreBlock.Number,
ParentHash: coreBlock.ParentHash,
L1DAMode: utils.Ptr(rpc.Blob),
L1GasPrice: &rpc.ResourcePrice{
InFri: utils.HexToFelt(t, "0x17882b6aa74"),
InWei: utils.HexToFelt(t, "0x3b9aca10"),
},
L1DataGasPrice: &rpc.ResourcePrice{
InFri: utils.HexToFelt(t, "0x2cc6d7f596e1"),
InWei: utils.HexToFelt(t, "0x716a8f6dd"),
},
SequencerAddress: coreBlock.SequencerAddress,
Timestamp: coreBlock.Timestamp,
},
Expand Down Expand Up @@ -604,7 +609,7 @@ func TestBlockWithReceipts(t *testing.T) {

txsWithReceipt = append(txsWithReceipt, rpc.TransactionWithReceipt{
Transaction: adaptedTx,
Receipt: rpc.AdaptReceipt(receipt, tx, rpc.TxnAcceptedOnL2, nil, 0, false),
Receipt: rpc.AdaptReceipt(receipt, tx, rpc.TxnAcceptedOnL2, nil, 0),
})
}

Expand Down Expand Up @@ -648,7 +653,7 @@ func TestBlockWithReceipts(t *testing.T) {

transactions = append(transactions, rpc.TransactionWithReceipt{
Transaction: adaptedTx,
Receipt: rpc.AdaptReceipt(receipt, tx, rpc.TxnAcceptedOnL1, nil, 0, false),
Receipt: rpc.AdaptReceipt(receipt, tx, rpc.TxnAcceptedOnL1, nil, 0),
})
}

Expand Down
50 changes: 1 addition & 49 deletions rpc/estimate_fee.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rpc

import (
"encoding/json"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -36,27 +35,6 @@ type FeeEstimate struct {
DataGasPrice *felt.Felt `json:"data_gas_price"`
OverallFee *felt.Felt `json:"overall_fee"`
Unit *FeeUnit `json:"unit,omitempty"`
// pre 13.1 response
v0_6Response bool
}

func (f FeeEstimate) MarshalJSON() ([]byte, error) {
if f.v0_6Response {
return json.Marshal(struct {
GasConsumed *felt.Felt `json:"gas_consumed"`
GasPrice *felt.Felt `json:"gas_price"`
OverallFee *felt.Felt `json:"overall_fee"`
Unit *FeeUnit `json:"unit,omitempty"`
}{
GasConsumed: f.GasConsumed,
GasPrice: f.GasPrice,
OverallFee: f.OverallFee,
Unit: f.Unit,
})
} else {
type alias FeeEstimate // avoid infinite recursion
return json.Marshal(alias(f))
}
}

/****************************************************
Expand All @@ -66,20 +44,7 @@ func (f FeeEstimate) MarshalJSON() ([]byte, error) {
func (h *Handler) EstimateFee(broadcastedTxns []BroadcastedTransaction,
simulationFlags []SimulationFlag, id BlockID,
) ([]FeeEstimate, http.Header, *jsonrpc.Error) {
result, httpHeader, err := h.simulateTransactions(id, broadcastedTxns, append(simulationFlags, SkipFeeChargeFlag), false, true)
if err != nil {
return nil, httpHeader, err
}

return utils.Map(result, func(tx SimulatedTransaction) FeeEstimate {
return tx.FeeEstimation
}), httpHeader, nil
}

func (h *Handler) EstimateFeeV0_6(broadcastedTxns []BroadcastedTransaction,
simulationFlags []SimulationFlag, id BlockID,
) ([]FeeEstimate, http.Header, *jsonrpc.Error) {
result, httpHeader, err := h.simulateTransactions(id, broadcastedTxns, append(simulationFlags, SkipFeeChargeFlag), true, true)
result, httpHeader, err := h.simulateTransactions(id, broadcastedTxns, append(simulationFlags, SkipFeeChargeFlag), true)
if err != nil {
return nil, httpHeader, err
}
Expand All @@ -93,19 +58,6 @@ func (h *Handler) EstimateMessageFee(msg MsgFromL1, id BlockID) (*FeeEstimate, h
return h.estimateMessageFee(msg, id, h.EstimateFee)
}

func (h *Handler) EstimateMessageFeeV0_6(msg MsgFromL1, id BlockID) (*FeeEstimate, http.Header, *jsonrpc.Error) { //nolint:gocritic
feeEstimate, httpHeader, rpcErr := h.estimateMessageFee(msg, id, h.EstimateFeeV0_6)
if rpcErr != nil {
return nil, httpHeader, rpcErr
}

feeEstimate.v0_6Response = true
feeEstimate.DataGasPrice = nil
feeEstimate.DataGasConsumed = nil

return feeEstimate, httpHeader, nil
}

type estimateFeeHandler func(broadcastedTxns []BroadcastedTransaction,
simulationFlags []SimulationFlag, id BlockID,
) ([]FeeEstimate, http.Header, *jsonrpc.Error)
Expand Down
88 changes: 3 additions & 85 deletions rpc/estimate_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,101 +3,19 @@ package rpc_test
import (
"encoding/json"
"errors"
"fmt"
"testing"

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/mocks"
"github.com/NethermindEth/juno/rpc"
"github.com/NethermindEth/juno/utils"
"github.com/NethermindEth/juno/vm"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func TestEstimateMessageFeeV0_6(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)

n := utils.Ptr(utils.Mainnet)
mockReader := mocks.NewMockReader(mockCtrl)
mockReader.EXPECT().Network().Return(n).AnyTimes()
mockVM := mocks.NewMockVM(mockCtrl)

handler := rpc.New(mockReader, nil, mockVM, "", utils.NewNopZapLogger())
msg := rpc.MsgFromL1{
From: common.HexToAddress("0xDEADBEEF"),
To: *new(felt.Felt).SetUint64(1337),
Payload: []felt.Felt{*new(felt.Felt).SetUint64(1), *new(felt.Felt).SetUint64(2)},
Selector: *new(felt.Felt).SetUint64(44),
}

t.Run("block not found", func(t *testing.T) {
mockReader.EXPECT().HeadState().Return(nil, nil, db.ErrKeyNotFound)
_, httpHeader, err := handler.EstimateMessageFeeV0_6(msg, rpc.BlockID{Latest: true})
require.Equal(t, rpc.ErrBlockNotFound, err)
require.NotEmpty(t, httpHeader.Get(rpc.ExecutionStepsHeader))
})

latestHeader := &core.Header{
Number: 9,
Timestamp: 456,
GasPrice: new(felt.Felt).SetUint64(42),
}
mockState := mocks.NewMockStateHistoryReader(mockCtrl)

mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil)
mockReader.EXPECT().HeadsHeader().Return(latestHeader, nil)

expectedGasConsumed := new(felt.Felt).SetUint64(37)
mockVM.EXPECT().Execute(gomock.Any(), gomock.Any(), gomock.Any(), &vm.BlockInfo{
Header: latestHeader,
}, gomock.Any(), &utils.Mainnet, gomock.Any(), false, true, false).DoAndReturn(
func(txns []core.Transaction, declaredClasses []core.Class, paidFeesOnL1 []*felt.Felt, blockInfo *vm.BlockInfo,
state core.StateReader, network *utils.Network, skipChargeFee, skipValidate, errOnRevert, useBlobData bool,
) ([]*felt.Felt, []core.GasConsumed, []vm.TransactionTrace, uint64, error) {
require.Len(t, txns, 1)
assert.NotNil(t, txns[0].(*core.L1HandlerTransaction))

assert.Empty(t, declaredClasses)
assert.Len(t, paidFeesOnL1, 1)

actualFee := new(felt.Felt).Mul(expectedGasConsumed, blockInfo.Header.GasPrice)
daGas := []core.GasConsumed{{L1Gas: 0, L1DataGas: 0}}
return []*felt.Felt{actualFee}, daGas, []vm.TransactionTrace{{
StateDiff: &vm.StateDiff{
StorageDiffs: []vm.StorageDiff{},
Nonces: []vm.Nonce{},
DeployedContracts: []vm.DeployedContract{},
DeprecatedDeclaredClasses: []*felt.Felt{},
DeclaredClasses: []vm.DeclaredClass{},
ReplacedClasses: []vm.ReplacedClass{},
},
}}, uint64(123), nil
},
)

estimateFee, httpHeader, err := handler.EstimateMessageFeeV0_6(msg, rpc.BlockID{Latest: true})
require.Nil(t, err)
expectedJSON := fmt.Sprintf(
`{"gas_consumed":%q,"gas_price":%q,"overall_fee":%q,"unit":"WEI"}`,
expectedGasConsumed,
latestHeader.GasPrice,
new(felt.Felt).Mul(expectedGasConsumed, latestHeader.GasPrice),
)

// we check json response here because some fields are private and we can't set them and assert.Equal fails
// also in 0.6 response some fields should not be presented
estimateFeeJSON, jsonErr := json.Marshal(estimateFee)
require.NoError(t, jsonErr)
require.Equal(t, expectedJSON, string(estimateFeeJSON))
require.NotEmpty(t, httpHeader.Get(rpc.ExecutionStepsHeader))
}

func TestEstimateFee(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
Expand All @@ -116,7 +34,7 @@ func TestEstimateFee(t *testing.T) {

blockInfo := vm.BlockInfo{Header: &core.Header{}}
t.Run("ok with zero values", func(t *testing.T) {
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &blockInfo, mockState, n, true, false, true, true).
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &blockInfo, mockState, n, true, false, true).
Return([]*felt.Felt{}, []core.GasConsumed{}, []vm.TransactionTrace{}, uint64(123), nil)

_, httpHeader, err := handler.EstimateFee([]rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{}, rpc.BlockID{Latest: true})
Expand All @@ -125,7 +43,7 @@ func TestEstimateFee(t *testing.T) {
})

t.Run("ok with zero values, skip validate", func(t *testing.T) {
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &blockInfo, mockState, n, true, true, true, true).
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &blockInfo, mockState, n, true, true, true).
Return([]*felt.Felt{}, []core.GasConsumed{}, []vm.TransactionTrace{}, uint64(123), nil)

_, httpHeader, err := handler.EstimateFee([]rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{rpc.SkipValidateFlag}, rpc.BlockID{Latest: true})
Expand All @@ -134,7 +52,7 @@ func TestEstimateFee(t *testing.T) {
})

t.Run("transaction execution error", func(t *testing.T) {
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &blockInfo, mockState, n, true, true, true, true).
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &blockInfo, mockState, n, true, true, true).
Return(nil, nil, nil, uint64(0), vm.TransactionExecutionError{
Index: 44,
Cause: errors.New("oops"),
Expand Down
Loading