Beispiel #1
0
        public void StartMining(IAccount iAccount)
        {
            if (!isSystemStarted)
            {
                throw new InvalidOperationException("core_not_started");
            }
            if (mining.isStarted)
            {
                throw new InvalidOperationException("already_mining");
            }
            if (!canMine)
            {
                throw new InvalidOperationException("cant_mine");
            }

            Account account = iAccount as Account;

            if (account == null)
            {
                throw new ArgumentException("iaccount_type");
            }

            miningAddress = account.Address.Hash;

            mining.Start();

            Mine();
        }
Beispiel #2
0
            public TestWindow(Program.Logger _logger)
            {
                StackPanel sp1 = null;
                StackPanel sp2 = null;

                EventHandler<Program.LogData> _LoggerLogAdded = (sender, e) => ((Action)(() =>
                {
                    TextBlock tb = new TextBlock();
                    tb.Text = e.Text;
                    tb.Foreground = e.Kind == Program.LogData.LogKind.error ? Brushes.Red : Brushes.White;
                    tb.Margin = new Thickness(0.0, 10.0, 0.0, 10.0);

                    sp2.Children.Add(tb);
                })).BeginExecuteInUIThread();

                Loaded += (sender, e) =>
                {
                    Grid grid = new Grid();
                    grid.RowDefinitions.Add(new RowDefinition());
                    grid.RowDefinitions.Add(new RowDefinition());
                    grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
                    grid.ColumnDefinitions.Add(new ColumnDefinition());

                    ScrollViewer sv1 = new ScrollViewer();
                    sv1.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                    sv1.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                    sv1.SetValue(Grid.RowProperty, 0);
                    sv1.SetValue(Grid.ColumnProperty, 0);

                    sp1 = new StackPanel();
                    sp1.Background = Brushes.Black;

                    ScrollViewer sv2 = new ScrollViewer();
                    sv2.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                    sv2.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                    sv2.SetValue(Grid.RowProperty, 1);
                    sv2.SetValue(Grid.ColumnProperty, 0);

                    sp2 = new StackPanel();
                    sp2.Background = Brushes.Black;

                    sv1.Content = sp1;
                    sv2.Content = sp2;

                    TextBox tb = new TextBox();
                    tb.SetValue(Grid.RowProperty, 2);
                    tb.SetValue(Grid.ColumnProperty, 0);

                    grid.Children.Add(sv1);
                    grid.Children.Add(sv2);
                    grid.Children.Add(tb);

                    Content = grid;

                    Console.SetOut(new TextBlockStreamWriter(sp1));

                    _logger.LogAdded += _LoggerLogAdded;

                    //SimulationWindow sw = new SimulationWindow();
                    //sw.ShowDialog();

                    this.StartTask(string.Empty, string.Empty, () =>
                    {


                        //string testPrivateRsaParameters;
                        //using (RSACryptoServiceProvider rsacsp = new RSACryptoServiceProvider(2048))
                        //    testPrivateRsaParameters = rsacsp.ToXmlString(true);


                        //RealInboundChennel ric = new RealInboundChennel(7777, RsaKeySize.rsa2048, 100);
                        //ric.Accepted += (sender2, e2) =>
                        //{
                        //    this.StartTask("", "", () =>
                        //    {
                        //        e2.WriteBytes(BitConverter.GetBytes(true));

                        //        bool b = BitConverter.ToBoolean(e2.ReadBytes(), 0);

                        //        SessionChannel sc = e2.NewSession();
                        //        sc.WriteBytes(BitConverter.GetBytes(true));
                        //        sc.Close();

                        //        //e2.Close();
                        //    });


                        //    //e2.Close();

                        //    //Console.WriteLine("");
                        //};
                        //ric.RequestAcceptanceStart();


                        //AutoResetEvent are = new AutoResetEvent(false);
                        //SocketChannel socketc = null;

                        //RealOutboundChannel roc = new RealOutboundChannel(IPAddress.Loopback, 7777, RsaKeySize.rsa2048, testPrivateRsaParameters);
                        //roc.Connected += (sender2, e2) =>
                        //{
                        //    socketc = e2;
                        //    socketc.Sessioned += (sender3, e3) =>
                        //    {
                        //        bool b3 = BitConverter.ToBoolean(e3.ReadBytes(), 0);

                        //        Console.WriteLine("");
                        //    };

                        //    are.Set();

                        //    //e2.Close();

                        //    //Console.WriteLine("connected");
                        //};
                        //roc.RequestConnection();

                        //are.WaitOne();

                        //bool b2 = BitConverter.ToBoolean(socketc.ReadBytes(), 0);

                        //socketc.WriteBytes(BitConverter.GetBytes(true));

                        //socketc.Close();


                        //CirculatedInteger ci = new CirculatedInteger(5);

                        //Console.WriteLine(ci.GetForward(0));
                        //Console.WriteLine(ci.GetForward(1));
                        //Console.WriteLine(ci.GetForward(2));
                        //Console.WriteLine(ci.GetForward(3));
                        //Console.WriteLine(ci.GetForward(4));
                        //Console.WriteLine(ci.GetForward(5));
                        //Console.WriteLine(ci.GetForward(6));

                        //Console.WriteLine(ci.GetBackward(0));
                        //Console.WriteLine(ci.GetBackward(1));
                        //Console.WriteLine(ci.GetBackward(2));
                        //Console.WriteLine(ci.GetBackward(3));
                        //Console.WriteLine(ci.GetBackward(4));
                        //Console.WriteLine(ci.GetBackward(5));
                        //Console.WriteLine(ci.GetBackward(6));

                        Secp256k1KeyPair<Sha256Hash> secp256k1KeyPair = new Secp256k1KeyPair<Sha256Hash>(true);

                        Sha256Ripemd160Hash address = new Sha256Ripemd160Hash(secp256k1KeyPair.pubKey.pubKey);

                        TransactionInput ti1 = new TransactionInput();
                        ti1.LoadVersion1(0, 0, 0);

                        TransactionOutput to1 = new TransactionOutput();
                        to1.LoadVersion0(address, new Creacoin(50m));

                        CoinbaseTransaction ct1 = new CoinbaseTransaction();
                        ct1.LoadVersion0(new TransactionOutput[] { to1 });

                        byte[] ctBytes1 = ct1.ToBinary();

                        CoinbaseTransaction ct2 = SHAREDDATA.FromBinary<CoinbaseTransaction>(ctBytes1);

                        TransferTransaction tt1 = new TransferTransaction();
                        tt1.LoadVersion1(new TransactionInput[] { ti1 }, new TransactionOutput[] { to1 });
                        tt1.Sign(new TransactionOutput[] { to1 }, new DSAPRIVKEYBASE[] { secp256k1KeyPair.privKey });

                        byte[] ttBytes1 = tt1.ToBinary();

                        TransferTransaction tt2 = SHAREDDATA.FromBinary<TransferTransaction>(ttBytes1);

                        ResTransactions rt1 = new ResTransactions(new Transaction[] { ct1, tt1 });

                        byte[] rtBytes1 = rt1.ToBinary();

                        ResTransactions rt2 = SHAREDDATA.FromBinary<ResTransactions>(rtBytes1);


                        byte[] test1 = SHAREDDATA.ToBinary<Transaction>(ct2);

                        CoinbaseTransaction ct3 = SHAREDDATA.FromBinary<Transaction>(test1) as CoinbaseTransaction;

                        byte[] test2 = SHAREDDATA.ToBinary<Transaction>(tt2);

                        TransferTransaction tt3 = SHAREDDATA.FromBinary<Transaction>(test2) as TransferTransaction;

                        //string pathBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                        //New.BlockManagerDB bmdb = new New.BlockManagerDB(pathBase);
                        //New.BlockDB blkdb = new New.BlockDB(pathBase);
                        //New.BlockFilePointersDB bfpdb = new New.BlockFilePointersDB(pathBase);

                        //New.BlockManager bm = new New.BlockManager(bmdb, blkdb, bfpdb);

                        //New.TestBlock block1 = new New.TestBlock(1);

                        //bm.AddMainBlock(block1);
                        //bm.AddMainBlock(block1);


                        //Test10NodesInv();

                        //TestDHT();

                        //bool isFirst = true;
                        //int portNumber = 0;
                        //CreaNode cnlt = null;
                        //tb.KeyDown += (sender2, e2) =>
                        //{
                        //    if (e2.Key != Key.Enter)
                        //        return;

                        //    if (isFirst)
                        //    {
                        //        portNumber = int.Parse(tb.Text);

                        //        FirstNodeInfosDatabase fnidb = new FirstNodeInfosDatabase(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

                        //        cnlt = new CreaNode((ushort)portNumber, 0, "test", fnidb);
                        //        cnlt.Start();

                        //        cnlt.ReceivedNewChat += (sender3, e3) =>
                        //        {
                        //            this.ConsoleWriteLine(e3.Message);
                        //        };

                        //        isFirst = false;

                        //        return;
                        //    }

                        //    Chat chat = new Chat();
                        //    chat.LoadVersion0(portNumber.ToString(), tb.Text);
                        //    chat.Sign(secp256k1KeyPair.privKey);

                        //    cnlt.DiffuseNewChat(chat);
                        //};
                    });
                };

                Closed += (sender, e) =>
                {
                    _logger.LogAdded -= _LoggerLogAdded;

                    string fileText = string.Empty;
                    foreach (var child in sp1.Children)
                        fileText += (child as TextBlock).Text + Environment.NewLine;

                    File.AppendAllText(Path.Combine(new FileInfo(Assembly.GetEntryAssembly().Location).DirectoryName, "LogTest.txt"), fileText);
                };
            }
