Exemplo n.º 1
0
        public void Stop()
        {
            //stopEvent.Set();

            if (Miner != null)
            {
                Miner.Dispose();
                Miner = null;
            }

            if (_NodeManager != null)
            {
                _NodeManager.Dispose();
                _NodeManager = null;
            }

            if (_BlockChain != null)
            {
                _BlockChain.Dispose();
                _BlockChain = null;
            }

            if (_WalletManager != null)
            {
                _WalletManager.Dispose();
                _WalletManager = null;
            }

            _CanConnect = true;
        }
Exemplo n.º 2
0
        WalletManager GetWalletManager(BlockChain.BlockChain blockChain)
        {
            var walletManager = new WalletManager(blockChain, WalletDB);

            walletManager.OnItems -= WalletOnItemsHandler;             // ensure single registration
            walletManager.OnItems += WalletOnItemsHandler;

            return(walletManager);
        }
Exemplo n.º 3
0
    public void OneTimeSetUp()
    {
        Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        Dispose();
        _GenesisBlock = Utils.GetGenesisBlock();

        _BlockChain    = new BlockChain.BlockChain(BLOCKCHAIN_DB, Merkle.blockHeaderHasher.Invoke(_GenesisBlock.header));
        _WalletManager = new WalletManager(_BlockChain, WALLET_DB);
    }
Exemplo n.º 4
0
        public MinerManager(BlockChain.BlockChain blockChain, Address address)
        {
            TxsPerBlockLimit = 100; //TODO
            Difficulty       = 12;  //TODO: when set to 10, becomes apparent a issue of 'tx race condition' (i.e. when using a secure contract to send token, two subsequent txs are dispatched) in which the miner is reset after mining a block with 1st tx and 2nd is never gets mined

            _Address         = address;
            _BlockChain      = blockChain;
            _Hasher.OnMined += DispatchBlock;
            OwnResource(_Hasher);

            _BlockChainListener = new EventLoopMessageListener <BlockChainMessage>(OnBlockChainMessage, "Miner Consumer");
            OwnResource(MessageProducer <BlockChainMessage> .Instance.AddMessageListener(_BlockChainListener));

            Reset();
        }
Exemplo n.º 5
0
 private static void PrintChain(BlockChain blockChain)
 {
     Console.WriteLine("----------------- Start Blockchain -----------------");
     foreach (Block block in blockChain.Chain)
     {
         Console.WriteLine();
         Console.WriteLine("------ Start Block ------");
         Console.WriteLine("Hash: {0}", block.Hash);
         Console.WriteLine("Previous Hash: {0}", block.PreviousHash);
         Console.WriteLine("--- Start Transactions ---");
         foreach (Transaction transaction in block.Transactions)
         {
             Console.WriteLine("From: {0} To {1} Amount {2}", transaction.From, transaction.To, transaction.Amount);
         }
         Console.WriteLine("--- End Transactions ---");
         Console.WriteLine("------ End Block ------");
     }
     Console.WriteLine("----------------- End Blockchain -----------------");
 }
Exemplo n.º 6
0
        public WalletManager(BlockChain.BlockChain blockChain, string dbName)
        {
            _BlockChain = blockChain;

            _DBContext = new DBContext(dbName);

            _KeyStore = new KeyStore();
            _TxStore  = new TxStore();

            TxDeltaList = new List <TxDelta>();

            _BlockChainListener = new EventLoopMessageListener <BlockChainMessage>(OnBlockChainMessage, "Wallet Consumer");
            OwnResource(MessageProducer <BlockChainMessage> .Instance.AddMessageListener(_BlockChainListener));
            OwnResource(_DBContext);

            using (var dbTx = _DBContext.GetTransactionContext())
            {
                _Keys = _KeyStore.List(dbTx);
                var purgeList = new List <ulong>();

                _TxStore.All(dbTx).ToList().ForEach(item =>
                {
                    switch (item.Item2.TxState)
                    {
                    case TxStateEnum.Confirmed:
                        TxDeltaList.Add(new TxDelta(item.Item2.TxState, item.Item2.TxHash, item.Item2.Tx, item.Item2.AssetDeltas, item.Item2.DateTime));
                        break;

                    case TxStateEnum.Unconfirmed:
                        purgeList.Add(item.Item1);
                        //TODO: implement 'smarter' mode: only after the node has been synced and is up-to-date, try to revalidate
                        //_BlockChain.HandleTransaction(txData.Tx);
                        break;
                    }

                    foreach (var key in purgeList)
                    {
                        _TxStore.Remove(dbTx, key);
                    }
                });
            }
        }
