Skip to content

Commit

Permalink
feat: add more fields in fevm transaction (#1255)
Browse files Browse the repository at this point in the history
* Add new fields for fevm transaction

* Get the Actor from DataSource API
  • Loading branch information
Terryhung authored Aug 29, 2023
1 parent ba88e74 commit 01a3ec2
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 1 deletion.
60 changes: 59 additions & 1 deletion chain/datasource/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/filecoin-project/lily/chain/actors/adt/diff"
"github.com/filecoin-project/lily/chain/actors/builtin/miner"
"github.com/filecoin-project/lily/lens"
"github.com/filecoin-project/lily/lens/util"
"github.com/filecoin-project/lily/metrics"
"github.com/filecoin-project/lily/tasks"
)
Expand Down Expand Up @@ -65,7 +66,7 @@ func init() {
executedTsCacheSize = getCacheSizeFromEnv(executedTsCacheSizeEnv, 4)
diffPreCommitCacheSize = getCacheSizeFromEnv(diffPreCommitCacheSizeEnv, 500)
diffSectorCacheSize = getCacheSizeFromEnv(diffSectorCacheSizeEnv, 500)
actorCacheSize = getCacheSizeFromEnv(actorCacheSizeEnv, 1000)
actorCacheSize = getCacheSizeFromEnv(actorCacheSizeEnv, 5000)
}

var _ tasks.DataSource = (*DataSource)(nil)
Expand Down Expand Up @@ -221,6 +222,55 @@ func (t *DataSource) Actor(ctx context.Context, addr address.Address, tsk types.
return act, err
}

// ActorInfo retrieves information about an actor at the given address within
// the context of a specific tipset. It first checks a cache for the requested
// information and returns it if available, otherwise it fetches the actor's
// details from the statetree. The retrieved actor information is cached
// for future access. If the actor information is successfully retrieved,
// it includes the actor's state and relevant metadata such as family and name.
// If the actor is not found or an error occurs during retrieval, the function
// returns an error.
func (t *DataSource) ActorInfo(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*tasks.ActorInfo, error) {
metrics.RecordInc(ctx, metrics.DataSourceActorCacheRead)
ctx, span := otel.Tracer("").Start(ctx, "DataSource.ActorInfo")
if span.IsRecording() {
span.SetAttributes(attribute.String("tipset", tsk.String()))
span.SetAttributes(attribute.String("address", addr.String()))
}
defer span.End()

// Includes a prefix to prevent duplication of key names in the cache
key, keyErr := asKey(KeyPrefix{"ActorInfo"}, addr, tsk)
if keyErr == nil {
value, found := t.actorCache.Get(key)
if found {
metrics.RecordInc(ctx, metrics.DataSourceActorCacheHit)
return value.(*tasks.ActorInfo), nil
}
}

act, err := t.Actor(ctx, addr, tsk)
actorInfo := tasks.ActorInfo{}
if err == nil {
if act.Address == nil {
act.Address = &addr
}
actorInfo.Actor = act
actorName, actorFamily, err := util.ActorNameAndFamilyFromCode(act.Code)
if err == nil {
actorInfo.ActorFamily = actorFamily
actorInfo.ActorName = actorName
}
}

// Save the ActorInfo into cache
if err == nil && keyErr == nil {
t.actorCache.Add(key, &actorInfo)
}

return &actorInfo, err
}

