Exemplo n.º 1
0
        public CoinTransaction Send(Dictionary <String, TransactionOutput> chainUtxos, string recipient, decimal amount)
        {
            if (GetBalance(chainUtxos) < amount)
            {
                return(null);
            }

            var     inputs = new List <TransactionInput>();
            decimal sum    = 0;

            foreach (var utxoItem in UTXOs)
            {
                var utxo = utxoItem.Value;
                sum += utxo.Amount;
                inputs.Add(new TransactionInput()
                {
                    TransactionOutputId = utxo.Id
                });
                if (sum > amount)
                {
                    break;
                }
            }

            var tx = new CoinTransaction(PublicKey, recipient, amount, inputs);

            tx.Sign(this);

            foreach (var transactionInput in inputs)
            {
                UTXOs.Remove(transactionInput.TransactionOutputId);
            }

            return(tx);
        }
Exemplo n.º 2
0
        public void Mine(Wallet wallet)
        {
            var txs           = _mempoolRepository.GetNextList();
            var lastBlockHash = _blockRepository.GetLastBlockHash();

            if (lastBlockHash == null)
            {
                lastBlockHash = InsertGenesis(wallet);
            }

            var newBlock = new Block(lastBlockHash);

            foreach (var tx in txs)
            {
                switch (tx.Type)
                {
                case TransactionType.Coins:
                    break;

                case TransactionType.VisitToTheDoctor:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }


            var coinBase         = new Wallet();
            var mainedBlockMoney = new CoinTransaction(coinBase.PublicKey, wallet.PublicKey, 100, null);

            mainedBlockMoney.Sign(coinBase);
            mainedBlockMoney.Id = Guid.NewGuid().ToString();
            mainedBlockMoney.Outputs.Add(
                new TransactionOutput(mainedBlockMoney.Reciepient, mainedBlockMoney.Amount, mainedBlockMoney.Id)
                );
            UTXOs[mainedBlockMoney.Outputs[0].Id] = mainedBlockMoney.Outputs[0];

            newBlock.AddTransaction(UTXOs, mainedBlockMoney);

            newBlock.MineBlock(new Random().Next(0, 5));


            _blockRepository.Add(newBlock);
        }
Exemplo n.º 3
0
        private string InsertGenesis(Wallet wallet)
        {
            var coinBase = new Wallet();
            var genesisCoinTransaction = new CoinTransaction(coinBase.PublicKey, wallet.PublicKey, 100, null);

            genesisCoinTransaction.Sign(coinBase);
            genesisCoinTransaction.Id = "0";
            genesisCoinTransaction.Outputs.Add(
                new TransactionOutput(genesisCoinTransaction.Reciepient, genesisCoinTransaction.Amount, genesisCoinTransaction.Id)
                );
            UTXOs[genesisCoinTransaction.Outputs[0].Id] = genesisCoinTransaction.Outputs[0];

            var genesis = new Block("0");

            genesis.AddTransaction(UTXOs, genesisCoinTransaction);

            _blockRepository.Add(genesis);
            return(_blockRepository.GetLastBlockHash());
        }
Exemplo n.º 4
0
        public static void Test()
        {
            WalletA = new Wallet();
            WalletB = new Wallet();
            var coinBase = new Wallet();

            _genesisCoinTransaction = new CoinTransaction(coinBase.PublicKey, WalletA.PublicKey, 100, null);
            _genesisCoinTransaction.Sign(coinBase);
            _genesisCoinTransaction.Id = "0";
            _genesisCoinTransaction.Outputs.Add(
                new TransactionOutput(_genesisCoinTransaction.Reciepient, _genesisCoinTransaction.Amount, _genesisCoinTransaction.Id)
                );
            UTXOs[_genesisCoinTransaction.Outputs[0].Id] = _genesisCoinTransaction.Outputs[0];

            var genesis = new Block("0");

            genesis.AddTransaction(UTXOs, _genesisCoinTransaction);
            AddBlock(genesis);

            var balance = WalletA.GetBalance(UTXOs);
            var block1  = new Block(genesis.Hash);

            block1.AddTransaction(UTXOs, WalletA.Send(UTXOs, WalletB.PublicKey, 40));
            AddBlock(block1);

            balance = WalletA.GetBalance(UTXOs);
            var block2 = new Block(block1.Hash);

            block2.AddTransaction(UTXOs, WalletA.Send(UTXOs, WalletB.PublicKey, 1000));
            AddBlock(block2);

            balance = WalletA.GetBalance(UTXOs);
            var block3 = new Block(block2.Hash);

            block3.AddTransaction(UTXOs, WalletB.Send(UTXOs, WalletA.PublicKey, 20));
            AddBlock(block3);

            balance = WalletA.GetBalance(UTXOs);
        }