public static async void TestTransfer(HttpMessage httpMessage)
        {
            if (Wallet.GetWallet().keys.Count < 1000)
            {
                for (int ii = Wallet.GetWallet().keys.Count; ii < 1000; ii++)
                {
                    Wallet.GetWallet().Create("123");
                }
                Wallet.GetWallet().SaveWallet();

                Log.Info("TestTransfer create");

                Session session2 = null;
                for (int ii = 1; ii < 1000; ii++)
                {
                    int random1 = 0;
                    int random2 = ii;
                    int random3 = 100000;

                    BlockSub transfer = new BlockSub();
                    transfer.type       = "transfer";
                    transfer.addressIn  = Wallet.GetWallet().keys[random1].ToAddress();
                    transfer.addressOut = Wallet.GetWallet().keys[random2].ToAddress();
                    transfer.amount     = random3.ToString();
                    transfer.data       = $"";
                    transfer.depend     = "";
                    transfer.nonce      = HttpRpc.GetAccountNotice(transfer.addressIn, false);
                    transfer.timestamp  = TimeHelper.Now();
                    transfer.hash       = transfer.ToHash();
                    transfer.sign       = transfer.ToSign(Wallet.GetWallet().keys[random1]);

                    session2 = await HttpRpc.OnTransferAsync2(transfer, session2);

                    while (session2 == null)
                    {
                        session2 = await HttpRpc.OnTransferAsync2(transfer, session2);
                    }
                    ;
                }
            }
            else
            {
                if (!HttpRpc.GetParam(httpMessage, "3", "Count", out string strCount))
                {
                    httpMessage.result = "command error! \nexample: test 5 Address C:\\Address.csv";
                    return;
                }
                TestTransfersyncCount = int.Parse(strCount);

                var consensus = Entity.Root.GetComponent <Consensus>();
                while (true)
                {
                    Log.Info($"Test2Async {TestTransfersyncCount}");
                    consensus.AddRunAction(TestTransfersync);
                    await Task.Delay(1000);
                }
            }
        }
Beispiel #2
0
        public void OnBeRuler(HttpMessage httpMessage)
        {
            var consensus = Entity.Root.GetComponent <Consensus>();
            var blockMgr  = Entity.Root.GetComponent <BlockMgr>();

            // 判断当前块高度是否接近主线
            if (blockMgr.newBlockHeight - consensus.transferHeight > 1000)
            {
                httpMessage.result = $"{consensus.transferHeight}:{blockMgr.newBlockHeight} The current block height is too low. command BeRuler have been ignore.";
                return;
            }

            WalletKey key = Wallet.GetWallet().GetCurWallet();

            var  address = key.ToAddress();
            long notice  = 1;

            using (var dbSnapshot = Entity.Root.GetComponent <LevelDBStore>().GetSnapshot(0))
            {
                var account = dbSnapshot.Accounts.Get(address);
                if (account != null)
                {
                    notice = account.nonce + 1;
                }
            }

            BlockSub transfer = new BlockSub();

            transfer.addressIn  = address;
            transfer.addressOut = consensus.consAddress;
            transfer.amount     = "0";
            transfer.nonce      = notice;
            transfer.type       = "contract";

            LuaVMCall luaVMCall = new LuaVMCall();

            luaVMCall.fnName   = "add";
            luaVMCall.args     = new FieldParam[0];
            transfer.data      = luaVMCall.Encode();
            transfer.timestamp = TimeHelper.Now();
            transfer.hash      = transfer.ToHash();
            transfer.sign      = transfer.ToSign(key);

            var rel = Entity.Root.GetComponent <Rule>().AddTransfer(transfer);

            if (rel == -1)
            {
                OnTransferAsync(transfer);
            }
            httpMessage.result = $"accepted transfer:{transfer.hash}";
        }
