github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/storage/mru/lookup.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 "encoding/binary" 29 "hash" 30 31 "github.com/ethereum/go-ethereum/swarm/storage" 32 ) 33 34 // 35 // 36 // 37 type LookupParams struct { 38 UpdateLookup 39 Limit uint32 40 } 41 42 // 43 func (r *LookupParams) RootAddr() storage.Address { 44 return r.rootAddr 45 } 46 47 func NewLookupParams(rootAddr storage.Address, period, version uint32, limit uint32) *LookupParams { 48 return &LookupParams{ 49 UpdateLookup: UpdateLookup{ 50 period: period, 51 version: version, 52 rootAddr: rootAddr, 53 }, 54 Limit: limit, 55 } 56 } 57 58 // 59 func LookupLatest(rootAddr storage.Address) *LookupParams { 60 return NewLookupParams(rootAddr, 0, 0, 0) 61 } 62 63 // 64 func LookupLatestVersionInPeriod(rootAddr storage.Address, period uint32) *LookupParams { 65 return NewLookupParams(rootAddr, period, 0, 0) 66 } 67 68 // 69 func LookupVersion(rootAddr storage.Address, period, version uint32) *LookupParams { 70 return NewLookupParams(rootAddr, period, version, 0) 71 } 72 73 // 74 type UpdateLookup struct { 75 period uint32 76 version uint32 77 rootAddr storage.Address 78 } 79 80 // 81 // 82 // 83 const updateLookupLength = 4 + 4 + storage.KeyLength 84 85 // 86 func (u *UpdateLookup) UpdateAddr() (updateAddr storage.Address) { 87 serializedData := make([]byte, updateLookupLength) 88 u.binaryPut(serializedData) 89 hasher := hashPool.Get().(hash.Hash) 90 defer hashPool.Put(hasher) 91 hasher.Reset() 92 hasher.Write(serializedData) 93 return hasher.Sum(nil) 94 } 95 96 // 97 func (u *UpdateLookup) binaryPut(serializedData []byte) error { 98 if len(serializedData) != updateLookupLength { 99 return NewErrorf(ErrInvalidValue, "Incorrect slice size to serialize UpdateLookup. Expected %d, got %d", updateLookupLength, len(serializedData)) 100 } 101 if len(u.rootAddr) != storage.KeyLength { 102 return NewError(ErrInvalidValue, "UpdateLookup.binaryPut called without rootAddr set") 103 } 104 binary.LittleEndian.PutUint32(serializedData[:4], u.period) 105 binary.LittleEndian.PutUint32(serializedData[4:8], u.version) 106 copy(serializedData[8:], u.rootAddr[:]) 107 return nil 108 } 109 110 // 111 func (u *UpdateLookup) binaryLength() int { 112 return updateLookupLength 113 } 114 115 // 116 func (u *UpdateLookup) binaryGet(serializedData []byte) error { 117 if len(serializedData) != updateLookupLength { 118 return NewErrorf(ErrInvalidValue, "Incorrect slice size to read UpdateLookup. Expected %d, got %d", updateLookupLength, len(serializedData)) 119 } 120 u.period = binary.LittleEndian.Uint32(serializedData[:4]) 121 u.version = binary.LittleEndian.Uint32(serializedData[4:8]) 122 u.rootAddr = storage.Address(make([]byte, storage.KeyLength)) 123 copy(u.rootAddr[:], serializedData[8:]) 124 return nil 125 }