Skip to content

Commit

Permalink
Create a Lily node by embedding lotus into visor to observe tipset ap…
Browse files Browse the repository at this point in the history
…ply and revert events (#408)
  • Loading branch information
frrist committed Mar 11, 2021
1 parent e4d8f43 commit a0e1aa0
Show file tree
Hide file tree
Showing 24 changed files with 1,105 additions and 394 deletions.
4 changes: 3 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ commands:
condition: << parameters.linux >>
steps:
- run: sudo apt-get update
- run: sudo apt-get install -y ocl-icd-opencl-dev libhwloc-dev
- run: sudo apt-get install -y pkg-config jq
- run: git submodule sync
- run: sudo apt-get install gcc libc-dev
- run: git submodule update --init
Expand Down Expand Up @@ -109,13 +111,13 @@ jobs:
POSTGRES_PASSWORD: password
steps:
- checkout
- prepare
- run: # dep for DB wait script
name: install dockerize
command: wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && sudo tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
environment:
DOCKERIZE_VERSION: v0.3.0
- run: sudo apt-get update
- run: sudo apt-get install -y pkg-config jq
- run: make deps
- run: make build
- run:
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "extern/fil-blst"]
path = extern/fil-blst
url = https://github.com/filecoin-project/fil-blst
[submodule "extern/filecoin-ffi"]
path = extern/filecoin-ffi
url = https://github.com/filecoin-project/filecoin-ffi.git
46 changes: 40 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
SHELL=/usr/bin/env bash

PG_IMAGE?=postgres:10
REDIS_IMAGE?=redis:6
COMMIT := $(shell git rev-parse --short HEAD)
Expand All @@ -12,6 +14,39 @@ BINS:=

GOFLAGS:=

## FFI

FFI_PATH:=extern/filecoin-ffi/
FFI_DEPS:=.install-filcrypto
FFI_DEPS:=$(addprefix $(FFI_PATH),$(FFI_DEPS))

$(FFI_DEPS): build/.filecoin-install ;

build/.filecoin-install: $(FFI_PATH)
$(MAKE) -C $(FFI_PATH) $(FFI_DEPS:$(FFI_PATH)%=%)
@touch $@

MODULES+=$(FFI_PATH)
BUILD_DEPS+=build/.filecoin-install
CLEAN+=build/.filecoin-install

ffi-version-check:
@[[ "$$(awk '/const Version/{print $$5}' extern/filecoin-ffi/version.go)" -eq 2 ]] || (echo "FFI version mismatch, update submodules"; exit 1)
BUILD_DEPS+=ffi-version-check

.PHONY: ffi-version-check


$(MODULES): build/.update-modules ;
# dummy file that marks the last time modules were updated
build/.update-modules:
git submodule update --init --recursive
touch $@

CLEAN+=build/.update-modules
# end git modules


ldflags=-X=github.com/filecoin-project/sentinel-visor/version.GitVersion=$(GITVERSION)
ifneq ($(strip $(LDFLAGS)),)
ldflags+=-extldflags=$(LDFLAGS)
Expand All @@ -24,13 +59,9 @@ all: build
.PHONY: build
build: deps visor

# dummy file that marks the last time modules were updated
build/.update-modules:
git submodule update --init --recursive
touch $@

.PHONY: deps
deps: build/.update-modules
deps: $(BUILD_DEPS)
cd ./vector; ./fetch_vectors.sh

# test starts dependencies and runs all tests
Expand Down Expand Up @@ -73,9 +104,12 @@ docker-image:
.PHONY: clean
clean:
rm -rf $(CLEAN) $(BINS)
.PHONY: clean

