-
Notifications
You must be signed in to change notification settings - Fork 44
/
diff.go
211 lines (189 loc) · 5.06 KB
/
diff.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package datacap
import (
"bytes"
"context"
"github.com/multiformats/go-varint"
cbg "github.com/whyrusleeping/cbor-gen"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-hamt-ipld/v3"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lily/chain/actors/adt"
"github.com/filecoin-project/lily/chain/actors/adt/diff"
)
type BalanceInfo struct {
Address address.Address
DataCap abi.StoragePower
}
type BalanceChange struct {
Before BalanceInfo
After BalanceInfo
}
type BalanceChanges struct {
Added []BalanceInfo
Modified []BalanceChange
Removed []BalanceInfo
}
func DiffDataCapBalances(ctx context.Context, store adt.Store, pre, cur State) (*BalanceChanges, error) {
ctx, span := otel.Tracer("").Start(ctx, "DiffDataCapBalances")
defer span.End()
preMap, err := pre.VerifiedClients()
if err != nil {
return nil, err
}
curMap, err := cur.VerifiedClients()
if err != nil {
return nil, err
}
return diffBalanceMap(ctx, store, pre, cur, preMap, curMap,
&adt.MapOpts{
Bitwidth: pre.VerifiedClientsMapBitWidth(),
HashFunc: pre.VerifiedClientsMapHashFunction(),
},
&adt.MapOpts{
Bitwidth: cur.VerifiedClientsMapBitWidth(),
HashFunc: cur.VerifiedClientsMapHashFunction(),
},
)
}
func diffBalanceMap(ctx context.Context, store adt.Store, pre, cur State, preM, curM adt.Map, preOpts, curOpts *adt.MapOpts) (*BalanceChanges, error) {
ctx, span := otel.Tracer("").Start(ctx, "diffStates")
defer span.End()
diffContainer := NewBalanceDiffContainer(pre, cur)
if mapRequiresLegacyDiffing(pre, cur, preOpts, curOpts) {
if span.IsRecording() {
span.SetAttributes(attribute.String("diff", "slow"))
}
if err := diff.CompareMap(preM, curM, diffContainer); err != nil {
return nil, err
}
return diffContainer.Results, nil
}
if span.IsRecording() {
span.SetAttributes(attribute.String("diff", "fast"))
}
changes, err := diff.Hamt(ctx, preM, curM, store, store, hamt.UseHashFunction(hamt.HashFunc(preOpts.HashFunc)), hamt.UseTreeBitWidth(preOpts.Bitwidth))
if err != nil {
return nil, err
}
for _, change := range changes {
switch change.Type {
case hamt.Add:
if err := diffContainer.Add(change.Key, change.After); err != nil {
return nil, err
}
case hamt.Modify:
if err := diffContainer.Modify(change.Key, change.Before, change.After); err != nil {
return nil, err
}
case hamt.Remove:
if err := diffContainer.Add(change.Key, change.Before); err != nil {
return nil, err
}
}
}
return diffContainer.Results, nil
}
func NewBalanceDiffContainer(pre, cur State) *balanceDiffContainer {
return &balanceDiffContainer{
Results: new(BalanceChanges),
pre: pre,
after: cur,
}
}
type balanceDiffContainer struct {
Results *BalanceChanges
pre, after State
}
func (m *balanceDiffContainer) AsKey(key string) (abi.Keyer, error) {
addr, err := address.NewFromBytes([]byte(key))
if err != nil {
return nil, err
}
return abi.AddrKey(addr), nil
}
func (m *balanceDiffContainer) Add(key string, val *cbg.Deferred) error {
id, n, err := varint.FromUvarint([]byte(key))
if n != len([]byte(key)) {
return xerrors.Errorf("could not get varint from address string")
}
if err != nil {
return err
}
addr, err := address.NewIDAddress(id)
if err != nil {
return err
}
var sp abi.StoragePower
if err := sp.UnmarshalCBOR(bytes.NewReader(val.Raw)); err != nil {
return err
}
m.Results.Added = append(m.Results.Added, BalanceInfo{
Address: addr,
DataCap: sp,
})
return nil
}
func (m *balanceDiffContainer) Modify(key string, before, after *cbg.Deferred) error {
id, n, err := varint.FromUvarint([]byte(key))
if n != len([]byte(key)) {
return xerrors.Errorf("could not get varint from address string")
}
if err != nil {
return err
}
addr, err := address.NewIDAddress(id)
if err != nil {
return err
}
var bsp abi.StoragePower
if err := bsp.UnmarshalCBOR(bytes.NewReader(before.Raw)); err != nil {
return err
}
var asp abi.StoragePower
if err := asp.UnmarshalCBOR(bytes.NewReader(after.Raw)); err != nil {
return err
}
m.Results.Modified = append(m.Results.Modified, BalanceChange{
Before: BalanceInfo{
Address: addr,
DataCap: bsp,
},
After: BalanceInfo{
Address: addr,
DataCap: asp,
},
})
return nil
}
func (m *balanceDiffContainer) Remove(key string, val *cbg.Deferred) error {
id, n, err := varint.FromUvarint([]byte(key))
if n != len([]byte(key)) {
return xerrors.Errorf("could not get varint from address string")
}
if err != nil {
return err
}
addr, err := address.NewIDAddress(id)
if err != nil {
return err
}
var sp abi.StoragePower
if err := sp.UnmarshalCBOR(bytes.NewReader(val.Raw)); err != nil {
return err
}
m.Results.Removed = append(m.Results.Removed, BalanceInfo{
Address: addr,
DataCap: sp,
})
return nil
}
func mapRequiresLegacyDiffing(pre, cur State, pOpts, cOpts *adt.MapOpts) bool {
// bitwidth or hashfunction differences mean legacy diffing.
if !pOpts.Equal(cOpts) {
return true
}
return false
}