diff --git a/changeLog.md b/changeLog.md index 6b1cd08..846bfa1 100644 --- a/changeLog.md +++ b/changeLog.md @@ -1,6 +1,10 @@ Go-conflux-sdk Change Log ============ +v1.4.3 +------------ +- Support `SubscribeNewHeadsWitReconn`, `SubscribeEpochsWithReconn`, `SubscribeLogsWithReconn` + v1.4.0 ------------ - Remove offset & limit from LogFilter diff --git a/client.go b/client.go index 4795580..ba6af49 100644 --- a/client.go +++ b/client.go @@ -1203,6 +1203,27 @@ func (client *Client) SubscribeLogs(channel chan types.SubscriptionLog, filter t return client.Subscribe(context.Background(), "cfx", channel, "logs", filter) } +// SubscribeNewHeadsWitReconn subscribes all new block headers participating in the consensus. +// It will auto re-subscribe if lost connect. +func (client *Client) SubscribeNewHeadsWitReconn(channel chan types.BlockHeader) *rpc.ReconnClientSubscription { + return client.SubscribeWithReconn(context.Background(), "cfx", channel, "newHeads") +} + +// 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" +// It will auto re-subscribe if lost connect. +func (client *Client) SubscribeEpochsWithReconn(channel chan types.WebsocketEpochResponse, subscriptionEpochType ...types.Epoch) *rpc.ReconnClientSubscription { + if len(subscriptionEpochType) > 0 { + return client.SubscribeWithReconn(context.Background(), "cfx", channel, "epochs", subscriptionEpochType[0].String()) + } + return client.SubscribeWithReconn(context.Background(), "cfx", channel, "epochs") +} + +// SubscribeLogs subscribes all logs matching a certain filter, in order. +// It will auto re-subscribe if lost connect. +func (client *Client) SubscribeLogsWithReconn(channel chan types.SubscriptionLog, filter types.LogFilter) *rpc.ReconnClientSubscription { + return client.SubscribeWithReconn(context.Background(), "cfx", channel, "logs", filter) +} + // === helper methods === // WaitForTransationBePacked returns transaction when it is packed diff --git a/go.mod b/go.mod index 43bf36a..c8dc429 100644 --- a/go.mod +++ b/go.mod @@ -8,11 +8,12 @@ require ( github.com/graph-gophers/graphql-go v1.3.0 // indirect github.com/jackpal/go-nat-pmp v1.0.2 // indirect github.com/mcuadros/go-defaults v1.2.0 - github.com/openweb3/go-rpc-provider v0.2.2 + github.com/openweb3/go-rpc-provider v0.3.0 github.com/openweb3/go-sdk-common v0.0.0-20220720074746-a7134e1d372c github.com/pkg/errors v0.9.1 github.com/shopspring/decimal v1.3.1 github.com/smartystreets/goconvey v1.6.4 + github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4 github.com/stretchr/testify v1.7.0 gopkg.in/urfave/cli.v1 v1.20.0 gotest.tools v2.2.0+incompatible @@ -21,4 +22,4 @@ require ( // replace github.com/openweb3/go-sdk-common => ../go-sdk-common // replace github.com/ethereum/go-ethereum => ../../ethereum/go-ethereum -// replace github.com/openweb3/go-rpc-provider v0.2.0 => ../go-rpc-provider +// replace github.com/openweb3/go-rpc-provider v0.2.2 => ../go-rpc-provider diff --git a/go.sum b/go.sum index c013f33..b49cd31 100644 --- a/go.sum +++ b/go.sum @@ -351,8 +351,8 @@ github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFSt github.com/opentracing/opentracing-go v1.0.3-0.20180606204148-bd9c31933947/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= -github.com/openweb3/go-rpc-provider v0.2.2 h1:7p+CEnum8A4ZCzcCWhaQsf/leNAUkwI2n/LKesxGqqE= -github.com/openweb3/go-rpc-provider v0.2.2/go.mod h1:DYz40TbzhzyTA06UFqGIKSXp0uFot6ZKh4QarD//eZ0= +github.com/openweb3/go-rpc-provider v0.3.0 h1:8D22MVyUG/NrpspwonzOGOhLeWZhxjJdQd4VT5KdJy4= +github.com/openweb3/go-rpc-provider v0.3.0/go.mod h1:DYz40TbzhzyTA06UFqGIKSXp0uFot6ZKh4QarD//eZ0= github.com/openweb3/go-sdk-common v0.0.0-20220720074746-a7134e1d372c h1:BrPXZpkTdmZe5bNjSSnxWqL44X9FcZ3xftLcYNkIJ68= github.com/openweb3/go-sdk-common v0.0.0-20220720074746-a7134e1d372c/go.mod h1:0WCVKMiLiYEaHhpQWQ3rgLti/Fv/+JPRiB0sEoovwk8= github.com/paulbellamy/ratecounter v0.2.0/go.mod h1:Hfx1hDpSGoqxkVVpBi/IlYD7kChlfo5C6hzIHwPqfFE= diff --git a/integration_test/resub_test.go b/integration_test/resub_test.go new file mode 100644 index 0000000..78b6dd9 --- /dev/null +++ b/integration_test/resub_test.go @@ -0,0 +1,42 @@ +package integrationtest + +import ( + "fmt" + "sync" + "testing" + + sdk "github.com/Conflux-Chain/go-conflux-sdk" + "github.com/Conflux-Chain/go-conflux-sdk/types" +) + +func _TestResubHeads(t *testing.T) { + client := sdk.MustNewClient("wss://test.confluxrpc.com/ws") + + headc := make(chan types.BlockHeader) + sub := client.SubscribeNewHeadsWitReconn(headc) + + retry := 0 + + var wg sync.WaitGroup + wg.Add(1) + go func() { + defer wg.Done() + for { + select { + case err := <-sub.Err(): + fmt.Printf("sub error: %v\n", err) + retry++ + if retry >= 20 { + sub.Unsubscribe() + return + } + case <-sub.ResubSuccess(): + fmt.Println("sub success") + retry = 0 + case h := <-headc: + fmt.Printf("received head %v\n", h) + } + } + }() + wg.Wait() +}