Exemplo n.º 1
0
        private byte[] GenerateRandomAccountRlp()
        {
            Account account = GenerateRandomAccount();

            byte[] value = _accountDecoder.Encode(account).Bytes;
            return(value);
        }
Exemplo n.º 2
0
        public void Can_read_hashes_only()
        {
            Account        account = new Account(100).WithChangedCodeHash(TestItem.KeccakA).WithChangedStorageRoot(TestItem.KeccakB);
            AccountDecoder decoder = new AccountDecoder();
            Rlp            rlp     = decoder.Encode(account);

            (Keccak codeHash, Keccak storageRoot) = decoder.DecodeHashesOnly(new RlpStream(rlp.Bytes));
            Assert.AreEqual(codeHash, TestItem.KeccakA);
            Assert.AreEqual(storageRoot, TestItem.KeccakB);
        }
Exemplo n.º 3
0
        public void Roundtrip_test()
        {
            Account        account = new Account(100).WithChangedCodeHash(TestItem.KeccakA).WithChangedStorageRoot(TestItem.KeccakB);
            AccountDecoder decoder = new AccountDecoder();
            Rlp            rlp     = decoder.Encode(account);
            Account        decoded = decoder.Decode(new RlpStream(rlp.Bytes));

            Assert.AreEqual((int)decoded.Balance, 100);
            Assert.AreEqual((int)decoded.Nonce, 0);
            Assert.AreEqual(decoded.CodeHash, TestItem.KeccakA);
            Assert.AreEqual(decoded.StorageRoot, TestItem.KeccakB);
        }
Exemplo n.º 4
0
        public void Leaf_with_contract_without_storage_and_empty_code_can_accept_visitors()
        {
            ITreeVisitor     visitor = Substitute.For <ITreeVisitor>();
            TrieVisitContext context = new TrieVisitContext();
            PatriciaTree     tree    = new PatriciaTree();
            Account          account = new Account(1, 100, Keccak.EmptyTreeHash, Keccak.OfAnEmptyString);
            AccountDecoder   decoder = new AccountDecoder();
            TrieNode         node    = new TrieNode(NodeType.Leaf);

            node.Value = decoder.Encode(account).Bytes;

            node.Accept(visitor, tree, context);

            visitor.Received().VisitLeaf(node, context, node.Value);
        }
Exemplo n.º 5
0
        public void Leaf_with_simple_account_can_accept_visitors()
        {
            ITreeVisitor     visitor = Substitute.For <ITreeVisitor>();
            TrieVisitContext context = new TrieVisitContext();
            PatriciaTree     tree    = new PatriciaTree();
            Account          account = new Account(100);
            AccountDecoder   decoder = new AccountDecoder();
            TrieNode         node    = new TrieNode(NodeType.Leaf);

            node.Value = decoder.Encode(account).Bytes;

            node.Accept(visitor, tree, context);

            visitor.Received().VisitLeaf(node, context, node.Value);
        }
Exemplo n.º 6
0
        public void Setup()
        {
            _tiniestLeaf       = new TrieNode(NodeType.Leaf);
            _tiniestLeaf.Key   = new HexPrefix(true, 5);
            _tiniestLeaf.Value = new byte[] { 10 };

            _heavyLeaf       = new TrieNode(NodeType.Leaf);
            _heavyLeaf.Key   = new HexPrefix(true, new byte[20]);
            _heavyLeaf.Value = Keccak.EmptyTreeHash.Bytes.Concat(Keccak.EmptyTreeHash.Bytes).ToArray();

            Account        account = new Account(100);
            AccountDecoder decoder = new AccountDecoder();

            _accountLeaf       = new TrieNode(NodeType.Leaf);
            _accountLeaf.Value = decoder.Encode(account).Bytes;
        }
