2023-01-04 13:22:41 +00:00
|
|
|
package modules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-26 04:48:12 +00:00
|
|
|
"os"
|
2023-01-04 13:22:41 +00:00
|
|
|
"path/filepath"
|
2023-06-05 22:57:10 +00:00
|
|
|
"time"
|
2023-01-04 13:22:41 +00:00
|
|
|
|
|
|
|
"go.uber.org/fx"
|
|
|
|
|
2023-06-05 22:57:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
|
2023-01-13 16:49:01 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/ethhashlookup"
|
2023-01-04 13:22:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/events"
|
|
|
|
"github.com/filecoin-project/lotus/chain/messagepool"
|
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
|
|
|
"github.com/filecoin-project/lotus/node/impl/full"
|
|
|
|
"github.com/filecoin-project/lotus/node/modules/helpers"
|
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
2023-04-21 12:02:40 +00:00
|
|
|
func EthModuleAPI(cfg config.FevmConfig) func(helpers.MetricsCtx, repo.LockedRepo, fx.Lifecycle, *store.ChainStore, *stmgr.StateManager, EventAPI, *messagepool.MessagePool, full.StateAPI, full.ChainAPI, full.MpoolAPI, full.SyncAPI) (*full.EthModule, error) {
|
|
|
|
return func(mctx helpers.MetricsCtx, r repo.LockedRepo, lc fx.Lifecycle, cs *store.ChainStore, sm *stmgr.StateManager, evapi EventAPI, mp *messagepool.MessagePool, stateapi full.StateAPI, chainapi full.ChainAPI, mpoolapi full.MpoolAPI, syncapi full.SyncAPI) (*full.EthModule, error) {
|
2023-01-19 22:44:58 +00:00
|
|
|
sqlitePath, err := r.SqlitePath()
|
2023-01-04 13:22:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-26 04:48:12 +00:00
|
|
|
dbPath := filepath.Join(sqlitePath, "txhash.db")
|
|
|
|
|
|
|
|
// Check if the db exists, if not, we'll back-fill some entries
|
|
|
|
_, err = os.Stat(dbPath)
|
|
|
|
dbAlreadyExists := err == nil
|
|
|
|
|
|
|
|
transactionHashLookup, err := ethhashlookup.NewTransactionHashLookup(dbPath)
|
2023-01-04 13:22:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStop: func(ctx context.Context) error {
|
|
|
|
return transactionHashLookup.Close()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
ethTxHashManager := full.EthTxHashManager{
|
|
|
|
StateAPI: stateapi,
|
|
|
|
TransactionHashLookup: transactionHashLookup,
|
|
|
|
}
|
|
|
|
|
2023-01-26 04:48:12 +00:00
|
|
|
if !dbAlreadyExists {
|
|
|
|
err = ethTxHashManager.PopulateExistingMappings(mctx, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 22:57:10 +00:00
|
|
|
// prefill the whole skiplist cache maintained internally by the GetTipsetByHeight
|
|
|
|
go func() {
|
|
|
|
start := time.Now()
|
|
|
|
log.Infoln("Start prefilling GetTipsetByHeight cache")
|
|
|
|
_, err := cs.GetTipsetByHeight(mctx, abi.ChainEpoch(0), cs.GetHeaviestTipSet(), false)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("error when prefilling GetTipsetByHeight cache: %w", err)
|
|
|
|
}
|
|
|
|
log.Infof("Prefilling GetTipsetByHeight done in %s", time.Since(start))
|
|
|
|
}()
|
|
|
|
|
2023-01-04 13:22:41 +00:00
|
|
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStart: func(context.Context) error {
|
2023-04-20 21:44:16 +00:00
|
|
|
ev, err := events.NewEvents(ctx, &evapi)
|
2023-01-04 13:22:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tipset listener
|
|
|
|
_ = ev.Observe(ðTxHashManager)
|
|
|
|
|
|
|
|
ch, err := mp.Updates(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go full.WaitForMpoolUpdates(ctx, ch, ðTxHashManager)
|
2023-01-16 07:56:45 +00:00
|
|
|
go full.EthTxHashGC(ctx, cfg.EthTxHashMappingLifetimeDays, ðTxHashManager)
|
2023-01-04 13:22:41 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-01-19 18:30:18 +00:00
|
|
|
return &full.EthModule{
|
|
|
|
Chain: cs,
|
|
|
|
Mpool: mp,
|
|
|
|
StateManager: sm,
|
|
|
|
|
|
|
|
ChainAPI: chainapi,
|
|
|
|
MpoolAPI: mpoolapi,
|
|
|
|
StateAPI: stateapi,
|
2023-04-21 12:02:40 +00:00
|
|
|
SyncAPI: syncapi,
|
2023-01-19 18:30:18 +00:00
|
|
|
|
|
|
|
EthTxHashManager: ðTxHashManager,
|
|
|
|
}, nil
|
2023-01-04 13:22:41 +00:00
|
|
|
}
|
|
|
|
}
|