2020-11-23 11:09:45 +00:00
|
|
|
package testchain
|
|
|
|
|
|
|
|
import (
|
2020-12-13 15:26:35 +00:00
|
|
|
"encoding/json"
|
2020-11-23 11:09:45 +00:00
|
|
|
gio "io"
|
|
|
|
|
|
|
|
"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/config/netmode"
|
|
|
|
"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"
|
|
|
|
"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()
|
2020-12-15 09:04:08 +00:00
|
|
|
tx := transaction.New(netmode.UnitTestNet, 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.
|
2020-12-13 15:26:35 +00:00
|
|
|
func NewDeployTx(bc blockchainer.Blockchainer, name string, sender util.Uint160, r gio.Reader) (*transaction.Transaction, util.Uint160, 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 {
|
2020-11-18 20:10:48 +00:00
|
|
|
return nil, util.Uint160{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ne, err := nef.NewFile(avm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, util.Uint160{}, err
|
|
|
|
}
|
2020-11-23 11:09:45 +00:00
|
|
|
|
2020-12-10 14:42:12 +00:00
|
|
|
m, err := di.ConvertToManifest(&compiler.Options{Name: name})
|
2020-11-23 11:09:45 +00:00
|
|
|
if err != nil {
|
2020-11-18 20:10:48 +00:00
|
|
|
return nil, util.Uint160{}, 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 {
|
2020-11-18 20:10:48 +00:00
|
|
|
return nil, util.Uint160{}, err
|
2020-11-23 11:09:45 +00:00
|
|
|
}
|
2020-12-13 15:26:35 +00:00
|
|
|
neb, err := ne.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, util.Uint160{}, err
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return nil, util.Uint160{}, buf.Err
|
|
|
|
}
|
2020-11-18 20:10:48 +00:00
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
tx := transaction.New(Network(), 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
|
|
|
|
|
|
|
return tx, h, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func signTxGeneric(bc blockchainer.Blockchainer, sign func([]byte) []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()
|
|
|
|
data := tx.GetSignedPart()
|
|
|
|
tx.Scripts = []transaction.Witness{{
|
2021-01-21 09:23:53 +00:00
|
|
|
InvocationScript: sign(data),
|
|
|
|
VerificationScript: verif,
|
2020-11-23 11:09:45 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
}
|