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

Create FEVM actor stats task #1173

Merged
merged 8 commits into from
Apr 3, 2023
Merged
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
9 changes: 9 additions & 0 deletions chain/indexer/integrated/processor/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ import (
receipttask "github.com/filecoin-project/lily/tasks/messages/receipt"
msapprovaltask "github.com/filecoin-project/lily/tasks/msapprovals"

// fevm task
fevmactorstatstask "github.com/filecoin-project/lily/tasks/fevmactorstats"

"github.com/filecoin-project/lily/chain/indexer/tasktype"
"github.com/filecoin-project/lily/metrics"
"github.com/filecoin-project/lily/model"
Expand Down Expand Up @@ -628,6 +631,12 @@ func MakeProcessors(api tasks.DataSource, indexerTasks []string) (*IndexerProces
case tasktype.ChainConsensus:
out.TipsetProcessors[t] = consensustask.NewTask(api)

//
// FEVM
//
case tasktype.FEVMActorStats:
out.TipsetProcessors[t] = fevmactorstatstask.NewTask(api)

case BuiltinTaskName:
out.ReportProcessors[t] = indexertask.NewTask(api)
default:
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 @@ -53,7 +53,7 @@ func TestNewProcessor(t *testing.T) {
require.NoError(t, err)
require.Equal(t, t.Name(), proc.name)
require.Len(t, proc.actorProcessors, 24)
require.Len(t, proc.tipsetProcessors, 9)
require.Len(t, proc.tipsetProcessors, 10)
require.Len(t, proc.tipsetsProcessors, 9)
require.Len(t, proc.builtinProcessors, 1)

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 @@ -402,7 +402,7 @@ func TestMakeProcessorsAllTasks(t *testing.T) {
proc, err := processor.MakeProcessors(nil, append(tasktype.AllTableTasks, processor.BuiltinTaskName))
require.NoError(t, err)
require.Len(t, proc.ActorProcessors, 24)
require.Len(t, proc.TipsetProcessors, 9)
require.Len(t, proc.TipsetProcessors, 10)
require.Len(t, proc.TipsetsProcessors, 9)
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 @@ -44,6 +44,7 @@ const (
VerifiedRegistryVerifier = "verified_registry_verifier"
VerifiedRegistryVerifiedClient = "verified_registry_verified_client"
VerifiedRegistryClaim = "verified_registry_claim"
FEVMActorStats = "fevm_actor_stats"
)

var AllTableTasks = []string{
Expand Down Expand Up @@ -89,6 +90,7 @@ var AllTableTasks = []string{
VerifiedRegistryVerifier,
VerifiedRegistryVerifiedClient,
VerifiedRegistryClaim,
FEVMActorStats,
}

var TableLookup = map[string]struct{}{
Expand Down Expand Up @@ -134,6 +136,7 @@ var TableLookup = map[string]struct{}{
VerifiedRegistryVerifier: {},
VerifiedRegistryVerifiedClient: {},
VerifiedRegistryClaim: {},
FEVMActorStats: {},
}

var TableComment = map[string]string{
Expand Down Expand Up @@ -179,6 +182,7 @@ var TableComment = map[string]string{
VerifiedRegistryVerifier: ``,
VerifiedRegistryVerifiedClient: ``,
VerifiedRegistryClaim: ``,
FEVMActorStats: ``,
}

var TableFieldComments = map[string]map[string]string{
Expand Down Expand Up @@ -281,4 +285,5 @@ var TableFieldComments = map[string]map[string]string{
VerifiedRegistryVerifier: {},
VerifiedRegistryVerifiedClient: {},
VerifiedRegistryClaim: {},
FEVMActorStats: {},
}
4 changes: 4 additions & 0 deletions chain/indexer/tasktype/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
MultisigApprovalsTask = "msapprovals" // task that extracts multisig actor approvals
ImplicitMessageTask = "implicitmessage" // task that extract implicitly executed messages: cron tick and block reward.
ChainConsensusTask = "consensus"
FEVMTask = "fevm"
)

var TaskLookup = map[string][]string{
Expand Down Expand Up @@ -90,6 +91,9 @@ var TaskLookup = map[string][]string{
ChainConsensusTask: {
ChainConsensus,
},
FEVMTask: {
FEVMActorStats,
},
}

func MakeTaskNames(tasks []string) ([]string, error) {
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 = 42
const TotalTableTasks = 43
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
56 changes: 56 additions & 0 deletions model/fevm/fevmactorstats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package fevm

import (
"context"

"go.opencensus.io/tag"

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

type FEVMActorStats struct {
tableName struct{} `pg:"fevm_actor_stats"` // nolint: structcheck

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

// Balance of EVM actor in attoFIL.
ContractBalance string `pg:",notnull"`
// Balance of Eth account actor in attoFIL.
EthAccountBalance string `pg:",notnull"`
// Balance of Placeholder Actor in attoFIL.
PlaceholderBalance string `pg:",notnull"`

// number of contracts
ContractCount uint64 `pg:",use_zero"`
// number of unique contracts
UniqueContractCount uint64 `pg:",use_zero"`
// number of Eth account actors
EthAccountCount uint64 `pg:",use_zero"`
// number of placeholder actors
PlaceholderCount uint64 `pg:",use_zero"`
}

func (f *FEVMActorStats) Persist(ctx context.Context, s model.StorageBatch, version model.Version) error {
ctx, _ = tag.New(ctx, tag.Upsert(metrics.Table, "fevm_actor_stats"))
stop := metrics.Timer(ctx, metrics.PersistDuration)
defer stop()

metrics.RecordCount(ctx, metrics.PersistModel, 1)
return s.PersistModel(ctx, f)
}

type FEVMActorStatsList []*FEVMActorStats

func (f FEVMActorStatsList) 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_actor_stats"))
stop := metrics.Timer(ctx, metrics.PersistDuration)
defer stop()

metrics.RecordCount(ctx, metrics.PersistModel, len(f))
return s.PersistModel(ctx, f)
}
20 changes: 20 additions & 0 deletions schemas/v1/19_fevm_actor_stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package v1

func init() {
patches.Register(
19,
`
CREATE TABLE IF NOT EXISTS {{ .SchemaName | default "public"}}.fevm_actor_stats (
height BIGINT NOT NULL,
contract_balance TEXT NOT NULL,
eth_account_balance TEXT NOT NULL,
placeholder_balance TEXT NOT NULL,
contract_count BIGINT NOT NULL,
unique_contract_count BIGINT NOT NULL,
eth_account_count BIGINT NOT NULL,
placeholder_count BIGINT NOT NULL,
PRIMARY KEY(height)
);
`,
)
}
3 changes: 3 additions & 0 deletions storage/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/filecoin-project/lily/model/blocks"
"github.com/filecoin-project/lily/model/chain"
"github.com/filecoin-project/lily/model/derived"
"github.com/filecoin-project/lily/model/fevm"
"github.com/filecoin-project/lily/model/messages"
"github.com/filecoin-project/lily/model/msapprovals"
"github.com/filecoin-project/lily/model/visor"
Expand Down Expand Up @@ -91,6 +92,8 @@ var Models = []interface{}{
(*verifreg.VerifiedRegistryVerifier)(nil),
(*verifreg.VerifiedRegistryVerifiedClient)(nil),
(*verifreg.VerifiedRegistryClaim)(nil),

(*fevm.FEVMActorStats)(nil),
}

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

import (
"context"

"github.com/filecoin-project/go-state-types/abi"

"github.com/filecoin-project/lily/model/fevm"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/chain/actors/builtin"
evm2 "github.com/filecoin-project/lotus/chain/actors/builtin/evm"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/types"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"

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

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

type Task struct {
node tasks.DataSource
}

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

func (p *Task) ProcessTipSet(ctx context.Context, ts *types.TipSet) (model.Persistable, *visormodel.ProcessingReport, error) {
_, span := otel.Tracer("").Start(ctx, "ProcessTipSet")
if span.IsRecording() {
span.SetAttributes(
attribute.String("tipset", ts.Key().String()),
attribute.Int64("height", int64(ts.Height())),
attribute.String("processor", "fevmactorstats"),
)
}
report := &visormodel.ProcessingReport{
Height: int64(ts.Height()),
StateRoot: ts.ParentState().String(),
}

st, err := state.LoadStateTree(p.node.Store(), ts.ParentState())
if err != nil {
return nil, report, err
}

log.Infow("iterating over all actors")
count := 0
evmBalance := abi.NewTokenAmount(0)
ethAccountBalance := abi.NewTokenAmount(0)
placeholderBalance := abi.NewTokenAmount(0)
evmCount := 0
ethAccountCount := 0
placeholderCount := 0
bytecodeCIDs := []cid.Cid{}

_ = st.ForEach(func(addr address.Address, act *types.Actor) error {
count++

if builtin.IsEvmActor(act.Code) {
evmBalance = types.BigAdd(evmBalance, act.Balance)
evmCount++
es, err := evm2.Load(p.node.Store(), act)
if err != nil {
log.Errorw("fail to load evm actorcount: ", "error", err)
return err
}
bcid, err := es.GetBytecodeCID()
if err != nil {
log.Errorw("fail to get evm bytecode: ", "error", err)
return err
}
bytecodeCIDs = append(bytecodeCIDs, bcid)
}

if builtin.IsEthAccountActor(act.Code) {
ethAccountBalance = types.BigAdd(ethAccountBalance, act.Balance)
ethAccountCount++
}

if builtin.IsPlaceholderActor(act.Code) {
placeholderBalance = types.BigAdd(placeholderBalance, act.Balance)
placeholderCount++
}

return nil
})

uniqueBytecode := unique(bytecodeCIDs)

return &fevm.FEVMActorStats{
Height: int64(ts.Height()),
ContractBalance: evmBalance.String(),
EthAccountBalance: ethAccountBalance.String(),
PlaceholderBalance: placeholderBalance.String(),
ContractCount: uint64(evmCount),
UniqueContractCount: uint64(len(uniqueBytecode)),
EthAccountCount: uint64(ethAccountCount),
PlaceholderCount: uint64(placeholderCount),
}, report, nil
}
func unique(intSlice []cid.Cid) []cid.Cid {
keys := make(map[cid.Cid]bool)
list := []cid.Cid{}
for _, entry := range intSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}