mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-21 23:29:38 +00:00
core: allow to compile test contracts with yaml config
And refactored Rubl test contract (it should support NEP-17 and onNEP17Payment).
This commit is contained in:
parent
ee76db9ff2
commit
0a5072a1da
9 changed files with 59 additions and 24 deletions
|
@ -443,7 +443,7 @@ func contractCompile(ctx *cli.Context) error {
|
|||
}
|
||||
|
||||
if len(confFile) != 0 {
|
||||
conf, err := parseContractConfig(confFile)
|
||||
conf, err := ParseContractConfig(confFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -875,7 +875,8 @@ func contractDeploy(ctx *cli.Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func parseContractConfig(confFile string) (ProjectConfig, error) {
|
||||
// ParseContractConfig reads contract configuration file (.yaml) and returns unmarshalled ProjectConfig.
|
||||
func ParseContractConfig(confFile string) (ProjectConfig, error) {
|
||||
conf := ProjectConfig{}
|
||||
confBytes, err := ioutil.ReadFile(confFile)
|
||||
if err != nil {
|
||||
|
|
|
@ -2,8 +2,10 @@ package testchain
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
gio "io"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/cli/smartcontract"
|
||||
"github.com/nspcc-dev/neo-go/pkg/compiler"
|
||||
"github.com/nspcc-dev/neo-go/pkg/config"
|
||||
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
||||
|
@ -49,7 +51,7 @@ func NewTransferFromOwner(bc blockchainer.Blockchainer, contractHash, to util.Ui
|
|||
}
|
||||
|
||||
// NewDeployTx returns new deployment tx for contract with name with Go code read from r.
|
||||
func NewDeployTx(bc blockchainer.Blockchainer, name string, sender util.Uint160, r gio.Reader) (*transaction.Transaction, util.Uint160, []byte, error) {
|
||||
func NewDeployTx(bc blockchainer.Blockchainer, name string, sender util.Uint160, r gio.Reader, confFile *string) (*transaction.Transaction, util.Uint160, []byte, error) {
|
||||
// nef.NewFile() cares about version a lot.
|
||||
config.Version = "0.90.0-test"
|
||||
|
||||
|
@ -63,9 +65,25 @@ func NewDeployTx(bc blockchainer.Blockchainer, name string, sender util.Uint160,
|
|||
return nil, util.Uint160{}, nil, err
|
||||
}
|
||||
|
||||
m, err := di.ConvertToManifest(&compiler.Options{Name: name})
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, util.Uint160{}, nil, err
|
||||
return nil, util.Uint160{}, nil, fmt.Errorf("failed to create manifest: %w", err)
|
||||
}
|
||||
|
||||
rawManifest, err := json.Marshal(m)
|
||||
|
|
|
@ -1230,7 +1230,7 @@ func TestIsTxStillRelevant(t *testing.T) {
|
|||
currentHeight := contract.Call(addr, "currentIndex", contract.ReadStates)
|
||||
return currentHeight.(int) < %d
|
||||
}`, bc.BlockHeight()+2) // deploy + next block
|
||||
txDeploy, h, _, err := testchain.NewDeployTx(bc, "TestVerify", neoOwner, strings.NewReader(src))
|
||||
txDeploy, h, _, err := testchain.NewDeployTx(bc, "TestVerify", neoOwner, strings.NewReader(src), nil)
|
||||
require.NoError(t, err)
|
||||
txDeploy.ValidUntilBlock = bc.BlockHeight() + 1
|
||||
addSigners(neoOwner, txDeploy)
|
||||
|
|
|
@ -345,7 +345,8 @@ func initBasicChain(t *testing.T, bc *Blockchain) {
|
|||
acc0 := wallet.NewAccountFromPrivateKey(priv0)
|
||||
|
||||
// Push some contract into the chain.
|
||||
txDeploy, cHash := newDeployTx(t, bc, priv0ScriptHash, prefix+"test_contract.go", "Rubl")
|
||||
cfgPath := prefix + "test_contract.yml"
|
||||
txDeploy, cHash := newDeployTx(t, bc, priv0ScriptHash, prefix+"test_contract.go", "Rubl", &cfgPath)
|
||||
txDeploy.Nonce = getNextNonce()
|
||||
txDeploy.ValidUntilBlock = validUntilBlock
|
||||
require.NoError(t, addNetworkFee(bc, txDeploy, acc0))
|
||||
|
@ -431,7 +432,7 @@ func initBasicChain(t *testing.T, bc *Blockchain) {
|
|||
t.Logf("sendRublesTx: %v", transferTx.Hash().StringLE())
|
||||
|
||||
// Push verification contract into the chain.
|
||||
txDeploy2, _ := newDeployTx(t, bc, priv0ScriptHash, prefix+"verification_contract.go", "Verify")
|
||||
txDeploy2, _ := newDeployTx(t, bc, priv0ScriptHash, prefix+"verification_contract.go", "Verify", nil)
|
||||
txDeploy2.Nonce = getNextNonce()
|
||||
txDeploy2.ValidUntilBlock = validUntilBlock
|
||||
require.NoError(t, addNetworkFee(bc, txDeploy2, acc0))
|
||||
|
@ -465,7 +466,7 @@ func initBasicChain(t *testing.T, bc *Blockchain) {
|
|||
t.Logf("Designated Notary node: %s", hex.EncodeToString(ntr.Accounts[0].PrivateKey().PublicKey().Bytes()))
|
||||
|
||||
// Push verification contract with arguments into the chain.
|
||||
txDeploy3, _ := newDeployTx(t, bc, priv0ScriptHash, prefix+"verification_with_args_contract.go", "VerifyWithArgs")
|
||||
txDeploy3, _ := newDeployTx(t, bc, priv0ScriptHash, prefix+"verification_with_args_contract.go", "VerifyWithArgs", nil)
|
||||
txDeploy3.Nonce = getNextNonce()
|
||||
txDeploy3.ValidUntilBlock = validUntilBlock
|
||||
require.NoError(t, addNetworkFee(bc, txDeploy3, acc0))
|
||||
|
@ -474,7 +475,7 @@ func initBasicChain(t *testing.T, bc *Blockchain) {
|
|||
require.NoError(t, bc.AddBlock(b))
|
||||
|
||||
// Compile contract to test `invokescript` RPC call
|
||||
_, _ = newDeployTx(t, bc, priv0ScriptHash, prefix+"invokescript_contract.go", "ContractForInvokescriptTest")
|
||||
_, _ = newDeployTx(t, bc, priv0ScriptHash, prefix+"invokescript_contract.go", "ContractForInvokescriptTest", nil)
|
||||
}
|
||||
|
||||
func newNEP17Transfer(sc, from, to util.Uint160, amount int64, additionalArgs ...interface{}) *transaction.Transaction {
|
||||
|
@ -489,10 +490,10 @@ func newNEP17Transfer(sc, from, to util.Uint160, amount int64, additionalArgs ..
|
|||
return transaction.New(testchain.Network(), script, 11000000)
|
||||
}
|
||||
|
||||
func newDeployTx(t *testing.T, bc *Blockchain, sender util.Uint160, name, ctrName string) (*transaction.Transaction, util.Uint160) {
|
||||
func newDeployTx(t *testing.T, bc *Blockchain, sender util.Uint160, name, ctrName string, cfgName *string) (*transaction.Transaction, util.Uint160) {
|
||||
c, err := ioutil.ReadFile(name)
|
||||
require.NoError(t, err)
|
||||
tx, h, avm, err := testchain.NewDeployTx(bc, ctrName, sender, bytes.NewReader(c))
|
||||
tx, h, avm, err := testchain.NewDeployTx(bc, ctrName, sender, bytes.NewReader(c), cfgName)
|
||||
require.NoError(t, err)
|
||||
t.Logf("contract (%s): \n\tHash: %s\n\tAVM: %s", name, h.StringLE(), base64.StdEncoding.EncodeToString(avm))
|
||||
return tx, h
|
||||
|
|
|
@ -60,8 +60,8 @@ type rpcTestCase struct {
|
|||
check func(t *testing.T, e *executor, result interface{})
|
||||
}
|
||||
|
||||
const testContractHash = "1e1c3024bd955ff3baf7cb92e3b7608c7bb3712b"
|
||||
const deploymentTxHash = "9218bba2a6e145aab5dc21c4bb16a650efdf0b9b16b0d69bd754278363a1d1c2"
|
||||
const testContractHash = "c6ca2347bb84b99807221365c900ec069a265e7c"
|
||||
const deploymentTxHash = "fdd4f9252cde778010d14e9710efeeb80796fd38d778e9943c1d5bb2dc656c99"
|
||||
const genesisBlockHash = "5b60644c6c6f58faca72c70689d7ed1f40c2e795772bd0de5a88e983ad55080c"
|
||||
|
||||
const verifyContractHash = "5bb4bac40e961e334ba7bd36d2496010f67e246e"
|
||||
|
@ -1650,7 +1650,7 @@ func checkNep17Balances(t *testing.T, e *executor, acc interface{}) {
|
|||
},
|
||||
{
|
||||
Asset: e.chain.UtilityTokenHash(),
|
||||
Amount: "68992647820",
|
||||
Amount: "68992456820",
|
||||
LastUpdated: 10,
|
||||
}},
|
||||
Address: testchain.PrivateKeyByID(0).GetScriptHash().StringLE(),
|
||||
|
|
19
pkg/rpc/server/testdata/test_contract.go
vendored
19
pkg/rpc/server/testdata/test_contract.go
vendored
|
@ -1,6 +1,9 @@
|
|||
package testdata
|
||||
|
||||
import (
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
||||
)
|
||||
|
@ -19,7 +22,7 @@ func Init() bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func Transfer(from, to []byte, amount int, data interface{}) bool {
|
||||
func Transfer(from, to interop.Hash160, amount int, data interface{}) bool {
|
||||
ctx := storage.GetContext()
|
||||
if len(from) != 20 {
|
||||
runtime.Log("invalid 'from' address")
|
||||
|
@ -55,22 +58,22 @@ func Transfer(from, to []byte, amount int, data interface{}) bool {
|
|||
storage.Put(ctx, to, toBalance)
|
||||
|
||||
runtime.Notify("Transfer", from, to, amount)
|
||||
|
||||
if management.GetContract(to) != nil {
|
||||
contract.Call(to, "onNEP17Payment", contract.All, from, amount, data)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func BalanceOf(addr []byte) int {
|
||||
func BalanceOf(account interop.Hash160) int {
|
||||
ctx := storage.GetContext()
|
||||
if len(addr) != 20 {
|
||||
runtime.Log("invalid address")
|
||||
return 0
|
||||
if len(account) != 20 {
|
||||
panic("invalid address")
|
||||
}
|
||||
var amount int
|
||||
val := storage.Get(ctx, addr)
|
||||
val := storage.Get(ctx, account)
|
||||
if val != nil {
|
||||
amount = val.(int)
|
||||
}
|
||||
runtime.Notify("balanceOf", addr, amount)
|
||||
return amount
|
||||
}
|
||||
|
||||
|
|
12
pkg/rpc/server/testdata/test_contract.yml
vendored
Normal file
12
pkg/rpc/server/testdata/test_contract.yml
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
name: "Rubl"
|
||||
supportedstandards: ["NEP-17"]
|
||||
safemethods: ["balanceOf", "decimals", "symbol", "totalSupply"]
|
||||
events:
|
||||
- name: Transfer
|
||||
parameters:
|
||||
- name: from
|
||||
type: Hash160
|
||||
- name: to
|
||||
type: Hash160
|
||||
- name: amount
|
||||
type: Integer
|
BIN
pkg/rpc/server/testdata/testblocks.acc
vendored
BIN
pkg/rpc/server/testdata/testblocks.acc
vendored
Binary file not shown.
|
@ -75,7 +75,7 @@ func main() {
|
|||
handleError("can't tranfser GAS", err)
|
||||
lastBlock = addBlock(bc, lastBlock, valScript, txMoveNeo, txMoveGas)
|
||||
|
||||
tx, contractHash, _, err := testchain.NewDeployTx(bc, "DumpContract", h, strings.NewReader(contract))
|
||||
tx, contractHash, _, err := testchain.NewDeployTx(bc, "DumpContract", h, strings.NewReader(contract), nil)
|
||||
handleError("can't create deploy tx", err)
|
||||
tx.NetworkFee = 10_000_000
|
||||
tx.ValidUntilBlock = bc.BlockHeight() + 1
|
||||
|
|
Loading…
Reference in a new issue