github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/swarm/storage/feed/id.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 19:16:44</date> 10 //</624450119026085888> 11 12 13 package feed 14 15 import ( 16 "fmt" 17 "hash" 18 "strconv" 19 20 "github.com/ethereum/go-ethereum/common" 21 "github.com/ethereum/go-ethereum/swarm/storage/feed/lookup" 22 23 "github.com/ethereum/go-ethereum/swarm/storage" 24 ) 25 26 //ID唯一标识网络上的更新。 27 type ID struct { 28 Feed `json:"feed"` 29 lookup.Epoch `json:"epoch"` 30 } 31 32 //ID布局: 33 //进纸长度字节 34 //时代纪元 35 const idLength = feedLength + lookup.EpochLength 36 37 //addr计算与此ID对应的源更新块地址 38 func (u *ID) Addr() (updateAddr storage.Address) { 39 serializedData := make([]byte, idLength) 40 var cursor int 41 u.Feed.binaryPut(serializedData[cursor : cursor+feedLength]) 42 cursor += feedLength 43 44 eid := u.Epoch.ID() 45 copy(serializedData[cursor:cursor+lookup.EpochLength], eid[:]) 46 47 hasher := hashPool.Get().(hash.Hash) 48 defer hashPool.Put(hasher) 49 hasher.Reset() 50 hasher.Write(serializedData) 51 return hasher.Sum(nil) 52 } 53 54 //BinaryPut将此实例序列化到提供的切片中 55 func (u *ID) binaryPut(serializedData []byte) error { 56 if len(serializedData) != idLength { 57 return NewErrorf(ErrInvalidValue, "Incorrect slice size to serialize ID. Expected %d, got %d", idLength, len(serializedData)) 58 } 59 var cursor int 60 if err := u.Feed.binaryPut(serializedData[cursor : cursor+feedLength]); err != nil { 61 return err 62 } 63 cursor += feedLength 64 65 epochBytes, err := u.Epoch.MarshalBinary() 66 if err != nil { 67 return err 68 } 69 copy(serializedData[cursor:cursor+lookup.EpochLength], epochBytes[:]) 70 cursor += lookup.EpochLength 71 72 return nil 73 } 74 75 //BinaryLength返回序列化时此结构的预期大小 76 func (u *ID) binaryLength() int { 77 return idLength 78 } 79 80 //binaryget从传递的切片中包含的信息还原当前实例 81 func (u *ID) binaryGet(serializedData []byte) error { 82 if len(serializedData) != idLength { 83 return NewErrorf(ErrInvalidValue, "Incorrect slice size to read ID. Expected %d, got %d", idLength, len(serializedData)) 84 } 85 86 var cursor int 87 if err := u.Feed.binaryGet(serializedData[cursor : cursor+feedLength]); err != nil { 88 return err 89 } 90 cursor += feedLength 91 92 if err := u.Epoch.UnmarshalBinary(serializedData[cursor : cursor+lookup.EpochLength]); err != nil { 93 return err 94 } 95 cursor += lookup.EpochLength 96 97 return nil 98 } 99 100 //FromValues从字符串键值存储中反序列化此实例 101 //用于分析查询字符串 102 func (u *ID) FromValues(values Values) error { 103 level, _ := strconv.ParseUint(values.Get("level"), 10, 32) 104 u.Epoch.Level = uint8(level) 105 u.Epoch.Time, _ = strconv.ParseUint(values.Get("time"), 10, 64) 106 107 if u.Feed.User == (common.Address{}) { 108 return u.Feed.FromValues(values) 109 } 110 return nil 111 } 112 113 //AppendValues将此结构序列化到提供的字符串键值存储区中 114 //用于生成查询字符串 115 func (u *ID) AppendValues(values Values) { 116 values.Set("level", fmt.Sprintf("%d", u.Epoch.Level)) 117 values.Set("time", fmt.Sprintf("%d", u.Epoch.Time)) 118 u.Feed.AppendValues(values) 119 } 120