vector-clean:
rm ./vector/data/*json
.PHONY: vector-clean

.PHONY: dist-clean
dist-clean:
git clean -xdff
git submodule deinit --all -f
Expand Down
45 changes: 45 additions & 0 deletions chain/indexing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package chain

import (
"context"

"github.com/filecoin-project/lotus/chain/events"
"github.com/filecoin-project/lotus/chain/types"
)

func NewIndexingTipSetObserver(obs TipSetObserver, cache *TipSetCache) *IndexingTipSetObserver {
return &IndexingTipSetObserver{
obs: obs,
cache: cache,
}
}

type IndexingTipSetObserver struct {
obs TipSetObserver
cache *TipSetCache
}

func (i *IndexingTipSetObserver) Apply(ctx context.Context, ts *types.TipSet) error {
log.Debugw("add tipset", "height", ts.Height(), "tipset", ts.Key().String())
tail, err := i.cache.Add(ts)
if err != nil {
log.Errorw("tipset cache add", "error", err.Error())
}

// Send the tipset that fell out of the confidence window to the observer
if tail != nil {
return i.obs.TipSet(ctx, tail)
}
return nil
}

func (i *IndexingTipSetObserver) Revert(ctx context.Context, ts *types.TipSet) error {
log.Debugw("revert tipset", "height", ts.Height(), "tipset", ts.Key().String())
err := i.cache.Revert(ts)
if err != nil {
log.Errorw("tipset cache revert", "error", err.Error())
}
return nil
}

var _ events.TipSetObserver = (*IndexingTipSetObserver)(nil)
179 changes: 179 additions & 0 deletions commands/lily/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package lily

import (
"context"
"io/ioutil"

paramfetch "github.com/filecoin-project/go-paramfetch"
lotusbuild "github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/events"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/lib/lotuslog"
"github.com/filecoin-project/lotus/lib/peermgr"
"github.com/filecoin-project/lotus/node"
lotusmodules "github.com/filecoin-project/lotus/node/modules"
"github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/node/repo"
logging "github.com/ipfs/go-log/v2"
"github.com/mitchellh/go-homedir"
"github.com/multiformats/go-multiaddr"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"

"github.com/filecoin-project/sentinel-visor/commands/util"
"github.com/filecoin-project/sentinel-visor/lens/lily"
"github.com/filecoin-project/sentinel-visor/lens/lily/modules"
)

var log = logging.Logger("lily-cli")

type daemonOpts struct {
api string
repo string
bootstrap bool
config string
importsnapshot string
genesis string
}

var daemonFlags daemonOpts

var LilyDaemon = &cli.Command{
Name: "daemon",
Usage: "Start a lily daemon process",
After: destroy,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "api",
EnvVars: []string{"SENTINEL_LILY_API"},
Value: "1234",
Destination: &daemonFlags.api,
},
&cli.StringFlag{
Name: "repo",
EnvVars: []string{"SENTINEL_LILY_REPO"},
Value: "~/.lotus",
Destination: &daemonFlags.repo,
},
&cli.BoolFlag{
Name: "bootstrap",
EnvVars: []string{"SENTINEL_LILY_BOOTSTRAP"},
Value: true,
Destination: &daemonFlags.bootstrap,
},
&cli.StringFlag{
Name: "config",
Usage: "specify path of config file to use",
EnvVars: []string{"SENTINEL_LILY_CONFIG"},
Destination: &daemonFlags.config,
},
&cli.StringFlag{
Name: "import-snapshot",
Usage: "import chain state from a given chain export file or url",
EnvVars: []string{"SENTINEL_LILY_SNAPSHOT"},
Destination: &daemonFlags.importsnapshot,
},
&cli.StringFlag{
Name: "genesis",
Usage: "genesis file to use for first node run",
EnvVars: []string{"SENTINEL_LILY_GENESIS"},
Destination: &daemonFlags.genesis,
},
},
Action: func(c *cli.Context) error {
lotuslog.SetupLogLevels()
ctx := context.Background()
{
dir, err := homedir.Expand(daemonFlags.repo)
if err != nil {
log.Warnw("could not expand repo location", "error", err)
} else {
log.Infof("lotus repo: %s", dir)
}
}

r, err := repo.NewFS(daemonFlags.repo)
if err != nil {
return xerrors.Errorf("opening fs repo: %w", err)
}

if daemonFlags.config != "" {
r.SetConfigPath(daemonFlags.config)
}

err = r.Init(repo.FullNode)
if err != nil && err != repo.ErrRepoExists {
return xerrors.Errorf("repo init error: %w", err)
}

if err := paramfetch.GetParams(lcli.ReqContext(c), lotusbuild.ParametersJSON(), 0); err != nil {
return xerrors.Errorf("fetching proof parameters: %w", err)
}

var genBytes []byte
if c.String("genesis") != "" {
genBytes, err = ioutil.ReadFile(daemonFlags.genesis)
if err != nil {
return xerrors.Errorf("reading genesis: %w", err)
}
} else {
genBytes = lotusbuild.MaybeGenesis()
}

if daemonFlags.importsnapshot != "" {
if err := util.ImportChain(ctx, r, daemonFlags.importsnapshot, true); err != nil {
return err
}
}

genesis := node.Options()
if len(genBytes) > 0 {
genesis = node.Override(new(lotusmodules.Genesis), lotusmodules.LoadGenesis(genBytes))
}

isBootstrapper := false
shutdown := make(chan struct{})
liteModeDeps := node.Options()
var api lily.LilyAPI
stop, err := node.New(ctx,
// Start Sentinel Dep injection
LilyNodeAPIOption(&api),
node.Override(new(*events.Events), modules.NewEvents),
// End Injection

node.Override(new(dtypes.Bootstrapper), isBootstrapper),
node.Override(new(dtypes.ShutdownChan), shutdown),
node.Online(),
node.Repo(r),

genesis,
liteModeDeps,

node.ApplyIf(func(s *node.Settings) bool { return c.IsSet("api") },
node.Override(node.SetApiEndpointKey, func(lr repo.LockedRepo) error {
apima, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/" +
c.String("api"))
if err != nil {
return err
}
return lr.SetAPIEndpoint(apima)
})),
node.ApplyIf(func(s *node.Settings) bool { return !daemonFlags.bootstrap },
node.Unset(node.RunPeerMgrKey),
node.Unset(new(*peermgr.PeerMgr)),
),
)
if err != nil {
return xerrors.Errorf("initializing node: %w", err)
}

endpoint, err := r.APIEndpoint()
if err != nil {
return xerrors.Errorf("getting api endpoint: %w", err)
}

// TODO: properly parse api endpoint (or make it a URL)
maxAPIRequestSize := int64(0)
return util.ServeRPC(api, stop, endpoint, shutdown, maxAPIRequestSize)
},
}
Loading

0 comments on commit a0e1aa0

Please sign in to comment.