-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfactory.go
302 lines (256 loc) · 10 KB
/
factory.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package fil_parser
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/manifest"
types2 "github.com/filecoin-project/lotus/chain/types"
"github.com/google/uuid"
"github.com/zondax/fil-parser/actors/cache"
"github.com/zondax/fil-parser/actors/cache/impl/common"
logger2 "github.com/zondax/fil-parser/logger"
"github.com/zondax/fil-parser/parser"
helper2 "github.com/zondax/fil-parser/parser/helper"
v1 "github.com/zondax/fil-parser/parser/v1"
v2 "github.com/zondax/fil-parser/parser/v2"
"github.com/zondax/fil-parser/tools"
multisigTools "github.com/zondax/fil-parser/tools/multisig"
"github.com/zondax/fil-parser/types"
rosettaFilecoinLib "github.com/zondax/rosetta-filecoin-lib"
"go.uber.org/zap"
)
var (
errUnknownImpl = errors.New("unknown implementation")
errUnknownVersion = errors.New("unknown trace version")
)
type FilecoinParser struct {
parserV1 Parser
parserV2 Parser
Helper *helper2.Helper
logger *zap.Logger
}
type Parser interface {
Version() string
NodeVersionsSupported() []string
ParseTransactions(ctx context.Context, txsData types.TxsData) (*types.TxsParsedResult, error)
ParseNativeEvents(ctx context.Context, eventsData types.EventsData) (*types.EventsParsedResult, error)
ParseMultisigEvents(ctx context.Context, multisigTxs []*types.Transaction, tipsetCid string, tipsetKey types2.TipSetKey) (*types.MultisigEvents, error)
ParseEthLogs(ctx context.Context, eventsData types.EventsData) (*types.EventsParsedResult, error)
GetBaseFee(traces []byte, tipset *types.ExtendedTipSet) (uint64, error)
IsNodeVersionSupported(ver string) bool
}
func NewFilecoinParser(lib *rosettaFilecoinLib.RosettaConstructionFilecoin, cacheSource common.DataSource, logger *zap.Logger) (*FilecoinParser, error) {
logger = logger2.GetSafeLogger(logger)
actorsCache, err := cache.SetupActorsCache(cacheSource, logger)
if err != nil {
logger.Sugar().Errorf("could not setup actors cache: %v", err)
return nil, err
}
helper := helper2.NewHelper(lib, actorsCache, cacheSource.Node, logger)
parserV1 := v1.NewParser(helper, logger)
parserV2 := v2.NewParser(helper, logger)
return &FilecoinParser{
parserV1: parserV1,
parserV2: parserV2,
Helper: helper,
logger: logger,
}, nil
}
func (p *FilecoinParser) ParseTransactions(ctx context.Context, txsData types.TxsData) (*types.TxsParsedResult, error) {
parserVersion, err := p.translateParserVersionFromMetadata(txsData.Metadata)
if err != nil {
return nil, errUnknownVersion
}
var parsedResult *types.TxsParsedResult
p.logger.Sugar().Debugf("trace files node version: [%s] - parser to use: [%s]", txsData.Metadata.NodeMajorMinorVersion, parserVersion)
switch parserVersion {
case v1.Version:
parsedResult, err = p.parserV1.ParseTransactions(ctx, txsData)
case v2.Version:
parsedResult, err = p.parserV2.ParseTransactions(ctx, txsData)
default:
p.logger.Sugar().Errorf("[parser] implementation not supported: %s", parserVersion)
return nil, errUnknownImpl
}
if err != nil {
return nil, err
}
parsedResult.Txs = p.FilterDuplicated(parsedResult.Txs)
return parsedResult, nil
}
func (p *FilecoinParser) ParseNativeEvents(ctx context.Context, eventsData types.EventsData) (*types.EventsParsedResult, error) {
parserVersion, err := p.translateParserVersionFromMetadata(eventsData.Metadata)
if err != nil {
return nil, errUnknownVersion
}
var parsedResult *types.EventsParsedResult
p.logger.Sugar().Debugf("trace files node version: [%s] - parser to use: [%s]", eventsData.Metadata.NodeMajorMinorVersion, parserVersion)
switch parserVersion {
case v1.Version, v2.Version:
parsedResult, err = p.parserV2.ParseNativeEvents(ctx, eventsData)
default:
p.logger.Sugar().Errorf("[parser] implementation not supported: %s", parserVersion)
return nil, errUnknownImpl
}
if err != nil {
return nil, err
}
return parsedResult, nil
}
func (p *FilecoinParser) ParseEthLogs(ctx context.Context, eventsData types.EventsData) (*types.EventsParsedResult, error) {
parserVersion, err := p.translateParserVersionFromMetadata(eventsData.Metadata)
if err != nil {
return nil, errUnknownVersion
}
var parsedResult *types.EventsParsedResult
p.logger.Sugar().Debugf("trace files node version: [%s] - parser to use: [%s]", eventsData.Metadata.NodeMajorMinorVersion, parserVersion)
switch parserVersion {
case v1.Version, v2.Version:
parsedResult, err = p.parserV2.ParseEthLogs(ctx, eventsData)
default:
p.logger.Sugar().Errorf("[parser] implementation not supported: %s", parserVersion)
return nil, errUnknownImpl
}
if err != nil {
return nil, err
}
return parsedResult, nil
}
func (p *FilecoinParser) ParseMultisigEvents(ctx context.Context, txs []*types.Transaction, tipsetCid string, tipsetKey types2.TipSetKey) (*types.MultisigEvents, error) {
multisigTxs, err := p.Helper.FilterTxsByActorType(ctx, txs, manifest.MultisigKey, tipsetKey)
if err != nil {
return nil, err
}
return p.parserV2.ParseMultisigEvents(ctx, multisigTxs, tipsetCid, tipsetKey)
}
func (p *FilecoinParser) translateParserVersionFromMetadata(metadata types.BlockMetadata) (string, error) {
switch {
// The empty string is for backwards compatibility with older traces versions
case p.parserV1.IsNodeVersionSupported(metadata.NodeMajorMinorVersion), metadata.NodeMajorMinorVersion == "":
return v1.Version, nil
case p.parserV2.IsNodeVersionSupported(metadata.NodeMajorMinorVersion):
return v2.Version, nil
default:
p.logger.Sugar().Errorf("[parser] unsupported node version: %s", metadata.NodeFullVersion)
return "", fmt.Errorf("node version not supported %s", metadata.NodeFullVersion)
}
}
func (p *FilecoinParser) FilterDuplicated(txs []*types.Transaction) []*types.Transaction {
idsFound := make(map[string]bool)
filteredTxs := make([]*types.Transaction, 0)
for _, tx := range txs {
if _, found := idsFound[tx.Id]; !found {
idsFound[tx.Id] = true
filteredTxs = append(filteredTxs, tx)
}
}
return filteredTxs
}
func (p *FilecoinParser) GetBaseFee(traces []byte, metadata types.BlockMetadata, tipset *types.ExtendedTipSet) (uint64, error) {
parserVersion, err := p.translateParserVersionFromMetadata(metadata)
if err != nil {
return 0, errUnknownVersion
}
p.logger.Sugar().Debugf("trace files node version: [%s] - parser to use: [%s]", metadata.NodeMajorMinorVersion, parserVersion)
switch parserVersion {
case v1.Version:
return p.parserV1.GetBaseFee(traces, tipset)
case v2.Version:
return p.parserV2.GetBaseFee(traces, tipset)
}
return 0, errUnknownImpl
}
func (p *FilecoinParser) ParseGenesis(genesis *types.GenesisBalances, genesisTipset *types.ExtendedTipSet) ([]*types.Transaction, *types.AddressInfoMap) {
genesisTxs := make([]*types.Transaction, 0)
addresses := types.NewAddressInfoMap()
genesisTimestamp := parser.GetTimestamp(genesisTipset.MinTimestamp())
for _, balance := range genesis.Actors.All {
if balance.Value.Balance == "0" {
continue
}
filAdd, _ := address.NewFromString(balance.Key)
shortAdd, _ := p.Helper.GetActorsCache().GetShortAddress(filAdd)
robustAdd, _ := p.Helper.GetActorsCache().GetRobustAddress(filAdd)
actorCode, _ := p.Helper.GetActorsCache().GetActorCode(filAdd, types2.EmptyTSK, false)
actorName, _ := p.Helper.GetActorNameFromAddress(filAdd, 0, types2.EmptyTSK)
addresses.Set(balance.Key, &types.AddressInfo{
Short: shortAdd,
Robust: robustAdd,
ActorCid: actorCode,
ActorType: actorName,
})
amount, _ := big.FromString(balance.Value.Balance)
tipsetCid := genesisTipset.GetCidString()
txType := "Genesis"
blockCid := genesisTipset.Key().String()
blockCid = strings.ReplaceAll(blockCid, "{", "")
blockCid = strings.ReplaceAll(blockCid, "}", "")
genesisTxs = append(genesisTxs, &types.Transaction{
TxBasicBlockData: types.TxBasicBlockData{
BasicBlockData: types.BasicBlockData{
Height: 0,
TipsetCid: tipsetCid,
},
BlockCid: blockCid,
},
Id: tools.BuildId(genesisTipset.Key().String(), balance.Key, balance.Value.Balance),
ParentId: uuid.Nil.String(),
Level: 0,
TxTimestamp: genesisTimestamp,
TxTo: balance.Key,
Amount: amount.Int,
Status: "Ok",
TxType: txType,
TxMetadata: "{}",
})
}
return genesisTxs, addresses
}
func (p *FilecoinParser) ParseGenesisMultisig(ctx context.Context, genesis *types.GenesisBalances, genesisTipset *types.ExtendedTipSet) ([]*types.MultisigInfo, error) {
var multisigInfos []*types.MultisigInfo
for _, actor := range genesis.Actors.All {
addrStr := actor.Key
// parse address
addr, err := address.NewFromString(addrStr)
if err != nil {
p.logger.Sugar().Errorf("could not parse address: %s. err: %s", addrStr, err)
continue
}
// get actor name from address
actorName, err := p.Helper.GetActorNameFromAddress(addr, int64(parser.GenesisHeight), genesisTipset.Key())
if err != nil {
p.logger.Sugar().Errorf("could not get actor name from address: %s. err: %s", addrStr, err)
continue
}
// check if the address is a multisig address
if !strings.EqualFold(actorName, manifest.MultisigKey) {
continue
}
api := p.Helper.GetFilecoinNodeClient()
metadata, err := multisigTools.GenerateGenesisMultisigData(ctx, api, addr, genesisTipset)
if err != nil {
return nil, fmt.Errorf("multisigTools.GenerateGenesisMultisigData(%s): %s", addrStr, err)
}
metadataJson, err := json.Marshal(metadata)
if err != nil {
return nil, fmt.Errorf("json.Marshal(): %s", err)
}
multisigInfo := &types.MultisigInfo{
ID: tools.BuildId(genesisTipset.GetCidString(), addrStr, fmt.Sprint(parser.GenesisHeight), "", parser.TxTypeGenesis),
MultisigAddress: addrStr,
Height: parser.GenesisHeight,
ActionType: parser.MultisigConstructorMethod,
Value: string(metadataJson),
// there is no signer as this is genesis
Signer: "",
// there are no transactions for the multisig addresses in the genesis block
TxCid: "",
}
multisigInfos = append(multisigInfos, multisigInfo)
}
return multisigInfos, nil
}