Beispiel #1
0
 public static NodeType GetNodeType(RlpDecoder rlp)
 {
     if (rlp.Count == 17)
     {
         return(NodeType.FullNode);
     }
     if (rlp.Count == 2)
     {
         return(TrieKey.Decode(rlp[0].Value).HasTerm ? NodeType.ValueNode : NodeType.ShortNode);
     }
     return(NodeType.EmptyNode);
 }
Beispiel #2
0
        // RLP를 디코딩하여 노드 상태를 완전하게 만든다.
        internal TrieNode DecodeRLP()
        {
            if (ReferenceEquals(rlp, null))
            {
                throw new Exception("can't decode empty rlp");
            }

            if (!Parsed)
            {
                var rlp = new RlpDecoder(this.rlp);

                if (Type == NodeType.FullNode)
                {
                    if (rlp.Count != 17)
                    {
                        throw new Exception("can't decode rlp for full node");
                    }

                    // child node
                    chidrens = new TrieNode[16];
                    for (int i = 0; i < 16; i++)
                    {
                        byte[] node = trie.Read(rlp[i].Value);
                        chidrens[i] = node != null ? new TrieNode(trie, node) : null;
                    }

                    // value
                    value = rlp[16].Value;
                }
                else if (Type == NodeType.ShortNode)
                {
                    if (rlp.Count != 2)
                    {
                        throw new Exception("can't decode rlp for short node");
                    }

                    // key
                    key = TrieKey.Decode(rlp[0].Value);

                    // next node
                    byte[] node = trie.Read(rlp[1].Value);
                    next = node != null ? new TrieNode(trie, node) : null;
                }
                else if (Type == NodeType.ValueNode)
                {
                    if (rlp.Count != 2)
                    {
                        throw new Exception("can't decode rlp for value node");
                    }

                    // key
                    key = TrieKey.Decode(rlp[0].Value);

                    // value
                    value = rlp[1].Value;
                }
                else
                {
                    throw new Exception("can't decoded rlp for unknown node type");
                }

                Parsed = true;
            }

            return(this);
        }