Example #1
0
        public static async Task GenerateNewBlockAsync(AppDbContext dbContext, IAppLogger logger,
                                                       LedgerOptions options)
        {
            // get the last block from db?
            var parentBlock = await dbContext.Blocks.SingleAsync(b => b.ChildBlockId == null);

            var childBlock = new Block();

            childBlock.ParentBlockId = parentBlock.BlockId;

            // payload
            childBlock.CreatedAt  = DateTime.Now;
            childBlock.Originator = options.PublicKey;
            childBlock.Content    = "CHILD BLOCK " + Guid.NewGuid().ToString();

            // payload signature
            childBlock.Signature = childBlock.GetPayloadSignature(options.PrivateKey);

            childBlock.LocalCreatedAt = childBlock.CreatedAt;
            childBlock.BlockId        = childBlock.GetHash();

            parentBlock.ChildBlockId = childBlock.BlockId;

            dbContext.Blocks.Add(childBlock);

            await dbContext.SaveChangesAsync();
        }
Example #2
0
        private void InitRequestLedgerOptions()
        {
            var options = new LedgerOptions();

            options.LedgerIndex = 330853;
            pgRequestLedgerOptions.SelectedObject = options;
        }
Example #3
0
        public void TestRequestLedger_Invalid()
        {
            var deferred = new Task(() => { });
            MessageResult <LedgerResponse> response = null;
            var options = new LedgerOptions();

            options.LedgerHash = "abcxyz";
            _remote.RequestLedger(options).Submit(r =>
            {
                response = r;
                deferred.Start();
            });

            Assert.IsTrue(deferred.Wait(DeferredWaitingTime));

            Assert.IsNotNull(response);
            Assert.IsInstanceOfType(response.Exception, typeof(InvalidHashException));
        }
Example #4
0
        public void TestRequestLedger()
        {
            var deferred = new Task(() => { });

            _remote = new Remote(ServerUrl);
            MessageResult <LedgerResponse> response = null;

            _remote.Connect(r =>
            {
                // closed ledger
                var req = _remote.RequestLedger();
                req.Submit(r1 =>
                {
                    response = r1;
                    deferred.Start();
                });
            });
            Assert.IsTrue(deferred.Wait(DeferredWaitingTime));

            Assert.IsNotNull(response);
            var ledger = response.Result.Ledger;

            Assert.IsNotNull(ledger);
            Assert.IsTrue(ledger.LedgerIndex > 0);
            Assert.IsNotNull(ledger.LedgerHash);

            var ledgerIndex = ledger.LedgerIndex;
            var ledgerHash  = ledger.LedgerHash;

            deferred = new Task(() => { });
            response = null;
            var options = new LedgerOptions();

            options.LedgerIndex = ledgerIndex;
            _remote.RequestLedger(options).Submit(r =>
            {
                response = r;
                deferred.Start();
            });
            Assert.IsTrue(deferred.Wait(DeferredWaitingTime * 2));

            Assert.IsNotNull(response);
            ledger = response.Result.Ledger;
            Assert.IsNotNull(ledger);
            Assert.AreEqual(ledgerIndex, ledger.LedgerIndex);
            Assert.AreEqual(ledgerHash, ledger.LedgerHash);

            deferred             = new Task(() => { });
            response             = null;
            options.Accounts     = true;
            options.Transactions = true;
            _remote.RequestLedger(options).Submit(r =>
            {
                response = r;
                deferred.Start();
            });
            Assert.IsTrue(deferred.Wait(DeferredWaitingTime * 2));

            Assert.IsNotNull(response);
            ledger = response.Result.Ledger;
            Assert.IsNotNull(ledger);
            Assert.AreEqual(ledgerIndex, ledger.LedgerIndex);
            Assert.AreEqual(ledgerHash, ledger.LedgerHash);
            Assert.IsNotNull(ledger.Transactions);  // has transactions data
            Assert.IsNotNull(ledger.AccountStates); // has accountStates data
        }
Example #5
0
        public static async void LoadInitialSettingsFromJsonFile(AppDbContext ctx, IAppLogger log,
                                                                 string jsonDataFile, LedgerOptions options)
        {
            // validate inputs
            if (ctx == null)
            {
                throw new ArgumentNullException(nameof(AppDbContext));
            }
            if (string.IsNullOrWhiteSpace(jsonDataFile))
            {
                throw new ArgumentNullException(nameof(jsonDataFile));
            }
            if (!File.Exists(jsonDataFile))
            {
                throw new FileNotFoundException(nameof(jsonDataFile), jsonDataFile);
            }

            // import data into db, get public and private key

            // add ourselves as first in list
            await ctx.Hosts.AddAsync(new Host()
            {
                Addr       = options.Addr,
                Port       = options.Port,
                LastSeenDT = DateTime.Now
            });


            using (var file = File.OpenText(jsonDataFile))
            {
                var serializer = new JsonSerializer();

                var settings = (HostSettings)serializer.Deserialize(file, typeof(HostSettings));

                foreach (var host in settings.Hosts)
                {
                    await ctx.Hosts.AddAsync(host);
                }
            }

            await ctx.SaveChangesAsync();

            await log.InfoAsync(
                "startup - Keys",
                $"Public: {Program.PublicKey} Private: {Program.PrivateKey}"
                );

            await log.InfoAsync(
                "startup - import hosts",
                JsonConvert.SerializeObject(await ctx.Hosts.ToListAsync(), Formatting.None)
                );

            // generate GENESIS block and start to synchronize with others

            var genesisBlock = new Block();

            genesisBlock.ParentBlockId = null;
            genesisBlock.ChildBlockId  = null;

            // payload
            genesisBlock.CreatedAt  = DateTime.Now;
            genesisBlock.Originator = options.PublicKey;
            genesisBlock.Content    = "GENESIS BLOCK " + options.Port;

            // payload signature
            genesisBlock.Signature = genesisBlock.GetPayloadSignature(Program.PrivateKey);

            genesisBlock.LocalCreatedAt = genesisBlock.CreatedAt;
            genesisBlock.BlockId        = genesisBlock.GetHash();


            await ctx.Blocks.AddAsync(genesisBlock);

            await ctx.SaveChangesAsync();

            await log.InfoAsync(
                "startup - created GENESIS block",
                JsonConvert.SerializeObject(await ctx.Blocks.FirstAsync(), Formatting.None)
                );
        }