Skip to content

Commit

Permalink
Consolidate all addresses to robust format (#109)
Browse files Browse the repository at this point in the history
* consolidate all addresses to robust format

* Increase test timeout

* Consolidate robust address

* Tests

* Refactor on consolidate addresses

* more changes

* more changes

* More changes

* minor changes

* enhance ensurerobustAddress
  • Loading branch information
lucaslopezf authored and emmanuelm41 committed Apr 7, 2024
1 parent 574eb9d commit f028c3d
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ lint:
golangci-lint run -E gofmt -E gosec -E goconst -E gocritic --timeout 5m

test:
go test -race ./...
go test -race -timeout 25m ./...
4 changes: 4 additions & 0 deletions actors/cache/actors_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ func (a *ActorsCache) storeRobustAddress(add address.Address, info types.Address
return nil
}

func (a *ActorsCache) StoreAddressInfoAddress(addInfo types.AddressInfo) {
a.offChainCache.StoreAddressInfo(addInfo)
}

func (a *ActorsCache) isBadAddress(add address.Address) bool {
_, bad := a.badAddress.Get(add.String())
return bad
Expand Down
61 changes: 53 additions & 8 deletions actors/cache/impl/zcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package impl
import (
"context"
"fmt"
"github.com/zondax/fil-parser/actors/constants"
"strings"

"github.com/filecoin-project/go-address"
filTypes "github.com/filecoin-project/lotus/chain/types"
Expand All @@ -19,7 +21,7 @@ const (
ZCacheLocalOnly = "in-memory"
ZCacheCombined = "combined"
NoTtl = -1
DummyTtl = -1
NotExpiringTtl = -1
PrefixSplitter = "/"
)

Expand Down Expand Up @@ -131,14 +133,20 @@ func (m *ZCache) GetRobustAddress(address address.Address) (string, error) {
return "", err
}

ctx := context.Background()

if isRobustAddress {
// Already a robust address
// If already a robust address, we attempt to get a f4 address.
// This is particularly useful in the case of EVM actors, where a robust f2 address
// may need to be converted as a f4 address.
if f4Address := m.tryToGetF4Address(address); f4Address != "" {
return f4Address, nil
}

return address.String(), nil
}

// This is a short address, get the robust one
var robustAdd string
ctx := context.Background()
if err = m.shortRobustMap.Get(ctx, address.String(), &robustAdd); err != nil {
return "", common.ErrKeyNotFound
}
Expand Down Expand Up @@ -185,7 +193,7 @@ func (m *ZCache) storeRobustShort(robust string, short string) {
// Possible ZCache types can be Local or Combined. Both types set the TTL at instantiation time
// The ttl here is pointless
ctx := context.Background()
_ = m.robustShortMap.Set(ctx, robust, short, DummyTtl)
_ = m.robustShortMap.Set(ctx, robust, short, NotExpiringTtl)
}

func (m *ZCache) storeShortRobust(short string, robust string) {
Expand All @@ -197,13 +205,22 @@ func (m *ZCache) storeShortRobust(short string, robust string) {
// Possible ZCache types can be Local or Combined. Both types set the TTL at instantiation time
// The ttl here is pointless
ctx := context.Background()
_ = m.shortRobustMap.Set(ctx, short, robust, DummyTtl)
_ = m.shortRobustMap.Set(ctx, short, robust, NotExpiringTtl)
}

func (m *ZCache) StoreAddressInfo(info types.AddressInfo) {
m.storeRobustShort(info.Robust, info.Short)
m.storeShortRobust(info.Short, info.Robust)
m.storeActorCode(info.Short, info.ActorCid)

isEvm := strings.EqualFold(info.ActorType, constants.ActorTypeEVM)
isEvmAndAddressIsF4 := isEvm && strings.HasPrefix(info.Robust, constants.AddressTypePrefixF4)

// Only store the mapping for addresses that are not related to EVM actors,
// or are associated with EVM actors but use an f4 prefix. We skip storing
// addresses when they are f2 for EVM actors because only f4 addresses are of interest.
if !isEvm || isEvmAndAddressIsF4 {
m.storeShortRobust(info.Short, info.Robust)
}
}

func (m *ZCache) storeActorCode(shortAddress string, cid string) {
Expand All @@ -215,5 +232,33 @@ func (m *ZCache) storeActorCode(shortAddress string, cid string) {
// Possible ZCache types can be Local or Combined. Both types set the TTL at instantiation time
// The ttl here is pointless
ctx := context.Background()
_ = m.shortCidMap.Set(ctx, shortAddress, cid, DummyTtl)
_ = m.shortCidMap.Set(ctx, shortAddress, cid, NotExpiringTtl)
}

func (m *ZCache) tryToGetF4Address(address address.Address) string {
ctx := context.Background()

// Return the address if it's already a f4 address
if strings.HasPrefix(address.String(), constants.AddressTypePrefixF4) {
return address.String()
}

// If the robust address is not f4, it should be f2
// Try to get the corresponding f0 for the f2 address
f0Address, err := m.GetShortAddress(address)
if err != nil {
m.logger.Sugar().Errorf("error getting short address for %s: %s", address.String(), err)
return ""
}

// Try to get the f4 address associated with the f0 address
// If no f4 is found, it implies the address might not be an EVM actor type
var f4Address string
err = m.shortRobustMap.Get(ctx, f0Address, &f4Address)
if err == nil && f4Address != "" {
return f4Address
}

m.logger.Sugar().Infof("no f4 address associated with f0 address: %s. The address might not be an EVM actor type.", f0Address)
return ""
}
16 changes: 16 additions & 0 deletions actors/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"bytes"
"encoding/hex"
"github.com/filecoin-project/go-address"
"github.com/zondax/fil-parser/actors/cache"
"github.com/zondax/fil-parser/actors/cache/impl/common"
"github.com/zondax/fil-parser/parser"
"go.uber.org/zap"
)

func (p *ActorParser) parseSend(msg *parser.LotusMessage) map[string]interface{} {
Expand Down Expand Up @@ -40,3 +43,16 @@ func (p *ActorParser) unknownMetadata(msgParams, msgReturn []byte) (map[string]i
func (p *ActorParser) emptyParamsAndReturn() (map[string]interface{}, error) {
return make(map[string]interface{}), nil
}

func EnsureRobustAddress(address address.Address, actorCache *cache.ActorsCache, logger *zap.Logger) string {
if isRobust, _ := common.IsRobustAddress(address); isRobust {
return address.String()
}

robustAddress, err := actorCache.GetRobustAddress(address)
if err != nil {
logger.Sugar().Warnf("Error converting address to robust format: %v", err)
return address.String() // Fallback
}
return robustAddress
}
8 changes: 8 additions & 0 deletions actors/constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package constants

const (
ActorTypeEVM = "evm"
AddressTypePrefixF4 = "f4"

ActorTypeMiner = "miner"
)
10 changes: 7 additions & 3 deletions actors/eam.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"fmt"
"github.com/filecoin-project/go-state-types/abi"
"github.com/zondax/fil-parser/actors/constants"
"github.com/zondax/fil-parser/parser"
"strconv"

Expand Down Expand Up @@ -102,9 +103,10 @@ func (p *ActorParser) parseCreate(rawParams, rawReturn []byte, msgCid cid.Cid) (
Short: parser.FilPrefix + strconv.FormatUint(r.ActorID, 10),
Robust: r.RobustAddress.String(),
EthAddress: parser.EthPrefix + hex.EncodeToString(r.EthAddress[:]),
ActorType: "evm",
ActorType: constants.ActorTypeEVM,
CreationTxCid: msgCid.String(),
}
p.helper.GetActorsCache().StoreAddressInfoAddress(*createdEvmActor)
return metadata, createdEvmActor, nil
}

Expand Down Expand Up @@ -135,9 +137,10 @@ func (p *ActorParser) parseCreate2(rawParams, rawReturn []byte, msgCid cid.Cid)
Short: parser.FilPrefix + strconv.FormatUint(r.ActorID, 10),
Robust: r.RobustAddress.String(),
EthAddress: parser.EthPrefix + hex.EncodeToString(r.EthAddress[:]),
ActorType: "evm",
ActorType: constants.ActorTypeEVM,
CreationTxCid: msgCid.String(),
}
p.helper.GetActorsCache().StoreAddressInfoAddress(*createdEvmActor)
return metadata, createdEvmActor, nil
}

Expand Down Expand Up @@ -171,8 +174,9 @@ func (p *ActorParser) parseCreateExternal(rawParams, rawReturn []byte, msgCid ci
Short: parser.FilPrefix + strconv.FormatUint(r.ActorID, 10),
Robust: r.RobustAddress.String(),
EthAddress: parser.EthPrefix + hex.EncodeToString(r.EthAddress[:]),
ActorType: "evm",
ActorType: constants.ActorTypeEVM,
CreationTxCid: msgCid.String(),
}
p.helper.GetActorsCache().StoreAddressInfoAddress(*createdEvmActor)
return metadata, createdEvmActor, nil
}
2 changes: 2 additions & 0 deletions actors/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (p *ActorParser) parseExec(msg *parser.LotusMessage, rawReturn []byte) (map
CreationTxCid: msg.Cid.String(),
}
metadata[parser.ReturnKey] = createdActor
p.helper.GetActorsCache().StoreAddressInfoAddress(*createdActor)
return metadata, createdActor, nil
}

Expand Down Expand Up @@ -114,6 +115,7 @@ func (p *ActorParser) parseExec4(msg *parser.LotusMessage, rawReturn []byte) (ma
CreationTxCid: msg.Cid.String(),
}
metadata[parser.ReturnKey] = createdActor
p.helper.GetActorsCache().StoreAddressInfoAddress(*createdActor)
return metadata, createdActor, nil
}

Expand Down
4 changes: 3 additions & 1 deletion actors/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actors

import (
"bytes"
"github.com/zondax/fil-parser/actors/constants"

"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/builtin/v11/power"
Expand Down Expand Up @@ -105,10 +106,11 @@ func (p *ActorParser) parseCreateMiner(msg *parser.LotusMessage, rawReturn []byt
createdActor := &types.AddressInfo{
Short: r.IDAddress.String(),
Robust: r.RobustAddress.String(),
ActorType: "miner",
ActorType: constants.ActorTypeMiner,
CreationTxCid: msg.Cid.String(),
}
metadata[parser.ReturnKey] = createdActor
p.helper.GetActorsCache().StoreAddressInfoAddress(*createdActor)
return metadata, createdActor, nil
}

Expand Down
7 changes: 5 additions & 2 deletions parser/v1/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ func (p *Parser) parseTrace(trace typesV1.ExecutionTraceV1, mainMsgCid cid.Cid,

messageUuid := tools.BuildMessageId(tipsetCid, blockCid, mainMsgCid.String(), trace.Msg.Cid().String(), parentId)

txFromRobust := actors.EnsureRobustAddress(trace.Msg.From, p.helper.GetActorsCache(), p.logger)
txToRobust := actors.EnsureRobustAddress(trace.Msg.To, p.helper.GetActorsCache(), p.logger)

tx := &types.Transaction{
TxBasicBlockData: types.TxBasicBlockData{
BasicBlockData: types.BasicBlockData{
Expand All @@ -254,8 +257,8 @@ func (p *Parser) parseTrace(trace typesV1.ExecutionTraceV1, mainMsgCid cid.Cid,
Id: messageUuid,
TxTimestamp: parser.GetTimestamp(tipset.MinTimestamp()),
TxCid: mainMsgCid.String(),
TxFrom: trace.Msg.From.String(),
TxTo: trace.Msg.To.String(),
TxFrom: txFromRobust,
TxTo: txToRobust,
Amount: trace.Msg.Value.Int,
Status: parser.GetExitCodeStatus(trace.MsgRct.ExitCode),
TxType: txType,
Expand Down
6 changes: 4 additions & 2 deletions parser/v2/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ func (p *Parser) parseTrace(trace typesV2.ExecutionTraceV2, mainMsgCid cid.Cid,

tipsetCid := tipset.GetCidString()
messageUuid := tools.BuildMessageId(tipsetCid, blockCid, mainMsgCid.String(), msgCid, parentId)
txFromRobust := actors.EnsureRobustAddress(trace.Msg.From, p.helper.GetActorsCache(), p.logger)
txToRobust := actors.EnsureRobustAddress(trace.Msg.To, p.helper.GetActorsCache(), p.logger)

tx := &types.Transaction{
TxBasicBlockData: types.TxBasicBlockData{
Expand All @@ -230,8 +232,8 @@ func (p *Parser) parseTrace(trace typesV2.ExecutionTraceV2, mainMsgCid cid.Cid,
Id: messageUuid,
TxTimestamp: parser.GetTimestamp(tipset.MinTimestamp()),
TxCid: mainMsgCid.String(),
TxFrom: trace.Msg.From.String(),
TxTo: trace.Msg.To.String(),
TxFrom: txFromRobust,
TxTo: txToRobust,
Amount: trace.Msg.Value.Int,
Status: parser.GetExitCodeStatus(trace.MsgRct.ExitCode),
TxType: txType,
Expand Down

0 comments on commit f028c3d

Please sign in to comment.