├── test_data
├── asset.wasm
├── domain.wasm
├── assetraw.wasm
├── blockapi.wasm
├── callNeo.wasm
├── contract.wasm
├── headerapi.wasm
├── icotest.wasm
├── callNative.wasm
├── rawcontract.wasm
├── callContract.wasm
├── callNativeJson.wasm
├── testwallet.dat
├── testwallet2.dat
├── callNative.c
├── callContract.c
├── rawcontract.c
├── blockapi.c
├── ont.h
├── contract.c
├── assetraw.c
├── asset.c
├── headerapi.c
└── icotest.c
├── testcase
├── vm
│ ├── init.go
│ └── neovm
│ │ ├── call
│ │ ├── init.go
│ │ └── call.go
│ │ ├── cond_loop
│ │ ├── init.go
│ │ ├── switch.go
│ │ ├── for.go
│ │ └── ifelse.go
│ │ ├── init.go
│ │ ├── datatype
│ │ ├── init.go
│ │ ├── integer.go
│ │ ├── boolean.go
│ │ ├── string.go
│ │ ├── array.go
│ │ ├── bytearray.go
│ │ └── returntype.go
│ │ └── operator
│ │ ├── init.go
│ │ ├── ng.go
│ │ ├── selfsub.go
│ │ ├── selfadd.go
│ │ ├── sub.go
│ │ ├── mode.go
│ │ ├── eq.go
│ │ ├── multi.go
│ │ ├── or.go
│ │ ├── and.go
│ │ ├── ne.go
│ │ ├── sl.go
│ │ ├── lr.go
│ │ ├── se.go
│ │ ├── le.go
│ │ ├── leftshift.go
│ │ ├── rightshift.go
│ │ ├── add.go
│ │ └── divide.go
├── smartcontract
│ ├── native
│ │ ├── ontid
│ │ │ ├── init.go
│ │ │ └── common.go
│ │ ├── init.go
│ │ ├── transfer_multi.go
│ │ ├── transfer.go
│ │ ├── withdrawong.go
│ │ └── global_param.go
│ ├── neovm
│ │ ├── deploy_invoke
│ │ │ ├── init.go
│ │ │ ├── deploy.go
│ │ │ ├── contract.go
│ │ │ └── invoke.go
│ │ ├── nep5
│ │ │ └── nep5wallet.dat
│ │ ├── utils
│ │ │ ├── init.go
│ │ │ ├── asbytearray_string.go
│ │ │ ├── concat.go
│ │ │ ├── asstring.go
│ │ │ ├── range.go
│ │ │ ├── take.go
│ │ │ ├── asbiginteger.go
│ │ │ └── asbytearray_biginteger.go
│ │ └── init.go
│ ├── testcase.go
│ └── api
│ │ ├── runtime
│ │ ├── log.go
│ │ ├── notify.go
│ │ └── checkwitness.go
│ │ ├── contract
│ │ ├── create.go
│ │ ├── getcontract.go
│ │ └── destroy.go
│ │ ├── executionengine
│ │ ├── executingscripthash.go
│ │ └── callingscripthash.go
│ │ ├── init.go
│ │ ├── transaction
│ │ ├── gettype.go
│ │ └── gethash.go
│ │ └── storage
│ │ └── storage.go
├── http
│ └── testcase.go
└── testcase.go
├── .gitignore
├── config_test.json
├── README.md
├── log4go.xml
├── main.go
├── common
└── config.go
├── wallet.dat
└── testframework
└── framework_context.go
/test_data/asset.wasm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ontio/ontology-test/HEAD/test_data/asset.wasm
--------------------------------------------------------------------------------
/test_data/domain.wasm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ontio/ontology-test/HEAD/test_data/domain.wasm
--------------------------------------------------------------------------------
/test_data/assetraw.wasm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ontio/ontology-test/HEAD/test_data/assetraw.wasm
--------------------------------------------------------------------------------
/test_data/blockapi.wasm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ontio/ontology-test/HEAD/test_data/blockapi.wasm
--------------------------------------------------------------------------------
/test_data/callNeo.wasm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ontio/ontology-test/HEAD/test_data/callNeo.wasm
--------------------------------------------------------------------------------
/test_data/contract.wasm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ontio/ontology-test/HEAD/test_data/contract.wasm
--------------------------------------------------------------------------------
/test_data/headerapi.wasm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ontio/ontology-test/HEAD/test_data/headerapi.wasm
--------------------------------------------------------------------------------
/test_data/icotest.wasm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ontio/ontology-test/HEAD/test_data/icotest.wasm
--------------------------------------------------------------------------------
/test_data/callNative.wasm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ontio/ontology-test/HEAD/test_data/callNative.wasm
--------------------------------------------------------------------------------
/test_data/rawcontract.wasm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ontio/ontology-test/HEAD/test_data/rawcontract.wasm
--------------------------------------------------------------------------------
/test_data/callContract.wasm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ontio/ontology-test/HEAD/test_data/callContract.wasm
--------------------------------------------------------------------------------
/test_data/callNativeJson.wasm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ontio/ontology-test/HEAD/test_data/callNativeJson.wasm
--------------------------------------------------------------------------------
/testcase/vm/init.go:
--------------------------------------------------------------------------------
1 | package vm
2 |
3 | import "github.com/ontio/ontology-test/testcase/vm/neovm"
4 |
5 | func TestVM() {
6 | neovm.TestNeoVM()
7 | }
8 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/*
2 | idea/*
3 | temp/*
4 | build/
5 | *.patch
6 | *.log
7 | *.rej
8 | vendor/*
9 | *.iml
10 | ontology-test
11 | config_test.json
12 | wallet.dat
13 | main.exe
--------------------------------------------------------------------------------
/testcase/vm/neovm/call/init.go:
--------------------------------------------------------------------------------
1 | package call
2 |
3 | import (
4 | "github.com/ontio/ontology-test/testframework"
5 | )
6 |
7 | func TestCall() {
8 | testframework.TFramework.RegTestCase("TestCallContractStatic", TestCallContractStatic)
9 | }
10 |
--------------------------------------------------------------------------------
/testcase/smartcontract/native/ontid/init.go:
--------------------------------------------------------------------------------
1 | package ontid
2 |
3 | import "github.com/ontio/ontology-test/testframework"
4 |
5 | func TestNativeOntID() {
6 | testframework.TFramework.RegTestCase("ontid", TestID)
7 | testframework.TFramework.RegTestCase("ontid-attr", TestAttr)
8 | }
9 |
--------------------------------------------------------------------------------
/config_test.json:
--------------------------------------------------------------------------------
1 | {
2 | "RestfulAddress":"http://localhost:20334",
3 | "WebSocketAddress":"http://localhost:20335",
4 | "JsonRpcAddress":"http://localhost:20336",
5 | "WalletFile":"./wallet.dat",
6 | "Password":"wangbing",
7 | "GasPrice":0,
8 | "GasLimit":100000000
9 | }
10 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/cond_loop/init.go:
--------------------------------------------------------------------------------
1 | package cond_loop
2 |
3 | import (
4 | "github.com/ontio/ontology-test/testframework"
5 | )
6 |
7 | func TestCondLoop() {
8 | testframework.TFramework.RegTestCase("TestIfElse", TestIfElse)
9 | testframework.TFramework.RegTestCase("TestSwitch", TestSwitch)
10 | testframework.TFramework.RegTestCase("TestFor", TestFor)
11 | }
12 |
--------------------------------------------------------------------------------
/testcase/smartcontract/neovm/deploy_invoke/init.go:
--------------------------------------------------------------------------------
1 | package deploy_invoke
2 |
3 | import (
4 | "github.com/ontio/ontology-test/testframework"
5 | )
6 |
7 | func TestDeployInvoke() {
8 | testframework.TFramework.RegTestCase("TestDeploySmartContract", TestDeploySmartContract)
9 | testframework.TFramework.RegTestCase("TestInvokeSmartContract", TestInvokeSmartContract)
10 | testframework.TFramework.RegTestCase("TestDomainSmartContract", TestDomainSmartContract)
11 | }
12 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/init.go:
--------------------------------------------------------------------------------
1 | package neovm
2 |
3 | import (
4 | "github.com/ontio/ontology-test/testcase/vm/neovm/call"
5 | "github.com/ontio/ontology-test/testcase/vm/neovm/cond_loop"
6 | "github.com/ontio/ontology-test/testcase/vm/neovm/datatype"
7 | "github.com/ontio/ontology-test/testcase/vm/neovm/operator"
8 | )
9 |
10 | func TestNeoVM() {
11 | operator.TestNeoVMOperator()
12 | datatype.TestDataType()
13 | cond_loop.TestCondLoop()
14 | call.TestCall()
15 | }
16 |
--------------------------------------------------------------------------------
/testcase/smartcontract/neovm/nep5/nep5wallet.dat:
--------------------------------------------------------------------------------
1 | {"PublicKeyHash":"01a6f4bc8853512eec042fccdda1a4f1636ca7cd","PrivateKeyEncrypted":"b8bab39a740e93ea7364b8d46a077a75d0850d4439494fa888c4500ba747d131a4d1470fde79d80e32d723792174e26a5827fb9cb77deb1351a994bdab0afc5380ac60d4473630e6f2061e5bb5d35623","Address":"","ScriptHash":"","RawData":"","PasswordHash":"3180b4071170db0ae9f666167ed379f53468463f152e3c3cfb57d1de45fd01d6","IV":"753f0fc11cb59cb748de4645f0286736","MasterKey":"b0802532ae3d049592b560c56a40158b56e4a0ce2ffa4a5e58812a95c1ad3f03bdbfe07b69f4d9e672f0e061307f3ae5"}
--------------------------------------------------------------------------------
/test_data/testwallet.dat:
--------------------------------------------------------------------------------
1 | {"name":"MyWallet","version":"1.1","scrypt":{"p":8,"n":16384,"r":8,"dkLen":64},"identities":null,"accounts":[{"address":"TA8Xe297g4wGj67maMYZFmdfk9i2riVNrC","enc-alg":"aes-256-ctr","key":"mOQehSKnmMtXVwqbMY1tBFJTFiTMtt5cMKaBUQxNc9s=","hash":"sha256","algorithm":"ECDSA","parameters":{"curve":"P-256"},"label":"","publicKey":"1202020297274dc0cc6068b58b9103791d609aec778834f1d7fa82b3ead3d8e579dcc1","signatureScheme":"SHA256withECDSA","isDefault":true,"lock":false,"passwordHash":"8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92"}],"extra":"null"}
--------------------------------------------------------------------------------
/test_data/testwallet2.dat:
--------------------------------------------------------------------------------
1 | {"name":"MyWallet","version":"1.1","scrypt":{"p":8,"n":16384,"r":8,"dkLen":64},"identities":null,"accounts":[{"address":"TA8aqS3PyDcFG567qa2qJuufHH1M82zVig","enc-alg":"aes-256-ctr","key":"usLr1BXrb+oY42o0lG7fa+3DQtEIlLVDNNOmCflDWeo=","hash":"sha256","algorithm":"ECDSA","parameters":{"curve":"P-256"},"label":"","publicKey":"120203cd3b49c9c637ac3fcc1f66e7a87163ca433c8ae8d6591ab6cc3c33798543a875","signatureScheme":"SHA256withECDSA","isDefault":true,"lock":false,"passwordHash":"8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92"}],"extra":"null"}
--------------------------------------------------------------------------------
/testcase/vm/neovm/datatype/init.go:
--------------------------------------------------------------------------------
1 | package datatype
2 |
3 | import (
4 | "github.com/ontio/ontology-test/testframework"
5 | )
6 |
7 | func TestDataType() {
8 | testframework.TFramework.RegTestCase("TestBoolean", TestBoolean)
9 | testframework.TFramework.RegTestCase("TestInteger", TestInteger)
10 | testframework.TFramework.RegTestCase("TestString", TestString)
11 | testframework.TFramework.RegTestCase("TestArray", TestArray)
12 | testframework.TFramework.RegTestCase("TestReturnType", TestReturnType)
13 | testframework.TFramework.RegTestCase("TestByteArray", TestByteArray)
14 | }
15 |
--------------------------------------------------------------------------------
/testcase/smartcontract/neovm/utils/init.go:
--------------------------------------------------------------------------------
1 | package utils
2 |
3 | import (
4 | "github.com/ontio/ontology-test/testframework"
5 | )
6 |
7 | func TestUtils() {
8 | testframework.TFramework.RegTestCase("TestAsBigInteger", TestAsBigInteger)
9 | testframework.TFramework.RegTestCase("TestAsByteArrayBigInteger", TestAsByteArrayBigInteger)
10 | testframework.TFramework.RegTestCase("TestAsByteArrayString", TestAsByteArrayString)
11 | testframework.TFramework.RegTestCase("TestAsString", TestAsString)
12 | testframework.TFramework.RegTestCase("TestRange", TestRange)
13 | testframework.TFramework.RegTestCase("TestTake", TestTake)
14 | }
15 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Ontology Test Framework
2 | Ontology Test Framework is a light-weight test framework for ontology. Integration ontology-sdk to run Ontology test case.
3 |
4 | ## How to use?
5 |
6 | 1. copy wallet file from bookkeeper ontology node to ontology-test. Otherwise some testcase will failed because of balance of ont is zero.
7 | 2. Set rpc server address of ontology, wallet file and password in config_test.json config file.
8 |
9 | ```
10 | {
11 | "JsonRpcAddress":"http://localhost:20336",
12 | "RestfulAddress":"http://localhost:20334",
13 | "WebSocketAddress":"http://localhost:20335",
14 | "WalletFile":"./wallet.dat",
15 | "Password":"your wallet password"
16 | }
17 | ```
18 |
19 | Then start to run.
20 |
21 |
22 |
--------------------------------------------------------------------------------
/testcase/smartcontract/native/init.go:
--------------------------------------------------------------------------------
1 | package native
2 |
3 | import (
4 | "github.com/ontio/ontology-test/testcase/smartcontract/native/auth"
5 | "github.com/ontio/ontology-test/testcase/smartcontract/native/ontid"
6 | "github.com/ontio/ontology-test/testframework"
7 | )
8 |
9 | func TestNative() {
10 | testframework.TFramework.RegTestCase("TestOntTransfer", TestOntTransfer)
11 | testframework.TFramework.RegTestCase("TestOntTransferMulti", TestOntTransferMulti)
12 | testframework.TFramework.RegTestCase("TestWithdrawONG", TestWithdrawONG)
13 | testframework.TFramework.RegTestCase("TestGlobalParam", TestGlobalParam)
14 | testframework.TFramework.RegTestCase("TestAuth", auth.TestAuthContract)
15 | ontid.TestNativeOntID()
16 | }
17 |
--------------------------------------------------------------------------------
/testcase/http/testcase.go:
--------------------------------------------------------------------------------
1 | package http
2 |
3 | import "github.com/ontio/ontology-test/testframework"
4 |
5 | func TestHttp() {
6 | testframework.TFramework.RegTestCase("TestGetBlockByHeight", TestGetBlockByHeight)
7 | testframework.TFramework.RegTestCase("TestGetBlockByHash", TestGetBlockByHash)
8 | testframework.TFramework.RegTestCase("TestGetCurrentBlockHeight", TestGetCurrentBlockHeight)
9 | testframework.TFramework.RegTestCase("TestGetBlockHash", TestGetBlockHash)
10 | testframework.TFramework.RegTestCase("TestGetCurrentBlockHash", TestGetCurrentBlockHash)
11 | testframework.TFramework.RegTestCase("TestGetRawTransaction", TestGetRawTransaction)
12 | testframework.TFramework.RegTestCase("TestGetSmartContract", TestGetSmartContract)
13 | testframework.TFramework.RegTestCase("TestGetSmartContractEvent", TestGetSmartContractEvent)
14 | testframework.TFramework.RegTestCase("TestGetStorage", TestGetStorage)
15 | //testframework.TFramework.RegTestCase("TestGetVbftInfo", TestGetVbftInfo)
16 | }
17 |
--------------------------------------------------------------------------------
/testcase/smartcontract/neovm/deploy_invoke/deploy.go:
--------------------------------------------------------------------------------
1 | package deploy_invoke
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-test/testframework"
7 | )
8 |
9 | func TestDeploySmartContract(ctx *testframework.TestFrameworkContext) bool {
10 | signer, err := ctx.GetDefaultAccount()
11 | if err != nil {
12 | ctx.LogError("TestDeploySmartContract GetDefaultAccount error:%s", err)
13 | return false
14 | }
15 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
16 | signer,
17 | true,
18 | contractCode,
19 | "TestDeploySmartContract",
20 | "1.0",
21 | "",
22 | "",
23 | "",
24 | )
25 |
26 | ctx.LogInfo("CodeAddress:%x", contractCodeAddress)
27 | if err != nil {
28 | ctx.LogError("TestDeploySmartContract DeploySmartContract error:%s", err)
29 | return false
30 | }
31 | //WaitForGenerateBlock
32 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
33 |
34 | if err != nil {
35 | ctx.LogError("TestDeploySmartContract WaitForGenerateBlock error:%s", err)
36 | return false
37 | }
38 | return true
39 | }
40 |
--------------------------------------------------------------------------------
/testcase/smartcontract/neovm/init.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | */
18 | package neovm
19 |
20 | import (
21 | "github.com/ontio/ontology-test/testcase/smartcontract/neovm/deploy_invoke"
22 | "github.com/ontio/ontology-test/testcase/smartcontract/neovm/utils"
23 | )
24 |
25 | func TestNeoVM() {
26 | deploy_invoke.TestDeployInvoke()
27 |
28 | utils.TestUtils()
29 | }
30 |
--------------------------------------------------------------------------------
/testcase/smartcontract/testcase.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | */
18 | package smartcontract
19 |
20 | import (
21 | "github.com/ontio/ontology-test/testcase/smartcontract/api"
22 | "github.com/ontio/ontology-test/testcase/smartcontract/native"
23 | "github.com/ontio/ontology-test/testcase/smartcontract/neovm"
24 | )
25 |
26 | //Register test case
27 | func TestSmartContract() {
28 | api.TestSmartContractApi()
29 | native.TestNative()
30 | neovm.TestNeoVM()
31 | }
--------------------------------------------------------------------------------
/testcase/smartcontract/neovm/deploy_invoke/contract.go:
--------------------------------------------------------------------------------
1 | package deploy_invoke
2 |
3 | import "github.com/ontio/ontology-go-sdk/utils"
4 |
5 | //Smart contract code in hex string after compiled
6 | var contractCode = "51c56b6161681953797374656d2e53746f726167652e476574436f6e746578740548656c6c6f05576f726c64615272681253797374656d2e53746f726167652e50757461610568656c6c6f05776f726c64017b615272087472616e7366657254c1681553797374656d2e52756e74696d652e4e6f7469667961033132336c766b00527ac46203006c766b00c3616c7566"
7 |
8 | //Address of smart contract
9 | var contractCodeAddress, _ = utils.GetContractAddress(contractCode)
10 |
11 | /*
12 | using Neo.SmartContract.Framework;
13 | using Neo.SmartContract.Framework.Services.Neo;
14 | using Neo.SmartContract.Framework.Services.System;
15 | using System;
16 | using System.ComponentModel;
17 | using System.Numerics;
18 |
19 | namespace NeoContract3
20 | {
21 | public class Contract1 : SmartContract
22 | {
23 | [DisplayName("transfer")]
24 | public static event Action Transferred;
25 |
26 | public static object Main()
27 | {
28 | Storage.Put(Storage.CurrentContext, "Hello", "World");
29 | Transferred("hello".AsByteArray(), "world".AsByteArray(), 123);
30 | return "123";
31 | }
32 | }
33 | }
34 | */
35 |
--------------------------------------------------------------------------------
/log4go.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | stdout
4 | console
5 |
6 | DEBUG
7 | [%D %T] [%L] %M
8 |
9 |
10 | file
11 | file
12 | DEBUG
13 | ./log/test.log
14 |
25 | [%D %T] [%L] %M
26 | true
27 | 100M
28 | 0K
29 | true
30 |
31 |
32 |
--------------------------------------------------------------------------------
/test_data/callNative.c:
--------------------------------------------------------------------------------
1 | #include "ont.h"
2 | /*
3 | *this is the common standard interface of ontology wasm contract
4 | */
5 | char * invoke(char * method,char * args){
6 |
7 | if(strcmp(method,"transferont") == 0){
8 | char * from = ONT_ReadStringParam(args);
9 | char * to = ONT_ReadStringParam(args);
10 | long long ontAmount = ONT_ReadInt64Param(args);
11 | struct State{
12 | int ver;
13 | char * from;
14 | char * to ;
15 | long long amount;
16 | };
17 |
18 | struct Transfer {
19 | int ver;
20 | struct State * states
21 | };
22 |
23 | struct State * state = (struct State*)malloc(sizeof(struct State));
24 | state->ver = 1;
25 | state->amount = ontAmount;
26 | state->from = from;
27 | state->to = to;
28 |
29 | struct Transfer * transfer =(struct Transfer*)malloc(sizeof(struct Transfer));
30 | transfer->ver = 1;
31 | transfer->states = state;
32 | //1. call native contract to tranfer ont
33 |
34 | char * args = ONT_MarshalNativeParams(transfer);
35 | char * result = ONT_CallContract("ff00000000000000000000000000000000000001","","transfer",args);
36 | char * res;
37 | if (strcmp(result,"true")==0){
38 | res = ONT_JsonMashalResult("transfer succeed","string",1);
39 | }else{
40 | res = ONT_JsonMashalResult("transfer failed","string",0);
41 | }
42 | ONT_Runtime_Notify(res);
43 | return res;
44 | }
45 | }
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/init.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import "github.com/ontio/ontology-test/testframework"
4 |
5 | func TestNeoVMOperator() {
6 | testframework.TFramework.RegTestCase("TestOperationAdd", TestOperationAdd)
7 | testframework.TFramework.RegTestCase("TestOperationSub", TestOperationSub)
8 | testframework.TFramework.RegTestCase("TestOperationMulti", TestOperationMulti)
9 | testframework.TFramework.RegTestCase("TestOperationDivide", TestOperationDivide)
10 | testframework.TFramework.RegTestCase("TestOperationSelfAdd", TestOperationSelfAdd)
11 | testframework.TFramework.RegTestCase("TestOperationSelfSub", TestOperationSelfSub)
12 | testframework.TFramework.RegTestCase("TestOperationLarger", TestOperationLarger)
13 | testframework.TFramework.RegTestCase("TestOperationLargerEqual", TestOperationLargerEqual)
14 | testframework.TFramework.RegTestCase("TestOperationSmaller", TestOperationSmaller)
15 | testframework.TFramework.RegTestCase("TestOperationSmallerEqual", TestOperationSmallerEqual)
16 | testframework.TFramework.RegTestCase("TestOperationEqual", TestOperationEqual)
17 | testframework.TFramework.RegTestCase("TestOperationNotEqual", TestOperationNotEqual)
18 | testframework.TFramework.RegTestCase("TestOperationNegative", TestOperationNegative)
19 | testframework.TFramework.RegTestCase("TestOperationOr", TestOperationOr)
20 | testframework.TFramework.RegTestCase("TestOperationAnd", TestOperationAnd)
21 | testframework.TFramework.RegTestCase("TestOperationLeftShift", TestOperationLeftShift)
22 | testframework.TFramework.RegTestCase("TestOperationRightShift", TestOperationRightShift)
23 | }
24 |
--------------------------------------------------------------------------------
/testcase/smartcontract/api/runtime/log.go:
--------------------------------------------------------------------------------
1 | package runtime
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | )
9 |
10 | /**
11 | using Neo.SmartContract.Framework;
12 | using Neo.SmartContract.Framework.Services.Neo;
13 | using System;
14 | using System.ComponentModel;
15 | using System.Numerics;
16 |
17 | public class HelloWorld : SmartContract
18 | {
19 | public static void Main(string msg)
20 | {
21 | Runtime.Log(msg);
22 | }
23 | }
24 | */
25 |
26 | func TestRuntimLog(ctx *testframework.TestFrameworkContext) bool {
27 | code := "51c56b6c766b00527ac4616c766b00c361681253797374656d2e52756e74696d652e4c6f6761616c7566"
28 | codeAddr, _ := utils.GetContractAddress(code)
29 | signer, err := ctx.GetDefaultAccount()
30 |
31 | if err != nil {
32 | ctx.LogError("TestRuntimLog - GetDefaultAccount error: %s", err)
33 | return false
34 | }
35 |
36 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
37 | signer,
38 | true,
39 | code,
40 | "TestRuntimLog",
41 | "",
42 | "",
43 | "",
44 | "")
45 |
46 | //等待出块
47 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 2)
48 |
49 | if err != nil {
50 | ctx.LogError("TestRuntimLog WaitForGenerateBlock error:%s", err)
51 | return false
52 | }
53 |
54 | txHash, err := ctx.Ont.NeoVM.InvokeNeoVMContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
55 | signer,
56 | codeAddr,
57 | []interface{}{"ontology"})
58 |
59 | if err != nil {
60 | ctx.LogError("TestRuntimLog InvokeSmartContract error:%s", err)
61 | return false
62 | }
63 | ctx.LogInfo("TestRuntimLog invoke Tx:%s\n", txHash.ToHexString())
64 | return true
65 | }
66 |
--------------------------------------------------------------------------------
/testcase/smartcontract/api/contract/create.go:
--------------------------------------------------------------------------------
1 | package contract
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | )
9 |
10 | /*
11 |
12 | using Neo.SmartContract.Framework;
13 | using Neo.SmartContract.Framework.Services.Neo;
14 |
15 | class A : SmartContract
16 | {
17 | public static string Main()
18 | {
19 | return "Hello World!";
20 | }
21 | }
22 |
23 | code 51c56b610c48656c6c6f20576f726c64216c766b00527ac46203006c766b00c3616c7566
24 | */
25 |
26 | func TestContractCreate(ctx *testframework.TestFrameworkContext) bool {
27 | code := "51c56b610c48656c6c6f20576f726c64216c766b00527ac46203006c766b00c3616c7566"
28 | codeAddr, _ := utils.GetContractAddress(code)
29 |
30 | signer, err := ctx.GetDefaultAccount()
31 | if err != nil {
32 | ctx.LogError("TestContractCreate - GetDefaultAccount error: %s", err)
33 | return false
34 | }
35 |
36 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
37 | signer,
38 | true,
39 | code,
40 | "TestContractCreate",
41 | "",
42 | "",
43 | "",
44 | "")
45 |
46 | if err != nil {
47 | ctx.LogError("TestContractCreate DeploySmartContract error: %s", err)
48 | return false
49 | }
50 |
51 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
52 | if err != nil {
53 | ctx.LogError("TestContractCreate WaitForGenerateBlock error: %s", err)
54 | return false
55 | }
56 |
57 | _, err = ctx.Ont.NeoVM.InvokeNeoVMContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
58 | signer,
59 | codeAddr,
60 | []interface{}{0})
61 |
62 | if err != nil {
63 | ctx.LogError("TestContractCreate InvokeSmartContract error: %s", err)
64 | return false
65 | }
66 |
67 | return true
68 | }
69 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/datatype/integer.go:
--------------------------------------------------------------------------------
1 | package datatype
2 |
3 | import (
4 | "time"
5 | "github.com/ontio/ontology-go-sdk/utils"
6 | "github.com/ontio/ontology-test/testframework"
7 | )
8 |
9 | func TestInteger(ctx *testframework.TestFrameworkContext) bool {
10 | code := "00C56B5A616C7566"
11 | codeAddress, _ := utils.GetContractAddress(code)
12 | signer, err := ctx.GetDefaultAccount()
13 | if err != nil {
14 | ctx.LogError("TestInteger GetDefaultAccount error:%s", err)
15 | return false
16 | }
17 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
18 | signer,
19 | false,
20 | code,
21 | "TestInteger",
22 | "1.0",
23 | "",
24 | "",
25 | "",
26 | )
27 | if err != nil {
28 | ctx.LogError("TestInteger DeploySmartContract error:%s", err)
29 | return false
30 | }
31 | //等待出块
32 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
33 | if err != nil {
34 | ctx.LogError("TestInteger WaitForGenerateBlock error:%s", err)
35 | return false
36 | }
37 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
38 | codeAddress,
39 | []interface{}{},
40 | )
41 | if err != nil {
42 | ctx.LogError("TestInteger InvokeSmartContract error:%s", err)
43 | return false
44 | }
45 | resValue, err := res.Result.ToInteger()
46 | if err != nil {
47 | ctx.LogError("TestInteger Result.ToInteger error:%s", err)
48 | return false
49 | }
50 | err = ctx.AssertToInt(resValue, 10)
51 | if err != nil {
52 | ctx.LogError("TestInteger test failed %s", err)
53 | return false
54 | }
55 | return true
56 | }
57 |
58 | /*
59 | using Neo.SmartContract.Framework;
60 | using Neo.SmartContract.Framework.Services.Neo;
61 |
62 | class A : SmartContract
63 | {
64 | public static int Main()
65 | {
66 | return 10;
67 | }
68 | }
69 | */
70 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/datatype/boolean.go:
--------------------------------------------------------------------------------
1 | package datatype
2 |
3 | import (
4 | "time"
5 | "github.com/ontio/ontology-go-sdk/utils"
6 | "github.com/ontio/ontology-test/testframework"
7 | )
8 |
9 | func TestBoolean(ctx *testframework.TestFrameworkContext) bool {
10 | code := "00C56B51616C7566"
11 | codeAddress, _ := utils.GetContractAddress(code)
12 | signer, err := ctx.GetDefaultAccount()
13 | if err != nil {
14 | ctx.LogError("TestReturnType GetDefaultAccount error:%s", err)
15 | return false
16 | }
17 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
18 | signer,
19 |
20 | false,
21 | code,
22 | "TestReturnType",
23 | "1.0",
24 | "",
25 | "",
26 | "",
27 | )
28 | if err != nil {
29 | ctx.LogError("TestBoolean DeploySmartContract error:%s", err)
30 | return false
31 | }
32 | //等待出块
33 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
34 | if err != nil {
35 | ctx.LogError("TestBoolean WaitForGenerateBlock error:%s", err)
36 | return false
37 | }
38 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
39 | codeAddress,
40 | []interface{}{},
41 | )
42 | if err != nil {
43 | ctx.LogError("TestBoolean InvokeSmartContract error:%s", err)
44 | return false
45 | }
46 | resValue, err := res.Result.ToBool()
47 | if err != nil {
48 | ctx.LogError("TestBoolean Result.ToBool error:%s", err)
49 | return false
50 | }
51 | err = ctx.AssertToBoolean(resValue, true)
52 | if err != nil {
53 | ctx.LogError("TestBoolean test failed %s", err)
54 | return false
55 | }
56 | return true
57 | }
58 |
59 | /*
60 | using Neo.SmartContract.Framework;
61 | using Neo.SmartContract.Framework.Services.Neo;
62 |
63 | class A : SmartContract
64 | {
65 | public static bool Main()
66 | {
67 | return true;
68 | }
69 | }
70 | */
71 |
--------------------------------------------------------------------------------
/test_data/callContract.c:
--------------------------------------------------------------------------------
1 | #include "ont.h"
2 | char * invoke(char * method,char * args){
3 |
4 | if (strcmp(method ,"init")==0 ){
5 | return "init success!";
6 | }
7 |
8 | if (strcmp(method, "getValue")==0){
9 |
10 | struct Input{
11 | char * key;
12 | };
13 | struct Input * input = (struct Input *)malloc(sizeof(struct Input));
14 | ONT_JsonUnmashalInput(input,sizeof(struct Input),args);
15 |
16 | struct Param{
17 | char * ptype;
18 | char * pvalue;
19 | };
20 | struct Param * newargs = (struct Param *)malloc(sizeof(struct Param));
21 |
22 | newargs -> ptype = "string";
23 | newargs -> pvalue = input->key;
24 |
25 | char * res = ONT_CallContract("90e45f98d7c49851fe7b6e33eef7a34b9507c493","","getStorage",ONT_JsonMashalParams(newargs));
26 | ONT_Runtime_Notify(res);
27 | return res;
28 | }
29 | if (strcmp(method,"putValue") == 0){
30 |
31 | struct Input{
32 | char * key;
33 | char * value;
34 | };
35 | struct Input * input = (struct Param *)malloc(sizeof(struct Input));
36 | ONT_JsonUnmashalInput(input,sizeof(struct Input),args);
37 |
38 | struct Param{
39 | char * ptype;
40 | char * pvalue;
41 | };
42 |
43 | struct Param * newargs = (struct Param *)malloc(sizeof(struct Param)*2);
44 |
45 | newargs[0].ptype = "string";
46 | newargs[0].pvalue = input->key;
47 |
48 | newargs[1].ptype = "string";
49 | newargs[1].pvalue = input->value;
50 |
51 | char * res = ONT_CallContract("90e45f98d7c49851fe7b6e33eef7a34b9507c493","","addStorage",ONT_JsonMashalParams(newargs));
52 | ONT_Runtime_Notify(res);
53 | return res;
54 |
55 | }
56 | }
--------------------------------------------------------------------------------
/testcase/vm/neovm/datatype/string.go:
--------------------------------------------------------------------------------
1 | package datatype
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | )
9 |
10 | func TestString(ctx *testframework.TestFrameworkContext) bool {
11 | code := "00C56B0B48656C6C6F20576F726C64616C7566"
12 | codeAddress, _ := utils.GetContractAddress(code)
13 | signer, err := ctx.GetDefaultAccount()
14 | if err != nil {
15 | ctx.LogError("TestString GetDefaultAccount error:%s", err)
16 | return false
17 | }
18 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
19 | signer,
20 |
21 | false,
22 | code,
23 | "TestString",
24 | "1.0",
25 | "",
26 | "",
27 | "",
28 | )
29 | if err != nil {
30 | ctx.LogError("TestString DeploySmartContract error:%s", err)
31 | return false
32 | }
33 | //等待出块
34 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
35 | if err != nil {
36 | ctx.LogError("TestString WaitForGenerateBlock error:%s", err)
37 | return false
38 | }
39 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
40 | codeAddress,
41 | []interface{}{},
42 | )
43 | if err != nil {
44 | ctx.LogError("TestString InvokeSmartContract error:%s", err)
45 | return false
46 | }
47 | resValue, err := res.Result.ToString()
48 | if err != nil {
49 | ctx.LogError("TestString Result.ToString error:%s", err)
50 | return false
51 | }
52 | err = ctx.AssertToString(resValue, "Hello World")
53 | if err != nil {
54 | ctx.LogError("TestString test failed %s", err)
55 | return false
56 | }
57 | return true
58 | }
59 |
60 | /*
61 | using Neo.SmartContract.Framework;
62 | using Neo.SmartContract.Framework.Services.Neo;
63 |
64 | class A : SmartContract
65 | {
66 | public static string Main()
67 | {
68 | return "Hello World";
69 | }
70 | }
71 | */
72 |
--------------------------------------------------------------------------------
/testcase/smartcontract/neovm/utils/asbytearray_string.go:
--------------------------------------------------------------------------------
1 | package utils
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | )
9 |
10 | func TestAsByteArrayString(ctx *testframework.TestFrameworkContext) bool {
11 | code := "51C56B6C766B00527AC46C766B00C3616C756600"
12 | codeAddress, _ := utils.GetContractAddress(code)
13 | signer, err := ctx.GetDefaultAccount()
14 | if err != nil {
15 | ctx.LogError("TestAsByteArrayString GetDefaultAccount error:%s", err)
16 | return false
17 | }
18 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
19 | signer,
20 | false,
21 | code,
22 | "TestAsByteArrayString",
23 | "1.0",
24 | "",
25 | "",
26 | "",
27 | )
28 | if err != nil {
29 | ctx.LogError("TestAsByteArrayString DeploySmartContract error:%s", err)
30 | return false
31 | }
32 | //等待出块
33 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
34 | if err != nil {
35 | ctx.LogError("TestAsByteArrayString WaitForGenerateBlock error:%s", err)
36 | return false
37 | }
38 |
39 | input := "Hello World"
40 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
41 | codeAddress,
42 | []interface{}{input},
43 | )
44 | if err != nil {
45 | ctx.LogError("TestAsByteArrayString InvokeSmartContract error:%s", err)
46 | return false
47 | }
48 | resValue,err := res.Result.ToByteArray()
49 | if err != nil {
50 | ctx.LogError("TestAsByteArrayString Result.ToByteArray error:%s", err)
51 | return false
52 | }
53 | err = ctx.AssertToByteArray(resValue, []byte(input))
54 | if err != nil {
55 | ctx.LogError("TestAsByteArrayString test failed %s", err)
56 | return false
57 | }
58 | return true
59 | }
60 |
61 | /*
62 | using Neo.SmartContract.Framework;
63 | using Neo.SmartContract.Framework.Services.Neo;
64 |
65 | class A : SmartContract
66 | {
67 | public static byte[] Main(string arg)
68 | {
69 | return arg.AsByteArray();
70 | }
71 | }
72 | */
73 |
--------------------------------------------------------------------------------
/testcase/smartcontract/neovm/utils/concat.go:
--------------------------------------------------------------------------------
1 | package utils
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | )
9 |
10 | func TestConcat(ctx *testframework.TestFrameworkContext) bool {
11 | code := "52C56B6C766B00527AC46C766B51527AC46C766B00C36C766B51C37E616C7566"
12 | codeAddress, _ := utils.GetContractAddress(code)
13 | signer, err := ctx.GetDefaultAccount()
14 | if err != nil {
15 | ctx.LogError("TestConcat GetDefaultAccount error:%s", err)
16 | return false
17 | }
18 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
19 | signer,
20 | false,
21 | code,
22 | "TestConcat",
23 | "1.0",
24 | "",
25 | "",
26 | "",
27 | )
28 | if err != nil {
29 | ctx.LogError("TestConcat DeploySmartContract error:%s", err)
30 | return false
31 | }
32 | //等待出块
33 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
34 | if err != nil {
35 | ctx.LogError("TestConcat WaitForGenerateBlock error:%s", err)
36 | return false
37 | }
38 | input1 := "Hello"
39 | input2 := "World"
40 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
41 | codeAddress,
42 | []interface{}{input1, input2},
43 | )
44 | if err != nil {
45 | ctx.LogError("TestConcat InvokeSmartContract error:%s", err)
46 | return false
47 | }
48 | resValue, err := res.Result.ToByteArray()
49 | if err != nil {
50 | ctx.LogError("TestConcat Result.ToByteArray error:%s", err)
51 | return false
52 | }
53 | err = ctx.AssertToByteArray(resValue, []byte(string(input1)+string(input2)))
54 | if err != nil {
55 | ctx.LogError("TestConcat test failed %s", err)
56 | return false
57 | }
58 | return true
59 | }
60 |
61 | /*
62 | using Neo.SmartContract.Framework;
63 | using Neo.SmartContract.Framework.Services.Neo;
64 |
65 | public class HelloWorld : SmartContract
66 | {
67 | public static byte[] Main(byte[] arg1, byte[] arg2)
68 | {
69 | return arg1.Concat(arg2);
70 | }
71 | }
72 | */
73 |
--------------------------------------------------------------------------------
/test_data/rawcontract.c:
--------------------------------------------------------------------------------
1 | #include "ont.h"
2 |
3 | int add(int a, int b ){
4 | return a + b;
5 | }
6 |
7 | char * concat(char * a, char * b){
8 | int lena = arrayLen(a);
9 | int lenb = arrayLen(b);
10 | char * res = (char *)malloc((lena + lenb)*sizeof(char));
11 | for (int i = 0 ;i < lena ;i++){
12 | res[i] = a[i];
13 | }
14 |
15 | for (int j = 0; j < lenb ;j++){
16 | res[lenb + j] = b[j];
17 | }
18 | return res;
19 | }
20 |
21 |
22 | int sumArray(int * a, int * b){
23 |
24 | int res = 0;
25 | int lena = arrayLen(a);
26 | int lenb = arrayLen(b);
27 |
28 | for (int i = 0;i.
17 | */
18 |
19 | package api
20 |
21 | import (
22 | "github.com/ontio/ontology-test/testcase/smartcontract/api/blockchain"
23 | "github.com/ontio/ontology-test/testcase/smartcontract/api/contract"
24 | "github.com/ontio/ontology-test/testcase/smartcontract/api/executionengine"
25 | "github.com/ontio/ontology-test/testcase/smartcontract/api/runtime"
26 | "github.com/ontio/ontology-test/testcase/smartcontract/api/storage"
27 | "github.com/ontio/ontology-test/testcase/smartcontract/api/transaction"
28 | "github.com/ontio/ontology-test/testframework"
29 | )
30 |
31 | func TestSmartContractApi() {
32 | testframework.TFramework.RegTestCase("TestGetBlock", blockchain.TestGetBlock)
33 | testframework.TFramework.RegTestCase("TestGetContract", contract.TestGetContract)
34 | testframework.TFramework.RegTestCase("TestContractCreate", contract.TestContractCreate)
35 | testframework.TFramework.RegTestCase("TestContractDestroy", contract.TestContractDestroy)
36 | testframework.TFramework.RegTestCase("TestCallingScriptHash", executionengine.TestCallingScriptHash)
37 | testframework.TFramework.RegTestCase("TestCheckWitness", runtime.TestCheckWitness)
38 | //testframework.TFramework.RegTestCase("TestRuntimLog", runtime.TestRuntimLog)
39 | testframework.TFramework.RegTestCase("TestRuntimeNotify", runtime.TestRuntimeNotify)
40 | testframework.TFramework.RegTestCase("TestGetTxHash", transaction.TestGetTxHash)
41 | testframework.TFramework.RegTestCase("TestGetTxType", transaction.TestGetTxType)
42 | testframework.TFramework.RegTestCase("TestStorage", storage.TestStorage)
43 | }
44 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/sub.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestOperationSub(ctx *testframework.TestFrameworkContext) bool {
12 | code := "52C56B6C766B00527AC46C766B51527AC46C766B00C36C766B51C394616C7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestOperationSub GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 |
22 | false,
23 | code,
24 | "TestOperationSub",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 | if err != nil {
31 | ctx.LogError("TestOperationSub DeploySmartContract error:%s", err)
32 | return false
33 | }
34 | //等待出块
35 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
36 | if err != nil {
37 | ctx.LogError("TestOperationSub WaitForGenerateBlock error:%s", err)
38 | return false
39 | }
40 |
41 | if !testOperationSub(ctx, codeAddress, 1, 2) {
42 | return false
43 | }
44 |
45 | if !testOperationSub(ctx, codeAddress, -1, 1) {
46 | return false
47 | }
48 |
49 | if !testOperationSub(ctx, codeAddress, -1, -2) {
50 | return false
51 | }
52 |
53 | if !testOperationSub(ctx, codeAddress, 0, 0) {
54 | return false
55 | }
56 |
57 | return true
58 | }
59 |
60 | func testOperationSub(ctx *testframework.TestFrameworkContext, code common.Address, a, b int) bool {
61 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
62 | code,
63 | []interface{}{a, b},
64 | )
65 | if err != nil {
66 | ctx.LogError("TestOperationSub InvokeSmartContract error:%s", err)
67 | return false
68 | }
69 | resValue, err := res.Result.ToInteger()
70 | if err != nil {
71 | ctx.LogError("TestOperationSub Result.ToInteger error:%s", err)
72 | return false
73 | }
74 | err = ctx.AssertToInt(resValue, a-b)
75 | if err != nil {
76 | ctx.LogError("TestOperationSub test failed %s", err)
77 | return false
78 | }
79 | return true
80 | }
81 |
82 | /*
83 | using Neo.SmartContract.Framework;
84 | using Neo.SmartContract.Framework.Services.Neo;
85 |
86 | class A : SmartContract
87 | {
88 | public static int Main(int a, int b)
89 | {
90 | return a - b;
91 | }
92 | }
93 | */
94 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/mode.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestOperationMode(ctx *testframework.TestFrameworkContext) bool {
12 | code := "52C56B6C766B00527AC46C766B51527AC46C766B00C36C766B51C397616C7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestOperationMode GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 |
22 | false,
23 | code,
24 | "TestOperationMode",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 | if err != nil {
31 | ctx.LogError("TestOperationMode DeploySmartContract error:%s", err)
32 | return false
33 | }
34 | //等待出块
35 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
36 | if err != nil {
37 | ctx.LogError("TestOperationMode WaitForGenerateBlock error:%s", err)
38 | return false
39 | }
40 |
41 | if !testOperationMode(ctx, codeAddress, 23, 2) {
42 | return false
43 | }
44 |
45 | if !testOperationMode(ctx, codeAddress, -345, 34) {
46 | return false
47 | }
48 |
49 | if !testOperationMode(ctx, codeAddress, -10, -234) {
50 | return false
51 | }
52 |
53 | if !testOperationMode(ctx, codeAddress, 0, 100) {
54 | return false
55 | }
56 |
57 | return true
58 | }
59 |
60 | func testOperationMode(ctx *testframework.TestFrameworkContext, code common.Address, a, b int) bool {
61 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
62 | code,
63 | []interface{}{a, b},
64 | )
65 | if err != nil {
66 | ctx.LogError("TestOperationMode InvokeSmartContract error:%s", err)
67 | return false
68 | }
69 | resValue,err := res.Result.ToInteger()
70 | if err != nil {
71 | ctx.LogError("TestOperationMode Result.ToInteger error:%s", err)
72 | return false
73 | }
74 | err = ctx.AssertToInt(resValue, a%b)
75 | if err != nil {
76 | ctx.LogError("TestOperationMode test failed %s", err)
77 | return false
78 | }
79 | return true
80 | }
81 |
82 | /*
83 | using Neo.SmartContract.Framework;
84 | using Neo.SmartContract.Framework.Services.Neo;
85 |
86 | class A : SmartContract
87 | {
88 | public static int Main(int a, int b)
89 | {
90 | return a * b;
91 | }
92 | }
93 | */
94 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/eq.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestOperationEqual(ctx *testframework.TestFrameworkContext) bool {
12 | code := "52C56B6C766B00527AC46C766B51527AC46C766B00C36C766B51C39C616C7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestOperationEqual GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 |
22 | false,
23 | code,
24 | "TestOperationEqual",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 | if err != nil {
31 | ctx.LogError("TestOperationEqual DeploySmartContract error:%s", err)
32 | return false
33 | }
34 | //等待出块
35 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
36 | if err != nil {
37 | ctx.LogError("TestOperationEqual WaitForGenerateBlock error:%s", err)
38 | return false
39 | }
40 |
41 | if !testOperationEqual(ctx, codeAddress, -1, 1) {
42 | return false
43 | }
44 |
45 | if !testOperationEqual(ctx, codeAddress, -1, -1) {
46 | return false
47 | }
48 |
49 | if !testOperationEqual(ctx, codeAddress, 1, 1) {
50 | return false
51 | }
52 |
53 | if !testOperationEqual(ctx, codeAddress, 0, 0) {
54 | return false
55 | }
56 |
57 | return true
58 | }
59 |
60 | func testOperationEqual(ctx *testframework.TestFrameworkContext, code common.Address, a, b int) bool {
61 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
62 | code,
63 | []interface{}{a, b},
64 | )
65 | if err != nil {
66 | ctx.LogError("TestOperationEqual InvokeSmartContract error:%s", err)
67 | return false
68 | }
69 | resValue, err := res.Result.ToBool()
70 | if err != nil {
71 | ctx.LogError("TestOperationEqual Result.ToBool error:%s", err)
72 | return false
73 | }
74 | err = ctx.AssertToBoolean(resValue, a == b)
75 | if err != nil {
76 | ctx.LogError("TestOperationEqual test failed %s", err)
77 | return false
78 | }
79 | return true
80 | }
81 |
82 | /*
83 | using Neo.SmartContract.Framework;
84 | using Neo.SmartContract.Framework.Services.Neo;
85 |
86 | class A : SmartContract
87 | {
88 | public static bool Main(int a, int b)
89 | {
90 | return a == b;
91 | }
92 | }
93 | */
94 |
--------------------------------------------------------------------------------
/testcase/smartcontract/neovm/utils/range.go:
--------------------------------------------------------------------------------
1 | package utils
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestRange(ctx *testframework.TestFrameworkContext) bool {
12 | code := "53C56B6C766B00527AC46C766B51527AC46C766B52527AC46C766B00C36C766B51C36C766B52C37F616C7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestRange GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 | false,
22 | code,
23 | "TestRange",
24 | "1.0",
25 | "",
26 | "",
27 | "",
28 | )
29 | if err != nil {
30 | ctx.LogError("TestRange DeploySmartContract error:%s", err)
31 | return false
32 | }
33 | //等待出块
34 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
35 | if err != nil {
36 | ctx.LogError("TestRange WaitForGenerateBlock error:%s", err)
37 | return false
38 | }
39 |
40 | input := []byte("Hello World!")
41 | if !testRange(ctx, codeAddress, input, 0, len(input)) {
42 | return false
43 | }
44 |
45 | if !testRange(ctx, codeAddress, input, 1, len(input)-2) {
46 | return false
47 | }
48 |
49 | if !testRange(ctx, codeAddress, input, 2, len(input)-3) {
50 | return false
51 | }
52 | return true
53 | }
54 |
55 | func testRange(ctx *testframework.TestFrameworkContext, code common.Address, b []byte, start, count int) bool {
56 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
57 | code,
58 | []interface{}{b, start, count},
59 | )
60 | if err != nil {
61 | ctx.LogError("TestRange InvokeSmartContract error:%s", err)
62 | return false
63 | }
64 | resValue, err := res.Result.ToByteArray()
65 | if err !=nil {
66 | ctx.LogError("TestRange Result.ToByteArray error:%s", err)
67 | return false
68 | }
69 | err = ctx.AssertToByteArray(resValue, b[start:start+count])
70 | if err != nil {
71 | ctx.LogError("TestRange test failed %s", err)
72 | return false
73 | }
74 | return true
75 | }
76 |
77 | /*
78 | using Neo.SmartContract.Framework;
79 | using Neo.SmartContract.Framework.Services.Neo;
80 |
81 | class A : SmartContract
82 | {
83 | public static byte[] Main(byte[] arg, int start, int count)
84 | {
85 | return arg.Range(start, count);
86 | }
87 | }
88 | */
89 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/multi.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestOperationMulti(ctx *testframework.TestFrameworkContext) bool {
12 | code := "52C56B6C766B00527AC46C766B51527AC46C766B00C36C766B51C395616C7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestOperationMulti GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 |
22 | false,
23 | code,
24 | "TestOperationMulti",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 | if err != nil {
31 | ctx.LogError("TestOperationMulti DeploySmartContract error:%s", err)
32 | return false
33 | }
34 | //等待出块
35 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
36 | if err != nil {
37 | ctx.LogError("TestOperationMulti WaitForGenerateBlock error:%s", err)
38 | return false
39 | }
40 |
41 | if !testOperationMulti(ctx, codeAddress, 23, 2) {
42 | return false
43 | }
44 |
45 | if !testOperationMulti(ctx, codeAddress, -1, 34) {
46 | return false
47 | }
48 |
49 | if !testOperationMulti(ctx, codeAddress, -1, -2) {
50 | return false
51 | }
52 |
53 | if !testOperationMulti(ctx, codeAddress, 0, 100) {
54 | return false
55 | }
56 |
57 | return true
58 | }
59 |
60 | func testOperationMulti(ctx *testframework.TestFrameworkContext, code common.Address, a, b int) bool {
61 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
62 | code,
63 | []interface{}{a, b},
64 | )
65 | if err != nil {
66 | ctx.LogError("TestOperationMulti InvokeSmartContract error:%s", err)
67 | return false
68 | }
69 | resValue,err := res.Result.ToInteger()
70 | if err != nil {
71 | ctx.LogError("TestOperationMulti Result.ToInteger error:%s", err)
72 | return false
73 | }
74 | err = ctx.AssertToInt(resValue, a*b)
75 | if err != nil {
76 | ctx.LogError("TestOperationMulti test failed %s", err)
77 | return false
78 | }
79 | return true
80 | }
81 |
82 | /*
83 | using Neo.SmartContract.Framework;
84 | using Neo.SmartContract.Framework.Services.Neo;
85 |
86 | class A : SmartContract
87 | {
88 | public static int Main(int a, int b)
89 | {
90 | return a * b;
91 | }
92 | }
93 | */
94 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/or.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestOperationOr(ctx *testframework.TestFrameworkContext) bool {
12 | code := "52C56B6C766B00527AC46C766B51527AC46C766B00C3630C006C766B51C3616C756651616C7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestOperationOr GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 |
22 | false,
23 | code,
24 | "TestOperationOr",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 | if err != nil {
31 | ctx.LogError("TestOperationOr DeploySmartContract error:%s", err)
32 | return false
33 | }
34 | //等待出块
35 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
36 | if err != nil {
37 | ctx.LogError("TestOperationOr WaitForGenerateBlock error:%s", err)
38 | return false
39 | }
40 |
41 | if !testOperationOr(ctx, codeAddress, true, true) {
42 | return false
43 | }
44 |
45 | if !testOperationOr(ctx, codeAddress, true, false) {
46 | return false
47 | }
48 |
49 | if !testOperationOr(ctx, codeAddress, false, true) {
50 | return false
51 | }
52 |
53 | if !testOperationOr(ctx, codeAddress, false, false) {
54 | return false
55 | }
56 |
57 | return true
58 | }
59 |
60 | func testOperationOr(ctx *testframework.TestFrameworkContext, code common.Address, a, b bool) bool {
61 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
62 | code,
63 | []interface{}{a, b},
64 | )
65 | if err != nil {
66 | ctx.LogError("TestOperationOr InvokeSmartContract error:%s", err)
67 | return false
68 | }
69 | resValue, err := res.Result.ToBool()
70 | if err != nil {
71 | ctx.LogError("TestOperationOr Result.ToBool error:%s", err)
72 | return false
73 | }
74 | err = ctx.AssertToBoolean(resValue, a || b)
75 | if err != nil {
76 | ctx.LogError("TestOperationOr test failed %s", err)
77 | return false
78 | }
79 | return true
80 | }
81 |
82 | /*
83 | using Neo.SmartContract.Framework;
84 | using Neo.SmartContract.Framework.Services.Neo;
85 |
86 | class A : SmartContract
87 | {
88 | public static bool Main(bool a, bool b)
89 | {
90 | return a || b;
91 | }
92 | }
93 | */
94 |
--------------------------------------------------------------------------------
/testcase/smartcontract/neovm/utils/take.go:
--------------------------------------------------------------------------------
1 | package utils
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestTake(ctx *testframework.TestFrameworkContext) bool {
12 | code := "53c56b6c766b00527ac46c766b51527ac4616c766b00c36c766b51c3806c766b52527ac46203006c766b52c3616c7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestAsString GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 | false,
22 | code,
23 | "TestAsString",
24 | "1.0",
25 | "",
26 | "",
27 | "",
28 | )
29 | if err != nil {
30 | ctx.LogError("TestTake DeploySmartContract error:%s", err)
31 | return false
32 | }
33 | //等待出块
34 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
35 | if err != nil {
36 | ctx.LogError("TestTake WaitForGenerateBlock error:%s", err)
37 | return false
38 | }
39 |
40 | input := []byte("Hello World!")
41 | if !testTake(ctx, codeAddress, input, 0) {
42 | return false
43 | }
44 |
45 | if !testTake(ctx, codeAddress, input, len(input)-1) {
46 | return false
47 | }
48 |
49 | if !testTake(ctx, codeAddress, input, len(input)) {
50 | return false
51 | }
52 | return true
53 | }
54 |
55 | func testTake(ctx *testframework.TestFrameworkContext, code common.Address, b []byte, count int) bool {
56 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
57 | code,
58 | []interface{}{b, count},
59 | )
60 | if err != nil {
61 | ctx.LogError("TestTake InvokeSmartContract error:%s", err)
62 | return false
63 | }
64 | r := count
65 | if count > len(b) {
66 | r = len(b)
67 | }
68 | resValue, err := res.Result.ToByteArray()
69 | if err != nil {
70 | ctx.LogError("TestTake Result.ToByteArray error:%s", err)
71 | return false
72 | }
73 | err = ctx.AssertToByteArray(resValue, b[0:r])
74 | if err != nil {
75 | ctx.LogError("TestTake test failed %s", err)
76 | return false
77 | }
78 | return true
79 | }
80 |
81 | /*
82 | using Neo.SmartContract.Framework;
83 | using Neo.SmartContract.Framework.Services.Neo;
84 |
85 | class A : SmartContract
86 | {
87 | public static byte[] Main(byte[] arg, int count)
88 | {
89 | return arg.Take(count);
90 | }
91 | }
92 | */
93 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/and.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestOperationAnd(ctx *testframework.TestFrameworkContext) bool {
12 | code := "52C56B6C766B00527AC46C766B51527AC46C766B00C3640C006C766B51C3616C756600616C7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestOperationAnd GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 |
22 | false,
23 | code,
24 | "TestOperationAnd",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 | if err != nil {
31 | ctx.LogError("TestOperationAnd DeploySmartContract error:%s", err)
32 | return false
33 | }
34 | //等待出块
35 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
36 | if err != nil {
37 | ctx.LogError("TestOperationAnd WaitForGenerateBlock error:%s", err)
38 | return false
39 | }
40 |
41 | if !testOperationAnd(ctx, codeAddress, true, true) {
42 | return false
43 | }
44 |
45 | if !testOperationAnd(ctx, codeAddress, true, false) {
46 | return false
47 | }
48 |
49 | if !testOperationAnd(ctx, codeAddress, false, true) {
50 | return false
51 | }
52 |
53 | if !testOperationAnd(ctx, codeAddress, false, false) {
54 | return false
55 | }
56 |
57 | return true
58 | }
59 |
60 | func testOperationAnd(ctx *testframework.TestFrameworkContext, codeAddress common.Address, a, b bool) bool {
61 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
62 | codeAddress,
63 | []interface{}{a, b},
64 | )
65 | if err != nil {
66 | ctx.LogError("TestOperationAnd InvokeSmartContract error:%s", err)
67 | return false
68 | }
69 | resValue , err := res.Result.ToBool()
70 | if err != nil {
71 | ctx.LogError("TestOperationAnd Result.ToBool error:%s", err)
72 | return false
73 | }
74 | err = ctx.AssertToBoolean(resValue, a && b)
75 | if err != nil {
76 | ctx.LogError("TestOperationAnd test failed %s", err)
77 | return false
78 | }
79 | return true
80 | }
81 |
82 | /*
83 | using Neo.SmartContract.Framework;
84 | using Neo.SmartContract.Framework.Services.Neo;
85 |
86 | class A : SmartContract
87 | {
88 | public static bool Main(bool a, bool b)
89 | {
90 | return a && b;
91 | }
92 | }
93 | */
94 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/ne.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestOperationNotEqual(ctx *testframework.TestFrameworkContext) bool {
12 | code := "52C56B6C766B00527AC46C766B51527AC46C766B00C36C766B51C39C009C616C7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestOperationNotEqual GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 |
22 | false,
23 | code,
24 | "TestOperationNotEqual",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 | if err != nil {
31 | ctx.LogError("TestOperationNotEqual DeploySmartContract error:%s", err)
32 | return false
33 | }
34 | //等待出块
35 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
36 | if err != nil {
37 | ctx.LogError("TestOperationNotEqual WaitForGenerateBlock error:%s", err)
38 | return false
39 | }
40 |
41 | if !testOperationNotEqual(ctx, codeAddress, -1, 1) {
42 | return false
43 | }
44 |
45 | if !testOperationNotEqual(ctx, codeAddress, -1, -1) {
46 | return false
47 | }
48 |
49 | if !testOperationNotEqual(ctx, codeAddress, 1, 1) {
50 | return false
51 | }
52 |
53 | if !testOperationNotEqual(ctx, codeAddress, 0, 0) {
54 | return false
55 | }
56 |
57 | return true
58 | }
59 |
60 | func testOperationNotEqual(ctx *testframework.TestFrameworkContext, code common.Address, a, b int) bool {
61 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
62 | code,
63 | []interface{}{a, b},
64 | )
65 | if err != nil {
66 | ctx.LogError("TestOperationNotEqual InvokeSmartContract error:%s", err)
67 | return false
68 | }
69 | resValue, err := res.Result.ToBool()
70 | if err != nil {
71 | ctx.LogError("TestOperationNotEqual Result.ToBool error:%s", err)
72 | return false
73 | }
74 | err = ctx.AssertToBoolean(resValue, a != b)
75 | if err != nil {
76 | ctx.LogError("TestOperationNotEqual test failed %s", err)
77 | return false
78 | }
79 | return true
80 | }
81 |
82 | /*
83 | using Neo.SmartContract.Framework;
84 | using Neo.SmartContract.Framework.Services.Neo;
85 |
86 | class A : SmartContract
87 | {
88 | public static bool Main(int a, int b)
89 | {
90 | return a != b;
91 | }
92 | }
93 | */
94 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/sl.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestOperationSmaller(ctx *testframework.TestFrameworkContext) bool {
12 | code := "52C56B6C766B00527AC46C766B51527AC46C766B00C36C766B51C39F616C7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestOperationSmaller GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 |
22 | false,
23 | code,
24 | "TestOperationSmaller",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 | if err != nil {
31 | ctx.LogError("TestOperationSmaller DeploySmartContract error:%s", err)
32 | return false
33 | }
34 | //等待出块
35 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
36 | if err != nil {
37 | ctx.LogError("TestOperationSmaller WaitForGenerateBlock error:%s", err)
38 | return false
39 | }
40 |
41 | if !testOperationSmaller(ctx, codeAddress, 23, 2) {
42 | return false
43 | }
44 |
45 | if !testOperationSmaller(ctx, codeAddress, -345, 34) {
46 | return false
47 | }
48 |
49 | if !testOperationSmaller(ctx, codeAddress, -10, -234) {
50 | return false
51 | }
52 |
53 | if !testOperationSmaller(ctx, codeAddress, 100, 100) {
54 | return false
55 | }
56 |
57 | return true
58 | }
59 |
60 | func testOperationSmaller(ctx *testframework.TestFrameworkContext, code common.Address, a, b int) bool {
61 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
62 | code,
63 | []interface{}{a, b},
64 | )
65 | if err != nil {
66 | ctx.LogError("TestOperationSmaller InvokeSmartContract error:%s", err)
67 | return false
68 | }
69 | resValue, err := res.Result.ToBool()
70 | if err != nil {
71 | ctx.LogError("TestOperationLarger Result.ToBool error:%s", err)
72 | return false
73 | }
74 | err = ctx.AssertToBoolean(resValue, a < b)
75 | if err != nil {
76 | ctx.LogError("TestOperationLarger test %d < %d failed %s", a, b, err)
77 | return false
78 | }
79 | return true
80 | }
81 |
82 | /*
83 | using Neo.SmartContract.Framework;
84 | using Neo.SmartContract.Framework.Services.Neo;
85 |
86 | class A : SmartContract
87 | {
88 | public static bool Main(int a, int b)
89 | {
90 | return a < b;
91 | }
92 | }
93 | */
94 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/datatype/array.go:
--------------------------------------------------------------------------------
1 | package datatype
2 |
3 | import (
4 | "time"
5 | "github.com/ontio/ontology-go-sdk/utils"
6 | "github.com/ontio/ontology-test/testframework"
7 | "github.com/ontio/ontology/common"
8 | )
9 |
10 | func TestArray(ctx *testframework.TestFrameworkContext) bool {
11 | code := "52c56b6c766b00527ac4616c766b00c3c06c766b51527ac46203006c766b51c3616c7566"
12 | codeAddress, _ := utils.GetContractAddress(code)
13 | signer, err := ctx.GetDefaultAccount()
14 | if err != nil {
15 | ctx.LogError("TestArray GetDefaultAccount error:%s", err)
16 | return false
17 | }
18 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
19 | signer,
20 | false,
21 | code,
22 | "TestArray",
23 | "1.0",
24 | "",
25 | "",
26 | "",
27 | )
28 | if err != nil {
29 | ctx.LogError("TestArray DeploySmartContract error:%s", err)
30 | return false
31 | }
32 | //等待出块
33 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
34 | if err != nil {
35 | ctx.LogError("TestArray WaitForGenerateBlock error:%s", err)
36 | return false
37 | }
38 | params := []interface{}{[]byte("Hello"), []byte("world")}
39 | if !testArray(ctx, codeAddress, params) {
40 | return false
41 | }
42 | params = []interface{}{[]byte("Hello"), []byte("world"), "123456", 8}
43 | if !testArray(ctx, codeAddress, params) {
44 | return false
45 | }
46 | return true
47 | }
48 |
49 | func testArray(ctx *testframework.TestFrameworkContext, code common.Address, params []interface{}) bool {
50 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
51 | code,
52 | []interface{}{params},
53 | )
54 | if err != nil {
55 | ctx.LogError("TestArray InvokeSmartContract error:%s", err)
56 | return false
57 | }
58 | resValue, err := res.Result.ToInteger()
59 | if err != nil {
60 | ctx.LogError("TestArray Result.ToInteger error:%s", err)
61 | return false
62 | }
63 | err = ctx.AssertToInt(resValue, len(params))
64 | if err != nil {
65 | ctx.LogError("TestArray test failed %s", err)
66 | return false
67 | }
68 | return true
69 | }
70 |
71 | /*
72 | using Neo.SmartContract.Framework;
73 | using Neo.SmartContract.Framework.Services.Neo;
74 | using Neo.SmartContract.Framework.Services.System;
75 | using System;
76 | using System.Numerics;
77 |
78 | namespace Hello
79 | {
80 | public class Hello : SmartContract
81 | {
82 | public static int Main(params object[] args)
83 | {
84 | return args.Length;
85 | }
86 | }
87 | }
88 | */
89 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/lr.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestOperationLarger(ctx *testframework.TestFrameworkContext) bool {
12 | code := "52C56B6C766B00527AC46C766B51527AC46C766B00C36C766B51C3A0616C7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestOperationLarger GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 |
22 | false,
23 | code,
24 | "TestOperationLarger",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 |
31 | if err != nil {
32 | ctx.LogError("TestOperationLarger DeploySmartContract error:%s", err)
33 | return false
34 | }
35 | //等待出块
36 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
37 | if err != nil {
38 | ctx.LogError("TestOperationLarger WaitForGenerateBlock error:%s", err)
39 | return false
40 | }
41 |
42 | if !testOperationLarger(ctx, codeAddress, 23, 2) {
43 | return false
44 | }
45 |
46 | if !testOperationLarger(ctx, codeAddress, -345, 34) {
47 | return false
48 | }
49 |
50 | if !testOperationLarger(ctx, codeAddress, -10, -234) {
51 | return false
52 | }
53 |
54 | if !testOperationLarger(ctx, codeAddress, 100, 100) {
55 | return false
56 | }
57 |
58 | return true
59 | }
60 |
61 | func testOperationLarger(ctx *testframework.TestFrameworkContext, codeAddress common.Address, a, b int) bool {
62 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
63 | codeAddress,
64 | []interface{}{a, b},
65 | )
66 | if err != nil {
67 | ctx.LogError("TestOperationLarger InvokeSmartContract error:%s", err)
68 | return false
69 | }
70 | resValue, err := res.Result.ToBool()
71 | if err != nil {
72 | ctx.LogError("TestOperationLarger Result.ToBool error:%s", err)
73 | return false
74 | }
75 | err = ctx.AssertToBoolean(resValue, a > b)
76 | if err != nil {
77 | ctx.LogError("TestOperationLarger test %d > %d failed %s", a, b, err)
78 | return false
79 | }
80 | return true
81 | }
82 |
83 | /*
84 | using Neo.SmartContract.Framework;
85 | using Neo.SmartContract.Framework.Services.Neo;
86 |
87 | class A : SmartContract
88 | {
89 | public static bool Main(int a, int b)
90 | {
91 | return a > b;
92 | }
93 | }
94 | */
95 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/datatype/bytearray.go:
--------------------------------------------------------------------------------
1 | package datatype
2 |
3 | import (
4 | "time"
5 | "github.com/ontio/ontology-go-sdk/utils"
6 | "github.com/ontio/ontology-test/testframework"
7 | "github.com/ontio/ontology/common"
8 | )
9 |
10 | func TestByteArray(ctx *testframework.TestFrameworkContext) bool {
11 | code := "53c56b6c766b00527ac46c766b51527ac4616c766b00c36c766b51c39c6c766b52527ac46203006c766b52c3616c7566"
12 | codeAddress, _ := utils.GetContractAddress(code)
13 | signer, err := ctx.GetDefaultAccount()
14 | if err != nil {
15 | ctx.LogError("TestByteArray GetDefaultAccount error:%s", err)
16 | return false
17 | }
18 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
19 | signer,
20 | false,
21 | code,
22 | "TestByteArray",
23 | "1.0",
24 | "",
25 | "",
26 | "",
27 | )
28 | if err != nil {
29 | ctx.LogError("TestArray DeploySmartContract error:%s", err)
30 | return false
31 | }
32 | //等待出块
33 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
34 | if err != nil {
35 | ctx.LogError("TestArray WaitForGenerateBlock error:%s", err)
36 | return false
37 | }
38 |
39 | arg1 := []byte("Hello")
40 | arg2 := []byte("World")
41 |
42 | if !testByteArray(ctx, codeAddress, arg1, arg1, true) {
43 | return false
44 | }
45 | if !testByteArray(ctx, codeAddress, arg1, arg2, false) {
46 | return false
47 | }
48 | return true
49 | }
50 |
51 | func testByteArray(ctx *testframework.TestFrameworkContext, code common.Address, arg1, arg2 []byte, expect bool) bool {
52 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
53 | code,
54 | []interface{}{arg1, arg2},
55 | )
56 | if err != nil {
57 | ctx.LogError("testByteArray InvokeSmartContract error:%s", err)
58 | return false
59 | }
60 | resValue, err := res.Result.ToBool()
61 | if err != nil {
62 | ctx.LogError("testByteArray Result.ToBool error:%s", err)
63 | return false
64 | }
65 | err = ctx.AssertToBoolean(resValue, expect)
66 | if err != nil {
67 | ctx.LogError("testByteArray test failed %s", err)
68 | return false
69 | }
70 | return true
71 | }
72 |
73 | /*
74 | using Neo.SmartContract.Framework;
75 | using Neo.SmartContract.Framework.Services.Neo;
76 | using Neo.SmartContract.Framework.Services.System;
77 | using System;
78 | using System.Numerics;
79 |
80 | namespace Hello
81 | {
82 | public class A : SmartContract
83 | {
84 | public static bool Main(byte[] arg1, byte[] arg2)
85 | {
86 | return arg1 == arg2;
87 | }
88 | }
89 | }
90 | */
91 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/se.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestOperationSmallerEqual(ctx *testframework.TestFrameworkContext) bool {
12 | code := "52C56B6C766B00527AC46C766B51527AC46C766B00C36C766B51C3A0009C616C7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestOperationSmallerEqual GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 |
22 | false,
23 | code,
24 | "TestOperationSmallerEqual",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 | if err != nil {
31 | ctx.LogError("TestOperationSmallerEqual DeploySmartContract error:%s", err)
32 | return false
33 | }
34 | //等待出块
35 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
36 | if err != nil {
37 | ctx.LogError("TestOperationSmallerEqual WaitForGenerateBlock error:%s", err)
38 | return false
39 | }
40 |
41 | if !testOperationSmallerEqual(ctx, codeAddress, 23, 2) {
42 | return false
43 | }
44 |
45 | if !testOperationSmallerEqual(ctx, codeAddress, -345, 34) {
46 | return false
47 | }
48 |
49 | if !testOperationSmallerEqual(ctx, codeAddress, -10, -234) {
50 | return false
51 | }
52 |
53 | if !testOperationSmallerEqual(ctx, codeAddress, 100, 100) {
54 | return false
55 | }
56 |
57 | return true
58 | }
59 |
60 | func testOperationSmallerEqual(ctx *testframework.TestFrameworkContext, code common.Address, a, b int) bool {
61 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
62 | code,
63 | []interface{}{a, b},
64 | )
65 | if err != nil {
66 | ctx.LogError("TestOperationSmallerEqual InvokeSmartContract error:%s", err)
67 | return false
68 | }
69 | resValue, err := res.Result.ToBool()
70 | if err != nil {
71 | ctx.LogError("TestOperationLarger Result.ToBool error:%s", err)
72 | return false
73 | }
74 | err = ctx.AssertToBoolean(resValue, a <= b)
75 | if err != nil {
76 | ctx.LogError("TestOperationLarger test %d <= %d failed %s", a, b, err)
77 | return false
78 | }
79 | return true
80 | }
81 |
82 | /*
83 | using Neo.SmartContract.Framework;
84 | using Neo.SmartContract.Framework.Services.Neo;
85 |
86 | class A : SmartContract
87 | {
88 | public static bool Main(int a, int b)
89 | {
90 | return a <= b;
91 | }
92 | }
93 | */
94 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/le.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestOperationLargerEqual(ctx *testframework.TestFrameworkContext) bool {
12 | code := "53c56b6c766b00527ac46c766b51527ac4616c766b00c36c766b51c39f009c6c766b52527ac46203006c766b52c3616c7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestOperationLargerEqual GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 |
22 | false,
23 | code,
24 | "TestOperationLargerEqual",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 | if err != nil {
31 | ctx.LogError("TestOperationLargerEqual DeploySmartContract error:%s", err)
32 | return false
33 | }
34 | //等待出块
35 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
36 | if err != nil {
37 | ctx.LogError("TestOperationLargerEqual WaitForGenerateBlock error:%s", err)
38 | return false
39 | }
40 |
41 | if !testOperationLargerEqual(ctx, codeAddress, 23, 2) {
42 | return false
43 | }
44 |
45 | if !testOperationLargerEqual(ctx, codeAddress, -345, 34) {
46 | return false
47 | }
48 |
49 | if !testOperationLargerEqual(ctx, codeAddress, -10, -234) {
50 | return false
51 | }
52 |
53 | if !testOperationLargerEqual(ctx, codeAddress, 100, 100) {
54 | return false
55 | }
56 |
57 | return true
58 | }
59 |
60 | func testOperationLargerEqual(ctx *testframework.TestFrameworkContext, code common.Address, a, b int) bool {
61 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
62 | code,
63 | []interface{}{a, b},
64 | )
65 | if err != nil {
66 | ctx.LogError("TestOperationLargerEqual InvokeSmartContract error:%s", err)
67 | return false
68 | }
69 | resValue, err := res.Result.ToBool()
70 | if err != nil {
71 | ctx.LogError("TestOperationLargerEqual Result.ToBool error:%s", err)
72 | return false
73 | }
74 | err = ctx.AssertToBoolean(resValue, a >= b)
75 | if err != nil {
76 | ctx.LogError("TestOperationLarger test %d >= %d failed %s", a, b, err)
77 | return false
78 | }
79 | return true
80 | }
81 |
82 | /*
83 | using Neo.SmartContract.Framework;
84 | using Neo.SmartContract.Framework.Services.Neo;
85 |
86 | class A : SmartContract
87 | {
88 | public static bool Main(int a, int b)
89 | {
90 | return a >= b;
91 | }
92 | }
93 | */
94 |
--------------------------------------------------------------------------------
/main.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | */
18 | package main
19 |
20 | import (
21 | "flag"
22 | log4 "github.com/alecthomas/log4go"
23 | sdk "github.com/ontio/ontology-go-sdk"
24 | "github.com/ontio/ontology-test/common"
25 | _ "github.com/ontio/ontology-test/testcase"
26 | "github.com/ontio/ontology-test/testframework"
27 | "github.com/ontio/ontology/common/log"
28 | "math/rand"
29 | "strings"
30 | "time"
31 | )
32 |
33 | var (
34 | TestConfig string //Test config file
35 | LogConfig string //Log config file
36 | TestCases string //TestCase list in cmdline
37 | )
38 |
39 | func init() {
40 | flag.StringVar(&TestConfig, "cfg", "./config_test.json", "Config of ontology-test")
41 | flag.StringVar(&LogConfig, "lfg", "./log4go.xml", "Log config of ontology-test")
42 | flag.StringVar(&TestCases, "t", "", "Test case to run. use ',' to split test case")
43 | flag.Parse()
44 | }
45 |
46 | func main() {
47 | rand.Seed(time.Now().UnixNano())
48 | log4.LoadConfiguration(LogConfig)
49 | log.InitLog(1) //init log module in ontology
50 | defer time.Sleep(time.Second)
51 |
52 | err := common.DefConfig.Init(TestConfig)
53 | if err != nil {
54 | log4.Error("DefConfig.Init error:%s", err)
55 | return
56 | }
57 |
58 | ontSdk := sdk.NewOntologySdk()
59 | ontSdk.NewRpcClient().SetAddress(common.DefConfig.JsonRpcAddress)
60 | wallet, err := ontSdk.OpenWallet(common.DefConfig.WalletFile)
61 | if err != nil {
62 | log4.Error("OpenOrCreateWallet %s error:%s", common.DefConfig.WalletFile, err)
63 | return
64 | }
65 | testCases := make([]string, 0)
66 | if TestCases != "" {
67 | testCases = strings.Split(TestCases, ",")
68 | }
69 | testframework.TFramework.SetOntSdk(ontSdk)
70 | testframework.TFramework.SetWallet(wallet)
71 | //Start run test case
72 | testframework.TFramework.Start(testCases)
73 | }
74 |
--------------------------------------------------------------------------------
/testcase/smartcontract/neovm/utils/asbiginteger.go:
--------------------------------------------------------------------------------
1 | package utils
2 |
3 | import (
4 | "math/big"
5 | "time"
6 |
7 | "github.com/ontio/ontology-go-sdk/utils"
8 | "github.com/ontio/ontology-test/testframework"
9 | "github.com/ontio/ontology/common"
10 | )
11 |
12 | func TestAsBigInteger(ctx *testframework.TestFrameworkContext) bool {
13 | code := "52c56b6c766b00527ac4616c766b00c36c766b51527ac46203006c766b51c3616c7566"
14 | codeAddress, _ := utils.GetContractAddress(code)
15 | signer, err := ctx.GetDefaultAccount()
16 | if err != nil {
17 | ctx.LogError("TestAsBigInteger GetDefaultAccount error:%s", err)
18 | return false
19 | }
20 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
21 | signer,
22 | false,
23 | code,
24 | "TestAsBigInteger",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 | if err != nil {
31 | ctx.LogError("TestAsBigInteger DeploySmartContract error:%s", err)
32 | return false
33 | }
34 | //等待出块
35 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
36 | if err != nil {
37 | ctx.LogError("TestAsBigInteger WaitForGenerateBlock error:%s", err)
38 | return false
39 | }
40 |
41 | b := big.NewInt(1233)
42 | if !testAsBigInteger(ctx, codeAddress, b) {
43 | return false
44 | }
45 | b = big.NewInt(0)
46 | if !testAsBigInteger(ctx, codeAddress, b) {
47 | return false
48 | }
49 | b = big.NewInt(-1233)
50 | if !testAsBigInteger(ctx, codeAddress, b) {
51 | return false
52 | }
53 | return true
54 | }
55 |
56 | func testAsBigInteger(ctx *testframework.TestFrameworkContext, code common.Address, b *big.Int) bool {
57 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
58 | code,
59 | []interface{}{b},
60 | )
61 | if err != nil {
62 | ctx.LogError("TestAsBigInteger InvokeSmartContract error:%s", err)
63 | return false
64 | }
65 | if res.State == 0 {
66 | ctx.LogError("TestAsBigInteger PreExecInvokeNeoVMContract failed. state == 0")
67 | return false
68 | }
69 | resValue, err := res.Result.ToInteger()
70 | if err != nil {
71 | ctx.LogError("TestAsBigInteger Result.ToInteger error:%s", err)
72 | return false
73 | }
74 | err = ctx.AssertBigInteger(resValue, b)
75 | if err != nil {
76 | ctx.LogError("TestAsBigInteger test failed %s", err)
77 | return false
78 | }
79 | return true
80 | }
81 |
82 | /*
83 | using System.Numerics;
84 | using Neo.SmartContract.Framework;
85 | using Neo.SmartContract.Framework.Services.Neo;
86 |
87 | class A : SmartContract
88 | {
89 | public static BigInteger Main(byte[] v)
90 | {
91 | return v.AsBigInteger();
92 | }
93 | }
94 | */
95 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/leftshift.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestOperationLeftShift(ctx *testframework.TestFrameworkContext) bool {
12 | code := "53c56b6c766b00527ac46c766b51527ac4616c766b00c36c766b51c3011f84986c766b52527ac46203006c766b52c3616c7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestOperationLeftShift GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 |
22 | false,
23 | code,
24 | "TestOperationLeftShift",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 | if err != nil {
31 | ctx.LogError("TestOperationLeftShift DeploySmartContract error:%s", err)
32 | return false
33 | }
34 | //等待出块
35 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
36 | if err != nil {
37 | ctx.LogError("TestOperationLeftShift WaitForGenerateBlock error:%s", err)
38 | return false
39 | }
40 |
41 | if !testOperationLeftShift(ctx, codeAddress, 1, 2) {
42 | return false
43 | }
44 |
45 | if !testOperationLeftShift(ctx, codeAddress, 1326567565434, 2) {
46 | return false
47 | }
48 |
49 | if !testOperationLeftShift(ctx, codeAddress, 2, 3) {
50 | return false
51 | }
52 |
53 | if !testOperationLeftShift(ctx, codeAddress, -1, 2) {
54 | return false
55 | }
56 |
57 | return true
58 | }
59 |
60 | func testOperationLeftShift(ctx *testframework.TestFrameworkContext, code common.Address, a int, b int) bool {
61 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
62 | code,
63 | []interface{}{a, b},
64 | )
65 | if err != nil {
66 | ctx.LogError("TestOperationLeftShift InvokeSmartContract error:%s", err)
67 | return false
68 | }
69 | expect := 0
70 | if b >= 0 {
71 | expect = a << uint(b)
72 | }
73 | resValue,err := res.Result.ToInteger()
74 | if err != nil {
75 | ctx.LogError("TestOperationLeftShift Result.ToInteger error:%s", err)
76 | return false
77 | }
78 | err = ctx.AssertToInt(resValue, expect)
79 | if err != nil {
80 | ctx.LogError("TestOperationLeftShift test %d << %d failed %s", a, b, err)
81 | return false
82 | }
83 | return true
84 | }
85 |
86 | /*
87 | using Neo.SmartContract.Framework;
88 | using Neo.SmartContract.Framework.Services.Neo;
89 |
90 | class A : SmartContract
91 | {
92 | public static int Main(int a, int b)
93 | {
94 | return a << b;
95 | }
96 | }
97 | */
98 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/cond_loop/switch.go:
--------------------------------------------------------------------------------
1 | package cond_loop
2 |
3 | import (
4 | "time"
5 | "github.com/ontio/ontology-go-sdk/utils"
6 | "github.com/ontio/ontology-test/testframework"
7 | "github.com/ontio/ontology/common"
8 | )
9 |
10 | func TestSwitch(ctx *testframework.TestFrameworkContext) bool {
11 | code := "52C56B6C766B00527AC46C766B00C36C766B51527AC46C766B51C351907C907C9E63080051616C756600616C7566"
12 | codeAddress, _ := utils.GetContractAddress(code)
13 | signer, err := ctx.GetDefaultAccount()
14 | if err != nil {
15 | ctx.LogError("TestArray GetDefaultAccount error:%s", err)
16 | return false
17 | }
18 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
19 | signer,
20 |
21 | false,
22 | code,
23 | "TestArray",
24 | "1.0",
25 | "",
26 | "",
27 | "",
28 | )
29 | if err != nil {
30 | ctx.LogError("TestSwitch DeploySmartContract error:%s", err)
31 | return false
32 | }
33 | //等待出块
34 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
35 | if err != nil {
36 | ctx.LogError("TestSwitch WaitForGenerateBlock error:%s", err)
37 | return false
38 | }
39 |
40 | if !testSwitch(ctx, codeAddress, 23) {
41 | return false
42 | }
43 |
44 | if !testSwitch(ctx, codeAddress, 1) {
45 | return false
46 | }
47 |
48 | if !testSwitch(ctx, codeAddress, 0) {
49 | return false
50 | }
51 |
52 | return true
53 | }
54 |
55 | func testSwitch(ctx *testframework.TestFrameworkContext, code common.Address, a int) bool {
56 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
57 | code,
58 | []interface{}{a},
59 | )
60 | if err != nil {
61 | ctx.LogError("TestSwitch InvokeSmartContract error:%s", err)
62 | return false
63 | }
64 | resValue, err := res.Result.ToInteger()
65 | if err != nil {
66 | ctx.LogError("TestSwitch Result.ToInteger error:%s", err)
67 | return false
68 | }
69 | err = ctx.AssertToInt(resValue, tswitch(a))
70 | if err != nil {
71 | ctx.LogError("TestSwitch test switch %d failed %s", a, err)
72 | return false
73 | }
74 | return true
75 | }
76 |
77 | func tswitch(a int) int {
78 | switch a {
79 | case 1:
80 | return 1
81 | default:
82 | return 0
83 | }
84 | }
85 |
86 | /*
87 | using Neo.SmartContract.Framework;
88 | using Neo.SmartContract.Framework.Services.Neo;
89 |
90 | class A : SmartContract
91 | {
92 | public static int Main(int a)
93 | {
94 | switch(a)
95 | {
96 | case 1:
97 | return 1;
98 | default:
99 | return 0;
100 | }
101 | }
102 | }
103 | */
104 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/cond_loop/for.go:
--------------------------------------------------------------------------------
1 | package cond_loop
2 |
3 | import (
4 | "time"
5 | "github.com/ontio/ontology-go-sdk/utils"
6 | "github.com/ontio/ontology-test/testframework"
7 | "github.com/ontio/ontology/common"
8 | )
9 |
10 | func TestFor(ctx *testframework.TestFrameworkContext) bool {
11 | code := "53C56B6C766B00527AC4006C766B51527AC4006C766B52527AC46223006C766B51C36C766B52C3936C766B51527AC46C766B52C351936C766B52527AC46C766B52C36C766B00C39F63D5FF6C766B51C3616C7566"
12 | codeAddress, _ := utils.GetContractAddress(code)
13 | signer, err := ctx.GetDefaultAccount()
14 | if err != nil {
15 | ctx.LogError("TestFor GetDefaultAccount error:%s", err)
16 | return false
17 | }
18 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
19 | signer,
20 |
21 | false,
22 | code,
23 | "TestFor",
24 | "1.0",
25 | "",
26 | "",
27 | "",
28 | )
29 | if err != nil {
30 | ctx.LogError("TestFor DeploySmartContract error:%s", err)
31 | return false
32 | }
33 | //等待出块
34 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
35 | if err != nil {
36 | ctx.LogError("TestFor WaitForGenerateBlock error:%s", err)
37 | return false
38 | }
39 |
40 | if !testFor(ctx, codeAddress, 23) {
41 | return false
42 | }
43 |
44 | if !testFor(ctx, codeAddress, -23) {
45 | return false
46 | }
47 |
48 | if !testFor(ctx, codeAddress, 0) {
49 | return false
50 | }
51 |
52 | return true
53 | }
54 |
55 | func testFor(ctx *testframework.TestFrameworkContext, code common.Address, a int) bool {
56 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
57 | code,
58 | []interface{}{a},
59 | )
60 | if err != nil {
61 | ctx.LogError("TestFor InvokeSmartContract error:%s", err)
62 | return false
63 | }
64 | resValue, err := res.Result.ToInteger()
65 | if err != nil {
66 | ctx.LogError("TestFor Result.ToInteger error:%s", err)
67 | return false
68 | }
69 | err = ctx.AssertToInt(resValue, forloop(a))
70 | if err != nil {
71 | ctx.LogError("TestFor test for %d failed %s", a, err)
72 | return false
73 | }
74 | return true
75 | }
76 |
77 | func forloop(a int) int {
78 | b := 0
79 | for i := 0; i < a; i++ {
80 | b = b + i
81 | }
82 | return b
83 | }
84 |
85 | /*
86 | using Neo.SmartContract.Framework;
87 | using Neo.SmartContract.Framework.Services.Neo;
88 |
89 | class A : SmartContract
90 | {
91 | public static int Main(int a)
92 | {
93 | int b = 0;
94 | for(int i = 0;i < a;i++)
95 | {
96 | b = b+i;
97 | }
98 | return b;
99 | }
100 | }
101 | */
102 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/rightshift.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestOperationRightShift(ctx *testframework.TestFrameworkContext) bool {
12 | code := "52C56B6C766B00527AC46C766B51527AC46C766B00C36C766B51C3011F8499616C7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestOperationRightShift GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 |
22 | false,
23 | code,
24 | "TestOperationRightShift",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 | if err != nil {
31 | ctx.LogError("TestOperationRightShift DeploySmartContract error:%s", err)
32 | return false
33 | }
34 | //等待出块
35 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 2)
36 | if err != nil {
37 | ctx.LogError("TestOperationRightShift WaitForGenerateBlock error:%s", err)
38 | return false
39 | }
40 |
41 | if !testOperationRightShift(ctx, codeAddress, 1, 2) {
42 | return false
43 | }
44 |
45 | if !testOperationRightShift(ctx, codeAddress, 34252452, 3) {
46 | return false
47 | }
48 |
49 | if !testOperationRightShift(ctx, codeAddress, -1, 2) {
50 | return false
51 | }
52 |
53 | if !testOperationRightShift(ctx, codeAddress, 1, -1) {
54 | return false
55 | }
56 |
57 | return true
58 | }
59 |
60 | func testOperationRightShift(ctx *testframework.TestFrameworkContext, code common.Address, a, b int) bool {
61 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
62 | code,
63 | []interface{}{a, b},
64 | )
65 | if err != nil {
66 | ctx.LogError("TestOperationRightShift InvokeSmartContract error:%s", err)
67 | return false
68 | }
69 | resValue,err := res.Result.ToInteger()
70 | if err != nil {
71 | ctx.LogError("TestOperationRightShift Result.ToInteger error:%s", err)
72 | return false
73 | }
74 | expect := 0
75 | if b >= 0 {
76 | expect = a >> uint(b)
77 | }
78 | err = ctx.AssertToInt(resValue, expect)
79 | if err != nil {
80 | ctx.LogError("TestOperationRightShift test %d >> %d failed %s", a, b, err)
81 | return false
82 | }
83 | return true
84 | }
85 |
86 | /*
87 | using Neo.SmartContract.Framework;
88 | using Neo.SmartContract.Framework.Services.Neo;
89 | using System.Numerics;
90 |
91 | public class HelloWorld : SmartContract
92 | {
93 | public static BigInteger Main(BigInteger a, int b)
94 | {
95 | return a >> b;
96 | }
97 | }
98 | */
99 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/add.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestOperationAdd(ctx *testframework.TestFrameworkContext) bool {
12 | code := "52C56B6C766B00527AC46C766B51527AC46C766B00C36C766B51C393616C7566"
13 | codeAddress, err := utils.GetContractAddress(code)
14 | if err != nil {
15 | ctx.LogError("TestOperationAdd GetContractAddress error:%s", err)
16 | return false
17 | }
18 | ctx.LogInfo("TestOperationAdd contact address:%s", codeAddress.ToHexString())
19 | signer, err := ctx.GetDefaultAccount()
20 | if err != nil {
21 | ctx.LogError("TestOperationAdd GetDefaultAccount error:%s", err)
22 | return false
23 | }
24 | tx, err := ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
25 | signer,
26 | false,
27 | code,
28 | "TestOperationAdd",
29 | "1.0",
30 | "",
31 | "",
32 | "",
33 | )
34 | if err != nil {
35 | ctx.LogError("TestOperationAdd DeploySmartContract error:%s", err)
36 | return false
37 | }
38 | ctx.LogInfo("DeployContract TxHash:%s", tx.ToHexString())
39 | //等待出块
40 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
41 | if err != nil {
42 | ctx.LogError("TestOperationAdd WaitForGenerateBlock error:%s", err)
43 | return false
44 | }
45 |
46 | if !testOperationAdd(ctx, codeAddress, 1, 2) {
47 | return false
48 | }
49 |
50 | if !testOperationAdd(ctx, codeAddress, -1, 1) {
51 | return false
52 | }
53 |
54 | if !testOperationAdd(ctx, codeAddress, -1, -2) {
55 | return false
56 | }
57 |
58 | if !testOperationAdd(ctx, codeAddress, 0, 0) {
59 | return false
60 | }
61 |
62 | return true
63 | }
64 |
65 | func testOperationAdd(ctx *testframework.TestFrameworkContext, codeAddress common.Address, a, b int) bool {
66 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
67 | codeAddress,
68 | []interface{}{a, b},
69 | )
70 | if err != nil {
71 | ctx.LogError("TestOperationAdd InvokeSmartContract error:%s", err)
72 | return false
73 | }
74 | resValue,err := res.Result.ToInteger()
75 | if err != nil {
76 | ctx.LogError("TestOperationAdd Result.ToInteger error:%s", err)
77 | return false
78 | }
79 | err = ctx.AssertToInt(resValue, a+b)
80 | if err != nil {
81 | ctx.LogError("TestOperationAdd test failed %s , %d, %d", err, a, b)
82 | return false
83 | }
84 | return true
85 | }
86 |
87 | /*
88 | using Neo.SmartContract.Framework;
89 | using Neo.SmartContract.Framework.Services.Neo;
90 |
91 | class A : SmartContract
92 | {
93 | public static int Main(int a, int b)
94 | {
95 | return a + b;
96 | }
97 | }
98 | */
99 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/cond_loop/ifelse.go:
--------------------------------------------------------------------------------
1 | package cond_loop
2 |
3 | import (
4 | "time"
5 | "github.com/ontio/ontology-go-sdk/utils"
6 | "github.com/ontio/ontology-test/testframework"
7 | "github.com/ontio/ontology/common"
8 | )
9 |
10 | func TestIfElse(ctx *testframework.TestFrameworkContext) bool {
11 | code := "52C56B6C766B00527AC46C766B51527AC46C766B00C36C766B51C3A163080051616C75666C766B00C36C766B51C3A26308004F616C756600616C7566"
12 | codeAddress, _ := utils.GetContractAddress(code)
13 | signer, err := ctx.GetDefaultAccount()
14 | if err != nil {
15 | ctx.LogError("TestIfElse GetDefaultAccount error:%s", err)
16 | return false
17 | }
18 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
19 | signer,
20 |
21 | false,
22 | code,
23 | "TestIfElse",
24 | "1.0",
25 | "",
26 | "",
27 | "",
28 | )
29 | if err != nil {
30 | ctx.LogError("TestIfElse DeploySmartContract error:%s", err)
31 | return false
32 | }
33 | //等待出块
34 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
35 | if err != nil {
36 | ctx.LogError("TestIfElse WaitForGenerateBlock error:%s", err)
37 | return false
38 | }
39 |
40 | if !testIfElse(ctx, codeAddress, 23, 2) {
41 | return false
42 | }
43 |
44 | if !testIfElse(ctx, codeAddress, 2, 23) {
45 | return false
46 | }
47 |
48 | if !testIfElse(ctx, codeAddress, 0, 0) {
49 | return false
50 | }
51 |
52 | return true
53 | }
54 |
55 | func testIfElse(ctx *testframework.TestFrameworkContext, code common.Address, a, b int) bool {
56 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
57 | code,
58 | []interface{}{a, b},
59 | )
60 | if err != nil {
61 | ctx.LogError("TestIfElse InvokeSmartContract error:%s", err)
62 | return false
63 | }
64 | resValue, err := res.Result.ToInteger()
65 | if err != nil {
66 | ctx.LogError("TestIfElse Result.ToInteger error:%s", err)
67 | return false
68 | }
69 | err = ctx.AssertToInt(resValue, condIfElse(a, b))
70 | if err != nil {
71 | ctx.LogError("TestIfElse test %d ifelse %d failed %s", a, b, err)
72 | return false
73 | }
74 | return true
75 | }
76 |
77 | func condIfElse(a, b int) int {
78 | if a > b {
79 | return 1
80 | } else if a < b {
81 | return -1
82 | } else {
83 | return 0
84 | }
85 | }
86 |
87 | /*
88 | using Neo.SmartContract.Framework;
89 | using Neo.SmartContract.Framework.Services.Neo;
90 |
91 | class A : SmartContract
92 | {
93 | public static int Main(int a, int b)
94 | {
95 | if(a > b)
96 | {
97 | return 1;
98 | }
99 | else if(a < b)
100 | {
101 | return -1;
102 | }
103 | else{
104 | return 0;
105 | }
106 | }
107 | }
108 | */
109 |
--------------------------------------------------------------------------------
/common/config.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | */
18 |
19 | //common use fot ontology-test
20 | package common
21 |
22 | import (
23 | "encoding/json"
24 | "fmt"
25 | log4 "github.com/alecthomas/log4go"
26 | "io/ioutil"
27 | "os"
28 | )
29 |
30 | //Default config instance
31 | var DefConfig = NewTestConfig()
32 |
33 | //Config object used by ontology-instance
34 | type TestConfig struct {
35 | //JsonRpcAddress of ontology
36 | JsonRpcAddress string
37 | //RestfulAddress of ontology
38 | RestfulAddress string
39 | //WebSocketAddress of ontology
40 | WebSocketAddress string
41 | //WalletFile of test
42 | WalletFile string
43 | //The Password of wallet
44 | Password string
45 | //Gas Price of transaction
46 | GasPrice uint64
47 | //Gas Limit of invoke transaction
48 | GasLimit uint64
49 | //Gas Limit of deploy transaction
50 | GasDeployLimit uint64
51 | }
52 |
53 | //NewTestConfig retuen a TestConfig instance
54 | func NewTestConfig() *TestConfig {
55 | return &TestConfig{}
56 | }
57 |
58 | //Init TestConfig with a config file
59 | func (this *TestConfig) Init(fileName string) error {
60 | err := this.loadConfig(fileName)
61 | if err != nil {
62 | return fmt.Errorf("loadConfig error:%s", err)
63 | }
64 | return nil
65 | }
66 |
67 | func (this *TestConfig) loadConfig(fileName string) error {
68 | data, err := this.readFile(fileName)
69 | if err != nil {
70 | return err
71 | }
72 | err = json.Unmarshal(data, this)
73 | if err != nil {
74 | return fmt.Errorf("json.Unmarshal TestConfig:%s error:%s", data, err)
75 | }
76 | return nil
77 | }
78 |
79 | func (this *TestConfig) readFile(fileName string) ([]byte, error) {
80 | file, err := os.OpenFile(fileName, os.O_RDONLY, 0666)
81 | if err != nil {
82 | return nil, fmt.Errorf("OpenFile %s error %s", fileName, err)
83 | }
84 | defer func() {
85 | err := file.Close()
86 | if err != nil {
87 | log4.Error("File %s close error %s", fileName, err)
88 | }
89 | }()
90 | data, err := ioutil.ReadAll(file)
91 | if err != nil {
92 | return nil, fmt.Errorf("ioutil.ReadAll %s error %s", fileName, err)
93 | }
94 | return data, nil
95 | }
96 |
--------------------------------------------------------------------------------
/testcase/smartcontract/neovm/utils/asbytearray_biginteger.go:
--------------------------------------------------------------------------------
1 | package utils
2 |
3 | import (
4 | "math/big"
5 | "time"
6 |
7 | "github.com/ontio/ontology-go-sdk/utils"
8 | "github.com/ontio/ontology-test/testframework"
9 | "github.com/ontio/ontology/common"
10 | )
11 |
12 | func TestAsByteArrayBigInteger(ctx *testframework.TestFrameworkContext) bool {
13 | code := "52c56b6c766b00527ac4616c766b00c36c766b51527ac46203006c766b51c3616c7566"
14 | codeAddress, _ := utils.GetContractAddress(code)
15 | signer, err := ctx.GetDefaultAccount()
16 | if err != nil {
17 | ctx.LogError("TestAsByteArrayBigInteger GetDefaultAccount error:%s", err)
18 | return false
19 | }
20 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
21 | signer,
22 | false,
23 | code,
24 | "TestAsByteArrayBigInteger",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 | if err != nil {
31 | ctx.LogError("TestAsByteArrayBigInteger DeploySmartContract error:%s", err)
32 | return false
33 | }
34 | //等待出块
35 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
36 | if err != nil {
37 | ctx.LogError("TestAsByteArrayBigInteger WaitForGenerateBlock error:%s", err)
38 | return false
39 | }
40 |
41 | input := new(big.Int).SetInt64(1)
42 | if !testAsArray_BigInteger(ctx, codeAddress, input) {
43 | return false
44 | }
45 | input = new(big.Int).SetInt64(0)
46 | if !testAsArray_BigInteger(ctx, codeAddress, input) {
47 | return false
48 | }
49 | input = new(big.Int).SetInt64(-233545554)
50 | if !testAsArray_BigInteger(ctx, codeAddress, input) {
51 | return false
52 | }
53 | input = new(big.Int).SetInt64(-3434)
54 | if !testAsArray_BigInteger(ctx, codeAddress, input) {
55 | return false
56 | }
57 | return true
58 | }
59 |
60 | func testAsArray_BigInteger(ctx *testframework.TestFrameworkContext, code common.Address, input *big.Int) bool {
61 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
62 | code,
63 | []interface{}{input},
64 | )
65 | if err != nil {
66 | ctx.LogError("TestAsByteArrayBigInteger InvokeSmartContract error:%s", err)
67 | return false
68 | }
69 | if res.State == 0 {
70 | ctx.LogError("TestAsByteArrayBigInteger PreExecInvokeNeoVMContract failed. state == 0")
71 | return false
72 | }
73 | resValue, err := res.Result.ToInteger()
74 | if err != nil {
75 | ctx.LogError("TestAsByteArrayBigInteger Result.ToInteger error:%s", err)
76 | return false
77 | }
78 | err = ctx.AssertBigInteger(resValue, input)
79 | if err != nil {
80 | ctx.LogError("TestAsByteArrayBigInteger test failed %s", err)
81 | return false
82 | }
83 | return true
84 | }
85 |
86 | /*
87 | using System.Numerics;
88 | using Neo.SmartContract.Framework;
89 | using Neo.SmartContract.Framework.Services.Neo;
90 |
91 | class A : SmartContract
92 | {
93 | public static byte[] Main(BigInteger arg)
94 | {
95 | return arg.AsByteArray();
96 | }
97 | }
98 | */
99 |
--------------------------------------------------------------------------------
/test_data/blockapi.c:
--------------------------------------------------------------------------------
1 | #include "ont.h"
2 |
3 | char * invoke(char * method,char * args){
4 |
5 | if (strcmp(method,"getCurrentHeadHash")==0){
6 | char * hash = ONT_Block_GetCurrentHeaderHash();
7 | char * result = ONT_JsonMashalResult(hash,"string",1);
8 | ONT_Runtime_Notify(result);
9 | return result;
10 | }
11 |
12 | if(strcmp(method,"getCurrentHeaderHeight")==0){
13 | int height = ONT_Block_GetCurrentBlockHeight();
14 | char * result = ONT_JsonMashalResult(height,"int",1);
15 | ONT_Runtime_Notify(result);
16 | return result;
17 | }
18 |
19 | if(strcmp(method,"getCurrentBlockHash")==0){
20 | char * hash = ONT_Block_GetCurrentBlockHash();
21 | char * result = ONT_JsonMashalResult(hash,"string",1);
22 | ONT_Runtime_Notify(result);
23 | return result;
24 | }
25 |
26 | if(strcmp(method,"getCurrentBlockHeight")==0){
27 | int height = ONT_Block_GetCurrentBlockHeight();
28 | char * result = ONT_JsonMashalResult(height,"int",1);
29 | ONT_Runtime_Notify(result);
30 | return result;
31 | }
32 |
33 | if(strcmp(method,"getTransactionByHash")==0){
34 | char * hash = ONT_ReadStringParam(args);
35 | char * data = ONT_Block_GetTransactionByHash(hash);
36 | char * result = ONT_JsonMashalResult(data,"byte_array",1);
37 | ONT_Runtime_Notify(result);
38 | return result;
39 | }
40 |
41 | if(strcmp(method,"getTransactionCountByHash")==0){
42 | char * hash = ONT_ReadStringParam(args);
43 | int count = ONT_Block_GetTransactionCountByBlkHash(hash);
44 | char * result = ONT_JsonMashalResult(count,"int",1);
45 | ONT_Runtime_Notify(result);
46 | return result;
47 | }
48 |
49 | if(strcmp(method,"getTransactionCountByHeight")==0){
50 | int height = ONT_ReadInt32Param(args);
51 | int count = ONT_Block_GetTransactionCountByBlkHeight(height);
52 | char * result = ONT_JsonMashalResult(count,"int",1);
53 | ONT_Runtime_Notify(result);
54 | return result;
55 | }
56 |
57 | if(strcmp(method,"getTransactions")==0){
58 | char * hash = ONT_ReadStringParam(args);
59 | char ** trans = ONT_Block_GetTransactionsByBlkHash(hash);
60 | char * result = ONT_JsonMashalResult(trans,"string_array",1);
61 | ONT_Runtime_Notify(result);
62 | return result;
63 | }
64 |
65 | if(strcmp(method,"getTransactionsByHeight")==0){
66 | int height = ONT_ReadInt32Param(args);
67 | char ** trans = ONT_Block_GetTransactionsByBlkHeight(height);
68 | char * result = ONT_JsonMashalResult(trans,"string_array",1);
69 | ONT_Runtime_Notify(result);
70 | return result;
71 | }
72 |
73 | char * failed = ONT_JsonMashalResult(strconcat(method,"not supported"),"string",0);
74 | ONT_Runtime_Notify(failed);
75 | return failed;
76 | }
--------------------------------------------------------------------------------
/testcase/smartcontract/neovm/deploy_invoke/invoke.go:
--------------------------------------------------------------------------------
1 | package deploy_invoke
2 |
3 | import (
4 | "github.com/ontio/ontology-test/testframework"
5 | "time"
6 | )
7 |
8 | func TestInvokeSmartContract(ctx *testframework.TestFrameworkContext) bool {
9 | signer, err := ctx.GetDefaultAccount()
10 | if err != nil {
11 | ctx.LogError("TestInvokeSmartContract GetDefaultAccount error:%s", err)
12 | return false
13 | }
14 |
15 | txHash, err := ctx.Ont.NeoVM.InvokeNeoVMContract(ctx.GetGasPrice(), ctx.GetGasLimit(), signer, contractCodeAddress, []interface{}{})
16 | if err != nil {
17 | ctx.LogError("TestInvokeSmartContract InvokeNeoVMSmartContract error:%s", err)
18 | return false
19 | }
20 |
21 | ctx.LogInfo("TestInvokeSmartContract txHash:%s\n", txHash.ToHexString())
22 | //WaitForGenerateBlock
23 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
24 | if err != nil {
25 | ctx.LogError("TestInvokeSmartContract WaitForGenerateBlock error:%s", err)
26 | return false
27 | }
28 |
29 | //Test GetStorageItem api
30 | skey := "Hello"
31 | svalue, err := ctx.Ont.GetStorage(contractCodeAddress.ToHexString(), []byte(skey))
32 | if err != nil {
33 | ctx.LogError("TestInvokeSmartContract GetStorageItem key:%s error:%s", skey, err)
34 | return false
35 | }
36 | err = ctx.AssertToString(string(svalue), "World")
37 | if err != nil {
38 | ctx.LogError("TestInvokeSmartContract AssertToString error:%s", err)
39 | return false
40 | }
41 |
42 | //GetEventLog, to check the result of invoke
43 | events, err := ctx.Ont.GetSmartContractEvent(txHash.ToHexString())
44 | if err != nil {
45 | ctx.LogError("TestInvokeSmartContract GetSmartContractEvent error:%s", err)
46 | return false
47 | }
48 | if events.State == 0 {
49 | ctx.LogError("TestInvokeSmartContract failed invoked exec state return 0")
50 | return false
51 | }
52 | notify := events.Notify[0]
53 | ctx.LogInfo("%+v", notify)
54 |
55 | invokeState := notify.States.([]interface{})
56 | //Event name
57 | name, _ := ctx.ConvertToHexString(invokeState[0])
58 | err = ctx.AssertToString(name, "transfer")
59 | if err != nil {
60 | ctx.LogError("TestInvokeSmartContract failed AssertToString:%s", err)
61 | return false
62 | }
63 |
64 | //key of event
65 | key, _ := ctx.ConvertToHexString(invokeState[1])
66 | err = ctx.AssertToString(key, "hello")
67 | if err != nil {
68 | ctx.LogError("TestInvokeSmartContract failed AssertToString %s ", err)
69 | return false
70 | }
71 |
72 | //value of event
73 | value, _ := ctx.ConvertToHexString(invokeState[2])
74 | err = ctx.AssertToString(value, "world")
75 | if err != nil {
76 | ctx.LogError("TestInvokeSmartContract failed AssertToString %s ", err)
77 | return false
78 | }
79 |
80 | //amount of event
81 | amount, _ := ctx.ConvertToBigInt(invokeState[3])
82 | err = ctx.AssertToInt(amount, 123)
83 | if err != nil {
84 | ctx.LogError("TestInvokeSmartContract failed AssertToInt %s ", err)
85 | return false
86 | }
87 |
88 | return true
89 | }
90 |
--------------------------------------------------------------------------------
/testcase/smartcontract/native/ontid/common.go:
--------------------------------------------------------------------------------
1 | package ontid
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-crypto/keypair"
7 | "github.com/ontio/ontology-test/testframework"
8 | sdk"github.com/ontio/ontology-go-sdk"
9 | "github.com/ontio/ontology/common"
10 | )
11 |
12 | type Contract struct {
13 | Address common.Address
14 | Method string
15 | Args []interface{}
16 | }
17 |
18 | func getEvent(ctx *testframework.TestFrameworkContext, txHash common.Uint256) bool {
19 | _, err := ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
20 | if err != nil {
21 | ctx.LogError("WaitForGenerateBlock error: %s", err)
22 | return false
23 | }
24 | events, err := ctx.Ont.GetSmartContractEvent(txHash.ToHexString())
25 | if err != nil {
26 | ctx.LogError("GetSmartContractEvent error: %s", err)
27 | return false
28 | }
29 |
30 | if events.State == 0 {
31 | ctx.LogWarn("ontio contract invoke failed, state:0")
32 | return false
33 | }
34 |
35 | if len(events.Notify) > 0 {
36 | states := events.Notify[0].States
37 | ctx.LogInfo("result is : %+v", states)
38 | return true
39 | } else {
40 | return false
41 | }
42 |
43 | }
44 |
45 | func InvokeContract(ctx *testframework.TestFrameworkContext, contract *Contract, isPreInvoke bool) (bool, []byte) {
46 | if !isPreInvoke {
47 | user, _ := ctx.GetDefaultAccount()
48 | txHash, err := ctx.Ont.Native.InvokeNativeContract(
49 | ctx.GetGasPrice(),
50 | ctx.GetGasLimit(),
51 | user,
52 | 0,
53 | contract.Address,
54 | contract.Method,
55 | contract.Args,
56 | )
57 | if err != nil {
58 | ctx.LogError("InvokeNativeContract error: %s", err)
59 | return false, nil
60 | }
61 | return getEvent(ctx, txHash), nil
62 | } else {
63 | res, err := ctx.Ont.Native.PreExecInvokeNativeContract(
64 | contract.Address,
65 | 0,
66 | contract.Method,
67 | contract.Args,
68 | )
69 | if err != nil {
70 | ctx.LogError("PrepareInvokeNativeContract failed, %s", err)
71 | return false, nil
72 | }
73 |
74 | data, err := res.Result.ToByteArray()
75 | if err != nil {
76 | ctx.LogError("error result value type")
77 | return false, nil
78 | }
79 | ctx.LogInfo("result: %s", data)
80 | return true, data
81 | }
82 | }
83 |
84 | func MultiSigInvoke(ctx *testframework.TestFrameworkContext, c *Contract, m uint16, pubs []keypair.PublicKey, user *sdk.Account) bool {
85 | tx, err := ctx.Ont.Native.NewNativeInvokeTransaction(
86 | ctx.GetGasPrice(),
87 | ctx.GetGasLimit(),
88 | 0,
89 | c.Address,
90 | c.Method,
91 | c.Args,
92 | )
93 | if err != nil {
94 | ctx.LogError(err)
95 | return false
96 | }
97 |
98 | err = ctx.Ont.MultiSignToTransaction(tx, m, pubs, user)
99 | if err != nil {
100 | ctx.LogError("MultiSignToTransaction error: %s", err)
101 | return false
102 | }
103 | txHash, err := ctx.Ont.SendTransaction(tx)
104 | if err != nil {
105 | ctx.LogError("SendRawTransaction error: %s", err)
106 | }
107 | return getEvent(ctx, txHash)
108 | }
109 |
--------------------------------------------------------------------------------
/testcase/smartcontract/native/transfer_multi.go:
--------------------------------------------------------------------------------
1 | package native
2 |
3 | import (
4 | "github.com/ontio/ontology-test/testframework"
5 | "github.com/ontio/ontology/smartcontract/service/native/ont"
6 | "time"
7 | )
8 |
9 | func TestOntTransferMulti(ctx *testframework.TestFrameworkContext) bool {
10 | defAcc, err := ctx.GetDefaultAccount()
11 | if err != nil {
12 | ctx.LogError("TestOntTransferMulti GetDefaultAccount error:%s", err)
13 | return false
14 | }
15 | acc1 := ctx.NewAccount()
16 | acc2 := ctx.NewAccount()
17 |
18 | _, err = ctx.Ont.Native.Ont.Transfer(ctx.GetGasPrice(), ctx.GetGasLimit(), defAcc, acc1.Address, 10)
19 | if err != nil {
20 | ctx.LogError("TestOntTransferMulti Rpc.Transfer error:%s", err)
21 | return false
22 | }
23 | _, err = ctx.Ont.Native.Ont.Transfer(ctx.GetGasPrice(), ctx.GetGasLimit(), defAcc, acc2.Address, 10)
24 | if err != nil {
25 | ctx.LogError("TestOntTransferMulti Rpc.Transfer error:%s", err)
26 | return false
27 | }
28 | _, err = ctx.Ont.WaitForGenerateBlock(time.Second*30, 1)
29 | if err != nil {
30 | ctx.LogError("TestOntTransferMulti WaitForGenerateBlock error:%s", err)
31 | return false
32 | }
33 |
34 | //Start multi address transfer
35 | multiTransfer, err := ctx.Ont.Native.Ont.NewMultiTransferTransaction(ctx.GetGasPrice(), ctx.GetGasLimit(), []*ont.State{
36 | {From: acc1.Address, To: acc2.Address, Value: 2},
37 | {From: acc2.Address, To: acc1.Address, Value: 8},
38 | })
39 | if err != nil {
40 | ctx.LogError("TestOntTransferMulti NewMultiTransferTransfer error:%s", err)
41 | return false
42 | }
43 |
44 | err = ctx.Ont.SignToTransaction(multiTransfer, acc1)
45 | if err != nil {
46 | ctx.LogError("TestOntTransferMulti SignToTransaction error:%s", err)
47 | return false
48 | }
49 | err = ctx.Ont.SignToTransaction(multiTransfer, acc2)
50 | if err != nil {
51 | ctx.LogError("TestOntTransferMulti SignToTransaction error:%s", err)
52 | return false
53 | }
54 | txHash, err := ctx.Ont.SendTransaction(multiTransfer)
55 | if err != nil {
56 | ctx.LogError("TestOntTransferMulti SendRawTransaction error:%s", err)
57 | return false
58 | }
59 | ctx.LogInfo("TestOntTransferMulti MultiTransfer TxHash:%s", txHash.ToHexString())
60 |
61 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
62 | if err != nil {
63 | ctx.LogError("TestOntTransferMulti WaitForGenerateBlock error:%s", err)
64 | return false
65 | }
66 |
67 | acc1_balance, err := ctx.Ont.Native.Ont.BalanceOf(acc1.Address)
68 | if err != nil {
69 | ctx.LogError("TestOntTransferMulti Rpc.GetBalance error:%s", err)
70 | return false
71 | }
72 | acc2_balance, err := ctx.Ont.Native.Ont.BalanceOf(acc2.Address)
73 | if err != nil {
74 | ctx.LogError("TestOntTransferMulti Rpc.GetBalance error:%s", err)
75 | return false
76 | }
77 |
78 | if acc1_balance != 16 {
79 | ctx.LogError("TestOntTransferMulti Account1 balance %d != %d", acc1_balance, 16)
80 | return false
81 | }
82 |
83 | if acc2_balance != 4 {
84 | ctx.LogError("TestOntTransferMulti Account1 balance %d != %d", acc2_balance, 4)
85 | return false
86 | }
87 | return true
88 | }
89 |
--------------------------------------------------------------------------------
/testcase/smartcontract/api/runtime/notify.go:
--------------------------------------------------------------------------------
1 | package runtime
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | )
9 |
10 | /**
11 | using Neo.SmartContract.Framework;
12 | using Neo.SmartContract.Framework.Services.Neo;
13 | using System;
14 | using System.ComponentModel;
15 | using System.Numerics;
16 |
17 | public class HelloWorld : SmartContract
18 | {
19 | public delegate void PushDelegate(string operation,string msg);
20 |
21 | [DisplayName("notify")]
22 | public static event PushDelegate Notify;
23 |
24 | public static void Main()
25 | {
26 | Notify("hello", "world");
27 | }
28 | }
29 | */
30 |
31 | func TestRuntimeNotify(ctx *testframework.TestFrameworkContext) bool {
32 | code := "00c56b61610568656c6c6f05776f726c64617c066e6f7469667953c1681553797374656d2e52756e74696d652e4e6f7469667961616c7566"
33 | codeAddr, _ := utils.GetContractAddress(code)
34 | signer, err := ctx.GetDefaultAccount()
35 |
36 | if err != nil {
37 | ctx.LogError("TestRuntimeNotify - GetDefaultAccount error: %s", err)
38 | return false
39 | }
40 |
41 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
42 | signer,
43 | true,
44 | code,
45 | "TestRuntimeNotify",
46 | "",
47 | "",
48 | "",
49 | "")
50 |
51 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
52 | if err != nil {
53 | ctx.LogError("TestRuntimeNotify WaitForGenerateBlock error:%s", err)
54 | return false
55 | }
56 |
57 | txHash, err := ctx.Ont.NeoVM.InvokeNeoVMContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
58 | signer,
59 | codeAddr,
60 | []interface{}{},
61 | )
62 |
63 | if err != nil {
64 | ctx.LogError("TestRuntimeNotify InvokeSmartContract error:%s", err)
65 | return false
66 | }
67 |
68 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
69 | if err != nil {
70 | ctx.LogError("TestRuntimeNotify WaitForGenerateBlock error:%s", err)
71 | return false
72 | }
73 |
74 | events, err := ctx.Ont.GetSmartContractEvent(txHash.ToHexString())
75 | if err != nil {
76 | ctx.LogError("TestRuntimeNotify GetSmartContractEvent error:%s", err)
77 | return false
78 | }
79 | if events.State == 0 {
80 | ctx.LogError("TestRuntimeNotify contract invoke failed, state:0")
81 | return false
82 | }
83 | notify := events.Notify[0].States.([]interface{})
84 |
85 | name, _ := ctx.ConvertToHexString(notify[0])
86 |
87 | err = ctx.AssertToString(name, "notify")
88 | if err != nil {
89 | ctx.LogError("TestRuntimeNotify failed AssertToString:%s", err)
90 | return false
91 | }
92 |
93 | key, _ := ctx.ConvertToHexString(notify[1])
94 | err = ctx.AssertToString(key, "hello")
95 | if err != nil {
96 | ctx.LogError("TestRuntimeNotify failed AssertToString %s ", err)
97 | return false
98 | }
99 |
100 | value, _ := ctx.ConvertToHexString(notify[2])
101 | err = ctx.AssertToString(value, "world")
102 | if err != nil {
103 | ctx.LogError("TestRuntimeNotify failed AssertToString %s ", err)
104 | return false
105 | }
106 |
107 | return true
108 | }
109 |
--------------------------------------------------------------------------------
/test_data/ont.h:
--------------------------------------------------------------------------------
1 | //system apis
2 | void * calloc(int count,int length);
3 | void * malloc(int size);
4 | int arrayLen(void *a);
5 | int memcpy(void * dest,void * src,int length);
6 | int memset(void * dest,char c,int length);
7 |
8 | //utility apis
9 | int strcmp(char *a,char *b);
10 | char * strconcat(char *a,char *b);
11 | int Atoi(char * s);
12 | long long Atoi64(char *s);
13 | char * Itoa(int a);
14 | char * I64toa(long long amount,int radix);
15 | char * SHA1(char *s);
16 | char * SHA256(char *s);
17 |
18 | //parameter apis
19 | int ONT_ReadInt32Param(char *args);
20 | long long ONT_ReadInt64Param(char * args);
21 | char * ONT_ReadStringParam(char * args);
22 | void ONT_JsonUnmashalInput(void * addr,int size,char * arg);
23 | char * ONT_JsonMashalResult(void * val,char * types,int succeed);
24 | char * ONT_JsonMashalParams(void * s);
25 | char * ONT_RawMashalParams(void *s);
26 | char * ONT_GetCallerAddress();
27 | char * ONT_GetSelfAddress();
28 | char * ONT_CallContract(char * address,char * contractCode,char * method,char * args);
29 | char * ONT_MarshalNativeParams(void * s);
30 | char * ONT_MarshalNeoParams(void * s);
31 |
32 | //Runtime apis
33 | int ONT_Runtime_CheckWitness(char * address);
34 | void ONT_Runtime_Notify(char * address);
35 | int ONT_Runtime_CheckSig(char * pubkey,char * data,char * sig);
36 | int ONT_Runtime_GetTime();
37 | void ONT_Runtime_Log(char * message);
38 |
39 | //Attribute apis
40 | int ONT_Attribute_GetUsage(char * data);
41 | char * ONT_Attribute_GetData(char * data);
42 |
43 | //Block apis
44 | char * ONT_Block_GetCurrentHeaderHash();
45 | int ONT_Block_GetCurrentHeaderHeight();
46 | char * ONT_Block_GetCurrentBlockHash();
47 | int ONT_Block_GetCurrentBlockHeight();
48 | char * ONT_Block_GetTransactionByHash(char * hash);
49 | int * ONT_Block_GetTransactionCountByBlkHash(char * hash);
50 | int * ONT_Block_GetTransactionCountByBlkHeight(int height);
51 | char ** ONT_Block_GetTransactionsByBlkHash(char * hash);
52 | char ** ONT_Block_GetTransactionsByBlkHeight(int height);
53 |
54 |
55 | //Blockchain apis
56 | int ONT_BlockChain_GetHeight();
57 | char * ONT_BlockChain_GetHeaderByHeight(int height);
58 | char * ONT_BlockChain_GetHeaderByHash(char * hash);
59 | char * ONT_BlockChain_GetBlockByHeight(int height);
60 | char * ONT_BlockChain_GetBlockByHash(char * hash);
61 | char * ONT_BlockChain_GetContract(char * address);
62 |
63 | //header apis
64 | char * ONT_Header_GetHash(char * data);
65 | int ONT_Header_GetVersion(char * data);
66 | char * ONT_Header_GetPrevHash(char * data);
67 | char * ONT_Header_GetMerkleRoot(char * data);
68 | int ONT_Header_GetIndex(char * data);
69 | int ONT_Header_GetTimestamp(char * data);
70 | long long ONT_Header_GetConsensusData(char * data);
71 | char * ONT_Header_GetNextConsensus(char * data);
72 |
73 | //storage apis
74 | void ONT_Storage_Put(char * key,char * value);
75 | char * ONT_Storage_Get(char * key);
76 | void ONT_Storage_Delete(char * key);
77 |
78 | //transaction apis
79 | char * ONT_Transaction_GetHash(char * data);
80 | int ONT_Transaction_GetType(char * data);
81 | char * ONT_Transaction_GetAttributes(char * data);
82 |
83 | //for debug only
84 | void ContractLogDebug(char * msg);
85 | void ContractLogInfo(char * msg);
86 | void ContractLogError(char * msg);
--------------------------------------------------------------------------------
/testcase/vm/neovm/operator/divide.go:
--------------------------------------------------------------------------------
1 | package operator
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestOperationDivide(ctx *testframework.TestFrameworkContext) bool {
12 | code := "52C56B6C766B00527AC46C766B51527AC46C766B00C36C766B51C396616C7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestOperationDivide GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 |
22 | false,
23 | code,
24 | "TestOperationDivide",
25 | "1.0",
26 | "",
27 | "",
28 | "",
29 | )
30 | if err != nil {
31 | ctx.LogError("TestOperationDivide DeploySmartContract error:%s", err)
32 | return false
33 | }
34 | //等待出块
35 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
36 | if err != nil {
37 | ctx.LogError("TestOperationDivide WaitForGenerateBlock error:%s", err)
38 | return false
39 | }
40 |
41 | if !testOperationDivideFail(ctx, codeAddress, 10, 0) {
42 | return false
43 | }
44 |
45 | if !testOperationDivide(ctx, codeAddress, 23, 2) {
46 | return false
47 | }
48 |
49 | if !testOperationDivide(ctx, codeAddress, 544, 345) {
50 | return false
51 | }
52 |
53 | if !testOperationDivide(ctx, codeAddress, 3456345, 3545) {
54 | return false
55 | }
56 |
57 | if !testOperationDivide(ctx, codeAddress, -10, -234) {
58 | return false
59 | }
60 |
61 | if !testOperationDivide(ctx, codeAddress, -345, 34) {
62 | return false
63 | }
64 |
65 | if !testOperationDivide(ctx, codeAddress, 0, 100) {
66 | return false
67 | }
68 |
69 | return true
70 | }
71 |
72 | func testOperationDivide(ctx *testframework.TestFrameworkContext, code common.Address, a, b int) bool {
73 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
74 | code,
75 | []interface{}{a, b},
76 | )
77 | if err != nil {
78 | ctx.LogError("TestOperationDivide InvokeSmartContract error:%s", err)
79 | return false
80 | }
81 | resValue,err := res.Result.ToInteger()
82 | if err != nil {
83 | ctx.LogError("TestOperationDivide Result.ToInteger error:%s", err)
84 | return false
85 | }
86 | err = ctx.AssertToInt(resValue, a/b)
87 | if err != nil {
88 | ctx.LogError("TestOperationDivide test %d / %d failed %s", a, b, err)
89 | return false
90 | }
91 | return true
92 | }
93 |
94 | func testOperationDivideFail(ctx *testframework.TestFrameworkContext, code common.Address, a, b int) bool {
95 | _, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
96 | code,
97 | []interface{}{a, b},
98 | )
99 | if err == nil {
100 | ctx.LogError("testOperationDivideFail %v / %v should failed", a, b)
101 | return false
102 | }
103 | return true
104 | }
105 |
106 | /*
107 | using Neo.SmartContract.Framework;
108 | using Neo.SmartContract.Framework.Services.Neo;
109 |
110 | class A : SmartContract
111 | {
112 | public static int Main(int a, int b)
113 | {
114 | return a / b;
115 | }
116 | }
117 | */
118 |
--------------------------------------------------------------------------------
/testcase/smartcontract/api/transaction/gettype.go:
--------------------------------------------------------------------------------
1 | package transaction
2 |
3 | import (
4 | "time"
5 |
6 | "bytes"
7 |
8 | "github.com/ontio/ontology-go-sdk/utils"
9 | "github.com/ontio/ontology-test/testframework"
10 | ctypes "github.com/ontio/ontology/core/types"
11 | )
12 |
13 | /*
14 |
15 | using Neo.SmartContract.Framework;
16 | using Neo.SmartContract.Framework.Services.Neo;
17 | using Neo.SmartContract.Framework.Services.System;
18 | using System.Numerics;
19 |
20 | public class A : SmartContract
21 | {
22 | public static BigInteger Main(byte[] txHash)
23 | {
24 | Transaction tx = Blockchain.GetTransaction(txHash);
25 | Storage.Put(Storage.CurrentContext, "txType", tx.Type);
26 | return 0;
27 | }
28 | }
29 |
30 | */
31 |
32 | func TestGetTxType(ctx *testframework.TestFrameworkContext) bool {
33 | code := "51c56b610c48656c6c6f20576f726c64216c766b00527ac46203006c766b00c3616c7566"
34 | signer, err := ctx.GetDefaultAccount()
35 |
36 | if err != nil {
37 | ctx.LogError("TestGetTxType - GetDefaultAccount error: %s", err)
38 | return false
39 | }
40 |
41 | txHash, err := ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
42 | signer,
43 | true,
44 | code,
45 | "TestGetTxType",
46 | "",
47 | "",
48 | "",
49 | "")
50 |
51 | if err != nil {
52 | ctx.LogError("TestGetTxType DeploySmartContract error: %s", err)
53 | return false
54 | }
55 |
56 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
57 | if err != nil {
58 | ctx.LogError("TestGetTxType WaitForGenerateBlock error: %s", err)
59 | return false
60 | }
61 |
62 | code = "53c56b6c766b00527ac4616c766b00c361682053797374656d2e426c6f636b636861696e2e4765745472616e73616374696f6e6c766b51527ac461681953797374656d2e53746f726167652e476574436f6e74657874067478547970656c766b51c361681c4f6e746f6c6f67792e5472616e73616374696f6e2e47657454797065615272681253797374656d2e53746f726167652e50757461006c766b52527ac46203006c766b52c3616c7566"
63 | codeAddr, _ := utils.GetContractAddress(code)
64 | txHash, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
65 | signer,
66 |
67 | true,
68 | code,
69 | "TestGetTxType",
70 | "",
71 | "",
72 | "",
73 | "")
74 |
75 | if err != nil {
76 | ctx.LogError("TestGetTxType DeploySmartContract error: %s", err)
77 | return false
78 | }
79 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
80 | if err != nil {
81 | ctx.LogError("TestGetTxType WaitForGenerateBlock error: %s", err)
82 | return false
83 | }
84 |
85 | _, err = ctx.Ont.NeoVM.InvokeNeoVMContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
86 | signer,
87 | codeAddr,
88 | []interface{}{txHash.ToArray()})
89 |
90 | if err != nil {
91 | ctx.LogError("TestGetTxType InvokeSmartContract error: %s", err)
92 | return false
93 | }
94 |
95 | txType, err := ctx.Ont.GetStorage(codeAddr.ToHexString(), []byte("txType"))
96 | if err != nil {
97 | ctx.LogError("TestGetTxType - GetStorage error: %s", err)
98 | return false
99 | }
100 |
101 | err = ctx.AssertToInt(txType, int(0xd0))
102 | if bytes.Equal(txType, []byte{byte(ctypes.Deploy)}) {
103 | ctx.LogError("TestGetTxType AssertToInt %s", err)
104 | }
105 | return true
106 | }
107 |
--------------------------------------------------------------------------------
/testcase/smartcontract/native/transfer.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | */
18 |
19 | package native
20 |
21 | import (
22 | "github.com/ontio/ontology-test/testframework"
23 | "time"
24 | )
25 |
26 | //TestOntTransfer test native transfer case
27 | func TestOntTransfer(ctx *testframework.TestFrameworkContext) bool {
28 | admin, err := ctx.GetDefaultAccount()
29 | if err != nil {
30 | ctx.LogError("Wallet.GetDefaultAccount error:%s", err)
31 | return false
32 | }
33 | user := ctx.NewAccount()
34 |
35 | adminBalanceBefore, err := ctx.Ont.Native.Ont.BalanceOf(admin.Address)
36 | if err != nil {
37 | ctx.LogError("Rpc.GetBalance error:%s", err)
38 | return false
39 | }
40 |
41 | if adminBalanceBefore == 0 {
42 | ctx.LogWarn("TestOntTransfer failed. Balance of admin is 0")
43 | return false
44 | }
45 | ctx.LogInfo("adminBalanceBefore %d", adminBalanceBefore)
46 |
47 | userBalanceBefore, err := ctx.Ont.Native.Ont.BalanceOf(user.Address)
48 | if err != nil {
49 | ctx.LogError("Rpc.GetBalance error:%s", err)
50 | return false
51 | }
52 | ctx.LogInfo("userBalanceBefore %d", userBalanceBefore)
53 |
54 | amount := uint64(100)
55 | _, err = ctx.Ont.Native.Ont.Transfer(ctx.GetGasPrice(), ctx.GetGasLimit(), admin, user.Address, amount)
56 | if err != nil {
57 | ctx.LogError("Rpc.Transfer error:%s", err)
58 | return false
59 | }
60 |
61 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
62 | if err != nil {
63 | ctx.LogError("WaitForGenerateBlock error:%s", err)
64 | return false
65 | }
66 |
67 | adminBalanceAfter, err := ctx.Ont.Native.Ont.BalanceOf(admin.Address)
68 | if err != nil {
69 | if err != nil {
70 | ctx.LogError("Rpc.GetBalance error:%s", err)
71 | return false
72 | }
73 | }
74 | ctx.LogInfo("adminBalanceAfter :%d", adminBalanceAfter)
75 |
76 | userBalanceAfter, err := ctx.Ont.Native.Ont.BalanceOf(user.Address)
77 | if err != nil {
78 | ctx.LogError("Rpc.GetBalance error:%s", err)
79 | return false
80 | }
81 | ctx.LogInfo("userBalanceAfter :%d", userBalanceAfter)
82 |
83 | //Assert admin balance
84 | adminRes := adminBalanceBefore - amount
85 | if adminRes != adminBalanceAfter {
86 | ctx.LogError("TestOntTransfer failed. Admin balance after transfer %d != %d", adminBalanceAfter, adminRes)
87 | return false
88 | }
89 |
90 | //Assert user balance
91 | userRes := userBalanceBefore + amount
92 | if userRes != userBalanceAfter {
93 | ctx.LogError("TestOntTransfer failed. User balance after transfer %d != %d", userBalanceAfter, userRes)
94 | return false
95 | }
96 | return true
97 | }
98 |
--------------------------------------------------------------------------------
/test_data/contract.c:
--------------------------------------------------------------------------------
1 | #include "ont.h"
2 |
3 | int add(int a, int b ){
4 | return a + b;
5 | }
6 |
7 | char * concat(char * a, char * b){
8 | int lena = arrayLen(a);
9 | int lenb = arrayLen(b);
10 | char * res = (char *)malloc((lena + lenb)*sizeof(char));
11 | for (int i = 0 ;i < lena ;i++){
12 | res[i] = a[i];
13 | }
14 |
15 | for (int j = 0; j < lenb ;j++){
16 | res[lena + j] = b[j];
17 | }
18 | return res;
19 | }
20 |
21 |
22 | int sumArray(int * a, int * b){
23 |
24 | int res = 0;
25 | int lena = arrayLen(a);
26 | int lenb = arrayLen(b);
27 |
28 | for (int i = 0;ia,p->b);
53 | char * result = ONT_JsonMashalResult(res,"int",1);
54 | ONT_Runtime_Notify(result);
55 | return result;
56 | }
57 |
58 | if(strcmp(method,"concat")==0){
59 | struct Params{
60 | char *a;
61 | char *b;
62 | };
63 | struct Params *p = (struct Params *)malloc(sizeof(struct Params));
64 | ONT_JsonUnmashalInput(p,sizeof(struct Params),args);
65 | char * res = concat(p->a,p->b);
66 | char * result = ONT_JsonMashalResult(res,"string",1);
67 | ONT_Runtime_Notify(result);
68 | return result;
69 | }
70 |
71 | if(strcmp(method,"sumArray")==0){
72 | struct Params{
73 | int *a;
74 | int *b;
75 | };
76 | struct Params *p = (struct Params *)malloc(sizeof(struct Params));
77 | ONT_JsonUnmashalInput(p,sizeof(struct Params),args);
78 | int res = sumArray(p->a,p->b);
79 | char * result = ONT_JsonMashalResult(res,"int",1);
80 | ONT_Runtime_Notify(result);
81 | return result;
82 | }
83 |
84 | if(strcmp(method,"addStorage")==0){
85 |
86 | struct Params{
87 | char * a;
88 | char * b;
89 | };
90 | struct Params *p = (struct Params *)malloc(sizeof(struct Params));
91 | ONT_JsonUnmashalInput(p,sizeof(struct Params),args);
92 | ONT_Storage_Put(p->a,p->b);
93 | char * result = ONT_JsonMashalResult("Done","string",1);
94 | ONT_Runtime_Notify(result);
95 | return result;
96 | }
97 |
98 | if(strcmp(method,"getStorage")==0){
99 | struct Params{
100 | char * a;
101 | };
102 | struct Params *p = (struct Params *)malloc(sizeof(struct Params));
103 | ONT_JsonUnmashalInput(p,sizeof(struct Params),args);
104 | char * value = ONT_Storage_Get(p->a);
105 | char * result = ONT_JsonMashalResult(value,"string",1);
106 | ONT_Runtime_Notify(result);
107 | return result;
108 | }
109 |
110 | if(strcmp(method,"deleteStorage")==0){
111 |
112 | struct Params{
113 | char * a;
114 | };
115 | struct Params *p = (struct Params *)malloc(sizeof(struct Params));
116 | ONT_JsonUnmashalInput(p,sizeof(struct Params),args);
117 | ONT_Storage_Delete(p->a);
118 | char * result = ONT_JsonMashalResult("Done","string",1);
119 | ONT_Runtime_Notify(result);
120 | return result;
121 | }
122 | }
--------------------------------------------------------------------------------
/wallet.dat:
--------------------------------------------------------------------------------
1 | {"name":"MyWallet","version":"1.1","scrypt":{"p":8,"n":16384,"r":8,"dkLen":64},"accounts":[{"address":"ARVVxBPGySL56CvSSWfjRVVyZYpNZ7zp48","enc-alg":"aes-256-gcm","key":"5qUUS1qXPTpXbyhnfM5fprz8HINVGhaUc70jbaxlOjBXt3y/r4wSrFp0AzyMZP9R","algorithm":"ECDSA","salt":"apFYKZo6d5NH3g2TctLYVw==","parameters":{"curve":"P-256"},"label":"","publicKey":"03c0c30f11c7fc1396e8595bf2e339d553d728ea6f21ae831e8ab704ca14fe8a56","signatureScheme":"SHA256withECDSA","isDefault":true,"lock":false},{"address":"AaCe8nVkMRABnp5YgEjYZ9E5KYCxks2uce","enc-alg":"aes-256-gcm","key":"qEUyPM7m2y8cxIMXYkDYuY/jQ1Ktc/S0LtmDqUVKs59TAIEMVZmKB3UNnEhdcsfG","algorithm":"ECDSA","salt":"BK+SZc36mTy1qYcs+41+VA==","parameters":{"curve":"P-256"},"label":"","publicKey":"02b2b9fb60a0add9ef6715ffbac8bc7e81cb47cd06c157c19e6a858859c0158231","signatureScheme":"SHA256withECDSA","isDefault":false,"lock":false},{"address":"AdnvEG9s7yzfDESvUA7E3HYkVvrbg5cx6u","enc-alg":"aes-256-gcm","key":"ZcGHjKk/nTRAAC/DdOiNUily7m6H3CH5xr+lzQtaOA6CirZXNB0p8vtcKXVEquCq","algorithm":"ECDSA","salt":"B2jOWkG480AWutXl3sKzAA==","parameters":{"curve":"P-256"},"label":"","publicKey":"02af6ecee5aa6a604bbfa6d122c8de77361e96f83e27d33678090a0d2a8d7e9805","signatureScheme":"SHA256withECDSA","isDefault":false,"lock":false},{"address":"AbSiqM5fSfJftqRBRUguga9aqbKfkJyMyz","enc-alg":"aes-256-gcm","key":"C8UQEcDGWgeXNkjQ1PafROYHJeRjJ8q+olact9Ja+5YxmB59UxK1otLw85fuIVY2","algorithm":"ECDSA","salt":"vdKRUPdkcaE1QgTh+qSosg==","parameters":{"curve":"P-256"},"label":"","publicKey":"0394592affaf9ff10fa4b81254df542e3279e767a2a964863ad21dbd86db797d5b","signatureScheme":"SHA256withECDSA","isDefault":false,"lock":false},{"address":"APkenWKjZYwnigddZUJGgjTeSLPToydGvg","enc-alg":"aes-256-gcm","key":"Ugc6Sz4O0UyqHKVzQaDftdfpOqFpXP8iT7Z4alIX7hZS2ISLIY5BQnDP0EENMWkb","algorithm":"ECDSA","salt":"VFOUHRITitHiSwl1JoVLvA==","parameters":{"curve":"P-256"},"label":"","publicKey":"034cf3fd005f0e43058709a9421c24a3a3ecd4a9f6937ceb4158df8d46909d9f0e","signatureScheme":"SHA256withECDSA","isDefault":false,"lock":false},{"address":"AaAnp7WVpjC9iVDeQcwjVPtaR1ZVXXBbRY","enc-alg":"aes-256-gcm","key":"oPOS62Fmal1e79C2h2J2Aj2jPT8L4S9+Qr1KbJp0v7zvSihMP9XEr2dPa+DwlVq9","algorithm":"ECDSA","salt":"RFNKeUEUGDY3pKl8U9j6Eg==","parameters":{"curve":"P-256"},"label":"","publicKey":"0370943cb371143ec11896c0487b4ed61f0040d9375cdd61b45f4e579a13c1df94","signatureScheme":"SHA256withECDSA","isDefault":false,"lock":false},{"address":"ARqqECifHoLzoybNpPMLcu9BM2p9wfXGyj","enc-alg":"aes-256-gcm","key":"gX4RhpBGHNGyJhSpGDmOsxtdW1XHEzzm6wSjfFPMuecub1v3nfDIEuPY6waLPQx9","algorithm":"ECDSA","salt":"G3SUzpvvunM/h6tiuMaTmg==","parameters":{"curve":"P-256"},"label":"","publicKey":"02d504a2117201972814bf5123b76b5ccc613e0ab55ff38be94d27e4ac9d47f503","signatureScheme":"SHA256withECDSA","isDefault":false,"lock":false},{"address":"AczivuWmqgaNnX7boNYk7mYg3xBGKWQdZi","enc-alg":"aes-256-gcm","key":"EJ1vGXSnHQavKHrtj9nQD+2V9YNZmb5ax8uXE55gZDhVvUcV6tzYe2f6jRj5m1wR","algorithm":"ECDSA","salt":"8MsbFv+hmWOslVnYe97mgg==","parameters":{"curve":"P-256"},"label":"","publicKey":"03c99fc45376b6034ebaa451e2266281590642ea4a2495406d6d5d10b365a08dd2","signatureScheme":"SHA256withECDSA","isDefault":false,"lock":false},{"address":"AQhWQ4Jh5hNJC251zKcsBN1HnSbE6TM5pp","enc-alg":"aes-256-gcm","key":"uPdcIHGQWcGv50EJrEpn/cgqROX/ujIbt6JkQP+GdVlrE2wvw3DrxZfVraS70tGo","algorithm":"ECDSA","salt":"NT6zsMXXhqLd2NSCOv24Bg==","parameters":{"curve":"P-256"},"label":"","publicKey":"0349cc6b4bab471fa335c33a41d7c14f610c0532c2b49f51bbbbd8c57d6280831e","signatureScheme":"SHA256withECDSA","isDefault":false,"lock":false}]}
--------------------------------------------------------------------------------
/testcase/smartcontract/api/runtime/checkwitness.go:
--------------------------------------------------------------------------------
1 | package runtime
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-crypto/keypair"
7 | "github.com/ontio/ontology-crypto/signature"
8 | "github.com/ontio/ontology-go-sdk/utils"
9 | "github.com/ontio/ontology-test/testframework"
10 | )
11 |
12 | /*
13 | using Neo.SmartContract.Framework;
14 | using Neo.SmartContract.Framework.Services.Neo;
15 | using Neo.SmartContract.Framework.Services.System;
16 | using System.Numerics;
17 |
18 | public class A : SmartContract
19 | {
20 | public static void Main(byte[] input)
21 | {
22 |
23 |
24 | bool b = Runtime.CheckWitness(input);
25 | if (b) {
26 | Storage.Put(Storage.CurrentContext, "result", "true");
27 | }
28 | Storage.Put(Storage.CurrentContext, "result", "true");
29 | }
30 | }
31 |
32 | code = 53c56b6c766b00527ac4616c766b00c361681b53797374656d2e52756e74696d652e436865636b5769746e6573736c766b51527ac46c766b51c36c766b52527ac46c766b52c36445006161681953797374656d2e53746f726167652e476574436f6e7465787406726573756c740474727565615272681253797374656d2e53746f726167652e507574616161681953797374656d2e53746f726167652e476574436f6e7465787406726573756c740474727565615272681253797374656d2e53746f726167652e50757461616c7566
33 | */
34 |
35 | func TestCheckWitness(ctx *testframework.TestFrameworkContext) bool {
36 | code := "53c56b6c766b00527ac4616c766b00c361681b53797374656d2e52756e74696d652e436865636b5769746e6573736c766b51527ac46c766b51c36c766b52527ac46c766b52c36445006161681953797374656d2e53746f726167652e476574436f6e7465787406726573756c740474727565615272681253797374656d2e53746f726167652e507574616161681953797374656d2e53746f726167652e476574436f6e7465787406726573756c740474727565615272681253797374656d2e53746f726167652e50757461616c7566"
37 | codeAddress, _ := utils.GetContractAddress(code)
38 | signer, err := ctx.GetDefaultAccount()
39 |
40 | if err != nil {
41 | ctx.LogError("TestCheckWitness - GetDefaultAccount error: %s", err)
42 | return false
43 | }
44 |
45 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
46 | signer,
47 | true,
48 | code,
49 | "",
50 | "",
51 | "",
52 | "",
53 | "")
54 |
55 | if err != nil {
56 | ctx.LogError("TestCheckWitness DeploySmartContract error:%s", err)
57 | return false
58 | }
59 |
60 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
61 |
62 | if err != nil {
63 | ctx.LogError("TestCheckWitness WaitForGenerateBlock error:%s", err)
64 | return false
65 | }
66 |
67 | checker, err := ctx.Wallet.NewAccount( keypair.PK_ECDSA, keypair.P256, signature.SHA256withECDSA, []byte("test"))
68 |
69 | if err != nil {
70 | ctx.LogError("TestCheckWitness generate account error:%s", err)
71 | return false
72 | }
73 |
74 | _, err = ctx.Ont.NeoVM.InvokeNeoVMContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
75 | signer,
76 | codeAddress,
77 | []interface{}{checker.Address[:]})
78 | if err != nil {
79 | ctx.LogError("TestDomainSmartContract InvokeNeoVMSmartContract error: %s", err)
80 | }
81 |
82 | ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
83 |
84 | res, err := ctx.Ont.GetStorage(codeAddress.ToHexString(), []byte("result"))
85 | if err != nil {
86 | ctx.LogError("TestDomainSmartContract GetStorageItem key:hello error: %s", err)
87 | return false
88 | }
89 |
90 | err = ctx.AssertToString(string(res), "true")
91 | if err != nil {
92 | ctx.LogError("TestDomainSmartContract AssertToString error: %s", err)
93 | return false
94 | }
95 | return true
96 | }
97 |
--------------------------------------------------------------------------------
/test_data/assetraw.c:
--------------------------------------------------------------------------------
1 | #include "ont.h"
2 |
3 | char * init(){
4 | char * totalsupply = GetStorage("TCOIN_TOTAL_SUPPLY");
5 | if (arrayLen(totalsupply) > 0){
6 | return JsonMashalResult("this contract already initialized!","string");
7 | }else{
8 | PutStorage("TCOIN_TOTAL_SUPPLY","1000000000");
9 | PutStorage("00000001","1000000000");
10 | }
11 | return JsonMashalResult("init succeed","string");
12 | }
13 |
14 | char * getTotalSupply(){
15 | char * store = GetStorage("TCOIN_TOTAL_SUPPLY");
16 | if (arrayLen(store) > 0){
17 | long long totalsupply = Atoi64(store);
18 | return JsonMashalResult(totalsupply,"int64");
19 | }else{
20 | return JsonMashalResult("total supply has not been init!","string");
21 | }
22 | }
23 |
24 | char * balanceOf(char * address){
25 | long long balance =Atoi64(GetStorage(address));
26 | return JsonMashalResult(balance,"int64");
27 | }
28 |
29 | int transfer(char * from ,char * to, int amount){
30 | if(amount <= 0){
31 | return 0;
32 | }
33 | //checkwitness(from)
34 | if(strcmp(from,to) == 0){
35 |
36 | return 0;
37 | }
38 | char * fromValuestr = GetStorage(from);
39 | long long fromValue = Atoi64(fromValuestr);
40 | if (fromValue < amount){
41 | return 0;
42 | }
43 | if (fromValue == amount){
44 | DeleteStorage(from);
45 | }else{
46 | long long tovalue = Atoi64(GetStorage(to));
47 | PutStorage(from,Itoa64(fromValue -amount));
48 | PutStorage(to,Itoa64(tovalue + amount));
49 | }
50 | return 1;
51 | }
52 |
53 | char * concat(char * a, char * b){
54 | int lena = arrayLen(a);
55 | int lenb = arrayLen(b);
56 | char * res = (char *)malloc((lena + lenb)*sizeof(char));
57 | for (int i = 0 ;i < lena ;i++){
58 | res[i] = a[i];
59 | }
60 |
61 | for (int j = 0; j < lenb ;j++){
62 | res[lenb + j] = b[j];
63 | }
64 | return res;
65 | }
66 |
67 | /*
68 | *this is the common standard interface of ontology wasm contract
69 | */
70 | char * invoke(char * method,char * args){
71 |
72 | if(strcmp(method,"init") == 0){
73 | char * result = init();
74 | RuntimeNotify(result);
75 | return result;
76 | }
77 |
78 | if(strcmp(method,"totalSupply") == 0){
79 |
80 | char *result = getTotalSupply();
81 | RuntimeNotify(result);
82 | return result;
83 | }
84 |
85 | if(strcmp(method,"balanceOf") == 0){
86 | char * address = ReadStringParam(args);
87 |
88 | char *result = balanceOf(address);
89 | RuntimeNotify(result);
90 | return result;
91 | }
92 |
93 | if(strcmp(method,"transfer") == 0){
94 |
95 | char * from = ReadStringParam(args);
96 | char * to = ReadStringParam(args);
97 | long long amount = ReadInt64Param(args);
98 |
99 | char * result;
100 | if(transfer(from,to,amount) > 0){
101 |
102 | result = JsonMashalResult(Itoa64(amount),"string");
103 | }else{
104 | // result = JsonMashalResult(concat(concat(concat(concat(concat("transfer :",p.from),"to:"),p.to),I64toa(p.amount)),"failed!"),"string");
105 | result = JsonMashalResult(Itoa64(amount),"string");
106 | }
107 | RuntimeNotify(result);
108 | return result;
109 | }
110 | }
--------------------------------------------------------------------------------
/testcase/vm/neovm/datatype/returntype.go:
--------------------------------------------------------------------------------
1 | package datatype
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | "github.com/ontio/ontology/common"
9 | )
10 |
11 | func TestReturnType(ctx *testframework.TestFrameworkContext) bool {
12 | code := "55c56b6c766b00527ac46c766b51527ac46c766b52527ac46153c56c766b53527ac46c766b53c3006c766b00c3c46c766b53c3516c766b51c3c46c766b53c3526c766b52c3c46c766b53c36c766b54527ac46203006c766b54c3616c7566"
13 | codeAddress, _ := utils.GetContractAddress(code)
14 | signer, err := ctx.GetDefaultAccount()
15 | if err != nil {
16 | ctx.LogError("TestReturnType GetDefaultAccount error:%s", err)
17 | return false
18 | }
19 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
20 | signer,
21 | false,
22 | code,
23 | "TestReturnType",
24 | "1.0",
25 | "",
26 | "",
27 | "",
28 | )
29 | if err != nil {
30 | ctx.LogError("TestArray DeploySmartContract error:%s", err)
31 | return false
32 | }
33 | //等待出块
34 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
35 | if err != nil {
36 | ctx.LogError("TestReturnType WaitForGenerateBlock error:%s", err)
37 | return false
38 | }
39 | if !testReturnType(ctx, codeAddress, []int{100343, 2433554}, []byte("Hello world")) {
40 | return false
41 | }
42 | return true
43 | }
44 |
45 | func testReturnType(ctx *testframework.TestFrameworkContext, code common.Address, args []int, arg3 []byte) bool {
46 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
47 | code,
48 | []interface{}{args[0], args[1], arg3},
49 | )
50 | if err != nil {
51 | ctx.LogError("TestReturnType InvokeSmartContract error:%s", err)
52 | return false
53 | }
54 |
55 | rt, err := res.Result.ToArray()
56 | if err != nil {
57 | ctx.LogError("TestReturnType Result.ToArray error:%s", err)
58 | return false
59 | }
60 | a1, err := rt[0].ToInteger()
61 | if err != nil {
62 | ctx.LogError("TestReturnType Result.ToByteArray error:%s", err)
63 | return false
64 | }
65 | err = ctx.AssertToInt(a1, args[0])
66 | if err != nil {
67 | ctx.LogError("TestReturnType AssertToInt error:%s", err)
68 | return false
69 | }
70 | a2, err := rt[1].ToInteger()
71 | if err != nil {
72 | ctx.LogError("TestReturnType Result.ToByteArray error:%s", err)
73 | return false
74 | }
75 | err = ctx.AssertToInt(a2, args[1])
76 | if err != nil {
77 | ctx.LogError("TestReturnType AssertToInt error:%s", err)
78 | return false
79 | }
80 | a3, err := rt[2].ToByteArray()
81 | if err != nil {
82 | ctx.LogError("TestReturnType ToByteArray error:%s", err)
83 | return false
84 | }
85 | err = ctx.AssertToByteArray(a3, arg3)
86 | if err != nil {
87 | ctx.LogError("AssertToByteArray error:%s", err)
88 | return false
89 | }
90 |
91 | return true
92 | }
93 |
94 | /*
95 | using Neo.SmartContract.Framework;
96 | using Neo.SmartContract.Framework.Services.Neo;
97 | using Neo.SmartContract.Framework.Services.System;
98 | using System;
99 | using System.Numerics;
100 | namespace ONT_DEx
101 | {
102 | public class ONT_P2P : SmartContract
103 | {
104 | public static object[] Main(int arg1, int arg2, byte[] arg3)
105 | {
106 | object[] ret = new object[3];
107 | ret[0] = arg1;
108 | ret[1] = arg2;
109 | ret[2] = arg3;
110 | return ret;
111 | }
112 | }
113 | }
114 | */
115 |
--------------------------------------------------------------------------------
/testcase/vm/neovm/call/call.go:
--------------------------------------------------------------------------------
1 | package call
2 |
3 | import (
4 | "time"
5 | "github.com/ontio/ontology-go-sdk/utils"
6 | "github.com/ontio/ontology-test/testframework"
7 | )
8 |
9 | func TestCallContractStatic(ctx *testframework.TestFrameworkContext) bool {
10 | signer, err := ctx.GetDefaultAccount()
11 | if err != nil {
12 | ctx.LogError("TestIfElse GetDefaultAccount error:%s", err)
13 | return false
14 | }
15 |
16 | codeA := "52c56b6c766b00527ac4616c766b00c36c766b51527ac46203006c766b51c3616c7566"
17 | codeAddressA, _ := utils.GetContractAddress(codeA)
18 |
19 | ctx.LogInfo("CodeA Address:%s", codeAddressA.ToHexString())
20 |
21 | txhash, err := ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
22 | signer,
23 | false,
24 | codeA,
25 | "TestCallContractStaticA",
26 | "1.0",
27 | "",
28 | "",
29 | "",
30 | )
31 | if err != nil {
32 | ctx.LogError("TestCallContractStatic DeploySmartContract error:%s", err)
33 | return false
34 | }
35 |
36 | ctx.LogInfo("TestCallContractStatic Deploy contract a TxHash:%s", txhash.ToHexString())
37 | //等待出块
38 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 2)
39 | if err != nil {
40 | ctx.LogError("TestCallContractStatic WaitForGenerateBlock error:%s", err)
41 | return false
42 | }
43 |
44 | codeB := "52c56b6c766b00527ac4616c766b00c361673d711163a4da8a8e37fd469a37e6cc04d37df3696c766b51527ac46203006c766b51c3616c7566"
45 | codeAddressB, _ := utils.GetContractAddress(codeB)
46 | txhash, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
47 | signer,
48 | false,
49 | codeB,
50 | "TestCallContractStaticB",
51 | "1.0",
52 | "",
53 | "",
54 | "",
55 | )
56 | if err != nil {
57 | ctx.LogError("TestCallContractStatic DeploySmartContract error:%s", err)
58 | return false
59 | }
60 | ctx.LogInfo("TestCallContractStatic Deploy contract b TxHash:%s", txhash.ToHexString())
61 |
62 | //等待出块
63 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
64 | if err != nil {
65 | ctx.LogError("TestCallContractStatic WaitForGenerateBlock error:%s", err)
66 | return false
67 | }
68 |
69 | input := 12
70 | res, err := ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(
71 | codeAddressB,
72 | []interface{}{input},
73 | )
74 | if err != nil {
75 | ctx.LogError("TestCallContractStatic error:%s", err)
76 | return false
77 | }
78 | resValue, err := res.Result.ToInteger()
79 | if err != nil {
80 | ctx.LogError("TestCallContractStatic Result.ToInteger error:%s", err)
81 | return false
82 | }
83 | err = ctx.AssertToInt(resValue, input)
84 | if err != nil {
85 | ctx.LogError("TestCallContractStatic res AssertToInt error:%s", err)
86 | return false
87 | }
88 | return true
89 | }
90 |
91 | /*
92 | SmartContractA
93 |
94 | using Neo.SmartContract.Framework;
95 | using Neo.SmartContract.Framework.Services.Neo;
96 | using System.Numerics;
97 |
98 | public class A : SmartContract
99 | {
100 | public static int Main(int arg)
101 | {
102 | return arg;
103 | }
104 | }
105 |
106 | Code:52c56b6c766b00527ac4616c766b00c36c766b51527ac46203006c766b51c3616c7566
107 |
108 | SmartContractB
109 |
110 | using Neo.SmartContract.Framework;
111 | using Neo.SmartContract.Framework.Services.Neo;
112 | using System.Numerics;
113 |
114 | public class B : SmartContract
115 | {
116 | [Appcall("69f37dd304cce6379a46fd378e8adaa46311713d")]
117 | public static extern int OtherContract(int input);
118 | public static int Main(int input)
119 | {
120 | return OtherContract(input);
121 | }
122 | }
123 |
124 | Code:52c56b6c766b00527ac4616c766b00c361673d711163a4da8a8e37fd469a37e6cc04d37df3696c766b51527ac46203006c766b51c3616c7566
125 | */
126 |
--------------------------------------------------------------------------------
/testcase/smartcontract/native/withdrawong.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | */
18 |
19 | package native
20 |
21 | import (
22 | "github.com/ontio/ontology-test/testframework"
23 | "time"
24 | )
25 |
26 | func TestWithdrawONG(ctx *testframework.TestFrameworkContext) bool {
27 | defAccount, _ := ctx.GetDefaultAccount()
28 | newAccount := ctx.NewAccount()
29 |
30 | balanceBefore, err := ctx.Ont.Native.Ont.BalanceOf(defAccount.Address)
31 | if err != nil {
32 | ctx.LogError("TestWithdrawONG GetBalance error:%s", err)
33 | return false
34 | }
35 | if balanceBefore == 0 {
36 | ctx.LogError("TestWithdrawONG ont balance = 0")
37 | return false
38 | }
39 |
40 | ctx.LogInfo("TestWithdrawONG Balance ONT:%d", balanceBefore)
41 |
42 | amount := uint64(100000)
43 | if balanceBefore < amount {
44 | amount = balanceBefore
45 | }
46 | _, err = ctx.Ont.Native.Ont.Transfer(ctx.GetGasPrice(), ctx.GetGasLimit(), defAccount, newAccount.Address, 10000)
47 | if err != nil {
48 | ctx.LogError("TestWithdrawONG Transfer ont error:%s", err)
49 | return false
50 | }
51 | ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
52 |
53 |
54 | ongBalanceBefor, err := ctx.Ont.Native.Ong.BalanceOf(defAccount.Address)
55 | if err != nil {
56 | ctx.LogError("TestWithdrawONG BalanceOf ong error:%s", err)
57 | return false
58 | }
59 | unBoundONG, err := ctx.Ont.Native.Ong.UnboundONG(defAccount.Address)
60 | if err != nil {
61 | ctx.LogError("TestWithdrawONG UnboundONG error:%s", err)
62 | return false
63 | }
64 | ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
65 |
66 | ctx.LogInfo("TestWithdrawONG UnboundONG:%d", unBoundONG)
67 | if unBoundONG == 0 {
68 | ctx.LogError("TestWithdrawONG UnboundONG = 0")
69 | return false
70 | }
71 |
72 | withdrawAmount := unBoundONG - 1
73 | _, err = ctx.Ont.Native.Ong.WithdrawONG(ctx.GetGasPrice(), ctx.GetGasLimit(), defAccount, withdrawAmount)
74 | if err != nil {
75 | ctx.LogError("TestWithdrawONG WithdrawONG error:%s", err)
76 | return false
77 | }
78 |
79 | ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
80 |
81 | unBoundONGAfter, err := ctx.Ont.Native.Ong.UnboundONG(defAccount.Address)
82 | if err != nil {
83 | ctx.LogError("TestWithdrawONG UnboundONG error:%s", err)
84 | return false
85 | }
86 |
87 | if unBoundONGAfter != unBoundONG-withdrawAmount {
88 | ctx.LogError("TestWithdrawONG unBoundONGAfter:%d != %d", unBoundONGAfter, unBoundONG-withdrawAmount)
89 | return false
90 | }
91 |
92 | ongBalanceAfter, err := ctx.Ont.Native.Ong.BalanceOf(defAccount.Address)
93 | if err != nil {
94 | ctx.LogError("TestWithdrawONG GetBalance error:%s", err)
95 | return false
96 | }
97 |
98 | ctx.LogInfo("TestWithdrawONG Balance after ONG:%d", ongBalanceAfter)
99 |
100 | if ongBalanceAfter != ongBalanceBefor+withdrawAmount {
101 | ctx.LogError("TestWithdrawONG ong balance %d != %d", ongBalanceAfter, ongBalanceBefor+withdrawAmount)
102 | return false
103 | }
104 |
105 | return true
106 | }
107 |
--------------------------------------------------------------------------------
/testcase/testcase.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | */
18 | package testcase
19 |
20 | import (
21 | "github.com/ontio/ontology-test/testcase/http"
22 | "github.com/ontio/ontology-test/testcase/smartcontract"
23 | "github.com/ontio/ontology-test/testcase/vm"
24 | "github.com/ontio/ontology-test/testframework"
25 | "math"
26 | "time"
27 | )
28 |
29 | //TestCase list
30 | func init() {
31 | testframework.TFramework.SetBeforeCallback(BeforeTestCase)
32 | http.TestHttp()
33 | vm.TestVM()
34 | smartcontract.TestSmartContract()
35 | }
36 |
37 | func BeforeTestCase(ctx *testframework.TestFrameworkContext) {
38 | defAccount, err := ctx.GetDefaultAccount()
39 | if err != nil {
40 | ctx.LogError("GetDefaultAccount error:%s", err)
41 | ctx.FailNow()
42 | return
43 | }
44 | newAccount := ctx.NewAccount()
45 | ontBalance, err := ctx.Ont.Native.Ont.BalanceOf(defAccount.Address)
46 | if err != nil {
47 | ctx.LogError("GetBalance error:%s", err)
48 | ctx.FailNow()
49 | return
50 | }
51 | amount := uint64(10000)
52 | ongBalance, err := ctx.Ont.Native.Ong.BalanceOf(defAccount.Address)
53 | if err != nil {
54 | ctx.LogError("GetBalance error:%s", err)
55 | ctx.FailNow()
56 | return
57 | }
58 | minONG := uint64(100000 * math.Pow10(9))
59 | if ongBalance > minONG {
60 | ctx.LogInfo("Default account balance ont:%d ong:%d", ontBalance, ongBalance)
61 | return
62 | }
63 | if ontBalance == 0 {
64 | ctx.LogWarn("Default Account balance = 0 ")
65 | return
66 | }
67 | if ontBalance < amount {
68 | amount = ontBalance
69 | }
70 | _, err = ctx.Ont.Native.Ont.Transfer(ctx.GetGasPrice(), ctx.GetGasLimit(), defAccount, newAccount.Address, amount)
71 | if err != nil {
72 | ctx.LogError("Transfer error:%s", err)
73 | ctx.FailNow()
74 | return
75 | }
76 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
77 | if err != nil {
78 | ctx.LogError("WaitForGenerateBlock error:%s", err)
79 | ctx.FailNow()
80 | return
81 | }
82 | unboundONG, err := ctx.Ont.Native.Ong.UnboundONG(defAccount.Address)
83 | if err != nil {
84 | ctx.LogError("UnboundONG error:%s", err)
85 | ctx.FailNow()
86 | return
87 | }
88 | _, err = ctx.Ont.Native.Ong.WithdrawONG(ctx.GetGasPrice(), ctx.GetGasLimit(), defAccount, unboundONG)
89 | if err != nil {
90 | ctx.LogError("WithdrawONG error:%s", err)
91 | ctx.FailNow()
92 | return
93 | }
94 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
95 | if err != nil {
96 | ctx.LogError("WaitForGenerateBlock error:%s", err)
97 | ctx.FailNow()
98 | return
99 | }
100 | ontBalanceAft, err := ctx.Ont.Native.Ont.BalanceOf(defAccount.Address)
101 | if err != nil {
102 | ctx.LogInfo("GetBalance error:%s", err)
103 | ctx.FailNow()
104 | return
105 | }
106 | ongBalanceAft, err := ctx.Ont.Native.Ong.BalanceOf(defAccount.Address)
107 | if err != nil {
108 | ctx.LogInfo("GetBalance error:%s", err)
109 | ctx.FailNow()
110 | return
111 | }
112 | ctx.LogInfo("Default account balance ont:%d ong:%d", ontBalanceAft, ongBalanceAft)
113 | }
114 |
--------------------------------------------------------------------------------
/testcase/smartcontract/api/transaction/gethash.go:
--------------------------------------------------------------------------------
1 | package transaction
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | )
9 |
10 | /*
11 |
12 | contract A:
13 |
14 | using Neo.SmartContract.Framework;
15 | using Neo.SmartContract.Framework.Services.Neo;
16 |
17 | class A : SmartContract
18 | {
19 | public static string Main()
20 | {
21 | return "Hello World!";
22 | }
23 | }
24 |
25 | Code:51c56b610c48656c6c6f20576f726c64216c766b00527ac46203006c766b00c3616c7566
26 |
27 | contract B:
28 |
29 | using Neo.SmartContract.Framework;
30 | using Neo.SmartContract.Framework.Services.Neo;
31 | using Neo.SmartContract.Framework.Services.System;
32 | using System.Numerics;
33 |
34 | public class A : SmartContract
35 | {
36 | public static void Main(byte[] txHash)
37 | {
38 | Transaction tx = Blockchain.GetTransaction(txHash);
39 | Storage.Put(Storage.CurrentContext, "txHash", tx.Hash);
40 | }
41 | }
42 |
43 | Code:52c56b6c766b00527ac4616c766b00c361682053797374656d2e426c6f636b636861696e2e4765745472616e73616374696f6e6c766b51527ac461681953797374656d2e53746f726167652e476574436f6e74657874067478486173686c766b51c361681a53797374656d2e5472616e73616374696f6e2e47657448617368615272681253797374656d2e53746f726167652e50757461616c7566
44 | */
45 |
46 | func TestGetTxHash(ctx *testframework.TestFrameworkContext) bool {
47 | code := "51c56b610c48656c6c6f20576f726c64216c766b00527ac46203006c766b00c3616c7566"
48 | signer, err := ctx.GetDefaultAccount()
49 |
50 | if err != nil {
51 | ctx.LogError("TestGetTxHash - GetDefaultAccount error: %s", err)
52 | return false
53 | }
54 |
55 | txHash, err := ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
56 | signer,
57 | true,
58 | code,
59 | "TestGetTxHash",
60 | "",
61 | "",
62 | "",
63 | "")
64 |
65 | if err != nil {
66 | ctx.LogError("TestGetTxHash DeploySmartContract error: %s", err)
67 | return false
68 | }
69 |
70 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
71 | if err != nil {
72 | ctx.LogError("TestGetTxHash WaitForGenerateBlock error: %s", err)
73 | return false
74 | }
75 |
76 | code = "52c56b6c766b00527ac4616c766b00c361682053797374656d2e426c6f636b636861696e2e4765745472616e73616374696f6e6c766b51527ac461681953797374656d2e53746f726167652e476574436f6e74657874067478486173686c766b51c361681a53797374656d2e5472616e73616374696f6e2e47657448617368615272681253797374656d2e53746f726167652e50757461616c7566"
77 | codeAddr, _ := utils.GetContractAddress(code)
78 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
79 | signer,
80 |
81 | true,
82 | code,
83 | "TestGetTxHash",
84 | "",
85 | "",
86 | "",
87 | "")
88 |
89 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
90 | if err != nil {
91 | ctx.LogError("TestGetTxHash WaitForGenerateBlock error: %s", err)
92 | return false
93 | }
94 |
95 | _, err = ctx.Ont.NeoVM.InvokeNeoVMContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
96 | signer,
97 | codeAddr,
98 | []interface{}{txHash.ToArray()})
99 |
100 | if err != nil {
101 | ctx.LogError("TestGetTxHash InvokeSmartContract error: %s", err)
102 | return false
103 | }
104 |
105 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
106 | if err != nil {
107 | ctx.LogError("TestGetTxHash WaitForGenerateBlock error: %s", err)
108 | return false
109 | }
110 |
111 | hash, err := ctx.Ont.GetStorage(codeAddr.ToHexString(), []byte("txHash"))
112 | if err != nil {
113 | ctx.LogError("TestGetTxHash - GetStorage error: %s", err)
114 | return false
115 | }
116 |
117 | err = ctx.AssertToByteArray(hash, txHash.ToArray())
118 | if err != nil {
119 | ctx.LogError("TestGetTxHash test failed %s", err)
120 | return false
121 | }
122 | return true
123 | }
124 |
--------------------------------------------------------------------------------
/testcase/smartcontract/api/contract/getcontract.go:
--------------------------------------------------------------------------------
1 | package contract
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-test/testframework"
7 |
8 | "encoding/hex"
9 |
10 | "github.com/ontio/ontology-go-sdk/utils"
11 | )
12 |
13 | /**
14 |
15 | using Neo.SmartContract.Framework;
16 | using Neo.SmartContract.Framework.Services.Neo;
17 |
18 | class A : SmartContract
19 | {
20 | public static string Main()
21 | {
22 | return "Hello World!";
23 | }
24 | }
25 |
26 | code = 51c56b610c48656c6c6f20576f726c64216c766b00527ac46203006c766b00c3616c7566
27 | ------------------------------------------------------------
28 |
29 | using Neo.SmartContract.Framework;
30 | using Neo.SmartContract.Framework.Services.Neo;
31 | using Neo.SmartContract.Framework.Services.System;
32 | using System;
33 | using System.ComponentModel;
34 | using System.Numerics;
35 |
36 | class OnTest : SmartContract
37 | {
38 | public static byte[] Main(byte[] codeHash)
39 | {
40 | byte[] script = Blockchain.GetContract(codeHash).Script;
41 | Storage.Put(Storage.CurrentContext, "script", script);
42 | return null;
43 | }
44 | }
45 |
46 | code = 53c56b6c766b00527ac4616c766b00c361681d53797374656d2e426c6f636b636861696e2e476574436f6e747261637461681b4f6e746f6c6f67792e436f6e74726163742e4765745363726970746c766b51527ac461681953797374656d2e53746f726167652e476574436f6e74657874067363726970746c766b51c3615272681253797374656d2e53746f726167652e50757461006c766b52527ac46203006c766b52c3616c7566
47 | */
48 |
49 | func TestGetContract(ctx *testframework.TestFrameworkContext) bool {
50 | signer, err := ctx.GetDefaultAccount()
51 | if err != nil {
52 | ctx.LogError("TestGetContract - GetDefaultAccount error: %s", err)
53 | return false
54 | }
55 |
56 | codeA := "53c56b6c766b00527ac4616c766b00c361681d53797374656d2e426c6f636b636861696e2e476574436f6e747261637461681b4f6e746f6c6f67792e436f6e74726163742e4765745363726970746c766b51527ac461681953797374656d2e53746f726167652e476574436f6e74657874067363726970746c766b51c3615272681253797374656d2e53746f726167652e50757461006c766b52527ac46203006c766b52c3616c7566"
57 | codeAAddr, _ := utils.GetContractAddress(codeA)
58 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
59 | signer,
60 | true,
61 | codeA,
62 | "TestGetContract",
63 | "",
64 | "",
65 | "",
66 | "")
67 |
68 | if err != nil {
69 | ctx.LogError("TestGetContract DeploySmartContract error: %s", err)
70 | return false
71 | }
72 |
73 | codeB := "51c56b610c48656c6c6f20576f726c64216c766b00527ac46203006c766b00c3616c7566"
74 | codeBAddr, _ := utils.GetContractAddress(codeB)
75 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
76 | signer,
77 | true,
78 | codeB,
79 | "TestGetContract",
80 | "",
81 | "",
82 | "",
83 | "")
84 |
85 | if err != nil {
86 | ctx.LogError("TestGetContract DeploySmartContract error: %s", err)
87 | return false
88 | }
89 |
90 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 2)
91 | if err != nil {
92 | ctx.LogError("TestGetContract - WaitForGenerateBlock error: %s", err)
93 | return false
94 | }
95 |
96 | _, err = ctx.Ont.NeoVM.InvokeNeoVMContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
97 | signer,
98 | codeAAddr,
99 | []interface{}{codeBAddr[:]})
100 |
101 | if err != nil {
102 | ctx.LogError("TestGetContract InvokeSmartContract error: %s", err)
103 | return false
104 | }
105 |
106 | script, err := ctx.Ont.GetStorage(codeAAddr.ToHexString(), []byte("script"))
107 | if err != nil {
108 | ctx.LogError("TestGetContract - GetStorage error: %s", err)
109 | return false
110 | }
111 |
112 | codeBHash, _ := hex.DecodeString(codeB)
113 |
114 | err = ctx.AssertToByteArray(script, codeBHash)
115 | if err != nil {
116 | ctx.LogError("TestGetContract - AssertToByteArray error: %s", err)
117 | return false
118 | }
119 |
120 | return true
121 | }
122 |
--------------------------------------------------------------------------------
/testcase/smartcontract/api/storage/storage.go:
--------------------------------------------------------------------------------
1 | package storage
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | )
9 |
10 | /*
11 |
12 | using Neo.SmartContract.Framework;
13 | using Neo.SmartContract.Framework.Services.Neo;
14 |
15 | class A : SmartContract
16 | {
17 | public static void Main()
18 | {
19 | Storage.Put(Storage.CurrentContext, "k1", "v1");
20 | byte[] temp = Storage.Get(Storage.CurrentContext, "k1");
21 | Storage.Put(Storage.CurrentContext, "k2", temp);
22 | Storage.Put(Storage.CurrentContext, "k3", "v3");
23 | Storage.Delete(Storage.CurrentContext, "k3");
24 | }
25 | }
26 | */
27 |
28 | func TestStorage(ctx *testframework.TestFrameworkContext) bool {
29 | code := "51c56b6161681953797374656d2e53746f726167652e476574436f6e74657874026b31027631615272681253797374656d2e53746f726167652e5075746161681953797374656d2e53746f726167652e476574436f6e74657874026b31617c681253797374656d2e53746f726167652e4765746c766b00527ac461681953797374656d2e53746f726167652e476574436f6e74657874026b326c766b00c3615272681253797374656d2e53746f726167652e5075746161681953797374656d2e53746f726167652e476574436f6e74657874026b33027633615272681253797374656d2e53746f726167652e5075746161681953797374656d2e53746f726167652e476574436f6e74657874026b33617c681553797374656d2e53746f726167652e44656c65746561616c7566"
30 | codeAddr, err := utils.GetContractAddress(code)
31 | if err != nil {
32 | ctx.LogError("TestStorage GetContractAddress error:%s", err)
33 | return false
34 | }
35 | ctx.LogInfo("TestStorage address:%s", codeAddr.ToHexString())
36 | signer, err := ctx.GetDefaultAccount()
37 | if err != nil {
38 | ctx.LogError("TestStorage - GetDefaultAccount error: %s", err)
39 | return false
40 | }
41 |
42 | tx, err := ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
43 | signer,
44 | true,
45 | code,
46 | "TestStorage",
47 | "",
48 | "",
49 | "",
50 | "")
51 |
52 | if err != nil {
53 | ctx.LogError("TestStorage DeploySmartContract error: %s", err)
54 | return false
55 | }
56 |
57 | ctx.LogInfo("TestStorage deploy tx:%s", tx.ToHexString())
58 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
59 | if err != nil {
60 | ctx.LogError("TestStorage WaitForGenerateBlock error: %s", err)
61 | return false
62 | }
63 |
64 | invoTx, err := ctx.Ont.NeoVM.InvokeNeoVMContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
65 | signer,
66 | codeAddr,
67 | []interface{}{})
68 |
69 | if err != nil {
70 | ctx.LogError("TestStorage InvokeSmartContract error: %s", err)
71 | return false
72 | }
73 |
74 | ctx.LogInfo("TestStorage invoke tx:%s\n", invoTx.ToHexString())
75 |
76 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
77 |
78 | if err != nil {
79 | ctx.LogError("TestStorage WaitForGenerateBlock error: %s", err)
80 | return false
81 | }
82 |
83 | v1, err := ctx.Ont.GetStorage(codeAddr.ToHexString(), []byte("k1"))
84 | if err != nil {
85 | ctx.LogError("TestStorage GetStorage k1 error:%s", err)
86 | return false
87 | }
88 | v2, err := ctx.Ont.GetStorage(codeAddr.ToHexString(), []byte("k2"))
89 | if err != nil {
90 | ctx.LogError("TestStorage GetStorage k2 error:%s", err)
91 | return false
92 | }
93 | v3, err := ctx.Ont.GetStorage(codeAddr.ToHexString(), []byte("k3"))
94 | if err != nil {
95 | ctx.LogError("TestStorage GetStorage k3 error:%s", err)
96 | return false
97 | }
98 |
99 | err = ctx.AssertToByteArray(v1, []byte("v1"))
100 | if err != nil {
101 | ctx.LogError("TestStorage - AssertToByteArray error: %s", err)
102 | return false
103 | }
104 |
105 | err = ctx.AssertToByteArray(v2, []byte("v1"))
106 | if err != nil {
107 | ctx.LogError("TestStorage - AssertToByteArray error: %s", err)
108 | return false
109 | }
110 |
111 | err = ctx.AssertToByteArray(v3, []byte(""))
112 | if err != nil {
113 | ctx.LogError("TestStorage - AssertToByteArray error: %s", err)
114 | return false
115 | }
116 |
117 | return true
118 | }
119 |
--------------------------------------------------------------------------------
/test_data/asset.c:
--------------------------------------------------------------------------------
1 | #include "ont.h"
2 |
3 | char * init(){
4 | char * totalsupply = GetStorage("TCOIN_TOTAL_SUPPLY");
5 | if (arrayLen(totalsupply) > 0){
6 | return JsonMashalResult("this contract already initialized!","string");
7 | }else{
8 | PutStorage("TCOIN_TOTAL_SUPPLY","1000000000");
9 | PutStorage("00000001","1000000000");
10 | }
11 | return JsonMashalResult("init succeed","string");
12 | }
13 |
14 | char * getTotalSupply(){
15 | char * store = GetStorage("TCOIN_TOTAL_SUPPLY");
16 | if (arrayLen(store) > 0){
17 | int totalsupply = Atoi(store);
18 | return JsonMashalResult(totalsupply,"int");
19 | }else{
20 | return JsonMashalResult("total supply has not been init!","string");
21 | }
22 | }
23 |
24 | char * balanceOf(char * address){
25 | int balance =Atoi(GetStorage(address));
26 | return JsonMashalResult(balance,"int");
27 | }
28 |
29 | int transfer(char * from ,char * to, int amount){
30 | ContractLogError("------1-------");
31 | if(amount <= 0){
32 | return 0;
33 | }
34 | ContractLogError("------2-------");
35 | //checkwitness(from)
36 | if(strcmp(from,to) == 0){
37 |
38 | return 0;
39 | }
40 | ContractLogError("------3-------");
41 | char * fromValuestr = GetStorage(from);
42 | ContractLogError(from);
43 | ContractLogError("------4-------");
44 | ContractLogError(fromValuestr);
45 | int fromValue = Atoi(fromValuestr);
46 | ContractLogError("------5-------");
47 | if (fromValue < amount){
48 | ContractLogError("------6-------");
49 | return 0;
50 | }
51 | ContractLogError("------7-------");
52 | if (fromValue == amount){
53 | DeleteStorage(from);
54 | }else{
55 | ContractLogError("------8-------");
56 | int tovalue = Atoi(GetStorage(to));
57 | ContractLogError("------9-------");
58 | PutStorage(from,Itoa(fromValue -amount));
59 | PutStorage(to,Itoa(tovalue + amount));
60 | }
61 | return 1;
62 | }
63 |
64 | char * concat(char * a, char * b){
65 | int lena = arrayLen(a);
66 | int lenb = arrayLen(b);
67 | char * res = (char *)malloc((lena + lenb)*sizeof(char));
68 | for (int i = 0 ;i < lena ;i++){
69 | res[i] = a[i];
70 | }
71 |
72 | for (int j = 0; j < lenb ;j++){
73 | res[lenb + j] = b[j];
74 | }
75 | return res;
76 | }
77 |
78 | /*
79 | *this is the common standard interface of ontology wasm contract
80 | */
81 | char * invoke(char * method,char * args){
82 |
83 | if(strcmp(method,"init") == 0){
84 | char * result = init();
85 | RuntimeNotify(result);
86 | return result;
87 | }
88 |
89 | if(strcmp(method,"totalSupply") == 0){
90 |
91 | char *result = getTotalSupply();
92 | RuntimeNotify(result);
93 | return result;
94 | }
95 |
96 | if(strcmp(method,"balanceOf") == 0){
97 | struct Param{
98 | char * address;
99 | };
100 |
101 | struct Param p;
102 | JsonUnmashalInput(&p,sizeof(p),args);
103 |
104 | char *result = balanceOf(p.address);
105 | RuntimeNotify(result);
106 | return result;
107 | }
108 |
109 | if(strcmp(method,"transfer") == 0){
110 | struct Param{
111 | char * from;
112 | char * to;
113 | int amount;
114 | };
115 |
116 | struct Param p;
117 | JsonUnmashalInput(&p,sizeof(p),args);
118 | char * result;
119 | if(transfer(p.from,p.to,p.amount) > 0){
120 |
121 | result = JsonMashalResult(Itoa(p.amount),"string");
122 | }else{
123 | // result = JsonMashalResult(concat(concat(concat(concat(concat("transfer :",p.from),"to:"),p.to),I64toa(p.amount)),"failed!"),"string");
124 | result = JsonMashalResult(Itoa(p.amount),"string");
125 | }
126 | RuntimeNotify(result);
127 | return result;
128 | }
129 | }
--------------------------------------------------------------------------------
/testcase/smartcontract/api/executionengine/callingscripthash.go:
--------------------------------------------------------------------------------
1 | package executionengine
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | )
9 |
10 | /*
11 | using Neo.SmartContract.Framework;
12 | using Neo.SmartContract.Framework.Services.Neo;
13 | using Neo.SmartContract.Framework.Services.System;
14 | using System.Numerics;
15 |
16 | public class A : SmartContract
17 | {
18 | public static byte[] Main()
19 | {
20 | return ExecutionEngine.CallingScriptHash;
21 | }
22 | }
23 | Code := 51c56b6161682b53797374656d2e457865637574696f6e456e67696e652e47657443616c6c696e67536372697074486173686c766b00527ac46203006c766b00c3616c7566
24 | ---------------------------------------------------------------
25 |
26 | using Neo.SmartContract.Framework;
27 | using Neo.SmartContract.Framework.Services.Neo;
28 | using Neo.SmartContract.Framework.Services.System;
29 | using System.Numerics;
30 |
31 | public class B : SmartContract
32 | {
33 | [Appcall("7d0cd19e13a388af45af797fff87894cecb6d4ae")]
34 | public static extern byte[] CallContract();
35 | public static void Main()
36 | {
37 | byte[] callScript = CallContract();
38 | Storage.Put(Storage.CurrentContext, "callScript", callScript);
39 | }
40 | }
41 | Code := 51c56b616167aed4b6ec4c8987ff7f79af45af88a3139ed10c7d6c766b00527ac461681953797374656d2e53746f726167652e476574436f6e746578740a63616c6c5363726970746c766b00c3615272681253797374656d2e53746f726167652e50757461616c7566
42 | */
43 |
44 | func TestCallingScriptHash(ctx *testframework.TestFrameworkContext) bool {
45 | codeA := "51c56b6161682b53797374656d2e457865637574696f6e456e67696e652e47657443616c6c696e67536372697074486173686c766b00527ac46203006c766b00c3616c7566"
46 | codeAddressA, _ := utils.GetContractAddress(codeA)
47 | signer, err := ctx.GetDefaultAccount()
48 |
49 | if err != nil {
50 | ctx.LogError("TestCallingScriptHash - GetDefaultAccount error: %s", err)
51 | return false
52 | }
53 |
54 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
55 | signer,
56 | true,
57 | codeA,
58 | "TestCallingScriptHash",
59 | "",
60 | "",
61 | "",
62 | "")
63 |
64 | if err != nil {
65 | ctx.LogError("TestCallingScriptHash DeploySmartContract error:%s", err)
66 | return false
67 | }
68 |
69 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
70 | if err != nil {
71 | ctx.LogError("TestCallingScriptHash WaitForGenerateBlock error:%s", err)
72 | return false
73 | }
74 |
75 | codeB := "51c56b616167aed4b6ec4c8987ff7f79af45af88a3139ed10c7d6c766b00527ac461681953797374656d2e53746f726167652e476574436f6e746578740a63616c6c5363726970746c766b00c3615272681253797374656d2e53746f726167652e50757461616c7566"
76 | codeAddressB, _ := utils.GetContractAddress(codeB)
77 |
78 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
79 | signer,
80 | true,
81 | codeB,
82 | "TestCallingScriptHash",
83 | "",
84 | "",
85 | "",
86 | "")
87 |
88 | if err != nil {
89 | ctx.LogError("TestCallingScriptHash DeploySmartContract error:%s", err)
90 | return false
91 | }
92 |
93 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
94 | if err != nil {
95 | ctx.LogError("TestCallingScriptHash WaitForGenerateBlock error:%s", err)
96 | return false
97 | }
98 |
99 | _, err = ctx.Ont.NeoVM.InvokeNeoVMContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
100 | signer,
101 | codeAddressB,
102 | []interface{}{})
103 |
104 | if err != nil {
105 | ctx.LogError("TestCallingScriptHash error:%s", err)
106 | return false
107 | }
108 |
109 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
110 | if err != nil {
111 | ctx.LogError("TestCallingScriptHash WaitForGenerateBlock error:%s", err)
112 | return false
113 | }
114 |
115 | callScript, err := ctx.Ont.GetStorage(codeAddressB.ToHexString(), []byte("callScript"))
116 | if err != nil {
117 | ctx.LogError("TestCallingScriptHash - GetStorage error: %s", err)
118 | return false
119 | }
120 |
121 | ctx.LogInfo("CodeA Address:%s", codeAddressA.ToHexString())
122 | ctx.LogInfo("CodeB Address:%s", codeAddressB.ToHexString())
123 |
124 | err = ctx.AssertToByteArray(callScript, codeAddressB[:])
125 | if err != nil {
126 | ctx.LogError("TestCallingScriptHash AssertToByteArray error:%s", err)
127 | return false
128 | }
129 | return true
130 | }
131 |
--------------------------------------------------------------------------------
/testcase/smartcontract/api/contract/destroy.go:
--------------------------------------------------------------------------------
1 | package contract
2 |
3 | import (
4 | "time"
5 |
6 | "github.com/ontio/ontology-go-sdk/utils"
7 | "github.com/ontio/ontology-test/testframework"
8 | )
9 |
10 | /*
11 | contract A
12 |
13 | using Neo.SmartContract.Framework;
14 | using Neo.SmartContract.Framework.Services.Neo;
15 | using Neo.SmartContract.Framework.Services.System;
16 |
17 | public class Contract1:SmartContract
18 | {
19 | public static void Main()
20 | {
21 | Neo.SmartContract.Framework.Services.Neo.Contract.Destroy();
22 | }
23 | }
24 |
25 | code = 00c56b6161681753797374656d2e436f6e74726163742e44657374726f7961616c7566
26 |
27 | ------------------------------------------------------------------------
28 | contract B
29 |
30 | using Neo.SmartContract.Framework;
31 | using Neo.SmartContract.Framework.Services.Neo;
32 | using Neo.SmartContract.Framework.Services.System;
33 | using System;
34 | using System.ComponentModel;
35 | using System.Numerics;
36 |
37 | class OnTest : SmartContract
38 | {
39 | public static bool Main(byte[] codeHash)
40 | {
41 | byte[] script = Blockchain.GetContract(codeHash).Script;
42 | if (script == null || script.Length == 0)
43 | {
44 | return false;
45 | }
46 | return true;
47 | }
48 | }
49 |
50 | code = 54c56b6c766b00527ac4616c766b00c361681d53797374656d2e426c6f636b636861696e2e476574436f6e747261637461681b4f6e746f6c6f67792e436f6e74726163742e4765745363726970746c766b51527ac46c766b51c3640e006c766b51c3c0009c620400516c766b52527ac46c766b52c3640f0061006c766b53527ac4620e00516c766b53527ac46203006c766b53c3616c7566
51 | */
52 |
53 | func TestContractDestroy(ctx *testframework.TestFrameworkContext) bool {
54 | code := "00c56b6161681753797374656d2e436f6e74726163742e44657374726f7961616c7566"
55 | codeAddressA, _ := utils.GetContractAddress(code)
56 |
57 | signer, err := ctx.GetDefaultAccount()
58 | if err != nil {
59 | ctx.LogError("TestGetContract - GetDefaultAccount error:%s", err)
60 | return false
61 | }
62 |
63 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
64 | signer,
65 |
66 | true,
67 | code,
68 | "TestContractDestroy",
69 | "",
70 | "",
71 | "",
72 | "")
73 |
74 | if err != nil {
75 | ctx.LogError("TestContractDestroy DeploySmartContract error: %s", err)
76 | return false
77 | }
78 |
79 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 2)
80 | if err != nil {
81 | ctx.LogError("TestContractDestroy WaitForGenerateBlock error: %s", err)
82 | return false
83 | }
84 |
85 | _, err = ctx.Ont.NeoVM.InvokeNeoVMContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
86 | signer,
87 | codeAddressA,
88 | []interface{}{0})
89 |
90 | if err != nil {
91 | ctx.LogError("TestContractDestroy InvokeSmartContract error: %s", err)
92 | return false
93 | }
94 |
95 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 2)
96 | if err != nil {
97 | ctx.LogError("TestContractDestroy WaitForGenerateBlock error: %s", err)
98 | return false
99 | }
100 |
101 | code = "54c56b6c766b00527ac4616c766b00c361681d53797374656d2e426c6f636b636861696e2e476574436f6e747261637461681b4f6e746f6c6f67792e436f6e74726163742e4765745363726970746c766b51527ac46c766b51c3640e006c766b51c3c0009c620400516c766b52527ac46c766b52c3640f0061006c766b53527ac4620e00516c766b53527ac46203006c766b53c3616c7566"
102 | codeAddressB, _ := utils.GetContractAddress(code)
103 |
104 | _, err = ctx.Ont.NeoVM.DeployNeoVMSmartContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
105 | signer,
106 |
107 | true,
108 | code,
109 | "TestContractDestroy",
110 | "",
111 | "",
112 | "",
113 | "")
114 |
115 | if err != nil {
116 | ctx.LogError("TestContractDestroy DeploySmartContract error: %s", err)
117 | return false
118 | }
119 |
120 | _, err = ctx.Ont.WaitForGenerateBlock(30*time.Second, 2)
121 | if err != nil {
122 | ctx.LogError("TestContractDestroy WaitForGenerateBlock error: %s", err)
123 | return false
124 | }
125 | ctx.LogInfo("TestContractDestroy start PrepareInvokeNeoVMContractWithRes")
126 | _, err = ctx.Ont.NeoVM.InvokeNeoVMContract(ctx.GetGasPrice(), ctx.GetGasLimit(),
127 | signer,
128 | codeAddressB,
129 | []interface{}{codeAddressA[:]})
130 |
131 | _, err = ctx.Ont.NeoVM.PreExecInvokeNeoVMContract(codeAddressB, []interface{}{codeAddressA[:]})
132 | if err == nil {
133 | ctx.LogError("TestContractDestroy PrepareInvokeNeoVMContractWithRes error:%s", err)
134 | return false
135 | }
136 |
137 | return true
138 | }
139 |
--------------------------------------------------------------------------------
/test_data/headerapi.c:
--------------------------------------------------------------------------------
1 | #include "ont.h"
2 |
3 | char * invoke(char * method,char * args){
4 |
5 | if(strcmp(method,"getHeaderHashByHeight")==0){
6 | int height = ONT_ReadInt32Param(args);
7 | char * hash = ONT_Header_GetHashByHeight(height);
8 | char * result = ONT_JsonMashalResult(hash,"string",1);
9 | ONT_Runtime_Notify(result);
10 | return result;
11 | }
12 |
13 | if(strcmp(method,"getHeaderVersionByHeight")==0){
14 | int height = ONT_ReadInt32Param(args);
15 | int version = ONT_Header_GetVersionByHeight(height);
16 | char * result = ONT_JsonMashalResult(version,"int",1);
17 | ONT_Runtime_Notify(result);
18 | return result;
19 | }
20 |
21 | if(strcmp(method,"getHeaderVersionByHash")==0){
22 | char * hash = ONT_ReadStringParam(args);
23 | int version = ONT_Header_GetVersionByHash(hash);
24 | char * result = ONT_JsonMashalResult(version,"int",1);
25 | ONT_Runtime_Notify(result);
26 | return result;
27 | }
28 |
29 | if(strcmp(method,"getPrevHashByHeight")==0){
30 | int height = ONT_ReadInt32Param(args);
31 | char * prevHash = ONT_Header_GetPrevHashByHeight(height);
32 | char * result = ONT_JsonMashalResult(prevHash,"string",1);
33 | ONT_Runtime_Notify(result);
34 | return result;
35 | }
36 |
37 | if(strcmp(method,"getPrevHashByHash")==0){
38 | char * hash = ONT_ReadStringParam(args);
39 | char * prevHash = ONT_Header_GetPrevHashByHash(hash);
40 | char * result = ONT_JsonMashalResult(prevHash,"string",1);
41 | ONT_Runtime_Notify(result);
42 | return result;
43 | }
44 |
45 | if(strcmp(method,"getMerkelRootByHeight")==0){
46 | int height = ONT_ReadInt32Param(args);
47 | char * prevHash = ONT_Header_GetMerkleRootByHeight(height);
48 | char * result = ONT_JsonMashalResult(prevHash,"string",1);
49 | ONT_Runtime_Notify(result);
50 | return result;
51 | }
52 |
53 | if(strcmp(method,"getMerkelRootByHash")==0){
54 | char * hash = ONT_ReadStringParam(args);
55 | char * prevHash = ONT_Header_GetMerkleRootByHash(hash);
56 | char * result = ONT_JsonMashalResult(prevHash,"string",1);
57 | ONT_Runtime_Notify(result);
58 | return result;
59 | }
60 |
61 | if(strcmp(method,"getTimestampByHeight")==0){
62 | int height = ONT_ReadInt32Param(args);
63 | int timestamp = ONT_Header_GetTimestampByHeight(height);
64 | char * result = ONT_JsonMashalResult(timestamp,"int",1);
65 | ONT_Runtime_Notify(result);
66 | return result;
67 | }
68 |
69 | if(strcmp(method,"getTimestampByHash")==0){
70 | char * hash = ONT_ReadStringParam(args);
71 | int timestamp = ONT_Header_GetTimestampByHash(hash);
72 | char * result = ONT_JsonMashalResult(timestamp,"int",1);
73 | ONT_Runtime_Notify(result);
74 | return result;
75 | }
76 |
77 | if(strcmp(method,"getIndexByHash")==0){
78 | char * hash = ONT_ReadStringParam(args);
79 | int index = ONT_Header_GetIndexByHash(hash);
80 | char * result = ONT_JsonMashalResult(index,"int",1);
81 | ONT_Runtime_Notify(result);
82 | return result;
83 | }
84 |
85 | if(strcmp(method,"getConsensusDataByHeight")==0){
86 | int height = ONT_ReadInt32Param(args);
87 | char * data = ONT_Header_GetConsensusDataByHeight(height);
88 | char * result = ONT_JsonMashalResult(data,"int64",1);
89 | ONT_Runtime_Notify(result);
90 | return result;
91 | }
92 |
93 | if(strcmp(method,"getConsensusDataByHash")==0){
94 | char * hash = ONT_ReadStringParam(args);
95 | char * data = ONT_Header_GetConsensusDataByHash(hash);
96 | char * result = ONT_JsonMashalResult(data,"int64",1);
97 | ONT_Runtime_Notify(result);
98 | return result;
99 | }
100 |
101 | if(strcmp(method,"getNextConsensusByHeight")==0){
102 | int height = ONT_ReadInt32Param(args);
103 | char * data = ONT_Header_GetNextConsensusByHeight(height);
104 | char * result = ONT_JsonMashalResult(data,"string",1);
105 | ONT_Runtime_Notify(result);
106 | return result;
107 | }
108 |
109 | if(strcmp(method,"getNextConsensusByHash")==0){
110 | char * hash = ONT_ReadStringParam(args);
111 | char * data = ONT_Header_GetNextConsensusByHash(hash);
112 | char * result = ONT_JsonMashalResult(data,"string",1);
113 | ONT_Runtime_Notify(result);
114 | return result;
115 | }
116 |
117 |
118 | char * failed = ONT_JsonMashalResult(strconcat(method,"not supported"),"string",0);
119 | ONT_Runtime_Notify(failed);
120 | return failed;
121 | }
122 |
--------------------------------------------------------------------------------
/testframework/framework_context.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2018 The ontology Authors
3 | * This file is part of The ontology library.
4 | *
5 | * The ontology is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU Lesser General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * The ontology is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU Lesser General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU Lesser General Public License
16 | * along with The ontology. If not, see .
17 | */
18 |
19 | package testframework
20 |
21 | import (
22 | "bytes"
23 | "encoding/hex"
24 | "fmt"
25 | log4 "github.com/alecthomas/log4go"
26 | sdk "github.com/ontio/ontology-go-sdk"
27 | "github.com/ontio/ontology-test/common"
28 | "math/big"
29 | )
30 |
31 | //TestFrameworkContext is the context for test case
32 | type TestFrameworkContext struct {
33 | Ont *sdk.OntologySdk //sdk to ontology
34 | Wallet *sdk.Wallet // wallet instance
35 | failNowCh chan interface{}
36 | }
37 |
38 | //NewTestFrameworkContext return a TestFrameworkContext instance
39 | func NewTestFrameworkContext(ont *sdk.OntologySdk, wal *sdk.Wallet, failNowCh chan interface{}) *TestFrameworkContext {
40 | return &TestFrameworkContext{
41 | Ont: ont,
42 | Wallet: wal,
43 | failNowCh: failNowCh,
44 | }
45 | }
46 |
47 | //LogInfo log info in test case
48 | func (this *TestFrameworkContext) LogInfo(arg0 interface{}, args ...interface{}) {
49 | log4.Info(arg0, args...)
50 | }
51 |
52 | //LogError log error info when error occur in test case
53 | func (this *TestFrameworkContext) LogError(arg0 interface{}, args ...interface{}) {
54 | log4.Error(arg0, args...)
55 | }
56 |
57 | //LogWarn log warning info in test case
58 | func (this *TestFrameworkContext) LogWarn(arg0 interface{}, args ...interface{}) {
59 | log4.Warn(arg0, args...)
60 | }
61 |
62 | func (this *TestFrameworkContext) GetDefaultAccount() (*sdk.Account, error) {
63 | return this.Wallet.GetDefaultAccount([]byte(common.DefConfig.Password))
64 | }
65 |
66 | func (this *TestFrameworkContext) GetAccount(addr string) (*sdk.Account, error) {
67 | acc, err := this.Wallet.GetAccountByAddress(addr, []byte(common.DefConfig.Password))
68 | if err != nil {
69 | return nil, err
70 | }
71 | if acc != nil {
72 | return acc, nil
73 | }
74 | return this.Wallet.GetAccountByLabel(addr, []byte(common.DefConfig.Password))
75 | }
76 |
77 | func (this *TestFrameworkContext) NewAccount() *sdk.Account {
78 | return sdk.NewAccount()
79 | }
80 |
81 | //FailNow will stop test, and skip all haven't not test case
82 | func (this *TestFrameworkContext) FailNow() {
83 | select {
84 | case <-this.failNowCh:
85 | default:
86 | close(this.failNowCh)
87 | }
88 | }
89 |
90 | func (this *TestFrameworkContext) GetGasPrice() uint64 {
91 | return common.DefConfig.GasPrice
92 | }
93 |
94 | func (this *TestFrameworkContext) GetGasLimit() uint64 {
95 | return common.DefConfig.GasLimit
96 | }
97 |
98 | //AssertToInt compare with int, if not equal, return error
99 | func (this *TestFrameworkContext) AssertToInt(value interface{}, expect int) error {
100 | v, ok := value.(*big.Int)
101 | if !ok {
102 | return fmt.Errorf("Assert:%v to big.Int failed", value)
103 | }
104 | if int(v.Int64()) != expect {
105 | return fmt.Errorf("%v not equal:%v", value, expect)
106 | }
107 | return nil
108 | }
109 |
110 | //AssertToInt compare with uint, if not equal, return error
111 | func (this *TestFrameworkContext) AssertToUint(value interface{}, expect uint) error {
112 | v, ok := value.(*big.Int)
113 | if !ok {
114 | return fmt.Errorf("Assert:%v to uint failed", value)
115 | }
116 | if uint(v.Uint64()) != expect {
117 | return fmt.Errorf("%v not equal:%v", value, expect)
118 | }
119 | return nil
120 | }
121 |
122 | //AssertToInt compare with bool, if not equal, return error
123 | func (this *TestFrameworkContext) AssertToBoolean(value interface{}, expect bool) error {
124 | v, ok := value.(bool)
125 | if !ok {
126 | return fmt.Errorf("Assert:%v to boolean failed", value)
127 | }
128 | if v != expect {
129 | return fmt.Errorf("%v not equal:%v", value, expect)
130 | }
131 | return nil
132 | }
133 |
134 | //AssertToInt compare with string, if not equal, return error
135 | func (this *TestFrameworkContext) AssertToString(value interface{}, expect string) error {
136 | v, ok := value.(string)
137 | if !ok {
138 | return fmt.Errorf("Assert:%v to string failed", value)
139 | }
140 | if v != expect {
141 | return fmt.Errorf("%v not equal:%v", value, expect)
142 | }
143 | return nil
144 | }
145 |
146 | //AssertToInt compare with byteArray, if not equal, return error
147 | func (this *TestFrameworkContext) AssertToByteArray(value interface{}, expect []byte) error {
148 | v, ok := value.([]byte)
149 | if !ok {
150 | return fmt.Errorf("Assert:%v to byte array failed", value)
151 | }
152 | if !bytes.EqualFold(v, expect) {
153 | return fmt.Errorf("%x not equal:%x", v, expect)
154 | }
155 | return nil
156 | }
157 |
158 | //AssertToInt compare with big.Int, if not equal, return error
159 | func (this *TestFrameworkContext) AssertBigInteger(value interface{}, expect *big.Int) error {
160 | v, ok := value.(*big.Int)
161 | if !ok {
162 | return fmt.Errorf("Assert:%v to big.int failed", value)
163 | }
164 | if v.Cmp(expect) != 0 {
165 | return fmt.Errorf("%v not equal:%v", v, expect)
166 | }
167 | return nil
168 | }
169 |
170 | //ConvertToHexString return hex string
171 | func (this *TestFrameworkContext) ConvertToHexString(v interface{}) (string, error) {
172 | value, ok := v.(string)
173 | if !ok {
174 | return "", fmt.Errorf("%v ConvertToString failed", v)
175 | }
176 | data, _ := hex.DecodeString(value)
177 | return string(data), nil
178 | }
179 |
180 | //ConvertToHexString return big.Int
181 | func (this *TestFrameworkContext) ConvertToBigInt(v interface{}) (*big.Int, error) {
182 | value, ok := v.(string)
183 | if !ok {
184 | return nil, fmt.Errorf("%ConvertToBigInt failed", v)
185 | }
186 | data, _ := hex.DecodeString(value)
187 | return new(big.Int).SetBytes(data), nil
188 | }
189 |
--------------------------------------------------------------------------------
/test_data/icotest.c:
--------------------------------------------------------------------------------
1 | #include "ont.h"
2 |
3 | char * init(){
4 | char * totalsupply = ONT_Storage_Get("TCOIN_TOTAL_SUPPLY");
5 | if (arrayLen(totalsupply) > 0){
6 | return ONT_JsonMashalResult("this contract already initialized!","string",0);
7 | }else{
8 | ONT_Storage_Put("TCOIN_TOTAL_SUPPLY","1000000000");
9 | }
10 | return ONT_JsonMashalResult("init succeed","string",1);
11 | }
12 |
13 | char * getTotalSupply(){
14 | char * store = ONT_Storage_Get("TCOIN_TOTAL_SUPPLY");
15 | if (arrayLen(store) > 0){
16 | long long totalsupply = Atoi64(store);
17 | return ONT_JsonMashalResult(totalsupply,"int64",1);
18 | }else{
19 | return ONT_JsonMashalResult("total supply has not been init!","string",0);
20 | }
21 | }
22 |
23 | char * balanceOf(char * address){
24 | long long balance =Atoi64(ONT_Storage_Get(address));
25 | return ONT_JsonMashalResult(balance,"int64",1);
26 | }
27 |
28 | int transfer(char * from ,char * to, long long amount){
29 | if(amount <= 0){
30 | return 0;
31 | }
32 | int witness = ONT_Runtime_CheckWitness(from);
33 | if (witness == 0){
34 | return 0;
35 | }
36 | if(strcmp(from,to) == 0){
37 |
38 | return 0;
39 | }
40 | char * fromValuestr = ONT_Storage_Get(from);
41 |
42 | long long fromValue = Atoi64(fromValuestr);
43 | if (fromValue < amount){
44 | return 0;
45 | }
46 | if (fromValue == amount){
47 | ONT_Storage_Delete(from);
48 | }else{
49 | long long tovalue = Atoi64(ONT_Storage_Get(to));
50 | ONT_Storage_Put(from,I64toa(fromValue -amount,10));
51 | ONT_Storage_Put(to,I64toa(tovalue + amount,10));
52 | }
53 | return 1;
54 | }
55 |
56 | int transOnt(char * from ,char * to,long long amount){
57 |
58 | struct State{
59 | int ver;
60 | char * from;
61 | char * to ;
62 | long long amount;
63 | };
64 |
65 | struct Transfer {
66 | int ver;
67 | struct State * states
68 | };
69 |
70 |
71 | struct State * state = (struct State*)malloc(sizeof(struct State));
72 | state->ver = 1;
73 | state->amount = amount;
74 | state->from = from;
75 | state->to = to;
76 |
77 | struct Transfer * transfer =(struct Transfer*)malloc(sizeof(struct Transfer));
78 | transfer->ver = 1;
79 | transfer->states = state;
80 |
81 | char * args = ONT_MarshalNativeParams(transfer);
82 | char * result = ONT_CallContract("ff00000000000000000000000000000000000001","","transfer",args);
83 | if (strcmp(result,"true")==0){
84 | return 1;
85 | }else{
86 | return 0;
87 | }
88 | }
89 |
90 | /*
91 | *this is the common standard interface of ontology wasm contract
92 | */
93 | char * invoke(char * method,char * args){
94 |
95 | if(strcmp(method,"init") == 0){
96 | char * result = init();
97 | ONT_Runtime_Notify(result);
98 | return result;
99 | }
100 |
101 | if(strcmp(method,"totalSupply") == 0){
102 |
103 | char *result = getTotalSupply();
104 | ONT_Runtime_Notify(result);
105 | return result;
106 | }
107 |
108 | if(strcmp(method,"balanceOf") == 0){
109 | struct Param{
110 | char * address;
111 | };
112 |
113 | struct Param *p = (struct Param *)malloc(sizeof(struct Param));
114 | ONT_JsonUnmashalInput(p,sizeof(struct Param),args);
115 |
116 | char *result = balanceOf(p->address);
117 | ONT_Runtime_Notify(result);
118 | return result;
119 | }
120 |
121 | if(strcmp(method,"transfer") == 0){
122 | struct Param{
123 | char * from;
124 | char * to;
125 | long long amount;
126 | };
127 |
128 | struct Param *p = (struct Param *)malloc(sizeof(struct Param));
129 | ONT_JsonUnmashalInput(p,sizeof(struct Param),args);
130 | char * result;
131 | if(transfer(p->from,p->to,p->amount) > 0){
132 |
133 | result = ONT_JsonMashalResult(Itoa(p->amount),"string",1);
134 | }else{
135 | result = ONT_JsonMashalResult(Itoa(p->amount),"string",0);
136 | }
137 | ONT_Runtime_Notify(result);
138 | return result;
139 | }
140 | if(strcmp(method,"collect") == 0){
141 | struct Param{
142 | char * from ;
143 | long long amount;
144 | };
145 | struct Param *p = (struct Param *)malloc(sizeof(struct Param));
146 | ONT_JsonUnmashalInput(p,sizeof(struct Param),args);
147 |
148 | int rate = 100;
149 |
150 | char * seflAddress = ONT_GetSelfAddress();
151 | char * store = ONT_Storage_Get("TCOIN_TOTAL_SUPPLY");
152 | long long totalsupply = Atoi64(store);
153 | char * result = "";
154 | long long count = rate * p->amount;
155 | if (totalsupply >= count){
156 | int transOntResult = transOnt(p->from,seflAddress,p->amount);
157 | int tranCoin ;
158 |
159 | if (transOntResult == 1){
160 | ONT_Storage_Put("TCOIN_TOTAL_SUPPLY",I64toa(totalsupply - count,10));
161 |
162 | char * toStore = ONT_Storage_Get(p->from);
163 | long long tovalue = 0;
164 | if (arrayLen(toStore) > 0){
165 | tovalue = Atoi64(toStore);
166 | }
167 |
168 | ONT_Storage_Put(p->from,I64toa(tovalue + count,10));
169 | result = ONT_JsonMashalResult(count,"int64",1);
170 | }else{
171 | result = ONT_JsonMashalResult("transfer ont failed","string",0);
172 | }
173 | }else{
174 | result = ONT_JsonMashalResult("not enough supply","string",0);
175 | }
176 |
177 | ONT_Runtime_Notify(result);
178 | return result;
179 | }
180 | if(strcmp(method,"withdraw") == 0){
181 | struct Param{
182 | long long amount;
183 | };
184 | struct Param *p = (struct Param *)malloc(sizeof(struct Param));
185 | ONT_JsonUnmashalInput(p,sizeof(struct Param),args);
186 |
187 | char * seflAddress = ONT_GetSelfAddress();
188 | char * ownerAddress = "TA4ieHoEDmRmARQo6bVBayqPuvN51rd6wY";
189 | long long cnt = p->amount;
190 | int transOntResult = transOnt(seflAddress,ownerAddress,cnt);
191 | char * result = "";
192 | if (transOntResult ==1){
193 | result = ONT_JsonMashalResult(cnt,"int64",1);
194 | }else{
195 | result = ONT_JsonMashalResult(cnt,"int64",0);
196 | }
197 |
198 | ONT_Runtime_Notify(result);
199 | return result;
200 | }
201 |
202 | }
--------------------------------------------------------------------------------
/testcase/smartcontract/native/global_param.go:
--------------------------------------------------------------------------------
1 | package native
2 |
3 | import (
4 | "fmt"
5 | "github.com/ontio/ontology-test/testframework"
6 | "github.com/ontio/ontology/smartcontract/service/neovm"
7 | sdk"github.com/ontio/ontology-go-sdk"
8 | "time"
9 | )
10 |
11 | func TestGlobalParam(ctx *testframework.TestFrameworkContext) bool {
12 | defAcc, err := ctx.GetDefaultAccount()
13 | if err != nil {
14 | ctx.LogError("TestGlobalParam GetDefaultAccount error:%s", err)
15 | return false
16 | }
17 | oldAdmin := defAcc
18 | oldOperator := defAcc
19 |
20 | newAdmin := ctx.NewAccount()
21 | newOperator := ctx.NewAccount()
22 |
23 | err = testGetGlobalParam(ctx)
24 | if err != nil {
25 | ctx.LogError("TestGlobalParam testGetGlobalParam error:%s", err)
26 | return false
27 | }
28 | ctx.LogInfo("TestGlobalParam testGetGlobalParam success")
29 |
30 | err = testSetGlobalParam(ctx, oldOperator)
31 | if err != nil {
32 | ctx.LogError("TestGlobalParam testSetGlobalParam error:%s", err)
33 | return false
34 | }
35 | ctx.LogInfo("TestGlobalParam testSetGlobalParam success")
36 |
37 | err = testSetOperator(ctx, oldAdmin, oldOperator, newOperator)
38 | if err != nil {
39 | ctx.LogError("TestGlobalParam testSetOperator error:%s", err)
40 | return false
41 | }
42 | ctx.LogInfo("TestGlobalParam testSetOperator success")
43 |
44 | err = testTransferAndAcceptAdmin(ctx, oldAdmin, newAdmin, oldOperator, newOperator)
45 | if err != nil {
46 | ctx.LogError("TestGlobalParam testTransferAndAcceptAdmin error:%s", err)
47 | return false
48 | }
49 | ctx.LogInfo("TestGlobalParam testTransferAndAcceptAdmin success")
50 |
51 | return true
52 | }
53 |
54 | func testGetGlobalParam(ctx *testframework.TestFrameworkContext) error {
55 | params := neovm.GAS_TABLE_KEYS
56 | values, err := ctx.Ont.Native.GlobalParams.GetGlobalParams(params)
57 | if err != nil || len(values) != len(params) {
58 | return fmt.Errorf("testGetGlobalParam GetGlobalParams error:%s", err)
59 | }
60 | return nil
61 | }
62 |
63 | func testSetGlobalParam(ctx *testframework.TestFrameworkContext, operator *sdk.Account) error {
64 | testKey := "testKey"
65 | params := []string{testKey}
66 | testValue := fmt.Sprintf("%d", time.Now().Unix())
67 |
68 | err := setParam(ctx, operator, map[string]string{testKey: testValue})
69 | if err != nil {
70 | return fmt.Errorf("testSetGlobalParam SetGlobalParams error:%s", err)
71 | }
72 |
73 | values, err := ctx.Ont.Native.GlobalParams.GetGlobalParams(params)
74 | if err != nil {
75 | return fmt.Errorf("testGetGlobalParam GetGlobalParams error:%s", err)
76 | }
77 | if values[testKey] == testValue {
78 | return fmt.Errorf("testGetGlobalParam set param should not take effect before CreateSnapshot.")
79 | }
80 |
81 | _, err = ctx.Ont.Native.GlobalParams.CreateSnapshot(0, ctx.GetGasLimit(), operator)
82 | if err != nil {
83 | return fmt.Errorf("CreateSnapshot error:%s", err)
84 | }
85 | ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
86 |
87 | values, err = ctx.Ont.Native.GlobalParams.GetGlobalParams(params)
88 | if err != nil {
89 | return fmt.Errorf("testGetGlobalParam GetGlobalParams error:%s", err)
90 | }
91 | if values[testKey] != testValue {
92 | return fmt.Errorf("testGetGlobalParam set param failed. Param:%s Value:%s != %s", testKey, values[testKey], testValue)
93 | }
94 | return nil
95 | }
96 |
97 | func testSetOperator(ctx *testframework.TestFrameworkContext, admin, oldOperator, newOperator *sdk.Account) error {
98 | testKey := "testKey"
99 | testValue := "testValue"
100 | testParams := map[string]string{testKey: testValue}
101 |
102 | err := setParam(ctx, oldOperator, testParams)
103 | if err != nil {
104 | return fmt.Errorf("oldOperator set param failed before set operator. Error:%s", err)
105 | }
106 | err = setParam(ctx, newOperator, testParams)
107 | if err == nil {
108 | return fmt.Errorf("newOperator set param should failed before set operator")
109 | }
110 |
111 | _, err = ctx.Ont.Native.GlobalParams.SetOperator(0, ctx.GetGasLimit(), admin, newOperator.Address)
112 | if err != nil {
113 | return fmt.Errorf("SetOperator error:%s", err)
114 | }
115 | ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
116 |
117 | err = setParam(ctx, newOperator, testParams)
118 | if err != nil {
119 | return fmt.Errorf("newOperator set param failed after set operator. Error:%s", err)
120 | }
121 | err = setParam(ctx, oldOperator, testParams)
122 | if err == nil {
123 | return fmt.Errorf("oldOperator set param should failed after operator")
124 | }
125 |
126 | //reset operator
127 | _, err = ctx.Ont.Native.GlobalParams.SetOperator(0, ctx.GetGasLimit(), admin, oldOperator.Address)
128 | if err != nil {
129 | return fmt.Errorf("SetOperator error:%s", err)
130 | }
131 | ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
132 | return nil
133 | }
134 |
135 | func testTransferAndAcceptAdmin(ctx *testframework.TestFrameworkContext, oldAdmin, newAdmin, oldOperator, newOperator *sdk.Account) error {
136 | err := testSetOperator(ctx, oldAdmin, oldOperator, newOperator)
137 | if err != nil {
138 | return fmt.Errorf("oldAdmin set operator failed before tansfer admin. Error:%s", err)
139 | }
140 | err = testSetOperator(ctx, newAdmin, oldOperator, newOperator)
141 | if err == nil {
142 | return fmt.Errorf("newAdmin set operator should failed before tansfer admin.")
143 | }
144 |
145 | _, err = ctx.Ont.Native.GlobalParams.TransferAdmin(0, ctx.GetGasLimit(), oldAdmin, newAdmin.Address)
146 | if err != nil {
147 | return fmt.Errorf("TransferAdmin error:%s", err)
148 | }
149 | ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
150 |
151 | err = testSetOperator(ctx, oldAdmin, oldOperator, newOperator)
152 | if err != nil {
153 | return fmt.Errorf("oldAdmin set operator failed before accept admin. Error:%s", err)
154 | }
155 | err = testSetOperator(ctx, newAdmin, oldOperator, newOperator)
156 | if err == nil {
157 | return fmt.Errorf("newAdmin set operator should failed before accept admin.")
158 | }
159 |
160 | _, err = ctx.Ont.Native.GlobalParams.AcceptAdmin(0, ctx.GetGasLimit(), newAdmin)
161 | if err != nil {
162 | return fmt.Errorf("AcceptAdmin error:%s", err)
163 | }
164 | ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
165 |
166 | err = testSetOperator(ctx, oldAdmin, oldOperator, newOperator)
167 | if err == nil {
168 | return fmt.Errorf("oldAdmin set operator should fialed after accept admin.")
169 | }
170 | err = testSetOperator(ctx, newAdmin, oldOperator, newOperator)
171 | if err != nil {
172 | return fmt.Errorf("newAdmin set operator failed after accept admin. Error:%s", err)
173 | }
174 |
175 | //reset admin
176 | _, err = ctx.Ont.Native.GlobalParams.TransferAdmin(0, ctx.GetGasLimit(), newAdmin, oldAdmin.Address)
177 | if err != nil {
178 | return fmt.Errorf("TransferAdmin error:%s", err)
179 | }
180 | ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
181 | _, err = ctx.Ont.Native.GlobalParams.AcceptAdmin(0, ctx.GetGasLimit(), oldAdmin)
182 | if err != nil {
183 | return fmt.Errorf("AcceptAdmin error:%s", err)
184 | }
185 | ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
186 | return nil
187 | }
188 |
189 | func setParam(ctx *testframework.TestFrameworkContext, operator *sdk.Account, params map[string]string) error {
190 | txHash, err := ctx.Ont.Native.GlobalParams.SetGlobalParams(0, ctx.GetGasLimit(), operator, params)
191 | if err != nil {
192 | return fmt.Errorf("testSetGlobalParam SetGlobalParams error:%s", err)
193 | }
194 | ctx.Ont.WaitForGenerateBlock(30*time.Second, 1)
195 | evt, err := ctx.Ont.GetSmartContractEvent(txHash.ToHexString())
196 | if err != nil {
197 | return fmt.Errorf("GetSmartContractEvent error:%s", err)
198 | }
199 | if evt.State == 0 {
200 | return fmt.Errorf("SetGlobalParams failed")
201 | }
202 | return nil
203 | }
204 |
--------------------------------------------------------------------------------