Beispiel #3
0
 public AddressEvent(Sha256Ripemd160Hash _address) { address = _address; }
Beispiel #4
0
        public void StartMining(IAccount iAccount)
        {
            if (!isSystemStarted)
                throw new InvalidOperationException("core_not_started");
            if (mining.isStarted)
                throw new InvalidOperationException("already_mining");
            if (!canMine)
                throw new InvalidOperationException("cant_mine");

            Account account = iAccount as Account;
            if (account == null)
                throw new ArgumentException("iaccount_type");

            miningAddress = account.Address.Hash;

            mining.Start();

            Mine();
        }
Beispiel #5
0
        public void NewTransaction(IAccount iAccount, Sha256Ripemd160Hash address, CurrencyUnit amount, CurrencyUnit fee)
        {
            if (!isSystemStarted)
                throw new InvalidOperationException("core_not_started");

            Account account = iAccount as Account;
            if (account == null)
                throw new ArgumentException("iaccount_type");

            utxodb.Open();

            List<Utxo> utxosList = blockChain.GetAllUtxos(account.Address.Hash);
            utxosList.Sort((a, b) =>
            {
                if (a.blockIndex < b.blockIndex)
                    return -1;
                else if (a.blockIndex > b.blockIndex)
                    return 1;

                if (a.txIndex < b.txIndex)
                    return -1;
                else if (a.txIndex > b.txIndex)
                    return 1;

                if (a.txOutIndex < b.txOutIndex)
                    return -1;
                else if (a.txOutIndex > b.txOutIndex)
                    return 1;

                return 0;
            });
            Utxo[] utxos = utxosList.ToArray();

            utxodb.Close();

            List<TransactionInput> usedTxInList = new List<TransactionInput>();
            foreach (var unconfirmedTh in transactionHistories.unconfirmedTransactionHistories.ToArray())
                for (int i = 0; i < unconfirmedTh.prevTxOuts.Length; i++)
                    if (unconfirmedTh.prevTxOuts[i].Address.Equals(account.Address.Hash))
                        usedTxInList.Add(unconfirmedTh.transaction.TxInputs[i]);
            usedTxInList.Sort((a, b) =>
            {
                if (a.PrevTxBlockIndex < b.PrevTxBlockIndex)
                    return -1;
                else if (a.PrevTxBlockIndex > b.PrevTxBlockIndex)
                    return 1;

                if (a.PrevTxIndex < b.PrevTxIndex)
                    return -1;
                else if (a.PrevTxIndex > b.PrevTxIndex)
                    return 1;

                if (a.PrevTxOutputIndex < b.PrevTxOutputIndex)
                    return -1;
                else if (a.PrevTxOutputIndex > b.PrevTxOutputIndex)
                    return 1;

                return 0;
            });
            TransactionInput[] usedTxIns = usedTxInList.ToArray();

            List<Utxo> unusedUtxosList = new List<Utxo>();

            int position = -1;
            for (int i = 0; i < usedTxIns.Length; i++)
            {
                bool flag = false;
                while (position < utxos.Length)
                {
                    position++;

                    if (usedTxIns[i].PrevTxBlockIndex == utxos[position].blockIndex && usedTxIns[i].PrevTxIndex == utxos[position].txIndex && usedTxIns[i].PrevTxOutputIndex == utxos[position].txOutIndex)
                    {
                        flag = true;

                        break;
                    }
                    else
                        unusedUtxosList.Add(utxos[position]);
                }

                if (!flag)
                    throw new InvalidOperationException();
            }

            long rawFeeAndAmount = amount.rawAmount + fee.rawAmount;

            List<Utxo> useUtxosList = new List<Utxo>();
            long rawFeeAndAmountAndChange = 0;

            bool flag2 = false;
            foreach (var utxo in unusedUtxosList)
            {
                useUtxosList.Add(utxo);

                if ((rawFeeAndAmountAndChange += utxo.amount.rawAmount) > rawFeeAndAmount)
                {
                    flag2 = true;

                    break;
                }
            }

            if (!flag2)
                for (int i = position + 1; i < utxos.Length; i++)
                {
                    useUtxosList.Add(utxos[i]);

                    if ((rawFeeAndAmountAndChange += utxos[i].amount.rawAmount) > rawFeeAndAmount)
                    {
                        flag2 = true;

                        break;
                    }
                }

            if (!flag2)
                throw new InvalidOperationException();

            Utxo[] useUtxos = useUtxosList.ToArray();

            TransactionInput[] txIns = new TransactionInput[useUtxos.Length];
            for (int i = 0; i < txIns.Length; i++)
            {
                txIns[i] = new TransactionInput();
                txIns[i].LoadVersion0(useUtxos[i].blockIndex, useUtxos[i].txIndex, useUtxos[i].txOutIndex, account.Ecdsa256KeyPair.pubKey);
            }

            long rawChange = rawFeeAndAmountAndChange - rawFeeAndAmount;

            TransactionOutput[] txOuts = new TransactionOutput[rawChange == 0 ? 1 : 2];
            txOuts[0] = new TransactionOutput();
            txOuts[0].LoadVersion0(address, amount);
            if (rawChange != 0)
            {
                txOuts[1] = new TransactionOutput();
                txOuts[1].LoadVersion0(account.Address.Hash, new CurrencyUnit(rawChange));
            }

            TransactionOutput[] prevTxOuts = new TransactionOutput[useUtxos.Length];
            for (int i = 0; i < prevTxOuts.Length; i++)
                prevTxOuts[i] = blockChain.GetMainBlock(txIns[i].PrevTxBlockIndex).Transactions[txIns[i].PrevTxIndex].TxOutputs[txIns[i].PrevTxOutputIndex];

            Ecdsa256PrivKey[] privKeys = new Ecdsa256PrivKey[useUtxos.Length];
            for (int i = 0; i < privKeys.Length; i++)
                privKeys[i] = account.Ecdsa256KeyPair.privKey;

            TransferTransaction ttx = new TransferTransaction();
            ttx.LoadVersion0(txIns, txOuts);
            ttx.Sign(prevTxOuts, privKeys);

            creaNodeTest.DiffuseNewTransaction(ttx);
        }
