mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-29 16:13:47 +00:00
fix: Content and Enrs ssz error
This commit is contained in:
parent
1e55782063
commit
3a5754f43a
4 changed files with 243 additions and 252 deletions
|
|
@ -966,6 +966,10 @@ func (p *PortalProtocol) handleFindContent(id enode.ID, addr *net.UDPAddr, reque
|
|||
}
|
||||
|
||||
enrs := p.truncateNodes(closestNodes, maxPayloadSize, enrOverhead)
|
||||
// TODO fix when no content and no enrs found
|
||||
if len(enrs) == 0 {
|
||||
enrs = append(enrs, []byte{})
|
||||
}
|
||||
|
||||
enrsMsg := &portalwire.Enrs{
|
||||
Enrs: enrs,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
package portalwire
|
||||
|
||||
//go:generate sszgen --path p2p/discover/portalwire/messages.go --exclude-objs BlockHeaderProof,PortalReceipts
|
||||
import (
|
||||
ssz "github.com/ferranbt/fastssz"
|
||||
)
|
||||
|
||||
//go:generate sszgen --path messages.go --exclude-objs Content,Enrs,ContentKV
|
||||
|
||||
// Message codes for the portal protocol.
|
||||
const (
|
||||
|
|
@ -108,3 +112,193 @@ type (
|
|||
ContentKeys []byte `ssz:"bitlist" ssz-max:"64"`
|
||||
}
|
||||
)
|
||||
|
||||
// MarshalSSZ ssz marshals the Content object
|
||||
func (c *Content) MarshalSSZ() ([]byte, error) {
|
||||
return ssz.MarshalSSZ(c)
|
||||
}
|
||||
|
||||
// MarshalSSZTo ssz marshals the Content object to a target array
|
||||
func (c *Content) MarshalSSZTo(buf []byte) (dst []byte, err error) {
|
||||
dst = buf
|
||||
|
||||
// Field (0) 'Content'
|
||||
if size := len(c.Content); size > 2048 {
|
||||
err = ssz.ErrBytesLengthFn("Content.Content", size, 2048)
|
||||
return
|
||||
}
|
||||
dst = append(dst, c.Content...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalSSZ ssz unmarshals the Content object
|
||||
func (c *Content) UnmarshalSSZ(buf []byte) error {
|
||||
var err error
|
||||
tail := buf
|
||||
|
||||
// Field (0) 'Content'
|
||||
{
|
||||
buf = tail[:]
|
||||
if len(buf) > 2048 {
|
||||
return ssz.ErrBytesLength
|
||||
}
|
||||
if cap(c.Content) == 0 {
|
||||
c.Content = make([]byte, 0, len(buf))
|
||||
}
|
||||
c.Content = append(c.Content, buf...)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// SizeSSZ returns the ssz encoded size in bytes for the Content object
|
||||
func (c *Content) SizeSSZ() (size int) {
|
||||
// Field (0) 'Content'
|
||||
return len(c.Content)
|
||||
}
|
||||
|
||||
// HashTreeRoot ssz hashes the Content object
|
||||
func (c *Content) HashTreeRoot() ([32]byte, error) {
|
||||
return ssz.HashWithDefaultHasher(c)
|
||||
}
|
||||
|
||||
// HashTreeRootWith ssz hashes the Content object with a hasher
|
||||
func (c *Content) HashTreeRootWith(hh ssz.HashWalker) (err error) {
|
||||
indx := hh.Index()
|
||||
|
||||
// Field (0) 'Content'
|
||||
{
|
||||
elemIndx := hh.Index()
|
||||
byteLen := uint64(len(c.Content))
|
||||
if byteLen > 2048 {
|
||||
err = ssz.ErrIncorrectListSize
|
||||
return
|
||||
}
|
||||
hh.Append(c.Content)
|
||||
hh.MerkleizeWithMixin(elemIndx, byteLen, (2048+31)/32)
|
||||
}
|
||||
|
||||
hh.Merkleize(indx)
|
||||
return
|
||||
}
|
||||
|
||||
// GetTree ssz hashes the Content object
|
||||
func (c *Content) GetTree() (*ssz.Node, error) {
|
||||
return ssz.ProofTree(c)
|
||||
}
|
||||
|
||||
// MarshalSSZ ssz marshals the Enrs object
|
||||
func (e *Enrs) MarshalSSZ() ([]byte, error) {
|
||||
return ssz.MarshalSSZ(e)
|
||||
}
|
||||
|
||||
// MarshalSSZTo ssz marshals the Enrs object to a target array
|
||||
func (e *Enrs) MarshalSSZTo(buf []byte) (dst []byte, err error) {
|
||||
dst = buf
|
||||
offset := int(0)
|
||||
|
||||
// Field (0) 'Enrs'
|
||||
if size := len(e.Enrs); size > 32 {
|
||||
err = ssz.ErrListTooBigFn("Enrs.Enrs", size, 32)
|
||||
return
|
||||
}
|
||||
{
|
||||
offset = 4 * len(e.Enrs)
|
||||
for ii := 0; ii < len(e.Enrs); ii++ {
|
||||
dst = ssz.WriteOffset(dst, offset)
|
||||
offset += len(e.Enrs[ii])
|
||||
}
|
||||
}
|
||||
for ii := 0; ii < len(e.Enrs); ii++ {
|
||||
if size := len(e.Enrs[ii]); size > 2048 {
|
||||
err = ssz.ErrBytesLengthFn("Enrs.Enrs[ii]", size, 2048)
|
||||
return
|
||||
}
|
||||
dst = append(dst, e.Enrs[ii]...)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalSSZ ssz unmarshals the Enrs object
|
||||
func (e *Enrs) UnmarshalSSZ(buf []byte) error {
|
||||
var err error
|
||||
tail := buf
|
||||
// Field (0) 'Enrs'
|
||||
{
|
||||
buf = tail[:]
|
||||
num, err := ssz.DecodeDynamicLength(buf, 32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.Enrs = make([][]byte, num)
|
||||
err = ssz.UnmarshalDynamic(buf, num, func(indx int, buf []byte) (err error) {
|
||||
if len(buf) > 2048 {
|
||||
return ssz.ErrBytesLength
|
||||
}
|
||||
if cap(e.Enrs[indx]) == 0 {
|
||||
e.Enrs[indx] = make([]byte, 0, len(buf))
|
||||
}
|
||||
e.Enrs[indx] = append(e.Enrs[indx], buf...)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// SizeSSZ returns the ssz encoded size in bytes for the Enrs object
|
||||
func (e *Enrs) SizeSSZ() (size int) {
|
||||
size = 0
|
||||
|
||||
// Field (0) 'Enrs'
|
||||
for ii := 0; ii < len(e.Enrs); ii++ {
|
||||
size += 4
|
||||
size += len(e.Enrs[ii])
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// HashTreeRoot ssz hashes the Enrs object
|
||||
func (e *Enrs) HashTreeRoot() ([32]byte, error) {
|
||||
return ssz.HashWithDefaultHasher(e)
|
||||
}
|
||||
|
||||
// HashTreeRootWith ssz hashes the Enrs object with a hasher
|
||||
func (e *Enrs) HashTreeRootWith(hh ssz.HashWalker) (err error) {
|
||||
indx := hh.Index()
|
||||
|
||||
// Field (0) 'Enrs'
|
||||
{
|
||||
subIndx := hh.Index()
|
||||
num := uint64(len(e.Enrs))
|
||||
if num > 32 {
|
||||
err = ssz.ErrIncorrectListSize
|
||||
return
|
||||
}
|
||||
for _, elem := range e.Enrs {
|
||||
{
|
||||
elemIndx := hh.Index()
|
||||
byteLen := uint64(len(elem))
|
||||
if byteLen > 2048 {
|
||||
err = ssz.ErrIncorrectListSize
|
||||
return
|
||||
}
|
||||
hh.AppendBytes32(elem)
|
||||
hh.MerkleizeWithMixin(elemIndx, byteLen, (2048+31)/32)
|
||||
}
|
||||
}
|
||||
hh.MerkleizeWithMixin(subIndx, num, 32)
|
||||
}
|
||||
|
||||
hh.Merkleize(indx)
|
||||
return
|
||||
}
|
||||
|
||||
// GetTree ssz hashes the Enrs object
|
||||
func (e *Enrs) GetTree() (*ssz.Node, error) {
|
||||
return ssz.ProofTree(e)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// Code generated by fastssz. DO NOT EDIT.
|
||||
// Hash: 1f7cf716b197089d098199d33ce79d7b9d61b272e3d573561109f635d370d026
|
||||
// Hash: 26a61b12807ff78c64a029acdd5bcb580dfe35b7bfbf8bf04ceebae1a3d5cac1
|
||||
// Version: 0.1.3
|
||||
package portalwire
|
||||
|
||||
|
|
@ -843,243 +843,6 @@ func (c *ConnectionId) GetTree() (*ssz.Node, error) {
|
|||
return ssz.ProofTree(c)
|
||||
}
|
||||
|
||||
// MarshalSSZ ssz marshals the Content object
|
||||
func (c *Content) MarshalSSZ() ([]byte, error) {
|
||||
return ssz.MarshalSSZ(c)
|
||||
}
|
||||
|
||||
// MarshalSSZTo ssz marshals the Content object to a target array
|
||||
func (c *Content) MarshalSSZTo(buf []byte) (dst []byte, err error) {
|
||||
dst = buf
|
||||
offset := int(4)
|
||||
|
||||
// Offset (0) 'Content'
|
||||
dst = ssz.WriteOffset(dst, offset)
|
||||
offset += len(c.Content)
|
||||
|
||||
// Field (0) 'Content'
|
||||
if size := len(c.Content); size > 2048 {
|
||||
err = ssz.ErrBytesLengthFn("Content.Content", size, 2048)
|
||||
return
|
||||
}
|
||||
dst = append(dst, c.Content...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalSSZ ssz unmarshals the Content object
|
||||
func (c *Content) UnmarshalSSZ(buf []byte) error {
|
||||
var err error
|
||||
size := uint64(len(buf))
|
||||
if size < 4 {
|
||||
return ssz.ErrSize
|
||||
}
|
||||
|
||||
tail := buf
|
||||
var o0 uint64
|
||||
|
||||
// Offset (0) 'Content'
|
||||
if o0 = ssz.ReadOffset(buf[0:4]); o0 > size {
|
||||
return ssz.ErrOffset
|
||||
}
|
||||
|
||||
if o0 < 4 {
|
||||
return ssz.ErrInvalidVariableOffset
|
||||
}
|
||||
|
||||
// Field (0) 'Content'
|
||||
{
|
||||
buf = tail[o0:]
|
||||
if len(buf) > 2048 {
|
||||
return ssz.ErrBytesLength
|
||||
}
|
||||
if cap(c.Content) == 0 {
|
||||
c.Content = make([]byte, 0, len(buf))
|
||||
}
|
||||
c.Content = append(c.Content, buf...)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// SizeSSZ returns the ssz encoded size in bytes for the Content object
|
||||
func (c *Content) SizeSSZ() (size int) {
|
||||
size = 4
|
||||
|
||||
// Field (0) 'Content'
|
||||
size += len(c.Content)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// HashTreeRoot ssz hashes the Content object
|
||||
func (c *Content) HashTreeRoot() ([32]byte, error) {
|
||||
return ssz.HashWithDefaultHasher(c)
|
||||
}
|
||||
|
||||
// HashTreeRootWith ssz hashes the Content object with a hasher
|
||||
func (c *Content) HashTreeRootWith(hh ssz.HashWalker) (err error) {
|
||||
indx := hh.Index()
|
||||
|
||||
// Field (0) 'Content'
|
||||
{
|
||||
elemIndx := hh.Index()
|
||||
byteLen := uint64(len(c.Content))
|
||||
if byteLen > 2048 {
|
||||
err = ssz.ErrIncorrectListSize
|
||||
return
|
||||
}
|
||||
hh.Append(c.Content)
|
||||
hh.MerkleizeWithMixin(elemIndx, byteLen, (2048+31)/32)
|
||||
}
|
||||
|
||||
hh.Merkleize(indx)
|
||||
return
|
||||
}
|
||||
|
||||
// GetTree ssz hashes the Content object
|
||||
func (c *Content) GetTree() (*ssz.Node, error) {
|
||||
return ssz.ProofTree(c)
|
||||
}
|
||||
|
||||
// MarshalSSZ ssz marshals the Enrs object
|
||||
func (e *Enrs) MarshalSSZ() ([]byte, error) {
|
||||
return ssz.MarshalSSZ(e)
|
||||
}
|
||||
|
||||
// MarshalSSZTo ssz marshals the Enrs object to a target array
|
||||
func (e *Enrs) MarshalSSZTo(buf []byte) (dst []byte, err error) {
|
||||
dst = buf
|
||||
offset := int(4)
|
||||
|
||||
// Offset (0) 'Enrs'
|
||||
dst = ssz.WriteOffset(dst, offset)
|
||||
for ii := 0; ii < len(e.Enrs); ii++ {
|
||||
offset += 4
|
||||
offset += len(e.Enrs[ii])
|
||||
}
|
||||
|
||||
// Field (0) 'Enrs'
|
||||
if size := len(e.Enrs); size > 32 {
|
||||
err = ssz.ErrListTooBigFn("Enrs.Enrs", size, 32)
|
||||
return
|
||||
}
|
||||
{
|
||||
offset = 4 * len(e.Enrs)
|
||||
for ii := 0; ii < len(e.Enrs); ii++ {
|
||||
dst = ssz.WriteOffset(dst, offset)
|
||||
offset += len(e.Enrs[ii])
|
||||
}
|
||||
}
|
||||
for ii := 0; ii < len(e.Enrs); ii++ {
|
||||
if size := len(e.Enrs[ii]); size > 2048 {
|
||||
err = ssz.ErrBytesLengthFn("Enrs.Enrs[ii]", size, 2048)
|
||||
return
|
||||
}
|
||||
dst = append(dst, e.Enrs[ii]...)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalSSZ ssz unmarshals the Enrs object
|
||||
func (e *Enrs) UnmarshalSSZ(buf []byte) error {
|
||||
var err error
|
||||
size := uint64(len(buf))
|
||||
if size < 4 {
|
||||
return ssz.ErrSize
|
||||
}
|
||||
|
||||
tail := buf
|
||||
var o0 uint64
|
||||
|
||||
// Offset (0) 'Enrs'
|
||||
if o0 = ssz.ReadOffset(buf[0:4]); o0 > size {
|
||||
return ssz.ErrOffset
|
||||
}
|
||||
|
||||
if o0 < 4 {
|
||||
return ssz.ErrInvalidVariableOffset
|
||||
}
|
||||
|
||||
// Field (0) 'Enrs'
|
||||
{
|
||||
buf = tail[o0:]
|
||||
num, err := ssz.DecodeDynamicLength(buf, 32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.Enrs = make([][]byte, num)
|
||||
err = ssz.UnmarshalDynamic(buf, num, func(indx int, buf []byte) (err error) {
|
||||
if len(buf) > 2048 {
|
||||
return ssz.ErrBytesLength
|
||||
}
|
||||
if cap(e.Enrs[indx]) == 0 {
|
||||
e.Enrs[indx] = make([]byte, 0, len(buf))
|
||||
}
|
||||
e.Enrs[indx] = append(e.Enrs[indx], buf...)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// SizeSSZ returns the ssz encoded size in bytes for the Enrs object
|
||||
func (e *Enrs) SizeSSZ() (size int) {
|
||||
size = 4
|
||||
|
||||
// Field (0) 'Enrs'
|
||||
for ii := 0; ii < len(e.Enrs); ii++ {
|
||||
size += 4
|
||||
size += len(e.Enrs[ii])
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// HashTreeRoot ssz hashes the Enrs object
|
||||
func (e *Enrs) HashTreeRoot() ([32]byte, error) {
|
||||
return ssz.HashWithDefaultHasher(e)
|
||||
}
|
||||
|
||||
// HashTreeRootWith ssz hashes the Enrs object with a hasher
|
||||
func (e *Enrs) HashTreeRootWith(hh ssz.HashWalker) (err error) {
|
||||
indx := hh.Index()
|
||||
|
||||
// Field (0) 'Enrs'
|
||||
{
|
||||
subIndx := hh.Index()
|
||||
num := uint64(len(e.Enrs))
|
||||
if num > 32 {
|
||||
err = ssz.ErrIncorrectListSize
|
||||
return
|
||||
}
|
||||
for _, elem := range e.Enrs {
|
||||
{
|
||||
elemIndx := hh.Index()
|
||||
byteLen := uint64(len(elem))
|
||||
if byteLen > 2048 {
|
||||
err = ssz.ErrIncorrectListSize
|
||||
return
|
||||
}
|
||||
hh.AppendBytes32(elem)
|
||||
hh.MerkleizeWithMixin(elemIndx, byteLen, (2048+31)/32)
|
||||
}
|
||||
}
|
||||
hh.MerkleizeWithMixin(subIndx, num, 32)
|
||||
}
|
||||
|
||||
hh.Merkleize(indx)
|
||||
return
|
||||
}
|
||||
|
||||
// GetTree ssz hashes the Enrs object
|
||||
func (e *Enrs) GetTree() (*ssz.Node, error) {
|
||||
return ssz.ProofTree(e)
|
||||
}
|
||||
|
||||
// MarshalSSZ ssz marshals the Accept object
|
||||
func (a *Accept) MarshalSSZ() ([]byte, error) {
|
||||
return ssz.MarshalSSZ(a)
|
||||
|
|
|
|||
|
|
@ -137,20 +137,50 @@ func TestContent(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, fmt.Sprintf("0x%x", data))
|
||||
|
||||
// TODO content response is Union type
|
||||
expected = "0x7468652063616b652069732061206c6965"
|
||||
|
||||
// idBuffer := make([]byte, 0, 2)
|
||||
// idBuffer = append(idBuffer, 0x01)
|
||||
// idBuffer = append(idBuffer, 0x02)
|
||||
// // binary.BigEndian.PutUint16()
|
||||
// // binary.BigEndian.PutUint16(idBuffer, uint16(0x0102))
|
||||
// cIds := &ConnectionId{
|
||||
// Id: idBuffer,
|
||||
// }
|
||||
// expected = "0x000102"
|
||||
// data, err = cIds.MarshalSSZ()
|
||||
// assert.NoError(t, err)
|
||||
// assert.Equal(t, expected, fmt.Sprintf("0x%x", data))
|
||||
contentRes := &Content{
|
||||
Content: hexutil.MustDecode("0x7468652063616b652069732061206c6965"),
|
||||
}
|
||||
|
||||
data, err = contentRes.MarshalSSZ()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, fmt.Sprintf("0x%x", data))
|
||||
|
||||
expectData := &Content{}
|
||||
err = expectData.UnmarshalSSZ(data)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, contentRes.Content, expectData.Content)
|
||||
|
||||
enrs := []string{
|
||||
"enr:-HW4QBzimRxkmT18hMKaAL3IcZF1UcfTMPyi3Q1pxwZZbcZVRI8DC5infUAB_UauARLOJtYTxaagKoGmIjzQxO2qUygBgmlkgnY0iXNlY3AyNTZrMaEDymNMrg1JrLQB2KTGtv6MVbcNEVv0AHacwUAPMljNMTg",
|
||||
"enr:-HW4QNfxw543Ypf4HXKXdYxkyzfcxcO-6p9X986WldfVpnVTQX1xlTnWrktEWUbeTZnmgOuAY_KUhbVV1Ft98WoYUBMBgmlkgnY0iXNlY3AyNTZrMaEDDiy3QkHAxPyOgWbxp5oF1bDdlYE6dLCUUp8xfVw50jU",
|
||||
}
|
||||
|
||||
enrsBytes := make([][]byte, 0)
|
||||
for _, enr := range enrs {
|
||||
n, err := enode.Parse(enode.ValidSchemes, enr)
|
||||
assert.NoError(t, err)
|
||||
|
||||
enrBytes, err := rlp.EncodeToBytes(n.Record())
|
||||
assert.NoError(t, err)
|
||||
enrsBytes = append(enrsBytes, enrBytes)
|
||||
}
|
||||
|
||||
enrsRes := &Enrs{
|
||||
Enrs: enrsBytes,
|
||||
}
|
||||
|
||||
expected = "0x080000007f000000f875b8401ce2991c64993d7c84c29a00bdc871917551c7d330fca2dd0d69c706596dc655448f030b98a77d4001fd46ae0112ce26d613c5a6a02a81a6223cd0c4edaa53280182696482763489736563703235366b31a103ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd3138f875b840d7f1c39e376297f81d7297758c64cb37dcc5c3beea9f57f7ce9695d7d5a67553417d719539d6ae4b445946de4d99e680eb8063f29485b555d45b7df16a1850130182696482763489736563703235366b31a1030e2cb74241c0c4fc8e8166f1a79a05d5b0dd95813a74b094529f317d5c39d235"
|
||||
|
||||
data, err = enrsRes.MarshalSSZ()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, fmt.Sprintf("0x%x", data))
|
||||
|
||||
expectEnrs := &Enrs{}
|
||||
err = expectEnrs.UnmarshalSSZ(data)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectEnrs.Enrs, enrsRes.Enrs)
|
||||
}
|
||||
|
||||
func TestOfferAndAcceptMessage(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue