Skip to content

Commit

Permalink
nns: Add Renew event
Browse files Browse the repository at this point in the history
Port neo-project/non-native-contracts#31.

Signed-off-by: Anna Shaleva <[email protected]>
  • Loading branch information
AnnaShaleva authored and roman-khimov committed Oct 31, 2024
1 parent 99ae3c8 commit ddf8767
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
8 changes: 8 additions & 0 deletions contracts/nns/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ events:
type: Hash160
- name: newAdmin
type: Hash160
- name: Renew
parameters:
- name: name
type: String
- name: oldExpiration
type: Integer
- name: newExpiration
type: Integer
permissions:
- hash: fffdc93764dbaddd97c48f252a53ea4643faa3fd
methods: ["update"]
Expand Down
2 changes: 2 additions & 0 deletions contracts/nns/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ func Renew(name string, years int) int64 {
ctx := storage.GetContext()
ns := getNameState(ctx, []byte(name))
ns.checkAdmin()
oldExpiration := ns.Expiration
ns.Expiration += millisecondsInYear * int64(years)

fragments := splitAndCheck(name)
Expand All @@ -488,6 +489,7 @@ func Renew(name string, years int) int64 {
panic("10 years of expiration period at max is allowed")
}
putNameState(ctx, ns)
runtime.Notify("Renew", name, oldExpiration, ns.Expiration)
return ns.Expiration
}

Expand Down
25 changes: 22 additions & 3 deletions tests/nns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,21 @@ func TestNNSRenew(t *testing.T) {
const msPerYear = 365 * 24 * time.Hour / time.Millisecond
b := c.TopBlock(t)
renewalPeriod := int64(2)
ts := b.Timestamp + uint64(expire*1000) + uint64(msPerYear)*uint64(renewalPeriod)
oldExpiration := b.Timestamp + uint64(expire*1000)
ts := oldExpiration + uint64(msPerYear)*uint64(renewalPeriod)

cAcc := c.WithSigners(acc)
cAcc.InvokeFail(t, "not witnessed by admin", "renew", "testdomain.com", renewalPeriod)
c1.Invoke(t, ts, "renew", "testdomain.com", renewalPeriod)
h := c1.Invoke(t, ts, "renew", "testdomain.com", renewalPeriod)
cAcc.CheckTxNotificationEvent(t, h, 0, state.NotificationEvent{
ScriptHash: cAcc.Hash,
Name: "Renew",
Item: stackitem.NewArray([]stackitem.Item{
stackitem.NewByteArray([]byte("testdomain.com")),
stackitem.Make(oldExpiration),
stackitem.Make(ts),
}),
})
expected := stackitem.NewMapWithValue([]stackitem.MapElement{
{Key: stackitem.Make("name"), Value: stackitem.Make("testdomain.com")},
{Key: stackitem.Make("expiration"), Value: stackitem.Make(ts)},
Expand All @@ -429,7 +439,16 @@ func TestNNSRenew(t *testing.T) {
c1.InvokeFail(t, "10 years of expiration period at max is allowed", "renew", "testdomain.com", 10)

// Default renewal period.
c1.Invoke(t, ts+uint64(msPerYear), "renew", "testdomain.com")
h = c1.Invoke(t, ts+uint64(msPerYear), "renew", "testdomain.com")
c1.CheckTxNotificationEvent(t, h, 0, state.NotificationEvent{
ScriptHash: cAcc.Hash,
Name: "Renew",
Item: stackitem.NewArray([]stackitem.Item{
stackitem.NewByteArray([]byte("testdomain.com")),
stackitem.Make(ts),
stackitem.Make(ts + uint64(msPerYear)),
}),
})
}

func TestNNSResolve(t *testing.T) {
Expand Down

0 comments on commit ddf8767

Please sign in to comment.