-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (93 loc) · 3.27 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
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
package main
import (
"context"
"fmt"
"github.com/bluesign/stateFollower/websocket"
"io"
"log"
"net/http"
"time"
gcloud "cloud.google.com/go/storage"
"github.com/fxamacker/cbor/v2"
client "github.com/onflow/flow-go-sdk/access/grpc"
"github.com/onflow/flow-go/engine/execution/computation/computer/uploader"
"github.com/onflow/flow-go/model/flow"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func DownloadAndDumpEvents(bucket *gcloud.BucketHandle, blockID string, height uint64) error {
//google cloud bucket
object := bucket.Object(fmt.Sprintf("%s.cbor", blockID))
reader, err := object.NewReader(context.Background())
if err != nil {
return fmt.Errorf("could not create object reader: %w", err)
}
data, err := io.ReadAll(reader)
if err != nil {
return fmt.Errorf("could not read execution record: %w", err)
}
var record uploader.BlockData
decOptions := cbor.DecOptions{
ExtraReturnErrors: cbor.ExtraDecErrorUnknownField,
}
decoder, err := decOptions.DecMode()
if err != nil {
panic(err)
}
err = decoder.Unmarshal(data, &record)
if err != nil {
return fmt.Errorf("could not decode execution record: %w", err)
}
if record.FinalStateCommitment == flow.DummyStateCommitment {
return fmt.Errorf("execution record contains empty state commitment")
}
if record.Block.Header.Height == 0 {
return fmt.Errorf("execution record contains empty block data")
}
for _, event := range record.Events {
fmt.Printf("BlockID\t\t : %s - %d \nTransactionID\t : %s\nTransactionIndex : %d\nEventIndex\t : %d\n",
blockID, height, event.TransactionID, event.TransactionIndex, event.EventIndex)
fmt.Printf("Event Type\t: %s\n", event.Type)
// fmt.Printf("Event Payload\t: %s\n\n", string(event.Payload))
server.Publish(string(event.Type), event.Payload)
}
return nil
}
var server = &websocket.Server{Subscriptions: make(websocket.Subscription)}
func main() {
go func() {
log.SetFlags(log.Lshortfile)
handler := websocket.NewHandler(server)
http.HandleFunc("/socket", handler.HandleWS)
if err := http.ListenAndServeTLS(":443", "fullchain.pem", "privkey.pem", nils); err != nil {
panic(err)
}
}()
flow, errGrpc := client.NewBaseClient("access-001.mainnet22.nodes.onflow.org:9000", grpc.WithTransportCredentials(insecure.NewCredentials()))
if errGrpc != nil {
log.Fatal("Failed to establish connection with the Access API")
}
latestBlock, _ := flow.GetLatestBlock(context.Background(), true)
lastBlockHeightProcessed := uint64(latestBlock.Height) //spork start + 10000 to skip empty ones
gcloudClient, _ := gcloud.NewClient(context.Background(), option.WithoutAuthentication())
bucket := gcloudClient.Bucket("flow_public_mainnet22_execution_state")
for {
block, _ := flow.GetLatestBlock(context.Background(), true)
for i := lastBlockHeightProcessed + 1; i <= block.Height; i++ {
log.Printf("Processing block height: %d\n", i)
header, _ := flow.GetBlockByHeight(context.Background(), i)
lastBlockHeightProcessed = i
if len(header.CollectionGuarantees) == 0 {
//has no collection
continue
}
log.Println(header.ID.String())
err := DownloadAndDumpEvents(bucket, header.ID.String(), i)
if err != nil {
log.Fatal(err)
}
}
time.Sleep(10 * time.Millisecond)
}
}