Skip to content

Commit 4cb1946

Browse files
Resub (#197)
* support SubscribeXXXWitReconn * add change log * update comments
1 parent 9439f95 commit 4cb1946

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

changeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Go-conflux-sdk Change Log
22
============
33

4+
v1.4.3
5+
------------
6+
- Support `SubscribeNewHeadsWitReconn`, `SubscribeEpochsWithReconn`, `SubscribeLogsWithReconn`
7+
48
v1.4.0
59
------------
610
- Remove offset & limit from LogFilter

client.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,27 @@ func (client *Client) SubscribeLogs(channel chan types.SubscriptionLog, filter t
12031203
return client.Subscribe(context.Background(), "cfx", channel, "logs", filter)
12041204
}
12051205

1206+
// SubscribeNewHeadsWitReconn subscribes all new block headers participating in the consensus.
1207+
// It will auto re-subscribe if lost connect.
1208+
func (client *Client) SubscribeNewHeadsWitReconn(channel chan types.BlockHeader) *rpc.ReconnClientSubscription {
1209+
return client.SubscribeWithReconn(context.Background(), "cfx", channel, "newHeads")
1210+
}
1211+
1212+
// SubscribeEpochsWithReconn subscribes consensus results: the total order of blocks, as expressed by a sequence of epochs. Currently subscriptionEpochType only support "latest_mined" and "latest_state"
1213+
// It will auto re-subscribe if lost connect.
1214+
func (client *Client) SubscribeEpochsWithReconn(channel chan types.WebsocketEpochResponse, subscriptionEpochType ...types.Epoch) *rpc.ReconnClientSubscription {
1215+
if len(subscriptionEpochType) > 0 {
1216+
return client.SubscribeWithReconn(context.Background(), "cfx", channel, "epochs", subscriptionEpochType[0].String())
1217+
}
1218+
return client.SubscribeWithReconn(context.Background(), "cfx", channel, "epochs")
1219+
}
1220+
1221+
// SubscribeLogs subscribes all logs matching a certain filter, in order.
1222+
// It will auto re-subscribe if lost connect.
1223+
func (client *Client) SubscribeLogsWithReconn(channel chan types.SubscriptionLog, filter types.LogFilter) *rpc.ReconnClientSubscription {
1224+
return client.SubscribeWithReconn(context.Background(), "cfx", channel, "logs", filter)
1225+
}
1226+
12061227
// === helper methods ===
12071228

12081229
// WaitForTransationBePacked returns transaction when it is packed

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ require (
88
github.com/graph-gophers/graphql-go v1.3.0 // indirect
99
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
1010
github.com/mcuadros/go-defaults v1.2.0
11-
github.com/openweb3/go-rpc-provider v0.2.2
11+
github.com/openweb3/go-rpc-provider v0.3.0
1212
github.com/openweb3/go-sdk-common v0.0.0-20220720074746-a7134e1d372c
1313
github.com/pkg/errors v0.9.1
1414
github.com/shopspring/decimal v1.3.1
1515
github.com/smartystreets/goconvey v1.6.4
16+
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4
1617
github.com/stretchr/testify v1.7.0
1718
gopkg.in/urfave/cli.v1 v1.20.0
1819
gotest.tools v2.2.0+incompatible
@@ -21,4 +22,4 @@ require (
2122

2223
// replace github.com/openweb3/go-sdk-common => ../go-sdk-common
2324
// replace github.com/ethereum/go-ethereum => ../../ethereum/go-ethereum
24-
// replace github.com/openweb3/go-rpc-provider v0.2.0 => ../go-rpc-provider
25+
// replace github.com/openweb3/go-rpc-provider v0.2.2 => ../go-rpc-provider

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFSt
351351
github.com/opentracing/opentracing-go v1.0.3-0.20180606204148-bd9c31933947/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
352352
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
353353
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
354-
github.com/openweb3/go-rpc-provider v0.2.2 h1:7p+CEnum8A4ZCzcCWhaQsf/leNAUkwI2n/LKesxGqqE=
355-
github.com/openweb3/go-rpc-provider v0.2.2/go.mod h1:DYz40TbzhzyTA06UFqGIKSXp0uFot6ZKh4QarD//eZ0=
354+
github.com/openweb3/go-rpc-provider v0.3.0 h1:8D22MVyUG/NrpspwonzOGOhLeWZhxjJdQd4VT5KdJy4=
355+
github.com/openweb3/go-rpc-provider v0.3.0/go.mod h1:DYz40TbzhzyTA06UFqGIKSXp0uFot6ZKh4QarD//eZ0=
356356
github.com/openweb3/go-sdk-common v0.0.0-20220720074746-a7134e1d372c h1:BrPXZpkTdmZe5bNjSSnxWqL44X9FcZ3xftLcYNkIJ68=
357357
github.com/openweb3/go-sdk-common v0.0.0-20220720074746-a7134e1d372c/go.mod h1:0WCVKMiLiYEaHhpQWQ3rgLti/Fv/+JPRiB0sEoovwk8=
358358
github.com/paulbellamy/ratecounter v0.2.0/go.mod h1:Hfx1hDpSGoqxkVVpBi/IlYD7kChlfo5C6hzIHwPqfFE=

integration_test/resub_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package integrationtest
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
"testing"
7+
8+
sdk "github.com/Conflux-Chain/go-conflux-sdk"
9+
"github.com/Conflux-Chain/go-conflux-sdk/types"
10+
)
11+
12+
func _TestResubHeads(t *testing.T) {
13+
client := sdk.MustNewClient("wss://test.confluxrpc.com/ws")
14+
15+
headc := make(chan types.BlockHeader)
16+
sub := client.SubscribeNewHeadsWitReconn(headc)
17+
18+
retry := 0
19+
20+
var wg sync.WaitGroup
21+
wg.Add(1)
22+
go func() {
23+
defer wg.Done()
24+
for {
25+
select {
26+
case err := <-sub.Err():
27+
fmt.Printf("sub error: %v\n", err)
28+
retry++
29+
if retry >= 20 {
30+
sub.Unsubscribe()
31+
return
32+
}
33+
case <-sub.ResubSuccess():
34+
fmt.Println("sub success")
35+
retry = 0
36+
case h := <-headc:
37+
fmt.Printf("received head %v\n", h)
38+
}
39+
}
40+
}()
41+
wg.Wait()
42+
}

0 commit comments

Comments
 (0)