Beispiel #6
0
        //TransactionOutput、TransactionInput、CoinbaseTransaction、TransferTransactionのテスト
        public static void Test10()
        {
            Ecdsa256KeyPair keypair1 = new Ecdsa256KeyPair(true);
            Ecdsa256KeyPair keypair2 = new Ecdsa256KeyPair(true);
            Ecdsa256KeyPair keypair3 = new Ecdsa256KeyPair(true);

            Sha256Ripemd160Hash address1 = new Sha256Ripemd160Hash(keypair1.pubKey.pubKey);
            CurrencyUnit amount1 = new Creacoin(50.0m);
            Sha256Ripemd160Hash address2 = new Sha256Ripemd160Hash(keypair2.pubKey.pubKey);
            CurrencyUnit amount2 = new Creacoin(25.0m);
            Sha256Ripemd160Hash address3 = new Sha256Ripemd160Hash(keypair3.pubKey.pubKey);
            CurrencyUnit amount3 = new Yumina(0.01m);

            TransactionOutput txOut1 = new TransactionOutput();
            txOut1.LoadVersion0(address1, amount1);
            TransactionOutput txOut2 = new TransactionOutput();
            txOut2.LoadVersion0(address2, amount2);
            TransactionOutput txOut3 = new TransactionOutput();
            txOut3.LoadVersion0(address3, amount3);

            if (txOut1.Address != address1)
                throw new Exception("test10_1");
            if (txOut1.Amount != amount1)
                throw new Exception("test10_2");

            byte[] txOutBytes = txOut1.ToBinary();

            if (txOutBytes.Length != 29)
                throw new Exception("test10_3");

            TransactionOutput txOutRestore = SHAREDDATA.FromBinary<TransactionOutput>(txOutBytes, 0);

            if (!txOut1.Address.Equals(txOutRestore.Address))
                throw new Exception("test10_4");
            if (txOut1.Amount.rawAmount != txOutRestore.Amount.rawAmount)
                throw new Exception("test10_5");

            TransactionInput txIn1 = new TransactionInput();
            txIn1.LoadVersion0(0, 0, 0, keypair1.pubKey);
            TransactionInput txIn2 = new TransactionInput();
            txIn2.LoadVersion0(1, 0, 0, keypair2.pubKey);
            TransactionInput txIn3 = new TransactionInput();
            txIn3.LoadVersion0(2, 0, 0, keypair3.pubKey);

            if (txIn1.PrevTxBlockIndex != 0)
                throw new Exception("test10_6");
            if (txIn1.PrevTxIndex != 0)
                throw new Exception("test10_7");
            if (txIn1.PrevTxOutputIndex != 0)
                throw new Exception("test10_8");
            if (txIn1.SenderPubKey != keypair1.pubKey)
                throw new Exception("test10_9");

            TransactionOutput[] txOuts = new TransactionOutput[] { txOut1, txOut2, txOut3 };

            CoinbaseTransaction cTx = new CoinbaseTransaction();
            cTx.LoadVersion0(txOuts);

            if (cTx.TxOutputs != txOuts)
                throw new Exception("test10_10");
            if (cTx.TxInputs.Length != 0)
                throw new Exception("test10_11");

            byte[] cTxBytes = cTx.ToBinary();

            if (cTxBytes.Length != 97)
                throw new Exception("test10_12");

            CoinbaseTransaction cTxRestore = SHAREDDATA.FromBinary<CoinbaseTransaction>(cTxBytes);

            if (!cTx.Id.Equals(cTxRestore.Id))
                throw new Exception("test10_13");

            if (cTx.Verify())
                throw new Exception("test10_14");
            if (cTx.VerifyNotExistDustTxOutput())
                throw new Exception("test10_15");
            if (!cTx.VerifyNumberOfTxInputs())
                throw new Exception("test10_16");
            if (!cTx.VerifyNumberOfTxOutputs())
                throw new Exception("test10_17");

            TransactionOutput[] txOuts2 = new TransactionOutput[11];
            for (int i = 0; i < txOuts2.Length; i++)
                txOuts2[i] = txOut1;

            CoinbaseTransaction cTx2 = new CoinbaseTransaction();
            cTx2.LoadVersion0(txOuts2);

            if (cTx2.Verify())
                throw new Exception("test10_18");
            if (!cTx2.VerifyNotExistDustTxOutput())
                throw new Exception("test10_19");
            if (!cTx2.VerifyNumberOfTxInputs())
                throw new Exception("test10_20");
            if (cTx2.VerifyNumberOfTxOutputs())
                throw new Exception("test10_21");

            TransactionOutput[] txOuts3 = new TransactionOutput[] { txOut1, txOut2 };

            CoinbaseTransaction cTx3 = new CoinbaseTransaction();
            cTx3.LoadVersion0(txOuts3);

            if (!cTx3.Verify())
                throw new Exception("test10_22");
            if (!cTx3.VerifyNotExistDustTxOutput())
                throw new Exception("test10_23");
            if (!cTx3.VerifyNumberOfTxInputs())
                throw new Exception("test10_24");
            if (!cTx3.VerifyNumberOfTxOutputs())
                throw new Exception("test10_25");

            TransactionInput[] txIns = new TransactionInput[] { txIn1, txIn2, txIn3 };

            TransferTransaction tTx1 = new TransferTransaction();
            tTx1.LoadVersion0(txIns, txOuts);
            tTx1.Sign(txOuts, new DSAPRIVKEYBASE[] { keypair1.privKey, keypair2.privKey, keypair3.privKey });

            if (tTx1.TxInputs != txIns)
                throw new Exception("test10_26");
            if (tTx1.TxOutputs != txOuts)
                throw new Exception("test10_27");

            byte[] txInBytes = txIn1.ToBinary();

            if (txInBytes.Length != 153)
                throw new Exception("test10_28");

            TransactionInput txInRestore = SHAREDDATA.FromBinary<TransactionInput>(txInBytes, 0);

            if (txIn1.PrevTxBlockIndex != txInRestore.PrevTxBlockIndex)
                throw new Exception("test10_29");
            if (txIn1.PrevTxIndex != txInRestore.PrevTxIndex)
                throw new Exception("test10_30");
            if (txIn1.PrevTxOutputIndex != txInRestore.PrevTxOutputIndex)
                throw new Exception("test10_31");
            if (!txIn1.SenderPubKey.pubKey.BytesEquals(txInRestore.SenderPubKey.pubKey))
                throw new Exception("test10_32");
            if (!txIn1.SenderSignature.signature.BytesEquals(txInRestore.SenderSignature.signature))
                throw new Exception("test10_33");

            byte[] tTxBytes = tTx1.ToBinary();

            if (tTxBytes.Length != 557)
                throw new Exception("test10_34");

            TransferTransaction tTxRestore = SHAREDDATA.FromBinary<TransferTransaction>(tTxBytes);

            if (!tTx1.Id.Equals(tTxRestore.Id))
                throw new Exception("test10_35");

            if (tTx1.Verify(txOuts))
                throw new Exception("test10_36");
            if (tTx1.VerifyNotExistDustTxOutput())
                throw new Exception("test10_37");
            if (!tTx1.VerifyNumberOfTxInputs())
                throw new Exception("test10_38");
            if (!tTx1.VerifyNumberOfTxOutputs())
                throw new Exception("test10_39");
            if (!tTx1.VerifySignature(txOuts))
                throw new Exception("test10_40");
            if (!tTx1.VerifyPubKey(txOuts))
                throw new Exception("test10_41");
            if (!tTx1.VerifyAmount(txOuts))
                throw new Exception("test10_42");
            if (tTx1.GetFee(txOuts).rawAmount != 0)
                throw new Exception("test10_43");

            TransactionOutput[] txOuts4 = new TransactionOutput[] { txOut2, txOut1, txOut3 };

            if (tTx1.Verify(txOuts4))
                throw new Exception("test10_44");
            if (tTx1.VerifySignature(txOuts4))
                throw new Exception("test10_45");
            if (tTx1.VerifyPubKey(txOuts4))
                throw new Exception("test10_46");

            byte temp2 = tTx1.TxInputs[0].SenderSignature.signature[0];

            tTx1.TxInputs[0].SenderSignature.signature[0] = 0;

            if (tTx1.Verify(txOuts))
                throw new Exception("test10_47");
            if (tTx1.VerifySignature(txOuts))
                throw new Exception("test10_48");
            if (!tTx1.VerifyPubKey(txOuts))
                throw new Exception("test10_49");

            tTx1.TxInputs[0].SenderSignature.signature[0] = temp2;

            TransferTransaction tTx2 = new TransferTransaction();
            tTx2.LoadVersion0(txIns, txOuts);
            tTx2.Sign(txOuts, new DSAPRIVKEYBASE[] { keypair2.privKey, keypair1.privKey, keypair3.privKey });

            if (tTx2.Verify(txOuts))
                throw new Exception("test10_50");
            if (tTx2.VerifySignature(txOuts))
                throw new Exception("test10_51");
            if (!tTx2.VerifyPubKey(txOuts))
                throw new Exception("test10_52");

            TransferTransaction tTx3 = new TransferTransaction();
            tTx3.LoadVersion0(txIns, txOuts);
            tTx3.Sign(txOuts, new DSAPRIVKEYBASE[] { keypair1.privKey, keypair2.privKey, keypair3.privKey });

            byte temp = tTx3.TxInputs[0].SenderPubKey.pubKey[0];

            tTx3.TxInputs[0].SenderPubKey.pubKey[0] = 0;

            if (tTx3.Verify(txOuts))
                throw new Exception("test10_50");
            if (tTx3.VerifySignature(txOuts))
                throw new Exception("test10_51");
            if (tTx3.VerifyPubKey(txOuts))
                throw new Exception("test10_52");

            tTx3.TxInputs[0].SenderPubKey.pubKey[0] = temp;

            TransferTransaction tTx4 = new TransferTransaction();
            tTx4.LoadVersion0(txIns, txOuts2);
            tTx4.Sign(txOuts, new DSAPRIVKEYBASE[] { keypair1.privKey, keypair2.privKey, keypair3.privKey });

            if (tTx4.Verify(txOuts))
                throw new Exception("test10_53");
            if (!tTx4.VerifyNotExistDustTxOutput())
                throw new Exception("test10_54");
            if (!tTx4.VerifyNumberOfTxInputs())
                throw new Exception("test10_55");
            if (tTx4.VerifyNumberOfTxOutputs())
                throw new Exception("test10_56");
            if (!tTx4.VerifySignature(txOuts))
                throw new Exception("test10_57");
            if (!tTx4.VerifyPubKey(txOuts))
                throw new Exception("test10_58");
            if (tTx4.VerifyAmount(txOuts))
                throw new Exception("test10_59");
            if (tTx4.GetFee(txOuts).rawAmount != -47499990000)
                throw new Exception("test10_60");

            TransferTransaction tTx5 = new TransferTransaction();
            tTx5.LoadVersion0(txIns, txOuts3);
            tTx5.Sign(txOuts, new DSAPRIVKEYBASE[] { keypair1.privKey, keypair2.privKey, keypair3.privKey });

            if (!tTx5.Verify(txOuts))
                throw new Exception("test10_61");
            if (!tTx5.VerifyNotExistDustTxOutput())
                throw new Exception("test10_62");
            if (!tTx5.VerifyNumberOfTxInputs())
                throw new Exception("test10_63");
            if (!tTx5.VerifyNumberOfTxOutputs())
                throw new Exception("test10_64");
            if (!tTx5.VerifySignature(txOuts))
                throw new Exception("test10_65");
            if (!tTx5.VerifyPubKey(txOuts))
                throw new Exception("test10_66");
            if (!tTx5.VerifyAmount(txOuts))
                throw new Exception("test10_67");
            if (tTx5.GetFee(txOuts).rawAmount != 10000)
                throw new Exception("test10_68");

            TransactionInput[] txIns2 = new TransactionInput[101];
            for (int i = 0; i < txIns2.Length; i++)
                txIns2[i] = txIn1;

            TransactionOutput[] txOuts5 = new TransactionOutput[txIns2.Length];
            for (int i = 0; i < txOuts5.Length; i++)
                txOuts5[i] = txOut1;

            Ecdsa256PrivKey[] privKeys = new Ecdsa256PrivKey[txIns2.Length];
            for (int i = 0; i < privKeys.Length; i++)
                privKeys[i] = keypair1.privKey;

            TransferTransaction tTx6 = new TransferTransaction();
            tTx6.LoadVersion0(txIns2, txOuts3);
            tTx6.Sign(txOuts5, privKeys);

            if (tTx6.Verify(txOuts5))
                throw new Exception("test10_61");
            if (!tTx6.VerifyNotExistDustTxOutput())
                throw new Exception("test10_62");
            if (tTx6.VerifyNumberOfTxInputs())
                throw new Exception("test10_63");
            if (!tTx6.VerifyNumberOfTxOutputs())
                throw new Exception("test10_64");
            if (!tTx6.VerifySignature(txOuts5))
                throw new Exception("test10_65");
            if (!tTx6.VerifyPubKey(txOuts5))
                throw new Exception("test10_66");
            if (!tTx6.VerifyAmount(txOuts5))
                throw new Exception("test10_67");
            if (tTx6.GetFee(txOuts5).rawAmount != 497500000000)
                throw new Exception("test10_68");

            byte[] cTxBytes2 = SHAREDDATA.ToBinary<Transaction>(cTx);

            if (cTxBytes2.Length != 117)
                throw new Exception("test10_69");

            CoinbaseTransaction cTxRestore2 = SHAREDDATA.FromBinary<Transaction>(cTxBytes2) as CoinbaseTransaction;

            if (!cTx.Id.Equals(cTxRestore2.Id))
                throw new Exception("test10_70");

            byte[] tTxBytes2 = SHAREDDATA.ToBinary<Transaction>(tTx6);

            if (tTxBytes2.Length != 15445)
                throw new Exception("test10_71");

            TransferTransaction tTxRestore2 = SHAREDDATA.FromBinary<Transaction>(tTxBytes2) as TransferTransaction;

            if (!tTx6.Id.Equals(tTxRestore2.Id))
                throw new Exception("test10_72");

            Sha256Sha256Hash ctxid = new Sha256Sha256Hash(cTxBytes);

            if (!ctxid.Equals(cTx.Id))
                throw new Exception("test10_73");

            Sha256Sha256Hash ttxid = new Sha256Sha256Hash(tTx6.ToBinary());

            if (!ttxid.Equals(tTx6.Id))
                throw new Exception("test10_74");

            Console.WriteLine("test10_succeeded");
        }
