Ejemplo n.º 1
0
        public void AddAccounts_OneLayer_GetRange()
        {
            SortedList <Keccak, TrieNode> list = new();

            for (int i = 0; i < 100; i++)
            {
                byte[]   accountBytes = TestItem.GenerateIndexedAccountRlp(i);
                Keccak   randomKeccak = TestItem.GetRandomKeccak();
                TrieNode leaf         = TrieNodeFactory.CreateLeaf(new HexPrefix(true), accountBytes);

                list.Add(randomKeccak, leaf);
            }

            SnapStorage storage = new();

            foreach (var item in list)
            {
                storage.AddLeafNode(item.Key, item.Value, 1001);
            }

            Keccak startingHash = list.Keys[20];
            Keccak endHash      = list.Keys[29];

            var range1 = storage.GetRange(startingHash, endHash, 1001);
            var range2 = storage.GetRange(startingHash, endHash, 1002);

            Assert.AreEqual(10, range1.Length);
            Assert.IsTrue(AreEqual(20, 29, list, range1));

            Assert.AreEqual(10, range2.Length);
            Assert.IsTrue(AreEqual(20, 29, list, range2));
        }
Ejemplo n.º 2
0
        private RequestDispatcher BuildRequestDispatcher(IModuleContainer container)
        {
            var moduleCatalog = new ModuleCatalog(
                () => { return(container.GetAllModules()); },
                (Type moduleType) => { return(container.GetModule(moduleType)); }
                );

            var routeSegmentExtractor    = new RouteSegmentExtractor();
            var routeDescriptionProvider = new RouteDescriptionProvider();
            var routeCache = new RouteCache(routeSegmentExtractor, routeDescriptionProvider);

            routeCache.BuildCache(moduleCatalog.GetAllModules());

            var trieNodeFactory = new TrieNodeFactory();
            var routeTrie       = new RouteResolverTrie(trieNodeFactory);

            routeTrie.BuildTrie(routeCache);

            var serializers = new List <ISerializer>()
            {
                new JsonSerializer(), new XmlSerializer()
            };
            var responseFormatterFactory = new ResponseFormatterFactory(serializers);
            var moduleBuilder            = new ModuleBuilder(responseFormatterFactory);

            var routeResolver = new RouteResolver(moduleCatalog, moduleBuilder, routeTrie);

            var negotiator        = new ResponseNegotiator();
            var routeInvoker      = new RouteInvoker(negotiator);
            var requestDispatcher = new RequestDispatcher(routeResolver, routeInvoker);

            return(requestDispatcher);
        }
Ejemplo n.º 3
0
        public void AddAccounts_MultipleLayers_GetRange()
        {
            SortedList <Keccak, TrieNode> list = new();

            for (int i = 0; i < 100; i++)
            {
                byte[]   accountBytes = TestItem.GenerateIndexedAccountRlp(i);
                Keccak   randomKeccak = TestItem.GetRandomKeccak();
                TrieNode leaf         = TrieNodeFactory.CreateLeaf(new HexPrefix(true), accountBytes);

                list.Add(randomKeccak, leaf);
            }

            SnapStorage storage = new();

            // create bottom layer
            foreach (var item in list)
            {
                storage.AddLeafNode(item.Key, item.Value, 1001);
            }

            // block 1002
            byte[]   updateAccountBytes_1002 = TestItem.GenerateRandomAccountRlp();
            TrieNode updateAccount_1002      = TrieNodeFactory.CreateLeaf(new HexPrefix(true), updateAccountBytes_1002);

            storage.AddLeafNode(list.Keys[21], updateAccount_1002, 1002);

            byte[]   newAccountBytes_1002 = TestItem.GenerateRandomAccountRlp();
            TrieNode newAccount_1002      = TrieNodeFactory.CreateLeaf(new HexPrefix(true), newAccountBytes_1002);
            Keccak   newAddress           = TestItem.GetRandomKeccak();

            while (newAddress <= list.Keys[21] || newAddress >= list.Keys[22])
            {
                newAddress = TestItem.GetRandomKeccak();
            }
            storage.AddLeafNode(newAddress, newAccount_1002, 1002);

            // block 1003
            storage.AddLeafNode(list.Keys[21], null, 1003);


            Keccak startingHash = list.Keys[20];
            Keccak endHash      = list.Keys[30];

            var range_1001 = storage.GetRange(startingHash, endHash, 1001);
            var range_1002 = storage.GetRange(startingHash, endHash, 1002);
            var range_1003 = storage.GetRange(startingHash, endHash, 1003);

            Assert.AreEqual(11, range_1001.Length);
            Assert.IsTrue(AreEqual(20, 30, list, range_1001));

            Assert.AreEqual(12, range_1002.Length);
            Assert.AreEqual(range_1002[1].node, updateAccount_1002);
            Assert.AreEqual(range_1002[2].node, newAccount_1002);

            Assert.AreEqual(11, range_1003.Length);
            Assert.AreEqual(range_1003[1].node, newAccount_1002);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TrieNode"/> class
        /// </summary>
        /// <param name="parent">Parent node</param>
        /// <param name="segment">Segment of the route definition</param>
        /// <param name="nodeFactory">Factory for creating new nodes</param>
        protected TrieNode(TrieNode parent, string segment, TrieNodeFactory nodeFactory)
        {
            this.nodeFactory            = nodeFactory;
            this.Parent                 = parent;
            this.RouteDefinitionSegment = segment;

            this.Children             = new Dictionary <string, TrieNode>();
            this.AdditionalParameters = new Dictionary <string, object>();
            this.NodeData             = new List <NodeData>();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TrieNode"/> class
        /// </summary>
        /// <param name="parent">Parent node</param>
        /// <param name="segment">Segment of the route definition</param>
        /// <param name="nodeFactory">Factory for creating new nodes</param>
        protected TrieNode(TrieNode parent, string segment, TrieNodeFactory nodeFactory)
        {
            this.nodeFactory = nodeFactory;
            this.Parent = parent;
            this.RouteDefinitionSegment = segment;

            this.Children = new Dictionary<string, TrieNode>();
            this.AdditionalParameters = new Dictionary<string, object>();
            this.NodeData = new List<NodeData>();
        }
        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);
        }
