public void Can_do_roundtrip_regression([Values(true, false)] bool valueDecoder)
        {
            BlockDecoder decoder = new BlockDecoder();

            byte[] bytes = Bytes.FromHexString(regression5644);
            Rlp.ValueDecoderContext valueDecoderContext = new Rlp.ValueDecoderContext(bytes);
            Block decoded = valueDecoder ? decoder.Decode(ref valueDecoderContext) : decoder.Decode(new RlpStream(bytes));
            Rlp   encoded = decoder.Encode(decoded);

            Assert.AreEqual(encoded.Bytes.ToHexString(), encoded.Bytes.ToHexString());
        }
        public void Can_do_roundtrip_regression()
        {
            BlockDecoder decoder = new BlockDecoder();

            Block decoded = decoder.Decode(new RlpStream(Bytes.FromHexString(regression5644)));
            Rlp   encoded = decoder.Encode(decoded);

            Assert.AreEqual(encoded.Bytes.ToHexString(), encoded.Bytes.ToHexString());
        }
Beispiel #3
0
        // TODO: use headers store or some simplified RLP decoder for number only or hash to number store
        private BigInteger LoadNumberOnly(Keccak blockHash)
        {
            Block block = _blockCache.Get(blockHash);

            if (block != null)
            {
                return(block.Number);
            }

            byte[] blockData = _blockDb.Get(blockHash);
            if (blockData == null)
            {
                throw new InvalidOperationException($"Not able to retrieve block number for an unknown block {blockHash}");
            }

            block = _blockDecoder.Decode(blockData.AsRlpContext(), RlpBehaviors.AllowExtraData);
            _blockCache.Set(blockHash, block);
            return(block.Number);
        }
        public void Can_do_roundtrip_scenarios()
        {
            BlockDecoder decoder = new BlockDecoder();

            foreach (Block block in _scenarios)
            {
                Rlp   encoded  = decoder.Encode(block);
                Block decoded  = decoder.Decode(new RlpStream(encoded.Bytes));
                Rlp   encoded2 = decoder.Encode(decoded);
                Assert.AreEqual(encoded.Bytes.ToHexString(), encoded2.Bytes.ToHexString());
            }
        }
        public void Can_do_roundtrip_scenarios([Values(true, false)] bool valueDecoder)
        {
            BlockDecoder decoder = new BlockDecoder();

            foreach (Block block in _scenarios)
            {
                Rlp   encoded             = decoder.Encode(block);
                var   valueDecoderContext = new Rlp.ValueDecoderContext(encoded.Bytes);
                Block decoded             = valueDecoder ? decoder.Decode(ref valueDecoderContext) : decoder.Decode(new RlpStream(encoded.Bytes));
                Rlp   encoded2            = decoder.Encode(decoded);
                Assert.AreEqual(encoded.Bytes.ToHexString(), encoded2.Bytes.ToHexString());
            }
        }
Beispiel #6
0
        private (Block Block, BlockInfo Info, ChainLevelInfo Level) Load(Keccak blockHash)
        {
            if (blockHash == null || blockHash == Keccak.Zero)
            {
                return(null, null, null);
            }

            Block block = _blockCache.Get(blockHash);

            if (block == null)
            {
                byte[] data = _blockDb.Get(blockHash);
                if (data == null)
                {
                    return(null, null, null);
                }

                block = _blockDecoder.Decode(data.AsRlpContext(), RlpBehaviors.AllowExtraData);
                if (ShouldCache(block.Number))
                {
                    _blockCache.Set(blockHash, block);
                    _headerCache.Set(blockHash, block.Header);
                }
            }

            (BlockInfo blockInfo, ChainLevelInfo level) = LoadInfo(block.Number, block.Hash);
            if (level == null || blockInfo == null)
            {
                // TODO: this is here because storing block data is not transactional
                // TODO: would be great to remove it, he?
                SetTotalDifficulty(block.Header);
                blockInfo = new BlockInfo(block.Hash, block.TotalDifficulty.Value);
                try
                {
                    _blockInfoLock.EnterWriteLock();
                    UpdateOrCreateLevel(block.Number, blockInfo);
                }
                finally
                {
                    _blockInfoLock.ExitWriteLock();
                }

                (blockInfo, level) = LoadInfo(block.Number, block.Hash);
            }
            else
            {
                block.Header.TotalDifficulty = blockInfo.TotalDifficulty;
            }

            return(block, blockInfo, level);
        }
        public void Init(string path)
        {
            var dbOnTheRocks = new BlocksRocksDb(path, new DbConfig(), LimboLogs.Instance);
            var blocksBytes  = dbOnTheRocks.GetAll();
            var blockDecoder = new BlockDecoder();
            var blocks       = blocksBytes
                               .Select(b => blockDecoder.Decode(b.Value.AsRlpStream()))
                               .OrderBy(b => b.Number)
                               .ToList();

            var window = new Window("Blocks")
            {
                X = 0, Y = 10, Width = Dim.Fill(), Height = Dim.Fill()
            };

            if (!blocks.Any())
            {
                MessageBox.Query(40, 7, "Info", "No data.");
                window.FocusPrev();
                return;
            }

            var y = 1;

            foreach (var block in blocks)
            {
                var blockBtn = new Button(1, y++, $"Number: {block.Number}, Hash: {block.Hash}");


                blockBtn.Clicked = () =>
                {
                    var blockDetailsWindow = new Window("Block details")
                    {
                        X = 0, Y = 10, Width = Dim.Fill(), Height = Dim.Fill()
                    };
                    Application.Top.Add(blockDetailsWindow);
                    var serializer = new EthereumJsonSerializer();
                    var blockLbl   = new Label(1, 1, serializer.Serialize(block, true));
                    blockDetailsWindow.Add(blockLbl);
                    Application.Run(blockDetailsWindow);
                };
                window.Add(blockBtn);
            }

            Application.Top.Add(window);
            Application.Run(window);
        }