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

feat: add new task fevm contract #1214

Merged
merged 7 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions chain/indexer/integrated/processor/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (

// fevm task
fevmblockheadertask "github.com/filecoin-project/lily/tasks/fevm/blockheader"
fevmcontracttask "github.com/filecoin-project/lily/tasks/fevm/contract"
fevmreceipttask "github.com/filecoin-project/lily/tasks/fevm/receipt"
fevmtransactiontask "github.com/filecoin-project/lily/tasks/fevm/transaction"
fevmactorstatstask "github.com/filecoin-project/lily/tasks/fevmactorstats"
Expand Down Expand Up @@ -650,6 +651,8 @@ func MakeProcessors(api tasks.DataSource, indexerTasks []string) (*IndexerProces
out.TipsetsProcessors[t] = fevmreceipttask.NewTask(api)
case tasktype.FEVMTransaction:
out.TipsetsProcessors[t] = fevmtransactiontask.NewTask(api)
case tasktype.FEVMContract:
out.TipsetsProcessors[t] = fevmcontracttask.NewTask(api)

case BuiltinTaskName:
out.ReportProcessors[t] = indexertask.NewTask(api)
Expand Down
2 changes: 1 addition & 1 deletion chain/indexer/integrated/processor/state_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestNewProcessor(t *testing.T) {
require.Equal(t, t.Name(), proc.name)
require.Len(t, proc.actorProcessors, 24)
require.Len(t, proc.tipsetProcessors, 10)
require.Len(t, proc.tipsetsProcessors, 12)
require.Len(t, proc.tipsetsProcessors, 13)
require.Len(t, proc.builtinProcessors, 1)

require.Equal(t, gasoutput.NewTask(nil), proc.tipsetsProcessors[tasktype.GasOutputs])
Expand Down
2 changes: 1 addition & 1 deletion chain/indexer/integrated/processor/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,6 @@ func TestMakeProcessorsAllTasks(t *testing.T) {
require.NoError(t, err)
require.Len(t, proc.ActorProcessors, 24)
require.Len(t, proc.TipsetProcessors, 10)
require.Len(t, proc.TipsetsProcessors, 12)
require.Len(t, proc.TipsetsProcessors, 13)
require.Len(t, proc.ReportProcessors, 1)
}
5 changes: 5 additions & 0 deletions chain/indexer/tasktype/table_tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
FEVMBlockHeader = "fevm_block_header"
FEVMReceipt = "fevm_receipt"
FEVMTransaction = "fevm_transaction"
FEVMContract = "fevm_contract"
)

var AllTableTasks = []string{
Expand Down Expand Up @@ -97,6 +98,7 @@ var AllTableTasks = []string{
FEVMBlockHeader,
FEVMReceipt,
FEVMTransaction,
FEVMContract,
}

var TableLookup = map[string]struct{}{
Expand Down Expand Up @@ -146,6 +148,7 @@ var TableLookup = map[string]struct{}{
FEVMBlockHeader: {},
FEVMReceipt: {},
FEVMTransaction: {},
FEVMContract: {},
}

var TableComment = map[string]string{
Expand Down Expand Up @@ -195,6 +198,7 @@ var TableComment = map[string]string{
FEVMBlockHeader: ``,
FEVMReceipt: ``,
FEVMTransaction: ``,
FEVMContract: ``,
}

var TableFieldComments = map[string]map[string]string{
Expand Down Expand Up @@ -301,4 +305,5 @@ var TableFieldComments = map[string]map[string]string{
FEVMBlockHeader: {},
FEVMReceipt: {},
FEVMTransaction: {},
FEVMContract: {},
}
1 change: 1 addition & 0 deletions chain/indexer/tasktype/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ var TaskLookup = map[string][]string{
FEVMBlockHeader,
FEVMReceipt,
FEVMTransaction,
FEVMContract,
},
}

Expand Down
2 changes: 1 addition & 1 deletion chain/indexer/tasktype/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestMakeAllTaskAliasNames(t *testing.T) {
}

func TestMakeAllTaskNames(t *testing.T) {
const TotalTableTasks = 46
const TotalTableTasks = 47
actual, err := tasktype.MakeTaskNames(tasktype.AllTableTasks)
require.NoError(t, err)
// if this test fails it means a new task name was added, update the above test
Expand Down
46 changes: 46 additions & 0 deletions model/fevm/contract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package fevm

import (
"context"

"go.opencensus.io/tag"

"github.com/filecoin-project/lily/metrics"
"github.com/filecoin-project/lily/model"
)

type FEVMContract struct {
tableName struct{} `pg:"fevm_contract"` // nolint: structcheck
Terryhung marked this conversation as resolved.
Show resolved Hide resolved

// Height message was executed at.
Height int64 `pg:",pk,notnull,use_zero"`

// Actor address.
ActorID string `pg:",notnull"`

// Actor Address in ETH
EthAddress string `pg:",notnull"`

ByteCode string `pg:",notnull"`
ByteCodeHash string `pg:",notnull"`

Balance string `pg:"type:numeric,notnull"`
Nonce uint64 `pg:",use_zero"`
}

func (f *FEVMContract) Persist(ctx context.Context, s model.StorageBatch, version model.Version) error {
ctx, _ = tag.New(ctx, tag.Upsert(metrics.Table, "fevm_contract"))
metrics.RecordCount(ctx, metrics.PersistModel, 1)
return s.PersistModel(ctx, f)
}

type FEVMContractList []*FEVMContract

func (f FEVMContractList) Persist(ctx context.Context, s model.StorageBatch, version model.Version) error {
if len(f) == 0 {
return nil
}
ctx, _ = tag.New(ctx, tag.Upsert(metrics.Table, "fevm_contract"))
metrics.RecordCount(ctx, metrics.PersistModel, len(f))
return s.PersistModel(ctx, f)
}
19 changes: 19 additions & 0 deletions schemas/v1/25_fevm_contract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package v1

func init() {
patches.Register(
25,
`
CREATE TABLE IF NOT EXISTS {{ .SchemaName | default "public"}}.fevm_contract (
height BIGINT NOT NULL,
actor_id TEXT,
eth_address TEXT,
byte_code TEXT,
byte_code_hash TEXT,
balance numeric,
Terryhung marked this conversation as resolved.
Show resolved Hide resolved
nonce BIGINT,
PRIMARY KEY(height, actor_id)
Terryhung marked this conversation as resolved.
Show resolved Hide resolved
);
`,
)
}
1 change: 1 addition & 0 deletions storage/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ var Models = []interface{}{
(*fevm.FEVMBlockHeader)(nil),
(*fevm.FEVMReceipt)(nil),
(*fevm.FEVMTransaction)(nil),
(*fevm.FEVMContract)(nil),
}

var log = logging.Logger("lily/storage")
Expand Down
118 changes: 118 additions & 0 deletions tasks/fevm/contract/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package fevmcontract

import (
"context"
"encoding/hex"
"fmt"

"github.com/filecoin-project/lotus/chain/actors/builtin/evm"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"

logging "github.com/ipfs/go-log/v2"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"

"github.com/filecoin-project/lily/model"
"github.com/filecoin-project/lily/model/fevm"
visormodel "github.com/filecoin-project/lily/model/visor"
"github.com/filecoin-project/lily/tasks"

"github.com/filecoin-project/lily/lens/util"
)

var log = logging.Logger("lily/tasks/fevmcontract")

type Task struct {
node tasks.DataSource
}

func NewTask(node tasks.DataSource) *Task {
return &Task{
node: node,
}
}

func (p *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, executed *types.TipSet) (model.Persistable, *visormodel.ProcessingReport, error) {
ctx, span := otel.Tracer("").Start(ctx, "ProcessTipSets")
if span.IsRecording() {
span.SetAttributes(
attribute.String("current", current.String()),
attribute.Int64("current_height", int64(current.Height())),
attribute.String("executed", executed.String()),
attribute.Int64("executed_height", int64(executed.Height())),
attribute.String("processor", "fevm_contract"),
)
}
defer span.End()

report := &visormodel.ProcessingReport{
Height: int64(current.Height()),
StateRoot: current.ParentState().String(),
}

actorChanges, err := p.node.ActorStateChanges(ctx, current, executed)
if err != nil {
return nil, report, err
}

out := make(fevm.FEVMContractList, 0)
errs := []error{}
for _, change := range actorChanges {
actor := change.Actor
if actor.Address == nil {
continue
}

if !util.IsSentToEVMAddress(ctx, p.node, *actor.Address, executed.Key()) {
Terryhung marked this conversation as resolved.
Show resolved Hide resolved
continue
}

evmState, err := evm.Load(p.node.Store(), &actor)
if err != nil {
log.Errorf("Error at loading evm state: [actor cid: %v] err: %v", actor.Code.String(), err)
errs = append(errs, err)
continue
}

ethAddress, err := ethtypes.EthAddressFromFilecoinAddress(*actor.Address)
if err != nil {
log.Errorf("Error at getting eth address: [actor cid: %v] err: %v", actor.Code.String(), err)
errs = append(errs, err)
continue
}

byteCode, err := evmState.GetBytecode()
if err != nil {
log.Errorf("Error at getting byte code: [actor cid: %v] err: %v", actor.Code.String(), err)
errs = append(errs, err)
continue
}

byteCodeHash, err := evmState.GetBytecodeHash()
if err != nil {
log.Errorf("Error at getting byte code hash: [actor cid: %v] err: %v", actor.Code.String(), err)
errs = append(errs, err)
continue
}

out = append(out, &fevm.FEVMContract{
Height: int64(executed.Height()),
ActorID: actor.Address.String(),
EthAddress: ethAddress.String(),
ByteCode: hex.EncodeToString(byteCode),
ByteCodeHash: hex.EncodeToString(byteCodeHash[:]),
Balance: actor.Balance.String(),
Nonce: actor.Nonce,
})

}

if len(errs) > 0 {
err = fmt.Errorf("%v", errs)
} else {
err = nil
}

return model.PersistableList{out}, report, err
}