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

Refactor: Reuse rpcv6 juno_version handler for v7 and v8 #2471

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 12 additions & 6 deletions rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@ type Handler struct {
rpcv6Handler *rpcv6.Handler
rpcv7Handler *rpcv7.Handler
rpcv8Handler *rpcv8.Handler
version string
}

func New(bcReader blockchain.Reader, syncReader sync.Reader, virtualMachine vm.VM, version string,
logger utils.Logger, network *utils.Network,
) *Handler {
handlerv6 := rpcv6.New(bcReader, syncReader, virtualMachine, version, network, logger)
handlerv7 := rpcv7.New(bcReader, syncReader, virtualMachine, version, network, logger)
handlerv8 := rpcv8.New(bcReader, syncReader, virtualMachine, version, logger)
handlerv6 := rpcv6.New(bcReader, syncReader, virtualMachine, network, logger)
handlerv7 := rpcv7.New(bcReader, syncReader, virtualMachine, network, logger)
handlerv8 := rpcv8.New(bcReader, syncReader, virtualMachine, logger)

return &Handler{
rpcv6Handler: handlerv6,
rpcv7Handler: handlerv7,
rpcv8Handler: handlerv8,
version: version,
}
}

Expand Down Expand Up @@ -77,6 +79,10 @@ func (h *Handler) WithGateway(gatewayClient rpccore.Gateway) *Handler {
return h
}

func (h *Handler) Version() (string, *jsonrpc.Error) {
return h.version, nil
}

