Exemplo n.º 1
0
        public static Dictionary MarshalBlockMetadata(IBlockMetadata metadata)
        {
            string timestamp =
                metadata.Timestamp.ToString(TimestampFormat, CultureInfo.InvariantCulture);
            Dictionary dict = Dictionary.Empty
                              .Add(IndexKey, metadata.Index)
                              .Add(TimestampKey, timestamp)
                              .Add(DifficultyKey, metadata.Difficulty)
                              .Add(TotalDifficultyKey, (IValue) new Integer(metadata.TotalDifficulty));

            if (metadata.ProtocolVersion != 0)
            {
                dict = dict.Add(ProtocolVersionKey, metadata.ProtocolVersion);
            }

            if (metadata.PreviousHash is { } prev)
            {
                dict = dict.Add(PreviousHashKey, prev.ByteArray);
            }

            if (metadata.TxHash is { } th)
            {
                dict = dict.Add(TxHashKey, th.ByteArray);
            }

            dict = metadata.PublicKey is { } pubKey
                ? dict.Add(PublicKeyKey, pubKey.Format(compress: true))
                : dict.Add(MinerKey, metadata.Miner.ByteArray);

            return(dict);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Creates a <see cref="PreEvaluationBlockHeader"/> instance with its
 /// <paramref name="metadata"/> and a valid proof-of-work <paramref name="nonce"/> which
 /// satisfies the required <see cref="Difficulty"/>.
 /// </summary>
 /// <param name="metadata">Block's metadata.</param>
 /// <param name="hashAlgorithm">The hash algorithm used for calculating
 /// <see cref="PreEvaluationHash"/>.</param>
 /// <param name="nonce">A valid proof-of-work nonce which satisfies the required
 /// <see cref="Difficulty"/>.</param>
 /// <exception cref="InvalidBlockProtocolVersionException">Thrown when
 /// the <paramref name="metadata"/>'s to set is <see cref="IBlockMetadata.ProtocolVersion"/>
 /// is less than 0, or greater than <see cref="BlockMetadata.CurrentProtocolVersion"/>,
 /// the latest known protocol version.</exception>
 /// <exception cref="InvalidBlockIndexException">Thrown when the <paramref name="metadata"/>
 /// has a negative <see cref="IBlockMetadata.Index"/>.</exception>
 /// <exception cref="InvalidBlockDifficultyException">Thrown when
 /// the <paramref name="metadata"/>'s <see cref="IBlockMetadata.Difficulty"/> is negative.
 /// </exception>
 /// <exception cref="InvalidBlockTotalDifficultyException">Thrown when
 /// the <paramref name="metadata"/>'s <see cref="IBlockMetadata.TotalDifficulty"/> is less
 /// than its <see cref="IBlockMetadata.Difficulty"/>.</exception>
 /// <exception cref="InvalidBlockNonceException">Thrown when the given proof-of-work
 /// <paramref name="nonce"/> does not satisfy the required <see cref="Difficulty"/>.
 /// </exception>
 /// <remarks><see cref="PreEvaluationHash"/> is automatically derived from the given
 /// arguments.</remarks>
 public PreEvaluationBlockHeader(
     IBlockMetadata metadata,
     HashAlgorithmType hashAlgorithm,
     Nonce nonce
     )
     : this(new BlockMetadata(metadata), hashAlgorithm, nonce)
 {
 }
Exemplo n.º 3
0
 /// <summary>
 /// Creates a <see cref="PreEvaluationBlockHeader"/> instance with its
 /// <paramref name="metadata"/>, a valid proof-of-work <paramref name="nonce"/> which
 /// satisfies the required <see cref="Difficulty"/>, and
 /// a <paramref name="preEvaluationHash"/> digest derived from them.
 /// </summary>
 /// <param name="metadata">Block's metadata.</param>
 /// <param name="hashAlgorithm">The hash algorithm used for calculating
 /// <see cref="PreEvaluationHash"/>.</param>
 /// <param name="nonce">A valid proof-of-work nonce which satisfies the required
 /// <see cref="Difficulty"/>.</param>
 /// <param name="preEvaluationHash">The hash digest of the <paramref name="hashAlgorithm"/>
 /// derived from the given arguments.</param>
 /// <exception cref="InvalidBlockProtocolVersionException">Thrown when
 /// the <paramref name="metadata"/>'s to set is <see cref="IBlockMetadata.ProtocolVersion"/>
 /// is less than 0, or greater than <see cref="BlockMetadata.CurrentProtocolVersion"/>,
 /// the latest known protocol version.</exception>
 /// <exception cref="InvalidBlockIndexException">Thrown when the <paramref name="metadata"/>
 /// has a negative <see cref="IBlockMetadata.Index"/>.</exception>
 /// <exception cref="InvalidBlockDifficultyException">Thrown when
 /// the <paramref name="metadata"/>'s <see cref="IBlockMetadata.Difficulty"/> is negative.
 /// </exception>
 /// <exception cref="InvalidBlockTotalDifficultyException">Thrown when
 /// the <paramref name="metadata"/>'s <see cref="IBlockMetadata.TotalDifficulty"/> is less
 /// than its <see cref="IBlockMetadata.Difficulty"/>.</exception>
 /// <exception cref="InvalidBlockPreEvaluationHashException">Thrown when the given
 /// <paramref name="preEvaluationHash"/> is invalid.</exception>
 /// <exception cref="InvalidBlockNonceException">Thrown when the given proof-of-work
 /// <paramref name="nonce"/> does not satisfy the required <see cref="Difficulty"/>.
 /// </exception>
 public PreEvaluationBlockHeader(
     IBlockMetadata metadata,
     HashAlgorithmType hashAlgorithm,
     Nonce nonce,
     ImmutableArray <byte> preEvaluationHash
     )
     : this(new BlockMetadata(metadata), hashAlgorithm, nonce, preEvaluationHash)
 {
 }
Exemplo n.º 4
0
        public VulkanMemoryBlock(VulkanMemoryAllocator allocator, VulkanMemoryPool?pool, int memoryTypeIndex, DeviceMemory memory, uint id, IBlockMetadata metaObject)
        {
            Allocator       = allocator;
            ParentPool      = pool;
            MemoryTypeIndex = memoryTypeIndex;
            DeviceMemory    = memory;
            ID = id;

            MetaData = metaObject;
        }
Exemplo n.º 5
0
 public BlockMetadata(IBlockMetadata metadata)
 {
     ProtocolVersion = metadata.ProtocolVersion;
     Index           = metadata.Index;
     Timestamp       = metadata.Timestamp;
     Miner           = metadata.Miner;
     PublicKey       = metadata.PublicKey;
     Difficulty      = metadata.Difficulty;
     TotalDifficulty = metadata.TotalDifficulty;
     PreviousHash    = metadata.PreviousHash;
     _txHash         = metadata.TxHash;
 }
Exemplo n.º 6
0
 public static void AssertBlockMetadataEqual(IBlockMetadata expected, IBlockMetadata actual)
 {
     Assert.NotNull(expected);
     Assert.NotNull(actual);
     Assert.Equal(expected.ProtocolVersion, actual.ProtocolVersion);
     Assert.Equal(expected.Index, actual.Index);
     Assert.Equal(expected.Timestamp, actual.Timestamp);
     AssertBytesEqual(expected.Miner, actual.Miner);
     Assert.Equal(expected.Difficulty, actual.Difficulty);
     Assert.Equal(expected.TotalDifficulty, actual.TotalDifficulty);
     AssertBytesEqual(expected.PreviousHash, actual.PreviousHash);
     AssertBytesEqual(expected.TxHash, actual.TxHash);
 }
Exemplo n.º 7
0
        private ViewEngineResult FindFirstView(IBlockMetadata blockMetadata, IEnumerable <string> templates, ViewContext viewContext, out ICollection <string> searchedLocations)
        {
            searchedLocations = new List <string>();

            foreach (var template in templates)
            {
                var viewName   = string.Concat("BlockTemplates/", blockMetadata.SystemName, "/", template);
                var viewResult = ViewEngines.Engines.FindPartialView(viewContext, viewName);
                searchedLocations.AddRange(viewResult.SearchedLocations);
                if (viewResult.View != null)
                {
                    return(viewResult);
                }
            }

            return(null);
        }
Exemplo n.º 8
0
 public static void ValidateTimestamp(
     this IBlockMetadata blockMetadata,
     DateTimeOffset currentTime
     )
 {
     if (currentTime + TimestampThreshold < blockMetadata.Timestamp)
     {
         string hash = blockMetadata is IBlockExcerpt h
             ? $" {h.Hash}"
             : string.Empty;
         throw new InvalidBlockTimestampException(
                   $"The block #{blockMetadata.Index}{hash}'s timestamp " +
                   $"({blockMetadata.Timestamp}) is later than now ({currentTime}, " +
                   $"threshold: {TimestampThreshold})."
                   );
     }
 }
Exemplo n.º 9
0
        private static ViewEngineResult FindFirstView(IBlockMetadata blockMetadata, IEnumerable <string> templates, ActionContext actionContext, out ICollection <string> searchedLocations)
        {
            searchedLocations = new List <string>();

            var viewEngine = actionContext.HttpContext.RequestServices.GetRequiredService <IRazorViewEngine>();

            foreach (var template in templates)
            {
                var viewName   = string.Concat("BlockTemplates/", blockMetadata.SystemName, "/", template);
                var viewResult = viewEngine.FindView(actionContext, viewName, false);
                searchedLocations.AddRange(viewResult.SearchedLocations);
                if (viewResult.View != null)
                {
                    return(viewResult);
                }
            }

            return(null);
        }
Exemplo n.º 10
0
 public BlockContent(IBlockMetadata metadata)
     : this(metadata, Enumerable.Empty <Transaction <T> >())
 {
 }
Exemplo n.º 11
0
 public BlockContent(IBlockMetadata metadata, IEnumerable <Transaction <T> > transactions)
     : base(metadata)
 {
     Transactions = transactions.ToImmutableArray();
     TxHash       = metadata.TxHash;
 }
Exemplo n.º 12
0
 public static void ValidateTimestamp(this IBlockMetadata blockMetadata) =>
 ValidateTimestamp(blockMetadata, DateTimeOffset.UtcNow);
Exemplo n.º 13
0
 public static BlockMetadata Copy(this IBlockMetadata metadata) =>
 new BlockMetadata(metadata);