Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit a22744d

Browse files
committed
[#1800] Clean all IPNS records from IPFS datastore cache
1 parent 8b10a88 commit a22744d

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

repo/migration.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var (
4646
migrations.Migration023{},
4747
migrations.Migration024{},
4848
migrations.Migration025{},
49+
migrations.Migration026{},
4950
}
5051
)
5152

repo/migrations/Migration026.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package migrations
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/ipfs/go-ipfs/repo/fsrepo"
7+
ds "gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore"
8+
dsq "gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore/query"
9+
ipnspb "gx/ipfs/QmUwMnKKjH3JwGKNVZ3TcP37W93xzqNA4ECFFiMo6sXkkc/go-ipns/pb"
10+
"gx/ipfs/QmddjPSGZb3ieihSseFeCfVRpZzcqczPNsD2DvarSwnjJB/gogo-protobuf/proto"
11+
)
12+
13+
var cleanIPNSMigrationNumber = 26 // should match name
14+
15+
type (
16+
cleanIPNSRecordsFromDatastore struct{}
17+
Migration026 struct {
18+
cleanIPNSRecordsFromDatastore
19+
}
20+
)
21+
22+
// Should we ever update these packages (which functionally changes their
23+
// behavior) the migrations should be made into a no-op.
24+
func (cleanIPNSRecordsFromDatastore) Up(repoPath, databasePassword string, testnetEnabled bool) error {
25+
r, err := fsrepo.Open(repoPath)
26+
if err != nil {
27+
return err
28+
}
29+
defer func() {
30+
if err := r.Close(); err != nil {
31+
log.Errorf("closing repo: %s", err.Error())
32+
}
33+
}()
34+
35+
resultCh, err := r.Datastore().Query(dsq.Query{Prefix: "/ipns/"})
36+
if err != nil {
37+
return err
38+
}
39+
defer func() {
40+
if err := resultCh.Close(); err != nil {
41+
log.Errorf("closing result channel: %s", err.Error())
42+
}
43+
}()
44+
45+
results, err := resultCh.Rest()
46+
if err != nil {
47+
return err
48+
}
49+
50+
log.Debugf("found %d IPNS records to cull...", len(results))
51+
for _, rawResult := range results {
52+
rec := new(ipnspb.IpnsEntry)
53+
if err = proto.Unmarshal(rawResult.Value, rec); err != nil {
54+
log.Warningf("failed unmarshaling record (%s): %s", rawResult.Key, err.Error())
55+
if err := r.Datastore().Delete(ds.NewKey(rawResult.Key)); err != nil {
56+
log.Errorf("failed dropping cached record (%s): %s", rawResult.Key, err.Error())
57+
}
58+
}
59+
}
60+
61+
if err := writeRepoVer(repoPath, cleanIPNSMigrationNumber+1); err != nil {
62+
return fmt.Errorf("updating repover to %d: %s", cleanIPNSMigrationNumber+1, err.Error())
63+
}
64+
return nil
65+
}
66+
67+
func (cleanIPNSRecordsFromDatastore) Down(repoPath, databasePassword string, testnetEnabled bool) error {
68+
if err := writeRepoVer(repoPath, cleanIPNSMigrationNumber); err != nil {
69+
return fmt.Errorf("updating repover to %d: %s", cleanIPNSMigrationNumber, err.Error())
70+
}
71+
return nil
72+
}

repo/migrations/Migration026_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package migrations
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/OpenBazaar/openbazaar-go/schema"
8+
"github.com/ipfs/go-ipfs/repo/fsrepo"
9+
10+
ds "gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore"
11+
)
12+
13+
func TestCleanIPNSRecordsMigration(t *testing.T) {
14+
var (
15+
basePath = schema.GenerateTempPath()
16+
ipnsKey = "/ipns/shoulddelete"
17+
otherKey = "/ipfs/shouldNOTdelete"
18+
migration = cleanIPNSRecordsFromDatastore{}
19+
20+
testRepoPath, err = schema.OpenbazaarPathTransform(basePath, true)
21+
)
22+
if err != nil {
23+
t.Fatal(err)
24+
}
25+
26+
appSchema, err := schema.NewCustomSchemaManager(schema.SchemaContext{DataPath: testRepoPath, TestModeEnabled: true})
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
31+
if err = appSchema.BuildSchemaDirectories(); err != nil {
32+
t.Fatal(err)
33+
}
34+
defer appSchema.DestroySchemaDirectories()
35+
36+
if err := fsrepo.Init(appSchema.DataPath(), schema.MustDefaultConfig()); err != nil {
37+
t.Fatal(err)
38+
}
39+
40+
r, err := fsrepo.Open(testRepoPath)
41+
if err != nil {
42+
t.Fatalf("opening repo: %s", err.Error())
43+
}
44+
45+
err = r.Datastore().Put(ds.NewKey(ipnsKey), []byte("randomdata"))
46+
if err != nil {
47+
t.Fatal("unable to put ipns record")
48+
}
49+
err = r.Datastore().Put(ds.NewKey(otherKey), []byte("randomdata"))
50+
if err != nil {
51+
t.Fatal("unable to put other record")
52+
}
53+
54+
// run migration up
55+
if err := migration.Up(appSchema.DataPath(), "", true); err != nil {
56+
t.Fatal(err)
57+
}
58+
59+
// validate state
60+
if _, err := r.Datastore().Get(ds.NewKey(ipnsKey)); err != ds.ErrNotFound {
61+
t.Errorf("expected the IPNS record to be removed, but was not")
62+
}
63+
if val, err := r.Datastore().Get(ds.NewKey(otherKey)); err != nil {
64+
t.Errorf("expected the other record to be present, but was not")
65+
} else {
66+
if !bytes.Equal([]byte("randomdata"), val) {
67+
t.Errorf("expected the other record data to be intact, but was not")
68+
}
69+
}
70+
71+
if err = appSchema.VerifySchemaVersion("27"); err != nil {
72+
t.Fatal(err)
73+
}
74+
75+
// run migration down
76+
if err := migration.Down(appSchema.DataPath(), "", true); err != nil {
77+
t.Fatal(err)
78+
}
79+
80+
// validate state
81+
if err = appSchema.VerifySchemaVersion("26"); err != nil {
82+
t.Fatal(err)
83+
}
84+
}

repo/migrations/log.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package migrations
2+
3+
import logging "github.com/op/go-logging"
4+
5+
var log = logging.MustGetLogger("migrations")

0 commit comments

Comments
 (0)