From 4961baf518ea9b491a8e2d7ffe68779a8ac22eac Mon Sep 17 00:00:00 2001 From: fearlessfe <505380967@qq.com> Date: Fri, 30 Aug 2024 23:34:38 +0800 Subject: [PATCH] feat: add state value types --- portalnetwork/state/types.go | 120 +++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/portalnetwork/state/types.go b/portalnetwork/state/types.go index 04401bc1c4..97a8049afc 100644 --- a/portalnetwork/state/types.go +++ b/portalnetwork/state/types.go @@ -231,3 +231,123 @@ func (c *ContractBytecodeKey) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) c &c.NodeHash, ) } + +const MaxTrieNodeLength = 1026 +// A content value type, used when retrieving a trie node. +type TrieNode struct { + Node []byte +} + +func (t *TrieNode) Deserialize(dr *codec.DecodingReader) error { + return dr.ByteList((*[]byte)(&t.Node), uint64(MaxTrieNodeLength)) +} + +func (t TrieNode) Serialize(w *codec.EncodingWriter) error { + return w.Write(t.Node) +} + +func (t TrieNode) ByteLength() (out uint64) { + return uint64(len(t.Node)) +} + +func (t *TrieNode) FixedLength() uint64 { + return 0 +} + +func (t TrieNode) HashTreeRoot(hFn tree.HashFn) tree.Root { + return hFn.ByteListHTR(t.Node, MaxTrieNodeLength) +} + +const MaxTrieProofLength = 65 + +type TrieProof []TrieNode + +func (r *TrieProof) Deserialize(dr *codec.DecodingReader) error { + return dr.List(func() codec.Deserializable { + i := len(*r) + *r = append(*r, TrieNode{}) + return &((*r)[i]) + }, 0, MaxTrieProofLength) +} + +func (r TrieProof) Serialize(w *codec.EncodingWriter) error { + return w.List(func(i uint64) codec.Serializable { + return &r[i] + }, 0, uint64(len(r))) +} + +func (r TrieProof) ByteLength() (out uint64) { + for _, v := range r { + out += v.ByteLength() + codec.OFFSET_SIZE + } + return +} + +func (r *TrieProof) FixedLength(_ *common.Spec) uint64 { + return 0 +} + +func (r TrieProof) HashTreeRoot(hFn tree.HashFn) common.Root { + length := uint64(len(r)) + return hFn.ComplexListHTR(func(i uint64) tree.HTR { + if i < length { + return &r[i] + } + return nil + }, length, MaxTrieProofLength) +} + +const MaxContractBytecodeLength = 32768 +// A content value type, used when retrieving contract's bytecode. +type ContractBytecode struct { + Code []byte +} + +func (t *ContractBytecode) Deserialize(dr *codec.DecodingReader) error { + return dr.ByteList((*[]byte)(&t.Code), uint64(MaxContractBytecodeLength)) +} + +func (t ContractBytecode) Serialize(w *codec.EncodingWriter) error { + return w.Write(t.Code) +} + +func (t ContractBytecode) ByteLength() (out uint64) { + return uint64(len(t.Code)) +} + +func (t *ContractBytecode) FixedLength() uint64 { + return 0 +} + +func (t ContractBytecode) HashTreeRoot(hFn tree.HashFn) tree.Root { + return hFn.ByteListHTR(t.Code, MaxContractBytecodeLength) +} + + +// A content value type, used when offering a trie node from the account trie. +type AccountTrieNodeWithProof struct{ + /// An proof for the account trie node. + Proof TrieProof + /// A block at which the proof is anchored. + BlockHash common.Bytes32 +} + +// A content value type, used when offering a trie node from the contract storage trie. +type ContractStorageTrieNodeWithProof struct { + // A proof for the contract storage trie node. + StoregeProof TrieProof + // A proof for the account state. + AccountProof TrieProof + // A block at which the proof is anchored. + BlockHash common.Bytes32 +} + +// A content value type, used when offering contract's bytecode. +type ContractBytecodeWithProof struct { + // A contract's bytecode. + Code ContractBytecode + // A proof for the account state of the corresponding contract. + AccountProof TrieProof + // A block at which the proof is anchored. + BlockHash common.Bytes32 +} \ No newline at end of file