From c5e026c6e79e6be0b930dc0cabd8e67038946296 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 19 Oct 2021 13:04:10 +0300 Subject: [PATCH] [#151] netmap: allow to update config values in `_deploy` Signed-off-by: Evgenii Stratonikov --- netmap/netmap_contract.go | 32 ++++++++++++++++---------------- tests/netmap_test.go | 17 +++++++++++++++-- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/netmap/netmap_contract.go b/netmap/netmap_contract.go index a4c3675..4e34ccd 100644 --- a/netmap/netmap_contract.go +++ b/netmap/netmap_contract.go @@ -61,10 +61,6 @@ var ( func _deploy(data interface{}, isUpdate bool) { ctx := storage.GetContext() - if isUpdate { - return - } - args := data.([]interface{}) notaryDisabled := args[0].(bool) addrBalance := args[1].(interop.Hash160) @@ -72,6 +68,22 @@ func _deploy(data interface{}, isUpdate bool) { keys := args[3].([]interop.PublicKey) config := args[4].([][]byte) + ln := len(config) + if ln%2 != 0 { + panic("_deploy: bad configuration") + } + + for i := 0; i < ln/2; i++ { + key := config[i*2] + val := config[i*2+1] + + setConfig(ctx, key, val) + } + + if isUpdate { + return + } + if len(addrBalance) != 20 || len(addrContainer) != 20 { panic("init: incorrect length of contract script hash") } @@ -101,18 +113,6 @@ func _deploy(data interface{}, isUpdate bool) { runtime.Log("netmap contract notary disabled") } - ln := len(config) - if ln%2 != 0 { - panic("init: bad configuration") - } - - for i := 0; i < ln/2; i++ { - key := config[i*2] - val := config[i*2+1] - - setConfig(ctx, key, val) - } - runtime.Log("netmap contract initialized") } diff --git a/tests/netmap_test.go b/tests/netmap_test.go index 1841941..6f1cf25 100644 --- a/tests/netmap_test.go +++ b/tests/netmap_test.go @@ -6,9 +6,11 @@ import ( "testing" "github.com/nspcc-dev/neo-go/pkg/core" + "github.com/nspcc-dev/neo-go/pkg/encoding/bigint" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neo-go/pkg/wallet" + "github.com/nspcc-dev/neofs-contract/container" "github.com/stretchr/testify/require" ) @@ -24,7 +26,7 @@ func deployNetmapContract(t *testing.T, bc *core.Blockchain, addrBalance, addrCo return DeployContract(t, bc, netmapPath, args) } -func prepareNetmapContract(t *testing.T, bc *core.Blockchain) util.Uint160 { +func prepareNetmapContract(t *testing.T, bc *core.Blockchain, config ...interface{}) util.Uint160 { addrNNS := DeployContract(t, bc, nnsPath, nil) ctrNetmap, err := ContractInfo(CommitteeAcc.Contract.ScriptHash(), netmapPath) @@ -38,7 +40,18 @@ func prepareNetmapContract(t *testing.T, bc *core.Blockchain) util.Uint160 { deployContainerContract(t, bc, ctrNetmap.Hash, ctrBalance.Hash, addrNNS) deployBalanceContract(t, bc, ctrNetmap.Hash, ctrContainer.Hash) - return deployNetmapContract(t, bc, ctrBalance.Hash, ctrContainer.Hash, "ContainerFee", []byte{}) + return deployNetmapContract(t, bc, ctrBalance.Hash, ctrContainer.Hash, config...) +} + +func TestDeploySetConfig(t *testing.T) { + bc := NewChain(t) + h := prepareNetmapContract(t, bc, "SomeKey", "TheValue", container.AliasFeeKey, int64(123)) + + tx := PrepareInvoke(t, bc, CommitteeAcc, h, "config", "SomeKey") + CheckTestInvoke(t, bc, tx, "TheValue") + + tx = PrepareInvoke(t, bc, CommitteeAcc, h, "config", container.AliasFeeKey) + CheckTestInvoke(t, bc, tx, bigint.ToBytes(big.NewInt(123))) } func dummyNodeInfo(acc *wallet.Account) []byte {