Beispiel #7
0
 public TransactionOutputContext(long _bIndex, int _txIndex, int _txOutIndex, CurrencyUnit _amount, Sha256Ripemd160Hash _address, Ecdsa256KeyPair _keyPair)
 {
     bIndex = _bIndex;
     txIndex = _txIndex;
     txOutIndex = _txOutIndex;
     amount = _amount;
     address = _address;
     keyPair = _keyPair;
 }
Beispiel #8
0
        public BlockGenerator(int _numOfKeyPairs = 10, int _numOfCoinbaseTxOuts = 5, int _maxNumOfSpendTxs = 5, int _maxNumOfSpendTxOuts = 2, double _avgIORatio = 1.5)
        {
            numOfKeyPairs = _numOfKeyPairs;
            numOfCoinbaseTxOuts = _numOfCoinbaseTxOuts;
            maxNumOfSpendTxs = _maxNumOfSpendTxs;
            maxNumOfSpendTxOuts = _maxNumOfSpendTxOuts;
            avgIORatio = _avgIORatio;

            keyPairs = new Ecdsa256KeyPair[numOfKeyPairs];
            addresses = new Sha256Ripemd160Hash[numOfKeyPairs];
            for (int i = 0; i < keyPairs.Length; i++)
            {
                keyPairs[i] = new Ecdsa256KeyPair(true);
                addresses[i] = new Sha256Ripemd160Hash(keyPairs[i].pubKey.pubKey);
            }

            currentBIndex = -1;

            unspentTxOuts = new List<TransactionOutputContext>();
            spentTxOuts = new List<TransactionOutputContext>();
            blks = new List<Block>();

            unspentTxOutsDict = new Dictionary<Sha256Ripemd160Hash, List<TransactionOutputContext>>();
            spentTxOutsDict = new Dictionary<Sha256Ripemd160Hash, List<TransactionOutputContext>>();
            for (int i = 0; i < keyPairs.Length; i++)
            {
                unspentTxOutsDict.Add(addresses[i], new List<TransactionOutputContext>());
                spentTxOutsDict.Add(addresses[i], new List<TransactionOutputContext>());
            }
        }
