@@ -40,51 +40,32 @@ var (
40
40
executedTsCacheSize int
41
41
diffPreCommitCacheSize int
42
42
diffSectorCacheSize int
43
+ actorCacheSize int
43
44
44
45
tipsetMessageReceiptSizeEnv = "LILY_TIPSET_MSG_RECEIPT_CACHE_SIZE"
45
46
executedTsCacheSizeEnv = "LILY_EXECUTED_TS_CACHE_SIZE"
46
47
diffPreCommitCacheSizeEnv = "LILY_DIFF_PRECOMMIT_CACHE_SIZE"
47
48
diffSectorCacheSizeEnv = "LILY_DIFF_SECTORS_CACHE_SIZE"
49
+ actorCacheSizeEnv = "LILY_ACTOR_CACHE_SIZE"
48
50
)
49
51
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 != "" {
64
54
v , err := strconv .ParseInt (s , 10 , 64 )
65
55
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 )
85
57
}
58
+ log .Warnf ("invalid value (%s) for %s defaulting to %d: %s" , s , env , defaultValue , err )
86
59
}
60
+ return defaultValue
61
+ }
87
62
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 )
88
69
}
89
70
90
71
var _ tasks.DataSource = (* DataSource )(nil )
@@ -117,6 +98,11 @@ func NewDataSource(node lens.API) (*DataSource, error) {
117
98
return nil , err
118
99
}
119
100
101
+ t .actorCache , err = lru .New (actorCacheSize )
102
+ if err != nil {
103
+ return nil , err
104
+ }
105
+
120
106
return t , nil
121
107
}
122
108
@@ -134,6 +120,8 @@ type DataSource struct {
134
120
135
121
diffPreCommitCache * lru.Cache
136
122
diffPreCommitGroup singleflight.Group
123
+
124
+ actorCache * lru.Cache
137
125
}
138
126
139
127
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)
148
136
return t .node .MessagesForTipSetBlocks (ctx , ts )
149
137
}
150
138
139
+ func (t * DataSource ) ChainGetMessagesInTipset (ctx context.Context , tsk types.TipSetKey ) ([]api.Message , error ) {
140
+ return t .node .ChainGetMessagesInTipset (ctx , tsk )
141
+ }
142
+
151
143
func (t * DataSource ) EthGetBlockByHash (ctx context.Context , blkHash ethtypes.EthHash , fullTxInfo bool ) (ethtypes.EthBlock , error ) {
152
144
return t .node .EthGetBlockByHash (ctx , blkHash , fullTxInfo )
153
145
}
154
146
147
+ func (t * DataSource ) EthGetTransactionReceipt (ctx context.Context , txHash ethtypes.EthHash ) (* api.EthTxReceipt , error ) {
148
+ return t .node .EthGetTransactionReceipt (ctx , txHash )
149
+ }
150
+
155
151
// TipSetMessageReceipts returns the blocks and messages in `pts` and their corresponding receipts from `ts` matching block order in tipset (`pts`).
156
152
// TODO replace with lotus chainstore method when https://github.com/filecoin-project/lotus/pull/9186 lands
157
153
func (t * DataSource ) TipSetMessageReceipts (ctx context.Context , ts , pts * types.TipSet ) ([]* lens.BlockMessageReceipts , error ) {
@@ -192,13 +188,29 @@ func (t *DataSource) Store() adt.Store {
192
188
}
193
189
194
190
func (t * DataSource ) Actor (ctx context.Context , addr address.Address , tsk types.TipSetKey ) (* types.Actor , error ) {
191
+ metrics .RecordInc (ctx , metrics .DataSourceActorCacheRead )
195
192
ctx , span := otel .Tracer ("" ).Start (ctx , "DataSource.Actor" )
196
193
if span .IsRecording () {
197
194
span .SetAttributes (attribute .String ("tipset" , tsk .String ()))
198
195
span .SetAttributes (attribute .String ("address" , addr .String ()))
199
196
}
200
197
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
202
214
}
203
215
204
216
func (t * DataSource ) MinerPower (ctx context.Context , addr address.Address , ts * types.TipSet ) (* api.MinerPower , error ) {
0 commit comments