func (h *Handler) Run(ctx context.Context) error {
g, ctx := errgroup.WithContext(ctx)

Expand Down Expand Up @@ -187,7 +193,7 @@ func (h *Handler) MethodsV0_8() ([]jsonrpc.Method, string) { //nolint: funlen
},
{
Name: "juno_version",
Handler: h.rpcv8Handler.Version,
Handler: h.Version,
},
{
Name: "starknet_getTransactionStatus",
Expand Down Expand Up @@ -381,7 +387,7 @@ func (h *Handler) MethodsV0_7() ([]jsonrpc.Method, string) { //nolint: funlen
},
{
Name: "juno_version",
Handler: h.rpcv7Handler.Version,
Handler: h.Version,
},
{
Name: "starknet_getTransactionStatus",
Expand Down Expand Up @@ -539,7 +545,7 @@ func (h *Handler) MethodsV0_6() ([]jsonrpc.Method, string) { //nolint: funlen
},
{
Name: "juno_version",
Handler: h.rpcv6Handler.Version,
Handler: h.Version,
},
{
Name: "starknet_getTransactionStatus",
Expand Down
23 changes: 17 additions & 6 deletions rpc/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,25 @@ import (
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/feed"
"github.com/NethermindEth/juno/mocks"
"github.com/NethermindEth/juno/rpc/v6"
"github.com/NethermindEth/juno/rpc/v7"
"github.com/NethermindEth/juno/rpc/v8"
rpcv6 "github.com/NethermindEth/juno/rpc/v6"
rpcv7 "github.com/NethermindEth/juno/rpc/v7"
rpcv8 "github.com/NethermindEth/juno/rpc/v8"
"github.com/NethermindEth/juno/sync"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func TestVersion(t *testing.T) {
const version = "1.2.3-rc1"

handler := New(nil, nil, nil, version, nil, nil)
ver, err := handler.Version()

require.Nil(t, err)
assert.Equal(t, version, ver)
}

func TestRun(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)
Expand All @@ -34,9 +45,9 @@ func TestRun(t *testing.T) {
mockSyncReader.EXPECT().SubscribePending().Return(sync.PendingSubscription{Subscription: pendingSub.Subscribe()}).AnyTimes()

handler := &Handler{
rpcv6Handler: rpcv6.New(mockBcReader, mockSyncReader, nil, "", nil, nil),
rpcv7Handler: rpcv7.New(mockBcReader, mockSyncReader, nil, "", nil, nil),
rpcv8Handler: rpcv8.New(mockBcReader, mockSyncReader, nil, "", nil),
rpcv6Handler: rpcv6.New(mockBcReader, mockSyncReader, nil, nil, nil),
rpcv7Handler: rpcv7.New(mockBcReader, mockSyncReader, nil, nil, nil),
rpcv8Handler: rpcv8.New(mockBcReader, mockSyncReader, nil, nil),
}

ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
Expand Down
20 changes: 10 additions & 10 deletions rpc/v6/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestBlockNumber(t *testing.T) {
t.Cleanup(mockCtrl.Finish)

mockReader := mocks.NewMockReader(mockCtrl)
handler := rpc.New(mockReader, nil, nil, "", utils.Ptr(utils.Mainnet), nil)
handler := rpc.New(mockReader, nil, nil, utils.Ptr(utils.Mainnet), nil)

t.Run("empty blockchain", func(t *testing.T) {
expectedHeight := uint64(0)
Expand All @@ -122,7 +122,7 @@ func TestBlockHashAndNumber(t *testing.T) {

n := utils.Ptr(utils.Mainnet)
mockReader := mocks.NewMockReader(mockCtrl)
handler := rpc.New(mockReader, nil, nil, "", n, nil)
handler := rpc.New(mockReader, nil, nil, n, nil)

t.Run("empty blockchain", func(t *testing.T) {
mockReader.EXPECT().Head().Return(nil, errors.New("empty blockchain"))
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestBlockTransactionCount(t *testing.T) {
mockSyncReader := mocks.NewMockSyncReader(mockCtrl)
n := utils.Ptr(utils.Sepolia)
mockReader := mocks.NewMockReader(mockCtrl)
handler := rpc.New(mockReader, mockSyncReader, nil, "", n, nil)
handler := rpc.New(mockReader, mockSyncReader, nil, n, nil)

client := feeder.NewTestClient(t, n)
gw := adaptfeeder.New(client)
Expand Down Expand Up @@ -259,7 +259,7 @@ func TestBlockWithTxHashes(t *testing.T) {
mockSyncReader = mocks.NewMockSyncReader(mockCtrl)
mockSyncReader.EXPECT().Pending().Return(nil, sync.ErrPendingBlockNotFound)
}
handler := rpc.New(chain, mockSyncReader, nil, "", n, log)
handler := rpc.New(chain, mockSyncReader, nil, n, log)

block, rpcErr := handler.BlockWithTxHashes(id)
assert.Nil(t, block)
Expand All @@ -268,7 +268,7 @@ func TestBlockWithTxHashes(t *testing.T) {
}

n := utils.Ptr(utils.Sepolia)
handler := rpc.New(mockReader, mockSyncReader, nil, "", n, nil)
handler := rpc.New(mockReader, mockSyncReader, nil, n, nil)

client := feeder.NewTestClient(t, n)
gw := adaptfeeder.New(client)
Expand Down Expand Up @@ -382,7 +382,7 @@ func TestBlockWithTxs(t *testing.T) {
mockSyncReader = mocks.NewMockSyncReader(mockCtrl)
mockSyncReader.EXPECT().Pending().Return(nil, sync.ErrPendingBlockNotFound)
}
handler := rpc.New(chain, mockSyncReader, nil, "", n, log)
handler := rpc.New(chain, mockSyncReader, nil, n, log)

block, rpcErr := handler.BlockWithTxs(id)
assert.Nil(t, block)
Expand All @@ -391,7 +391,7 @@ func TestBlockWithTxs(t *testing.T) {
}

n := utils.Ptr(utils.Mainnet)
handler := rpc.New(mockReader, mockSyncReader, nil, "", n, nil)
handler := rpc.New(mockReader, mockSyncReader, nil, n, nil)

client := feeder.NewTestClient(t, n)
gw := adaptfeeder.New(client)
Expand Down Expand Up @@ -509,7 +509,7 @@ func TestBlockWithTxHashesV013(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)
mockReader := mocks.NewMockReader(mockCtrl)
handler := rpc.New(mockReader, nil, nil, "", n, nil)
handler := rpc.New(mockReader, nil, nil, n, nil)

blockNumber := uint64(16350)
gw := adaptfeeder.New(feeder.NewTestClient(t, n))
Expand Down Expand Up @@ -578,7 +578,7 @@ func TestBlockWithReceipts(t *testing.T) {

n := utils.Ptr(utils.Mainnet)
mockReader := mocks.NewMockReader(mockCtrl)
handler := rpc.New(mockReader, mockSyncReader, nil, "", n, nil)
handler := rpc.New(mockReader, mockSyncReader, nil, n, nil)

t.Run("transaction not found", func(t *testing.T) {
blockID := rpc.BlockID{Number: 777}
Expand Down Expand Up @@ -696,7 +696,7 @@ func TestRpcBlockAdaptation(t *testing.T) {

n := utils.Ptr(utils.Sepolia)
mockReader := mocks.NewMockReader(mockCtrl)
handler := rpc.New(mockReader, nil, nil, "", n, nil)
handler := rpc.New(mockReader, nil, nil, n, nil)

client := feeder.NewTestClient(t, n)
gw := adaptfeeder.New(client)
Expand Down
2 changes: 1 addition & 1 deletion rpc/v6/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestChainId(t *testing.T) {

mockReader := mocks.NewMockReader(mockCtrl)
mockReader.EXPECT().Network().Return(n)
handler := rpc.New(mockReader, nil, nil, "", n, nil)
handler := rpc.New(mockReader, nil, nil, n, nil)

cID, err := handler.ChainID()
require.Nil(t, err)
Expand Down
10 changes: 5 additions & 5 deletions rpc/v6/class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestClass(t *testing.T) {
return nil
}, nil).AnyTimes()
mockReader.EXPECT().HeadsHeader().Return(new(core.Header), nil).AnyTimes()
handler := rpc.New(mockReader, nil, nil, "", n, utils.NewNopZapLogger())
handler := rpc.New(mockReader, nil, nil, n, utils.NewNopZapLogger())

latest := rpc.BlockID{Latest: true}

Expand Down Expand Up @@ -69,7 +69,7 @@ func TestClass(t *testing.T) {

t.Run("state by id error", func(t *testing.T) {
mockReader := mocks.NewMockReader(mockCtrl)
handler := rpc.New(mockReader, nil, nil, "", n, utils.NewNopZapLogger())
handler := rpc.New(mockReader, nil, nil, n, utils.NewNopZapLogger())

mockReader.EXPECT().HeadState().Return(nil, nil, db.ErrKeyNotFound)

Expand All @@ -81,7 +81,7 @@ func TestClass(t *testing.T) {
t.Run("class hash not found error", func(t *testing.T) {
mockReader := mocks.NewMockReader(mockCtrl)
mockState := mocks.NewMockStateHistoryReader(mockCtrl)
handler := rpc.New(mockReader, nil, nil, "", n, utils.NewNopZapLogger())
handler := rpc.New(mockReader, nil, nil, n, utils.NewNopZapLogger())

mockReader.EXPECT().HeadState().Return(mockState, func() error {
return nil
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestClassAt(t *testing.T) {
return nil
}, nil).AnyTimes()
mockReader.EXPECT().HeadsHeader().Return(new(core.Header), nil).AnyTimes()
handler := rpc.New(mockReader, nil, nil, "", n, utils.NewNopZapLogger())
handler := rpc.New(mockReader, nil, nil, n, utils.NewNopZapLogger())

latest := rpc.BlockID{Latest: true}

Expand Down Expand Up @@ -154,7 +154,7 @@ func TestClassHashAt(t *testing.T) {
n := utils.Ptr(utils.Mainnet)
mockReader := mocks.NewMockReader(mockCtrl)
log := utils.NewNopZapLogger()
handler := rpc.New(mockReader, nil, nil, "", n, log)
handler := rpc.New(mockReader, nil, nil, n, log)

t.Run("empty blockchain", func(t *testing.T) {
mockReader.EXPECT().HeadState().Return(nil, nil, db.ErrKeyNotFound)
Expand Down
4 changes: 2 additions & 2 deletions rpc/v6/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestNonce(t *testing.T) {
n := utils.Ptr(utils.Mainnet)
mockReader := mocks.NewMockReader(mockCtrl)
log := utils.NewNopZapLogger()
handler := rpc.New(mockReader, nil, nil, "", n, log)
handler := rpc.New(mockReader, nil, nil, n, log)

t.Run("empty blockchain", func(t *testing.T) {
mockReader.EXPECT().HeadState().Return(nil, nil, db.ErrKeyNotFound)
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestStorageAt(t *testing.T) {
n := utils.Ptr(utils.Mainnet)
mockReader := mocks.NewMockReader(mockCtrl)
log := utils.NewNopZapLogger()
handler := rpc.New(mockReader, nil, nil, "", n, log)
handler := rpc.New(mockReader, nil, nil, n, log)

t.Run("empty blockchain", func(t *testing.T) {
mockReader.EXPECT().HeadState().Return(nil, nil, db.ErrKeyNotFound)
Expand Down
2 changes: 1 addition & 1 deletion rpc/v6/estimate_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestEstimateMessageFee(t *testing.T) {
mockReader.EXPECT().Network().Return(n).AnyTimes()
mockVM := mocks.NewMockVM(mockCtrl)

handler := rpc.New(mockReader, nil, mockVM, "", n, utils.NewNopZapLogger())
handler := rpc.New(mockReader, nil, mockVM, n, utils.NewNopZapLogger())
msg := rpc.MsgFromL1{
From: common.HexToAddress("0xDEADBEEF"),
To: *new(felt.Felt).SetUint64(1337),
Expand Down
2 changes: 1 addition & 1 deletion rpc/v6/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestEvents(t *testing.T) {
}
}

handler := rpc.New(chain, nil, nil, "", n, utils.NewNopZapLogger())
handler := rpc.New(chain, nil, nil, n, utils.NewNopZapLogger())
from := utils.HexToFelt(t, "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7")
args := rpc.EventsArg{
EventFilter: rpc.EventFilter{
Expand Down
8 changes: 1 addition & 7 deletions rpc/v6/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type Handler struct {
newHeads *feed.Feed[*core.Block]

log utils.Logger
version string
blockTraceCache *lru.Cache[traceCacheKey, []TracedBlockTransaction]

filterLimit uint
Expand All @@ -50,7 +49,7 @@ type subscription struct {
conn jsonrpc.Conn
}

func New(bcReader blockchain.Reader, syncReader sync.Reader, virtualMachine vm.VM, version string, network *utils.Network,
func New(bcReader blockchain.Reader, syncReader sync.Reader, virtualMachine vm.VM, network *utils.Network,
logger utils.Logger,
) *Handler {
return &Handler{
Expand All @@ -64,7 +63,6 @@ func New(bcReader blockchain.Reader, syncReader sync.Reader, virtualMachine vm.V
}
return n
},
version: version,
newHeads: feed.New[*core.Block](),

blockTraceCache: lru.NewCache[traceCacheKey, []TracedBlockTransaction](rpccore.TraceCacheSize),
Expand Down Expand Up @@ -98,10 +96,6 @@ func (h *Handler) WithGateway(gatewayClient rpccore.Gateway) *Handler {
return h
}

func (h *Handler) Version() (string, *jsonrpc.Error) {
return h.version, nil
}

func (h *Handler) SpecVersion() (string, *jsonrpc.Error) {
return "0.6.0", nil
}
Expand Down
13 changes: 2 additions & 11 deletions rpc/v6/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,8 @@ import (

func nopCloser() error { return nil }

func TestVersion(t *testing.T) {
const version = "1.2.3-rc1"

handler := rpc.New(nil, nil, nil, version, utils.Ptr(utils.Mainnet), nil)
ver, err := handler.Version()
require.Nil(t, err)
assert.Equal(t, version, ver)
}

func TestSpecVersion(t *testing.T) {
handler := rpc.New(nil, nil, nil, "", utils.Ptr(utils.Mainnet), nil)
handler := rpc.New(nil, nil, nil, utils.Ptr(utils.Mainnet), nil)
legacyVersion, rpcErr := handler.SpecVersion()
require.Nil(t, rpcErr)
require.Equal(t, "0.6.0", legacyVersion)
Expand All @@ -42,7 +33,7 @@ func TestThrottledVMError(t *testing.T) {
mockSyncReader := mocks.NewMockSyncReader(mockCtrl)

throttledVM := node.NewThrottledVM(mockVM, 0, 0)
handler := rpc.New(mockReader, mockSyncReader, throttledVM, "", utils.Ptr(utils.Mainnet), nil)
handler := rpc.New(mockReader, mockSyncReader, throttledVM, utils.Ptr(utils.Mainnet), nil)
mockState := mocks.NewMockStateHistoryReader(mockCtrl)

throttledErr := "VM throughput limit reached"
Expand Down
2 changes: 1 addition & 1 deletion rpc/v6/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestSimulateTransactions(t *testing.T) {
mockReader := mocks.NewMockReader(mockCtrl)
mockReader.EXPECT().Network().Return(n).AnyTimes()
mockVM := mocks.NewMockVM(mockCtrl)
handler := rpc.New(mockReader, nil, mockVM, "", n, utils.NewNopZapLogger())
handler := rpc.New(mockReader, nil, mockVM, n, utils.NewNopZapLogger())

mockState := mocks.NewMockStateHistoryReader(mockCtrl)
mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil).AnyTimes()
Expand Down
4 changes: 2 additions & 2 deletions rpc/v6/state_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestStateUpdate(t *testing.T) {
mockSyncReader = mocks.NewMockSyncReader(mockCtrl)
mockSyncReader.EXPECT().Pending().Return(nil, sync.ErrPendingBlockNotFound)
}
handler := rpc.New(chain, mockSyncReader, nil, "", n, nil)
handler := rpc.New(chain, mockSyncReader, nil, n, nil)

update, rpcErr := handler.StateUpdate(id)
assert.Nil(t, update)
Expand All @@ -48,7 +48,7 @@ func TestStateUpdate(t *testing.T) {
}

mockReader := mocks.NewMockReader(mockCtrl)
handler := rpc.New(mockReader, mockSyncReader, nil, "", n, nil)
handler := rpc.New(mockReader, mockSyncReader, nil, n, nil)
client := feeder.NewTestClient(t, n)
mainnetGw := adaptfeeder.New(client)

Expand Down
2 changes: 1 addition & 1 deletion rpc/v6/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestSyncing(t *testing.T) {
n := utils.Ptr(utils.Mainnet)
synchronizer := mocks.NewMockSyncReader(mockCtrl)
mockReader := mocks.NewMockReader(mockCtrl)
handler := rpc.New(mockReader, synchronizer, nil, "", n, nil)
handler := rpc.New(mockReader, synchronizer, nil, n, nil)
defaultSyncState := false

startingBlock := uint64(0)
Expand Down
Loading
Loading