Beispiel #9
0
        //UtxoManagerのテスト3
        public static void Test6()
        {
            string basepath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

            UtxoFileAccessDB ufadb = new UtxoFileAccessDB(basepath);
            string ufadbPath = ufadb.GetPath();

            if (File.Exists(ufadbPath))
                File.Delete(ufadbPath);

            UtxoFilePointersDB ufpdb = new UtxoFilePointersDB(basepath);
            string ufpdbPath = ufpdb.GetPath();

            if (File.Exists(ufpdbPath))
                File.Delete(ufpdbPath);

            UtxoFilePointersTempDB ufptempdb = new UtxoFilePointersTempDB(basepath);
            string ufptempdbPath = ufptempdb.GetPath();

            if (File.Exists(ufptempdbPath))
                File.Delete(ufptempdbPath);

            UtxoDB utxodb = new UtxoDB(basepath);
            string utxodbPath = utxodb.GetPath();

            if (File.Exists(utxodbPath))
                File.Delete(utxodbPath);

            UtxoManager utxom = new UtxoManager(ufadb, ufpdb, ufptempdb, utxodb);

            int length = 100;

            Sha256Ripemd160Hash[] addrs = new Sha256Ripemd160Hash[length];
            long[] bis = new long[length];
            int[] tis = new int[length];
            int[] tois = new int[length];
            Creacoin[] cs = new Creacoin[length];

            for (int i = 0; i < length; i++)
            {
                addrs[i] = new Sha256Ripemd160Hash(BitConverter.GetBytes(i));
                bis[i] = 65536.RandomNum();
                tis[i] = 65536.RandomNum();
                tois[i] = 65536.RandomNum();
                cs[i] = new Creacoin(65536.RandomNum());
            }

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            utxodb.Open();

            for (int i = 0; i < length; i++)
                utxom.AddUtxo(addrs[i], bis[i], tis[i], tois[i], cs[i]);

            utxom.SaveUFPTemp();

            utxodb.Close();

            stopwatch.Stop();

            Console.WriteLine(string.Join(":", "test6_5", stopwatch.ElapsedMilliseconds.ToString() + "ms"));

            UtxoManager utxom2 = new UtxoManager(ufadb, ufpdb, ufptempdb, utxodb);

            Utxo[] utxos = new Utxo[length];

            stopwatch.Reset();
            stopwatch.Start();

            utxodb.Open();

            for (int i = 0; i < length; i++)
                utxos[i] = utxom.FindUtxo(addrs[i], bis[i], tis[i], tois[i]);

            utxodb.Close();

            stopwatch.Stop();

            Console.WriteLine(string.Join(":", "test6_6", stopwatch.ElapsedMilliseconds.ToString() + "ms"));

            for (int i = 0; i < length; i++)
            {
                if (utxos[i].blockIndex != bis[i])
                    throw new Exception("test6_1");
                if (utxos[i].txIndex != tis[i])
                    throw new Exception("test6_2");
                if (utxos[i].txOutIndex != tois[i])
                    throw new Exception("test6_3");
                if (utxos[i].amount.rawAmount != cs[i].rawAmount)
                    throw new Exception("test6_4");
            }
        }