Exemplo n.º 7
0
        public BlockVerificationHelper(
            BlockChain blockChain,
            TransactionContext dbTx,
            byte[] bkHash,
            Types.Block bk,
            bool handleOrphan = false,
            bool handleBranch = false,
            HashDictionary <TransactionValidation.PointedTransaction> confirmedTxs = null,
            HashDictionary <Types.Transaction> invalidatedTxs = null,
            List <QueueAction> queuedActions = null
            )
        {
            ConfirmedTxs   = confirmedTxs ?? new HashDictionary <TransactionValidation.PointedTransaction>();
            UnconfirmedTxs = invalidatedTxs ?? new HashDictionary <Types.Transaction>();            //todo: refactor to set as new obj by default
            QueueActions   = queuedActions ?? new List <QueueAction>();

            _BlockChain = blockChain;
            _DbTx       = dbTx;
            _BkHash     = bkHash;
            _Bk         = bk;

            if (!IsValid())
            {
                BlockChainTrace.Information($"block {_Bk.header.blockNumber} is invalid", _Bk);
                Result = new BkResult(BkResultEnum.Rejected);
                return;
            }

            if (IsInStore())
            {
                var    reject        = false;
                byte[] missingParent = null;
                var    location      = blockChain.BlockStore.GetLocation(dbTx, bkHash);

                switch (location)
                {
                case LocationEnum.Branch:
                    reject = !handleBranch;
                    break;

                case LocationEnum.Orphans:
                    missingParent = GetMissingParent();

                    reject = !handleOrphan && !handleBranch;
                    break;

                default:
                    reject = true;
                    break;
                }

                if (reject)
                {
                    BlockChainTrace.Information($"Block {_Bk.header.blockNumber} already in store ({location})", bk);
                    Result = new BkResult(BkResultEnum.Rejected, missingParent);
                    return;
                }
            }

            if (bk.transactions.Count() == 0)
            {
                BlockChainTrace.Information("empty tx list", bk);
                Result = new BkResult(BkResultEnum.Rejected);
                return;
            }

            if (!IsValidTime())
            {
                BlockChainTrace.Information("invalid time", bk);
                Result = new BkResult(BkResultEnum.Rejected);
                return;
            }

            //TODO:

            /*
             * 3. Transaction list must be non - empty
             * 4. Block hash must satisfy claimed nBits proof of work
             * 5. Block timestamp must not be more than two hours in the future
             * 6. First transaction must be coinbase, the rest must not be
             * 7. For each transaction, apply "tx" checks 2 - 4
             * 8. (omitted)
             * 9. (omitted)
             * 10. Verify Merkle hash
             */

            if (IsGenesis())
            {
                if (!IsGenesisValid())
                {
                    BlockChainTrace.Information("invalid genesis block", bk);
                    Result = new BkResult(BkResultEnum.Rejected);
                    return;
                }
                else
                {
                    blockChain.Timestamps.Init(bk.header.timestamp);
                    ExtendMain(QueueActions, 0, true);
                    Result = new BkResult(BkResultEnum.Accepted);
                    BlockChainTrace.Information("accepted genesis block", bk);
                    return;
                }
            }

            if (IsOrphan())
            {
                var missingParent = GetMissingParent();
                blockChain.BlockStore.Put(dbTx, bkHash, bk, LocationEnum.Orphans, 0);
                BlockChainTrace.Information($"block {_Bk.header.blockNumber} added as orphan", bk);
                Result = new BkResult(BkResultEnum.AcceptedOrphan, missingParent);
                return;
            }

            //12. Check that nBits value matches the difficulty rules

            if (!IsValidDifficulty() || !IsValidBlockNumber() || !IsValidTimeStamp())
            {
                BlockChainTrace.Information($"block {_Bk.header.blockNumber} rejected", bk);
                Result = new BkResult(BkResultEnum.Rejected);
                return;
            }

            //14. For certain old blocks(i.e.on initial block download) check that hash matches known values

            var totalWork = TotalWork();

            UtxoLookup = _BlockChain.UtxoLookupFactory(_DbTx, true);


            if (handleBranch)             // make a branch block main
            {
                if (!ExtendMain(QueueActions, totalWork))
                {
                    Result = new BkResult(BkResultEnum.Rejected);
                    return;
                }
            }
            else if (!IsNewGreatestWork(totalWork))
            {
                blockChain.BlockStore.Put(dbTx, bkHash, bk, LocationEnum.Branch, totalWork);
            }
            else if (blockChain.BlockStore.IsLocation(dbTx, bk.header.parent, LocationEnum.Main))
            {
                if (!ExtendMain(QueueActions, totalWork))
                {
                    BlockChainTrace.Information($"block {_Bk.header.blockNumber} rejected", bk);
                    Result = new BkResult(BkResultEnum.Rejected);
                    return;
                }
            }
            else
            {
                BlockChainTrace.Information($"block {bk.header.blockNumber} extends a branch with new difficulty", bk);

                Reorg();
            }

            BlockChainTrace.Information($"block {bk.header.blockNumber} accepted", bk);
            Result = new BkResult(BkResultEnum.Accepted);
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            Console.WriteLine("BlockChainDemo");

            BlockChain   chain         = new BlockChain();
            ITransaction transaction1  = new Transaction("claim311", 1000.00m, DateTime.Now, "RL29999", 10000, ClaimType.TotalLoss);
            ITransaction transaction2  = new Transaction("Claim342", 2000.00m, DateTime.Now, "KE10492", 20000, ClaimType.TotalLoss);
            ITransaction transaction3  = new Transaction("Claim679", 3000.00m, DateTime.Now, "JS12353", 30000, ClaimType.TotalLoss);
            ITransaction transaction4  = new Transaction("Claim522", 4000.00m, DateTime.Now, "IL82382", 40000, ClaimType.TotalLoss);
            ITransaction transaction5  = new Transaction("Claim419", 5000.00m, DateTime.Now, "AS12312", 50000, ClaimType.TotalLoss);
            ITransaction transaction6  = new Transaction("Claim222", 6000.00m, DateTime.Now, "RJ98765", 60000, ClaimType.TotalLoss);
            ITransaction transaction7  = new Transaction("Claim132", 7000.00m, DateTime.Now, "PK74821", 70000, ClaimType.TotalLoss);
            ITransaction transaction8  = new Transaction("Claim124", 8000.00m, DateTime.Now, "UL15826", 80000, ClaimType.TotalLoss);
            ITransaction transaction9  = new Transaction("claim634", 1000.00m, DateTime.Now, "RL29999", 10000, ClaimType.TotalLoss);
            ITransaction transaction10 = new Transaction("Claim32", 2000.00m, DateTime.Now, "KE10492", 20000, ClaimType.TotalLoss);
            ITransaction transaction11 = new Transaction("Claim6712", 3023.00m, DateTime.Now, "AK12353", 31200, ClaimType.TotalLoss);
            ITransaction transaction12 = new Transaction("Claim5322", 4015.00m, DateTime.Now, "GH82382", 43200, ClaimType.TotalLoss);
            ITransaction transaction13 = new Transaction("Claim4149", 5270.00m, DateTime.Now, "AS12312", 12300, ClaimType.TotalLoss);
            ITransaction transaction14 = new Transaction("Claim2262", 1980.00m, DateTime.Now, "AS12374", 654300, ClaimType.TotalLoss);
            ITransaction transaction15 = new Transaction("Claim1352", 2190.00m, DateTime.Now, "UL98765", 43100, ClaimType.TotalLoss);
            ITransaction transaction16 = new Transaction("Claim1241", 4180.00m, DateTime.Now, "UL13261", 12300, ClaimType.TotalLoss);

            Block block1 = new Block(0);
            Block block2 = new Block(1);
            Block block3 = new Block(2);
            Block block4 = new Block(3);

            block1.AddTransaction(transaction1);
            block1.AddTransaction(transaction2);
            block1.AddTransaction(transaction3);
            block1.AddTransaction(transaction4);

            block2.AddTransaction(transaction5);
            block2.AddTransaction(transaction6);
            block2.AddTransaction(transaction7);
            block2.AddTransaction(transaction8);

            block3.AddTransaction(transaction9);
            block3.AddTransaction(transaction10);
            block3.AddTransaction(transaction11);
            block3.AddTransaction(transaction12);

            block4.AddTransaction(transaction13);
            block4.AddTransaction(transaction14);
            block4.AddTransaction(transaction15);
            block4.AddTransaction(transaction16);

            block1.SetBlockHash(null);
            block2.SetBlockHash(block1);
            block3.SetBlockHash(block2);
            block4.SetBlockHash(block3);

            chain.AcceptBlock(block1);
            chain.AcceptBlock(block2);
            chain.AcceptBlock(block3);
            chain.AcceptBlock(block4);


            chain.VerifyChain();

            Console.WriteLine("");
            Console.WriteLine("");

            transaction5.ClaimNumber = "asdasd";
            chain.VerifyChain();

            Console.WriteLine();
        }
