Example #1
0
        private static async Task <Node> RecycleNode(Node oldNode, ILogger logger)
        {
            var conf  = oldNode.Conf;
            var id    = oldNode.Id;
            var key   = oldNode.Controller.Key;
            var peers = oldNode.PeerSelector.Peers();

            IStore store = null;

            if (oldNode.Store is InmemStore)
            {
                store = new InmemStore(oldNode.Store.Participants().participants.Clone(), conf.CacheSize, logger);
            }

            if (oldNode.Store is LocalDbStore)
            {
                (store, _) = await LocalDbStore.Load(conf.CacheSize, conf.StorePath, logger);
            }

            Assert.NotNull(store);

            await oldNode.Trans.CloseAsync();

            var trans = await((InMemRouterTransport)oldNode.Trans).Router.Register(oldNode.LocalAddr);

            var prox = new InMemAppProxy(id, logger);

            var newNode = new Node(conf, id, key, peers, store, trans, prox, logger);

            var err = await newNode.Init(true);

            Assert.Null(err);

            return(newNode);
        }
Example #2
0
        private static async Task <(CngKey[] keys, Node[] nodes)> InitNodes(int n, int cacheSize, int syncLimit, string storeType, string dbPath, ILogger logger)
        {
            var(keys, peers, pmap) = InitPeers(n);

            var nodes = new List <Node>();

            var proxies = new List <InMemAppProxy>();

            var router = new InMemRouter();

            for (var i = 0; i < peers.Length; i++)
            {
                var conf = new Config(TimeSpan.FromMilliseconds(5), TimeSpan.FromSeconds(1), cacheSize, syncLimit, storeType, $"{dbPath}/db_{i}");

                var trans = await router.Register(peers[i].NetAddr);

                IStore    store = null;
                Exception err;
                switch (storeType)
                {
                case "badger":
                    (store, err) = await LocalDbStore.New(pmap, conf.CacheSize, conf.StorePath, logger);

                    Assert.Null(err);
                    break;

                case "inmem":
                    store = new InmemStore(pmap, conf.CacheSize, logger);
                    break;

                default:
                    throw new NotImplementedException();
                }

                var id    = pmap[peers[i].PubKeyHex];
                var proxy = new InMemAppProxy(id, logger);
                var node  = new Node(conf, id, keys[i], peers,
                                     store,
                                     trans,
                                     proxy, logger);

                err = await node.Init(false);

                Assert.Null(err);

                nodes.Add(node);
                proxies.Add(proxy);
            }

            return(keys.ToArray(), nodes.ToArray());
        }
Example #3
0
        public async Task TestAddTransaction()
        {
            var(keys, peers, pmap) = InitPeers(2);

            var config = Config.TestConfig();

            //Start two nodes

            var router = new InMemRouter();

            var id0        = pmap[peers[0].PubKeyHex];
            var peer0Trans = await router.Register(peers[0].NetAddr);

            var peer0Proxy = new InMemAppProxy(id0, logger);
            var node0      = new Node(config, id0, keys[0], peers, new InmemStore(pmap, config.CacheSize, logger), peer0Trans, peer0Proxy, logger);
            await node0.Init(false);

            await node0.StartAsync(false);

            var id1        = pmap[peers[1].PubKeyHex];
            var peer1Trans = await router.Register(peers[1].NetAddr);

            var peer1Proxy = new InMemAppProxy(id1, logger);
            var node1      = new Node(config, id1, keys[1], peers, new InmemStore(pmap, config.CacheSize, logger), peer1Trans, peer1Proxy, logger);
            await node1.Init(false);

            await node1.StartAsync(false);

            //Submit a Tx to node0

            var message = "Hello World!";
            await peer0Proxy.SubmitTx(message.StringToBytes());

            await node0.AddingTransactionsCompleted();

            //simulate a SyncRequest from node0 to node1

            var node0Known = await node0.Controller.KnownEvents();

            var args = new SyncRequest
            {
                FromId = node0.Id,
                Known  = node0Known
            };

            Exception    err;
            SyncResponse resp;

            (resp, err) = await peer0Trans.Sync(peers[1].NetAddr, args);

            Assert.Null(err);

            err = await node0.Sync(resp.Events);

            Assert.Null(err);

            ////check the Tx was removed from the transactionPool and added to the new Head
            Assert.Empty(node0.Controller.TransactionPool);

            var(node0Head, _) = await node0.Controller.GetHead();

            Assert.Single(node0Head.Transactions());

            Assert.Equal(message, node0Head.Transactions()[0].BytesToString());

            node0.Shutdown();
            node1.Shutdown();
        }