Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkushinDaniil committed Jan 25, 2025
1 parent 7661b33 commit d4f0338
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 115 deletions.
2 changes: 1 addition & 1 deletion rpc/estimate_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,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, true, true, true).
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &blockInfo, mockState, n, true, false, true, true).
Return([]*felt.Felt{}, []core.GasConsumed{}, []vm.TransactionTrace{}, uint64(123), nil).Times(2)

_, httpHeader, err := handler.EstimateFee([]rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{}, rpc.BlockID{Latest: true})
Expand Down
199 changes: 88 additions & 111 deletions rpc/subscriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -867,130 +867,107 @@ func TestSubscriptionReorg(t *testing.T) {
}

func TestSubscribePendingTxs(t *testing.T) {

Check failure on line 869 in rpc/subscriptions_test.go

View workflow job for this annotation

GitHub Actions / lint

TestSubscribePendingTxs should call t.Parallel on the top level as well as its subtests (tparallel)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)

mockChain := mocks.NewMockReader(mockCtrl)
l1Feed := feed.New[*core.L1Head]()
mockChain.EXPECT().SubscribeL1Head().Return(blockchain.L1HeadSubscription{Subscription: l1Feed.Subscribe()})

syncer := newFakeSyncer()
handler, server := setupRPC(t, ctx, mockChain, syncer)

t.Run("Basic subscription", func(t *testing.T) {
conn := createWsConn(t, ctx, server)

subMsg := `{"jsonrpc":"2.0","id":1,"method":"starknet_subscribePendingTransactions"}`
id := uint64(1)
handler.WithIDGen(func() uint64 { return id })
got := sendWsMessage(t, ctx, conn, subMsg)
require.Equal(t, subResp(id), got)

hash1 := new(felt.Felt).SetUint64(1)
addr1 := new(felt.Felt).SetUint64(11)

hash2 := new(felt.Felt).SetUint64(2)
addr2 := new(felt.Felt).SetUint64(22)

hash3 := new(felt.Felt).SetUint64(3)
hash4 := new(felt.Felt).SetUint64(4)
hash5 := new(felt.Felt).SetUint64(5)

syncer.pendingTxs.Send([]core.Transaction{
&core.InvokeTransaction{TransactionHash: hash1, SenderAddress: addr1},
&core.DeclareTransaction{TransactionHash: hash2, SenderAddress: addr2},
&core.DeployTransaction{TransactionHash: hash3},
&core.DeployAccountTransaction{DeployTransaction: core.DeployTransaction{TransactionHash: hash4}},
&core.L1HandlerTransaction{TransactionHash: hash5},
})

want := `{"jsonrpc":"2.0","method":"starknet_subscriptionPendingTransactions","params":{"result":["0x1","0x2","0x3","0x4","0x5"],"subscription_id":%d}}`
want = fmt.Sprintf(want, id)
_, pendingTxsGot, err := conn.Read(ctx)
require.NoError(t, err)
require.Equal(t, want, string(pendingTxsGot))
})

