-
Notifications
You must be signed in to change notification settings - Fork 86
/
main.go
87 lines (67 loc) · 2.7 KB
/
main.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
/*
* Flow Go SDK
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"fmt"
"github.com/onflow/flow-go-sdk/access/http"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/examples"
)
func main() {
VerifyEventsDemo()
}
// VerifyEventsDemo showcases how clients can verify events they received.
// In mature Flow we provision different methods to verify the subset of events queried, however
// for now the only verification available is the hash of all the events emitted in a Chunk/Collection.
// This hash is verified by Verification Nodes so it's correctness is checked.
// For users, its possible to get all the events for chunk, calculate and compare the resulting hash
func VerifyEventsDemo() {
ctx := context.Background()
flowClient, err := http.NewClient(http.TestnetHost)
examples.Handle(err)
latestBlockHeader, err := flowClient.GetLatestBlockHeader(ctx, true)
examples.Handle(err)
block, err := flowClient.GetBlockByID(ctx, latestBlockHeader.ID)
examples.Handle(err)
executionResult, err := flowClient.GetExecutionResultForBlockID(ctx, block.ID)
examples.Handle(err)
totalEvents := 0
for i, colGuarantee := range block.CollectionGuarantees {
collection, err := flowClient.GetCollection(ctx, colGuarantee.CollectionID)
examples.Handle(err)
events := make([]flow.Event, 0)
// only way to get all the events regardless of the type is querying by transaction
for _, txID := range collection.TransactionIDs {
transactionResult, err := flowClient.GetTransactionResult(ctx, txID)
examples.Handle(err)
for _, event := range transactionResult.Events {
events = append(events, event)
}
}
calculatedEventsHash, err := flow.CalculateEventsHash(events)
examples.Handle(err)
// there should be more chunks (one last is system chunk), so we don't check range here
chunk := executionResult.Chunks[i]
if !calculatedEventsHash.Equal(chunk.EventCollection) {
examples.Handle(fmt.Errorf("events hash mismatch, expected %s, calculated %s", chunk.EventCollection, calculatedEventsHash))
}
totalEvents += len(events)
}
fmt.Printf("Events verified for block %s, total %d events\n", block.ID, totalEvents)
}