Ejemplo n.º 1
0
        public void ShouldRejectBroadcastedTransaction()
        {
            var p = new TestTransactionPool();

            p.Add("test1", 1);
            p.Add("test2", 0);
            p.Spend("test2", "test1", 1);

            p.Render();

            //ShouldRejectNewTx_DueToReferencedOutputDoesNotExist_DueToMissingOutputIndex

            var  waitForTransaction = new AutoResetEvent(false);
            bool gotTransaction     = false;

            BroadcastTransaction(
                hub =>
            {
                Trace.Information("-- Sending transaction from node group");
                hub.BroadcastTransactionAsync(p["test1"].Value);
                Thread.Sleep(1500);                         //TODO: use reset events instead of sleep?
                hub.BroadcastTransactionAsync(p["test2"].Value);

                Assert.True(waitForTransaction.WaitOne(15000));
                Assert.True(gotTransaction);
            }, () =>
            {
                gotTransaction = true;
                waitForTransaction.Set();
            }
                );
        }
Ejemplo n.º 2
0
        public void CanBroadcastTransaction()
        {
            var p = new TestTransactionPool();

            p.Add("test1", 1);
            p.Render();

            var waitForTransaction = new AutoResetEvent(false);
            var gotTransaction     = false;

            BroadcastTransaction(
                hub =>
            {
                Trace.Information("-- Sending transaction from node group");
                hub.BroadcastTransactionAsync(p["test1"].Value);
                //	Thread.Sleep(1500);
                //	hub.BroadcastTransactionAsync(p["test2"].Value);
            }, () =>
            {
                gotTransaction = true;
                waitForTransaction.Set();
            }
                );

            Assert.True(waitForTransaction.WaitOne(20000));
            Assert.True(gotTransaction);
        }
Ejemplo n.º 3
0
        public void CanSerDesTx()
        {
            var p = new TestTransactionPool();

            p.Add("test", 1);
            p.Render();

            var test = p.TakeOut("test").Value;

            var data = Merkle.serialize <Types.Transaction>(test);

            var t = Serialization.context.GetSerializer <Types.Transaction>().UnpackSingleObject(data);

            Assert.IsTrue(t.Equals(test));
        }
Ejemplo n.º 4
0
        public void ShouldSyncBlockChainAgainstLocal()
        {
            var network = new TestNetwork();

            network.AddSeed(new NetworkAddress(new IPEndPoint(IPAddress.Parse("192.168.2.101"), 9999)));

            var p = new TestTransactionPool();

            p.Add("t1", 1);
            p.Add("t2", 0);
            p.Spend("t2", "t1", 0);

            p.Render();

            var genesisBlock = new TestBlock(p.TakeOut("t1").Value);
            var block1       = new TestBlock(p.TakeOut("t2").Value);

            block1.Parent = genesisBlock;

            genesisBlock.Render();
            block1.Render();

            WithBlockChains(1, genesisBlock.Value.Key, blockChains =>
            {
                //	blockChains[0].HandleNewBlock(genesisBlock.Value.Value);
                //	blockChains[0].HandleNewBlock(block1.Value.Value);


                AutoResetEvent waitForConnection = new AutoResetEvent(false);
                bool connected = false;

                blockChains[0].OnAddedToStore += transaction =>
                {
                    Trace.Information("-- Transaction Received (node server)");
                    //	actionReceiver();
                };

                AddressManager addressManager = new AddressManager();
                addressManager.PeersToFind    = 1;
                NodeConnectionParameters nodesGroupParameters = new NodeConnectionParameters();
//				nodesGroupParameters.AddressFrom = servers[1].ExternalEndpoint;
                nodesGroupParameters.TemplateBehaviors.Add(new AddressManagerBehavior(addressManager));
                nodesGroupParameters.TemplateBehaviors.Add(new ChainBehavior(blockChains[0]));
                nodesGroupParameters.TemplateBehaviors.Add(new BroadcastHubBehavior());
                nodesGroupParameters.TemplateBehaviors.Add(new SPVBehavior(blockChains[0], BroadcastHub.GetBroadcastHub(nodesGroupParameters.TemplateBehaviors)));

                NodesGroup nodesGroup            = new NodesGroup(network, nodesGroupParameters);
                nodesGroup.AllowSameGroup        = true;
                nodesGroup.MaximumNodeConnection = 1;
                nodesGroup.ConnectedNodes.Added += (object sender, NodeEventArgs e) =>
                {
                    Trace.Information("-- Node added to node group");
                    connected = true;
                    waitForConnection.Set();
                };
                nodesGroup.Connect();


                Assert.True(waitForConnection.WaitOne(10000));                 //TODO: use reset events instead of sleep
                Assert.True(connected);



                //TODO
                Thread.Sleep(40000);



                //					actionSender(BroadcastHub.GetBroadcastHub(nodesGroup.NodeConnectionParameters));

                Trace.Information("-- Done");
            });
        }
Ejemplo n.º 5
0
        //private byte[] genesisBlockHash = new byte[] { 0x01, 0x02 };

        public ConsoleNode()
        {
            var p = new TestTransactionPool();

            p.Add("t1", 1);
            p.Add("t2", 0);
            p.Add("t3", 0);
            p.Spend("t2", "t1", 0);

            p.Render();

            var genesisBlock = new TestBlock(p.TakeOut("t1").Value);
            var block1       = new TestBlock(p.TakeOut("t2").Value, p.TakeOut("t3").Value);

            block1.Parent = genesisBlock;

            genesisBlock.Render();
            block1.Render();


            Settings settings = JsonLoader <Settings> .Instance.Value;

            var ipEndpoint = new System.Net.IPEndPoint(IPAddress.Parse(settings.ExternalIPAddress), settings.ServerPort);

            var blockChain = new BlockChain.BlockChain("db", genesisBlock.Value.Key);

            if (blockChain.GetBlock(genesisBlock.Value.Key) == null)
            {
                Console.WriteLine("Initializing blockchain...");
                blockChain.HandleNewBlock(genesisBlock.Value.Value);
                blockChain.HandleNewBlock(block1.Value.Value);
            }
            else
            {
                Console.WriteLine("Blockchain initialized.");
            }

            OwnResource(blockChain);


            blockChain.OnAddedToMempool += transaction =>
            {
                Console.WriteLine("\n** Got Transaction **\n");
            };

            var server = new Server(this, ipEndpoint, BetaNetwork.Instance);


            var addressManagerBehavior = server.Behaviors.Find <AddressManagerBehavior>();

            var addressManager = addressManagerBehavior.AddressManager;

            addressManager.Add(
                new NetworkAddress(ipEndpoint),
                ipEndpoint.Address
                );

            addressManager.Connected(new NetworkAddress(ipEndpoint));

            var broadcastHubBehavior = new BroadcastHubBehavior();

            Miner miner = new Miner(blockChain);

            OwnResource(miner);


            server.Behaviors.Add(broadcastHubBehavior);
            server.Behaviors.Add(new SPVBehavior(blockChain, broadcastHubBehavior.BroadcastHub));
            server.Behaviors.Add(new MinerBehavior(miner));
            server.Behaviors.Add(new ChainBehavior(blockChain));

            server.Start();
        }