Exemplo n.º 7
0
            public Context()
            {
                TiniestLeaf       = new TrieNode(NodeType.Leaf);
                TiniestLeaf.Key   = new HexPrefix(true, 5);
                TiniestLeaf.Value = new byte[] { 10 };

                HeavyLeaf       = new TrieNode(NodeType.Leaf);
                HeavyLeaf.Key   = new HexPrefix(true, new byte[20]);
                HeavyLeaf.Value = Keccak.EmptyTreeHash.Bytes.Concat(Keccak.EmptyTreeHash.Bytes).ToArray();

                Account        account = new Account(100);
                AccountDecoder decoder = new AccountDecoder();

                AccountLeaf = TrieNodeFactory.CreateLeaf(
                    HexPrefix.Leaf("bbb"),
                    decoder.Encode(account).Bytes);
            }
        public async Task Can_read_dependent_items_from_state_db_while_waiting_for_dependencies()
        {
            StateDb codeDb  = new StateDb();
            StateDb stateDB = new StateDb();
            MemDb   tempDb  = new MemDb();

            SyncConfig syncConfig = new SyncConfig();

            syncConfig.FastSync = true;

            IBlockTree    blockTree = Substitute.For <IBlockTree>();
            ISyncPeerPool pool      = Substitute.For <ISyncPeerPool>();

            SyncProgressResolver syncProgressResolver = new SyncProgressResolver(blockTree, NullReceiptStorage.Instance, stateDB, new MemDb(), syncConfig, LimboLogs.Instance);
            ISyncModeSelector    syncModeSelector     = new MultiSyncModeSelector(syncProgressResolver, pool, syncConfig, LimboLogs.Instance);
            StateSyncFeed        stateSyncFeed        = new StateSyncFeed(codeDb, stateDB, tempDb, syncModeSelector, blockTree, LimboLogs.Instance);

            // so we want to setup a trie in a structure of -> branch into two leaves
            // so we can respond with the branch node and with leaves missing
            // and we can prove that we can read the branch from the temp DB while it is still missing from the State DB

            AccountDecoder accountDecoder = new AccountDecoder();

            TrieNode leaf   = TrieNodeFactory.CreateLeaf(new HexPrefix(true, new byte[] { 1, 2, 3 }), accountDecoder.Encode(Account.TotallyEmpty).Bytes);
            TrieNode branch = TrieNodeFactory.CreateBranch();

            branch.SetChild(1, leaf);
            branch.ResolveKey(true);

            // PatriciaTree tree = new PatriciaTree();
            // tree = new PatriciaTree();
            // tree.Set(branch.Keccak.Bytes, branch.Value);

            stateSyncFeed.ResetStateRoot(0, branch.Keccak);

            var request = await stateSyncFeed.PrepareRequest();

            BuildRequestAndHandleResponse(branch, request, stateSyncFeed);

            byte[] value = tempDb.Get(branch.Keccak);
            value.Should().BeEquivalentTo(branch.FullRlp);

            byte[] valueFromState = stateDB.Get(branch.Keccak);
            valueFromState.Should().BeNull();

            request = await stateSyncFeed.PrepareRequest();

            BuildRequestAndHandleResponse(leaf, request, stateSyncFeed);

            value = tempDb.Get(branch.Keccak);
            value.Should().BeNull();

            valueFromState = stateDB.Get(branch.Keccak);
            valueFromState.Should().BeEquivalentTo(branch.FullRlp);
        }
Exemplo n.º 9
0
        public void Leaf_with_contract_with_storage_and_without_code_can_accept_visitors()
        {
            ITreeVisitor visitor = Substitute.For <ITreeVisitor>();

            visitor.ShouldVisit(Arg.Any <Keccak>()).Returns(true);

            TrieVisitContext context = new TrieVisitContext();
            Account          account = new Account(1, 100, Keccak.Zero, Keccak.OfAnEmptyString);
            AccountDecoder   decoder = new AccountDecoder();
            TrieNode         node    = TrieNodeFactory.CreateLeaf(HexPrefix.Leaf("aa"), decoder.Encode(account).Bytes);

            node.Accept(visitor, NullTrieNodeResolver.Instance, context);

            visitor.Received().VisitLeaf(node, context, node.Value);
        }
Exemplo n.º 10
0
        public void Leaf_with_simple_account_can_accept_visitors()
        {
            ITreeVisitor     visitor = Substitute.For <ITreeVisitor>();
            TrieVisitContext context = new TrieVisitContext();
            Account          account = new Account(100);
            AccountDecoder   decoder = new AccountDecoder();
            TrieNode         node    = TrieNodeFactory.CreateLeaf(HexPrefix.Leaf("aa"), decoder.Encode(account).Bytes);

            node.Accept(visitor, NullTrieNodeResolver.Instance, context);

            visitor.Received().VisitLeaf(node, context, node.Value);
        }