public static TrieNode CreateExtension(HexPrefix key) { TrieNode node = new TrieNode(NodeType.Extension); node.Key = key; return(node); }
public void Cannot_change_key_on_sealed() { TrieNode trieNode = new(NodeType.Leaf, Keccak.Zero); Assert.Throws <InvalidOperationException>( () => trieNode.Key = HexPrefix.FromBytes(Bytes.FromHexString("aaa"))); }
public static TrieNode CreateExtension(HexPrefix key, TrieNode child) { TrieNode node = new TrieNode(NodeType.Extension); node.SetChild(0, child); node.Key = key; return(node); }
public void Encode_gives_correct_output_when_one(bool flag, byte nibble1, byte byte1) { HexPrefix hexPrefix = new HexPrefix(flag, nibble1); byte[] output = hexPrefix.ToBytes(); Assert.AreEqual(1, output.Length); Assert.AreEqual(byte1, output[0]); }
public static TrieNode CreateLeaf(HexPrefix key, byte[] value) { TrieNode node = new TrieNode(NodeType.Leaf); node.Key = key; node.Value = value; return(node); }
public void Decode_gives_correct_output_when_one(bool expectedFlag, byte nibble1, byte byte1) { HexPrefix hexPrefix = HexPrefix.FromBytes(new[] { byte1 }); Assert.AreEqual(expectedFlag, hexPrefix.IsLeaf); Assert.AreEqual(1, hexPrefix.Path.Length); Assert.AreEqual(nibble1, hexPrefix.Path[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); }
public void Pruning_regression() { TrieNode child = new(NodeType.Unknown, Keccak.Zero); TrieNode trieNode = new(NodeType.Extension); trieNode.SetChild(0, child); trieNode.PrunePersistedRecursively(1); trieNode.Key = HexPrefix.Extension("abcd"); trieNode.RlpEncode(NullTrieStore.Instance); }
public void Unresolve_of_persisted() { TrieNode child = new(NodeType.Unknown, Keccak.Zero); TrieNode trieNode = new(NodeType.Extension); trieNode.SetChild(0, child); trieNode.Key = HexPrefix.Extension("abcd"); trieNode.ResolveKey(NullTrieStore.Instance, false); trieNode.PrunePersistedRecursively(1); trieNode.PrunePersistedRecursively(1); }
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); }
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); }
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); }
/// <summary> /// 获取字符的16进制Unicode码(HEX)(类似:\u679C) /// </summary> /// <param name="str"></param> /// <param name="hexPrefix"></param> /// <param name="endian">字节序</param> /// <returns></returns> public static string GetUnicode(string str, HexPrefix hexPrefix = HexPrefix.U, Endian endian = Endian.Big) { char[] charbuffers = str.ToCharArray(); byte[] buffer; var prefix = hexPrefix == HexPrefix.U ? "\\u" : string.Empty; StringBuilder sb = new StringBuilder(); for (int i = 0; i < charbuffers.Length; i++) { buffer = Encoding.Unicode.GetBytes(charbuffers[i].ToString()); sb.Append(prefix + string.Format("{0:X2}{1:X2}", buffer[1], buffer[0])); } return(sb.ToString()); }
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); }
public void Small_child_unresolve() { TrieNode child = new(NodeType.Leaf); child.Value = Bytes.FromHexString("a"); child.Key = HexPrefix.Leaf("b"); child.ResolveKey(NullTrieStore.Instance, false); child.IsPersisted = true; TrieNode trieNode = new(NodeType.Extension); trieNode.SetChild(0, child); trieNode.Key = HexPrefix.Extension("abcd"); trieNode.ResolveKey(NullTrieStore.Instance, false); trieNode.PrunePersistedRecursively(2); trieNode.GetChild(NullTrieStore.Instance, 0).Should().Be(child); }
public void Batch_not_db_regression() { TrieNode child = new(NodeType.Leaf); child.Key = HexPrefix.Leaf("abc"); child.Value = new byte[200]; child.Seal(); TrieNode trieNode = new(NodeType.Extension); trieNode.SetChild(0, child); trieNode.Seal(); ITrieNodeResolver trieStore = Substitute.For <ITrieNodeResolver>(); trieStore.LoadRlp(Arg.Any <Keccak>()).Throws(new TrieException()); child.ResolveKey(trieStore, false); child.IsPersisted = true; trieStore.FindCachedOrUnknown(Arg.Any <Keccak>()).Returns(new TrieNode(NodeType.Unknown, child.Keccak !)); trieNode.GetChild(trieStore, 0); Assert.Throws <TrieException>(() => trieNode.GetChild(trieStore, 0).ResolveNode(trieStore)); }
public byte Current() { return(HexPrefix.FromBytes(_a).Path[0]); }
public void Size_of_hex_prefix_is_correct() { HexPrefix hexPrefix = new HexPrefix(true, new byte[5]); hexPrefix.MemorySize.Should().Be(64); }
public byte Improved() { return(HexPrefix.FromBytes(_a).Path[0]); }
public HexPrefix Improved() { return(HexPrefix.FromBytes(_a)); }
public HexPrefix Current() { return(HexPrefix.FromBytes(_a)); }