t.Run("Filtered subscription", func(t *testing.T) {
conn := createWsConn(t, ctx, server)
tests := []struct {
name string
subscribeMsg string
expectedTxs []core.Transaction
want string
}{
{
name: "Basic subscription",
subscribeMsg: `{"jsonrpc":"2.0","id":1,"method":"starknet_subscribePendingTransactions"}`,
expectedTxs: []core.Transaction{
&core.InvokeTransaction{TransactionHash: new(felt.Felt).SetUint64(1), SenderAddress: new(felt.Felt).SetUint64(11)},
&core.DeclareTransaction{TransactionHash: new(felt.Felt).SetUint64(2), SenderAddress: new(felt.Felt).SetUint64(22)},
&core.DeployTransaction{TransactionHash: new(felt.Felt).SetUint64(3)},
&core.DeployAccountTransaction{DeployTransaction: core.DeployTransaction{TransactionHash: new(felt.Felt).SetUint64(4)}},
&core.L1HandlerTransaction{TransactionHash: new(felt.Felt).SetUint64(5)},
},
want: `{"jsonrpc":"2.0","method":"starknet_subscriptionPendingTransactions","params":{"result":["0x1","0x2","0x3","0x4","0x5"],"subscription_id":%d}}`,
},
{
name: "Filtered subscription",
subscribeMsg: `{"jsonrpc":"2.0","id":1,"method":"starknet_subscribePendingTransactions", "params":{"sender_address":["0xb", "0x16"]}}`,
expectedTxs: []core.Transaction{
&core.InvokeTransaction{TransactionHash: new(felt.Felt).SetUint64(1), SenderAddress: new(felt.Felt).SetUint64(11)},
&core.DeclareTransaction{TransactionHash: new(felt.Felt).SetUint64(2), SenderAddress: new(felt.Felt).SetUint64(22)},
&core.DeployTransaction{TransactionHash: new(felt.Felt).SetUint64(3)},
&core.DeployAccountTransaction{DeployTransaction: core.DeployTransaction{TransactionHash: new(felt.Felt).SetUint64(4)}},
&core.L1HandlerTransaction{TransactionHash: new(felt.Felt).SetUint64(5)},
&core.InvokeTransaction{TransactionHash: new(felt.Felt).SetUint64(6), SenderAddress: new(felt.Felt).SetUint64(66)},
&core.InvokeTransaction{TransactionHash: new(felt.Felt).SetUint64(7), SenderAddress: new(felt.Felt).SetUint64(77)},
},
want: `{"jsonrpc":"2.0","method":"starknet_subscriptionPendingTransactions","params":{"result":["0x1","0x2"],"subscription_id":%d}}`,
},
{
name: "Full details subscription",
subscribeMsg: `{"jsonrpc":"2.0","id":1,"method":"starknet_subscribePendingTransactions", "params":{"transaction_details": true}}`,
expectedTxs: []core.Transaction{
&core.InvokeTransaction{
TransactionHash: new(felt.Felt).SetUint64(1),
CallData: []*felt.Felt{new(felt.Felt).SetUint64(2)},
TransactionSignature: []*felt.Felt{new(felt.Felt).SetUint64(3)},
MaxFee: new(felt.Felt).SetUint64(4),
ContractAddress: new(felt.Felt).SetUint64(5),
Version: new(core.TransactionVersion).SetUint64(3),
EntryPointSelector: new(felt.Felt).SetUint64(6),
Nonce: new(felt.Felt).SetUint64(7),
SenderAddress: new(felt.Felt).SetUint64(8),
ResourceBounds: map[core.Resource]core.ResourceBounds{},
Tip: 9,
PaymasterData: []*felt.Felt{new(felt.Felt).SetUint64(10)},
AccountDeploymentData: []*felt.Felt{new(felt.Felt).SetUint64(11)},
},
},
want: `{"jsonrpc":"2.0","method":"starknet_subscriptionPendingTransactions","params":{"result":[{"transaction_hash":"0x1","type":"INVOKE","version":"0x3","nonce":"0x7","max_fee":"0x4","contract_address":"0x5","sender_address":"0x8","signature":["0x3"],"calldata":["0x2"],"entry_point_selector":"0x6","resource_bounds":{},"tip":"0x9","paymaster_data":["0xa"],"account_deployment_data":["0xb"],"nonce_data_availability_mode":"L1","fee_data_availability_mode":"L1"}],"subscription_id":%d}}`,
},
}

subMsg := `{"jsonrpc":"2.0","id":1,"method":"starknet_subscribePendingTransactions", "params":{"sender_address":["0xb", "0x16"]}}`
id := uint64(1)
handler.WithIDGen(func() uint64 { return id })
got := sendWsMessage(t, ctx, conn, subMsg)
require.Equal(t, subResp(id), got)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

hash1 := new(felt.Felt).SetUint64(1)
addr1 := new(felt.Felt).SetUint64(11)
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)

hash2 := new(felt.Felt).SetUint64(2)
addr2 := new(felt.Felt).SetUint64(22)
mockChain := mocks.NewMockReader(mockCtrl)
l1Feed := feed.New[*core.L1Head]()
mockChain.EXPECT().SubscribeL1Head().Return(blockchain.L1HeadSubscription{Subscription: l1Feed.Subscribe()})

hash3 := new(felt.Felt).SetUint64(3)
hash4 := new(felt.Felt).SetUint64(4)
hash5 := new(felt.Felt).SetUint64(5)
syncer := newFakeSyncer()
handler, server := setupRPC(t, ctx, mockChain, syncer)

hash6 := new(felt.Felt).SetUint64(6)
addr6 := new(felt.Felt).SetUint64(66)
conn := createWsConn(t, ctx, server)

hash7 := new(felt.Felt).SetUint64(7)
addr7 := new(felt.Felt).SetUint64(77)
id := uint64(1)
handler.WithIDGen(func() uint64 { return id })
got := sendWsMessage(t, ctx, conn, tt.subscribeMsg)
require.Equal(t, subResp(id), got)

syncer.pendingTxs.Send([]core.Transaction{
&core.InvokeTransaction{TransactionHash: hash1, SenderAddress: addr1},
&core.DeclareTransaction{TransactionHash: hash2, SenderAddress: addr2},
&core.DeployTransaction{TransactionHash: hash3},
&core.DeployAccountTransaction{DeployTransaction: core.DeployTransaction{TransactionHash: hash4}},
&core.L1HandlerTransaction{TransactionHash: hash5},
&core.InvokeTransaction{TransactionHash: hash6, SenderAddress: addr6},
&core.DeclareTransaction{TransactionHash: hash7, SenderAddress: addr7},
syncer.pendingTxs.Send(tt.expectedTxs)
_, pendingTxsGot, err := conn.Read(ctx)
require.NoError(t, err)
want := fmt.Sprintf(tt.want, id)
require.Equal(t, want, string(pendingTxsGot))
})
}

want := `{"jsonrpc":"2.0","method":"starknet_subscriptionPendingTransactions","params":{"result":["0x1","0x2"],"subscription_id":%d}}`
want = fmt.Sprintf(want, id)
_, pendingTxsGot, err := conn.Read(ctx)
require.NoError(t, err)
require.Equal(t, want, string(pendingTxsGot))
})

