2021-07-21 10:09:59 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
|
|
|
|
"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"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring"
|
2021-07-29 10:06:25 +00:00
|
|
|
"github.com/spf13/viper"
|
2021-07-21 10:09:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
nnsContract = "nns"
|
|
|
|
neofsContract = "neofs" // not deployed in side-chain.
|
|
|
|
processingContract = "processing" // not deployed in side-chain.
|
|
|
|
alphabetContract = "alphabet"
|
|
|
|
auditContract = "audit"
|
|
|
|
balanceContract = "balance"
|
|
|
|
containerContract = "container"
|
|
|
|
neofsIDContract = "neofsid"
|
|
|
|
netmapContract = "netmap"
|
|
|
|
proxyContract = "proxy"
|
|
|
|
reputationContract = "reputation"
|
|
|
|
)
|
|
|
|
|
2021-07-29 10:06:25 +00:00
|
|
|
const (
|
|
|
|
netmapEpochKey = "EpochDuration"
|
|
|
|
netmapMaxObjectSizeKey = "MaxObjectSize"
|
|
|
|
netmapAuditFeeKey = "AuditFee"
|
|
|
|
netmapContainerFeeKey = "ContainerFee"
|
|
|
|
netmapEigenTrustIterationsKey = "EigenTrustIterations"
|
|
|
|
netmapEigenTrustAlphaKey = "EigenTrustAlpha"
|
|
|
|
netmapBasicIncomeRateKey = "BasicIncomeRate"
|
|
|
|
netmapInnerRingCandidateFeeKey = "InnerRingCandidateFee"
|
|
|
|
netmapWithdrawFeeKey = "WithdrawFee"
|
|
|
|
|
2021-08-03 12:04:38 +00:00
|
|
|
defaultEigenTrustIterations = 4
|
|
|
|
defaultEigenTrustAlpha = "0.1"
|
2021-07-29 10:06:25 +00:00
|
|
|
)
|
|
|
|
|
2021-07-21 10:09:59 +00:00
|
|
|
var contractList = []string{
|
|
|
|
auditContract,
|
|
|
|
balanceContract,
|
|
|
|
containerContract,
|
|
|
|
neofsIDContract,
|
|
|
|
netmapContract,
|
|
|
|
proxyContract,
|
|
|
|
reputationContract,
|
|
|
|
}
|
|
|
|
|
|
|
|
type contractState struct {
|
|
|
|
NEF *nef.File
|
|
|
|
RawNEF []byte
|
|
|
|
Manifest *manifest.Manifest
|
|
|
|
RawManifest []byte
|
|
|
|
Hash util.Uint160
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *initializeContext) deployNNS() error {
|
2021-07-29 13:23:36 +00:00
|
|
|
cs, err := c.readContract(nnsContract)
|
2021-07-21 10:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h := state.CreateContractHash(c.CommitteeAcc.Contract.ScriptHash(), cs.NEF.Checksum, cs.Manifest.Name)
|
|
|
|
if _, err := c.Client.GetContractStateByHash(h); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
params := getContractDeployParameters(cs.RawNEF, cs.RawManifest, nil)
|
|
|
|
signer := transaction.Signer{
|
|
|
|
Account: c.CommitteeAcc.Contract.ScriptHash(),
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
}
|
|
|
|
|
2021-07-29 13:35:57 +00:00
|
|
|
mgmtHash := c.nativeHash(nativenames.Management)
|
2021-07-21 10:09:59 +00:00
|
|
|
res, err := c.Client.InvokeFunction(mgmtHash, "deploy", params, []transaction.Signer{signer})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't deploy contract: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := c.Client.CreateTxFromScript(res.Script, c.CommitteeAcc, res.GasConsumed, 0, []client.SignerAccount{{
|
|
|
|
Signer: signer,
|
|
|
|
Account: c.CommitteeAcc,
|
|
|
|
}})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create deploy tx for %s: %w", nnsContract, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.multiSignAndSend(tx, committeeAccountName); err != nil {
|
|
|
|
return fmt.Errorf("can't send deploy transaction: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.awaitTx()
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:28:07 +00:00
|
|
|
func (c *initializeContext) deployContracts(method string) error {
|
2021-07-29 13:35:57 +00:00
|
|
|
mgmtHash := c.nativeHash(nativenames.Management)
|
2021-07-21 10:09:59 +00:00
|
|
|
sender := c.CommitteeAcc.Contract.ScriptHash()
|
|
|
|
for _, ctrName := range contractList {
|
2021-07-29 13:23:36 +00:00
|
|
|
cs, err := c.readContract(ctrName)
|
2021-07-21 10:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cs.Hash = state.CreateContractHash(sender, cs.NEF.Checksum, cs.Manifest.Name)
|
|
|
|
}
|
|
|
|
|
2021-07-30 11:29:05 +00:00
|
|
|
alphaCs, err := c.readContract(alphabetContract)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:28:07 +00:00
|
|
|
nnsCs, err := c.Client.GetContractStateByID(1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nnsHash := nnsCs.Hash
|
|
|
|
|
2021-07-21 10:09:59 +00:00
|
|
|
var keysParam []smartcontract.Parameter
|
|
|
|
|
|
|
|
// alphabet contracts should be deployed by individual nodes to get different hashes.
|
2021-07-30 11:26:44 +00:00
|
|
|
for i, acc := range c.Accounts {
|
2021-07-30 11:29:05 +00:00
|
|
|
ctrHash := state.CreateContractHash(acc.Contract.ScriptHash(), alphaCs.NEF.Checksum, alphaCs.Manifest.Name)
|
2021-07-21 10:09:59 +00:00
|
|
|
if _, err := c.Client.GetContractStateByHash(ctrHash); err == nil {
|
2021-08-06 11:28:07 +00:00
|
|
|
c.Command.Printf("Alphabet contract #%d is already deployed.\n", i)
|
2021-07-21 10:09:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:28:07 +00:00
|
|
|
invokeHash := mgmtHash
|
|
|
|
if method == "migrate" {
|
|
|
|
invokeHash, err = nnsResolveHash(c.Client, nnsHash, getAlphabetNNSDomain(i))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't resolve hash for contract update: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 10:09:59 +00:00
|
|
|
keysParam = append(keysParam, smartcontract.Parameter{
|
|
|
|
Type: smartcontract.PublicKeyType,
|
|
|
|
Value: acc.PrivateKey().PublicKey().Bytes(),
|
|
|
|
})
|
|
|
|
|
2021-07-30 11:29:05 +00:00
|
|
|
params := getContractDeployParameters(alphaCs.RawNEF, alphaCs.RawManifest,
|
2021-07-21 10:09:59 +00:00
|
|
|
c.getAlphabetDeployParameters(i, len(c.Wallets)))
|
|
|
|
signer := transaction.Signer{
|
|
|
|
Account: acc.Contract.ScriptHash(),
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
}
|
2021-08-06 11:28:07 +00:00
|
|
|
res, err := c.Client.InvokeFunction(invokeHash, method, params, []transaction.Signer{signer})
|
2021-07-21 10:09:59 +00:00
|
|
|
if err != nil {
|
2021-07-23 12:07:44 +00:00
|
|
|
return fmt.Errorf("can't deploy alphabet #%d contract: %w", i, err)
|
2021-07-21 10:09:59 +00:00
|
|
|
}
|
|
|
|
h, err := c.Client.SignAndPushInvocationTx(res.Script, acc, -1, 0, []client.SignerAccount{{
|
|
|
|
Signer: signer,
|
|
|
|
Account: acc,
|
|
|
|
}})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't push deploy transaction: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Hashes = append(c.Hashes, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ctrName := range contractList {
|
|
|
|
cs := c.Contracts[ctrName]
|
|
|
|
if _, err := c.Client.GetContractStateByHash(cs.Hash); err == nil {
|
2021-08-06 11:28:07 +00:00
|
|
|
c.Command.Printf("%s contract is already deployed.\n", ctrName)
|
2021-07-21 10:09:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:28:07 +00:00
|
|
|
invokeHash := mgmtHash
|
|
|
|
if method == "migrate" {
|
|
|
|
invokeHash, err = nnsResolveHash(c.Client, nnsHash, ctrName+".neofs")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't resolve hash for contract update: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 10:09:59 +00:00
|
|
|
params := getContractDeployParameters(cs.RawNEF, cs.RawManifest,
|
|
|
|
c.getContractDeployData(ctrName, keysParam))
|
|
|
|
signer := transaction.Signer{
|
|
|
|
Account: c.CommitteeAcc.Contract.ScriptHash(),
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:28:07 +00:00
|
|
|
res, err := c.Client.InvokeFunction(invokeHash, method, params, []transaction.Signer{signer})
|
2021-07-21 10:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't deploy contract: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.sendCommitteeTx(res.Script, res.GasConsumed); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.awaitTx()
|
|
|
|
}
|
|
|
|
|
2021-07-29 13:23:36 +00:00
|
|
|
func (c *initializeContext) readContract(ctrName string) (*contractState, error) {
|
2021-07-21 10:09:59 +00:00
|
|
|
if cs, ok := c.Contracts[ctrName]; ok {
|
|
|
|
return cs, nil
|
|
|
|
}
|
|
|
|
|
2021-07-29 13:23:36 +00:00
|
|
|
rawNef, err := ioutil.ReadFile(path.Join(c.ContractPath, ctrName, ctrName+"_contract.nef"))
|
2021-07-21 10:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read NEF file: %w", err)
|
|
|
|
}
|
|
|
|
nf, err := nef.FileFromBytes(rawNef)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't parse NEF file: %w", err)
|
|
|
|
}
|
2021-07-29 13:23:36 +00:00
|
|
|
rawManif, err := ioutil.ReadFile(path.Join(c.ContractPath, ctrName, "config.json"))
|
2021-07-21 10:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't read manifest file: %w", err)
|
|
|
|
}
|
|
|
|
m := new(manifest.Manifest)
|
|
|
|
if err := json.Unmarshal(rawManif, m); err != nil {
|
|
|
|
return nil, fmt.Errorf("can't parse manifest file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Contracts[ctrName] = &contractState{
|
|
|
|
NEF: &nf,
|
|
|
|
RawNEF: rawNef,
|
|
|
|
Manifest: m,
|
|
|
|
RawManifest: rawManif,
|
|
|
|
}
|
|
|
|
return c.Contracts[ctrName], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getContractDeployParameters(rawNef, rawManif []byte, deployData []smartcontract.Parameter) []smartcontract.Parameter {
|
|
|
|
return []smartcontract.Parameter{
|
|
|
|
{
|
|
|
|
Type: smartcontract.ByteArrayType,
|
|
|
|
Value: rawNef,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: smartcontract.ByteArrayType,
|
|
|
|
Value: rawManif,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: smartcontract.ArrayType,
|
|
|
|
Value: deployData,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *initializeContext) getContractDeployData(ctrName string, keysParam []smartcontract.Parameter) []smartcontract.Parameter {
|
|
|
|
items := make([]smartcontract.Parameter, 2, 7)
|
|
|
|
items[0] = newContractParameter(smartcontract.BoolType, false) // notaryDisabled is false
|
|
|
|
items[1] = newContractParameter(smartcontract.Hash160Type, c.CommitteeAcc.Contract.ScriptHash()) // owner is committee
|
|
|
|
|
|
|
|
switch ctrName {
|
|
|
|
case neofsContract:
|
|
|
|
items = append(items,
|
|
|
|
newContractParameter(smartcontract.Hash160Type, c.Contracts[processingContract].Hash),
|
2021-07-29 10:06:25 +00:00
|
|
|
newContractParameter(smartcontract.ArrayType, keysParam),
|
|
|
|
newContractParameter(smartcontract.ArrayType, smartcontract.Parameter{}))
|
2021-07-21 10:09:59 +00:00
|
|
|
case processingContract:
|
|
|
|
items = append(items, newContractParameter(smartcontract.Hash160Type, c.Contracts[neofsContract].Hash))
|
|
|
|
return items[1:] // no notary info
|
|
|
|
case auditContract:
|
|
|
|
items = append(items,
|
|
|
|
newContractParameter(smartcontract.Hash160Type, c.Contracts[netmapContract].Hash))
|
|
|
|
case balanceContract:
|
|
|
|
items = append(items,
|
|
|
|
newContractParameter(smartcontract.Hash160Type, c.Contracts[netmapContract].Hash),
|
|
|
|
newContractParameter(smartcontract.Hash160Type, c.Contracts[containerContract].Hash))
|
|
|
|
case containerContract:
|
|
|
|
items = append(items,
|
|
|
|
newContractParameter(smartcontract.Hash160Type, c.Contracts[netmapContract].Hash),
|
|
|
|
newContractParameter(smartcontract.Hash160Type, c.Contracts[balanceContract].Hash),
|
|
|
|
newContractParameter(smartcontract.Hash160Type, c.Contracts[neofsIDContract].Hash))
|
|
|
|
case neofsIDContract:
|
|
|
|
items = append(items,
|
|
|
|
newContractParameter(smartcontract.Hash160Type, c.Contracts[netmapContract].Hash),
|
|
|
|
newContractParameter(smartcontract.Hash160Type, c.Contracts[containerContract].Hash))
|
|
|
|
case netmapContract:
|
2021-07-29 10:06:25 +00:00
|
|
|
configParam := []smartcontract.Parameter{
|
|
|
|
{Type: smartcontract.StringType, Value: netmapEpochKey},
|
|
|
|
{Type: smartcontract.IntegerType, Value: viper.GetInt64(epochDurationInitFlag)},
|
|
|
|
{Type: smartcontract.StringType, Value: netmapMaxObjectSizeKey},
|
|
|
|
{Type: smartcontract.IntegerType, Value: viper.GetInt64(maxObjectSizeInitFlag)},
|
|
|
|
{Type: smartcontract.StringType, Value: netmapAuditFeeKey},
|
2021-08-03 12:04:38 +00:00
|
|
|
{Type: smartcontract.IntegerType, Value: viper.GetInt64(auditFeeInitFlag)},
|
2021-07-29 10:06:25 +00:00
|
|
|
{Type: smartcontract.StringType, Value: netmapContainerFeeKey},
|
2021-08-03 12:04:38 +00:00
|
|
|
{Type: smartcontract.IntegerType, Value: viper.GetInt64(containerFeeInitFlag)},
|
2021-07-29 10:06:25 +00:00
|
|
|
{Type: smartcontract.StringType, Value: netmapEigenTrustIterationsKey},
|
|
|
|
{Type: smartcontract.IntegerType, Value: int64(defaultEigenTrustIterations)},
|
|
|
|
{Type: smartcontract.StringType, Value: netmapEigenTrustAlphaKey},
|
|
|
|
{Type: smartcontract.StringType, Value: defaultEigenTrustAlpha},
|
|
|
|
{Type: smartcontract.StringType, Value: netmapBasicIncomeRateKey},
|
2021-08-03 12:04:38 +00:00
|
|
|
{Type: smartcontract.IntegerType, Value: viper.GetInt64(incomeRateInitFlag)},
|
2021-07-29 10:06:25 +00:00
|
|
|
{Type: smartcontract.StringType, Value: netmapInnerRingCandidateFeeKey},
|
2021-08-03 12:04:38 +00:00
|
|
|
{Type: smartcontract.IntegerType, Value: viper.GetInt64(candidateFeeInitFlag)},
|
2021-07-29 10:06:25 +00:00
|
|
|
{Type: smartcontract.StringType, Value: netmapWithdrawFeeKey},
|
2021-08-03 12:04:38 +00:00
|
|
|
{Type: smartcontract.IntegerType, Value: viper.GetInt64(withdrawFeeInitFlag)},
|
2021-07-29 10:06:25 +00:00
|
|
|
}
|
2021-07-21 10:09:59 +00:00
|
|
|
items = append(items,
|
|
|
|
newContractParameter(smartcontract.Hash160Type, c.Contracts[balanceContract].Hash),
|
|
|
|
newContractParameter(smartcontract.Hash160Type, c.Contracts[containerContract].Hash),
|
2021-07-29 10:06:25 +00:00
|
|
|
newContractParameter(smartcontract.ArrayType, keysParam),
|
|
|
|
newContractParameter(smartcontract.ArrayType, configParam))
|
2021-07-21 10:09:59 +00:00
|
|
|
case proxyContract:
|
|
|
|
items = append(items, newContractParameter(smartcontract.Hash160Type, c.Contracts[netmapContract].Hash))
|
|
|
|
case reputationContract:
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("invalid contract name: %s", ctrName))
|
|
|
|
}
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *initializeContext) getAlphabetDeployParameters(i, n int) []smartcontract.Parameter {
|
|
|
|
return []smartcontract.Parameter{
|
|
|
|
newContractParameter(smartcontract.BoolType, false),
|
|
|
|
newContractParameter(smartcontract.Hash160Type, c.CommitteeAcc.Contract.ScriptHash()),
|
|
|
|
newContractParameter(smartcontract.Hash160Type, c.Contracts[netmapContract].Hash),
|
|
|
|
newContractParameter(smartcontract.Hash160Type, c.Contracts[proxyContract].Hash),
|
|
|
|
newContractParameter(smartcontract.StringType, innerring.GlagoliticLetter(i).String()),
|
|
|
|
newContractParameter(smartcontract.IntegerType, int64(i)),
|
|
|
|
newContractParameter(smartcontract.IntegerType, int64(n)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newContractParameter(typ smartcontract.ParamType, value interface{}) smartcontract.Parameter {
|
|
|
|
return smartcontract.Parameter{
|
|
|
|
Type: typ,
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
}
|