Exemplo n.º 9
0
        /// <summary>
        /// The main method.
        /// </summary>
        /// <param name="args">Some arguments.</param>
        public static void Main(string[] args)
        {
            BlockChain.InitializeChain();

            if (args.Length >= 1)
            {
                Port = int.Parse(args[0]);
            }

            if (args.Length >= 2)
            {
                name = args[1];
            }

            if (Port > 0)
            {
                Server.Start();
            }

            if (name != "Unknown")
            {
                Console.WriteLine($"Current user is {name}.");
            }

            var selection = 0;
            while (selection != 4)
            {
                switch (selection)
                {
                    case 1:
                        Console.WriteLine("Please enter the server URL.");
                        var serverUrl = Console.ReadLine();
                        Client.Connect($"{serverUrl}/{WebSocketUrl}");
                        break;
                    case 2:
                        Console.WriteLine("Please enter the receiver name.");
                        var receiverName = Console.ReadLine();
                        Console.WriteLine("Please enter the amount.");
                        var amount = Console.ReadLine();

                        var transaction = new Transaction
                        {
                            Amount = int.Parse(amount ?? "0"),
                            FromAddress = name,
                            ToAddress = receiverName
                        };

                        BlockChain.CreateTransaction(transaction);
                        BlockChain.ProcessPendingTransactions(name);
                        Client.Broadcast(JsonConvert.SerializeObject(BlockChain));
                        break;
                    case 3:
                        Console.WriteLine("Block chain:");
                        Console.WriteLine(JsonConvert.SerializeObject(BlockChain, Formatting.Indented));
                        break;
                }

                WriteOptions();

                var action = Console.ReadLine();
                selection = int.Parse(action ?? "-1");
            }

            Client.Close();
        }
