Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Height Based Actor Parsing #273

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions actors/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package actors
import (
"bytes"
"encoding/base64"
"github.com/zondax/fil-parser/parser"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/builtin/v11/account"
"github.com/filecoin-project/go-state-types/builtin/v9/account"
"github.com/zondax/fil-parser/parser"

typegen "github.com/whyrusleeping/cbor-gen"
)

Expand Down
55 changes: 55 additions & 0 deletions actors/account/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package account

import (
"bytes"
"encoding/base64"
"fmt"

"github.com/filecoin-project/go-address"
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"
typegen "github.com/whyrusleeping/cbor-gen"
"github.com/zondax/fil-parser/parser"
"github.com/zondax/fil-parser/tools"
)

type Account struct{}

func (a *Account) PubkeyAddress(network string, raw, rawReturn []byte) (map[string]interface{}, error) {
metadata := make(map[string]interface{})
metadata[parser.ParamsKey] = base64.StdEncoding.EncodeToString(raw)
reader := bytes.NewReader(rawReturn)
var r address.Address
err := r.UnmarshalCBOR(reader)
if err != nil {
return metadata, err
}
metadata[parser.ReturnKey] = r.String()
return metadata, nil
}

func (a *Account) AuthenticateMessage(network string, height int64, raw, rawReturn []byte) (map[string]interface{}, error) {
switch {
// all versions before V17
case tools.AnyIsSupported(network, height, tools.VersionsBefore(tools.V16)...):
return map[string]interface{}{}, nil // method did not exist
case tools.V17.IsSupported(network, height):
return authenticateMessageGeneric[*accountv9.AuthenticateMessageParams, *typegen.CborBool](raw, rawReturn, &accountv9.AuthenticateMessageParams{})
case tools.V18.IsSupported(network, height):
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, *typegen.CborBool](raw, rawReturn, &accountv14.AuthenticateMessageParams{})
default:
return nil, fmt.Errorf("not supported")
}
}
87 changes: 87 additions & 0 deletions actors/account/account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package account_test

import (
_ "embed"
"encoding/json"
"fmt"
"os"
"testing"

"github.com/stretchr/testify/require"
"github.com/zondax/fil-parser/actors/account"
"github.com/zondax/fil-parser/tools"
rosettaFilecoinLib "github.com/zondax/rosetta-filecoin-lib"

// typesV1 "github.com/zondax/fil-parser/parser/v1/types"
typesV2 "github.com/zondax/fil-parser/parser/v2/types"
)

//go:embed expected.json
var expectedData []byte
var expected map[string]any
var lib *rosettaFilecoinLib.RosettaConstructionFilecoin

var network string

func TestMain(m *testing.M) {
var err error
network = "mainnet"
lib, err = tools.GetLib(tools.NodeUrl)
if err != nil {
panic(err)
}

err = json.Unmarshal(expectedData, &expected)
if err != nil {
panic(err)
}
os.Exit(m.Run())
}

func TestAuthenticateMessage(t *testing.T) {

tests, err := tools.LoadTestData[map[string]any](network, "AuthenticateMessage", expected)
require.NoError(t, err)
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
computeState, err := tools.ComputeState[typesV2.ComputeStateOutputV2](tt.Height, tt.Version)
require.NoError(t, err)

for _, trace := range computeState.Trace {
if trace.Msg == nil {
continue
}
account := &account.Account{}
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))
}
})
}
}

func TestPubkeyAddress(t *testing.T) {
tests, err := tools.LoadTestData[map[string]any](network, "PubkeyAddress", expected)
require.NoError(t, err)

for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
computeState, err := tools.ComputeState[typesV2.ComputeStateOutputV2](tt.Height, tt.Version)
require.NoError(t, err)

for _, trace := range computeState.Trace {
if trace.Msg == nil {
continue
}
account := &account.Account{}
result, err := account.PubkeyAddress(tt.Network, trace.Msg.Params, trace.MsgRct.Return)
require.NoError(t, err)
require.True(t, tools.CompareResult(result, tt.Expected))
}

})
}
}
98 changes: 98 additions & 0 deletions actors/account/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"V1": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V2": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V3": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V4": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V5": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V6": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V7": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V8": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V9": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V10": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V11": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V12": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V13": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V14": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V15": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V16": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V17": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V18": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V19": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V20": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V21": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V22": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V23": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
},
"V24": {
"PubkeyAddress": {},
"AuthenticateMessage": {}
}
}
25 changes: 25 additions & 0 deletions actors/account/generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package account

import (
"bytes"

"github.com/zondax/fil-parser/parser"
)

