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

deploy: Fix witnesses for Balance and Container contract deployments #2695

Merged
merged 1 commit into from
Dec 21, 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
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
72 changes: 62 additions & 10 deletions pkg/morph/deploy/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"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 @@
methodUpdate = "update"
)

const (
_ uint8 = iota
witnessValidators
witnessValidatorsAndCommittee
)

// syncNeoFSContractPrm groups parameters of syncNeoFSContract.
type syncNeoFSContractPrm struct {
logger *zap.Logger
Expand Down Expand Up @@ -56,10 +63,12 @@
// 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
// witnessValidators: committee 2/3n+1 with validatorsDeployAllowedContracts
// witnessValidatorsAndCommittee: witnessValidators + committee n/2+1 with allowed NNS contract calls
deployWitness uint8
// contracts that are allowed to be called for the validators-witnessed deployment
validatorsDeployAllowedContracts []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,54 @@
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 != witnessValidators && prm.deployWitness != witnessValidatorsAndCommittee {
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
}

validatorsMultiSigAcc := wallet.NewAccountFromPrivateKey(prm.localAcc.PrivateKey())
err := validatorsMultiSigAcc.ConvertMultisig(smartcontract.GetDefaultHonestNodeCount(len(prm.committee)), prm.committee)
if err != nil {
return util.Uint160{}, fmt.Errorf("compose validators 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
// validators
signers[1].Account = validatorsMultiSigAcc
signers[1].Signer.Account = validatorsMultiSigAcc.ScriptHash()
signers[1].Signer.Scopes = transaction.CustomContracts
signers[1].Signer.AllowedContracts = prm.validatorsDeployAllowedContracts

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 == witnessValidatorsAndCommittee {
committeeMultiSigAcc := wallet.NewAccountFromPrivateKey(prm.localAcc.PrivateKey())
err := committeeMultiSigAcc.ConvertMultisig(smartcontract.GetMajorityHonestNodeCount(len(prm.committee)), prm.committee)
if err != nil {
return util.Uint160{}, fmt.Errorf("compose committee 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
}

if acc := committeeMultiSigAcc.ScriptHash(); acc.Equals(signers[1].Signer.Account) {
signers[1].Account = committeeMultiSigAcc
signers[1].Signer.Account = acc
signers[1].Signer.Scopes = transaction.CustomContracts
signers[1].Signer.AllowedContracts = append(prm.validatorsDeployAllowedContracts, prm.nnsContract)
} else {

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L167 - L172 were not covered by tests
// prevent 'transaction signers should be unique' error
signers = append(signers, actor.SignerAccount{
Signer: transaction.Signer{
Account: committeeMultiSigAcc.ScriptHash(),
Scopes: transaction.CustomContracts,
AllowedContracts: []util.Uint160{prm.nnsContract},
},
Account: committeeMultiSigAcc,
})

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

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

View check run for this annotation

Codecov / codecov/patch

pkg/morph/deploy/contracts.go#L185

Added line #L185 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 +278,7 @@
nefCp := prm.localNEF
manifestCp := prm.localManifest

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

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

View check run for this annotation

Codecov / codecov/patch

pkg/morph/deploy/contracts.go#L281

Added line #L281 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 @@
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 = witnessValidators
syncPrm.validatorsDeployAllowedContracts = []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 @@

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

syncPrm.committeeDeployRequired = false
syncPrm.extraCommitteeDeployAllowedContracts = nil
syncPrm.deployWitness = 0
syncPrm.validatorsDeployAllowedContracts = 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 @@
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 = witnessValidatorsAndCommittee
syncPrm.validatorsDeployAllowedContracts = []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 @@

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

syncPrm.committeeDeployRequired = false
syncPrm.deployWitness = 0
syncPrm.validatorsDeployAllowedContracts = 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
Loading