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

Drop deploy configs #373

Merged
merged 15 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions alphabet/alphabet_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ func _deploy(data any, isUpdate bool) {

common.CheckVersion(version)

if args[0].(bool) {
panic("update to non-notary mode is not supported anymore")
}

// switch to notary mode if version of the current contract deployment is
// earlier than v0.17.0 (initial version when non-notary mode was taken out of
// use)
Expand All @@ -56,20 +52,19 @@ func _deploy(data any, isUpdate bool) {
}

args := data.(struct {
notaryDisabled bool
addrNetmap interop.Hash160
addrProxy interop.Hash160
name string
index int
total int
_ bool // notaryDisabled
addrNetmap interop.Hash160
addrProxy interop.Hash160
name string
index int
total int
})

if args.notaryDisabled {
panic("non-notary mode is not supported anymore")
if len(args.addrNetmap) != interop.Hash160Len {
args.addrNetmap = common.ResolveFSContract("netmap")
}

if len(args.addrNetmap) != interop.Hash160Len || len(args.addrProxy) != interop.Hash160Len {
panic("incorrect length of contract script hash")
if len(args.addrProxy) != interop.Hash160Len {
args.addrProxy = common.ResolveFSContract("proxy")
}

storage.Put(ctx, netmapKey, args.addrNetmap)
Expand Down Expand Up @@ -110,7 +105,7 @@ func switchToNotary(ctx storage.Context, args []any) {
panic("address of the Proxy contract is missing or invalid")
}
} else {
proxyContract = common.ResolveContractHash("proxy")
proxyContract = common.ResolveFSContract("proxy")
}

if !common.TryPurgeVotes(ctx) {
Expand Down
3 changes: 0 additions & 3 deletions alphabet/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ func testMigrationFromDump(t *testing.T, d *dump.Reader) {
0, // unused
}

c.CheckUpdateFail(t, "update to non-notary mode is not supported anymore",
replaceArgI(updPrm, 0, true)...)

if notaryDisabled {
c.CheckUpdateFail(t, "address of the Proxy contract is missing or invalid",
replaceArgI(updPrm, 2, make([]byte, interop.Hash160Len+1))...)
Expand Down
4 changes: 0 additions & 4 deletions audit/audit_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ func _deploy(data any, isUpdate bool) {

common.CheckVersion(version)

if args[0].(bool) {
panic("update to non-notary mode is not supported anymore")
}

// switch to notary mode if version of the current contract deployment is
// earlier than v0.17.0 (initial version when non-notary mode was taken out of
// use)
Expand Down
4 changes: 1 addition & 3 deletions audit/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ func testMigrationFromDump(t *testing.T, d *dump.Reader) {
prevAuditResults := readAllAuditResults()

// try to update the contract
var notary bool
c.CheckUpdateFail(t, "update to non-notary mode is not supported anymore", !notary)
c.CheckUpdateSuccess(t, notary)
c.CheckUpdateSuccess(t)

// check that contract was updates as expected
newAuditResults := readAllAuditResults()
Expand Down
4 changes: 0 additions & 4 deletions balance/balance_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ func _deploy(data any, isUpdate bool) {

common.CheckVersion(version)

if args[0].(bool) {
panic("update to non-notary mode is not supported anymore")
}

// switch to notary mode if version of the current contract deployment is
// earlier than v0.17.0 (initial version when non-notary mode was taken out of
// use)
Expand Down
7 changes: 2 additions & 5 deletions balance/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,12 @@ func testMigrationFromDump(t *testing.T, d *dump.Reader) {
prevTotalSupply := readTotalSupply()

// try to update the contract
var notary bool
c.CheckUpdateFail(t, "update to non-notary mode is not supported anymore", !notary)

if notaryDisabled && prevPendingVotes {
c.CheckUpdateFail(t, "pending vote detected", notary)
c.CheckUpdateFail(t, "pending vote detected")
return
}

c.CheckUpdateSuccess(t, notary)
c.CheckUpdateSuccess(t)

// check that contract was updates as expected
newTotalSupply := readTotalSupply()
Expand Down
2 changes: 1 addition & 1 deletion common/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ import (
// a successive call is not guaranteed.
// Caller must have `NewEpoch` method with a single numeric argument.
func SubscribeForNewEpoch() {
netmapContract := ResolveContractHash("netmap")
netmapContract := ResolveFSContract("netmap")
contract.Call(netmapContract, "subscribeForNewEpoch", contract.All, runtime.GetExecutingScriptHash())
}
58 changes: 35 additions & 23 deletions common/nns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,53 @@ package common
import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/convert"
"github.com/nspcc-dev/neo-go/pkg/interop/lib/address"
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
)

// ResolveContractHash resolves contract hash by its well-known NeoFS name.
// Contract name should be lowercased, should not include `.neofs` TLD. Example
// values: "netmap", "container", etc.
// Relies on some NeoFS specifics:
// 1. NNS contract should be deployed first (and should have `1` contract ID)
// 2. It should be prefilled with contract hashes by their names (no
// capitalized chars; no `neofs` TLD)
func ResolveContractHash(contractName string) interop.Hash160 {
// get NNS contract (it always has ID=1 in the NeoFS Sidechain)
nnsContract := management.GetContractByID(1)
if nnsContract == nil {
panic("missing NNS contract")
// NNSID is the ID of the NNS contract in NeoFS networks. It's always deployed
// first.
const NNSID = 1

// ContractTLD is the default domain used by NeoFS contracts.
const ContractTLD = "neofs"

// InferNNSHash returns NNS contract hash by [NNSID] or panics if
// it can't be resolved.
func InferNNSHash() interop.Hash160 {
var nns = management.GetContractByID(NNSID)
if nns == nil {
panic("no NNS contract")
}
return nns.Hash
}

resResolve := contract.Call(nnsContract.Hash, "resolve", contract.ReadOnly,
contractName+".neofs", 16, // TXT
)
// ResolveFSContract returns contract hash by name as registered in NNS or
// panics if it can't be resolved. It's similar to [ResolveFSContractWithNNS],
// but retrieves NNS hash automatically (see [InferNNSHash]).
func ResolveFSContract(name string) interop.Hash160 {
return ResolveFSContractWithNNS(InferNNSHash(), name)
}

// ResolveFSContractWithNNS uses given NNS contract and returns target contract
// hash by name as registered in NNS (assuming NeoFS-specific NNS setup, see
// [NNSID]) or panics if it can't be resolved. Contract name should be
// lowercased and should not include [ContractTLD]. Example values: "netmap",
// "container", etc.
func ResolveFSContractWithNNS(nns interop.Hash160, contractName string) interop.Hash160 {
resResolve := contract.Call(nns, "resolve", contract.ReadOnly, contractName+"."+ContractTLD, 16 /*TXT*/)
records := resResolve.([]string)
if len(records) == 0 {
panic("did not find a record of the " + contractName + " contract in the NNS")
}

var hash interop.Hash160
if len(records[0]) == 2*interop.Hash160Len {
hash = convert.ToBytes(std.Atoi(records[0], 16))
} else {
hash = address.ToHash160(records[0])
var h = make([]byte, interop.Hash160Len)
for i := 0; i < interop.Hash160Len; i++ {
ii := (interop.Hash160Len - i - 1) * 2
h[i] = byte(std.Atoi(records[0][ii:ii+2], 16))
}
return h
}

return hash
return address.ToHash160(records[0])
}
69 changes: 44 additions & 25 deletions container/container_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ const (
// AliasFeeKey is a key in netmap config which contains fee for nice-name registration.
AliasFeeKey = "ContainerAliasFee"

// nolint:deadcode,unused
nnsDefaultTLD = "container"

// V2 format
containerIDSize = interop.Hash256Len // SHA256 size

Expand Down Expand Up @@ -101,15 +104,11 @@ func OnNEP11Payment(a interop.Hash160, b int, c []byte, d any) {
// nolint:deadcode,unused
func _deploy(data any, isUpdate bool) {
ctx := storage.GetContext()
args := data.([]any)
if isUpdate {
args := data.([]any)
version := args[len(args)-1].(int)
common.CheckVersion(version)

if args[0].(bool) {
panic("update to non-notary mode is not supported anymore")
}

it := storage.Find(ctx, []byte{}, storage.None)
for iterator.Next(it) {
item := iterator.Value(it).(struct {
Expand Down Expand Up @@ -141,33 +140,53 @@ func _deploy(data any, isUpdate bool) {
return
}

args := data.(struct {
notaryDisabled bool
addrNetmap interop.Hash160
addrBalance interop.Hash160
addrID interop.Hash160
addrNNS interop.Hash160
nnsRoot string
})
var (
addrNetmap interop.Hash160
addrBalance interop.Hash160
addrID interop.Hash160
addrNNS interop.Hash160
nnsRoot string
)
// args[0] is notaryDisabled flag

if args.notaryDisabled {
panic("non-notary mode is not supported anymore")
// Do this out of order since NNS has to be present anyway and it can
// be used to resolve other contracts.
if len(args) >= 5 && len(args[4].(interop.Hash160)) == interop.Hash160Len {
addrNNS = args[4].(interop.Hash160)
} else {
addrNNS = common.InferNNSHash()
}

if len(args.addrNetmap) != interop.Hash160Len ||
len(args.addrBalance) != interop.Hash160Len ||
len(args.addrID) != interop.Hash160Len {
panic("incorrect length of contract script hash")
if len(args) >= 2 && len(args[1].(interop.Hash160)) == interop.Hash160Len {
addrNetmap = args[1].(interop.Hash160)
} else {
addrNetmap = common.ResolveFSContractWithNNS(addrNNS, "netmap")
}

if len(args) >= 3 && len(args[2].(interop.Hash160)) == interop.Hash160Len {
addrBalance = args[2].(interop.Hash160)
} else {
addrBalance = common.ResolveFSContractWithNNS(addrNNS, "balance")
}
if len(args) >= 4 && len(args[3].(interop.Hash160)) == interop.Hash160Len {
addrID = args[3].(interop.Hash160)
} else {
addrID = common.ResolveFSContractWithNNS(addrNNS, "neofsid")
}
if len(args) >= 6 && len(args[5].(string)) > 0 {
nnsRoot = args[5].(string)
} else {
nnsRoot = nnsDefaultTLD
}

storage.Put(ctx, netmapContractKey, args.addrNetmap)
storage.Put(ctx, balanceContractKey, args.addrBalance)
storage.Put(ctx, neofsIDContractKey, args.addrID)
storage.Put(ctx, nnsContractKey, args.addrNNS)
storage.Put(ctx, nnsRootKey, args.nnsRoot)
storage.Put(ctx, netmapContractKey, addrNetmap)
storage.Put(ctx, balanceContractKey, addrBalance)
storage.Put(ctx, neofsIDContractKey, addrID)
storage.Put(ctx, nnsContractKey, addrNNS)
storage.Put(ctx, nnsRootKey, nnsRoot)

// add NNS root for container alias domains
registerNiceNameTLD(args.addrNNS, args.nnsRoot)
registerNiceNameTLD(addrNNS, nnsRoot)

common.SubscribeForNewEpoch()

Expand Down
3 changes: 2 additions & 1 deletion container/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ presented in the NeoFS network for which the contract is deployed. For
performance optimization, container are additionally indexed by their owners.

# NNS
Contract tracks container-related domains registered in the NNS.
Contract tracks container-related domains registered in the NNS. By default
"container" TLD is used (unless overridden on deploy).

# Size estimations
Contract stores containers' size estimations came from NeoFS storage nodes.
Expand Down
7 changes: 2 additions & 5 deletions container/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,12 @@ func testMigrationFromDump(t *testing.T, d *dump.Reader) {
prevOwnersToContainers := readOwnersToContainers()

// try to update the contract
var notary bool
c.CheckUpdateFail(t, "update to non-notary mode is not supported anymore", !notary)

if notaryDisabled && prevPendingVote {
c.CheckUpdateFail(t, "pending vote detected", notary)
c.CheckUpdateFail(t, "pending vote detected")
return
}

c.CheckUpdateSuccess(t, notary)
c.CheckUpdateSuccess(t)

// check that contract was updates as expected
newPendingVote := readPendingVotes()
Expand Down
8 changes: 3 additions & 5 deletions neofsid/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,20 @@ func testMigrationFromDump(t *testing.T, d *dump.Reader) {
prevOwnersToKeys := readOwnersToKeys()

// try to update the contract
var notary bool
c.CheckUpdateFail(t, "update to non-notary mode is not supported anymore", !notary)

if notaryDisabled && prevPendingVote {
c.CheckUpdateFail(t, "pending vote detected", notary)
c.CheckUpdateFail(t, "pending vote detected")
return
}

c.CheckUpdateSuccess(t, notary)
c.CheckUpdateSuccess(t)

// check that contract was updates as expected
newPendingVotes := readPendingVotes()
newOwnersToKeys := readOwnersToKeys()

require.Nil(t, c.GetStorageItem(notaryDisabledKey), "notary flag should be removed")
require.Nil(t, c.GetStorageItem([]byte("containerScriptHash")), "Container contract address should be removed")
require.Nil(t, c.GetStorageItem([]byte("netmapScriptHash")), "Netmap contract address should be removed")
require.False(t, newPendingVotes, "there should be no more pending votes")

require.Equal(t, len(prevOwnersToKeys), len(newOwnersToKeys))
Expand Down
Loading
Loading