Example #1
0
        public void Serialize(Peer peer)
        {
            Bencodex.Types.Dictionary serialized = peer.ToBencodex();
            var deserialized = new Peer(serialized);

            Assert.Equal(peer, deserialized);
        }
        /// <summary>
        /// Gathers <see cref="Transaction{T}"/>s for mining a next block
        /// from the current set of staged <see cref="Transaction{T}"/>s.
        /// </summary>
        /// <param name="metadata">The metadata of the block to be mined.</param>
        /// <param name="maxTransactions">The maximum number of <see cref="Transaction{T}"/>s
        /// allowed.</param>
        /// <param name="maxTransactionsPerSigner">The maximum number of
        /// <see cref="Transaction{T}"/>s with the same signer allowed.</param>
        /// <param name="txPriority">An optional comparer for give certain transactions to
        /// priority to belong to the block.  No certain priority by default.</param>
        /// <returns>An <see cref="ImmutableList"/> of <see cref="Transaction{T}"/>s with its
        /// count not exceeding <paramref name="maxTransactions"/> and the number of
        /// <see cref="Transaction{T}"/>s in the list for each signer not exceeding
        /// <paramref name="maxTransactionsPerSigner"/>.</returns>
        internal ImmutableList <Transaction <T> > GatherTransactionsToMine(
            BlockMetadata metadata,
            int maxTransactions,
            int maxTransactionsPerSigner,
            IComparer <Transaction <T> > txPriority = null
            )
        {
            long index = Count;
            ImmutableList <Transaction <T> > stagedTransactions = ListStagedTransactions(txPriority);

            _logger.Information(
                "Gathering transactions to mine for block #{Index} from {TransactionsCount} " +
                "staged transactions...",
                index,
                stagedTransactions.Count);

            var transactionsToMine = new List <Transaction <T> >();

            // FIXME: The tx collection timeout should be configurable.
            DateTimeOffset timeout = DateTimeOffset.UtcNow + TimeSpan.FromSeconds(4);

            HashAlgorithmType hashAlgorithm = Policy.GetHashAlgorithm(index);

            // Makes an empty block payload to estimate the length of bytes without transactions.
            // FIXME: We'd better to estimate only transactions rather than the whole block.
            var dumbSig = metadata.PublicKey is null
                ? (ImmutableArray <byte>?)null
                : ImmutableArray.Create(new byte[71]);

            Bencodex.Types.Dictionary marshaledEmptyBlock = MarshalBlock(
                marshaledBlockHeader: MarshalBlockHeader(
                    marshaledPreEvaluatedBlockHeader: MarshalPreEvaluationBlockHeader(
                        marshaledMetadata: MarshalBlockMetadata(metadata),
                        nonce: default,
Example #3
0
 public CombinationMail(Bencodex.Types.Dictionary serialized)
     : base(serialized)
 {
 }
Example #4
0
        public void Balance(
            [Option('v', Description = "Print more logs.")]
            bool verbose,
            [Option('s', Description = "Path to the chain store.")]
            string storePath,
            [Option(
                 'b',
                 Description = "Optional block hash/index offset to query balances at.  " +
                               "Tip by default.")]
            string block = null,
            [Option('c', Description = "Optional chain ID.  Default is the canonical chain ID.")]
            Guid?chainId = null,
            [Argument(Description = "Account address.")]
            string address = null
            )
        {
            using Logger logger = Utils.ConfigureLogger(verbose);
            TextWriter stderr = Console.Error;

            (BlockChain <NCAction> chain, IStore store) =
                Utils.GetBlockChain(logger, storePath, chainId);

            Block <NCAction> offset = Utils.ParseBlockOffset(chain, block);

            stderr.WriteLine("The offset block: #{0} {1}.", offset.Index, offset.Hash);

            Bencodex.Types.Dictionary goldCurrencyStateDict = (Bencodex.Types.Dictionary)
                                                              chain.GetState(GoldCurrencyState.Address);
            GoldCurrencyState goldCurrencyState = new GoldCurrencyState(goldCurrencyStateDict);
            Currency          gold = goldCurrencyState.Currency;

            if (address is {} addrStr)
            {
                Address            addr    = Utils.ParseAddress(addrStr);
                FungibleAssetValue balance = chain.GetBalance(addr, gold, offset.Hash);
                Console.WriteLine("{0}\t{1}", addr, balance);
                return;
            }

            var printed = new HashSet <Address>();

            foreach (BlockHash blockHash in chain.BlockHashes)
            {
                BlockDigest digest = GetBlockDigest(store, blockHash);
                stderr.WriteLine("Scanning block #{0} {1}...", digest.Index, digest.Hash);
                stderr.Flush();
                IEnumerable <Address> addrs = digest.TxIds
                                              .Select(txId => store.GetTransaction <NCAction>(new TxId(txId.ToArray())))
                                              .SelectMany(tx => tx.Actions
                                                          .Select(a => a.InnerAction)
                                                          .SelectMany(a => a is TransferAsset t
                            ? new[] { t.Sender, t.Recipient }
                            : a is InitializeStates i &&
                                                                      i.GoldDistributions is Bencodex.Types.List l
                            ? l.OfType <Bencodex.Types.Dictionary>()
                                                                      .Select(d => new GoldDistribution(d).Address)
                            : new Address[0]))
                                              .Append(digest.Miner);
                foreach (Address addr in addrs)
                {
                    if (!printed.Contains(addr))
                    {
                        FungibleAssetValue balance = chain.GetBalance(addr, gold, offset.Hash);
                        Console.WriteLine("{0}\t{1}", addr, balance);
                        printed.Add(addr);
                    }
                }
            }
        }
Example #5
0
 public DailyRewardMail(Bencodex.Types.Dictionary serialized)
     : base(serialized)
 {
 }
Example #6
0
 public SellCancelMail(Bencodex.Types.Dictionary serialized)
     : base(serialized)
 {
 }
Example #7
0
 public BuyerMail(Bencodex.Types.Dictionary serialized)
     : base(serialized)
 {
 }
Example #8
0
 public ItemEnhanceMail(Bencodex.Types.Dictionary serialized)
     : base(serialized)
 {
 }
Example #9
0
 public static void ExportBlock(Block <PolymorphicAction <ActionBase> > block, string path)
 {
     Bencodex.Types.Dictionary dict = block.MarshalBlock();
     byte[] encoded = new Codec().Encode(dict);
     File.WriteAllBytes(path, encoded);
 }