Skip to content

Commit 6ffef28

Browse files
authored
fix: Add Lens to repository on sync (#143)
1 parent c1fa19f commit 6ffef28

7 files changed

Lines changed: 73 additions & 25 deletions

File tree

host-go/node/node.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/sourcenetwork/corekv/badger"
1515
"github.com/sourcenetwork/immutable"
16+
"github.com/sourcenetwork/lens/host-go/repository"
1617
"github.com/sourcenetwork/lens/host-go/runtimes"
1718
"github.com/sourcenetwork/lens/host-go/store"
1819
)
@@ -68,16 +69,18 @@ func New(ctx context.Context, opts ...Option) (*Node, error) {
6869
o.IndexstoreNamespace = immutable.Some("i")
6970
}
7071

72+
repo := repository.NewRepository(o.PoolSize.Value(), o.Runtime.Value(), &repositoryTxnSource{src: o.TxnSource.Value()})
73+
7174
node, err := createNode(
7275
ctx,
73-
store.New(
76+
store.NewWithRepository(
7477
o.TxnSource.Value(),
75-
o.PoolSize.Value(),
76-
o.Runtime.Value(),
78+
repo,
7779
o.BlockstoreNamespace.Value(),
7880
o.BlockstoreChunkSize,
7981
o.IndexstoreNamespace.Value(),
8082
),
83+
repo,
8184
o,
8285
onClose,
8386
)
@@ -105,3 +108,18 @@ func (n *Node) Close() error {
105108

106109
return nil
107110
}
111+
112+
// repositoryTxnSource wraps a `TxnSource` so that it satisfies the `repository.TxnSource`
113+
// interface and can be passed through.
114+
//
115+
// Without this, either the `store` package or the `repository` package would require an unnecessarily
116+
// enlarged Txn interface, hindering consumption.
117+
type repositoryTxnSource struct {
118+
src store.TxnSource
119+
}
120+
121+
var _ repository.TxnSource = (*repositoryTxnSource)(nil)
122+
123+
func (s *repositoryTxnSource) NewTxn(readonly bool) (repository.Txn, error) {
124+
return s.src.NewTxn(readonly)
125+
}

host-go/node/node_p2p.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/sourcenetwork/lens/host-go/engine/module"
1717
"github.com/sourcenetwork/lens/host-go/p2p"
18+
"github.com/sourcenetwork/lens/host-go/repository"
1819
"github.com/sourcenetwork/lens/host-go/store"
1920
)
2021

@@ -58,7 +59,13 @@ func WithP2Poptions(opts ...sourceP2P.NodeOpt) Option {
5859
}
5960
}
6061

61-
func createNode(ctx context.Context, store store.TxnStore, o Options, onClose []closer) (*Node, error) {
62+
func createNode(
63+
ctx context.Context,
64+
store store.TxnStore,
65+
repository repository.Repository,
66+
o Options,
67+
onClose []closer,
68+
) (*Node, error) {
6269
var p2pSys immutable.Option[*p2p.P2P]
6370
if !o.DisableP2P {
6471
var host p2p.Host
@@ -85,7 +92,7 @@ func createNode(ctx context.Context, store store.TxnStore, o Options, onClose []
8592
}
8693
}
8794

88-
p2pSys = immutable.Some(p2p.New(host, o.Rootstore.Value(), o.IndexstoreNamespace.Value()))
95+
p2pSys = immutable.Some(p2p.New(host, repository, o.Rootstore.Value(), o.IndexstoreNamespace.Value()))
8996
}
9097

9198
return &Node{

host-go/node/node_p2p_free.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/sourcenetwork/immutable"
1414

1515
"github.com/sourcenetwork/lens/host-go/engine/module"
16+
"github.com/sourcenetwork/lens/host-go/repository"
1617
"github.com/sourcenetwork/lens/host-go/store"
1718
)
1819

@@ -46,7 +47,13 @@ func WithP2PDisabled(disableP2P bool) Option {
4647
}
4748
}
4849

