From 9c36fa6c7cfbabcbd541cb9718cd6cc5668fdd4c Mon Sep 17 00:00:00 2001 From: Rian Hughes Date: Thu, 12 Oct 2023 13:22:48 +0100 Subject: [PATCH] Implement rpc05 specVersion (#416) * Implement rpc05 specVersion --- README.md | 1 + account/account.go | 3 + mocks/mock_account.go | 127 +++++++++++++++++++++++++++++++++---- mocks/mock_rpc_provider.go | 15 +++++ rpc/provider.go | 1 + rpc/version.go | 12 ++++ rpc/version_test.go | 32 ++++++++++ 7 files changed, 179 insertions(+), 12 deletions(-) create mode 100644 rpc/version.go create mode 100644 rpc/version_test.go diff --git a/README.md b/README.md index 32a05442..61d37846 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,7 @@ go run main.go | `starknet_addDeployAccountTransaction` | :heavy_check_mark: | | `starknet_traceTransaction` | :heavy_check_mark: | | `starknet_simulateTransaction` | :heavy_check_mark: | +| `starknet_specVersion` | :heavy_check_mark: | | `starknet_traceBlockTransactions` | :heavy_check_mark: | ### Run Tests diff --git a/account/account.go b/account/account.go index 0ab8c708..65e39efc 100644 --- a/account/account.go +++ b/account/account.go @@ -389,6 +389,9 @@ func (account *Account) StorageAt(ctx context.Context, contractAddress *felt.Fel func (account *Account) StateUpdate(ctx context.Context, blockID rpc.BlockID) (*rpc.StateUpdateOutput, error) { return account.provider.StateUpdate(ctx, blockID) } +func (account *Account) SpecVersion(ctx context.Context) (string, error) { + return account.provider.SpecVersion(ctx) +} func (account *Account) Syncing(ctx context.Context) (*rpc.SyncStatus, error) { return account.provider.Syncing(ctx) } diff --git a/mocks/mock_account.go b/mocks/mock_account.go index 373cf36f..d4df8b8d 100644 --- a/mocks/mock_account.go +++ b/mocks/mock_account.go @@ -7,6 +7,7 @@ package mocks import ( context "context" reflect "reflect" + time "time" felt "github.com/NethermindEth/juno/core/felt" rpc "github.com/NethermindEth/starknet.go/rpc" @@ -36,32 +37,134 @@ func (m *MockAccountInterface) EXPECT() *MockAccountInterfaceMockRecorder { return m.recorder } -// Call mocks base method. -func (m *MockAccountInterface) Call(ctx context.Context, call rpc.FunctionCall) ([]*felt.Felt, error) { +// PrecomputeAddress mocks base method. +func (m *MockAccountInterface) PrecomputeAddress(deployerAddress, salt, classHash *felt.Felt, constructorCalldata []*felt.Felt) (*felt.Felt, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Call", ctx, call) + ret := m.ctrl.Call(m, "PrecomputeAddress", deployerAddress, salt, classHash, constructorCalldata) + ret0, _ := ret[0].(*felt.Felt) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// PrecomputeAddress indicates an expected call of PrecomputeAddress. +func (mr *MockAccountInterfaceMockRecorder) PrecomputeAddress(deployerAddress, salt, classHash, constructorCalldata interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PrecomputeAddress", reflect.TypeOf((*MockAccountInterface)(nil).PrecomputeAddress), deployerAddress, salt, classHash, constructorCalldata) +} + +// Sign mocks base method. +func (m *MockAccountInterface) Sign(ctx context.Context, msg *felt.Felt) ([]*felt.Felt, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Sign", ctx, msg) ret0, _ := ret[0].([]*felt.Felt) ret1, _ := ret[1].(error) return ret0, ret1 } -// Call indicates an expected call of Call. -func (mr *MockAccountInterfaceMockRecorder) Call(ctx, call interface{}) *gomock.Call { +// Sign indicates an expected call of Sign. +func (mr *MockAccountInterfaceMockRecorder) Sign(ctx, msg interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Sign", reflect.TypeOf((*MockAccountInterface)(nil).Sign), ctx, msg) +} + +// SignDeclareTransaction mocks base method. +func (m *MockAccountInterface) SignDeclareTransaction(ctx context.Context, tx *rpc.DeclareTxnV2) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SignDeclareTransaction", ctx, tx) + ret0, _ := ret[0].(error) + return ret0 +} + +// SignDeclareTransaction indicates an expected call of SignDeclareTransaction. +func (mr *MockAccountInterfaceMockRecorder) SignDeclareTransaction(ctx, tx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SignDeclareTransaction", reflect.TypeOf((*MockAccountInterface)(nil).SignDeclareTransaction), ctx, tx) +} + +// SignDeployAccountTransaction mocks base method. +func (m *MockAccountInterface) SignDeployAccountTransaction(ctx context.Context, tx *rpc.DeployAccountTxn, precomputeAddress *felt.Felt) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SignDeployAccountTransaction", ctx, tx, precomputeAddress) + ret0, _ := ret[0].(error) + return ret0 +} + +// SignDeployAccountTransaction indicates an expected call of SignDeployAccountTransaction. +func (mr *MockAccountInterfaceMockRecorder) SignDeployAccountTransaction(ctx, tx, precomputeAddress interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SignDeployAccountTransaction", reflect.TypeOf((*MockAccountInterface)(nil).SignDeployAccountTransaction), ctx, tx, precomputeAddress) +} + +// SignInvokeTransaction mocks base method. +func (m *MockAccountInterface) SignInvokeTransaction(ctx context.Context, tx *rpc.InvokeTxnV1) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SignInvokeTransaction", ctx, tx) + ret0, _ := ret[0].(error) + return ret0 +} + +// SignInvokeTransaction indicates an expected call of SignInvokeTransaction. +func (mr *MockAccountInterfaceMockRecorder) SignInvokeTransaction(ctx, tx interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Call", reflect.TypeOf((*MockAccountInterface)(nil).Call), ctx, call) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SignInvokeTransaction", reflect.TypeOf((*MockAccountInterface)(nil).SignInvokeTransaction), ctx, tx) } -// TransactionHash mocks base method. -func (m *MockAccountInterface) TransactionHash(calls rpc.FunctionCall, txDetails rpc.TxDetails) (*felt.Felt, error) { +// TransactionHashDeclare mocks base method. +func (m *MockAccountInterface) TransactionHashDeclare(tx rpc.DeclareTxnType) (*felt.Felt, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "TransactionHash", calls, txDetails) + ret := m.ctrl.Call(m, "TransactionHashDeclare", tx) ret0, _ := ret[0].(*felt.Felt) ret1, _ := ret[1].(error) return ret0, ret1 } -// TransactionHash indicates an expected call of TransactionHash. -func (mr *MockAccountInterfaceMockRecorder) TransactionHash(calls, txDetails interface{}) *gomock.Call { +// TransactionHashDeclare indicates an expected call of TransactionHashDeclare. +func (mr *MockAccountInterfaceMockRecorder) TransactionHashDeclare(tx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TransactionHashDeclare", reflect.TypeOf((*MockAccountInterface)(nil).TransactionHashDeclare), tx) +} + +// TransactionHashDeployAccount mocks base method. +func (m *MockAccountInterface) TransactionHashDeployAccount(tx rpc.DeployAccountTxn, contractAddress *felt.Felt) (*felt.Felt, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "TransactionHashDeployAccount", tx, contractAddress) + ret0, _ := ret[0].(*felt.Felt) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// TransactionHashDeployAccount indicates an expected call of TransactionHashDeployAccount. +func (mr *MockAccountInterfaceMockRecorder) TransactionHashDeployAccount(tx, contractAddress interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TransactionHashDeployAccount", reflect.TypeOf((*MockAccountInterface)(nil).TransactionHashDeployAccount), tx, contractAddress) +} + +// TransactionHashInvoke mocks base method. +func (m *MockAccountInterface) TransactionHashInvoke(invokeTxn rpc.InvokeTxnType) (*felt.Felt, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "TransactionHashInvoke", invokeTxn) + ret0, _ := ret[0].(*felt.Felt) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// TransactionHashInvoke indicates an expected call of TransactionHashInvoke. +func (mr *MockAccountInterfaceMockRecorder) TransactionHashInvoke(invokeTxn interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TransactionHashInvoke", reflect.TypeOf((*MockAccountInterface)(nil).TransactionHashInvoke), invokeTxn) +} + +// WaitForTransactionReceipt mocks base method. +func (m *MockAccountInterface) WaitForTransactionReceipt(ctx context.Context, transactionHash *felt.Felt, pollInterval time.Duration) (*rpc.TransactionReceipt, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "WaitForTransactionReceipt", ctx, transactionHash, pollInterval) + ret0, _ := ret[0].(*rpc.TransactionReceipt) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// WaitForTransactionReceipt indicates an expected call of WaitForTransactionReceipt. +func (mr *MockAccountInterfaceMockRecorder) WaitForTransactionReceipt(ctx, transactionHash, pollInterval interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TransactionHash", reflect.TypeOf((*MockAccountInterface)(nil).TransactionHash), calls, txDetails) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WaitForTransactionReceipt", reflect.TypeOf((*MockAccountInterface)(nil).WaitForTransactionReceipt), ctx, transactionHash, pollInterval) } diff --git a/mocks/mock_rpc_provider.go b/mocks/mock_rpc_provider.go index 59d50f11..8d024a2c 100644 --- a/mocks/mock_rpc_provider.go +++ b/mocks/mock_rpc_provider.go @@ -306,6 +306,21 @@ func (mr *MockRpcProviderMockRecorder) SimulateTransactions(ctx, blockID, txns, return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SimulateTransactions", reflect.TypeOf((*MockRpcProvider)(nil).SimulateTransactions), ctx, blockID, txns, simulationFlags) } +// SpecVersion mocks base method. +func (m *MockRpcProvider) SpecVersion(ctx context.Context) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SpecVersion", ctx) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// SpecVersion indicates an expected call of SpecVersion. +func (mr *MockRpcProviderMockRecorder) SpecVersion(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SpecVersion", reflect.TypeOf((*MockRpcProvider)(nil).SpecVersion), ctx) +} + // StateUpdate mocks base method. func (m *MockRpcProvider) StateUpdate(ctx context.Context, blockID rpc.BlockID) (*rpc.StateUpdateOutput, error) { m.ctrl.T.Helper() diff --git a/rpc/provider.go b/rpc/provider.go index 262c608c..bcbffe2c 100644 --- a/rpc/provider.go +++ b/rpc/provider.go @@ -46,6 +46,7 @@ type RpcProvider interface { SimulateTransactions(ctx context.Context, blockID BlockID, txns []Transaction, simulationFlags []SimulationFlag) ([]SimulatedTransaction, error) StateUpdate(ctx context.Context, blockID BlockID) (*StateUpdateOutput, error) StorageAt(ctx context.Context, contractAddress *felt.Felt, key string, blockID BlockID) (string, error) + SpecVersion(ctx context.Context) (string, error) Syncing(ctx context.Context) (*SyncStatus, error) TraceBlockTransactions(ctx context.Context, blockHash *felt.Felt) ([]Trace, error) TransactionByBlockIdAndIndex(ctx context.Context, blockID BlockID, index uint64) (Transaction, error) diff --git a/rpc/version.go b/rpc/version.go new file mode 100644 index 00000000..e46f4c23 --- /dev/null +++ b/rpc/version.go @@ -0,0 +1,12 @@ +package rpc + +import "context" + +// SpecVersion returns the version of the Starknet JSON-RPC specification being used +// Parameters: None +// Returns: String of the Starknet JSON-RPC specification +func (provider *Provider) SpecVersion(ctx context.Context) (string, error) { + var result string + err := do(ctx, provider.c, "starknet_specVersion", &result) + return result, err +} diff --git a/rpc/version_test.go b/rpc/version_test.go new file mode 100644 index 00000000..57a53fcf --- /dev/null +++ b/rpc/version_test.go @@ -0,0 +1,32 @@ +package rpc + +import ( + "context" + "testing" + + "github.com/test-go/testify/require" +) + +// TestSpecVersion tests starknet_specVersion +func TestSpecVersion(t *testing.T) { + + testConfig := beforeEach(t) + + type testSetType struct { + ExpectedResp string + } + testSet := map[string][]testSetType{ + "devnet": {}, + "mainnet": {}, + "mock": {}, + "testnet": {{ + ExpectedResp: "0.5.0", + }}, + }[testEnv] + + for _, test := range testSet { + resp, err := testConfig.provider.SpecVersion(context.Background()) + require.NoError(t, err) + require.Equal(t, test.ExpectedResp, resp) + } +}