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

ipfs name publish doesn't take the TTL into account when caching locally #6670

Closed
Stebalien opened this issue Sep 23, 2019 · 1 comment · Fixed by #6671
Closed

ipfs name publish doesn't take the TTL into account when caching locally #6670

Stebalien opened this issue Sep 23, 2019 · 1 comment · Fixed by #6671
Labels
kind/bug A bug in existing code (including security flaws)

Comments

@Stebalien
Copy link
Member

Version information:

go-ipfs version: 0.5.0-dev
Repo version: 7
System version: amd64/linux
Golang version: go1.12.9

Description:

From ipfs/notes#391 (comment) by @postables:

I've been doing some testing on the fix for #6656, and it doesn't seem like its working. For whatever reason the ttl value in the context seems to be getting overwritten somewhere.

If you modify the cacheEntry to also include a ttl, and run the following test:

func Test_PublishWithEOL(t *testing.T) {
	ds := dssync.MutexWrap(ds.NewMapDatastore())
	ps := pstoremem.NewPeerstore()
	routing := offroute.NewOfflineRouter(
		ds,
		record.NamespacedValidator{
			"ipns": ipns.Validator{KeyBook: ps},
			"pk":   record.PublicKeyValidator{},
		},
	)
	ns := newMPNS(t, routing, ds, 128)

	p, err := path.ParsePath(unixfs.EmptyDirNode().Cid().String())
	if err != nil {
		t.Fatal(err)
	}
	type args struct {
		ttl time.Duration
		eol time.Time
	}
	tests := []struct {
		name    string
		args    args
		wantTTL time.Duration
	}{
		{"TTL-Set", args{
			ttl: time.Hour,
			eol: time.Now().Add(time.Hour),
		}, time.Hour},
		{"TTL-Not-Set", args{
			ttl: 0,
			eol: time.Now().Add(time.Hour),
		}, time.Minute},
		{"EOL-Short", args{
			ttl: 0,
			eol: time.Now().Add(time.Second * 10), // shorter than default resolve time
		}, time.Second * 10},
	}
	type contextKey string

	const (
		ipnsPublishTTL contextKey = "ipns-publish-ttl"
	)

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var ctx context.Context
			if tt.args.ttl > 0 {
				ctx = context.WithValue(
					context.Background(),
					ipnsPublishTTL,
					tt.args.ttl,
				)
				tl, ok := checkCtxTTL(ctx)
				fmt.Println(ok)
				fmt.Println(tl)
			} else {
				ctx = context.Background()
			}
			pk, _, err := ci.GenerateKeyPair(ci.Ed25519, 256)
			if err != nil {
				t.Fatal(err)
			}
			pid, err := peer.IDFromPrivateKey(pk)
			if err != nil {
				t.Fatal(err)
			}
			if err := ps.AddPrivKey(pid, pk); err != nil {
				t.Fatal(err)
			}
			if err := ns.PublishWithEOL(ctx, pk, p, tt.args.eol); err != nil {
				t.Fatal(err)
			}
			ientry, ok := ns.cache.Get(peer.IDB58Encode(pid))
			if !ok {
				t.Fatal("cache get failed")
			}
			entry, ok := ientry.(cacheEntry)
			if !ok {
				t.Fatal("bad cache item returned")
			}
			if entry.ttl != tt.wantTTL {
				fmt.Println(entry.ttl)
				t.Fatal("bad cache ttl")
			}
		})
	}
}

func newMPNS(t *testing.T, r routing.ValueStore, ds ds.Datastore, cachesize int) *MPNS {
	if cachesize == 0 {
		cachesize = DefaultResolverCacheSize
	}
	cache, err := lru.New(cachesize)
	if err != nil {
		t.Fatal(err)
	}
	return &MPNS{
		dnsResolver:      NewDNSResolver(),
		proquintResolver: new(ProquintResolver),
		ipnsResolver:     NewIpnsResolver(r),
		ipnsPublisher:    NewIpnsPublisher(r, ds),
		cache:            cache,
	}
}

it will fail on the test named TTL-Set which is the test which derives the ttl value through the passed in context value

@Stebalien Stebalien added the kind/bug A bug in existing code (including security flaws) label Sep 23, 2019
Stebalien added a commit that referenced this issue Sep 24, 2019
No fix is complete without a test.

fixes #6670
@Stebalien
Copy link
Member Author

I can't reproduce but I've written a regression test for this: #6671.

Stebalien added a commit that referenced this issue Jan 17, 2020
No fix is complete without a test.

fixes #6670
Stebalien added a commit that referenced this issue Jan 17, 2020
No fix is complete without a test.

fixes #6670
ralendor pushed a commit to ralendor/go-ipfs that referenced this issue Jun 6, 2020
No fix is complete without a test.

fixes ipfs#6670
ralendor pushed a commit to ralendor/go-ipfs that referenced this issue Jun 8, 2020
No fix is complete without a test.

fixes ipfs#6670
ralendor pushed a commit to ralendor/go-ipfs that referenced this issue Jun 8, 2020
No fix is complete without a test.

fixes ipfs#6670
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug A bug in existing code (including security flaws)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant