/
init.go
109 lines (96 loc) · 3.32 KB
/
init.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
package init_ // nolint: revive
import (
"context"
"fmt"
logging "github.com/ipfs/go-log/v2"
"go.opentelemetry.io/otel"
"go.uber.org/zap"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
init_ "github.com/filecoin-project/lily/chain/actors/builtin/init"
"github.com/filecoin-project/lily/model"
initmodel "github.com/filecoin-project/lily/model/actors/init"
"github.com/filecoin-project/lily/tasks/actorstate"
"github.com/filecoin-project/specs-actors/actors/builtin"
)
var log = logging.Logger("lily/tasks/init")
// InitExtractor extracts init actor state
type InitExtractor struct{}
func (InitExtractor) Extract(ctx context.Context, a actorstate.ActorInfo, node actorstate.ActorStateAPI) (model.Persistable, error) {
log.Debugw("extract", zap.String("extractor", "InitExtractor"), zap.Inline(a))
ctx, span := otel.Tracer("").Start(ctx, "InitExtractor.Extract")
defer span.End()
if span.IsRecording() {
span.SetAttributes(a.Attributes()...)
}
// genesis state.
if a.Current.Height() == 1 {
initActorState, err := init_.Load(node.Store(), &a.Actor)
if err != nil {
return nil, err
}
out := initmodel.IDAddressList{}
for _, builtinAddress := range []address.Address{
builtin.SystemActorAddr, builtin.InitActorAddr,
builtin.RewardActorAddr, builtin.CronActorAddr, builtin.StoragePowerActorAddr, builtin.StorageMarketActorAddr,
builtin.VerifiedRegistryActorAddr, builtin.BurntFundsActorAddr,
} {
out = append(out, &initmodel.IDAddress{
Height: 0,
ID: builtinAddress.String(),
Address: builtinAddress.String(),
StateRoot: a.Executed.ParentState().String(),
})
}
if err := initActorState.ForEachActor(func(id abi.ActorID, addr address.Address) error {
idAddr, err := address.NewIDAddress(uint64(id))
if err != nil {
return err
}
out = append(out, &initmodel.IDAddress{
Height: int64(a.Current.Height()),
ID: idAddr.String(),
Address: addr.String(),
StateRoot: a.Current.ParentState().String(),
})
return nil
}); err != nil {
return nil, err
}
return out, nil
}
prevActor, err := node.Actor(ctx, a.Address, a.Executed.Key())
if err != nil {
return nil, fmt.Errorf("loading previous init actor: %w", err)
}
prevState, err := init_.Load(node.Store(), prevActor)
if err != nil {
return nil, fmt.Errorf("loading previous init actor state: %w", err)
}
curState, err := init_.Load(node.Store(), &a.Actor)
if err != nil {
return nil, fmt.Errorf("loading current init actor state: %w", err)
}
addressChanges, err := init_.DiffAddressMap(ctx, node.Store(), prevState, curState)
if err != nil {
return nil, fmt.Errorf("diffing init actor state: %w", err)
}
out := make(initmodel.IDAddressList, 0, len(addressChanges.Added)+len(addressChanges.Modified))
for _, newAddr := range addressChanges.Added {
out = append(out, &initmodel.IDAddress{
Height: int64(a.Current.Height()),
StateRoot: a.Current.ParentState().String(),
ID: newAddr.ID.String(),
Address: newAddr.PK.String(),
})
}
for _, modAddr := range addressChanges.Modified {
out = append(out, &initmodel.IDAddress{
Height: int64(a.Current.Height()),
StateRoot: a.Current.ParentState().String(),
ID: modAddr.To.ID.String(),
Address: modAddr.To.PK.String(),
})
}
return out, nil
}