Skip to content

Commit

Permalink
Renew domain expiration time update (#1195)
Browse files Browse the repository at this point in the history
* update domain renew and update unit testing

* updated CHANGELOG.md
  • Loading branch information
fdymylja authored Apr 2, 2020
1 parent 488744a commit 6a4c371
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## HEAD
- `bnsd`: domain renew expiration time equals to domain expiration time + configuration domain renew duration, if this time is before current block time then renew time becomes current block time + configuration domain renew duration
- `bnsd`: revert burn feature
- `bnsd`: when a domain is transferred accounts ownership is transferred to the new domain owner and accounts' targets are cleared
- `bnsapi`: move bnsapi to new repo
Expand Down
29 changes: 16 additions & 13 deletions cmd/bnsd/x/account/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,23 +375,26 @@ func (h *renewDomainHandler) Deliver(ctx weave.Context, db weave.KVStore, tx wea
return nil, err
}

now, err := weave.BlockTime(ctx)
if err != nil {
return nil, errors.Wrap(errors.ErrState, "block time not present in context")
}
conf, err := loadConf(db)
if err != nil {
return nil, errors.Wrap(err, "cannot load configuration")
}
nextValidUntil := now.Add(conf.DomainRenew.Duration())
// ValidUntil time is only extended. We want to avoid the situation when
// the configuration is changed, limiting the expiration time period
// and renewing a domain by shortening its expiration date.
if nextValidUntil.After(domain.ValidUntil.Time()) {
domain.ValidUntil = weave.AsUnixTime(nextValidUntil)
if _, err := h.domains.Put(db, []byte(msg.Domain), domain); err != nil {
return nil, errors.Wrap(err, "cannot store domain")
}
// nextValidUntil is domain actual expiration date + configuration domain renew time
nextValidUntil := domain.ValidUntil.Add(conf.DomainRenew.Duration()).Time()
// get current block time
now, err := weave.BlockTime(ctx)
if err != nil {
return nil, errors.Wrap(err, "unable to get current block time")
}
// next valid until appears to be in the past then
// the new ValidUntil becomes now + DomainRenew duration
if now.After(nextValidUntil) {
nextValidUntil = now.Add(conf.DomainRenew.Duration())
}
// update domain
domain.ValidUntil = weave.AsUnixTime(nextValidUntil)
if _, err := h.domains.Put(db, []byte(msg.Domain), domain); err != nil {
return nil, errors.Wrap(err, "cannot store domain")
}
return &weave.DeliverResult{Data: nil}, nil
}
Expand Down
42 changes: 11 additions & 31 deletions cmd/bnsd/x/account/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1228,15 +1228,15 @@ func TestUseCases(t *testing.T) {
WantErr: nil,
},
{
Now: now + 2,
Now: now + 1,
Conditions: []weave.Condition{aliceCond},
Tx: &weavetest.Tx{
Msg: &RenewDomainMsg{
Metadata: &weave.Metadata{Schema: 1},
Domain: "wunderland",
},
},
BlockHeight: 102,
BlockHeight: 101,
WantErr: nil,
},
},
Expand All @@ -1246,15 +1246,15 @@ func TestUseCases(t *testing.T) {
if err := b.One(db, []byte("wunderland"), &d); err != nil {
t.Fatalf("cannot get wunderland domain: %s", err)
}
// Expiration time should be execution time
// (block time) which is now + 2, plus
// expiration offset.
if got, want := d.ValidUntil, weave.UnixTime(now+2+1000); want != got {
// expiration time should be old expiration time which is
// registration block time (now) + configuration domain renew (1000) + configuration domain renew(10000)
if got, want := d.ValidUntil, weave.UnixTime(now+1000+1000); want != got {
t.Fatalf("want valid till %s, got %s", want, got)
}
},
},
"renewing a domain does not shorten its expiration time": {
"if after renew domain is expired then domain valid until becomes now + conf domain renew": {
// create domain
Requests: []Request{
{
Now: now,
Expand All @@ -1271,28 +1271,9 @@ func TestUseCases(t *testing.T) {
BlockHeight: 100,
WantErr: nil,
},
// renew it after expiration (which is now + 1000) + next offset (1000)
{
Now: now + 1,
Conditions: []weave.Condition{adminCond},
Tx: &weavetest.Tx{
Msg: &UpdateConfigurationMsg{
Metadata: &weave.Metadata{Schema: 1},
Patch: &Configuration{
Metadata: &weave.Metadata{Schema: 1},
Owner: aliceCond.Address(),
ValidName: `^[a-z]+$`,
ValidDomain: `^[a-z]+$`,
ValidBlockchainID: `^[a-z]+$`,
ValidBlockchainAddress: `^[a-z]+$`,
DomainRenew: 1,
},
},
},
BlockHeight: 101,
WantErr: nil,
},
{
Now: now + 2,
Now: now + 2001,
Conditions: []weave.Condition{aliceCond},
Tx: &weavetest.Tx{
Msg: &RenewDomainMsg{
Expand All @@ -1310,9 +1291,8 @@ func TestUseCases(t *testing.T) {
if err := b.One(db, []byte("wunderland"), &d); err != nil {
t.Fatalf("cannot get wunderland domain: %s", err)
}
// Expiration time should not be updated
// because it would be shortened.
if got, want := d.ValidUntil, weave.UnixTime(now+1000); want != got {
// expiration time should be actual block time + 1000
if got, want := d.ValidUntil, weave.UnixTime(now+2001+1000); want != got {
t.Logf("want %d %s", want, want)
t.Logf(" got %d %s", got, got)
t.Fatal("unexpected valid till")
Expand Down

0 comments on commit 6a4c371

Please sign in to comment.