Skip to content

Commit

Permalink
Refactor code, consolidate addresses on more places (#114)
Browse files Browse the repository at this point in the history
* refactor fn to make it more generic

* refactor FeesData struct

* consolidate miner address to robust on fees data

* extract transaction fess calculation to function

* fix lint
  • Loading branch information
emmanuelm41 committed Aug 2, 2024
1 parent f2edc67 commit 18a2966
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 75 deletions.
60 changes: 51 additions & 9 deletions actors/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package actors
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"

"github.com/filecoin-project/lotus/api"
"github.com/zondax/fil-parser/types"

"github.com/filecoin-project/go-address"
"github.com/zondax/fil-parser/actors/cache"
"github.com/zondax/fil-parser/actors/cache/impl/common"
Expand Down Expand Up @@ -46,20 +50,16 @@ func (p *ActorParser) emptyParamsAndReturn() (map[string]interface{}, error) {
return make(map[string]interface{}), nil
}

func ConsolidateRobustAddresses(from, to address.Address, actorCache *cache.ActorsCache, logger *zap.Logger, config *parser.ConsolidateAddressesToRobust) (string, string, error) {
func ConsolidateRobustAddress(address address.Address, actorCache *cache.ActorsCache, logger *zap.Logger, config *parser.ConsolidateAddressesToRobust) (string, error) {
var err error
txFrom := from.String()
txTo := to.String()
addressStr := address.String()
if config != nil && config.Enable {
if txFrom, err = EnsureRobustAddress(from, actorCache, logger); err != nil && !config.BestEffort {
return "", "", err
}
if txTo, err = EnsureRobustAddress(to, actorCache, logger); err != nil && !config.BestEffort {
return "", "", err
if addressStr, err = EnsureRobustAddress(address, actorCache, logger); err != nil && !config.BestEffort {
return "", err
}
}

return txFrom, txTo, nil
return addressStr, nil
}

func EnsureRobustAddress(address address.Address, actorCache *cache.ActorsCache, logger *zap.Logger) (string, error) {
Expand All @@ -74,3 +74,45 @@ func EnsureRobustAddress(address address.Address, actorCache *cache.ActorsCache,
}
return robustAddress, nil
}

func CalculateTransactionFees(gasCost api.MsgGasCost, tipset *types.ExtendedTipSet, blockCid string, actorCache *cache.ActorsCache, logger *zap.Logger, config *parser.FilecoinParserConfig) []byte {
minerAddressStr, err := tipset.GetBlockMiner(blockCid)
if err == nil {
minerAddress, err := address.NewFromString(minerAddressStr)
if err != nil {
logger.Sugar().Errorf("Error when trying to parse miner address: %v", err)
}

minerAddressStr, err = ConsolidateRobustAddress(minerAddress, actorCache, logger, &config.ConsolidateAddressesToRobust)
if err != nil {
logger.Sugar().Errorf("Error when trying to consolidate miner address to robust: %v", err)
}
} else {
logger.Sugar().Errorf("Error when trying to get miner address from block cid '%s': %v", blockCid, err)
}

feeData := parser.FeeData{
FeesMetadata: parser.FeesMetadata{
MinerFee: parser.MinerFee{
MinerAddress: minerAddressStr,
Amount: gasCost.MinerTip.String(),
},
OverEstimationBurnFee: parser.OverEstimationBurnFee{
BurnAddress: parser.BurnAddress,
Amount: gasCost.OverEstimationBurn.String(),
},
BurnFee: parser.BurnFee{
BurnAddress: parser.BurnAddress,
Amount: gasCost.BaseFeeBurn.String(),
},
},
Amount: gasCost.TotalCost.Int.String(),
}

data, err := json.Marshal(feeData)
if err != nil {
logger.Sugar().Errorf("Error when trying to marshal fees data: %v", err)
}

return data
}
4 changes: 1 addition & 3 deletions parser/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package parser

import (
"encoding/json"
"math/big"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
Expand Down Expand Up @@ -91,11 +90,10 @@ type BurnFee struct {

type FeeData struct {
FeesMetadata
Amount *big.Int `json:"amount"`
Amount string `json:"amount"`
}

type FeesMetadata struct {
TxType string
MinerFee MinerFee
OverEstimationBurnFee OverEstimationBurnFee
BurnFee BurnFee
Expand Down
43 changes: 11 additions & 32 deletions parser/v1/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ func (p *Parser) ParseTransactions(ctx context.Context, txsData types.TxsData) (
config = &p.config.ConsolidateAddressesToRobust
}

txFrom, txTo, err := actors.ConsolidateRobustAddresses(trace.Msg.From, trace.Msg.To, p.helper.GetActorsCache(), p.logger, config)
txFrom, err := actors.ConsolidateRobustAddress(trace.Msg.From, p.helper.GetActorsCache(), p.logger, config)
if err != nil {
return nil, nil, err
}
txTo, err := actors.ConsolidateRobustAddress(trace.Msg.To, p.helper.GetActorsCache(), p.logger, config)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -289,7 +293,11 @@ func (p *Parser) parseTrace(trace typesV1.ExecutionTraceV1, mainMsgCid cid.Cid,
config = &p.config.ConsolidateAddressesToRobust
}

txFrom, txTo, err := actors.ConsolidateRobustAddresses(trace.Msg.From, trace.Msg.To, p.helper.GetActorsCache(), p.logger, config)
txFrom, err := actors.ConsolidateRobustAddress(trace.Msg.From, p.helper.GetActorsCache(), p.logger, config)
if err != nil {
return nil, err
}
txTo, err := actors.ConsolidateRobustAddress(trace.Msg.To, p.helper.GetActorsCache(), p.logger, config)
if err != nil {
return nil, err
}
Expand All @@ -315,7 +323,7 @@ func (p *Parser) parseTrace(trace typesV1.ExecutionTraceV1, mainMsgCid cid.Cid,
}

if gasCost.TotalCost.Uint64() > 0 {
transactionFeesJson := p.calculateTransactionFees(gasCost, tipset, blockCid)
transactionFeesJson := actors.CalculateTransactionFees(gasCost, tipset, blockCid, p.helper.GetActorsCache(), p.logger, p.config)
tx.FeeData = string(transactionFeesJson)
}
return tx, nil
Expand All @@ -342,32 +350,3 @@ func (p *Parser) appendAddressInfo(msg *filTypes.Message, key filTypes.TipSetKey
toAdd := p.helper.GetActorAddressInfo(msg.To, key)
parser.AppendToAddressesMap(p.addresses, fromAdd, toAdd)
}

func (p *Parser) calculateTransactionFees(gasCost api.MsgGasCost, tipset *types.ExtendedTipSet, blockCid string) []byte {
minerAddress, err := tipset.GetBlockMiner(blockCid)
if err != nil {
p.logger.Sugar().Errorf("Error when trying to get miner address from block cid '%s': %v", blockCid, err)
}

feeData := parser.FeeData{
FeesMetadata: parser.FeesMetadata{
MinerFee: parser.MinerFee{
MinerAddress: minerAddress,
Amount: gasCost.MinerTip.String(),
},
OverEstimationBurnFee: parser.OverEstimationBurnFee{
BurnAddress: parser.BurnAddress,
Amount: gasCost.OverEstimationBurn.String(),
},
BurnFee: parser.BurnFee{
BurnAddress: parser.BurnAddress,
Amount: gasCost.BaseFeeBurn.String(),
},
},
Amount: gasCost.TotalCost.Int,
}

data, _ := json.Marshal(feeData)

return data
}
37 changes: 6 additions & 31 deletions parser/v2/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,11 @@ func (p *Parser) parseTrace(trace typesV2.ExecutionTraceV2, mainMsgCid cid.Cid,
config = &p.config.ConsolidateAddressesToRobust
}

txFrom, txTo, err := actors.ConsolidateRobustAddresses(trace.Msg.From, trace.Msg.To, p.helper.GetActorsCache(), p.logger, config)
txFrom, err := actors.ConsolidateRobustAddress(trace.Msg.From, p.helper.GetActorsCache(), p.logger, config)
if err != nil {
return nil, err
}
txTo, err := actors.ConsolidateRobustAddress(trace.Msg.To, p.helper.GetActorsCache(), p.logger, config)
if err != nil {
return nil, err
}
Expand All @@ -327,41 +331,12 @@ func (p *Parser) parseTrace(trace typesV2.ExecutionTraceV2, mainMsgCid cid.Cid,
}

if gasCost.TotalCost.Uint64() > 0 {
transactionFeesJson := p.calculateTransactionFees(gasCost, tipset, blockCid)
transactionFeesJson := actors.CalculateTransactionFees(gasCost, tipset, blockCid, p.helper.GetActorsCache(), p.logger, p.config)
tx.FeeData = string(transactionFeesJson)
}
return tx, nil
}

func (p *Parser) calculateTransactionFees(gasCost api.MsgGasCost, tipset *types.ExtendedTipSet, blockCid string) []byte {
minerAddress, err := tipset.GetBlockMiner(blockCid)
if err != nil {
p.logger.Sugar().Errorf("Error when trying to get miner address from block cid '%s': %v", blockCid, err)
}

feeData := parser.FeeData{
FeesMetadata: parser.FeesMetadata{
MinerFee: parser.MinerFee{
MinerAddress: minerAddress,
Amount: gasCost.MinerTip.String(),
},
OverEstimationBurnFee: parser.OverEstimationBurnFee{
BurnAddress: parser.BurnAddress,
Amount: gasCost.OverEstimationBurn.String(),
},
BurnFee: parser.BurnFee{
BurnAddress: parser.BurnAddress,
Amount: gasCost.BaseFeeBurn.String(),
},
},
Amount: gasCost.TotalCost.Int,
}

data, _ := json.Marshal(feeData)

return data
}

func (p *Parser) appendAddressInfo(msg *parser.LotusMessage, key filTypes.TipSetKey) {
if msg == nil {
return
Expand Down

0 comments on commit 18a2966

Please sign in to comment.