github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/storage/mru/updateheader.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  //
    11  //
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  
    25  package mru
    26  
    27  import (
    28  	"github.com/ethereum/go-ethereum/swarm/storage"
    29  )
    30  
    31  //
    32  type updateHeader struct {
    33  UpdateLookup        //
    34  multihash    bool   //
    35  metaHash     []byte //
    36  }
    37  
    38  const metaHashLength = storage.KeyLength
    39  
    40  //
    41  //
    42  //
    43  const updateHeaderLength = updateLookupLength + 1 + metaHashLength
    44  
    45  //
    46  func (h *updateHeader) binaryPut(serializedData []byte) error {
    47  	if len(serializedData) != updateHeaderLength {
    48  		return NewErrorf(ErrInvalidValue, "Incorrect slice size to serialize updateHeaderLength. Expected %d, got %d", updateHeaderLength, len(serializedData))
    49  	}
    50  	if len(h.metaHash) != metaHashLength {
    51  		return NewError(ErrInvalidValue, "updateHeader.binaryPut called without metaHash set")
    52  	}
    53  	if err := h.UpdateLookup.binaryPut(serializedData[:updateLookupLength]); err != nil {
    54  		return err
    55  	}
    56  	cursor := updateLookupLength
    57  	copy(serializedData[cursor:], h.metaHash[:metaHashLength])
    58  	cursor += metaHashLength
    59  
    60  	var flags byte
    61  	if h.multihash {
    62  		flags |= 0x01
    63  	}
    64  
    65  	serializedData[cursor] = flags
    66  	cursor++
    67  
    68  	return nil
    69  }
    70  
    71  //
    72  func (h *updateHeader) binaryLength() int {
    73  	return updateHeaderLength
    74  }
    75  
    76  //
    77  func (h *updateHeader) binaryGet(serializedData []byte) error {
    78  	if len(serializedData) != updateHeaderLength {
    79  		return NewErrorf(ErrInvalidValue, "Incorrect slice size to read updateHeaderLength. Expected %d, got %d", updateHeaderLength, len(serializedData))
    80  	}
    81  
    82  	if err := h.UpdateLookup.binaryGet(serializedData[:updateLookupLength]); err != nil {
    83  		return err
    84  	}
    85  	cursor := updateLookupLength
    86  	h.metaHash = make([]byte, metaHashLength)
    87  	copy(h.metaHash[:storage.KeyLength], serializedData[cursor:cursor+storage.KeyLength])
    88  	cursor += metaHashLength
    89  
    90  	flags := serializedData[cursor]
    91  	cursor++
    92  
    93  	h.multihash = flags&0x01 != 0
    94  
    95  	return nil
    96  }