github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/test/tools/LTE/experiments/util.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package experiments 18 19 import ( 20 "bytes" 21 "fmt" 22 "math/rand" 23 24 logging "github.com/op/go-logging" 25 ) 26 27 var logger = logging.MustGetLogger("experiments") 28 29 func constructKey(keyNumber int) string { 30 return fmt.Sprintf("%s%09d", "key_", keyNumber) 31 } 32 33 func constructValue(keyNumber int, kvSize int) []byte { 34 prefix := constructValuePrefix(keyNumber) 35 randomBytes := constructRandomBytes(kvSize - len(prefix)) 36 return append(prefix, randomBytes...) 37 } 38 39 func constructValuePrefix(keyNumber int) []byte { 40 return []byte(fmt.Sprintf("%s%09d", "value_", keyNumber)) 41 } 42 43 func verifyValue(keyNumber int, value []byte) bool { 44 prefix := constructValuePrefix(keyNumber) 45 if len(value) < len(prefix) { 46 return false 47 } 48 return bytes.Equal(value[:len(prefix)], prefix) 49 } 50 51 func disableLogging() { 52 logging.SetLevel(logging.ERROR, "") 53 } 54 55 func calculateShare(total int, numParts int, partNum int) int { 56 share := total / numParts 57 remainder := total % numParts 58 if partNum < remainder { 59 share++ 60 } 61 return share 62 } 63 64 func constructRandomBytes(length int) []byte { 65 b := make([]byte, length) 66 rand.Read(b) 67 return b 68 }