github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/storage/mru/resource.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 "bytes" 29 "context" 30 "time" 31 32 "github.com/ethereum/go-ethereum/swarm/storage" 33 ) 34 35 const ( 36 defaultStoreTimeout = 4000 * time.Millisecond 37 hasherCount = 8 38 resourceHashAlgorithm = storage.SHA3Hash 39 defaultRetrieveTimeout = 100 * time.Millisecond 40 ) 41 42 // 43 type resource struct { 44 resourceUpdate 45 ResourceMetadata 46 *bytes.Reader 47 lastKey storage.Address 48 updated time.Time 49 } 50 51 func (r *resource) Context() context.Context { 52 return context.TODO() 53 } 54 55 // 56 func (r *resource) isSynced() bool { 57 return !r.updated.IsZero() 58 } 59 60 // 61 func (r *resource) Size(ctx context.Context, _ chan bool) (int64, error) { 62 if !r.isSynced() { 63 return 0, NewError(ErrNotSynced, "Not synced") 64 } 65 return int64(len(r.resourceUpdate.data)), nil 66 } 67 68 // 69 func (r *resource) Name() string { 70 return r.ResourceMetadata.Name 71 } 72 73 // 74 func getNextPeriod(start uint64, current uint64, frequency uint64) (uint32, error) { 75 if current < start { 76 return 0, NewErrorf(ErrInvalidValue, "given current time value %d < start time %d", current, start) 77 } 78 if frequency == 0 { 79 return 0, NewError(ErrInvalidValue, "frequency is 0") 80 } 81 timeDiff := current - start 82 period := timeDiff / frequency 83 return uint32(period + 1), nil 84 }