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

thiagodeev/rpcv08-websocket #651

Open
wants to merge 32 commits into
base: v0.8.0
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2d3dcce
Update go.mod to include gorilla/websocket and enhance RPC provider w…
thiagodeev Dec 24, 2024
418db12
Enhance NewWebsocketProvider with cookie support in provider.go
thiagodeev Jan 3, 2025
1247764
Added new errors
thiagodeev Jan 6, 2025
f7f342f
Refactor RPC client and enhance WebSocket support
thiagodeev Jan 7, 2025
6d54e5a
adds remaining test data and improves tests, and adapt subscribe meth…
thiagodeev Jan 8, 2025
f188b94
Enhance Starknet WebSocket support and subscription handling
thiagodeev Jan 8, 2025
71d5a4e
improves newHeads method and WS close message
thiagodeev Jan 10, 2025
05d7e90
Enhance Starknet compatibility and testing framework
thiagodeev Jan 10, 2025
c73bfce
Fixes linter and test errors
thiagodeev Jan 13, 2025
76d01c8
draft implementation of subscribeEvents
thiagodeev Jan 14, 2025
f678b3f
client adaptation and functional version of subscribeEvents
thiagodeev Jan 14, 2025
245dbac
Implement SubscribeEvents functionality in WebSocket provider with en…
thiagodeev Jan 15, 2025
094d21f
finish subscribeEvents tests
thiagodeev Jan 15, 2025
d01082f
add starknet-spec updates
thiagodeev Jan 16, 2025
f386d32
Finish subscribeTransactionStatus
thiagodeev Jan 16, 2025
4d61a71
Implement SubscribePendingTransactions
thiagodeev Jan 22, 2025
67c1eb7
revert mistake in the account_test.go file
thiagodeev Jan 22, 2025
6c22c90
improve SubscribeNewHeads param
thiagodeev Jan 22, 2025
c5c147f
improve SubscribeEvents method
thiagodeev Jan 22, 2025
8f99018
Add SubscribePendingTransactions tests
thiagodeev Jan 22, 2025
087ed79
Add support for Reorg notifications
thiagodeev Jan 23, 2025
e3bc2d1
add TestUnsubscribe
thiagodeev Jan 24, 2025
8b908c5
Create WebsocketProvider interface
thiagodeev Jan 30, 2025
a0dd75a
Remove some TODO comments
thiagodeev Jan 30, 2025
ecfe19f
Reorg ws methods alphabetically
thiagodeev Jan 30, 2025
838bd82
Improve ws methods description
thiagodeev Jan 30, 2025
35023f8
Add WebSocket example with detailed usage and README update
thiagodeev Jan 30, 2025
359fc44
Merge branch 'v0.8.0' into thiagodeev/rpcv08-websocket
thiagodeev Jan 30, 2025
2457999
Update README with WebSocket example documentation
thiagodeev Jan 30, 2025
9bae8fb
Update CI workflow to build WebSocket and typedData projects
thiagodeev Jan 30, 2025
956df89
Remove HexToFeltNoErr
thiagodeev Jan 30, 2025
4beb3ff
Refactor WebSocket subscription types with SubscriptionBlockID and re…
thiagodeev Jan 30, 2025
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
Prev Previous commit
Next Next commit
improves newHeads method and WS close message
thiagodeev committed Jan 13, 2025
commit 71d5a4ecb794428f4b56b14cb1192930ba48f8d2
19 changes: 18 additions & 1 deletion client/subscription.go
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ import (
"errors"
"math/rand"
"reflect"
"strconv"
"strings"
"sync"
"time"
@@ -268,6 +269,11 @@ func (sub *ClientSubscription) Unsubscribe() {
})
}

// ID returns the subscription ID.
func (sub *ClientSubscription) ID() string {
return sub.subid
}

// deliver is called by the client's message dispatcher to send a notification value.
func (sub *ClientSubscription) deliver(result json.RawMessage) (ok bool) {
select {
@@ -373,6 +379,17 @@ func (sub *ClientSubscription) requestUnsubscribe() error {
var result interface{}
ctx, cancel := context.WithTimeout(context.Background(), unsubscribeTimeout)
defer cancel()
err := sub.client.CallContext(ctx, &result, sub.namespace+unsubscribeMethodSuffix, sub.subid)

var err error
if sub.namespace == "starknet" {
var subId uint64
subId, err = strconv.ParseUint(sub.subid, 10, 64)
if err != nil {
return err
}
err = sub.client.CallContext(ctx, &result, sub.namespace+unsubscribeMethodSuffix, subId)
} else {
err = sub.client.CallContext(ctx, &result, sub.namespace+unsubscribeMethodSuffix, sub.subid)
}
return err
}
6 changes: 6 additions & 0 deletions client/websocket.go
Original file line number Diff line number Diff line change
@@ -324,6 +324,12 @@ func newWebsocketCodec(conn *websocket.Conn, host string, req http.Header, readL
}

func (wc *websocketCodec) close() {
err := wc.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "client is closed"), time.Time{})
if err != nil {
// Handle error but ensure we still try to close the connection
log.Warn("Error sending close message: ", err)
}

wc.jsonCodec.close()
wc.wg.Wait()
}
5 changes: 5 additions & 0 deletions rpc/provider.go
Original file line number Diff line number Diff line change
@@ -28,6 +28,11 @@ type WsProvider struct {
c wsConn
}

// Close closes the client, aborting any in-flight requests.
func (p *WsProvider) Close() {
p.c.Close()
}

// NewProvider creates a new HTTP rpc Provider instance.
func NewProvider(url string, options ...client.ClientOption) (*Provider, error) {
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
12 changes: 9 additions & 3 deletions rpc/websocket.go
Original file line number Diff line number Diff line change
@@ -11,13 +11,19 @@ import (
//
// Parameters:
// - ctx: The context.Context object for controlling the function call
// - blockID: The ID of the block to retrieve the transactions from
// - headers: The channel to send the new block headers to
// - blockID (optional): The block to get notifications from, default is latest, limited to 1024 blocks back
// Returns:
// - subscriptionId: The subscription ID
// - error: An error, if any
func (provider *WsProvider) SubscribeNewHeads(ctx context.Context, headers chan<- *BlockHeader, blockID ...BlockID) (*client.ClientSubscription, error) {
thiagodeev marked this conversation as resolved.
Show resolved Hide resolved
// Convert blockID to []any
params := make([]any, len(blockID))
for i, v := range blockID {
params[i] = v
}

func (provider *WsProvider) SubscribeNewHeads(ctx context.Context, ch chan<- *BlockHeader) (*client.ClientSubscription, error) {
sub, err := provider.c.Subscribe(ctx, "starknet", "_subscribeNewHeads", ch)
sub, err := provider.c.Subscribe(ctx, "starknet", "_subscribeNewHeads", headers, params...)
if err != nil {
return nil, tryUnwrapToRPCErr(err, ErrTooManyBlocksBack, ErrBlockNotFound, ErrCallOnPending)
}