Skip to content

Commit

Permalink
deploy: Fix witnesses for Balance and Container contract deployments
Browse files Browse the repository at this point in the history
To deploy these contracts in `@v0.19.1` revision:
 - both must be witnessed by the Alphabet (2/3n+1) to call Netmap
 - Container must be also witnessed by the committee majority (n/2+1) to
  call NNS

Signed-off-by: Leonard Lyubich <[email protected]>
  • Loading branch information
cthulhu-rider committed Dec 20, 2023
1 parent 5c66479 commit daa6344
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changelog for NeoFS Node
### Added

### Fixed
- Auto-deployment of the Balance and Container contracts (#2695)

### Changed

Expand Down
64 changes: 54 additions & 10 deletions pkg/morph/deploy/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/notary"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
"github.com/nspcc-dev/neo-go/pkg/util"
Expand All @@ -25,6 +26,12 @@ const (
methodUpdate = "update"
)

const (
_ uint8 = iota
witnessAlphabet
witnessAlphabetAndCommittee
)

// syncNeoFSContractPrm groups parameters of syncNeoFSContract.
type syncNeoFSContractPrm struct {
logger *zap.Logger
Expand Down Expand Up @@ -56,10 +63,12 @@ type syncNeoFSContractPrm struct {
// if set, syncNeoFSContract attempts to deploy the contract when it's
// missing on the chain
tryDeploy bool
// is contract must be deployed by the committee
committeeDeployRequired bool
// additional allowed contracts to be added to the NNS one if committeeDeployRequired
extraCommitteeDeployAllowedContracts []util.Uint160
// 0: committee witness is not needed
// witnessAlphabet: committee 2/3n+1 with allowed alphabetDeployAllowedContracts
// witnessAlphabetAndCommittee: witnessAlphabet + committee n/2+1 with allowed NNS contract calls
deployWitness uint8
// contracts that are allowed to be called for the Alphabet-witnessed deployment
alphabetDeployAllowedContracts []util.Uint160

// optional constructor of extra arguments to be passed into method deploying
// the contract. If returns both nil, no data is passed (noExtraDeployArgs can
Expand Down Expand Up @@ -126,11 +135,46 @@ func syncNeoFSContract(ctx context.Context, prm syncNeoFSContractPrm) (util.Uint
Sender() util.Uint160
}
var managementContract *management.Contract
if prm.committeeDeployRequired {
deployCommitteeActor, err := newCommitteeNotaryActorWithCustomCommitteeSigner(prm.blockchain, prm.localAcc, prm.committee, func(s *transaction.Signer) {
s.Scopes = transaction.CustomContracts
s.AllowedContracts = append(prm.extraCommitteeDeployAllowedContracts, prm.nnsContract)
})
if prm.deployWitness > 0 {
if prm.deployWitness != witnessAlphabet && prm.deployWitness != witnessAlphabetAndCommittee {
panic(fmt.Sprintf("unexpected deploy witness mode value %v", prm.deployWitness))

Check warning on line 140 in pkg/morph/deploy/contracts.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/deploy/contracts.go#L138-L140

Added lines #L138 - L140 were not covered by tests
}

committeeDefaultMultiSigAcc := wallet.NewAccountFromPrivateKey(prm.localAcc.PrivateKey())
err := committeeDefaultMultiSigAcc.ConvertMultisig(smartcontract.GetDefaultHonestNodeCount(len(prm.committee)), prm.committee)
if err != nil {
return util.Uint160{}, fmt.Errorf("compose committee default multi-signature account: %w", err)

Check warning on line 146 in pkg/morph/deploy/contracts.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/deploy/contracts.go#L143-L146

Added lines #L143 - L146 were not covered by tests
}

signers := make([]actor.SignerAccount, 2, 3)

Check warning on line 149 in pkg/morph/deploy/contracts.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/deploy/contracts.go#L149

Added line #L149 was not covered by tests
// payer
signers[0].Account = prm.localAcc
signers[0].Signer.Account = prm.localAcc.ScriptHash()
signers[0].Signer.Scopes = transaction.None

Check warning on line 153 in pkg/morph/deploy/contracts.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/deploy/contracts.go#L151-L153

Added lines #L151 - L153 were not covered by tests
// Alphabet
signers[1].Account = committeeDefaultMultiSigAcc
signers[1].Signer.Account = committeeDefaultMultiSigAcc.ScriptHash()
signers[1].Signer.Scopes = transaction.CustomContracts
signers[1].Signer.AllowedContracts = prm.alphabetDeployAllowedContracts

Check warning on line 158 in pkg/morph/deploy/contracts.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/deploy/contracts.go#L155-L158

Added lines #L155 - L158 were not covered by tests

if prm.deployWitness == witnessAlphabetAndCommittee {
committeeMajorityMultiSigAcc := wallet.NewAccountFromPrivateKey(prm.localAcc.PrivateKey())
err := committeeMajorityMultiSigAcc.ConvertMultisig(smartcontract.GetMajorityHonestNodeCount(len(prm.committee)), prm.committee)
if err != nil {
return util.Uint160{}, fmt.Errorf("compose committee majority multi-signature account: %w", err)

Check warning on line 164 in pkg/morph/deploy/contracts.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/deploy/contracts.go#L160-L164

Added lines #L160 - L164 were not covered by tests
}

signers = append(signers, actor.SignerAccount{
Signer: transaction.Signer{
Account: committeeMajorityMultiSigAcc.ScriptHash(),
Scopes: transaction.CustomContracts,
AllowedContracts: []util.Uint160{prm.nnsContract},
},
Account: committeeMajorityMultiSigAcc,
})

Check warning on line 174 in pkg/morph/deploy/contracts.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/deploy/contracts.go#L167-L174

Added lines #L167 - L174 were not covered by tests
}

deployCommitteeActor, err := notary.NewActor(prm.blockchain, signers, prm.localAcc)

Check warning on line 177 in pkg/morph/deploy/contracts.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/deploy/contracts.go#L177

Added line #L177 was not covered by tests
if err != nil {
return util.Uint160{}, fmt.Errorf("create Notary service client sending deploy transactions to be signed by the committee: %w", err)
}
Expand Down Expand Up @@ -226,7 +270,7 @@ func syncNeoFSContract(ctx context.Context, prm syncNeoFSContractPrm) (util.Uint
nefCp := prm.localNEF
manifestCp := prm.localManifest

if prm.committeeDeployRequired {
if prm.deployWitness > 0 {

Check warning on line 273 in pkg/morph/deploy/contracts.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/deploy/contracts.go#L273

Added line #L273 was not covered by tests
l.Info("contract requires committee witness for deployment, sending Notary request...")

mainTxID, fallbackTxID, vub, err := prm.committeeLocalActor.Notarize(managementContract.DeployTransaction(&nefCp, &manifestCp, extraDeployArgs))
Expand Down
15 changes: 8 additions & 7 deletions pkg/morph/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,8 @@ func Deploy(ctx context.Context, prm Prm) error {
syncPrm.localNEF = prm.BalanceContract.Common.NEF
syncPrm.localManifest = prm.BalanceContract.Common.Manifest
syncPrm.domainName = domainBalance
syncPrm.committeeDeployRequired = true
syncPrm.extraCommitteeDeployAllowedContracts = []util.Uint160{netmapContractAddress}
syncPrm.deployWitness = witnessAlphabet
syncPrm.alphabetDeployAllowedContracts = []util.Uint160{netmapContractAddress}

Check warning on line 468 in pkg/morph/deploy/deploy.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/deploy/deploy.go#L467-L468

Added lines #L467 - L468 were not covered by tests
syncPrm.buildExtraDeployArgs = noExtraDeployArgs

prm.Logger.Info("synchronizing Balance contract with the chain...")
Expand All @@ -477,8 +477,8 @@ func Deploy(ctx context.Context, prm Prm) error {

prm.Logger.Info("Balance contract successfully synchronized", zap.Stringer("address", balanceContractAddress))

syncPrm.committeeDeployRequired = false
syncPrm.extraCommitteeDeployAllowedContracts = nil
syncPrm.deployWitness = 0
syncPrm.alphabetDeployAllowedContracts = nil

Check warning on line 481 in pkg/morph/deploy/deploy.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/deploy/deploy.go#L480-L481

Added lines #L480 - L481 were not covered by tests

// 5. Reputation
syncPrm.localNEF = prm.ReputationContract.Common.NEF
Expand Down Expand Up @@ -514,8 +514,8 @@ func Deploy(ctx context.Context, prm Prm) error {
syncPrm.localNEF = prm.ContainerContract.Common.NEF
syncPrm.localManifest = prm.ContainerContract.Common.Manifest
syncPrm.domainName = domainContainer
syncPrm.committeeDeployRequired = true
syncPrm.extraCommitteeDeployAllowedContracts = []util.Uint160{netmapContractAddress}
syncPrm.deployWitness = witnessAlphabetAndCommittee
syncPrm.alphabetDeployAllowedContracts = []util.Uint160{netmapContractAddress}

Check warning on line 518 in pkg/morph/deploy/deploy.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/deploy/deploy.go#L517-L518

Added lines #L517 - L518 were not covered by tests
syncPrm.buildExtraDeployArgs = func() ([]any, error) {
return []any{
notaryDisabledExtraUpdateArg,
Expand All @@ -536,7 +536,8 @@ func Deploy(ctx context.Context, prm Prm) error {

prm.Logger.Info("Container contract successfully synchronized", zap.Stringer("address", containerContractAddress))

syncPrm.committeeDeployRequired = false
syncPrm.deployWitness = 0
syncPrm.alphabetDeployAllowedContracts = nil

Check warning on line 540 in pkg/morph/deploy/deploy.go

View check run for this annotation

Codecov / codecov/patch

pkg/morph/deploy/deploy.go#L539-L540

Added lines #L539 - L540 were not covered by tests

// 8. Alphabet
syncPrm.localNEF = prm.AlphabetContract.Common.NEF
Expand Down

0 comments on commit daa6344

Please sign in to comment.