Merge pull request #142 from fearlessfe/state

add ssz for State content value
This commit is contained in:
彭振 2024-09-02 13:46:26 +08:00 committed by GitHub
commit fd798cad85
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 561 additions and 41 deletions

37
portalnetwork/state/testdata/data.json vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -154,14 +154,14 @@ func (a *AccountTrieNodeKey) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) co
}
type ContractStorageTrieNodeKey struct {
Address common.Eth1Address
Path Nibbles
NodeHash common.Bytes32
AddressHash common.Bytes32
Path Nibbles
NodeHash common.Bytes32
}
func (c *ContractStorageTrieNodeKey) Deserialize(dr *codec.DecodingReader) error {
return dr.Container(
&c.Address,
&c.AddressHash,
&c.Path,
&c.NodeHash,
)
@ -169,7 +169,7 @@ func (c *ContractStorageTrieNodeKey) Deserialize(dr *codec.DecodingReader) error
func (c *ContractStorageTrieNodeKey) Serialize(w *codec.EncodingWriter) error {
return w.Container(
&c.Address,
&c.AddressHash,
&c.Path,
&c.NodeHash,
)
@ -177,7 +177,7 @@ func (c *ContractStorageTrieNodeKey) Serialize(w *codec.EncodingWriter) error {
func (c *ContractStorageTrieNodeKey) ByteLength(spec *common.Spec) uint64 {
return codec.ContainerLength(
&c.Address,
&c.AddressHash,
&c.Path,
&c.NodeHash,
)
@ -189,35 +189,35 @@ func (c *ContractStorageTrieNodeKey) FixedLength(spec *common.Spec) uint64 {
func (c *ContractStorageTrieNodeKey) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
return hFn.HashTreeRoot(
&c.Address,
&c.AddressHash,
&c.Path,
&c.NodeHash,
)
}
type ContractBytecodeKey struct {
Address common.Eth1Address
NodeHash common.Bytes32
AddressHash common.Bytes32
CodeHash common.Bytes32
}
func (c *ContractBytecodeKey) Deserialize(dr *codec.DecodingReader) error {
return dr.FixedLenContainer(
&c.Address,
&c.NodeHash,
&c.AddressHash,
&c.CodeHash,
)
}
func (c *ContractBytecodeKey) Serialize(w *codec.EncodingWriter) error {
return w.FixedLenContainer(
&c.Address,
&c.NodeHash,
&c.AddressHash,
&c.CodeHash,
)
}
func (c *ContractBytecodeKey) ByteLength(spec *common.Spec) uint64 {
return codec.ContainerLength(
&c.Address,
&c.NodeHash,
&c.AddressHash,
&c.CodeHash,
)
}
@ -227,7 +227,278 @@ func (c *ContractBytecodeKey) FixedLength(spec *common.Spec) uint64 {
func (c *ContractBytecodeKey) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
return hFn.HashTreeRoot(
&c.Address,
&c.NodeHash,
&c.AddressHash,
&c.CodeHash,
)
}
const MaxTrieNodeLength = 1026
type EncodedTrieNode []byte
func (e *EncodedTrieNode) Deserialize(dr *codec.DecodingReader) error {
return dr.ByteList((*[]byte)(e), uint64(MaxTrieNodeLength))
}
func (e EncodedTrieNode) Serialize(w *codec.EncodingWriter) error {
return w.Write(e)
}
func (e EncodedTrieNode) ByteLength() (out uint64) {
return uint64(len(e))
}
func (e *EncodedTrieNode) FixedLength() uint64 {
return 0
}
func (e EncodedTrieNode) HashTreeRoot(hFn tree.HashFn) tree.Root {
return hFn.ByteListHTR(e, MaxTrieNodeLength)
}
// A content value type, used when retrieving a trie node.
type TrieNode struct {
Node EncodedTrieNode
}
func (t *TrieNode) Deserialize(dr *codec.DecodingReader) error {
return dr.Container(
&t.Node,
)
}
func (t TrieNode) Serialize(w *codec.EncodingWriter) error {
return w.Container(&t.Node)
}
func (t TrieNode) ByteLength() (out uint64) {
return codec.ContainerLength(&t.Node)
}
func (t *TrieNode) FixedLength() uint64 {
return 0
}
func (t TrieNode) HashTreeRoot(hFn tree.HashFn) tree.Root {
return hFn.HashTreeRoot(&t.Node)
}
const MaxTrieProofLength = 65
type TrieProof []EncodedTrieNode
func (r *TrieProof) Deserialize(dr *codec.DecodingReader) error {
return dr.List(func() codec.Deserializable {
i := len(*r)
*r = append(*r, EncodedTrieNode{})
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() 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
type ContractByteCode []byte
func (t *ContractByteCode) Deserialize(dr *codec.DecodingReader) error {
return dr.ByteList((*[]byte)(t), uint64(MaxContractBytecodeLength))
}
func (t ContractByteCode) Serialize(w *codec.EncodingWriter) error {
return w.Write(t)
}
func (t ContractByteCode) ByteLength() (out uint64) {
return uint64(len(t))
}
func (t *ContractByteCode) FixedLength() uint64 {
return 0
}
func (t ContractByteCode) HashTreeRoot(hFn tree.HashFn) tree.Root {
return hFn.ByteListHTR(t, MaxContractBytecodeLength)
}
// A content value type, used when retrieving contract's bytecode.
type ContractBytecodeContainer struct {
Code ContractByteCode
}
func (t *ContractBytecodeContainer) Deserialize(dr *codec.DecodingReader) error {
return dr.Container(&t.Code)
}
func (t ContractBytecodeContainer) Serialize(w *codec.EncodingWriter) error {
return w.Container(&t.Code)
}
func (t ContractBytecodeContainer) ByteLength() (out uint64) {
return codec.ContainerLength(&t.Code)
}
func (t *ContractBytecodeContainer) FixedLength() uint64 {
return 0
}
func (t ContractBytecodeContainer) HashTreeRoot(hFn tree.HashFn) tree.Root {
return hFn.HashTreeRoot(t.Code)
}
// 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
}
func (a *AccountTrieNodeWithProof) Deserialize(dr *codec.DecodingReader) error {
return dr.Container(
&a.Proof,
&a.BlockHash,
)
}
func (a *AccountTrieNodeWithProof) Serialize(w *codec.EncodingWriter) error {
return w.Container(
&a.Proof,
&a.BlockHash,
)
}
func (a *AccountTrieNodeWithProof) ByteLength() uint64 {
return codec.ContainerLength(
&a.Proof,
&a.BlockHash,
)
}
func (a *AccountTrieNodeWithProof) FixedLength() uint64 {
return 0
}
func (a *AccountTrieNodeWithProof) HashTreeRoot(hFn tree.HashFn) common.Root {
return hFn.HashTreeRoot(
&a.Proof,
&a.BlockHash,
)
}
// 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
}
func (c *ContractStorageTrieNodeWithProof) Deserialize(dr *codec.DecodingReader) error {
return dr.Container(
&c.StoregeProof,
&c.AccountProof,
&c.BlockHash,
)
}
func (c *ContractStorageTrieNodeWithProof) Serialize(w *codec.EncodingWriter) error {
return w.Container(
&c.StoregeProof,
&c.AccountProof,
&c.BlockHash,
)
}
func (c *ContractStorageTrieNodeWithProof) ByteLength() uint64 {
return codec.ContainerLength(
&c.StoregeProof,
&c.AccountProof,
&c.BlockHash,
)
}
func (c *ContractStorageTrieNodeWithProof) FixedLength() uint64 {
return 0
}
func (c *ContractStorageTrieNodeWithProof) HashTreeRoot(hFn tree.HashFn) common.Root {
return hFn.HashTreeRoot(
&c.StoregeProof,
&c.AccountProof,
&c.BlockHash,
)
}
// 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
}
func (c *ContractBytecodeWithProof) Deserialize(dr *codec.DecodingReader) error {
return dr.Container(
&c.Code,
&c.AccountProof,
&c.BlockHash,
)
}
func (c *ContractBytecodeWithProof) Serialize(w *codec.EncodingWriter) error {
return w.Container(
&c.Code,
&c.AccountProof,
&c.BlockHash,
)
}
func (c *ContractBytecodeWithProof) ByteLength() uint64 {
return codec.ContainerLength(
&c.Code,
&c.AccountProof,
&c.BlockHash,
)
}
func (c *ContractBytecodeWithProof) FixedLength() uint64 {
return 0
}
func (c *ContractBytecodeWithProof) HashTreeRoot(hFn tree.HashFn) common.Root {
return hFn.HashTreeRoot(
&c.Code,
&c.AccountProof,
&c.BlockHash,
)
}

File diff suppressed because one or more lines are too long