t.Run("Full details subscription", func(t *testing.T) {
conn := createWsConn(t, ctx, server)
t.Run("Return error if too many addresses in filter", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

subMsg := `{"jsonrpc":"2.0","id":1,"method":"starknet_subscribePendingTransactions", "params":{"transaction_details": true}}`
id := uint64(1)
handler.WithIDGen(func() uint64 { return id })
got := sendWsMessage(t, ctx, conn, subMsg)
require.Equal(t, subResp(id), got)
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)

syncer.pendingTxs.Send([]core.Transaction{
&core.InvokeTransaction{
TransactionHash: new(felt.Felt).SetUint64(1),
CallData: []*felt.Felt{new(felt.Felt).SetUint64(2)},
TransactionSignature: []*felt.Felt{new(felt.Felt).SetUint64(3)},
MaxFee: new(felt.Felt).SetUint64(4),
ContractAddress: new(felt.Felt).SetUint64(5),
Version: new(core.TransactionVersion).SetUint64(3),
EntryPointSelector: new(felt.Felt).SetUint64(6),
Nonce: new(felt.Felt).SetUint64(7),
SenderAddress: new(felt.Felt).SetUint64(8),
ResourceBounds: map[core.Resource]core.ResourceBounds{},
Tip: 9,
PaymasterData: []*felt.Felt{new(felt.Felt).SetUint64(10)},
AccountDeploymentData: []*felt.Felt{new(felt.Felt).SetUint64(11)},
},
})
mockChain := mocks.NewMockReader(mockCtrl)
l1Feed := feed.New[*core.L1Head]()
mockChain.EXPECT().SubscribeL1Head().Return(blockchain.L1HeadSubscription{Subscription: l1Feed.Subscribe()})

want := `{"jsonrpc":"2.0","method":"starknet_subscriptionPendingTransactions","params":{"result":[{"transaction_hash":"0x1","type":"INVOKE","version":"0x3","nonce":"0x7","max_fee":"0x4","contract_address":"0x5","sender_address":"0x8","signature":["0x3"],"calldata":["0x2"],"entry_point_selector":"0x6","resource_bounds":{},"tip":"0x9","paymaster_data":["0xa"],"account_deployment_data":["0xb"],"nonce_data_availability_mode":"L1","fee_data_availability_mode":"L1"}],"subscription_id":%d}}`
want = fmt.Sprintf(want, id)
_, pendingTxsGot, err := conn.Read(ctx)
require.NoError(t, err)
require.Equal(t, want, string(pendingTxsGot))
})
syncer := newFakeSyncer()
handler, _ := setupRPC(t, ctx, mockChain, syncer)

t.Run("Return error if too many addresses in filter", func(t *testing.T) {
addresses := make([]felt.Felt, 1024+1)

serverConn, _ := net.Pipe()
Expand Down
6 changes: 3 additions & 3 deletions rpc/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func TestTraceTransactionV0_6(t *testing.T) {
hash := utils.HexToFelt(t, "0xBBBB")
// Receipt() returns error related to db
mockReader.EXPECT().Receipt(hash).Return(nil, nil, uint64(0), db.ErrKeyNotFound)
mockSyncReader.EXPECT().Pending().Return(nil, nil)
mockSyncReader.EXPECT().Pending().Return(&sync.Pending{Block: &core.Block{}}, nil)

trace, httpHeader, err := handler.TraceTransactionV0_6(context.Background(), *hash)
assert.Nil(t, trace)
Expand Down Expand Up @@ -520,7 +520,7 @@ func TestTraceBlockTransactions(t *testing.T) {
stepsUsedStr := "123"
require.NoError(t, json.Unmarshal(vmTraceJSON, &vmTrace))
mockVM.EXPECT().Execute(block.Transactions, []core.Class{declaredClass.Class}, paidL1Fees, &vm.BlockInfo{Header: header},
gomock.Any(), n, false, false, false, false).
gomock.Any(), n, false, false, false, true).
Return(nil, []core.GasConsumed{{}, {}}, []vm.TransactionTrace{vmTrace, vmTrace}, stepsUsed, nil)

result, httpHeader, err := handler.TraceBlockTransactions(context.Background(), rpc.BlockID{Hash: blockHash})
Expand Down Expand Up @@ -596,7 +596,7 @@ func TestTraceBlockTransactions(t *testing.T) {
stepsUsed := uint64(123)
stepsUsedStr := "123"
mockVM.EXPECT().Execute([]core.Transaction{tx}, []core.Class{declaredClass.Class}, []*felt.Felt{}, &vm.BlockInfo{Header: header},
gomock.Any(), n, false, false, false, false).
gomock.Any(), n, false, false, false, true).
Return(nil, []core.GasConsumed{{}, {}}, []vm.TransactionTrace{vmTrace}, stepsUsed, nil)

expectedResult := []rpc.TracedBlockTransaction{
Expand Down

0 comments on commit d4f0338

Please sign in to comment.