Skip to content

Commit 8814fc4

Browse files
TerryhungTerry
andauthored
feat: add new task for fevm receipt (#1208)
* Add new task: fevm receipt * Add new schema * Add the cache for actor * Refine the logic in cache and placeholder checking --------- Co-authored-by: Terry <[email protected]>
1 parent 4aa0e37 commit 8814fc4

File tree

17 files changed

+304
-37
lines changed

17 files changed

+304
-37
lines changed

chain/datasource/datasource.go

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,51 +40,32 @@ var (
4040
executedTsCacheSize int
4141
diffPreCommitCacheSize int
4242
diffSectorCacheSize int
43+
actorCacheSize int
4344

4445
tipsetMessageReceiptSizeEnv = "LILY_TIPSET_MSG_RECEIPT_CACHE_SIZE"
4546
executedTsCacheSizeEnv = "LILY_EXECUTED_TS_CACHE_SIZE"
4647
diffPreCommitCacheSizeEnv = "LILY_DIFF_PRECOMMIT_CACHE_SIZE"
4748
diffSectorCacheSizeEnv = "LILY_DIFF_SECTORS_CACHE_SIZE"
49+
actorCacheSizeEnv = "LILY_ACTOR_CACHE_SIZE"
4850
)
4951

50-
func init() {
51-
tipsetMessageReceiptCacheSize = 4
52-
executedTsCacheSize = 4
53-
diffPreCommitCacheSize = 500
54-
diffSectorCacheSize = 500
55-
if s := os.Getenv(tipsetMessageReceiptSizeEnv); s != "" {
56-
v, err := strconv.ParseInt(s, 10, 64)
57-
if err == nil {
58-
tipsetMessageReceiptCacheSize = int(v)
59-
} else {
60-
log.Warnf("invalid value (%s) for %s defaulting to %d: %s", s, tipsetMessageReceiptSizeEnv, tipsetMessageReceiptCacheSize, err)
61-
}
62-
}
63-
if s := os.Getenv(executedTsCacheSizeEnv); s != "" {
52+
func getCacheSizeFromEnv(env string, defaultValue int) int {
53+
if s := os.Getenv(env); s != "" {
6454
v, err := strconv.ParseInt(s, 10, 64)
6555
if err == nil {
66-
executedTsCacheSize = int(v)
67-
} else {
68-
log.Warnf("invalid value (%s) for %s defaulting to %d: %s", s, executedTsCacheSizeEnv, executedTsCacheSize, err)
69-
}
70-
}
71-
if s := os.Getenv(diffPreCommitCacheSizeEnv); s != "" {
72-
v, err := strconv.ParseInt(s, 10, 64)
73-
if err == nil {
74-
diffPreCommitCacheSize = int(v)
75-
} else {
76-
log.Warnf("invalid value (%s) for %s defaulting to %d: %s", s, diffPreCommitCacheSizeEnv, diffPreCommitCacheSize, err)
77-
}
78-
}
79-
if s := os.Getenv(diffSectorCacheSizeEnv); s != "" {
80-
v, err := strconv.ParseInt(s, 10, 64)
81-
if err == nil {
82-
diffSectorCacheSize = int(v)
83-
} else {
84-
log.Warnf("invalid value (%s) for %s defaulting to %d: %s", s, diffSectorCacheSizeEnv, diffSectorCacheSize, err)
56+
return int(v)
8557
}
58+
log.Warnf("invalid value (%s) for %s defaulting to %d: %s", s, env, defaultValue, err)
8659
}
60+
return defaultValue
61+
}
8762

63+
func init() {
64+
tipsetMessageReceiptCacheSize = getCacheSizeFromEnv(tipsetMessageReceiptSizeEnv, 4)
65+
executedTsCacheSize = getCacheSizeFromEnv(executedTsCacheSizeEnv, 4)
66+
diffPreCommitCacheSize = getCacheSizeFromEnv(diffPreCommitCacheSizeEnv, 500)
67+
diffSectorCacheSize = getCacheSizeFromEnv(diffSectorCacheSizeEnv, 500)
68+
actorCacheSize = getCacheSizeFromEnv(actorCacheSizeEnv, 1000)
8869
}
8970

9071
var _ tasks.DataSource = (*DataSource)(nil)
@@ -117,6 +98,11 @@ func NewDataSource(node lens.API) (*DataSource, error) {
11798
return nil, err
11899
}
119100

101+
t.actorCache, err = lru.New(actorCacheSize)
102+
if err != nil {
103+
return nil, err
104+
}
105+
120106
return t, nil
121107
}
122108

@@ -134,6 +120,8 @@ type DataSource struct {
134120

135121
diffPreCommitCache *lru.Cache
136122
diffPreCommitGroup singleflight.Group
123+
124+
actorCache *lru.Cache
137125
}
138126

139127
func (t *DataSource) MessageReceiptEvents(ctx context.Context, root cid.Cid) ([]types.Event, error) {
@@ -148,10 +136,18 @@ func (t *DataSource) TipSetBlockMessages(ctx context.Context, ts *types.TipSet)
148136
return t.node.MessagesForTipSetBlocks(ctx, ts)
149137
}
150138

139+
func (t *DataSource) ChainGetMessagesInTipset(ctx context.Context, tsk types.TipSetKey) ([]api.Message, error) {
140+
return t.node.ChainGetMessagesInTipset(ctx, tsk)
141+
}
142+
151143
func (t *DataSource) EthGetBlockByHash(ctx context.Context, blkHash ethtypes.EthHash, fullTxInfo bool) (ethtypes.EthBlock, error) {
152144
return t.node.EthGetBlockByHash(ctx, blkHash, fullTxInfo)
153145
}
154146

147+
func (t *DataSource) EthGetTransactionReceipt(ctx context.Context, txHash ethtypes.EthHash) (*api.EthTxReceipt, error) {
148+
return t.node.EthGetTransactionReceipt(ctx, txHash)
149+
}
150+
155151
// TipSetMessageReceipts returns the blocks and messages in `pts` and their corresponding receipts from `ts` matching block order in tipset (`pts`).
156152
// TODO replace with lotus chainstore method when https://github.com/filecoin-project/lotus/pull/9186 lands
157153
func (t *DataSource) TipSetMessageReceipts(ctx context.Context, ts, pts *types.TipSet) ([]*lens.BlockMessageReceipts, error) {
@@ -192,13 +188,29 @@ func (t *DataSource) Store() adt.Store {
192188
}
193189

194190
func (t *DataSource) Actor(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*types.Actor, error) {
191+
metrics.RecordInc(ctx, metrics.DataSourceActorCacheRead)
195192
ctx, span := otel.Tracer("").Start(ctx, "DataSource.Actor")
196193
if span.IsRecording() {
197194
span.SetAttributes(attribute.String("tipset", tsk.String()))
198195
span.SetAttributes(attribute.String("address", addr.String()))
199196
}
200197
defer span.End()
201-
return t.node.StateGetActor(ctx, addr, tsk)
198+
199+
key, keyErr := asKey(addr, tsk)
200+
if keyErr == nil {
201+
value, found := t.actorCache.Get(key)
202+
if found {
203+
metrics.RecordInc(ctx, metrics.DataSourceActorCacheHit)
204+
return value.(*types.Actor), nil
205+
}
206+
}
207+
208+
act, err := t.node.StateGetActor(ctx, addr, tsk)
209+
if err == nil && keyErr == nil {
210+
t.actorCache.Add(key, act)
211+
}
212+
213+
return act, err
202214
}
203215

204216
func (t *DataSource) MinerPower(ctx context.Context, addr address.Address, ts *types.TipSet) (*api.MinerPower, error) {

chain/indexer/integrated/processor/state.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import (
6060

6161
// fevm task
6262
fevmblockheadertask "github.com/filecoin-project/lily/tasks/fevm/blockheader"
63+
fevmreceipttask "github.com/filecoin-project/lily/tasks/fevm/receipt"
6364
fevmactorstatstask "github.com/filecoin-project/lily/tasks/fevmactorstats"
6465

6566
"github.com/filecoin-project/lily/chain/indexer/tasktype"
@@ -644,6 +645,8 @@ func MakeProcessors(api tasks.DataSource, indexerTasks []string) (*IndexerProces
644645
out.TipsetProcessors[t] = fevmactorstatstask.NewTask(api)
645646
case tasktype.FEVMBlockHeader:
646647
out.TipsetsProcessors[t] = fevmblockheadertask.NewTask(api)
648+
case tasktype.FEVMReceipt:
649+
out.TipsetsProcessors[t] = fevmreceipttask.NewTask(api)
647650

648651
case BuiltinTaskName:
649652
out.ReportProcessors[t] = indexertask.NewTask(api)

chain/indexer/integrated/processor/state_internal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestNewProcessor(t *testing.T) {
5454
require.Equal(t, t.Name(), proc.name)
5555
require.Len(t, proc.actorProcessors, 24)
5656
require.Len(t, proc.tipsetProcessors, 10)
57-
require.Len(t, proc.tipsetsProcessors, 10)
57+
require.Len(t, proc.tipsetsProcessors, 11)
5858
require.Len(t, proc.builtinProcessors, 1)
5959

6060
require.Equal(t, gasoutput.NewTask(nil), proc.tipsetsProcessors[tasktype.GasOutputs])

chain/indexer/integrated/processor/state_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,6 @@ func TestMakeProcessorsAllTasks(t *testing.T) {
408408
require.NoError(t, err)
409409
require.Len(t, proc.ActorProcessors, 24)
410410
require.Len(t, proc.TipsetProcessors, 10)
411-
require.Len(t, proc.TipsetsProcessors, 10)
411+
require.Len(t, proc.TipsetsProcessors, 11)
412412
require.Len(t, proc.ReportProcessors, 1)
413413
}

chain/indexer/tasktype/table_tasks.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const (
4646
VerifiedRegistryClaim = "verified_registry_claim"
4747
FEVMActorStats = "fevm_actor_stats"
4848
FEVMBlockHeader = "fevm_block_header"
49+
FEVMReceipt = "fevm_receipt"
4950
)
5051

5152
var AllTableTasks = []string{
@@ -93,6 +94,7 @@ var AllTableTasks = []string{
9394
VerifiedRegistryClaim,
9495
FEVMActorStats,
9596
FEVMBlockHeader,
97+
FEVMReceipt,
9698
}
9799

98100
var TableLookup = map[string]struct{}{
@@ -140,6 +142,7 @@ var TableLookup = map[string]struct{}{
140142
VerifiedRegistryClaim: {},
141143
FEVMActorStats: {},
142144
FEVMBlockHeader: {},
145+
FEVMReceipt: {},
143146
}
144147

145148
var TableComment = map[string]string{
@@ -187,6 +190,7 @@ var TableComment = map[string]string{
187190
VerifiedRegistryClaim: ``,
188191
FEVMActorStats: ``,
189192
FEVMBlockHeader: ``,
193+
FEVMReceipt: ``,
190194
}
191195

192196
var TableFieldComments = map[string]map[string]string{
@@ -291,4 +295,5 @@ var TableFieldComments = map[string]map[string]string{
291295
VerifiedRegistryClaim: {},
292296
FEVMActorStats: {},
293297
FEVMBlockHeader: {},
298+
FEVMReceipt: {},
294299
}

chain/indexer/tasktype/tasks.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ var TaskLookup = map[string][]string{
9494
FEVMTask: {
9595
FEVMActorStats,
9696
FEVMBlockHeader,
97+
FEVMReceipt,
9798
},
9899
}
99100

chain/indexer/tasktype/tasks_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func TestMakeAllTaskAliasNames(t *testing.T) {
101101
}
102102

103103
func TestMakeAllTaskNames(t *testing.T) {
104-
const TotalTableTasks = 44
104+
const TotalTableTasks = 45
105105
actual, err := tasktype.MakeTaskNames(tasktype.AllTableTasks)
106106
require.NoError(t, err)
107107
// if this test fails it means a new task name was added, update the above test

lens/interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type ChainAPI interface {
4545

4646
ChainGetBlockMessages(ctx context.Context, msg cid.Cid) (*api.BlockMessages, error)
4747
ChainGetParentMessages(ctx context.Context, blockCid cid.Cid) ([]api.Message, error)
48+
ChainGetMessagesInTipset(ctx context.Context, tsk types.TipSetKey) ([]api.Message, error)
4849

4950
ComputeBaseFee(ctx context.Context, ts *types.TipSet) (abi.TokenAmount, error)
5051

@@ -78,6 +79,7 @@ type VMAPI interface {
7879

7980
type EthModuleAPI interface {
8081
EthGetBlockByHash(ctx context.Context, blkHash ethtypes.EthHash, fullTxInfo bool) (ethtypes.EthBlock, error)
82+
EthGetTransactionReceipt(ctx context.Context, txHash ethtypes.EthHash) (*api.EthTxReceipt, error)
8183
}
8284

8385
type MessageExecution struct {

lens/lily/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ type LilyAPI interface {
5656
ChainPrune(ctx context.Context, opts api.PruneOpts) error //perm:read
5757
ChainHotGC(ctx context.Context, opts api.HotGCOpts) error //perm:read
5858
EthGetBlockByHash(ctx context.Context, blkHash ethtypes.EthHash, fullTxInfo bool) (ethtypes.EthBlock, error) //perm:read
59+
EthGetTransactionReceipt(ctx context.Context, txHash ethtypes.EthHash) (*api.EthTxReceipt, error) //perm:read
60+
ChainGetMessagesInTipset(ctx context.Context, tsk types.TipSetKey) ([]api.Message, error) //perm:read
5961

6062
// trigger graceful shutdown
6163
Shutdown(context.Context) error

lens/lily/impl.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,14 @@ func (m *LilyNodeAPI) EthGetBlockByHash(ctx context.Context, blkHash ethtypes.Et
559559
return m.EthModuleAPI.EthGetBlockByHash(ctx, blkHash, fullTxInfo)
560560
}
561561

562+
func (m *LilyNodeAPI) EthGetTransactionReceipt(ctx context.Context, txHash ethtypes.EthHash) (*api.EthTxReceipt, error) {
563+
return m.EthModuleAPI.EthGetTransactionReceipt(ctx, txHash)
564+
}
565+
566+
func (m *LilyNodeAPI) ChainGetMessagesInTipset(ctx context.Context, tsk types.TipSetKey) ([]api.Message, error) {
567+
return m.ChainAPI.ChainGetMessagesInTipset(ctx, tsk)
568+
}
569+
562570
// MessagesForTipSetBlocks returns messages stored in the blocks of the specified tipset, messages may be duplicated
563571
// across the returned set of BlockMessages.
564572
func (m *LilyNodeAPI) MessagesForTipSetBlocks(ctx context.Context, ts *types.TipSet) ([]*lens.BlockMessages, error) {

0 commit comments

Comments
 (0)