Ejemplo n.º 7
0
        public void Extension_can_accept_visitors()
        {
            ITreeVisitor     visitor = Substitute.For <ITreeVisitor>();
            TrieVisitContext context = new();
            TrieNode         ignore  = TrieNodeFactory.CreateLeaf(HexPrefix.Leaf("ccc"), Array.Empty <byte>());
            TrieNode         node    = TrieNodeFactory.CreateExtension(HexPrefix.Extension("aa"), ignore);

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

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

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

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

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

            visitor.Received().VisitLeaf(node, context, node.Value);
        }
Ejemplo n.º 10
0
        public void Extension_with_leaf_can_be_visited()
        {
            Context      ctx     = new();
            ITreeVisitor visitor = Substitute.For <ITreeVisitor>();

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

            TrieVisitContext context = new();
            TrieNode         node    = TrieNodeFactory.CreateExtension(HexPrefix.Extension("aa"), ctx.AccountLeaf);

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

            visitor.Received().VisitExtension(node, context);
            visitor.Received().VisitLeaf(ctx.AccountLeaf, context, ctx.AccountLeaf.Value);
        }
Ejemplo n.º 11
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(100);
                AccountDecoder decoder = new();

                AccountLeaf = TrieNodeFactory.CreateLeaf(
                    HexPrefix.Leaf("bbb"),
                    decoder.Encode(account).Bytes);
            }
Ejemplo n.º 12
0
 public GreedyCaptureNode(TrieNode parent, string segment, TrieNodeFactory nodeFactory)
     : base(parent, segment, nodeFactory)
 {
     this.GetParameterName();
 }
Ejemplo n.º 13
0
 public CaptureNodeWithDefaultValue(TrieNode parent, string segment, TrieNodeFactory nodeFactory)
     : base(parent, segment, nodeFactory)
 {
     this.ExtractParameterNameAndDefaultValue();
 }
Ejemplo n.º 14
0
 public RegExNode(TrieNode parent, string segment, TrieNodeFactory nodeFactory)
     : base(parent, segment, nodeFactory)
 {
     this.BuildRegEx();
 }
Ejemplo n.º 15
0
 public RegExNode(TrieNode parent, string segment, TrieNodeFactory nodeFactory)
     : base(parent, segment, nodeFactory)
 {
     this.BuildRegEx();
 }
Ejemplo n.º 16
0
 public OptionalCaptureNode(TrieNode parent, string segment, TrieNodeFactory nodeFactory)
     : base(parent, segment, nodeFactory)
 {
     this.ExtractParameterName();
 }
Ejemplo n.º 17
0
 public OptionalCaptureNode(TrieNode parent, string segment, TrieNodeFactory nodeFactory)
     : base(parent, segment, nodeFactory)
 {
     this.ExtractParameterName();
 }
Ejemplo n.º 18
0
 public CaptureNodeWithDefaultValue(TrieNode parent, string segment, TrieNodeFactory nodeFactory)
     : base(parent, segment, nodeFactory)
 {
     this.ExtractParameterNameAndDefaultValue();
 }
Ejemplo n.º 19
0
 public LiteralNode(TrieNode parent, string segment, TrieNodeFactory nodeFactory)
     : base(parent, segment, nodeFactory)
 {
 }
Ejemplo n.º 20
0
 public GreedyRegExCaptureNode(TrieNode parent, string segment, TrieNodeFactory nodeFactory)
     : base(parent, segment, nodeFactory)
 {
     this.BuildRegEx();
 }
Ejemplo n.º 21
0
 public LiteralNode(TrieNode parent, string segment, TrieNodeFactory nodeFactory)
     : base(parent, segment, nodeFactory)
 {
 }
Ejemplo n.º 22
0
 public GreedyCaptureNode(TrieNode parent, string segment, TrieNodeFactory nodeFactory)
     : base(parent, segment, nodeFactory)
 {
     this.GetParameterName();
 }
Ejemplo n.º 23
0
 public RootNode(TrieNodeFactory nodeFactory)
     : base(null, null, nodeFactory)
 {
 }