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-03-22 09:21:48 +00:00
|
|
|
"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"
|
2020-11-23 11:09:45 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer"
|
|
|
|
"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"
|
2020-12-29 10:44:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2020-11-18 20:10:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
2020-11-23 11:09:45 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ownerHash = MultisigScriptHash()
|
|
|
|
ownerScript = MultisigVerificationScript()
|
|
|
|
)
|
|
|
|
|
2020-12-28 14:27:04 +00:00
|
|
|
// NewTransferFromOwner returns transaction transferring funds from NEO and GAS owner.
|
2020-11-23 11:09:45 +00:00
|
|
|
func NewTransferFromOwner(bc blockchainer.Blockchainer, contractHash, to util.Uint160, amount int64,
|
|
|
|
nonce, validUntil uint32) (*transaction.Transaction, error) {
|
|
|
|
w := io.NewBufBinWriter()
|
2020-12-29 10:44:07 +00:00
|
|
|
emit.AppCall(w.BinWriter, contractHash, "transfer", callflag.All, ownerHash, to, amount, nil)
|
2020-11-23 11:09:45 +00:00
|
|
|
emit.Opcodes(w.BinWriter, opcode.ASSERT)
|
|
|
|
if w.Err != nil {
|
|
|
|
return nil, w.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
script := w.Bytes()
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDeployTx returns new deployment tx for contract with name with Go code read from r.
|
2021-03-22 09:21:48 +00:00
|
|
|
func NewDeployTx(bc blockchainer.Blockchainer, 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"
|
|
|
|
|
2020-11-23 11:09:45 +00:00
|
|
|
avm, di, err := compiler.CompileWithDebugInfo(name, r)
|
|
|
|
if err != nil {
|
2021-03-05 07:18:03 +00:00
|
|
|
return nil, util.Uint160{}, nil, err
|
2020-11-18 20:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ne, err := nef.NewFile(avm)
|
|
|
|
if err != nil {
|
2021-03-05 07:18:03 +00:00
|
|
|
return nil, util.Uint160{}, nil, err
|
2020-11-18 20:10:48 +00:00
|
|
|
}
|
2020-11-23 11:09:45 +00:00
|
|
|
|
2021-03-22 09:21:48 +00:00
|
|
|
o := &compiler.Options{
|
|
|
|
Name: name,
|
|
|
|
NoStandardCheck: true,
|
|
|
|
NoEventsCheck: true,
|
|
|
|
}
|
|
|
|
if confFile != nil {
|
|
|
|
conf, err := smartcontract.ParseContractConfig(*confFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, util.Uint160{}, nil, fmt.Errorf("failed to parse configuration: %w", err)
|
|
|
|
}
|
|
|
|
o.Name = conf.Name
|
|
|
|
o.ContractEvents = conf.Events
|
|
|
|
o.ContractSupportedStandards = conf.SupportedStandards
|
|
|
|
o.SafeMethods = conf.SafeMethods
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
buf := io.NewBufBinWriter()
|
2020-12-29 10:44:07 +00:00
|
|
|
emit.AppCall(buf.BinWriter, bc.ManagementContractHash(), "deploy", callflag.All, neb, rawManifest)
|
2020-12-13 15:26:35 +00:00
|
|
|
if buf.Err != nil {
|
2021-03-05 07:18:03 +00:00
|
|
|
return nil, util.Uint160{}, nil, buf.Err
|
2020-12-13 15:26:35 +00:00
|
|
|
}
|
2020-11-18 20:10:48 +00:00
|
|
|
|
2021-03-25 16:18:01 +00:00
|
|
|
tx := transaction.New(buf.Bytes(), 100*native.GASFactor)
|
2020-11-18 20:10:48 +00:00
|
|
|
tx.Signers = []transaction.Signer{{Account: sender}}
|
2021-01-22 09:22:48 +00:00
|
|
|
h := state.CreateContractHash(tx.Sender(), ne.Checksum, name)
|
2020-11-18 20:10:48 +00:00
|
|
|
|
2021-03-05 07:18:03 +00:00
|
|
|
return tx, h, avm, nil
|
2020-11-23 11:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SignTx signs provided transactions with validator keys.
|
|
|
|
func SignTx(bc blockchainer.Blockchainer, txs ...*transaction.Transaction) error {
|
2021-01-21 09:23:53 +00:00
|
|
|
signTxGeneric(bc, Sign, ownerScript, txs...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignTxCommittee signs transactions by committee.
|
|
|
|
func SignTxCommittee(bc blockchainer.Blockchainer, txs ...*transaction.Transaction) error {
|
|
|
|
signTxGeneric(bc, SignCommittee, CommitteeVerificationScript(), txs...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-25 16:18:01 +00:00
|
|
|
func signTxGeneric(bc blockchainer.Blockchainer, 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)
|
2021-01-21 09:23:53 +00:00
|
|
|
netFee, sizeDelta := fee.Calculate(bc.GetPolicer().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
|
|
|
}}
|
|
|
|
}
|
|
|
|
}
|