-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubscriber.go
85 lines (75 loc) · 2.07 KB
/
subscriber.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package cosmos
import (
"context"
"fmt"
"github.com/anconprotocol/sdk"
"github.com/gin-gonic/gin"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/basicnode"
rpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
)
type SubscriptionType string
const (
Tx SubscriptionType = "tm.event='Tx'"
ValidatorSetUpdates SubscriptionType = "tm.event='ValidatorSetUpdates'"
NewBlock SubscriptionType = "tm.event='NewBlock'"
BankModule SubscriptionType = "message.module='bank'"
)
type CosmosIndexer struct {
AnconSyncContext *sdk.AnconSyncContext
Client *rpcclient.WSClient
LastLinkNode datamodel.Node
LastLink datamodel.Link
}
func (i *CosmosIndexer) Subscribe(ctx context.Context, subscriptionType SubscriptionType) {
i.Client.Subscribe(ctx, string(subscriptionType))
}
// @BasePath /v0
// DagJsonWrite godoc
// @Summary Stores JSON as dag-json
// @Schemes
// @Description Writes a dag-json block which syncs with IPFS. Returns a CID.
// @Tags dag-json
// @Accept json
// @Produce json
// @Success 201 {string} cid
// @Router /v0/tip [post]
func (i *CosmosIndexer) TipEvent(c *gin.Context) {
c.JSON(201, gin.H{
"cid": i.LastLink,
})
}
func New(ctx context.Context, tmRPCAddr, tmEndpoint string) *CosmosIndexer {
tmWsClient, err := rpcclient.NewWS(tmRPCAddr, tmEndpoint)
dag := ctx.Value("dag").(*sdk.AnconSyncContext)
if err != nil {
panic(err)
}
err = tmWsClient.Start()
if err != nil {
panic(err)
}
i := &CosmosIndexer{Client: tmWsClient, AnconSyncContext: dag}
go func() {
for {
select {
case <-i.Client.Quit():
return
case res, ok := <-i.Client.ResponsesCh:
if ok {
block, err := sdk.Decode(basicnode.Prototype.Any, string(res.Result))
if err != nil {
fmt.Errorf("invalid json %v", err)
}
i.LastLink = i.AnconSyncContext.Store.Store(ipld.LinkContext{
LinkNode: i.LastLinkNode,
}, block)
i.LastLinkNode = block
// PushBlock(c.Request.Context(), dagctx, cid)
}
}
}
}()
return i
}