Beispiel #3
0
        public void OnBeRuler(HttpMessage httpMessage)
        {
            Consensus consensus = Entity.Root.GetComponent <Consensus>();
            WalletKey key       = Wallet.GetWallet().GetCurWallet();

            var  address = key.ToAddress();
            long notice  = 1;

            using (var dbSnapshot = Entity.Root.GetComponent <LevelDBStore>().GetSnapshot(0))
            {
                var account = dbSnapshot.Accounts.Get(address);
                if (account != null)
                {
                    notice = account.nonce + 1;
                }
            }

            BlockSub transfer = new BlockSub();

            transfer.addressIn  = address;
            transfer.addressOut = consensus.consAddress;
            transfer.amount     = "0";
            transfer.nonce      = notice;
            transfer.type       = "contract";

            LuaVMCall luaVMCall = new LuaVMCall();

            luaVMCall.fnName   = "add";
            luaVMCall.args     = new FieldParam[0];
            transfer.data      = luaVMCall.Encode();
            transfer.timestamp = TimeHelper.Now();

            transfer.hash = transfer.ToHash();
            transfer.sign = transfer.ToSign(key);

            var rel = Entity.Root.GetComponent <Rule>().AddTransfer(transfer);

            if (rel == -1)
            {
                OnTransferAsync(transfer);
            }
            httpMessage.result = $"accepted transfer:{transfer.hash}";
        }
Beispiel #4
0
        public async void Test3Async(object o)
        {
            Session session2 = null;

            var accountCount = Wallet.GetWallet().keys.Count;

            while (true)
            {
                Log.Info("Test2Async 200");
                session2 = null;
                for (int ii = 0; ii < 200; ii++)
                {
                    int random1 = RandomHelper.Range(0, accountCount);
                    int random2 = RandomHelper.Range(0, accountCount);
                    while (random1 == random2)
                    {
                        random2 = RandomHelper.Range(0, accountCount);
                    }
                    int random3 = RandomHelper.Range(1, 100);

                    BlockSub transfer = new BlockSub();
                    transfer.type       = "transfer";
                    transfer.addressIn  = Wallet.GetWallet().keys[random1].ToAddress();
                    transfer.addressOut = Wallet.GetWallet().keys[random2].ToAddress();
                    transfer.amount     = random3.ToString();
                    transfer.data       = "";
                    transfer.depend     = "";
                    transfer.nonce      = GetAccountNotice(transfer.addressIn);
                    transfer.timestamp  = TimeHelper.Now();
                    transfer.hash       = transfer.ToHash();
                    transfer.sign       = transfer.ToSign(Wallet.GetWallet().keys[random1]);

                    session2 = await OnTransferAsync2(transfer, session2);

                    while (session2 == null)
                    {
                        session2 = await OnTransferAsync2(transfer, session2);
                    }
                    ;
                }
                await Task.Delay(1000);
            }
        }
