Skip to content

Commit

Permalink
fix: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ziscky committed Jan 22, 2025
1 parent 46b3650 commit a2eb7aa
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 73 deletions.
27 changes: 18 additions & 9 deletions actors/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
accountv10 "github.com/filecoin-project/go-state-types/builtin/v10/account"
accountv11 "github.com/filecoin-project/go-state-types/builtin/v11/account"
accountv9 "github.com/filecoin-project/go-state-types/builtin/v11/account"
accountv12 "github.com/filecoin-project/go-state-types/builtin/v12/account"
accountv13 "github.com/filecoin-project/go-state-types/builtin/v13/account"
accountv14 "github.com/filecoin-project/go-state-types/builtin/v14/account"
accountv15 "github.com/filecoin-project/go-state-types/builtin/v15/account"
typegen "github.com/whyrusleeping/cbor-gen"
"github.com/zondax/fil-parser/parser"
"github.com/zondax/fil-parser/tools"
)
Expand All @@ -29,18 +31,25 @@ func PubkeyAddress(network string, raw, rawReturn []byte) (map[string]interface{
}

func AuthenticateMessage(network string, height int64, raw, rawReturn []byte) (map[string]interface{}, error) {
versions := tools.GetSupportedVersions(network)

switch {
case tools.V8.IsSupported(network, height):
return nil, fmt.Errorf("not supported")
// all versions before V17
case tools.AnyIsSupported(network, height, versions[:len(versions)-7]...):
return map[string]interface{}{}, nil // method did not exist
case tools.V17.IsSupported(network, height):
return authenticateMessageGeneric[*accountv9.AuthenticateMessageParams, *accountv9.AuthenticateMessageParams](raw, rawReturn, &accountv9.AuthenticateMessageParams{})
return authenticateMessageGeneric[*accountv9.AuthenticateMessageParams, *typegen.CborBool](raw, rawReturn, &accountv9.AuthenticateMessageParams{})
case tools.V18.IsSupported(network, height):
return authenticateMessageGeneric[*accountv10.AuthenticateMessageParams, *accountv10.AuthenticateMessageParams](raw, rawReturn, &accountv10.AuthenticateMessageParams{})
case tools.V19.IsSupported(network, height):
return authenticateMessageGeneric[*accountv11.AuthenticateMessageParams, *accountv11.AuthenticateMessageParams](raw, rawReturn, &accountv11.AuthenticateMessageParams{})
return authenticateMessageGeneric[*accountv10.AuthenticateMessageParams, *typegen.CborBool](raw, rawReturn, &accountv10.AuthenticateMessageParams{})
case tools.V19.IsSupported(network, height) || tools.V20.IsSupported(network, height):
return authenticateMessageGeneric[*accountv11.AuthenticateMessageParams, *typegen.CborBool](raw, rawReturn, &accountv11.AuthenticateMessageParams{})
case tools.V21.IsSupported(network, height):
return authenticateMessageGeneric[*accountv12.AuthenticateMessageParams, *typegen.CborBool](raw, rawReturn, &accountv12.AuthenticateMessageParams{})
case tools.V22.IsSupported(network, height):
return authenticateMessageGeneric[*accountv13.AuthenticateMessageParams, *typegen.CborBool](raw, rawReturn, &accountv13.AuthenticateMessageParams{})
case tools.V23.IsSupported(network, height):
return authenticateMessageGeneric[*accountv14.AuthenticateMessageParams, *accountv14.AuthenticateMessageParams](raw, rawReturn, &accountv14.AuthenticateMessageParams{})
return authenticateMessageGeneric[*accountv14.AuthenticateMessageParams, *typegen.CborBool](raw, rawReturn, &accountv14.AuthenticateMessageParams{})
default:
return authenticateMessageGeneric[*accountv15.AuthenticateMessageParams, *accountv15.AuthenticateMessageParams](raw, rawReturn, &accountv15.AuthenticateMessageParams{})
return nil, fmt.Errorf("not supported")
}
}
4 changes: 4 additions & 0 deletions actors/account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package account_test
import (
_ "embed"
"encoding/json"
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -51,6 +52,9 @@ func TestAuthenticateMessage(t *testing.T) {
}
result, err := account.AuthenticateMessage(tt.Network, tt.Height, trace.Msg.Params, trace.MsgRct.Return)
require.NoError(t, err)
data, err := json.Marshal(result)
require.NoError(t, err)
fmt.Println(string(data))
require.True(t, tools.CompareResult(result, tt.Expected))
}
})
Expand Down
88 changes: 47 additions & 41 deletions cmd/tracedl/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func GetStartCommand(c *cli.CLI) *cobra.Command {
cmd.Flags().String("type", "traces", "--type trace")
cmd.Flags().String("outPath", ".", "--outPath ../")
cmd.Flags().String("compress", "gz", "--compress s2")
cmd.Flags().Uint64("height", 0, "--height 387926")

cmd.Flags().UintSlice("heights", []uint{0}, "--heights 387926,387927,387928")
return cmd
}

