nns: accept expiration period in renew

Port https://github.com/neo-project/non-native-contracts/pull/13.
This commit is contained in:
Anna Shaleva 2022-09-16 11:31:53 +03:00
parent dea82ac4dd
commit 9eed7c856e
3 changed files with 29 additions and 5 deletions

View file

@ -69,6 +69,8 @@ const (
millisecondsInSecond = 1000 millisecondsInSecond = 1000
// millisecondsInYear is amount of milliseconds per year. // millisecondsInYear is amount of milliseconds per year.
millisecondsInYear = 365 * 24 * 3600 * millisecondsInSecond millisecondsInYear = 365 * 24 * 3600 * millisecondsInSecond
// millisecondsInTenYears is the amount of milliseconds per ten years.
millisecondsInTenYears = 10 * millisecondsInYear
) )
// RecordState is a type that registered entities are saved to. // RecordState is a type that registered entities are saved to.
@ -413,8 +415,16 @@ func updateSoaSerial(ctx storage.Context, tokenId []byte) {
storage.Put(ctx, recordKey, recBytes) storage.Put(ctx, recordKey, recBytes)
} }
// Renew increases domain expiration date. // RenewDefault increases domain expiration date up to 1 year.
func Renew(name string) int { func RenewDefault(name string) int {
return Renew(name, 1)
}
// Renew increases domain expiration date up to the specified amount of years.
func Renew(name string, years int64) int {
if years < 1 || years > 10 {
panic("invalid renewal period value")
}
if len(name) > maxDomainNameLength { if len(name) > maxDomainNameLength {
panic("invalid domain name format") panic("invalid domain name format")
} }
@ -426,12 +436,15 @@ func Renew(name string) int {
if price < 0 { if price < 0 {
checkCommittee() checkCommittee()
} else { } else {
runtime.BurnGas(price) runtime.BurnGas(price * int(years))
} }
ctx := storage.GetContext() ctx := storage.GetContext()
ns := getNameState(ctx, []byte(name)) ns := getNameState(ctx, []byte(name))
ns.Expiration += millisecondsInYear ns.Expiration += int(millisecondsInYear * years)
if ns.Expiration > int(runtime.GetTime())+millisecondsInTenYears {
panic("10 years of expiration period at max is allowed")
}
putNameState(ctx, ns) putNameState(ctx, ns)
return ns.Expiration return ns.Expiration
} }

View file

@ -19,3 +19,5 @@ permissions:
- hash: fffdc93764dbaddd97c48f252a53ea4643faa3fd - hash: fffdc93764dbaddd97c48f252a53ea4643faa3fd
methods: ["update"] methods: ["update"]
- methods: ["onNEP11Payment"] - methods: ["onNEP11Payment"]
overloads:
renewDefault: renew

View file

@ -158,7 +158,7 @@ func TestExpiration(t *testing.T) {
func TestRegisterAndRenew(t *testing.T) { func TestRegisterAndRenew(t *testing.T) {
c := newNSClient(t, false) c := newNSClient(t, false)
e := c.Executor e := c.Executor
mail, refresh, retry, expire, ttl := "sami@nspcc.ru", int64(101), int64(102), int64(millisecondsInYear/1000*100), int64(104) mail, refresh, retry, expire, ttl := "sami@nspcc.ru", int64(101), int64(102), int64(millisecondsInYear/1000*2), int64(104)
c.InvokeFail(t, "TLD not found", "isAvailable", "neo-go.com") c.InvokeFail(t, "TLD not found", "isAvailable", "neo-go.com")
c.Invoke(t, true, "register", "org", c.CommitteeHash, mail, refresh, retry, expire, ttl) c.Invoke(t, true, "register", "org", c.CommitteeHash, mail, refresh, retry, expire, ttl)
@ -214,6 +214,15 @@ func TestRegisterAndRenew(t *testing.T) {
props.Add(stackitem.Make("expiration"), stackitem.Make(expectedExpiration)) props.Add(stackitem.Make("expiration"), stackitem.Make(expectedExpiration))
c.Invoke(t, props, "properties", "neo-go.com") c.Invoke(t, props, "properties", "neo-go.com")
// Invalid renewal period.
c.InvokeFail(t, "invalid renewal period value", "renew", "neo-go.com", 11)
// Too large expiration period.
c.InvokeFail(t, "10 years of expiration period at max is allowed", "renew", "neo-go.com", 10)
// Non-default renewal period.
mult := 2
c.Invoke(t, expectedExpiration+uint64(mult*millisecondsInYear), "renew", "neo-go.com", mult)
} }
func TestSetAddGetRecord(t *testing.T) { func TestSetAddGetRecord(t *testing.T) {