Beispiel #1
0
        public override Block <T> GetBlock <T>(HashDigest <SHA256> blockHash)
        {
            var blockFile = new FileInfo(GetBlockPath(blockHash));

            if (!blockFile.Exists)
            {
                return(null);
            }

            using (Stream stream = blockFile.OpenRead())
            {
                var                 formatter    = new BencodexFormatter <RawBlock>();
                RawBlock            rawBlock     = (RawBlock)formatter.Deserialize(stream);
                HashDigest <SHA256>?previousHash = (rawBlock.PreviousHash != null) ?
                                                   new HashDigest <SHA256>?(new HashDigest <SHA256>(rawBlock.PreviousHash)) :
                                                   null;
                return(new Block <T>(
                           index: rawBlock.Index,
                           difficulty: rawBlock.Difficulty,
                           nonce: new Nonce(rawBlock.Nonce),
                           rewardBeneficiary: new Address(rawBlock.RewardBeneficiary),
                           previousHash: previousHash,
                           timestamp: DateTime.ParseExact(
                               rawBlock.Timestamp,
                               Block <T> .TimestampFormat,
                               CultureInfo.InvariantCulture
                               ).ToUniversalTime(),
                           transactions: GetTransactions <T>(rawBlock.Transactions)
                           ));
            }
        }
        public void Execute(int index)
        {
            var localPos = index.To3D(RawSize);
            var worldPos = localPos + position;

            var minEdges = localPos == 0;
            var maxEdges = localPos == RawSize - 1;

            var value = 0f;

            var pos = (float2)(localPos + position).xz;
            var n1  = ((1f + noise.snoise((float3)(localPos + position) * 0.025f)) / 2f) * 6f;
            var n   = (1f + noise.snoise(pos * 0.01f)) * .5f * 32;

            value = n - worldPos.y - n1;

            // if(minEdges.x || minEdges.y || minEdges.z || maxEdges.x || maxEdges.y || maxEdges.z)
            //     value = -1;

            blocks[index] = new RawBlock
            {
                type  = 0,
                value = value,
            };
        }
Beispiel #3
0
 public Task Init(Block block, RawBlock rawBlock)
 {
     Block        = block;
     AppState     = Cache.AppState.Get();
     NextProtocol = rawBlock.Metadata.NextProtocol;
     return(Task.CompletedTask);
 }
Beispiel #4
0
        public static async Task <BlockCommit> Apply(ProtocolHandler proto, RawBlock rawBlock)
        {
            var commit = new BlockCommit(proto);
            await commit.Init(rawBlock);

            await commit.Apply();

            return(commit);
        }
Beispiel #5
0
 public async Task Init(RawBlock rawBlock)
 {
     Block = new Block
     {
         Id        = Cache.AppState.NextOperationId(),
         Hash      = rawBlock.Hash,
         Level     = rawBlock.Level,
         Protocol  = await Cache.Protocols.GetAsync(rawBlock.Protocol),
         Timestamp = rawBlock.Header.Timestamp,
         Events    = BlockEvents.ProtocolBegin | BlockEvents.ProtocolEnd
     };
 }
Beispiel #6
0
        /// <inheritdoc/>
        public Block <T> GetBlock <T>(HashDigest <SHA256> blockHash)
            where T : IAction, new()
        {
            LiteFileInfo file =
                _db.FileStorage.FindById(BlockFileId(blockHash));

            if (file is null)
            {
                return(null);
            }

            using (var stream = new MemoryStream())
            {
                file.CopyTo(stream);
                stream.Seek(0, SeekOrigin.Begin);

                var                 formatter    = new BencodexFormatter <RawBlock>();
                RawBlock            rawBlock     = (RawBlock)formatter.Deserialize(stream);
                HashDigest <SHA256>?previousHash = null;

                if (rawBlock.PreviousHash != null)
                {
                    previousHash =
                        new HashDigest <SHA256>(rawBlock.PreviousHash);
                }

                return(new Block <T>(
                           index: rawBlock.Index,
                           difficulty: rawBlock.Difficulty,
                           nonce: new Nonce(rawBlock.Nonce),
                           miner: new Address(rawBlock.Miner),
                           previousHash: previousHash,
                           timestamp: DateTimeOffset.ParseExact(
                               rawBlock.Timestamp,
                               Block <T> .TimestampFormat,
                               CultureInfo.InvariantCulture
                               ).ToUniversalTime(),
                           transactions: GetTransactions <T>(rawBlock.Transactions)
                           ));
            }
        }
Beispiel #7
0
        internal override RawBlock?GetRawBlock(HashDigest <SHA256> blockHash)
        {
            if (_blockCache.TryGetValue(blockHash, out RawBlock cahcedBlock))
            {
                return(cahcedBlock);
            }

            UPath path = BlockPath(blockHash);

            if (!_blocks.FileExists(path))
            {
                return(null);
            }

            RawBlock rawBlock;

            try
            {
                var value = new Codec().Decode(_blocks.ReadAllBytes(path));
                if (!(value is Bencodex.Types.Dictionary dict))
                {
                    throw new DecodingException(
                              $"Expected {typeof(Bencodex.Types.Dictionary)} but " +
                              $"{value.GetType()}");
                }

                rawBlock = new RawBlock(dict);
            }
            catch (FileNotFoundException)
            {
                return(null);
            }

            _blockCache.AddOrUpdate(blockHash, rawBlock);
            return(rawBlock);
        }
 public override RawBlock VisitRawBlock(RawBlock rawBlock)
 {
     rawBlock = rawBlockDelegate?.Invoke(rawBlock) ?? rawBlock;
     return(base.VisitRawBlock(rawBlock));
 }
Beispiel #9
0
 public abstract void pushBlock(RawBlock rawBlock);
Beispiel #10
0
        public static async Task <StateCommit> Apply(ProtocolHandler proto, Block block, RawBlock rawBlock)
        {
            var commit = new StateCommit(proto);
            await commit.Init(block, rawBlock);

            await commit.Apply();

            return(commit);
        }