Skip to content

Commit

Permalink
Remove v0.6 endpoints, added v0.8 endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan authored and derrix060 committed Oct 21, 2024
1 parent 0ab532f commit 2614b70
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 321 deletions.
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
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
4 changes: 2 additions & 2 deletions rpc/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,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 +648,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
82 changes: 0 additions & 82 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 Down
42 changes: 23 additions & 19 deletions rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ const (
)

type traceCacheKey struct {
blockHash felt.Felt
v0_6Response bool
blockHash felt.Felt
}

type Handler struct {
Expand Down Expand Up @@ -164,14 +163,14 @@ func (h *Handler) Version() (string, *jsonrpc.Error) {
}

func (h *Handler) SpecVersion() (string, *jsonrpc.Error) {
return "0.7.1", nil
return "0.8.0", nil
}

func (h *Handler) SpecVersionV0_6() (string, *jsonrpc.Error) {
return "0.6.0", nil
func (h *Handler) SpecVersionV0_7() (string, *jsonrpc.Error) {
return "0.7.1", nil
}

func (h *Handler) Methods() ([]jsonrpc.Method, string) { //nolint: funlen
func (h *Handler) Methods() ([]jsonrpc.Method, string) { //nolint: funlen, dupl
return []jsonrpc.Method{
{
Name: "starknet_chainId",
Expand Down Expand Up @@ -326,10 +325,10 @@ func (h *Handler) Methods() ([]jsonrpc.Method, string) { //nolint: funlen
Params: []jsonrpc.Parameter{{Name: "block_id"}},
Handler: h.BlockWithReceipts,
},
}, "/v0_7"
}, "/v0_8"
}

func (h *Handler) MethodsV0_6() ([]jsonrpc.Method, string) { //nolint: funlen
func (h *Handler) MethodsV0_7() ([]jsonrpc.Method, string) { //nolint: funlen, dupl
return []jsonrpc.Method{
{
Name: "starknet_chainId",
Expand All @@ -346,12 +345,12 @@ func (h *Handler) MethodsV0_6() ([]jsonrpc.Method, string) { //nolint: funlen
{
Name: "starknet_getBlockWithTxHashes",
Params: []jsonrpc.Parameter{{Name: "block_id"}},
Handler: h.BlockWithTxHashesV0_6,
Handler: h.BlockWithTxHashes,
},
{
Name: "starknet_getBlockWithTxs",
Params: []jsonrpc.Parameter{{Name: "block_id"}},
Handler: h.BlockWithTxsV0_6,
Handler: h.BlockWithTxs,
},
{
Name: "starknet_getTransactionByHash",
Expand All @@ -361,7 +360,7 @@ func (h *Handler) MethodsV0_6() ([]jsonrpc.Method, string) { //nolint: funlen
{
Name: "starknet_getTransactionReceipt",
Params: []jsonrpc.Parameter{{Name: "transaction_hash"}},
Handler: h.TransactionReceiptByHashV0_6,
Handler: h.TransactionReceiptByHash,
},
{
Name: "starknet_getBlockTransactionCount",
Expand Down Expand Up @@ -439,36 +438,36 @@ func (h *Handler) MethodsV0_6() ([]jsonrpc.Method, string) { //nolint: funlen
{
Name: "starknet_call",
Params: []jsonrpc.Parameter{{Name: "request"}, {Name: "block_id"}},
Handler: h.CallV0_6,
Handler: h.Call,
},
{
Name: "starknet_estimateFee",
Params: []jsonrpc.Parameter{{Name: "request"}, {Name: "simulation_flags"}, {Name: "block_id"}},
Handler: h.EstimateFeeV0_6,
Handler: h.EstimateFee,
},
{
Name: "starknet_estimateMessageFee",
Params: []jsonrpc.Parameter{{Name: "message"}, {Name: "block_id"}},
Handler: h.EstimateMessageFeeV0_6,
Handler: h.EstimateMessageFee,
},
{
Name: "starknet_traceTransaction",
Params: []jsonrpc.Parameter{{Name: "transaction_hash"}},
Handler: h.TraceTransactionV0_6,
Handler: h.TraceTransaction,
},
{
Name: "starknet_simulateTransactions",
Params: []jsonrpc.Parameter{{Name: "block_id"}, {Name: "transactions"}, {Name: "simulation_flags"}},
Handler: h.SimulateTransactionsV0_6,
Handler: h.SimulateTransactions,
},
{
Name: "starknet_traceBlockTransactions",
Params: []jsonrpc.Parameter{{Name: "block_id"}},
Handler: h.TraceBlockTransactionsV0_6,
Handler: h.TraceBlockTransactions,
},
{
Name: "starknet_specVersion",
Handler: h.SpecVersionV0_6,
Handler: h.SpecVersion,
},
{
Name: "juno_subscribeNewHeads",
Expand All @@ -479,5 +478,10 @@ func (h *Handler) MethodsV0_6() ([]jsonrpc.Method, string) { //nolint: funlen
Params: []jsonrpc.Parameter{{Name: "id"}},
Handler: h.Unsubscribe,
},
}, "/v0_6"
{
Name: "starknet_getBlockWithReceipts",
Params: []jsonrpc.Parameter{{Name: "block_id"}},
Handler: h.BlockWithReceipts,
},
}, "/v0_7"
}
6 changes: 3 additions & 3 deletions rpc/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ func TestSpecVersion(t *testing.T) {
handler := rpc.New(nil, nil, nil, "", nil)
version, rpcErr := handler.SpecVersion()
require.Nil(t, rpcErr)
require.Equal(t, "0.7.1", version)
require.Equal(t, "0.8.0", version)

legacyVersion, rpcErr := handler.SpecVersionV0_6()
legacyVersion, rpcErr := handler.SpecVersionV0_7()
require.Nil(t, rpcErr)
require.Equal(t, "0.6.0", legacyVersion)
require.Equal(t, "0.7.1", legacyVersion)
}

func TestThrottledVMError(t *testing.T) {
Expand Down
Loading

0 comments on commit 2614b70

Please sign in to comment.