forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
143 lines (112 loc) · 2.96 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package experiments
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"strconv"
"github.com/hyperledger/fabric/common/flogging"
)
var logger = flogging.MustGetLogger("experiments")
type marbleRecord struct {
ID string `json:"_id,omitempty"`
Rev string `json:"_rev,omitempty"`
Prefix string `json:"prefix,omitempty"`
AssetType string `json:"asset_type,omitempty"`
AssetName string `json:"asset_name,omitempty"`
Color string `json:"color,omitempty"`
Size int `json:"size,omitempty"`
Owner string `json:"owner,omitempty"`
DataPadding string `json:"datapadding,omitempty"`
}
var colors = []string{
"red",
"green",
"purple",
"yellow",
"white",
"black",
}
var owners = []string{
"fred",
"jerry",
"tom",
"alice",
"kim",
"angela",
"john",
}
//TestValue is a struct for holding the test value
type TestValue struct {
Value string
}
func constructKey(keyNumber int) string {
return fmt.Sprintf("key_%09d", keyNumber)
}
func constructValue(keyNumber int, kvSize int) []byte {
prefix := constructValuePrefix(keyNumber)
randomBytes := constructRandomBytes(kvSize - len(prefix))
return append(prefix, randomBytes...)
}
func constructJSONValue(keyNumber int, kvSize int) []byte {
prefix := constructValuePrefix(keyNumber)
rand.Seed(int64(keyNumber))
color := colors[rand.Intn(len(colors))]
size := rand.Intn(len(colors))*10 + 10
owner := owners[rand.Intn(len(owners))]
assetName := "marble" + strconv.Itoa(keyNumber)
testRecord := marbleRecord{Prefix: string(prefix), AssetType: "marble", AssetName: assetName, Color: color, Size: size, Owner: owner}
jsonValue, _ := json.Marshal(testRecord)
if kvSize > len(jsonValue) {
randomJSONBytes := constructRandomBytes(kvSize - len(jsonValue))
//add in extra bytes
testRecord.DataPadding = string(randomJSONBytes)
jsonValue, _ = json.Marshal(testRecord)
}
return jsonValue
}
func constructValuePrefix(keyNumber int) []byte {
return []byte(fmt.Sprintf("%s%09d", "value_", keyNumber))
}
func verifyValue(keyNumber int, value []byte) bool {
prefix := constructValuePrefix(keyNumber)
if len(value) < len(prefix) {
return false
}
return bytes.Equal(value[:len(prefix)], prefix)
}
func verifyJSONValue(keyNumber int, value []byte) bool {
prefix := constructValuePrefix(keyNumber)
if len(value) < len(prefix) {
return false
}
var marble marbleRecord
json.Unmarshal(value, &marble)
if len(value) < len(prefix) {
return false
}
valuePrefix := []byte(marble.Prefix)
return bytes.Equal(valuePrefix, prefix)
}
func calculateShare(total int, numParts int, partNum int) int {
share := total / numParts
remainder := total % numParts
if partNum < remainder {
share++
}
return share
}
func constructRandomBytes(length int) []byte {
b := make([]byte, length)
rand.Read(b)
return b
}
func panicOnError(err error) {
if err != nil {
panic(fmt.Errorf("Error:%s", err))
}
}