-
Notifications
You must be signed in to change notification settings - Fork 244
/
debug.go
74 lines (67 loc) · 1.81 KB
/
debug.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
package shhext
import (
"context"
"errors"
"fmt"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/status-im/status-go/services"
whisper "github.com/status-im/whisper/whisperv6"
)
var (
postSyncTimeout = 60 * time.Second
errEnvelopeExpired = errors.New("envelope expired before being sent")
errNoShhextAttachedAPI = errors.New("No shhext attached")
)
// DebugAPI represents a set of APIs from the `web3.debug` namespace.
type DebugAPI struct {
s *Service
}
// NewDebugAPI creates an instance of the debug API.
func NewDebugAPI(s *Service) *DebugAPI {
return &DebugAPI{s: s}
}
// PostSync sends an envelope through shhext_post and waits until it's sent.
func (api *DebugAPI) PostSync(ctx context.Context, req whisper.NewMessage) (hash hexutil.Bytes, err error) {
shhAPI := services.APIByNamespace(api.s.APIs(), "shhext")
if shhAPI == nil {
err = errNoShhextAttachedAPI
return
}
s, ok := shhAPI.(*PublicAPI)
if !ok {
err = errNoShhextAttachedAPI
return
}
hash, err = s.Post(ctx, req)
if err != nil {
return
}
ctxTimeout, cancel := context.WithTimeout(ctx, postSyncTimeout)
defer cancel()
err = api.waitForHash(ctxTimeout, hash)
return
}
// waitForHash waits for a specific hash to be sent
func (api *DebugAPI) waitForHash(ctx context.Context, hash hexutil.Bytes) error {
h := common.BytesToHash(hash)
events := make(chan whisper.EnvelopeEvent, 100)
sub := api.s.w.SubscribeEnvelopeEvents(events)
defer sub.Unsubscribe()
for {
select {
case ev := <-events:
if ev.Hash == h {
if ev.Event == whisper.EventEnvelopeSent {
return nil
}
if ev.Event == whisper.EventEnvelopeExpired {
return errEnvelopeExpired
}
}
case <-ctx.Done():
return fmt.Errorf("wait for hash canceled: %v", ctx.Err())
}
}
}