Beispiel #10
0
        //UtxoManagerのテスト1
        public static void Test4()
        {
            string basepath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

            UtxoFileAccessDB ufadb = new UtxoFileAccessDB(basepath);
            string ufadbPath = ufadb.GetPath();

            if (File.Exists(ufadbPath))
                File.Delete(ufadbPath);

            UtxoFilePointersDB ufpdb = new UtxoFilePointersDB(basepath);
            string ufpdbPath = ufpdb.GetPath();

            if (File.Exists(ufpdbPath))
                File.Delete(ufpdbPath);

            UtxoFilePointersTempDB ufptempdb = new UtxoFilePointersTempDB(basepath);
            string ufptempdbPath = ufptempdb.GetPath();

            if (File.Exists(ufptempdbPath))
                File.Delete(ufptempdbPath);

            UtxoDB utxodb = new UtxoDB(basepath);
            string utxodbPath = utxodb.GetPath();

            if (File.Exists(utxodbPath))
                File.Delete(utxodbPath);

            UtxoManager utxom = new UtxoManager(ufadb, ufpdb, ufptempdb, utxodb);

            if (File.Exists(ufadbPath))
                throw new Exception("test4_9");

            utxodb.Open();

            Sha256Ripemd160Hash address1 = new Sha256Ripemd160Hash(new byte[] { 0 });
            Sha256Ripemd160Hash address2 = new Sha256Ripemd160Hash(new byte[] { 1 });
            Sha256Ripemd160Hash address3 = new Sha256Ripemd160Hash(new byte[] { 2 });

            Utxo utxoNull = utxom.FindUtxo(address1, 65536.RandomNum(), 65536.RandomNum(), 65536.RandomNum());

            if (utxoNull != null)
                throw new Exception("test4_1");

            long bi1 = 65536.RandomNum();
            int ti1 = 65536.RandomNum();
            int toi1 = 65536.RandomNum();
            Creacoin c1 = new Creacoin(65536.RandomNum());

            long bi2 = 65536.RandomNum();
            int ti2 = 65536.RandomNum();
            int toi2 = 65536.RandomNum();
            Creacoin c2 = new Creacoin(65536.RandomNum());

            long bi3 = 65536.RandomNum();
            int ti3 = 65536.RandomNum();
            int toi3 = 65536.RandomNum();
            Creacoin c3 = new Creacoin(65536.RandomNum());

            utxom.AddUtxo(address1, bi1, ti1, toi1, c1);
            utxom.AddUtxo(address2, bi1, ti1, toi1, c1);
            utxom.AddUtxo(address3, bi1, ti1, toi1, c1);
            utxom.AddUtxo(address1, bi2, ti2, toi2, c2);
            utxom.AddUtxo(address2, bi2, ti2, toi2, c2);
            utxom.AddUtxo(address3, bi2, ti2, toi2, c2);
            utxom.AddUtxo(address1, bi3, ti3, toi3, c3);
            utxom.AddUtxo(address2, bi3, ti3, toi3, c3);
            utxom.AddUtxo(address3, bi3, ti3, toi3, c3);

            //2014/12/17追加 GetAllUtxosLatestFirstの試験

            List<Utxo> utxos1 = utxom.GetAllUtxosLatestFirst(address1);
            List<Utxo> utxos2 = utxom.GetAllUtxosLatestFirst(address2);
            List<Utxo> utxos3 = utxom.GetAllUtxosLatestFirst(address3);

            if (utxos1.Count != 3)
                throw new Exception("test4_19");
            if (utxos2.Count != 3)
                throw new Exception("test4_20");
            if (utxos3.Count != 3)
                throw new Exception("test4_21");

            long max = Math.Max(Math.Max(bi1, bi2), bi3);

            if (utxos1[0].blockIndex != max)
                throw new Exception("test4_22");
            if (utxos2[0].blockIndex != max)
                throw new Exception("test4_23");
            if (utxos3[0].blockIndex != max)
                throw new Exception("test4_24");

            //2014/12/17追加(終)

            Utxo utxo1 = utxom.FindUtxo(address1, bi1, ti1, toi1);

            if (utxo1 == null)
                throw new Exception("test4_2");
            if (utxo1.blockIndex != bi1)
                throw new Exception("test4_3");
            if (utxo1.txIndex != ti1)
                throw new Exception("test4_4");
            if (utxo1.txOutIndex != toi1)
                throw new Exception("test4_5");
            if (utxo1.amount.rawAmount != c1.rawAmount)
                throw new Exception("test4_6");

            utxom.AddUtxo(address1, bi1, ti1, toi1, c1);

            utxom.RemoveUtxo(address1, bi1, ti1, toi1);
            utxom.RemoveUtxo(address1, bi1, ti1, toi1);

            bool flag = false;
            try
            {
                utxom.RemoveUtxo(address1, bi1, ti1, toi1);
            }
            catch (InvalidOperationException)
            {
                flag = true;
            }
            if (!flag)
                throw new Exception("test4_7");

            Utxo utxoNull2 = utxom.FindUtxo(address1, bi1, ti1, toi1);

            if (utxoNull2 != null)
                throw new Exception("test4_8");

            utxom.SaveUFPTemp();

            utxodb.Close();

            if (!File.Exists(ufpdbPath))
                throw new Exception("test4_9");
            if (!File.Exists(ufptempdbPath))
                throw new Exception("test4_10");
            if (!File.Exists(utxodbPath))
                throw new Exception("test4_11");

            UtxoManager utxom2 = new UtxoManager(ufadb, ufpdb, ufptempdb, utxodb);

            if (File.Exists(ufptempdbPath))
                throw new Exception("test4_13");

            utxodb.Open();

            Utxo utxo2 = utxom.FindUtxo(address2, bi1, ti1, toi1);

            if (utxo2 == null)
                throw new Exception("test4_14");
            if (utxo2.blockIndex != bi1)
                throw new Exception("test4_15");
            if (utxo2.txIndex != ti1)
                throw new Exception("test4_16");
            if (utxo2.txOutIndex != toi1)
                throw new Exception("test4_17");
            if (utxo2.amount.rawAmount != c1.rawAmount)
                throw new Exception("test4_18");

            utxodb.Close();

            Console.WriteLine("test4_succeeded");
        }
Beispiel #11
0
        //UtxoFilePointersのテスト
        public static void Test3()
        {
            UtxoFilePointers ufp = new UtxoFilePointers();

            Dictionary<Sha256Ripemd160Hash, long> afps = ufp.GetAll();

            if (afps.Count != 0)
                throw new Exception("test3_1");

            Sha256Ripemd160Hash hash1 = new Sha256Ripemd160Hash(new byte[] { (byte)256.RandomNum() });
            Sha256Ripemd160Hash hash2 = new Sha256Ripemd160Hash(new byte[] { (byte)256.RandomNum() });
            Sha256Ripemd160Hash hash3 = new Sha256Ripemd160Hash(new byte[] { (byte)256.RandomNum() });

            long position1 = 56636.RandomNum();
            long position2 = 56636.RandomNum();
            long position3 = 56636.RandomNum();

            long? positionNull = ufp.Get(hash1);

            if (positionNull.HasValue)
                throw new Exception("test3_2");

            ufp.Add(hash1, position1);
            ufp.Add(hash2, position2);
            ufp.Add(hash3, position3);

            bool flag = false;
            try
            {
                ufp.Add(hash1, position1);
            }
            catch (InvalidOperationException)
            {
                flag = true;
            }
            if (!flag)
                throw new Exception("test3_3");

            Dictionary<Sha256Ripemd160Hash, long> afps2 = ufp.GetAll();

            if (afps2.Count != 3)
                throw new Exception("test3_4");
            if (!afps2.Keys.Contains(hash1))
                throw new Exception("test3_5");
            if (!afps2.Keys.Contains(hash2))
                throw new Exception("test3_6");
            if (!afps2.Keys.Contains(hash3))
                throw new Exception("test3_7");
            if (afps2[hash1] != position1)
                throw new Exception("test3_8");
            if (afps2[hash2] != position2)
                throw new Exception("test3_9");
            if (afps2[hash3] != position3)
                throw new Exception("test3_10");

            long? position1Out = ufp.Get(hash1);
            long? position2Out = ufp.Get(hash2);
            long? position3Out = ufp.Get(hash3);

            if (!position1Out.HasValue || position1Out.Value != position1)
                throw new Exception("test3_11");
            if (!position2Out.HasValue || position2Out.Value != position2)
                throw new Exception("test3_12");
            if (!position3Out.HasValue || position3Out.Value != position3)
                throw new Exception("test3_13");

            ufp.Remove(hash1);

            bool flag2 = false;
            try
            {
                ufp.Remove(hash1);
            }
            catch (InvalidOperationException)
            {
                flag2 = true;
            }
            if (!flag2)
                throw new Exception("test3_14");

            Dictionary<Sha256Ripemd160Hash, long> afps3 = ufp.GetAll();

            if (afps3.Count != 2)
                throw new Exception("test3_15");

            ufp.Update(hash2, position1);

            long? position1Out2 = ufp.Get(hash2);

            if (!position1Out2.HasValue || position1Out2.Value != position1)
                throw new Exception("test3_16");

            bool flag3 = false;
            try
            {
                ufp.Update(hash1, position2);
            }
            catch (InvalidOperationException)
            {
                flag3 = true;
            }
            if (!flag3)
                throw new Exception("test3_17");

            Dictionary<Sha256Ripemd160Hash, long> afps4 = ufp.GetAll();

            if (afps4.Count != 2)
                throw new Exception("test3_18");

            ufp.AddOrUpdate(hash2, position3);

            long? position1Out3 = ufp.Get(hash2);

            if (!position1Out3.HasValue || position1Out3.Value != position3)
                throw new Exception("test3_19");

            Dictionary<Sha256Ripemd160Hash, long> afps5 = ufp.GetAll();

            if (afps5.Count != 2)
                throw new Exception("test3_20");

            ufp.AddOrUpdate(hash1, position3);

            long? position1Out4 = ufp.Get(hash1);

            if (!position1Out4.HasValue || position1Out4.Value != position3)
                throw new Exception("test3_21");

            Dictionary<Sha256Ripemd160Hash, long> afps6 = ufp.GetAll();

            if (afps5.Count != 3)
                throw new Exception("test3_22");

            byte[] ufpBytes = ufp.ToBinary();

            UtxoFilePointers ufp2 = SHAREDDATA.FromBinary<UtxoFilePointers>(ufpBytes);

            Dictionary<Sha256Ripemd160Hash, long> afps7 = ufp2.GetAll();

            if (afps6.Count != afps7.Count)
                throw new Exception("test3_23");

            foreach (var key in afps6.Keys)
            {
                if (!afps7.Keys.Contains(key))
                    throw new Exception("test3_24");
                if (afps6[key] != afps7[key])
                    throw new Exception("test3_25");
            }

            Console.WriteLine("test3_succeeded");
        }
