/
diff_v8.go
145 lines (126 loc) · 3.7 KB
/
diff_v8.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
package miner
import (
"context"
"fmt"
cbg "github.com/whyrusleeping/cbor-gen"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"github.com/filecoin-project/go-hamt-ipld/v3"
"github.com/filecoin-project/go-state-types/abi"
miner8 "github.com/filecoin-project/go-state-types/builtin/v8/miner"
"github.com/filecoin-project/lily/chain/actors/adt"
"github.com/filecoin-project/lily/chain/actors/adt/diff"
)
func DiffPreCommitsV8(ctx context.Context, store adt.Store, pre, cur State) (*PreCommitChangesV8, error) {
ctx, span := otel.Tracer("").Start(ctx, "DiffPreCommits")
defer span.End()
prep, err := pre.PrecommitsMap()
if err != nil {
return nil, err
}
curp, err := cur.PrecommitsMap()
if err != nil {
return nil, err
}
prepR, err := prep.Root()
if err != nil {
return nil, err
}
curpR, err := curp.Root()
if err != nil {
return nil, err
}
diffContainer := NewPreCommitDiffContainerV8(pre, cur)
if prepR.Equals(curpR) {
return diffContainer.Results, nil
}
if MapRequiresLegacyDiffing(pre, cur,
&adt.MapOpts{
Bitwidth: pre.SectorsAmtBitwidth(),
HashFunc: pre.PrecommitsMapHashFunction(),
},
&adt.MapOpts{
Bitwidth: cur.PrecommitsMapBitWidth(),
HashFunc: cur.PrecommitsMapHashFunction(),
}) {
if span.IsRecording() {
span.SetAttributes(attribute.String("diff", "slow"))
}
err = diff.CompareMap(prep, curp, diffContainer)
if err != nil {
return nil, fmt.Errorf("diff miner precommit: %w", err)
}
return diffContainer.Results, nil
}
if span.IsRecording() {
span.SetAttributes(attribute.String("diff", "fast"))
}
changes, err := diff.Hamt(ctx, prep, curp, store, store, hamt.UseHashFunction(pre.PrecommitsMapHashFunction()), hamt.UseTreeBitWidth(pre.PrecommitsMapBitWidth()))
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.Remove:
if err := diffContainer.Remove(change.Key, change.Before); err != nil {
return nil, err
}
case hamt.Modify:
if err := diffContainer.Modify(change.Key, change.Before, change.After); err != nil {
return nil, err
}
}
}
return diffContainer.Results, nil
}
type PreCommitChangesV8 struct {
Added []miner8.SectorPreCommitOnChainInfo
Removed []miner8.SectorPreCommitOnChainInfo
}
func MakePreCommitChangesV8() *PreCommitChangesV8 {
return &PreCommitChangesV8{
Added: []miner8.SectorPreCommitOnChainInfo{},
Removed: []miner8.SectorPreCommitOnChainInfo{},
}
}
func NewPreCommitDiffContainerV8(pre, cur State) *preCommitDiffContainerV8 {
return &preCommitDiffContainerV8{
Results: MakePreCommitChangesV8(),
pre: pre,
after: cur,
}
}
type preCommitDiffContainerV8 struct {
Results *PreCommitChangesV8
pre, after State
}
func (m *preCommitDiffContainerV8) AsKey(key string) (abi.Keyer, error) {
sector, err := abi.ParseUIntKey(key)
if err != nil {
return nil, fmt.Errorf("pre commit diff container as key: %w", err)
}
return abi.UIntKey(sector), nil
}
func (m *preCommitDiffContainerV8) Add(key string, val *cbg.Deferred) error {
sp, err := m.after.DecodeSectorPreCommitOnChainInfoToV8(val)
if err != nil {
return fmt.Errorf("pre commit diff container add: %w", err)
}
m.Results.Added = append(m.Results.Added, sp)
return nil
}
func (m *preCommitDiffContainerV8) Modify(key string, from, to *cbg.Deferred) error {
return nil
}
func (m *preCommitDiffContainerV8) Remove(key string, val *cbg.Deferred) error {
sp, err := m.pre.DecodeSectorPreCommitOnChainInfoToV8(val)
if err != nil {
return fmt.Errorf("pre commit diff container remove: %w", err)
}
m.Results.Removed = append(m.Results.Removed, sp)
return nil
}