-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wip): implement
TopicMessageQuery
and `TopicMessageSubmitTrans…
…action`
- Loading branch information
1 parent
02e4ae2
commit 2413273
Showing
13 changed files
with
732 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package hedera | ||
|
||
import ( | ||
"math/rand" | ||
|
||
"github.com/hashgraph/hedera-sdk-go/proto/mirror" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type mirrorNetwork struct { | ||
channels map[string]mirror.ConsensusServiceClient | ||
network []string | ||
index uint | ||
} | ||
|
||
func newMirrorNetwork(network []string) mirrorNetwork { | ||
if len(network) > 0 { | ||
rand.Shuffle(len(network), func(i, j int) { | ||
network[i], network[j] = network[j], network[i] | ||
}) | ||
} | ||
|
||
return mirrorNetwork{ | ||
channels: make(map[string]mirror.ConsensusServiceClient), | ||
network: network, | ||
index: 0, | ||
} | ||
} | ||
|
||
func contains(arr []string, str string) bool { | ||
for _, a := range arr { | ||
if a == str { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (network mirrorNetwork) setNetwork(newNetwork []string) { | ||
for _, n := range network.network { | ||
if !contains(newNetwork, n) { | ||
delete(network.channels, n) | ||
} | ||
} | ||
|
||
for _, n := range newNetwork { | ||
if !contains(network.network, n) { | ||
network.network = append(network.network, n) | ||
} | ||
} | ||
|
||
network.index = 0 | ||
|
||
if len(network.network) > 0 { | ||
rand.Shuffle(len(network.network), func(i, j int) { | ||
network.network[i], network.network[j] = network.network[j], network.network[i] | ||
}) | ||
} | ||
} | ||
|
||
func (network mirrorNetwork) getNextChannel() (mirror.ConsensusServiceClient, error) { | ||
if channel, ok := network.channels[network.network[network.index]]; ok { | ||
network.index = (network.index + 1) % uint(len(network.network)) | ||
return channel, nil | ||
} | ||
|
||
conn, err := grpc.Dial(network.network[network.index], grpc.WithInsecure()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
channel := mirror.NewConsensusServiceClient(conn) | ||
|
||
network.channels[network.network[network.index]] = channel | ||
network.index = (network.index + 1) % uint(len(network.network)) | ||
return channel, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package hedera | ||
|
||
type SubscriptionHandle struct { | ||
onUnsubscribe func() | ||
} | ||
|
||
func newSubscriptionHandle(onUnsubscribe func()) SubscriptionHandle { | ||
return SubscriptionHandle{onUnsubscribe: onUnsubscribe} | ||
} | ||
|
||
func (handle SubscriptionHandle) Unsubscribe() { | ||
handle.onUnsubscribe() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package hedera | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/hashgraph/hedera-sdk-go/proto/mirror" | ||
) | ||
|
||
type TopicMessage struct { | ||
ConsensusTimestamp time.Time | ||
Contents []byte | ||
RunningHash []byte | ||
SequenceNumber uint64 | ||
Chunks []TopicMessageChunk | ||
TransactionID *TransactionID | ||
} | ||
|
||
func topicMessageOfSingle(resp *mirror.ConsensusTopicResponse) TopicMessage { | ||
return TopicMessage{ | ||
ConsensusTimestamp: timeFromProtobuf(resp.ConsensusTimestamp), | ||
Contents: resp.Message, | ||
RunningHash: resp.RunningHash, | ||
SequenceNumber: resp.SequenceNumber, | ||
Chunks: nil, | ||
TransactionID: nil, | ||
} | ||
} | ||
|
||
func topicMessageOfMany(message []*mirror.ConsensusTopicResponse) TopicMessage { | ||
length := len(message) | ||
size := uint64(0) | ||
chunks := make([]TopicMessageChunk, length) | ||
messages := make([][]byte, length) | ||
var transactionID *TransactionID = nil | ||
|
||
for _, m := range message { | ||
if transactionID == nil { | ||
value := transactionIDFromProtobuf(m.ChunkInfo.InitialTransactionID) | ||
transactionID = &value | ||
} | ||
|
||
chunks[m.ChunkInfo.Number-1] = newTopicMessageChunk(m) | ||
messages[m.ChunkInfo.Number-1] = m.Message | ||
size += uint64(len(m.Message)) | ||
} | ||
|
||
finalMessage := make([]byte, 0, size) | ||
|
||
for _, m := range messages { | ||
finalMessage = append(finalMessage, m...) | ||
} | ||
|
||
return TopicMessage{ | ||
ConsensusTimestamp: timeFromProtobuf(message[length-1].ConsensusTimestamp), | ||
RunningHash: message[length-1].RunningHash, | ||
SequenceNumber: message[length-1].SequenceNumber, | ||
Contents: finalMessage, | ||
Chunks: chunks, | ||
TransactionID: transactionID, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package hedera | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/hashgraph/hedera-sdk-go/proto/mirror" | ||
) | ||
|
||
type TopicMessageChunk struct { | ||
ConsensusTimestamp time.Time | ||
ContentSize uint64 | ||
RunningHash []byte | ||
SequenceNumber uint64 | ||
} | ||
|
||
func newTopicMessageChunk(resp *mirror.ConsensusTopicResponse) TopicMessageChunk { | ||
return TopicMessageChunk{ | ||
ConsensusTimestamp: timeFromProtobuf(resp.ConsensusTimestamp), | ||
ContentSize: uint64(len(resp.Message)), | ||
RunningHash: resp.RunningHash, | ||
SequenceNumber: resp.SequenceNumber, | ||
} | ||
} |
Oops, something went wrong.