Skip to content

Commit

Permalink
refactor code to make it more easier to understand
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelm41 committed Nov 23, 2023
1 parent a8d7e03 commit 36bc343
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 49 deletions.
41 changes: 20 additions & 21 deletions factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fil_parser

import (
"errors"
"fmt"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/big"
types2 "github.com/filecoin-project/lotus/chain/types"
Expand Down Expand Up @@ -33,8 +34,6 @@ type FilecoinParser struct {
}

type Parser interface {
VersionStable() string
VersionNext() string
Version() string
ParseTransactions(traces []byte, tipSet *types.ExtendedTipSet, ethLogs []types.EthLog) ([]*types.Transaction, *types.AddressInfoMap, error)
GetBaseFee(traces []byte) (uint64, error)
Expand Down Expand Up @@ -62,23 +61,22 @@ func NewFilecoinParser(lib *rosettaFilecoinLib.RosettaConstructionFilecoin, cach
}

func (p *FilecoinParser) ParseTransactions(traces []byte, tipSet *types.ExtendedTipSet, ethLogs []types.EthLog, metadata *types.BlockMetadata) ([]*types.Transaction, *types.AddressInfoMap, error) {
version := p.detectTraceVersion(*metadata)
if version == "" {
parserVersion, err := p.translateParserVersionFromMetadata(*metadata)
if err != nil {
return nil, nil, errUnknownVersion
}

var txs []*types.Transaction
var addrs *types.AddressInfoMap
var err error

p.logger.Sugar().Debugf("node version found on trace files %s to parse transactions", version)
switch {
case p.parserV21.IsVersionCompatible(version):
p.logger.Sugar().Debugf("node version found on trace files %s to parse transactions", parserVersion)
switch parserVersion {
case parser.ParserV1:
txs, addrs, err = p.parserV21.ParseTransactions(traces, tipSet, ethLogs)
case p.parserV23.IsVersionCompatible(version):
case parser.ParserV2:
txs, addrs, err = p.parserV23.ParseTransactions(traces, tipSet, ethLogs)
default:
p.logger.Sugar().Errorf("[parser] implementation not supported: %s", version)
p.logger.Sugar().Errorf("[parser] implementation not supported: %s", parserVersion)
return nil, nil, errUnknownImpl
}

Expand All @@ -89,15 +87,16 @@ func (p *FilecoinParser) ParseTransactions(traces []byte, tipSet *types.Extended
return p.FilterDuplicated(txs), addrs, nil
}

func (p *FilecoinParser) detectTraceVersion(metadata types.BlockMetadata) string {
func (p *FilecoinParser) translateParserVersionFromMetadata(metadata types.BlockMetadata) (string, error) {
switch {
case p.parserV21.IsVersionCompatible(metadata.NodeMajorMinorVersion), metadata.NodeMajorMinorVersion == "": // The empty string is for backwards compatibility with older traces versions
return V21.VersionNext
// The empty string is for backwards compatibility with older traces versions
case p.parserV21.IsVersionCompatible(metadata.NodeMajorMinorVersion), metadata.NodeMajorMinorVersion == "":
return parser.ParserV1, nil
case p.parserV23.IsVersionCompatible(metadata.NodeMajorMinorVersion):
return V23.VersionNext
return parser.ParserV2, nil
default:
p.logger.Sugar().Errorf("[parser] unsupported node version: %s", metadata.NodeFullVersion)
return ""
return "", fmt.Errorf("node version not supported %s", metadata.NodeFullVersion)
}
}

Expand All @@ -116,16 +115,16 @@ func (p *FilecoinParser) FilterDuplicated(txs []*types.Transaction) []*types.Tra
}

func (p *FilecoinParser) GetBaseFee(traces []byte, metadata types.BlockMetadata) (uint64, error) {
version := p.detectTraceVersion(metadata)
if version == "" {
parserVersion, err := p.translateParserVersionFromMetadata(metadata)
if err != nil {
return 0, errUnknownVersion
}

p.logger.Sugar().Debugf("node version found on trace files %s to get base fee", version)
switch {
case p.parserV21.IsVersionCompatible(version):
p.logger.Sugar().Debugf("node version found on trace files %s to get base fee", parserVersion)
switch parserVersion {
case parser.ParserV1:
return p.parserV21.GetBaseFee(traces)
case p.parserV23.IsVersionCompatible(version):
case parser.ParserV2:
return p.parserV23.GetBaseFee(traces)
}

Expand Down
23 changes: 9 additions & 14 deletions parser/V21/parserImpl_V21.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import (
"strings"
)

const (
VersionNext = "v1.21"
VersionStable = "v1.22"
)
var NodeVersionsSupported = []string{"v1.21", "v1.22"}

type Parser struct {
actorParser *actors.ActorParser
Expand All @@ -40,20 +37,18 @@ func NewParserV21(helper *helper.Helper, logger *zap.Logger) *Parser {
}
}

func (p *Parser) VersionStable() string {
return VersionStable
}

func (p *Parser) VersionNext() string {
return VersionNext
}

func (p *Parser) Version() string {
return VersionNext + "/" + VersionStable
return strings.Join(NodeVersionsSupported, "/")
}

func (p *Parser) IsVersionCompatible(ver string) bool {
return strings.EqualFold(VersionStable, ver) || strings.EqualFold(VersionNext, ver)
for _, i := range NodeVersionsSupported {
if strings.EqualFold(i, ver) {
return true
}
}

return false
}

func (p *Parser) ParseTransactions(traces []byte, tipset *types.ExtendedTipSet, ethLogs []types.EthLog) ([]*types.Transaction, *types.AddressInfoMap, error) {
Expand Down
23 changes: 9 additions & 14 deletions parser/V23/parserImpl_V23.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import (
"strings"
)

const (
VersionNext = "v1.23"
VersionStable = "v1.24"
)
var NodeVersionsSupported = []string{"v1.23", "v1.24"}

type Parser struct {
actorParser *actors.ActorParser
Expand All @@ -40,20 +37,18 @@ func NewParserV23(helper *helper.Helper, logger *zap.Logger) *Parser {
}
}

func (p *Parser) VersionStable() string {
return VersionStable
}

func (p *Parser) VersionNext() string {
return VersionNext
}

func (p *Parser) Version() string {
return VersionNext + "/" + VersionStable
return strings.Join(NodeVersionsSupported, "/")
}

func (p *Parser) IsVersionCompatible(ver string) bool {
return strings.EqualFold(VersionStable, ver) || strings.EqualFold(VersionNext, ver)
for _, i := range NodeVersionsSupported {
if strings.EqualFold(i, ver) {
return true
}
}

return false
}

func (p *Parser) ParseTransactions(traces []byte, tipset *types.ExtendedTipSet, ethLogs []types.EthLog) ([]*types.Transaction, *types.AddressInfoMap, error) {
Expand Down
3 changes: 3 additions & 0 deletions parser/constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package parser

const (
ParserV1 = "v1"
ParserV2 = "v2"

// Fees

TotalFeeOp = "Fee"
Expand Down

0 comments on commit 36bc343

Please sign in to comment.