Beispiel #1
0
        public void GetTxCounts(int begin, int end)
        {
            List <object> blockInfos = new List <object>();
            string        url        = "http://192.168.199.221:8000/chain";
            var           ch         = new CliHelper(url);
            var           ci         = new CommandInfo("get_block_info");

            for (int i = begin; i <= end; i++)
            {
                dynamic blockInfo = new System.Dynamic.ExpandoObject();
                int     height    = i;
                ci.Parameter = height.ToString();
                ch.ExecuteCommand(ci);
                Assert.IsTrue(ci.Result);
                ci.GetJsonInfo();

                var    result     = ci.JsonInfo;
                string count      = result["result"]["result"]["Body"]["TransactionsCount"].ToString();
                string txpoolSize = result["result"]["result"]["CurrentTransactionPoolSize"].ToString();
                blockInfo.Height   = height;
                blockInfo.TxCount  = count;
                blockInfo.PoolSize = txpoolSize;
                blockInfos.Add(blockInfo);
            }
            foreach (dynamic item in blockInfos)
            {
                string message = $"Height: {item.Height}, TxCount: {item.TxCount}, CurrentTransactionPoolSize: {item.PoolSize}";
                Console.WriteLine(message);
                System.Diagnostics.Debug.WriteLine(message);
            }
            Console.ReadLine();
        }
Beispiel #2
0
        public void CheckTransactionResult(out CommandInfo ci, string txId, int checkTimes = 15)
        {
            ci           = new CommandInfo("get_tx_result");
            ci.Parameter = txId;
            while (checkTimes > 0)
            {
                CH.RpcGetTxResult(ci);
                if (ci.Result)
                {
                    ci.GetJsonInfo();
                    ci.JsonInfo = ci.JsonInfo;
                    string txResult = ci.JsonInfo["result"]["result"]["tx_status"].ToString();
                    Logger.WriteInfo($"Transaction: {txId}, Status: {txResult}");

                    if (txResult == "Mined")
                    {
                        return;
                    }
                }

                checkTimes--;
                Thread.Sleep(2000);
            }

            Logger.WriteError(ci.JsonInfo.ToString());
            Assert.IsTrue(false, "Transaction execute status cannot mined.");
        }
Beispiel #3
0
        private bool GetContractAbi(string txId, out string contractAbi)
        {
            contractAbi = string.Empty;
            int checkTimes = 10;

            while (checkTimes > 0)
            {
                var ci = new CommandInfo("get_tx_result");
                ci.Parameter = txId;
                CH.RpcGetTxResult(ci);

                if (ci.Result)
                {
                    ci.GetJsonInfo();
                    ci.JsonInfo = ci.JsonInfo;
                    string deployResult = ci.JsonInfo["result"]["result"]["tx_status"].ToString();
                    Logger.WriteInfo($"Transaction: {txId}, Status: {deployResult}");
                    if (deployResult == "Mined")
                    {
                        contractAbi = ci.JsonInfo["result"]["result"]["return"].ToString();
                        ContractAbi = contractAbi;
                        Logger.WriteInfo($"Get contract ABI: TxId: {txId}, ABI address: {contractAbi}");
                        return(true);
                    }


                    checkTimes--;
                    Thread.Sleep(2000);
                }
            }

            return(false);
        }
Beispiel #4
0
        public List <IndexItem> GetIndexBlockInfo(int height)
        {
            List <IndexItem> reList = new List <IndexItem>();

            var ci = new CommandInfo("get_block_info");

            ci.Parameter = $"{height} {false}";
            CH.ExecuteCommand(ci);
            Assert.IsTrue(ci.Result, "Query block information got exception.");
            ci.GetJsonInfo();
            var indexSideInfo = ci.JsonInfo["result"]["result"]["Body"]["IndexedSideChainBlcokInfo"];

            if (indexSideInfo.Children().Count() != 0)
            {
                //continue with sidechain verification
                var indexs = indexSideInfo.ToArray();
                foreach (var item in indexs)
                {
                    string    chainId     = item.Path.Replace("result.result.Body.IndexedSideChainBlcokInfo.", "");
                    int       indexHeight = indexSideInfo[chainId].Value <Int32>("Height");
                    IndexItem index       = new IndexItem()
                    {
                        ChainId = chainId, Height = indexHeight
                    };
                    reList.Add(index);
                }
            }

            return(reList);
        }
        public void CheckNodeStatus()
        {
            for (int i = 0; i < 10; i++)
            {
                var ci = new CommandInfo("get_block_height");
                CH.ExecuteCommand(ci);
                ci.GetJsonInfo();
                var    result        = ci.JsonInfo;
                string countStr      = result["result"]["result"]["block_height"].ToString();
                int    currentHeight = Int32.Parse(countStr);

                if (BlockHeight != currentHeight)
                {
                    BlockHeight = currentHeight;
                    Logger.WriteInfo("Current block height: {0}", BlockHeight);
                    return;
                }
                else
                {
                    Thread.Sleep(3000);
                    Logger.WriteWarn("Block height not changed round: {0}", i);
                }
            }
            Assert.IsTrue(false, "Node block exception, block height not increment anymore.");
        }
        public void InitializeContract()
        {
            for (int i = 0; i < ContractList.Count; i++)
            {
                string account = AccountList[ContractList[i].AccountId].Account;
                string abiPath = ContractList[i].AbiPath;

                //Load Contract abi
                var ci = new CommandInfo("load_contract_abi");
                ci.Parameter = abiPath;
                CH.ExecuteCommand(ci);
                Assert.IsTrue(ci.Result);

                //Execute contract method
                string parameterinfo = "{\"from\":\"" + account +
                                       "\",\"to\":\"" + abiPath +
                                       "\",\"method\":\"InitBalance\",\"incr\":\"" +
                                       GetCurrentTimeStamp() + "\",\"params\":[\"" + account + "\"]}";
                ci           = new CommandInfo("broadcast_tx");
                ci.Parameter = parameterinfo;
                CH.ExecuteCommand(ci);
                Assert.IsTrue(ci.Result);
                ci.GetJsonInfo();

                string genesisContract = ci.JsonInfo["txId"].ToString();
                Assert.AreNotEqual(string.Empty, genesisContract);
                TxIdList.Add(genesisContract);
            }

            CheckResultStatus(TxIdList);
        }
        public void DeployContract()
        {
            List <object> contractList = new List <object>();

            for (int i = 0; i < ThreadCount; i++)
            {
                dynamic info = new System.Dynamic.ExpandoObject();
                info.Id      = i;
                info.Account = AccountList[i].Account;

                var ci = new CommandInfo("deploy_contract");
                ci.Parameter = $"AElf.Benchmark.TestContract 0 {AccountList[i].Account}";
                CH.ExecuteCommand(ci);
                Assert.IsTrue(ci.Result);

                ci.GetJsonInfo();
                string genesisContract = ci.JsonInfo["txId"].ToString();
                Assert.AreNotEqual(string.Empty, genesisContract);
                info.TxId   = genesisContract;
                info.Result = false;
                contractList.Add(info);
            }
            int count = 0;

            while (true)
            {
                foreach (dynamic item in contractList)
                {
                    if (item.Result == false)
                    {
                        var ci = new CommandInfo("get_tx_result");
                        ci.Parameter = item.TxId;
                        CH.ExecuteCommand(ci);
                        Assert.IsTrue(ci.Result);
                        ci.GetJsonInfo();
                        ci.JsonInfo = ci.JsonInfo;
                        string deployResult = ci.JsonInfo["result"]["result"]["tx_status"].ToString();

                        if (deployResult == "Mined")
                        {
                            count++;
                            item.Result = true;
                            string abiPath = ci.JsonInfo["result"]["result"]["return"].ToString();
                            ContractList.Add(new Contract(item.Id, abiPath));
                            AccountList[item.Id].Increment = 1;
                        }
                        Thread.Sleep(10);
                    }
                }
                if (count == contractList.Count)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(1000);
                }
            }
        }
Beispiel #8
0
        public int GetCurrentHeight()
        {
            var ci = new CommandInfo("get_block_height");

            CH.ExecuteCommand(ci);
            Assert.IsTrue(ci.Result, "Query current height got exception.");
            ci.GetJsonInfo();
            return(Int32.Parse(ci.JsonInfo["result"]["result"]["block_height"].ToString()));
        }
Beispiel #9
0
        public void GetTxResult(string rpcUrl, string txId)
        {
            var CH = new CliHelper(rpcUrl);

            string method = "get_tx_result";
            var    ci     = new CommandInfo(method);

            ci.Parameter = txId;
            CH.ExecuteCommand(ci);
            ci.GetJsonInfo();
        }
        private void CheckResultStatus(List <string> idList)
        {
            int length = idList.Count;

            for (int i = length - 1; i >= 0; i--)
            {
                //CommandRequest txReq = new CommandRequest("get_tx_result", $"get_tx_result {idList[i]}");
                //txReq.Result = Instance.ExecuteCommandWithPerformance(txReq.Command, out txReq.InfoMessage, out txReq.ErrorMessage, out txReq.TimeInfo);
                //RequestList.Add(txReq);
                var ci = new CommandInfo("get_tx_result");
                ci.Parameter = idList[i];
                CH.ExecuteCommand(ci);

                if (ci.Result)
                {
                    ci.GetJsonInfo();
                    string deployResult = ci.JsonInfo["result"]["result"]["tx_status"].ToString();
                    if (deployResult == "Mined")
                    {
                        idList.Remove(idList[i]);
                    }
                }
                Thread.Sleep(10);
            }
            if (idList.Count > 0 && idList.Count != 1)
            {
                Console.WriteLine("***************** {0} ******************", idList.Count);
                CheckResultStatus(idList);
            }
            if (idList.Count == 1)
            {
                Console.WriteLine("Last one: {0}", idList[0]);
                //CommandRequest txReq = new CommandRequest("get_tx_result", $"get_tx_result {idList[0]}");
                //txReq.Result = Instance.ExecuteCommandWithPerformance(txReq.Command, out txReq.InfoMessage, out txReq.ErrorMessage, out txReq.TimeInfo);
                //RequestList.Add(txReq);
                var ci = new CommandInfo("get_tx_result");
                ci.Parameter = idList[0];
                CH.ExecuteCommand(ci);

                if (ci.Result)
                {
                    ci.GetJsonInfo();
                    string deployResult = ci.JsonInfo["result"]["result"]["tx_status"].ToString();
                    if (deployResult != "Mined")
                    {
                        Thread.Sleep(10);
                        CheckResultStatus(idList);
                    }
                }
            }

            Thread.Sleep(10);
        }
Beispiel #11
0
        public void ExecuteContractMethod(string rawTx, out string txId)
        {
            txId = string.Empty;
            var ci = new CommandInfo("broadcast_tx");

            ci.Parameter = rawTx;
            CH.RpcBroadcastTx(ci);
            if (ci.Result)
            {
                ci.GetJsonInfo();
                txId = ci.JsonInfo["txId"].ToString();
            }
            Assert.IsTrue(ci.Result, $"Execute contract failed. Reason: {ci.GetErrorMessage()}");
        }
Beispiel #12
0
        public void GenerateRpcList(int threadNo, int times)
        {
            string account = AccountList[ContractList[threadNo].AccountId].Account;
            string abiPath = ContractList[threadNo].AbiPath;

            //Get Increment info
            var ci = new CommandInfo("get_increment");

            ci.Parameter = account;
            CH.ExecuteCommand(ci);
            Assert.IsTrue(ci.Result);
            ci.GetJsonInfo();
            string increNo = ci.JsonInfo["result"]["result"]["increment"].ToString();
            int    number  = Int32.Parse(increNo);

            HashSet <int> set = new HashSet <int>();

            for (int i = 0; i < times; i++)
            {
                Random rd         = new Random(DateTime.Now.Millisecond);
                int    randNumber = rd.Next(ThreadCount, AccountList.Count);
                int    countNo    = randNumber;
                set.Add(countNo);
                string account1 = AccountList[countNo].Account;
                AccountList[countNo].Increment++;

                //Execute Transfer
                string parameterinfo = "{\"from\":\"" + account +
                                       "\",\"to\":\"" + abiPath +
                                       "\",\"method\":\"Transfer\",\"incr\":\"" +
                                       number.ToString() + "\",\"params\":[\"" + account + "\",\"" + account1 + "\",\"1\"]}";
                ci           = new CommandInfo("broadcast_tx");
                ci.Parameter = parameterinfo;
                string requestInfo = CH.RpcGenerateTransactionRawTx(ci);
                ContractRpcList.Enqueue(requestInfo);
                number++;

                //Get Balance Info
                parameterinfo = "{\"from\":\"" + account +
                                "\",\"to\":\"" + abiPath +
                                "\",\"method\":\"GetBalance\",\"incr\":\"" +
                                number.ToString() + "\",\"params\":[\"" + account + "\"]}";
                ci           = new CommandInfo("broadcast_tx");
                ci.Parameter = parameterinfo;
                requestInfo  = CH.RpcGenerateTransactionRawTx(ci);
                ContractRpcList.Enqueue(requestInfo);
                number++;
            }
        }
Beispiel #13
0
        private void CheckResultStatus(List <string> idList)
        {
            Thread.Sleep(4000);
            int length = idList.Count;

            for (int i = length - 1; i >= 0; i--)
            {
                var ci = new CommandInfo("get_tx_result");
                ci.Parameter = idList[i];
                CH.ExecuteCommand(ci);

                if (ci.Result)
                {
                    ci.GetJsonInfo();
                    string deployResult = ci.JsonInfo["result"]["result"]["tx_status"].ToString();
                    if (deployResult == "Mined")
                    {
                        idList.Remove(idList[i]);
                    }
                }
                Thread.Sleep(50);
            }
            if (idList.Count > 0 && idList.Count != 1)
            {
                Logger.WriteInfo("***************** {0} ******************", idList.Count);
                CheckResultStatus(idList);
            }
            if (idList.Count == 1)
            {
                Logger.WriteInfo("Last one: {0}", idList[0]);
                var ci = new CommandInfo("get_tx_result");
                ci.Parameter = idList[0];
                CH.ExecuteCommand(ci);

                if (ci.Result)
                {
                    ci.GetJsonInfo();
                    string deployResult = ci.JsonInfo["result"]["result"]["tx_status"].ToString();
                    if (deployResult != "Mined")
                    {
                        Thread.Sleep(50);
                        CheckResultStatus(idList);
                    }
                }
            }

            Thread.Sleep(50);
        }
Beispiel #14
0
        public MerkleItem GetMerkelPath(string txId)
        {
            MerkleItem merkle = new MerkleItem();

            merkle.TxId = txId;
            var ci = new CommandInfo("get_merkle_path");

            ci.Parameter = txId;
            CH.RpcGetMerklePath(ci);
            Assert.IsTrue(ci.Result, "Get merkel path got exception.");
            ci.GetJsonInfo();
            merkle.MPath   = ci.JsonInfo["result"]["merkle_path"].ToString();
            merkle.PHeight = ci.JsonInfo["result"].Value <Int32>("parent_height");

            return(merkle);
        }
        public int GetCurrentHeight()
        {
            var ci = new CommandInfo("get_block_height");

            CH.ExecuteCommand(ci);
            if (ci.Result)
            {
                ci.GetJsonInfo();
                return(Int32.Parse(ci.JsonInfo["result"]["result"]["block_height"].ToString()));
            }
            else
            {
                Logger.WriteError(ci.ErrorMsg?[0]);
                return(0);
            }
        }
        public JObject GetTxResult(string txHash)
        {
            var ci = new CommandInfo("get_tx_result");

            ci.Parameter = txHash;
            CH.ExecuteCommand(ci);
            if (ci.Result)
            {
                ci.GetJsonInfo();
                return(ci.JsonInfo);
            }
            else
            {
                Logger.WriteError(ci.ErrorMsg?[0]);
                return(null);
            }
        }
Beispiel #17
0
        public void GetSideChainTxId()
        {
            //Connect Chain
            var ci = new CommandInfo("connect_chain");

            CH.ExecuteCommand(ci);
            Assert.IsTrue(ci.Result, "Connect chain got exception.");
            ci.GetJsonInfo();
            SideChainTxId = ci.JsonInfo["SideChainContract"].ToString();
            ChainId       = ci.JsonInfo["chain_id"].ToString();

            //Load Sidechain ABI
            ci           = new CommandInfo("load_contract_abi");
            ci.Parameter = SideChainTxId;
            CH.ExecuteCommand(ci);
            Assert.IsTrue(ci.Result, "Load sidechain ABI got exception.");
        }
        public JObject GetBlockInfo(int height)
        {
            var ci = new CommandInfo("get_block_info");

            ci.Parameter = $"{height.ToString()} true";
            CH.ExecuteCommand(ci);
            if (ci.Result)
            {
                ci.GetJsonInfo();
                return(ci.JsonInfo);
            }
            else
            {
                Logger.WriteError(ci.ErrorMsg?[0]);
                return(null);
            }
        }
Beispiel #19
0
        public bool GetTransactionResult(string txId, out CommandInfo ci)
        {
            ci           = new CommandInfo("get_tx_result");
            ci.Parameter = txId;
            CH.ExecuteCommand(ci);

            if (ci.Result)
            {
                ci.GetJsonInfo();
                ci.JsonInfo = ci.JsonInfo;
                string txResult = ci.JsonInfo["result"]["result"]["tx_status"].ToString();
                Logger.WriteInfo($"Transaction: {txId}, Status: {txResult}");

                return(txResult == "Mined");
            }

            Logger.WriteError(ci.GetErrorMessage());
            return(false);
        }
Beispiel #20
0
        public void DeployContract(out string txId)
        {
            txId = string.Empty;
            var ci = new CommandInfo("deploy_contract");

            ci.Parameter = $"{FileName} 0 {Account}";
            CH.RpcDeployContract(ci);
            if (ci.Result)
            {
                ci.GetJsonInfo();
                txId = ci.JsonInfo["txId"].ToString();
                Logger.WriteInfo($"Transaction: DeployContract, TxId: {txId}");

                bool result = GetContractAbi(txId, out var contractAbi);
                Assert.IsTrue(result, $"Get contract abi failed.");
            }

            Assert.IsTrue(ci.Result, $"Deploy contract failed. Reason: {ci.GetErrorMessage()}");
        }
Beispiel #21
0
        public List <string> GetBlockTransactions(int height)
        {
            List <string> trList = new List <string>();

            var ci = new CommandInfo("get_block_info");

            ci.Parameter = $"{height} {true}";
            CH.RpcGetBlockInfo(ci);
            Assert.IsTrue(ci.Result, "Query block information got exception.");
            ci.GetJsonInfo();
            var transactions = ci.JsonInfo["result"]["result"]["Body"]["Transactions"].ToArray();

            foreach (var item in transactions)
            {
                trList.Add(item.ToString());
            }

            return(trList);
        }
        private void CheckResultStatus(List <string> idList, int checkTimes = 60)
        {
            if (checkTimes < 0)
            {
                Assert.IsTrue(false, "Transaction status check is over time.");
            }
            checkTimes--;
            int listCount = idList.Count;

            Thread.Sleep(2000);
            int length = idList.Count;

            for (int i = length - 1; i >= 0; i--)
            {
                var ci = new CommandInfo("get_tx_result");
                ci.Parameter = idList[i];
                CH.ExecuteCommand(ci);

                if (ci.Result)
                {
                    ci.GetJsonInfo();
                    string deployResult = ci.JsonInfo["result"]["result"]["tx_status"].ToString();
                    if (deployResult == "Mined")
                    {
                        idList.Remove(idList[i]);
                    }
                }

                Thread.Sleep(50);
            }

            if (idList.Count > 0 && idList.Count != 1)
            {
                Logger.WriteInfo("***************** {0} ******************", idList.Count);
                if (listCount == idList.Count && checkTimes == 0)
                {
                    Assert.IsTrue(false, "Transaction not executed successfully.");
                }
                CheckResultStatus(idList, checkTimes);
            }

            if (idList.Count == 1)
            {
                Logger.WriteInfo("Last one: {0}", idList[0]);
                var ci = new CommandInfo("get_tx_result");
                ci.Parameter = idList[0];
                CH.ExecuteCommand(ci);

                if (ci.Result)
                {
                    ci.GetJsonInfo();
                    string deployResult = ci.JsonInfo["result"]["result"]["tx_status"].ToString();
                    if (deployResult != "Mined")
                    {
                        Thread.Sleep(50);
                        CheckResultStatus(idList, checkTimes);
                    }
                }
            }

            Thread.Sleep(50);
        }
        //Without conflict group category
        public void DoContractCategory(int threadNo, int times)
        {
            string account = AccountList[ContractList[threadNo].AccountId].Account;
            string abiPath = ContractList[threadNo].AbiPath;

            HashSet <int> set       = new HashSet <int>();
            List <string> txIdList  = new List <string>();
            int           passCount = 0;

            for (int i = 0; i < times; i++)
            {
                Random rd         = new Random(DateTime.Now.Millisecond);
                int    randNumber = rd.Next(ThreadCount, AccountList.Count);
                int    countNo    = randNumber;
                set.Add(countNo);
                string account1 = AccountList[countNo].Account;
                AccountList[countNo].Increment++;

                //Execute Transfer
                string parameterinfo = "{\"from\":\"" + account +
                                       "\",\"to\":\"" + abiPath +
                                       "\",\"method\":\"Transfer\",\"incr\":\"" +
                                       GetCurrentTimeStamp() + "\",\"params\":[\"" + account + "\",\"" + account1 +
                                       "\",\"1\"]}";
                var ci = new CommandInfo("broadcast_tx");
                ci.Parameter = parameterinfo;
                CH.ExecuteCommand(ci);

                if (ci.Result)
                {
                    ci.GetJsonInfo();
                    txIdList.Add(ci.JsonInfo["txId"].ToString());
                    passCount++;
                }

                Thread.Sleep(10);
                //Get Balance Info
                parameterinfo = "{\"from\":\"" + account +
                                "\",\"to\":\"" + abiPath +
                                "\",\"method\":\"GetBalance\",\"incr\":\"" +
                                GetCurrentTimeStamp() + "\",\"params\":[\"" + account + "\"]}";
                ci           = new CommandInfo("broadcast_tx");
                ci.Parameter = parameterinfo;
                CH.ExecuteCommand(ci);

                if (ci.Result)
                {
                    Assert.IsTrue(ci.Result);
                    ci.GetJsonInfo();
                    txIdList.Add(ci.JsonInfo["txId"].ToString());
                    passCount++;
                }

                Thread.Sleep(10);
            }

            Logger.WriteInfo("Total contract sent: {0}, passed number: {1}", 2 * times, passCount);
            txIdList.Reverse();
            CheckResultStatus(txIdList);
            Logger.WriteInfo("{0} Transfer from Address {1}", set.Count, account);
        }
        static void Main(string[] args)
        {
            #region Basic Preparation
            //Init Logger
            string logName = "ContractTest_" + DateTime.Now.ToString("MMddHHmmss") + ".log";
            string dir     = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs", logName);
            Logger.InitLogHelper(dir);

            string url = "http://192.168.199.222:8000/chain";
            var    ch  = new CliHelper(url, AccountManager.GetDefaultDataDir());

            //Account preparation
            List <string> accList = new List <string>();
            var           ci      = new CommandInfo("account new", "account");
            for (int i = 0; i < 50; i++)
            {
                ci.Parameter = "123";
                ci           = ch.ExecuteCommand(ci);
                if (ci.Result)
                {
                    accList.Add(ci.InfoMsg?[0].Replace("Account address:", "").Trim());
                }
            }

            //Unlock
            ci           = new CommandInfo("account unlock", "account");
            ci.Parameter = String.Format("{0} {1} {2}", accList[0], "123", "notimeout");
            ci           = ch.ExecuteCommand(ci);

            //Connect Chain
            ci = new CommandInfo("connect_chain");
            ch.ExecuteCommand(ci);
            Assert.IsTrue(ci.Result, "Connect chain got exception.");

            //Get AElf.Contracts.Token ABI
            ci.GetJsonInfo();
            TokenAbi = ci.JsonInfo["AElf.Contracts.Token"].ToObject <string>();

            //Load default Contract Abi
            ci = new CommandInfo("load_contract_abi");
            ch.RpcLoadContractAbi(ci);
            Assert.IsTrue(ci.Result, "Load contract abi got exception.");

            #endregion

            #region AElf.Token operation
            var contract = new ContractBase(ch, TokenAbi);
            contract.Account = accList[0];

            //Deploy
            //contract.DeployContract(out var txId);

            //Load
            contract.LoadContractAbi();

            //Init
            contract.ExecuteContractMethod(out var txId1, "Initialize", "elfToken", "ELF", "2000000", "2");
            contract.CheckTransactionResult(out var initCi, txId1);

            //Transfer to Account A, B, C
            contract.ExecuteContractMethod(out var txIdA, "Transfer", accList[1], "5000");
            contract.ExecuteContractMethod(out var txIdB, "Transfer", accList[2], "10000");
            contract.ExecuteContractMethod(out var txIdC, "Transfer", accList[3], "15000");

            //check result
            contract.CheckTransactionResult(out var aCi, txIdA);
            contract.CheckTransactionResult(out var bCi, txIdB);
            contract.CheckTransactionResult(out var cCi, txIdC);

            //Get balance
            contract.ExecuteContractMethod(out var txOwner, "BalanceOf", accList[0]);
            contract.ExecuteContractMethod(out var txBA, "BalanceOf", accList[1]);
            contract.ExecuteContractMethod(out var txBB, "BalanceOf", accList[2]);
            contract.ExecuteContractMethod(out var txBC, "BalanceOf", accList[3]);

            //Query Result
            contract.GetTransactionResult(txOwner, out var ciOwner);
            contract.CheckTransactionResult(out var ciA, txBA);
            contract.CheckTransactionResult(out var ciB, txBB);
            contract.CheckTransactionResult(out var ciC, txBC);

            //Convert to Value
            ciOwner.GetJsonInfo();
            string valueStr1 = ciOwner.JsonInfo["result"]["result"]["return"].ToString();
            Logger.WriteInfo($"Owner current balance: {Convert.ToInt32(valueStr1, 16)}");

            ciA.GetJsonInfo();
            string valueStrA = ciA.JsonInfo["result"]["result"]["return"].ToString();
            Logger.WriteInfo($"A current balance: {Convert.ToInt32(valueStrA, 16)}");

            ciB.GetJsonInfo();
            string valueStrB = ciB.JsonInfo["result"]["result"]["return"].ToString();
            Logger.WriteInfo($"B current balance: {Convert.ToInt32(valueStrB, 16)}");

            ciC.GetJsonInfo();
            string valueStrC = ciC.JsonInfo["result"]["result"]["return"].ToString();
            Logger.WriteInfo($"C current balance: {Convert.ToInt32(valueStrC, 16)}");

            #endregion

            #region AElf.Contract.Resource
            #endregion
        }
Beispiel #25
0
        public void GetAllBlocksInfo(string rpcUrl)
        {
            var           CH             = new CliHelper(rpcUrl);
            List <string> transactionIds = new List <string>();

            string method = "get_block_height";
            var    ci     = new CommandInfo(method);

            CH.ExecuteCommand(ci);
            ci.GetJsonInfo();
            var    result        = ci.JsonInfo;
            string countStr      = result["result"]["result"]["block_height"].ToString();
            int    currentHeight = Int32.Parse(countStr);

            for (int i = 1; i <= currentHeight; i++)
            {
                method       = "get_block_info";
                ci           = new CommandInfo(method);
                ci.Parameter = $"{i.ToString()} true";
                CH.ExecuteCommand(ci);
                ci.GetJsonInfo();
                result = ci.JsonInfo;
                string   txcount      = result["result"]["result"]["Body"]["TransactionsCount"].ToString();
                string[] transactions = result["result"]["result"]["Body"]["Transactions"].ToString().Replace("[\n", "").Replace("\n]", "").Replace("\"", "").Split(",");
                foreach (var tx in transactions)
                {
                    if (tx.Trim() != "")
                    {
                        transactionIds.Add(tx.Trim());
                    }

                    /*
                     * method = "get_tx_result";
                     * ci = new CommandInfo(method);
                     * ci.Parameter = tx.Trim();
                     * if (ci.Parameter == "")
                     *  break;
                     * CH.ExecuteCommand(ci);
                     * Thread.Sleep(20);
                     */
                }
                string txPoolSize = result["result"]["result"]["CurrentTransactionPoolSize"].ToString();
                Logger.WriteInfo("Height: {0},  TxCount: {1}, TxPoolSize: {2}, Time: {3}", i, txcount, txPoolSize, DateTime.Now.ToString());
                Thread.Sleep(50);
            }

            //Query tx result informtion
            Logger.WriteInfo("Begin tx result query.");
            for (int i = 0; i < 10; i++)
            {
                foreach (var tx in transactionIds)
                {
                    method       = "get_tx_result";
                    ci           = new CommandInfo(method);
                    ci.Parameter = tx;
                    CH.ExecuteCommand(ci);
                    Thread.Sleep(10);
                }
            }

            Logger.WriteInfo("Complete all query operation.");
        }
Beispiel #26
0
        public void CheckVerifyTransactionResult(CancellationToken token, object state)
        {
            while (true)
            {
                if (token.IsCancellationRequested)
                {
                    break;
                }

                VerifyResult vr = null;
                if (!VerifyResultList.TryDequeue(out vr))
                {
                    Thread.Sleep(5000);
                    continue;
                }

                int txLength   = vr.VerifyTxList.Count;
                int checkTimes = 0;
                while (true)
                {
                    if (txLength == vr.VerifyTxList.Count && checkTimes == 5)
                    {
                        Logger.WriteWarn("Chain={0} at Height: {1} failed due to transaction always pending.",
                                         vr.NodeName, vr.Height);
                        vr.Result = "Pending";
                        break;
                    }

                    if (txLength == vr.VerifyTxList.Count && checkTimes != 5)
                    {
                        checkTimes++;
                        Thread.Sleep(2000);
                    }
                    else
                    {
                        checkTimes = 0;
                    }

                    string txId = string.Empty;
                    if (!vr.VerifyTxList.TryDequeue(out txId))
                    {
                        Logger.WriteInfo("Chain={0}, Height={1} passed verification.", ChainName, vr.Height);
                        vr.Result = "Passed";
                        Console.WriteLine();
                        break;
                    }

                    var ci = new CommandInfo("get_tx_result");
                    ci.Parameter = txId;
                    CH.RpcGetTxResult(ci);
                    if (ci.Result)
                    {
                        ci.GetJsonInfo();
                        string deployResult = ci.JsonInfo["result"]["result"]["tx_status"].ToString();

                        if (deployResult == "Pending")
                        {
                            Logger.WriteWarn("TxId: {0}, Status: Pendig", txId);
                            vr.VerifyTxList.Enqueue(txId);
                            Thread.Sleep(500);
                        }
                        else if (deployResult == "Mined")
                        {
                            string returnValue = ci.JsonInfo["result"]["result"]["return"].ToString();
                            if (returnValue != "01")
                            {
                                Logger.WriteInfo(ci.InfoMsg[0]);
                                Assert.IsTrue(false,
                                              $"Verification failed with transaction with chain: {vr.NodeName} at height: {vr.Height}");
                            }
                        }
                    }
                    else
                    {
                        //Handle request failed scenario
                        vr.VerifyTxList.Enqueue(txId);
                        Thread.Sleep(500);
                    }
                }
            }
        }
Beispiel #27
0
        public void GenerateContractList(int threadNo, int times)
        {
            string account = AccountList[ContractList[threadNo].AccountId].Account;
            string abiPath = ContractList[threadNo].AbiPath;

            //Get Increment info
            var ci = new CommandInfo("get_increment");

            ci.Parameter = account;
            CH.ExecuteCommand(ci);
            Assert.IsTrue(ci.Result);
            ci.GetJsonInfo();
            string increNo = ci.JsonInfo["result"]["result"]["increment"].ToString();
            int    number  = Int32.Parse(increNo);

            HashSet <int> set = new HashSet <int>();

            List <string> rpcRequest = new List <string>();

            for (int i = 0; i < times; i++)
            {
                Random rd         = new Random(DateTime.Now.Millisecond);
                int    randNumber = rd.Next(ThreadCount, AccountList.Count);
                int    countNo    = randNumber;
                set.Add(countNo);
                string account1 = AccountList[countNo].Account;
                AccountList[countNo].Increment++;

                //Execute Transfer
                string parameterinfo = "{\"from\":\"" + account +
                                       "\",\"to\":\"" + abiPath +
                                       "\",\"method\":\"Transfer\",\"incr\":\"" +
                                       number.ToString() + "\",\"params\":[\"" + account + "\",\"" + account1 + "\",\"1\"]}";
                ci           = new CommandInfo("broadcast_tx");
                ci.Parameter = parameterinfo;
                string requestInfo = CH.RpcGenerateTransactionRawTx(ci);
                rpcRequest.Add(requestInfo);
                number++;

                //Get Balance Info
                parameterinfo = "{\"from\":\"" + account +
                                "\",\"to\":\"" + abiPath +
                                "\",\"method\":\"GetBalance\",\"incr\":\"" +
                                number.ToString() + "\",\"params\":[\"" + account + "\"]}";
                ci           = new CommandInfo("broadcast_tx");
                ci.Parameter = parameterinfo;
                requestInfo  = CH.RpcGenerateTransactionRawTx(ci);
                rpcRequest.Add(requestInfo);
                number++;
            }
            Logger.WriteInfo("Thread [{0}] contracts rpc list from account :{1} and contract abi: {2} generated completed.", threadNo, account, abiPath);
            //Send RPC Requests
            ci = new CommandInfo("broadcast_txs");
            foreach (var rpc in rpcRequest)
            {
                ci.Parameter += "," + rpc;
            }
            ci.Parameter = ci.Parameter.Substring(1);
            CH.ExecuteCommand(ci);
            Assert.IsTrue(ci.Result);
            var result = ci.InfoMsg[0].Replace("[", "").Replace("]", "").Split(",");

            Logger.WriteInfo("Batch request count: {0}, Pass count: {1} at {2}", rpcRequest.Count, result.Length, DateTime.Now.ToString("HH:mm:ss.fff"));
            Logger.WriteInfo("Thread [{0}] completeed executed {1} times contracts work at {2}.", threadNo, times, DateTime.Now.ToString());
            Logger.WriteInfo("{0} Transfer from Address {1}", set.Count, account);
        }