-
Notifications
You must be signed in to change notification settings - Fork 13
/
events.go
155 lines (129 loc) · 3.18 KB
/
events.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package cosmos
import (
"context"
"errors"
"fmt"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
"github.com/icon-project/ibc-integration/test/chains"
"golang.org/x/exp/slices"
"net/http"
"strings"
"sync"
"time"
)
type WebSocketListener struct {
contract string
chain *CosmosLocalnet
Shutdown chan struct{}
Events []map[string][]string
eventMapLock sync.RWMutex
wg sync.WaitGroup
timeout time.Duration
}
func NewCosmosEventListener(c *CosmosLocalnet, contract string, timeout time.Duration) *WebSocketListener {
return &WebSocketListener{
chain: c,
timeout: timeout,
contract: c.GetIBCAddress(contract),
Shutdown: make(chan struct{}),
Events: []map[string][]string{},
}
}
func (w *WebSocketListener) Start() {
fmt.Println("Starting to listen on...")
w.wg.Add(1)
go func() {
defer w.wg.Done()
if err := w.handleWebSocket(); err != nil && err != http.ErrServerClosed {
fmt.Printf("Error starting server: %v\n", err)
}
}()
}
func (w *WebSocketListener) Stop() {
fmt.Println("Stopping...")
close(w.Shutdown)
select {
case <-time.After(5 * time.Second):
fmt.Println("Shutdown took too long, force closing...")
}
fmt.Println("Stopped.")
}
func (w *WebSocketListener) handleWebSocket() error {
client, err := rpchttp.New(w.chain.GetHostRPCAddress(), "/websocket")
if err != nil {
return err
}
err = client.Start()
if err != nil {
return err
}
defer func(client *rpchttp.HTTP) {
err := client.Stop()
if err != nil {
}
}(client)
ctx, cancel := context.WithCancel(context.Background())
query := strings.Join([]string{"tm.event = 'Tx'",
//fmt.Sprintf("tx.height >= %d ", height),
fmt.Sprintf("message.module = 'wasm'"),
fmt.Sprintf("wasm._contract_address = '%s'", w.contract),
//fmt.Sprintf(index),
}, " AND ")
channel, err := client.Subscribe(ctx, "wasm-client", query)
if err != nil {
cancel()
return err
}
fmt.Println("WebSocket connected!")
for {
select {
case event := <-channel:
w.eventMapLock.Lock()
//e, _ := json.Marshal(event.Events)
w.Events = append(w.Events, event.Events)
w.eventMapLock.Unlock()
case <-w.Shutdown:
cancel()
fmt.Printf("Shutting down...\n")
return nil
}
}
}
func (w *WebSocketListener) FindEvent(filters chains.Filter) (chains.Event, error) {
timer := time.NewTimer(w.timeout)
defer timer.Stop()
for {
select {
case <-timer.C:
return nil, errors.New("event not found") // Event doesn't exist within the timeout
default:
if exist, event := w.eventExists(filters); exist {
fmt.Printf("event---%v \n", event)
return event, nil // Event exists
}
}
}
}
func (w *WebSocketListener) eventExists(filters chains.Filter) (bool, chains.Event) {
w.eventMapLock.RLock()
defer w.eventMapLock.RUnlock()
for _, event := range w.Events {
if matchesFilters(event, filters) {
return true, event
}
}
return false, nil
}
func matchesFilters(event chains.Event, filters chains.Filter) bool {
for key, value := range filters {
mValue, found := event[key]
if !found {
return false
}
if !slices.Contains(mValue, value.(string)) {
return false
}
}
fmt.Println("------------FOUND--------------")
return true
}