func (t *DataSource) MinerPower(ctx context.Context, addr address.Address, ts *types.TipSet) (*api.MinerPower, error) {
ctx, span := otel.Tracer("").Start(ctx, "DataSource.MinerPower")
if span.IsRecording() {
Expand Down Expand Up @@ -528,3 +578,11 @@ func asKey(strs ...fmt.Stringer) (string, error) {
}
return sb.String(), nil
}

type KeyPrefix struct {
Prefix string
}

func (k KeyPrefix) String() string {
return k.Prefix + ":"
}
10 changes: 10 additions & 0 deletions model/fevm/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ type FEVMTransaction struct {
R string `pg:",notnull"`
// Transaction’s signature. Outputs of an ECDSA signature.
S string `pg:",notnull"`
// Filecoin Address of the sender.
FromFilecoinAddress string `pg:",notnull"`
// Filecoin Address of the receiver.
ToFilecoinAddress string `pg:",notnull"`
// Human-readable identifier of sender (From).
FromActorName string `pg:",notnull"`
// Human-readable identifier of receiver (To).
ToActorName string `pg:",notnull"`
// On-chain message triggering the message.
MessageCid string `pg:",pk,notnull"`
}

func (f *FEVMTransaction) Persist(ctx context.Context, s model.StorageBatch, version model.Version) error {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package v1

func init() {
patches.Register(
31,
`
ALTER TABLE {{ .SchemaName | default "public"}}.fevm_transactions
ADD COLUMN IF NOT EXISTS "from_filecoin_address" text;
ALTER TABLE {{ .SchemaName | default "public"}}.fevm_transactions
ADD COLUMN IF NOT EXISTS "to_filecoin_address" text;
ALTER TABLE {{ .SchemaName | default "public"}}.fevm_transactions
ADD COLUMN IF NOT EXISTS "from_actor_name" text;
ALTER TABLE {{ .SchemaName | default "public"}}.fevm_transactions
ADD COLUMN IF NOT EXISTS "to_actor_name" text;
ALTER TABLE {{ .SchemaName | default "public"}}.fevm_transactions
ADD COLUMN IF NOT EXISTS "message_cid" text;
COMMENT ON COLUMN {{ .SchemaName | default "public"}}.fevm_transactions.from_filecoin_address IS 'Filecoin Address of the sender.';
COMMENT ON COLUMN {{ .SchemaName | default "public"}}.fevm_transactions.to_filecoin_address IS 'Filecoin Address of the receiver.';
COMMENT ON COLUMN {{ .SchemaName | default "public"}}.fevm_transactions.from_actor_name IS 'Fully-versioned human-readable identifier of sender (From).';
COMMENT ON COLUMN {{ .SchemaName | default "public"}}.fevm_transactions.to_actor_name IS 'Fully-versioned human-readable identifier of receiver (To).';
COMMENT ON COLUMN {{ .SchemaName | default "public"}}.fevm_transactions.message_cid IS 'Filecoin Message Cid';
`,
)
}
7 changes: 7 additions & 0 deletions tasks/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,17 @@ type ActorStateChangeDiff map[address.Address]ActorStateChange

type ActorStatesByType map[string][]*types.ActorV5

type ActorInfo struct {
Actor *types.Actor
ActorName string
ActorFamily string
}

type DataSource interface {
TipSet(ctx context.Context, tsk types.TipSetKey) (*types.TipSet, error)
Actor(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*types.Actor, error)
ActorState(ctx context.Context, addr address.Address, ts *types.TipSet) (*api.ActorState, error)
ActorInfo(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*ActorInfo, error)
CirculatingSupply(ctx context.Context, ts *types.TipSet) (api.CirculatingSupply, error)
MinerPower(ctx context.Context, addr address.Address, ts *types.TipSet) (*api.MinerPower, error)
ActorStateChanges(ctx context.Context, ts, pts *types.TipSet) (ActorStateChangeDiff, error)
Expand Down
17 changes: 17 additions & 0 deletions tasks/fevm/transaction/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func (p *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, execut
if message.Message == nil {
continue
}
fromActorInfo, err := p.node.ActorInfo(ctx, message.Message.From, current.Key())
if err != nil {
continue
}

if !util.IsEVMMessage(ctx, p.node, message.Message, current.Key()) {
continue
}
Expand Down Expand Up @@ -99,6 +104,9 @@ func (p *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, execut
V: txn.V.String(),
R: txn.R.String(),
S: txn.S.String(),
FromFilecoinAddress: fromActorInfo.Actor.Address.String(),
FromActorName: fromActorInfo.ActorName,
MessageCid: message.Cid.String(),
}

if txn.BlockHash != nil {
Expand All @@ -110,8 +118,17 @@ func (p *Task) ProcessTipSets(ctx context.Context, current *types.TipSet, execut
if txn.TransactionIndex != nil {
txnObj.TransactionIndex = uint64(*txn.TransactionIndex)
}

// Sometime the the "To" field could be nil
if txn.To != nil {
txnObj.To = txn.To.String()

// Get the Actor from ActorInfo
toActorInfo, err := p.node.ActorInfo(ctx, message.Message.To, current.Key())
if err == nil && toActorInfo.Actor != nil && toActorInfo.Actor.Address != nil {
txnObj.ToActorName = toActorInfo.ActorName
txnObj.ToFilecoinAddress = toActorInfo.Actor.Address.String()
}
}

if len(txn.AccessList) > 0 {
Expand Down

0 comments on commit 01a3ec2

Please sign in to comment.