Beispiel #12
0
            public TestWindow(Program.Logger _logger)
            {
                StackPanel sp1 = null;
                StackPanel sp2 = null;

                EventHandler <Program.LogData> _LoggerLogAdded = (sender, e) => ((Action)(() =>
                {
                    TextBlock tb = new TextBlock();
                    tb.Text = e.Text;
                    tb.Foreground = e.Kind == Program.LogData.LogKind.error ? Brushes.Red : Brushes.White;
                    tb.Margin = new Thickness(0.0, 10.0, 0.0, 10.0);

                    sp2.Children.Add(tb);
                })).BeginExecuteInUIThread();

                Loaded += (sender, e) =>
                {
                    Grid grid = new Grid();
                    grid.RowDefinitions.Add(new RowDefinition());
                    grid.RowDefinitions.Add(new RowDefinition());
                    grid.RowDefinitions.Add(new RowDefinition()
                    {
                        Height = GridLength.Auto
                    });
                    grid.ColumnDefinitions.Add(new ColumnDefinition());

                    ScrollViewer sv1 = new ScrollViewer();
                    sv1.VerticalScrollBarVisibility   = ScrollBarVisibility.Auto;
                    sv1.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                    sv1.SetValue(Grid.RowProperty, 0);
                    sv1.SetValue(Grid.ColumnProperty, 0);

                    sp1            = new StackPanel();
                    sp1.Background = Brushes.Black;

                    ScrollViewer sv2 = new ScrollViewer();
                    sv2.VerticalScrollBarVisibility   = ScrollBarVisibility.Auto;
                    sv2.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                    sv2.SetValue(Grid.RowProperty, 1);
                    sv2.SetValue(Grid.ColumnProperty, 0);

                    sp2            = new StackPanel();
                    sp2.Background = Brushes.Black;

                    sv1.Content = sp1;
                    sv2.Content = sp2;

                    TextBox tb = new TextBox();
                    tb.SetValue(Grid.RowProperty, 2);
                    tb.SetValue(Grid.ColumnProperty, 0);

                    grid.Children.Add(sv1);
                    grid.Children.Add(sv2);
                    grid.Children.Add(tb);

                    Content = grid;

                    Console.SetOut(new TextBlockStreamWriter(sp1));

                    _logger.LogAdded += _LoggerLogAdded;

                    //SimulationWindow sw = new SimulationWindow();
                    //sw.ShowDialog();

                    this.StartTask(string.Empty, string.Empty, () =>
                    {
                        //string testPrivateRsaParameters;
                        //using (RSACryptoServiceProvider rsacsp = new RSACryptoServiceProvider(2048))
                        //    testPrivateRsaParameters = rsacsp.ToXmlString(true);


                        //RealInboundChennel ric = new RealInboundChennel(7777, RsaKeySize.rsa2048, 100);
                        //ric.Accepted += (sender2, e2) =>
                        //{
                        //    this.StartTask("", "", () =>
                        //    {
                        //        e2.WriteBytes(BitConverter.GetBytes(true));

                        //        bool b = BitConverter.ToBoolean(e2.ReadBytes(), 0);

                        //        SessionChannel sc = e2.NewSession();
                        //        sc.WriteBytes(BitConverter.GetBytes(true));
                        //        sc.Close();

                        //        //e2.Close();
                        //    });


                        //    //e2.Close();

                        //    //Console.WriteLine("");
                        //};
                        //ric.RequestAcceptanceStart();


                        //AutoResetEvent are = new AutoResetEvent(false);
                        //SocketChannel socketc = null;

                        //RealOutboundChannel roc = new RealOutboundChannel(IPAddress.Loopback, 7777, RsaKeySize.rsa2048, testPrivateRsaParameters);
                        //roc.Connected += (sender2, e2) =>
                        //{
                        //    socketc = e2;
                        //    socketc.Sessioned += (sender3, e3) =>
                        //    {
                        //        bool b3 = BitConverter.ToBoolean(e3.ReadBytes(), 0);

                        //        Console.WriteLine("");
                        //    };

                        //    are.Set();

                        //    //e2.Close();

                        //    //Console.WriteLine("connected");
                        //};
                        //roc.RequestConnection();

                        //are.WaitOne();

                        //bool b2 = BitConverter.ToBoolean(socketc.ReadBytes(), 0);

                        //socketc.WriteBytes(BitConverter.GetBytes(true));

                        //socketc.Close();


                        //CirculatedInteger ci = new CirculatedInteger(5);

                        //Console.WriteLine(ci.GetForward(0));
                        //Console.WriteLine(ci.GetForward(1));
                        //Console.WriteLine(ci.GetForward(2));
                        //Console.WriteLine(ci.GetForward(3));
                        //Console.WriteLine(ci.GetForward(4));
                        //Console.WriteLine(ci.GetForward(5));
                        //Console.WriteLine(ci.GetForward(6));

                        //Console.WriteLine(ci.GetBackward(0));
                        //Console.WriteLine(ci.GetBackward(1));
                        //Console.WriteLine(ci.GetBackward(2));
                        //Console.WriteLine(ci.GetBackward(3));
                        //Console.WriteLine(ci.GetBackward(4));
                        //Console.WriteLine(ci.GetBackward(5));
                        //Console.WriteLine(ci.GetBackward(6));

                        Secp256k1KeyPair <Sha256Hash> secp256k1KeyPair = new Secp256k1KeyPair <Sha256Hash>(true);

                        Sha256Ripemd160Hash address = new Sha256Ripemd160Hash(secp256k1KeyPair.pubKey.pubKey);

                        TransactionInput ti1 = new TransactionInput();
                        ti1.LoadVersion1(0, 0, 0);

                        TransactionOutput to1 = new TransactionOutput();
                        to1.LoadVersion0(address, new Creacoin(50m));

                        CoinbaseTransaction ct1 = new CoinbaseTransaction();
                        ct1.LoadVersion0(new TransactionOutput[] { to1 });

                        byte[] ctBytes1 = ct1.ToBinary();

                        CoinbaseTransaction ct2 = SHAREDDATA.FromBinary <CoinbaseTransaction>(ctBytes1);

                        TransferTransaction tt1 = new TransferTransaction();
                        tt1.LoadVersion1(new TransactionInput[] { ti1 }, new TransactionOutput[] { to1 });
                        tt1.Sign(new TransactionOutput[] { to1 }, new DSAPRIVKEYBASE[] { secp256k1KeyPair.privKey });

                        byte[] ttBytes1 = tt1.ToBinary();

                        TransferTransaction tt2 = SHAREDDATA.FromBinary <TransferTransaction>(ttBytes1);

                        ResTransactions rt1 = new ResTransactions(new Transaction[] { ct1, tt1 });

                        byte[] rtBytes1 = rt1.ToBinary();

                        ResTransactions rt2 = SHAREDDATA.FromBinary <ResTransactions>(rtBytes1);


                        byte[] test1 = SHAREDDATA.ToBinary <Transaction>(ct2);

                        CoinbaseTransaction ct3 = SHAREDDATA.FromBinary <Transaction>(test1) as CoinbaseTransaction;

                        byte[] test2 = SHAREDDATA.ToBinary <Transaction>(tt2);

                        TransferTransaction tt3 = SHAREDDATA.FromBinary <Transaction>(test2) as TransferTransaction;

                        //string pathBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                        //New.BlockManagerDB bmdb = new New.BlockManagerDB(pathBase);
                        //New.BlockDB blkdb = new New.BlockDB(pathBase);
                        //New.BlockFilePointersDB bfpdb = new New.BlockFilePointersDB(pathBase);

                        //New.BlockManager bm = new New.BlockManager(bmdb, blkdb, bfpdb);

                        //New.TestBlock block1 = new New.TestBlock(1);

                        //bm.AddMainBlock(block1);
                        //bm.AddMainBlock(block1);


                        //Test10NodesInv();

                        //TestDHT();

                        //bool isFirst = true;
                        //int portNumber = 0;
                        //CreaNode cnlt = null;
                        //tb.KeyDown += (sender2, e2) =>
                        //{
                        //    if (e2.Key != Key.Enter)
                        //        return;

                        //    if (isFirst)
                        //    {
                        //        portNumber = int.Parse(tb.Text);

                        //        FirstNodeInfosDatabase fnidb = new FirstNodeInfosDatabase(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

                        //        cnlt = new CreaNode((ushort)portNumber, 0, "test", fnidb);
                        //        cnlt.Start();

                        //        cnlt.ReceivedNewChat += (sender3, e3) =>
                        //        {
                        //            this.ConsoleWriteLine(e3.Message);
                        //        };

                        //        isFirst = false;

                        //        return;
                        //    }

                        //    Chat chat = new Chat();
                        //    chat.LoadVersion0(portNumber.ToString(), tb.Text);
                        //    chat.Sign(secp256k1KeyPair.privKey);

                        //    cnlt.DiffuseNewChat(chat);
                        //};
                    });
                };

                Closed += (sender, e) =>
                {
                    _logger.LogAdded -= _LoggerLogAdded;

                    string fileText = string.Empty;
                    foreach (var child in sp1.Children)
                    {
                        fileText += (child as TextBlock).Text + Environment.NewLine;
                    }

                    File.AppendAllText(Path.Combine(new FileInfo(Assembly.GetEntryAssembly().Location).DirectoryName, "LogTest.txt"), fileText);
                };
            }
