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

Pins not working since Helia update from 2.3.0 to Helia 4.2.1 #571

Closed
Jollarvia opened this issue Jul 4, 2024 · 2 comments
Closed

Pins not working since Helia update from 2.3.0 to Helia 4.2.1 #571

Jollarvia opened this issue Jul 4, 2024 · 2 comments

Comments

@Jollarvia
Copy link

Jollarvia commented Jul 4, 2024

I have a test in my software that broke the moment I moved from 2.3.0 to 4.2.1. So I created a mocha test with pure Helia to illustrate my problem.

describe("pin|unpin|isPinned", function() {
        this.timeout(15000)
        it("should pin successfully", async() => {
            const { createHelia } = await import('helia')
            const helia = await createHelia({})
            const heliaJson = json(helia)
            const obj = {x:1, y:2}
            const cid = await heliaJson.add(obj)
            var pre = await helia.pins.add(cid)
            for (var x in pre){
                console.log(JSON.stringify(pre))
            }
            
            var result = await helia.pins.isPinned(cid)  
            expect(result).to.be.true   //<--- breaks because its not true
            await helia.stop()
        })
    })

The object is retrievable from the datastore. The generator recovered from the act of pinning ('variable name pre') does not have the passed in cid and ispinned always returns false. I would expect it to be true as it was in 2.3.0.

@acul71
Copy link
Contributor

acul71 commented Jul 31, 2024

The issue here is with how you're using the helia.pins.add method, which has changed in Helia 3.x.x.

Before:

const cid = CID.parse('QmFoo')
await helia.pin.add(cid)

After (with an AsyncGenerator):

const cid = CID.parse('QmFoo')

for await (const pinnedCid of helia.pins.add(cid)) {
  // ...
}

In your test, you're using the helia.pins.add method with a single parameter (cid) and then trying to iterate over the result using a for...in loop. However, since add now returns an AsyncGenerator, you should use a for await...of loop instead.

change this code:

var pre = await helia.pins.add(cid)
for (var x in pre){
    console.log(JSON.stringify(pre))
}

to

// Use the AsyncGenerator to pin the CID
for await (const pinnedCid of helia.pins.add(cid)) {
   console.log(`Pinned CID: ${pinnedCid}`);
}

It should work now!

@Jollarvia
Copy link
Author

Thanks @acul71 , that fixed it. Closing the issue.

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

2 participants