Beispiel #5
0
        public async void Test2Async(object o)
        {
            NodeManager nodeManager = Entity.Root.GetComponent <NodeManager>();

            for (int ii = Wallet.GetWallet().keys.Count; ii < 1000; ii++)
            {
                Wallet.GetWallet().Create();
            }
            Wallet.GetWallet().SaveWallet();

            Log.Info("Test2Async start1");

            Session session2 = null;

            for (int ii = 1; ii < 1000; ii++)
            {
                int random1 = 0;
                int random2 = ii;
                int random3 = 1000;

                BlockSub transfer = new BlockSub();
                transfer.type       = "transfer";
                transfer.addressIn  = Wallet.GetWallet().keys[random1].ToAddress();
                transfer.addressOut = Wallet.GetWallet().keys[random2].ToAddress();
                transfer.amount     = random3.ToString();
                transfer.data       = "";
                transfer.depend     = "";
                transfer.nonce      = GetAccountNotice(transfer.addressIn, false);
                transfer.timestamp  = TimeHelper.Now();
                transfer.hash       = transfer.ToHash();
                transfer.sign       = transfer.ToSign(Wallet.GetWallet().keys[random1]);

                session2 = await OnTransferAsync2(transfer, session2);

                while (session2 == null)
                {
                    session2 = await OnTransferAsync2(transfer, session2);
                }
                ;
            }
        }
        public void ThreadRun(object data)
        {
            System.Threading.Thread.Sleep(1000);

            // check url
            rulerRpc = rulerRpc ?? Entity.Root.Find("SmartxRpc")?.GetComponent <SmartxRpc>()?.GetIPEndPoint();
            if (httpPoolRelay != null && GetHeight(rulerRpc, 5) == 0)
            {
                Log.Error($"rulerRpc: {rulerRpc} can't connect");
                System.Diagnostics.Process.GetCurrentProcess().Kill();
                return;
            }

            LoadTransferFromDB();

            List <TransferHandle>        transfersDel = new List <TransferHandle>();
            Dictionary <string, Account> accounts     = new Dictionary <string, Account>();
            var timePassInfo = new TimePass(15 * 6);

            while (true)
            {
                System.Threading.Thread.Sleep(1000);

                // Query success
                try
                {
                    lock (this)
                    {
                        if (runAction != null)
                        {
                            runAction?.Invoke();
                            runAction = null;
                            SaveTransferToDB();
                        }
                    }

                    if (!timePassInfo.IsPassSet())
                    {
                        continue;
                    }

                    transfersDel.Clear();
                    for (int ii = 0; ii < transfers.Count; ii++)
                    {
                        if (transfers[ii].sendCount <= 5)
                        {
                            if (string.IsNullOrEmpty(transfers[ii].unique))
                            {
                                transfersDel.Add(transfers[ii]);
                                continue;
                            }

                            var transfer = GetUniqueTransfer(rulerRpc, transfers[ii].unique);
                            if (transfer != null)
                            {
                                if (transfer.data == transfers[ii].unique && transfer.height != 0)
                                {
                                    transfers[ii].hash = transfer.hash;
                                    transfersDel.Add(transfers[ii]);
                                }
                            }
                        }
                        else
                        {
                            File.AppendAllText("./TransferBad.csv", JsonHelper.ToJson(transfers[ii]) + "\n", Encoding.UTF8);
                            transfersDel.Add(transfers[ii]);
                        }
                    }

                    using (DbSnapshot snapshot = Entity.Root.GetComponent <Pool>().PoolDBStore.GetSnapshot())
                    {
                        bool remove = transfersDel.Count != 0;
                        // Successfully deleted from table
                        foreach (var it in transfersDel)
                        {
                            if (!string.IsNullOrEmpty(it.unique) && !string.IsNullOrEmpty(it.hash))
                            {
                                snapshot.Add($"unique_{it.unique}", it.hash); // Add to transaction cross reference table
                            }
                            transfers.Remove(it);
                        }
                        if (remove)
                        {
                            snapshot.Commit();
                        }
                    }

                    accounts.Clear();

                    long curHeight = GetHeight(rulerRpc);
                    if (curHeight == 0)
                    {
                        Log.Warning($"rulerRpc: {rulerRpc} can't connect");
                        continue;
                    }

                    // Start a new deal
                    bool bSaveDb = false;
                    for (int ii = transfers.Count - 1; ii >= 0; ii--)
                    {
                        if (transfers[ii].lastHeight < curHeight - 18 && transfers[ii].sendCount <= 5)
                        {
                            transfers[ii].lastHeight = curHeight;
                            transfers[ii].sendCount++;

                            if (BigHelper.Less(transfers[ii].amount, "0", true))
                            {
                                transfers.RemoveAt(ii);
                                continue;
                            }

                            Account account = null;
                            if (!accounts.TryGetValue(transfers[ii].addressIn, out account))
                            {
                                account = GetAccount(rulerRpc, transfers[ii].addressIn);
                                if (account == null)
                                {
                                    continue;
                                }
                                accounts.Add(transfers[ii].addressIn, account);
                            }

                            BlockSub transfer = new BlockSub();
                            transfer.addressIn  = transfers[ii].addressIn;
                            transfer.addressOut = transfers[ii].addressOut;
                            transfer.amount     = transfers[ii].amount;
                            transfer.type       = "transfer";
                            transfer.nonce      = ++account.nonce;
                            transfer.timestamp  = TimeHelper.Now();
                            transfer.data       = transfers[ii].unique;
                            transfer.extend     = new List <string>();
                            //transfer.extend.Add($"deadline:{curHeight + 16}");
                            transfer.extend.Add($"unique");

                            transfer.hash = transfer.ToHash();
                            transfer.sign = transfer.ToSign(Wallet.GetWallet().GetCurWallet());

                            //int rel = Entity.Root.GetComponent<Rule>().AddTransfer(transfer, false);
                            int rel = SendTransfer(rulerRpc, transfer);
                            if (rel == -1)
                            {
                                transfers[ii].sendCount--;
                                continue;
                            }
                            if (rel != 1)
                            {
                                File.AppendAllText("./TransferBad.csv", JsonHelper.ToJson(transfers[ii]) + "\n", Encoding.UTF8);
                                Log.Error($"TransferProcess: aAddTransfer  Error! {rel}");
                                transfers.RemoveAt(ii);
                            }
                            bSaveDb = true;
                        }
                    }
                    if (bSaveDb)
                    {
                        SaveTransferToDB();
                    }
                }
                catch (Exception)
                {
                    Log.Warning($"TransferProcess throw Exception: {rulerRpc}");
                }
            }
        }
        public static async void TestPledge(HttpMessage httpMessage)
        {
            if (Wallet.GetWallet().keys.Count < 1000)
            {
                for (int ii = Wallet.GetWallet().keys.Count; ii < 1000; ii++)
                {
                    Wallet.GetWallet().Create("123");
                }
                Wallet.GetWallet().SaveWallet();

                Log.Info("TestPledge create");

                Session session2 = null;
                for (int ii = 1; ii < 1000; ii++)
                {
                    int random1 = 0;
                    int random2 = ii;
                    int random3 = 100000;

                    BlockSub transfer = new BlockSub();
                    transfer.type       = "transfer";
                    transfer.addressIn  = Wallet.GetWallet().keys[random1].ToAddress();
                    transfer.addressOut = Wallet.GetWallet().keys[random2].ToAddress();
                    transfer.amount     = random3.ToString();
                    transfer.data       = $"";
                    transfer.depend     = "";
                    transfer.nonce      = HttpRpc.GetAccountNotice(transfer.addressIn, false);
                    transfer.timestamp  = TimeHelper.Now();
                    transfer.hash       = transfer.ToHash();
                    transfer.sign       = transfer.ToSign(Wallet.GetWallet().keys[random1]);

                    session2 = await HttpRpc.OnTransferAsync2(transfer, session2);

                    while (session2 == null)
                    {
                        session2 = await HttpRpc.OnTransferAsync2(transfer, session2);
                    }
                    ;
                }
            }
            else
            {
                var     consensus    = Entity.Root.GetComponent <Consensus>();
                var     accountCount = Wallet.GetWallet().keys.Count;
                Session session2     = null;
                while (true)
                {
                    Log.Info("TestPledge start");
                    session2 = null;
                    for (int ii = 0; ii < 200; ii++)
                    {
                        int random1 = RandomHelper.Range(1, accountCount);
                        int random2 = RandomHelper.Range(1, accountCount);
                        while (random1 == random2)
                        {
                            random2 = RandomHelper.Range(1, accountCount);
                        }

                        BlockSub transfer = new BlockSub();
                        transfer.type      = "contract";
                        transfer.addressIn = Wallet.GetWallet().keys[random1].ToAddress();
                        transfer.amount    = "";
                        transfer.depend    = "";
                        transfer.nonce     = HttpRpc.GetAccountNotice(transfer.addressIn);
                        transfer.timestamp = TimeHelper.Now();

                        var rules = consensus.GetRule(consensus.transferHeight).Select(x => x.Value.Contract).ToList();
                        rules.Remove("");
                        rules.Remove(null);

                        int callFunIndex = RandomHelper.Range(0, 3);
                        if (callFunIndex == 0)
                        {
                            int random3 = RandomHelper.Range(1, 1000);
                            transfer.data = $"addLiquidity(\"{random3}\",\"{random3}\")";

                            transfer.addressOut = rules[RandomHelper.Range(0, rules.Count) % rules.Count];
                        }
                        else
                        if (callFunIndex == 1)
                        {
                            int random3 = RandomHelper.Range(1, 1000);
                            transfer.data       = $"removeLiquidity(\"{random3}\")";
                            transfer.addressOut = rules[RandomHelper.Range(0, rules.Count) % rules.Count];
                        }
                        else
                        if (callFunIndex == 2)
                        {
                            int random3 = (int)consensus.transferHeight - RandomHelper.Range(1, 10);
                            transfer.data       = $"retrieved(\"{random3}\",\"{random3}\")";
                            transfer.addressOut = rules[RandomHelper.Range(0, rules.Count) % rules.Count];
                        }
                        else
                        if (callFunIndex == 3)
                        {
                            int random3 = (int)consensus.transferHeight - RandomHelper.Range(1, 10);
                            transfer.data       = $"diversionLiquidity(\"{random3}\",\"{rules[RandomHelper.Range(0, rules.Count) % rules.Count]}\")";
                            transfer.addressOut = rules[RandomHelper.Range(0, rules.Count) % rules.Count];
                        }
                        else
                        if (callFunIndex == 4)
                        {
                            int random3 = (int)consensus.transferHeight - RandomHelper.Range(1, 10);
                            transfer.data       = $"cancel(\"{rules[RandomHelper.Range(0, rules.Count) % rules.Count]}\",\"{random3}\")";
                            transfer.addressOut = consensus.PledgeFactory;
                        }

                        transfer.hash = transfer.ToHash();
                        transfer.sign = transfer.ToSign(Wallet.GetWallet().keys[random1]);

                        session2 = await HttpRpc.OnTransferAsync2(transfer, session2);

                        while (session2 == null)
                        {
                            session2 = await HttpRpc.OnTransferAsync2(transfer, session2);
                        }
                        ;
                    }
                    await Task.Delay(1000);
                }
            }
        }
