Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to Index Documents with Custom IPFSBlockStorage (pure browser environment) #1180

Open
orwithout opened this issue May 7, 2024 · 0 comments

Comments

@orwithout
Copy link

orwithout commented May 7, 2024

Description
I am using @orbitdb/core with helia for managing IPFS nodes entirely within a browser environment. I encounter issues when attempting to index documents using custom IPFSBlockStorage.

Expected Behavior
Documents should be indexed and retrievable when using custom IPFSBlockStorage options.

Actual Behavior
Documents fail to be indexed or retrieved when IPFSBlockStorage configurations are applied. However, removing these configurations results in normal indexing and retrieval of documents.

Steps to Reproduce

  1. Configure @orbitdb/core with IPFSBlockStorage for entryStorage, headsStorage, and indexStorage.
  2. Add a document using the add method.
  3. Attempt to retrieve the document using the searchId method.

Additional Information

  • Browser version: Google Chrome 123.0.6312, Microsoft Edge 123.2420
  • Operating System: Windows 2022
  • @orbitdb/core Version: 2.2.0
  • Helia Version: 4.2.1

Relevant Code

  • js:
// documents_db.js
import { createHelia } from 'helia';
import { IDBBlockstore } from 'blockstore-idb';
import { IDBDatastore } from 'datastore-idb';
import { createOrbitDB , Documents, IPFSBlockStorage, IPFSAccessController } from '@orbitdb/core';
import { gossipsub } from "@chainsafe/libp2p-gossipsub";
import { identify } from "@libp2p/identify";

class DocumentStore {
  constructor() { this.init(); };

  async init() {
    this.datastore = new IDBDatastore('/datastore');
    this.blockstore = new IDBBlockstore('/blockstore');
    await this.datastore.open();
    await this.blockstore.open();
    this.helia = await createHelia({
      libp2p: {
        services: {
          pubsub: gossipsub({allowPublishToZeroTopicPeers: true}),
          identify: identify()
        }
      },
      keychain: { pass: 'very-strong-password' },
      datastore: this.datastore,
      blockstore: this.blockstore,
      start: true
    });

    console.log('Helia ID:', this.helia.libp2p.peerId.toString());
    setInterval(async () => {
        const peers = await this.helia.libp2p.peerStore.all();
        console.log(`Total number of store peers: ${peers.length}`);
        console.log('Connected peers count:', this.helia.libp2p.getPeers().length);
    }, 5000);

    this.orbitdb = await createOrbitDB({ ipfs: this.helia, directory: '/my_odb'});
    this.db = await  this.orbitdb.open(`${'my_documents_db'}`, {
        create: true,
        type: 'documents',
        Database: Documents({ indexBy: 'id'} ),
        AccessController: IPFSAccessController({ write: ['*'] }),
        entryStorage: await IPFSBlockStorage({ ipfs: this.helia, pin: true }),
        headsStorage: await IPFSBlockStorage({ ipfs: this.helia, pin: true }),
        indexStorage: await IPFSBlockStorage({ ipfs: this.helia, pin: true })
    });
    console.log('odb.address:', this.db.address);
  }

  async add(doc) { return await this.db.put(doc); }
  async searchId(ids) { return await this.db.query(doc => ids.includes(doc.id)); }
  async delete(_id) { return await this.db.del(_id); }
}

const documentStore = new DocumentStore();
export default documentStore;
  • html:
<!-- documents_odb.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"><title>FileIDB Module Test</title>
</head>
<body>
    <h1>Test FileIDB Module</h1>
    <lable></lable> <button id="addData">Add Sample Data</button><br><br>
    <lable></lable> <input type="text" id="searchQuery" placeholder="Search ID, example: 1"> <button id="searchData">Search Data</button>

    <script type="module">
        import documentStore from './documents_odb.js';

        document.getElementById('addData').addEventListener('click', async () => {
            const sampleData = {
                id: "123",
                type: "text/markdown",
                name: "Sample File",
                content: "This is a test file content.",
            };
            try {
            await documentStore.add(sampleData);
                console.log('Data added successfully:', sampleData);
            } catch (error) {
                console.error('Error adding data:', error);
            }
        });

        document.getElementById('searchData').addEventListener('click', async () => {
            const query = document.getElementById("searchQuery").value;
            try {
                const results = await documentStore.searchId(query);
                console.log('found: ',results);
            } catch (error) {
                console.log('found error: ',error.message);
            }
        });
    </script>
</body>
</html>

Console outputs
image

  • After removing these three lines:
// In the documents_db.js file: 
        entryStorage: await IPFSBlockStorage({ ipfs: this.helia, pin: true }),
        headsStorage: await IPFSBlockStorage({ ipfs: this.helia, pin: true }),
        indexStorage: await IPFSBlockStorage({ ipfs: this.helia, pin: true })

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant