2020-11-23 11:09:45 +00:00
|
|
|
package testchain
|
|
|
|
|
|
|
|
import (
|
2020-12-13 15:26:35 +00:00
|
|
|
"encoding/json"
|
2021-03-22 09:21:48 +00:00
|
|
|
"fmt"
|
2020-11-23 11:09:45 +00:00
|
|
|
gio "io"
|
2021-12-02 13:36:29 +00:00
|
|
|
"strings"
|
2020-11-23 11:09:45 +00:00
|
|
|
|
2022-07-25 19:07:13 +00:00
|
|
|
clisc "github.com/nspcc-dev/neo-go/cli/smartcontract"
|
2020-11-23 11:09:45 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/compiler"
|
2020-11-18 20:10:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2022-07-22 19:10:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-11-23 11:09:45 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/fee"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
2020-11-18 20:10:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-11-23 11:09:45 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2021-03-25 16:18:01 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-11-23 11:09:45 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2022-07-25 19:07:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2021-06-02 07:50:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-11-23 11:09:45 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
2022-07-22 19:10:33 +00:00
|
|
|
// Ledger is an interface that abstracts the implementation of the blockchain.
|
|
|
|
type Ledger interface {
|
|
|
|
BlockHeight() uint32
|
|
|
|
FeePerByte() int64
|
|
|
|
GetBaseExecFee() int64
|
|
|
|
GetHeader(hash util.Uint256) (*block.Header, error)
|
|
|
|
GetHeaderHash(int) util.Uint256
|
|
|
|
HeaderHeight() uint32
|
|
|
|
ManagementContractHash() util.Uint160
|
|
|
|
}
|
|
|
|
|
2020-11-23 11:09:45 +00:00
|
|
|
var (
|
|
|
|
ownerHash = MultisigScriptHash()
|
|
|
|
ownerScript = MultisigVerificationScript()
|
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewTransferFromOwner returns a transaction transferring funds from NEO and GAS owner.
|
2022-07-22 19:10:33 +00:00
|
|
|
func NewTransferFromOwner(bc Ledger, contractHash, to util.Uint160, amount int64,
|
2020-11-23 11:09:45 +00:00
|
|
|
nonce, validUntil uint32) (*transaction.Transaction, error) {
|
2022-07-25 19:07:13 +00:00
|
|
|
script, err := smartcontract.CreateCallWithAssertScript(contractHash, "transfer", ownerHash, to, amount, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-11-23 11:09:45 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 16:18:01 +00:00
|
|
|
tx := transaction.New(script, 11000000)
|
2020-11-23 11:09:45 +00:00
|
|
|
tx.ValidUntilBlock = validUntil
|
|
|
|
tx.Nonce = nonce
|
|
|
|
tx.Signers = []transaction.Signer{{
|
|
|
|
Account: ownerHash,
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
AllowedContracts: nil,
|
|
|
|
AllowedGroups: nil,
|
|
|
|
}}
|
|
|
|
return tx, SignTx(bc, tx)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewDeployTx returns a new deployment transaction for a contract with the source from r and a name equal to
|
|
|
|
// the filename without '.go' suffix.
|
2022-07-22 19:10:33 +00:00
|
|
|
func NewDeployTx(bc Ledger, name string, sender util.Uint160, r gio.Reader, confFile *string) (*transaction.Transaction, util.Uint160, []byte, error) {
|
2020-11-18 20:10:48 +00:00
|
|
|
// nef.NewFile() cares about version a lot.
|
|
|
|
config.Version = "0.90.0-test"
|
|
|
|
|
2021-03-22 09:21:48 +00:00
|
|
|
o := &compiler.Options{
|
2021-12-02 13:36:29 +00:00
|
|
|
Name: strings.TrimSuffix(name, ".go"),
|
2021-03-22 09:21:48 +00:00
|
|
|
NoStandardCheck: true,
|
|
|
|
NoEventsCheck: true,
|
|
|
|
}
|
|
|
|
if confFile != nil {
|
2022-07-25 19:07:13 +00:00
|
|
|
conf, err := clisc.ParseContractConfig(*confFile)
|
2021-03-22 09:21:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, util.Uint160{}, nil, fmt.Errorf("failed to parse configuration: %w", err)
|
|
|
|
}
|
|
|
|
o.Name = conf.Name
|
2022-02-04 16:20:03 +00:00
|
|
|
o.SourceURL = conf.SourceURL
|
2021-03-22 09:21:48 +00:00
|
|
|
o.ContractEvents = conf.Events
|
|
|
|
o.ContractSupportedStandards = conf.SupportedStandards
|
2021-06-02 07:50:17 +00:00
|
|
|
o.Permissions = make([]manifest.Permission, len(conf.Permissions))
|
|
|
|
for i := range conf.Permissions {
|
|
|
|
o.Permissions[i] = manifest.Permission(conf.Permissions[i])
|
|
|
|
}
|
2021-03-22 09:21:48 +00:00
|
|
|
o.SafeMethods = conf.SafeMethods
|
2022-02-04 16:20:03 +00:00
|
|
|
o.Overloads = conf.Overloads
|
2021-03-22 09:21:48 +00:00
|
|
|
}
|
2021-12-02 13:36:29 +00:00
|
|
|
|
|
|
|
ne, di, err := compiler.CompileWithOptions(name, r, o)
|
|
|
|
if err != nil {
|
|
|
|
return nil, util.Uint160{}, nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-22 09:21:48 +00:00
|
|
|
m, err := compiler.CreateManifest(di, o)
|
2020-11-23 11:09:45 +00:00
|
|
|
if err != nil {
|
2021-03-22 09:21:48 +00:00
|
|
|
return nil, util.Uint160{}, nil, fmt.Errorf("failed to create manifest: %w", err)
|
2020-11-23 11:09:45 +00:00
|
|
|
}
|
2020-11-27 18:53:39 +00:00
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
rawManifest, err := json.Marshal(m)
|
2020-11-23 11:09:45 +00:00
|
|
|
if err != nil {
|
2021-03-05 07:18:03 +00:00
|
|
|
return nil, util.Uint160{}, nil, err
|
2020-11-23 11:09:45 +00:00
|
|
|
}
|
2020-12-13 15:26:35 +00:00
|
|
|
neb, err := ne.Bytes()
|
|
|
|
if err != nil {
|
2021-03-05 07:18:03 +00:00
|
|
|
return nil, util.Uint160{}, nil, err
|
2020-12-13 15:26:35 +00:00
|
|
|
}
|
2022-07-25 19:07:13 +00:00
|
|
|
script, err := smartcontract.CreateCallScript(bc.ManagementContractHash(), "deploy", neb, rawManifest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, util.Uint160{}, nil, err
|
2020-12-13 15:26:35 +00:00
|
|
|
}
|
2020-11-18 20:10:48 +00:00
|
|
|
|
2022-07-25 19:07:13 +00:00
|
|
|
tx := transaction.New(script, 100*native.GASFactor)
|
2020-11-18 20:10:48 +00:00
|
|
|
tx.Signers = []transaction.Signer{{Account: sender}}
|
2021-12-02 13:36:29 +00:00
|
|
|
h := state.CreateContractHash(tx.Sender(), ne.Checksum, m.Name)
|
2020-11-18 20:10:48 +00:00
|
|
|
|
2021-12-08 19:33:03 +00:00
|
|
|
return tx, h, ne.Script, nil
|
2020-11-23 11:09:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// SignTx signs the provided transactions with validator keys.
|
2022-07-22 19:10:33 +00:00
|
|
|
func SignTx(bc Ledger, txs ...*transaction.Transaction) error {
|
2021-01-21 09:23:53 +00:00
|
|
|
signTxGeneric(bc, Sign, ownerScript, txs...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignTxCommittee signs transactions by committee.
|
2022-07-22 19:10:33 +00:00
|
|
|
func SignTxCommittee(bc Ledger, txs ...*transaction.Transaction) error {
|
2021-01-21 09:23:53 +00:00
|
|
|
signTxGeneric(bc, SignCommittee, CommitteeVerificationScript(), txs...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-22 19:10:33 +00:00
|
|
|
func signTxGeneric(bc Ledger, sign func(hash.Hashable) []byte, verif []byte, txs ...*transaction.Transaction) {
|
2020-11-23 11:09:45 +00:00
|
|
|
for _, tx := range txs {
|
|
|
|
size := io.GetVarSize(tx)
|
2022-01-11 21:58:03 +00:00
|
|
|
netFee, sizeDelta := fee.Calculate(bc.GetBaseExecFee(), verif)
|
2020-11-23 11:09:45 +00:00
|
|
|
tx.NetworkFee += netFee
|
|
|
|
size += sizeDelta
|
|
|
|
tx.NetworkFee += int64(size) * bc.FeePerByte()
|
|
|
|
tx.Scripts = []transaction.Witness{{
|
2021-03-25 16:18:01 +00:00
|
|
|
InvocationScript: sign(tx),
|
2021-01-21 09:23:53 +00:00
|
|
|
VerificationScript: verif,
|
2020-11-23 11:09:45 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
}
|