Expand All @@ -55,57 +56,62 @@ func get(c *cli.CLI, cmd *cobra.Command, _ []string) {
zap.S().Errorf("Error loading compress: %s", err)
return
}
height, err := cmd.Flags().GetUint64("height")
if err != nil {
zap.S().Errorf("Error loading height: %s", err)
return
}

rpcClient, err := newFilecoinRPCClient(config.NodeURL, config.NodeToken)
if err != nil {
zap.S().Error(err)
return
}

var data any
switch logType {
case "traces":
data, err = getTraceFileByHeight(height, rpcClient.client)
case "tipset":
data, err = getTipsetFileByHeight(height, lotusChainTypes.EmptyTSK, rpcClient.client)
case "ethlog":
data, err = getEthLogsByHeight(height, rpcClient.client)
case "nativelog":
data, err = getNativeLogsByHeight(height, rpcClient.client)
case "metadata":
data, err = getMetadata(rpcClient)
}

heights, err := cmd.Flags().GetUintSlice("heights")
if err != nil {
zap.S().Error(err)
zap.S().Errorf("Error loading heights: %s", err)
return
}

dataJson, err := sonic.Marshal(data)
rpcClient, err := newFilecoinRPCClient(config.NodeURL, config.NodeToken)
if err != nil {
zap.S().Error(err)
return
}
if len(heights) > 0 {
for _, tmp := range heights {
height := uint64(tmp)
var data any
switch logType {
case "traces":
data, err = getTraceFileByHeight(height, rpcClient.client)
case "tipset":
data, err = getTipsetFileByHeight(height, lotusChainTypes.EmptyTSK, rpcClient.client)
case "ethlog":
data, err = getEthLogsByHeight(height, rpcClient.client)
case "nativelog":
data, err = getNativeLogsByHeight(height, rpcClient.client)
case "metadata":
data, err = getMetadata(rpcClient)
}

if err != nil {
zap.S().Error(err)
return
}

dataJson, err := sonic.Marshal(data)
if err != nil {
zap.S().Error(err)
return
}
out := dataJson
fname := fmt.Sprintf("%s_%d.json", logType, height)
if format != "" {
out, err = compress(format, dataJson)
if err != nil {
zap.S().Error(err)
return
}
fname = fmt.Sprintf("%s_%d.json.%s", logType, height, format)
}

if err := writeToFile(outPath, fname, out); err != nil {
zap.S().Error(err)
return
}

out := dataJson
fname := fmt.Sprintf("%s_%d.json", logType, height)
if format != "" {
out, err = compress(format, dataJson)
if err != nil {
zap.S().Error(err)
return
}
fname = fmt.Sprintf("%s_%d.json.%s", logType, height, format)
}

if err := writeToFile(outPath, fname, out); err != nil {
zap.S().Error(err)
return
}

}
Expand Down
42 changes: 22 additions & 20 deletions tools/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"reflect"
"strconv"

"github.com/bytedance/sonic"
"github.com/filecoin-project/lotus/api"
Expand All @@ -18,7 +19,7 @@ import (
)

const (
dataPath = "data/heights"
dataPath = "../../data/heights"
fileDataExtension = "json.gz"
tracesPrefix = "traces"
tipsetPrefix = "tipset"
Expand Down Expand Up @@ -124,28 +125,13 @@ func ReadTraces(height int64) ([]byte, error) {
}

func ComputeState[T any](height int64, version string) (*T, error) {
tipset, err := ReadTipset(height)
if err != nil {
return nil, err
}
ethlogs, err := ReadEthLogs(height)
if err != nil {
return nil, err
}
traces, err := ReadTraces(height)
if err != nil {
return nil, err
}

txsData := types.TxsData{
EthLogs: ethlogs,
Tipset: tipset,
Traces: traces,
Metadata: types.BlockMetadata{NodeInfo: types.NodeInfo{NodeMajorMinorVersion: version}},
}

var computeState T
err = sonic.UnmarshalString(string(txsData.Traces), &computeState)
err = sonic.UnmarshalString(string(traces), &computeState)
if err != nil {
return nil, err
}
Expand All @@ -170,7 +156,8 @@ type TestCase[T any] struct {

func LoadTestData[T any](network string, fnName string, expected map[string]any) ([]TestCase[T], error) {
var tests []TestCase[T]
for _, version := range GetSupportedVersions(network) {
versions := GetSupportedVersions(network)
for _, version := range versions {

versionData := expected[version.String()]
if versionData == nil {
Expand All @@ -186,11 +173,26 @@ func LoadTestData[T any](network string, fnName string, expected map[string]any)
return nil, fmt.Errorf("function %s not found in version %s", fnName, version.String())
}

height := version.Height()
if version != version.next() {
height = (version.Height() + version.next().Height()) / 2
}
if version.Height() == 0 {
height = V8.Height()
} else {
height += 100
}

if !version.IsSupported(network, height) {
fmt.Println("skipping", version.String(), height)
continue
}

tests = append(tests, TestCase[T]{
Name: fnName,
Name: fnName + "_" + version.String() + "_" + strconv.FormatInt(height, 10),
Version: version.String(),
Url: NodeUrl,
Height: version.Height(),
Height: height,
Expected: fnData.(T),
})
}
Expand Down
24 changes: 24 additions & 0 deletions tools/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

filTypes "github.com/filecoin-project/lotus/chain/types"
"github.com/stretchr/testify/require"
typesV2 "github.com/zondax/fil-parser/parser/v2/types"
)

func TestBuildTipSetKeyHash(t *testing.T) {
Expand Down Expand Up @@ -65,6 +66,29 @@ func TestIsSupported(t *testing.T) {
}
}

func TestDownloadTraces(t *testing.T) {
versions := GetSupportedVersions("mainnet")

for _, version := range versions {
height := version.Height()
if version != version.next() {
height = (version.Height() + version.next().Height()) / 2
}
if version.Height() == 0 {
height = V8.Height()
} else {
height += 100
}

traces, err := ReadTraces(height)
require.NoError(t, err)
require.NotEmpty(t, traces)
_, err = ComputeState[typesV2.ComputeStateOutputV2](height, version.String())
require.NoError(t, err)
require.NotEmpty(t, traces)
}
}

/*
func TestBuildCidFromMessageTrace(t *testing.T) {
h1, err := multihash.Sum([]byte("TEST"), multihash.SHA2_256, -1)
Expand Down
15 changes: 12 additions & 3 deletions tools/version_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type version struct {
}

var (
supportedVersions = []version{V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22}
supportedVersions = []version{V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20, V21, V22, V23, V24}
V1 version = version{calibration: 0, mainnet: 0, nodeVersion: 1}
V2 version = version{calibration: 0, mainnet: 0, nodeVersion: 2}
V3 version = version{calibration: 0, mainnet: 0, nodeVersion: 3}
Expand Down Expand Up @@ -61,9 +61,18 @@ func (v version) IsSupported(network string, height int64) bool {
return false
}

func AnyIsSupported(network string, height int64, versions ...version) bool {
for _, v := range versions {
if v.IsSupported(network, height) {
return true
}
}
return false
}

func (v version) next() version {
for i, version := range supportedVersions {
if version == v {
if version.nodeVersion == v.nodeVersion {
if i == len(supportedVersions)-1 {
return v
}
Expand All @@ -85,7 +94,7 @@ func (v version) Height() int64 {
}

func GetSupportedVersions(network string) []version {
result := make([]version, len(supportedVersions))
var result []version
for _, v := range supportedVersions {
v.currentNetwork = network
result = append(result, v)
Expand Down

0 comments on commit a2eb7aa

Please sign in to comment.