func authenticateMessageGeneric[P authenticateMessageParams, R authenticateMessageReturn](raw, rawReturn []byte, params P) (map[string]interface{}, error) {
metadata := make(map[string]interface{})
reader := bytes.NewReader(raw)
err := params.UnmarshalCBOR(reader)
if err != nil {
return metadata, err
}
metadata[parser.ParamsKey] = params
reader = bytes.NewReader(rawReturn)
var r R
err = r.UnmarshalCBOR(reader)
if err != nil {
return metadata, err
}
metadata[parser.ReturnKey] = r
return metadata, nil
}
13 changes: 13 additions & 0 deletions actors/account/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package account

import (
"io"
)

type authenticateMessageParams interface {
UnmarshalCBOR(r io.Reader) error
}

type authenticateMessageReturn interface {
UnmarshalCBOR(r io.Reader) error
}
34 changes: 34 additions & 0 deletions actors/cron/cron.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cron

import (
cronv2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/cron"
cronv3 "github.com/filecoin-project/specs-actors/v3/actors/builtin/cron"
cronv4 "github.com/filecoin-project/specs-actors/v4/actors/builtin/cron"
cronv5 "github.com/filecoin-project/specs-actors/v5/actors/builtin/cron"
cronv6 "github.com/filecoin-project/specs-actors/v6/actors/builtin/cron"
cronv7 "github.com/filecoin-project/specs-actors/v7/actors/builtin/cron"
cronv8 "github.com/filecoin-project/specs-actors/v8/actors/builtin/cron"
"github.com/zondax/fil-parser/tools"
)

type Cron struct{}

func (c *Cron) CronConstructor(network string, height int64, raw []byte) (map[string]interface{}, error) {
switch {
case tools.V16.IsSupported(network, height):
return cronConstructorGeneric[*cronv8.ConstructorParams](raw, &cronv8.ConstructorParams{})
case tools.V15.IsSupported(network, height):
return cronConstructorGeneric[*cronv7.ConstructorParams](raw, &cronv7.ConstructorParams{})
case tools.V14.IsSupported(network, height):
return cronConstructorGeneric[*cronv6.ConstructorParams](raw, &cronv6.ConstructorParams{})
case tools.V13.IsSupported(network, height):
return cronConstructorGeneric[*cronv5.ConstructorParams](raw, &cronv5.ConstructorParams{})
case tools.V12.IsSupported(network, height):
return cronConstructorGeneric[*cronv4.ConstructorParams](raw, &cronv4.ConstructorParams{})
case tools.V10.IsSupported(network, height) || tools.V11.IsSupported(network, height):
return cronConstructorGeneric[*cronv3.ConstructorParams](raw, &cronv3.ConstructorParams{})
case tools.V9.IsSupported(network, height):
return cronConstructorGeneric[*cronv2.ConstructorParams](raw, &cronv2.ConstructorParams{})
}
return nil, nil
}
60 changes: 60 additions & 0 deletions actors/cron/cron_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cron_test

import (
_ "embed"
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
"github.com/zondax/fil-parser/actors/cron"
typesV2 "github.com/zondax/fil-parser/parser/v2/types"
"github.com/zondax/fil-parser/tools"
)

//go:embed expected.json
var expectedData []byte
var expected map[string]any
var network string

func TestMain(m *testing.M) {
network = "mainnet"
if err := json.Unmarshal(expectedData, &expected); err != nil {
panic(err)
}
m.Run()
}

type testFn func(network string, height int64, raw []byte) (map[string]interface{}, error)

func TestCron(t *testing.T) {
cron := &cron.Cron{}
testFns := map[string]testFn{
"CronConstructor": cron.CronConstructor,
}
for name, fn := range testFns {
t.Run(name, func(t *testing.T) {
tests, err := tools.LoadTestData[map[string]any](network, name, expected)
require.NoError(t, err)
runTest(t, fn, tests)
})
}
}

func runTest(t *testing.T, fn testFn, tests []tools.TestCase[map[string]any]) {
for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
computeState, err := tools.ComputeState[typesV2.ComputeStateOutputV2](tt.Height, tt.Version)
require.NoError(t, err)

for _, trace := range computeState.Trace {
if trace.Msg == nil {
continue
}

result, err := fn(tt.Network, tt.Height, trace.Msg.Params)
require.NoError(t, err)
require.True(t, tools.CompareResult(result, tt.Expected))
}
})
}
}
5 changes: 5 additions & 0 deletions actors/cron/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"V1": {
"CronConstructor": {}
}
}
Loading
Loading