Exemplo n.º 10
0
 public P2PServer(string ipAddress, int port, BlockChain blockchain)
 {
     _ipAddress  = ipAddress ?? String.Empty;
     _port       = port;
     _blockchain = blockchain ?? new BlockChain(null, null);
 }
Exemplo n.º 11
0
 public BlockChainServer(BlockChain blockChain, string ipAddress, string port)
 {
     this.blockChain = blockChain;
     this.ipAddress  = ipAddress;
     this.port       = port;
 }
Exemplo n.º 12
0
 public P2PClient(BlockChain blockchain)
 {
     _blockchain = blockchain;
 }
Exemplo n.º 13
0
        public WebServer(BlockChain chain)
        {
            //start the web-server
            var    settings = ConfigurationManager.AppSettings;
            string host     = settings["host"]?.Length > 1 ? settings["host"] : "localhost";
            string port     = settings["port"]?.Length > 1 ? settings["port"] : "12345";

            Logger.Log($"Web Service Listening on port: {port}");

            var server = new TinyWebServer.WebServer(request =>
            {
                string path  = request.Url.PathAndQuery.ToLower();
                string query = "";
                string json  = "";

                Logger.Log($"Request:{path}");


                if (path.Contains("?"))
                {
                    string[] parts = path.Split('?');
                    path           = parts[0];
                    query          = parts[1];
                }

                switch (path)
                {
                //GET: http://localhost:12345/mine
                case "/mine":
                    return(chain.Mine(query));

                //POST: http://localhost:12345/transactions/new
                //{ "Amount":123, "Recipient":"ebeabf5cc1d54abdbca5a8fe9493b479", "Sender":"31de2e0ef1cb4937830fcfd5d2b3b24f" }
                case "/transfer":
                    if (request.HttpMethod != HttpMethod.Post.Method)
                    {
                        return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}");
                    }

                    json            = new StreamReader(request.InputStream).ReadToEnd();
                    Transaction trx = JsonConvert.DeserializeObject <Transaction>(json);
                    try
                    {
                        int blockId = chain.CreateTransaction(trx, false);
                        return($"Your transaction will be included in block {blockId}");
                    } catch (System.Exception ex)
                    {
                        return(ex.Message);
                    }

                //GET: http://localhost:12345/chain
                case "/chain":
                    return(chain.GetFullChain());

                //POST: http://localhost:12345/nodes/register
                //{ "Urls": ["localhost:54321", "localhost:54345", "localhost:12321"] }
                case "/nodes/register":
                    if (request.HttpMethod != HttpMethod.Post.Method)
                    {
                        return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}");
                    }

                    json        = new StreamReader(request.InputStream).ReadToEnd();
                    var urlList = new { Urls = new string[0] };
                    var obj     = JsonConvert.DeserializeAnonymousType(json, urlList);
                    return(chain.RegisterNodes(obj.Urls));

                //GET: http://localhost:12345/nodes/resolve
                case "/nodes/resolve":
                    return(chain.Consensus(false));

                case "/balance":
                    if (request.HttpMethod != HttpMethod.Post.Method)
                    {
                        return($"{new HttpResponseMessage(HttpStatusCode.MethodNotAllowed)}");
                    }

                    json = new StreamReader(request.InputStream).ReadToEnd();

                    return(chain.Balance(json));

                case "/history":
                    List <string> history = chain.TransactionHistory(query);
                    return(JsonConvert.SerializeObject(history));

                case "/pending":
                    return(JsonConvert.SerializeObject(chain.PendingTransactions()));

                case "/test/start":
                    Logger.Log($"Test {query} Start ----------------------------------------");
                    return($"Test {query} Start");

                case "/test/end":
                    Logger.Log($"Test {query} End ------------------------------------------");
                    return($"Test {query} end");

                case "/test/init":
                    chain.Init();
                    return($"BlockChain initialized");

                case "/test/checkpoint":
                    return(chain.CheckPoint());

                case "/test/rollback":
                    return(chain.Rollback());

                case "/test/miner/start":
                    string[] cmdArgs = query.Split('&');
                    chain.Miner_Start(cmdArgs[0]);
                    return("Miner started");

                case "/test/miner/stop":
                    chain.Miner_Stop();
                    return("Miner Stopped");
                }

                return("");
            },
                                                     $"http://{host}:{port}/mine/",
                                                     $"http://{host}:{port}/transfer/",
                                                     $"http://{host}:{port}/chain/",
                                                     $"http://{host}:{port}/nodes/register/",
                                                     $"http://{host}:{port}/nodes/resolve/",
                                                     $"http://{host}:{port}/balance/",
                                                     $"http://{host}:{port}/history/",
                                                     $"http://{host}:{port}/pending/",
                                                     $"http://{host}:{port}/test/init/",
                                                     $"http://{host}:{port}/test/start/",
                                                     $"http://{host}:{port}/test/end/",
                                                     $"http://{host}:{port}/test/checkpoint/",
                                                     $"http://{host}:{port}/test/rollback/",
                                                     $"http://{host}:{port}/test/miner/start/",
                                                     $"http://{host}:{port}/test/miner/stop/"
                                                     );

            server.Run();
        }
