forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmirror_consensus_topic_query.go
98 lines (74 loc) · 2.12 KB
/
mirror_consensus_topic_query.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
package hedera
import (
"context"
"github.com/hashgraph/hedera-sdk-go/proto/mirror"
"time"
)
type MirrorConsensusTopicQuery struct {
pb *mirror.ConsensusTopicQuery
}
type MirrorConsensusTopicResponse struct {
ConsensusTimeStamp time.Time
Message []byte
RunningHash []byte
SequenceNumber uint64
}
func NewMirrorConsensusTopicQuery() *MirrorConsensusTopicQuery {
pb := &mirror.ConsensusTopicQuery{}
return &MirrorConsensusTopicQuery{pb}
}
func (b *MirrorConsensusTopicQuery) SetTopicID(topicID ConsensusTopicID) *MirrorConsensusTopicQuery {
b.pb.TopicID = topicID.toProto()
return b
}
func (b *MirrorConsensusTopicQuery) SetStartTime(time time.Time) *MirrorConsensusTopicQuery {
b.pb.ConsensusStartTime = timeToProto(time)
return b
}
func (b *MirrorConsensusTopicQuery) SetEndTime(time time.Time) *MirrorConsensusTopicQuery {
b.pb.ConsensusEndTime = timeToProto(time)
return b
}
func (b *MirrorConsensusTopicQuery) SetLimit(limit uint64) *MirrorConsensusTopicQuery {
b.pb.Limit = limit
return b
}
func mirrorConsensusTopicResponseFromProto(r *mirror.ConsensusTopicResponse) MirrorConsensusTopicResponse {
return MirrorConsensusTopicResponse{
ConsensusTimeStamp: timeFromProto(r.ConsensusTimestamp),
Message: r.Message,
RunningHash: r.RunningHash,
SequenceNumber: r.SequenceNumber,
}
}
func (b *MirrorConsensusTopicQuery) Subscribe(
client MirrorClient,
onNext func(MirrorConsensusTopicResponse),
onError func(error),
) (MirrorSubscriptionHandle, error) {
if b.pb.TopicID == nil {
return MirrorSubscriptionHandle{}, newErrLocalValidationf("topic ID was not provided")
}
ctx, cancel := context.WithCancel(context.TODO())
subClient, err := client.client.SubscribeTopic(ctx, b.pb)
if err != nil {
return MirrorSubscriptionHandle{}, err
}
go func() {
for {
resp, err := subClient.Recv()
if err != nil {
if onError != nil {
onError(err)
}
cancel()
break
}
onNext(mirrorConsensusTopicResponseFromProto(resp))
}
}()
return newMirrorSubscriptionHandle(cancel), nil
}
//
// The QueryPayment functions are not required for this query
//