Beispiel #13
0
 public AddressEvent(Sha256Ripemd160Hash _address)
 {
     address = _address;
 }
Beispiel #14
0
        public void NewTransaction(IAccount iAccount, Sha256Ripemd160Hash address, CurrencyUnit amount, CurrencyUnit fee)
        {
            if (!isSystemStarted)
            {
                throw new InvalidOperationException("core_not_started");
            }

            Account account = iAccount as Account;

            if (account == null)
            {
                throw new ArgumentException("iaccount_type");
            }

            utxodb.Open();

            List <Utxo> utxosList = blockChain.GetAllUtxos(account.Address.Hash);

            utxosList.Sort((a, b) =>
            {
                if (a.blockIndex < b.blockIndex)
                {
                    return(-1);
                }
                else if (a.blockIndex > b.blockIndex)
                {
                    return(1);
                }

                if (a.txIndex < b.txIndex)
                {
                    return(-1);
                }
                else if (a.txIndex > b.txIndex)
                {
                    return(1);
                }

                if (a.txOutIndex < b.txOutIndex)
                {
                    return(-1);
                }
                else if (a.txOutIndex > b.txOutIndex)
                {
                    return(1);
                }

                return(0);
            });
            Utxo[] utxos = utxosList.ToArray();

            utxodb.Close();

            List <TransactionInput> usedTxInList = new List <TransactionInput>();

            foreach (var unconfirmedTh in transactionHistories.unconfirmedTransactionHistories.ToArray())
            {
                for (int i = 0; i < unconfirmedTh.prevTxOuts.Length; i++)
                {
                    if (unconfirmedTh.prevTxOuts[i].Address.Equals(account.Address.Hash))
                    {
                        usedTxInList.Add(unconfirmedTh.transaction.TxInputs[i]);
                    }
                }
            }
            usedTxInList.Sort((a, b) =>
            {
                if (a.PrevTxBlockIndex < b.PrevTxBlockIndex)
                {
                    return(-1);
                }
                else if (a.PrevTxBlockIndex > b.PrevTxBlockIndex)
                {
                    return(1);
                }

                if (a.PrevTxIndex < b.PrevTxIndex)
                {
                    return(-1);
                }
                else if (a.PrevTxIndex > b.PrevTxIndex)
                {
                    return(1);
                }

                if (a.PrevTxOutputIndex < b.PrevTxOutputIndex)
                {
                    return(-1);
                }
                else if (a.PrevTxOutputIndex > b.PrevTxOutputIndex)
                {
                    return(1);
                }

                return(0);
            });
            TransactionInput[] usedTxIns = usedTxInList.ToArray();

            List <Utxo> unusedUtxosList = new List <Utxo>();

            int position = -1;

            for (int i = 0; i < usedTxIns.Length; i++)
            {
                bool flag = false;
                while (position < utxos.Length)
                {
                    position++;

                    if (usedTxIns[i].PrevTxBlockIndex == utxos[position].blockIndex && usedTxIns[i].PrevTxIndex == utxos[position].txIndex && usedTxIns[i].PrevTxOutputIndex == utxos[position].txOutIndex)
                    {
                        flag = true;

                        break;
                    }
                    else
                    {
                        unusedUtxosList.Add(utxos[position]);
                    }
                }

                if (!flag)
                {
                    throw new InvalidOperationException();
                }
            }

            long rawFeeAndAmount = amount.rawAmount + fee.rawAmount;

            List <Utxo> useUtxosList             = new List <Utxo>();
            long        rawFeeAndAmountAndChange = 0;

            bool flag2 = false;

            foreach (var utxo in unusedUtxosList)
            {
                useUtxosList.Add(utxo);

                if ((rawFeeAndAmountAndChange += utxo.amount.rawAmount) > rawFeeAndAmount)
                {
                    flag2 = true;

                    break;
                }
            }

            if (!flag2)
            {
                for (int i = position + 1; i < utxos.Length; i++)
                {
                    useUtxosList.Add(utxos[i]);

                    if ((rawFeeAndAmountAndChange += utxos[i].amount.rawAmount) > rawFeeAndAmount)
                    {
                        flag2 = true;

                        break;
                    }
                }
            }

            if (!flag2)
            {
                throw new InvalidOperationException();
            }

            Utxo[] useUtxos = useUtxosList.ToArray();

            TransactionInput[] txIns = new TransactionInput[useUtxos.Length];
            for (int i = 0; i < txIns.Length; i++)
            {
                txIns[i] = new TransactionInput();
                txIns[i].LoadVersion0(useUtxos[i].blockIndex, useUtxos[i].txIndex, useUtxos[i].txOutIndex, account.Ecdsa256KeyPair.pubKey);
            }

            long rawChange = rawFeeAndAmountAndChange - rawFeeAndAmount;

            TransactionOutput[] txOuts = new TransactionOutput[rawChange == 0 ? 1 : 2];
            txOuts[0] = new TransactionOutput();
            txOuts[0].LoadVersion0(address, amount);
            if (rawChange != 0)
            {
                txOuts[1] = new TransactionOutput();
                txOuts[1].LoadVersion0(account.Address.Hash, new CurrencyUnit(rawChange));
            }

            TransactionOutput[] prevTxOuts = new TransactionOutput[useUtxos.Length];
            for (int i = 0; i < prevTxOuts.Length; i++)
            {
                prevTxOuts[i] = blockChain.GetMainBlock(txIns[i].PrevTxBlockIndex).Transactions[txIns[i].PrevTxIndex].TxOutputs[txIns[i].PrevTxOutputIndex];
            }

            Ecdsa256PrivKey[] privKeys = new Ecdsa256PrivKey[useUtxos.Length];
            for (int i = 0; i < privKeys.Length; i++)
            {
                privKeys[i] = account.Ecdsa256KeyPair.privKey;
            }

            TransferTransaction ttx = new TransferTransaction();

            ttx.LoadVersion0(txIns, txOuts);
            ttx.Sign(prevTxOuts, privKeys);

            creaNodeTest.DiffuseNewTransaction(ttx);
        }