feat: add unit test for state types

This commit is contained in:
fearlessfe 2024-09-01 13:26:35 +08:00
parent 4961baf518
commit d4a59f290d
3 changed files with 470 additions and 70 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,27 +227,52 @@ 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 []byte
}
Node EncodedTrieNode
}
func (t *TrieNode) Deserialize(dr *codec.DecodingReader) error {
return dr.ByteList((*[]byte)(&t.Node), uint64(MaxTrieNodeLength))
return dr.Container(
&t.Node,
)
}
func (t TrieNode) Serialize(w *codec.EncodingWriter) error {
return w.Write(t.Node)
return w.Container(&t.Node)
}
func (t TrieNode) ByteLength() (out uint64) {
return uint64(len(t.Node))
return codec.ContainerLength(&t.Node)
}
func (t *TrieNode) FixedLength() uint64 {
@ -255,17 +280,17 @@ func (t *TrieNode) FixedLength() uint64 {
}
func (t TrieNode) HashTreeRoot(hFn tree.HashFn) tree.Root {
return hFn.ByteListHTR(t.Node, MaxTrieNodeLength)
return hFn.HashTreeRoot(&t.Node)
}
const MaxTrieProofLength = 65
type TrieProof []TrieNode
type TrieProof []EncodedTrieNode
func (r *TrieProof) Deserialize(dr *codec.DecodingReader) error {
return dr.List(func() codec.Deserializable {
i := len(*r)
*r = append(*r, TrieNode{})
*r = append(*r, EncodedTrieNode{})
return &((*r)[i])
}, 0, MaxTrieProofLength)
}
@ -283,7 +308,7 @@ func (r TrieProof) ByteLength() (out uint64) {
return
}
func (r *TrieProof) FixedLength(_ *common.Spec) uint64 {
func (r *TrieProof) FixedLength() uint64 {
return 0
}
@ -298,38 +323,92 @@ func (r TrieProof) HashTreeRoot(hFn tree.HashFn) common.Root {
}
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))
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.Code)
func (t ContractByteCode) Serialize(w *codec.EncodingWriter) error {
return w.Write(t)
}
func (t ContractBytecode) ByteLength() (out uint64) {
return uint64(len(t.Code))
func (t ContractByteCode) ByteLength() (out uint64) {
return uint64(len(t))
}
func (t *ContractBytecode) FixedLength() uint64 {
func (t *ContractByteCode) FixedLength() uint64 {
return 0
}
func (t ContractBytecode) HashTreeRoot(hFn tree.HashFn) tree.Root {
return hFn.ByteListHTR(t.Code, MaxContractBytecodeLength)
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
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.
@ -342,12 +421,84 @@ type ContractStorageTrieNodeWithProof struct {
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
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