This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rounds.go
187 lines (150 loc) · 4.36 KB
/
rounds.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package types
import (
"context"
"fmt"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-hamt-ipld"
cbornode "github.com/ipfs/go-ipld-cbor"
"github.com/quorumcontrol/chaintree/nodestore"
"github.com/quorumcontrol/chaintree/safewrap"
"github.com/quorumcontrol/messages/v2/build/go/gossip"
"github.com/quorumcontrol/messages/v2/build/go/services"
"github.com/quorumcontrol/tupelo-go-sdk/gossip/hamtwrapper"
)
func init() {
cbornode.RegisterCborType(services.AddBlockRequest{})
cbornode.RegisterCborType(gossip.Round{})
cbornode.RegisterCborType(gossip.RoundConfirmation{})
}
type RoundWrapper struct {
round *gossip.Round
wrapped *cbornode.Node
checkpoint *CheckpointWrapper
hamtNode *hamt.Node
store nodestore.DagStore
}
func WrapRound(round *gossip.Round) *RoundWrapper {
sw := safewrap.SafeWrap{}
node := sw.WrapObject(round)
return &RoundWrapper{
round: round,
wrapped: node,
}
}
func (r *RoundWrapper) Value() *gossip.Round {
return r.round
}
func (r *RoundWrapper) CID() cid.Cid {
return r.Wrapped().Cid()
}
func (r *RoundWrapper) Wrapped() *cbornode.Node {
return r.wrapped
}
func (r *RoundWrapper) Height() uint64 {
return r.round.Height
}
func (r *RoundWrapper) SetStore(store nodestore.DagStore) {
r.store = store
}
func (r *RoundWrapper) FetchCheckpoint(ctx context.Context) (*CheckpointWrapper, error) {
if r.checkpoint != nil {
return r.checkpoint, nil
}
if r.store == nil {
return nil, fmt.Errorf("missing a store on the completed round, use SetStore")
}
checkpoint := &gossip.Checkpoint{}
checkpointCid, err := cid.Cast(r.round.CheckpointCid)
if err != nil {
return nil, fmt.Errorf("error casting checkpoint cid: %v", err)
}
checkpointNode, err := r.store.Get(ctx, checkpointCid)
if err != nil {
return nil, fmt.Errorf("error fetching checkpoint %w", err)
}
err = cbornode.DecodeInto(checkpointNode.RawData(), checkpoint)
if err != nil {
return nil, fmt.Errorf("error decoding %w", err)
}
r.checkpoint = WrapCheckpoint(checkpoint)
return r.checkpoint, nil
}
func (r *RoundWrapper) FetchHamt(ctx context.Context) (*hamt.Node, error) {
if r.hamtNode != nil {
return r.hamtNode, nil
}
if r.store == nil {
return nil, fmt.Errorf("missing a store on the completed round, use SetStore")
}
hamtStore := hamtwrapper.DagStoreToCborIpld(r.store)
stateCid, err := cid.Cast(r.round.StateCid)
if err != nil {
return nil, fmt.Errorf("error casting state cid: %v", err)
}
n, err := hamt.LoadNode(ctx, hamtStore, stateCid, hamt.UseTreeBitWidth(5))
if err != nil {
return nil, fmt.Errorf("error loading hamt %w", err)
}
r.hamtNode = n
return n, nil
}
func WrapRoundConfirmation(conf *gossip.RoundConfirmation) *RoundConfirmationWrapper {
sw := safewrap.SafeWrap{}
wrapped := sw.WrapObject(conf)
return &RoundConfirmationWrapper{
value: conf,
wrapped: wrapped,
}
}
type RoundConfirmationWrapper struct {
value *gossip.RoundConfirmation
wrapped *cbornode.Node
completedRound *RoundWrapper
store nodestore.DagStore
}
func (rc *RoundConfirmationWrapper) Value() *gossip.RoundConfirmation {
return rc.value
}
func (rc *RoundConfirmationWrapper) SetStore(store nodestore.DagStore) {
rc.store = store
}
func (rc *RoundConfirmationWrapper) Height() uint64 {
return rc.value.Height
}
func (rc *RoundConfirmationWrapper) FetchCompletedRound(ctx context.Context) (*RoundWrapper, error) {
if rc.completedRound != nil {
return rc.completedRound, nil
}
if rc.store == nil {
return nil, fmt.Errorf("missing a store on the round confirmation, use SetStore")
}
roundCid, err := cid.Cast(rc.value.RoundCid)
if err != nil {
return nil, fmt.Errorf("error casting round cid: %v", err)
}
roundNode, err := rc.store.Get(ctx, roundCid)
if err != nil {
return nil, fmt.Errorf("error getting node: %w", err)
}
completedRound := &gossip.Round{}
err = cbornode.DecodeInto(roundNode.RawData(), completedRound)
if err != nil {
return nil, fmt.Errorf("error decoding: %w", err)
}
wrappedCompletedRound := WrapRound(completedRound)
wrappedCompletedRound.SetStore(rc.store)
rc.completedRound = wrappedCompletedRound
return wrappedCompletedRound, nil
}
func (rc *RoundConfirmationWrapper) Data() []byte {
return rc.Wrapped().RawData()
}
func (rc *RoundConfirmationWrapper) Wrapped() *cbornode.Node {
if rc.wrapped != nil {
return rc.wrapped
}
sw := safewrap.SafeWrap{}
n := sw.WrapObject(rc)
rc.wrapped = n
return n
}