Exemplo n.º 14
0
        static void Main(string[] args)
        {
            Console.WriteLine("Bem Vindo ao BlockChain Structure");

            int difficulty = 1;

            do
            {
                Console.WriteLine("Escolha uma dificuldade para sua blockchain entre 1 e 5: ");
                Console.Write(">");

                if (!int.TryParse(Console.ReadLine(), out difficulty))
                {
                    difficulty = 0;
                }
            } while (difficulty <= 0 || difficulty > 5);

            Console.WriteLine("Digite o valor do bloco Gênesis");
            Console.Write(">");
            var genesis = new Block <string>(Console.ReadLine(), difficulty);
            var chain   = new BlockChain <string>(genesis);

            int opcao = 0;

            do
            {
                Console.WriteLine("Escolha dentre uma das opções abaixo");
                Console.WriteLine("1 - Adicionar Bloco");
                Console.WriteLine("2 - Ver último bloco");
                Console.WriteLine("3 - Ver primeiro bloco");
                Console.WriteLine("4 - Ver todos os blocos");
                Console.WriteLine("5 - Ver dificuldade");
                Console.WriteLine("9 - Limpar  Console");
                Console.WriteLine("0 - Sair");
                Console.Write(">");

                if (!int.TryParse(Console.ReadLine(), out opcao))
                {
                    opcao = -1;
                }

                switch (opcao)
                {
                case 1:
                    Console.WriteLine("Digite os dados do bloco");
                    Console.Write(">");
                    chain.AddBlock(Console.ReadLine());
                    Console.WriteLine();
                    Console.WriteLine("O bloco inserido foi: ");
                    chain.GetLastBlock().PrintBlock();
                    Console.WriteLine();
                    break;

                case 2:
                    Console.WriteLine("O último bloco é: ");
                    chain.GetLastBlock().PrintBlock();
                    Console.WriteLine();
                    break;

                case 3:
                    Console.WriteLine("O primeiro bloco é: ");
                    chain.GetGenesisBlock().PrintBlock();
                    Console.WriteLine();
                    break;

                case 4:
                    chain.PrintChain();
                    Console.WriteLine();
                    break;

                case 5:
                    Console.WriteLine("A dificuldade dessa BlockChain é: {0}", difficulty);
                    Console.WriteLine();
                    break;

                case 9:
                    Console.Clear();
                    break;
                }
            } while (opcao > 0);
            Console.WriteLine("Byeeee");
        }