49-
func createNode(ctx context.Context, store store.TxnStore, o Options, onClose []closer) (*Node, error) {
50+
func createNode(
51+
ctx context.Context,
52+
store store.TxnStore,
53+
repository repository.Repository,
54+
o Options,
55+
onClose []closer,
56+
) (*Node, error) {
5057
return &Node{
5158
onClose: onClose,
5259
Options: o,

host-go/p2p/p2p.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,28 @@ import (
1010
"github.com/ipfs/go-cid"
1111
"github.com/ipld/go-ipld-prime/linking"
1212
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
13-
"github.com/ipld/go-ipld-prime/node/bindnode"
1413
"github.com/sourcenetwork/corekv"
1514
"github.com/sourcenetwork/corekv/blockstore"
1615
"github.com/sourcenetwork/corekv/namespace"
16+
"github.com/sourcenetwork/lens/host-go/repository"
1717
"github.com/sourcenetwork/lens/host-go/store"
1818
)
1919

2020
type P2P struct {
2121
Host Host
22+
repository repository.Repository
2223
indexstore corekv.ReaderWriter
2324
}
2425

25-
func New(host Host, rootstore corekv.ReaderWriter, indexNamespace string) *P2P {
26+
func New(
27+
host Host,
28+
repository repository.Repository,
29+
rootstore corekv.ReaderWriter,
30+
indexNamespace string,
31+
) *P2P {
2632
return &P2P{
2733
Host: host,
34+
repository: repository,
2835
indexstore: namespace.Wrap(rootstore, []byte(indexNamespace)),
2936
}
3037
}
@@ -42,24 +49,14 @@ func (p *P2P) SyncLens(ctx context.Context, id string) error {
4249

4350
linkSys := makeLinkSystem(p.Host.IPLDStore())
4451

45-
configNode, err := linkSys.Load(linking.LinkContext{Ctx: ctx}, cidlink.Link{Cid: cid}, store.ConfigBlockSchemaPrototype)
52+
model, err := store.LoadLensModel(ctx, &linkSys, cid)
4653
if err != nil {
4754
return err
4855
}
4956

50-
configBlock := bindnode.Unwrap(configNode).(*store.ConfigBlock)
51-
52-
for _, moduleLink := range configBlock.Modules {
53-
moduleNode, err := linkSys.Load(linking.LinkContext{Ctx: ctx}, moduleLink, store.ModuleBlockSchemaPrototype)
54-
if err != nil {
55-
return err
56-
}
57-
moduleBlock := bindnode.Unwrap(moduleNode).(*store.ModuleBlock)
58-
59-
_, err = linkSys.Load(linking.LinkContext{Ctx: ctx}, moduleBlock.Lens, store.LensBlockSchemaPrototype)
60-
if err != nil {
61-
return err
62-
}
57+
err = p.repository.Add(ctx, id, model)
58+
if err != nil {
59+
return err
6360
}
6461

6562
// Store the top level index so that it may be fetched efficiently from the Store

host-go/store/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (b *ConfigBlock) generateNode() ipld.Node {
155155
return bindnode.Wrap(b, ConfigBlockSchema).Representation()
156156
}
157157

158-
func loadLensModel(ctx context.Context, linkSys *linking.LinkSystem, cid cid.Cid) (model.Lens, error) {
158+
func LoadLensModel(ctx context.Context, linkSys *linking.LinkSystem, cid cid.Cid) (model.Lens, error) {
159159
configNode, err := linkSys.Load(linking.LinkContext{Ctx: ctx}, cidlink.Link{Cid: cid}, ConfigBlockSchemaPrototype)
160160
if err != nil {
161161
return model.Lens{}, err

host-go/store/store.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ func New(
8989
}
9090
}
9191

92+
// NewWithRepository creates a new `TxnStore` using the given parameters.
93+
//
94+
// The blockstore namespace may safely be shared by other components if desired.
95+
func NewWithRepository(
96+
txnSource TxnSource,
97+
repository repository.TxnRepository,
98+
blockstoreNamespace string,
99+
blockstoreChunksize immutable.Option[int],
100+
indexstoreNamespace string,
101+
) TxnStore {
102+
return &implicitTxnStore{
103+
txnSource: txnSource,
104+
repository: repository,
105+
blockstoreNamespace: blockstoreNamespace,
106+
blockstoreChunksize: blockstoreChunksize,
107+
indexstoreNamespace: indexstoreNamespace,
108+
}
109+
}
110+
92111
func add(ctx context.Context, cfg model.Lens, txn *txn) (cid.Cid, error) {
93112
configLink, err := writeConfigBlock(ctx, txn.linkSystem, cfg)
94113
if err != nil {
@@ -142,7 +161,7 @@ func list(ctx context.Context, txn *txn) (map[cid.Cid]model.Lens, error) {
142161
return nil, errors.Join(err, iter.Close())
143162
}
144163

145-
config, err := loadLensModel(ctx, txn.linkSystem, configCID)
164+
config, err := LoadLensModel(ctx, txn.linkSystem, configCID)
146165
if err != nil {
147166
return nil, errors.Join(err, iter.Close())
148167
}

host-go/store/txn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ type repositoryTxnSource struct {
4040
var _ repository.TxnSource = (*repositoryTxnSource)(nil)
4141

4242
func (s *repositoryTxnSource) NewTxn(readonly bool) (repository.Txn, error) {
43-
return s.NewTxn(readonly)
43+
return s.src.NewTxn(readonly)
4444
}

0 commit comments

Comments
 (0)