Beispiel #8
0
        public async void Run()
        {
            await Task.Delay(1000);

            LoadTransferFromDB();

            List <TransferHandle> transfersDel = new List <TransferHandle>();
            var rule = Entity.Root.GetComponent <Rule>();

            while (true)
            {
                await Task.Delay(15000 * 6);

                // Query success
                using (var dbSnapshot = Entity.Root.GetComponent <LevelDBStore>().GetSnapshot(0))
                {
                    transfersDel.Clear();
                    for (int ii = 0; ii < transfers.Count; ii++)
                    {
                        if (transfers[ii].sendCount <= 5)
                        {
                            string hasht = dbSnapshot.Get($"unique_{transfers[ii].unique}");
                            if (!string.IsNullOrEmpty(hasht))
                            {
                                var transfer = dbSnapshot.Transfers.Get(hasht);
                                if (transfer != null)
                                {
                                    if (transfer.data == transfers[ii].unique)
                                    {
                                        transfers[ii].hash = hasht;
                                        transfersDel.Add(transfers[ii]);
                                    }
                                }
                            }
                        }
                        else
                        {
                            transfersDel.Add(transfers[ii]);
                        }
                    }

                    using (DbSnapshot snapshot = Entity.Root.GetComponent <Pool>().PoolDBStore.GetSnapshot())
                    {
                        bool remove = transfersDel.Count != 0;
                        // Successfully deleted from table
                        foreach (var it in transfersDel)
                        {
                            if (!string.IsNullOrEmpty(it.unique) && !string.IsNullOrEmpty(it.hash))
                            {
                                snapshot.Add($"unique_{it.unique}", it.hash); // Add to transaction cross reference table
                            }
                            transfers.Remove(it);
                        }
                        if (remove)
                        {
                            snapshot.Commit();
                        }
                    }

                    // Start a new deal
                    for (int ii = transfers.Count - 1; ii >= 0; ii--)
                    {
                        if (transfers[ii].lastHeight < rule.height + 6 && transfers[ii].sendCount <= 5)
                        {
                            transfers[ii].lastHeight = rule.height;
                            transfers[ii].sendCount++;

                            var      account  = dbSnapshot.Accounts.Get(transfers[ii].addressIn);
                            BlockSub transfer = new BlockSub();
                            transfer.addressIn  = transfers[ii].addressIn;
                            transfer.addressOut = transfers[ii].addressOut;
                            transfer.amount     = transfers[ii].amount;
                            transfer.type       = "transfer";
                            transfer.nonce      = ++account.nonce;
                            transfer.timestamp  = TimeHelper.Now();
                            transfer.data       = transfers[ii].unique;
                            transfer.hash       = transfer.ToHash();
                            transfer.sign       = transfer.ToSign(Wallet.GetWallet().GetCurWallet());
                            dbSnapshot.Accounts.Add(transfers[ii].addressIn, account); // account.nonce Count accumulation

                            int rel = Entity.Root.GetComponent <Rule>().AddTransfer(transfer);
                            if (rel != 1)
                            {
                                Log.Error($"TransferProcess: aAddTransfer  Error! {transfers[ii]}");
                                transfers.RemoveAt(ii);
                            }
                        }
                    }
                }
            }
        }