|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//go:build !js |
| 6 | + |
| 7 | +package node |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + |
| 12 | + "github.com/sourcenetwork/corekv" |
| 13 | + sourceP2P "github.com/sourcenetwork/go-p2p" |
| 14 | + "github.com/sourcenetwork/immutable" |
| 15 | + |
| 16 | + "github.com/sourcenetwork/lens/host-go/engine/module" |
| 17 | + "github.com/sourcenetwork/lens/host-go/p2p" |
| 18 | + "github.com/sourcenetwork/lens/host-go/store" |
| 19 | +) |
| 20 | + |
| 21 | +type Options struct { |
| 22 | + Path immutable.Option[string] |
| 23 | + Rootstore immutable.Option[corekv.TxnReaderWriter] |
| 24 | + TxnSource immutable.Option[store.TxnSource] |
| 25 | + PoolSize immutable.Option[int] |
| 26 | + Runtime immutable.Option[module.Runtime] |
| 27 | + BlockstoreNamespace immutable.Option[string] |
| 28 | + BlockstoreChunkSize immutable.Option[int] |
| 29 | + IndexstoreNamespace immutable.Option[string] |
| 30 | + P2P immutable.Option[p2p.Host] |
| 31 | + DisableP2P bool |
| 32 | + |
| 33 | + P2POptions []sourceP2P.NodeOpt |
| 34 | +} |
| 35 | + |
| 36 | +type Node struct { |
| 37 | + onClose []closer |
| 38 | + Options Options |
| 39 | + Store store.TxnStore |
| 40 | + P2P immutable.Option[*p2p.P2P] |
| 41 | +} |
| 42 | + |
| 43 | +func WithP2P(host p2p.Host) Option { |
| 44 | + return func(opts *Options) { |
| 45 | + opts.P2P = immutable.Some(host) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func WithP2PDisabled(disableP2P bool) Option { |
| 50 | + return func(opts *Options) { |
| 51 | + opts.DisableP2P = disableP2P |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func WithP2Poptions(opts ...sourceP2P.NodeOpt) Option { |
| 56 | + return func(opt *Options) { |
| 57 | + opt.P2POptions = opts |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func createNode(ctx context.Context, store store.TxnStore, o Options, onClose []closer) (*Node, error) { |
| 62 | + var p2pSys immutable.Option[*p2p.P2P] |
| 63 | + if !o.DisableP2P { |
| 64 | + var host p2p.Host |
| 65 | + if o.P2P.HasValue() { |
| 66 | + host = o.P2P.Value() |
| 67 | + } else { |
| 68 | + p2pOptions := append( |
| 69 | + o.P2POptions, |
| 70 | + sourceP2P.WithBlockstoreNamespace(o.BlockstoreNamespace.Value()), |
| 71 | + sourceP2P.WithRootstore(o.Rootstore.Value()), |
| 72 | + ) |
| 73 | + |
| 74 | + if o.BlockstoreChunkSize.HasValue() { |
| 75 | + p2pOptions = append(p2pOptions, sourceP2P.WithBlockstoreChunkSize(o.BlockstoreChunkSize.Value())) |
| 76 | + } |
| 77 | + |
| 78 | + var err error |
| 79 | + host, err = sourceP2P.NewPeer( |
| 80 | + ctx, |
| 81 | + p2pOptions..., |
| 82 | + ) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + p2pSys = immutable.Some(p2p.New(host, o.Rootstore.Value(), o.IndexstoreNamespace.Value())) |
| 89 | + } |
| 90 | + |
| 91 | + return &Node{ |
| 92 | + onClose: onClose, |
| 93 | + Options: o, |
| 94 | + Store: store, |
| 95 | + P2P: p2pSys, |
| 96 | + }, nil |
| 97 | +} |
0 commit comments