github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/swarm/storage/mru/updateheader.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package mru
    18  
    19  import (
    20  	"github.com/FusionFoundation/efsn/swarm/storage"
    21  )
    22  
    23  // updateHeader models the non-payload components of a Resource Update
    24  type updateHeader struct {
    25  	UpdateLookup        // UpdateLookup contains the information required to locate this resource (components of the search key used to find it)
    26  	multihash    bool   // Whether the data in this Resource Update should be interpreted as multihash
    27  	metaHash     []byte // SHA3 hash of the metadata chunk (less ownerAddr). Used to prove ownerhsip of the resource.
    28  }
    29  
    30  const metaHashLength = storage.AddressLength
    31  
    32  // updateLookupLength bytes
    33  // 1 byte flags (multihash bool for now)
    34  // 32 bytes metaHash
    35  const updateHeaderLength = updateLookupLength + 1 + metaHashLength
    36  
    37  // binaryPut serializes the resource header information into the given slice
    38  func (h *updateHeader) binaryPut(serializedData []byte) error {
    39  	if len(serializedData) != updateHeaderLength {
    40  		return NewErrorf(ErrInvalidValue, "Incorrect slice size to serialize updateHeaderLength. Expected %d, got %d", updateHeaderLength, len(serializedData))
    41  	}
    42  	if len(h.metaHash) != metaHashLength {
    43  		return NewError(ErrInvalidValue, "updateHeader.binaryPut called without metaHash set")
    44  	}
    45  	if err := h.UpdateLookup.binaryPut(serializedData[:updateLookupLength]); err != nil {
    46  		return err
    47  	}
    48  	cursor := updateLookupLength
    49  	copy(serializedData[cursor:], h.metaHash[:metaHashLength])
    50  	cursor += metaHashLength
    51  
    52  	var flags byte
    53  	if h.multihash {
    54  		flags |= 0x01
    55  	}
    56  
    57  	serializedData[cursor] = flags
    58  	cursor++
    59  
    60  	return nil
    61  }
    62  
    63  // binaryLength returns the expected size of this structure when serialized
    64  func (h *updateHeader) binaryLength() int {
    65  	return updateHeaderLength
    66  }
    67  
    68  // binaryGet restores the current updateHeader instance from the information contained in the passed slice
    69  func (h *updateHeader) binaryGet(serializedData []byte) error {
    70  	if len(serializedData) != updateHeaderLength {
    71  		return NewErrorf(ErrInvalidValue, "Incorrect slice size to read updateHeaderLength. Expected %d, got %d", updateHeaderLength, len(serializedData))
    72  	}
    73  
    74  	if err := h.UpdateLookup.binaryGet(serializedData[:updateLookupLength]); err != nil {
    75  		return err
    76  	}
    77  	cursor := updateLookupLength
    78  	h.metaHash = make([]byte, metaHashLength)
    79  	copy(h.metaHash[:storage.AddressLength], serializedData[cursor:cursor+storage.AddressLength])
    80  	cursor += metaHashLength
    81  
    82  	flags := serializedData[cursor]
    83  	cursor++
    84  
    85  	h.multihash = flags&0x01 != 0
    86  
    87  	return nil
    88  }