github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/p2p/types/types.go (about) 1 // Package types contains all the respective p2p types that are required for sync 2 // but cannot be represented as a protobuf schema. This package also contains those 3 // types associated fast ssz methods. 4 package types 5 6 import ( 7 ssz "github.com/ferranbt/fastssz" 8 "github.com/pkg/errors" 9 "github.com/prysmaticlabs/prysm/shared/params" 10 ) 11 12 const rootLength = 32 13 14 const maxErrorLength = 256 15 16 // SSZBytes is a bytes slice that satisfies the fast-ssz interface. 17 type SSZBytes []byte 18 19 // HashTreeRoot hashes the uint64 object following the SSZ standard. 20 func (b *SSZBytes) HashTreeRoot() ([32]byte, error) { 21 return ssz.HashWithDefaultHasher(b) 22 } 23 24 // HashTreeRootWith hashes the uint64 object with the given hasher. 25 func (b *SSZBytes) HashTreeRootWith(hh *ssz.Hasher) error { 26 indx := hh.Index() 27 hh.PutBytes(*b) 28 hh.Merkleize(indx) 29 return nil 30 } 31 32 // BeaconBlockByRootsReq specifies the block by roots request type. 33 type BeaconBlockByRootsReq [][rootLength]byte 34 35 // MarshalSSZTo marshals the block by roots request with the provided byte slice. 36 func (r *BeaconBlockByRootsReq) MarshalSSZTo(dst []byte) ([]byte, error) { 37 marshalledObj, err := r.MarshalSSZ() 38 if err != nil { 39 return nil, err 40 } 41 return append(dst, marshalledObj...), nil 42 } 43 44 // MarshalSSZ Marshals the block by roots request type into the serialized object. 45 func (r *BeaconBlockByRootsReq) MarshalSSZ() ([]byte, error) { 46 if len(*r) > int(params.BeaconNetworkConfig().MaxRequestBlocks) { 47 return nil, errors.Errorf("beacon block by roots request exceeds max size: %d > %d", len(*r), params.BeaconNetworkConfig().MaxRequestBlocks) 48 } 49 buf := make([]byte, 0, r.SizeSSZ()) 50 for _, r := range *r { 51 buf = append(buf, r[:]...) 52 } 53 return buf, nil 54 } 55 56 // SizeSSZ returns the size of the serialized representation. 57 func (r *BeaconBlockByRootsReq) SizeSSZ() int { 58 return len(*r) * rootLength 59 } 60 61 // UnmarshalSSZ unmarshals the provided bytes buffer into the 62 // block by roots request object. 63 func (r *BeaconBlockByRootsReq) UnmarshalSSZ(buf []byte) error { 64 bufLen := len(buf) 65 maxLength := int(params.BeaconNetworkConfig().MaxRequestBlocks * rootLength) 66 if bufLen > maxLength { 67 return errors.Errorf("expected buffer with length of upto %d but received length %d", maxLength, bufLen) 68 } 69 if bufLen%rootLength != 0 { 70 return ssz.ErrIncorrectByteSize 71 } 72 numOfRoots := bufLen / rootLength 73 roots := make([][rootLength]byte, 0, numOfRoots) 74 for i := 0; i < numOfRoots; i++ { 75 var rt [rootLength]byte 76 copy(rt[:], buf[i*rootLength:(i+1)*rootLength]) 77 roots = append(roots, rt) 78 } 79 *r = roots 80 return nil 81 } 82 83 // ErrorMessage describes the error message type. 84 type ErrorMessage []byte 85 86 // MarshalSSZTo marshals the error message with the provided byte slice. 87 func (m *ErrorMessage) MarshalSSZTo(dst []byte) ([]byte, error) { 88 marshalledObj, err := m.MarshalSSZ() 89 if err != nil { 90 return nil, err 91 } 92 return append(dst, marshalledObj...), nil 93 } 94 95 // MarshalSSZ Marshals the error message into the serialized object. 96 func (m *ErrorMessage) MarshalSSZ() ([]byte, error) { 97 if len(*m) > maxErrorLength { 98 return nil, errors.Errorf("error message exceeds max size: %d > %d", len(*m), maxErrorLength) 99 } 100 buf := make([]byte, m.SizeSSZ()) 101 copy(buf, *m) 102 return buf, nil 103 } 104 105 // SizeSSZ returns the size of the serialized representation. 106 func (m *ErrorMessage) SizeSSZ() int { 107 return len(*m) 108 } 109 110 // UnmarshalSSZ unmarshals the provided bytes buffer into the 111 // error message object. 112 func (m *ErrorMessage) UnmarshalSSZ(buf []byte) error { 113 bufLen := len(buf) 114 maxLength := maxErrorLength 115 if bufLen > maxLength { 116 return errors.Errorf("expected buffer with length of upto %d but received length %d", maxLength, bufLen) 117 } 118 errMsg := make([]byte, bufLen) 119 copy(errMsg, buf) 120 *m = errMsg 121 return nil 122 }