From 9eed7c856e0e4ff21fd46d30bfca13d52a83c9d3 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 16 Sep 2022 11:31:53 +0300 Subject: [PATCH] nns: accept expiration period in `renew` Port https://github.com/neo-project/non-native-contracts/pull/13. --- examples/nft-nd-nns/nns.go | 21 +++++++++++++++++---- examples/nft-nd-nns/nns.yml | 2 ++ examples/nft-nd-nns/nns_test.go | 11 ++++++++++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/examples/nft-nd-nns/nns.go b/examples/nft-nd-nns/nns.go index c4a8f40ee..e83454d49 100644 --- a/examples/nft-nd-nns/nns.go +++ b/examples/nft-nd-nns/nns.go @@ -69,6 +69,8 @@ const ( millisecondsInSecond = 1000 // millisecondsInYear is amount of milliseconds per year. 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. @@ -413,8 +415,16 @@ func updateSoaSerial(ctx storage.Context, tokenId []byte) { storage.Put(ctx, recordKey, recBytes) } -// Renew increases domain expiration date. -func Renew(name string) int { +// RenewDefault increases domain expiration date up to 1 year. +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 { panic("invalid domain name format") } @@ -426,12 +436,15 @@ func Renew(name string) int { if price < 0 { checkCommittee() } else { - runtime.BurnGas(price) + runtime.BurnGas(price * int(years)) } ctx := storage.GetContext() 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) return ns.Expiration } diff --git a/examples/nft-nd-nns/nns.yml b/examples/nft-nd-nns/nns.yml index 4c2508172..2dc880ca2 100644 --- a/examples/nft-nd-nns/nns.yml +++ b/examples/nft-nd-nns/nns.yml @@ -19,3 +19,5 @@ permissions: - hash: fffdc93764dbaddd97c48f252a53ea4643faa3fd methods: ["update"] - methods: ["onNEP11Payment"] +overloads: + renewDefault: renew diff --git a/examples/nft-nd-nns/nns_test.go b/examples/nft-nd-nns/nns_test.go index 4b2d10534..d03ce9c30 100644 --- a/examples/nft-nd-nns/nns_test.go +++ b/examples/nft-nd-nns/nns_test.go @@ -158,7 +158,7 @@ func TestExpiration(t *testing.T) { func TestRegisterAndRenew(t *testing.T) { c := newNSClient(t, false) 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.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)) 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) {