github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/storage/mru/updateheader.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:50</date> 10 //</624342684038860800> 11 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 // 25 // 26 // 27 28 package mru 29 30 import ( 31 "github.com/ethereum/go-ethereum/swarm/storage" 32 ) 33 34 // 35 type updateHeader struct { 36 UpdateLookup // 37 multihash bool // 38 metaHash []byte // 39 } 40 41 const metaHashLength = storage.KeyLength 42 43 // 44 // 45 // 46 const updateHeaderLength = updateLookupLength + 1 + metaHashLength 47 48 // 49 func (h *updateHeader) binaryPut(serializedData []byte) error { 50 if len(serializedData) != updateHeaderLength { 51 return NewErrorf(ErrInvalidValue, "Incorrect slice size to serialize updateHeaderLength. Expected %d, got %d", updateHeaderLength, len(serializedData)) 52 } 53 if len(h.metaHash) != metaHashLength { 54 return NewError(ErrInvalidValue, "updateHeader.binaryPut called without metaHash set") 55 } 56 if err := h.UpdateLookup.binaryPut(serializedData[:updateLookupLength]); err != nil { 57 return err 58 } 59 cursor := updateLookupLength 60 copy(serializedData[cursor:], h.metaHash[:metaHashLength]) 61 cursor += metaHashLength 62 63 var flags byte 64 if h.multihash { 65 flags |= 0x01 66 } 67 68 serializedData[cursor] = flags 69 cursor++ 70 71 return nil 72 } 73 74 // 75 func (h *updateHeader) binaryLength() int { 76 return updateHeaderLength 77 } 78 79 // 80 func (h *updateHeader) binaryGet(serializedData []byte) error { 81 if len(serializedData) != updateHeaderLength { 82 return NewErrorf(ErrInvalidValue, "Incorrect slice size to read updateHeaderLength. Expected %d, got %d", updateHeaderLength, len(serializedData)) 83 } 84 85 if err := h.UpdateLookup.binaryGet(serializedData[:updateLookupLength]); err != nil { 86 return err 87 } 88 cursor := updateLookupLength 89 h.metaHash = make([]byte, metaHashLength) 90 copy(h.metaHash[:storage.KeyLength], serializedData[cursor:cursor+storage.KeyLength]) 91 cursor += metaHashLength 92 93 flags := serializedData[cursor] 94 cursor++ 95 96 h.multihash = flags&0x01 != 0 97 98 return nil 99 } 100