-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract.go
164 lines (141 loc) · 3.08 KB
/
contract.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"bytes"
"encoding/json"
"sort"
"strings"
"github.com/orbs-network/orbs-contract-sdk/go/sdk/v1"
"github.com/orbs-network/orbs-contract-sdk/go/sdk/v1/state"
)
//PUBLIC orbs variable
var PUBLIC = sdk.Export(register, verify, search)
//SYSTEM orbs variable
var SYSTEM = sdk.Export(_init)
var dataKey = []byte("HASH_KEYS")
type searchHit struct {
Score float64 `json:"score"`
Source map[string]interface{} `json:"source"`
}
func _init() {
}
func register(phash string, meta string) {
//step 1: verify if meta is valid JSON
var data map[string]interface{}
err := json.Unmarshal([]byte(meta), &data)
if err != nil {
panic("Invalid JSON: " + meta)
}
//step 2: convert meta JSON to a string
bytes, err := json.Marshal(data)
if err != nil {
panic("JSON stringify error: " + meta)
}
//step 3: check for uniqueness
key := []byte(phash)
test := state.ReadString(key)
if test != "" {
panic(phash + " already exists")
}
//step 4: save phash
state.WriteBytes(key, bytes)
//step 5: modify phash collection
s := state.ReadString(dataKey)
if s == "" {
s = phash
} else {
s = s + "," + phash
}
state.WriteString(dataKey, s)
}
func verify(phash string) string {
key := []byte(phash)
s := state.ReadString(key)
if s == "" {
panic(phash + " does not exists")
}
return s
}
func search(phash string, minScore uint64) string {
s := state.ReadString(dataKey)
if s == "" {
return "[]"
}
var (
min float64
keys = make(map[string]float64)
hits []searchHit
)
if minScore == 0 || minScore > 100 {
min = 0.5
} else {
min = float64(minScore) / 100
}
key := []byte(phash)
phs := strings.Split(s, ",")
for _, ph := range phs {
score := hamming(key, []byte(ph))
if score >= min {
keys[ph] = score
}
}
for k, v := range keys {
meta := state.ReadString([]byte(k))
if meta != "" {
var jo map[string]interface{}
err := json.Unmarshal([]byte(meta), &jo)
if err == nil {
hit := searchHit{
Score: v,
Source: jo,
}
hits = append(hits, hit)
}
}
}
sort.SliceStable(hits, func(i, j int) bool {
return hits[i].Score > hits[j].Score
})
bytes, err := json.Marshal(hits)
if err != nil {
return "[]"
}
return string(bytes)
}
func hamming(txt1, txt2 []byte) float64 {
switch bytes.Compare(txt1, txt2) {
case 0: // txt1 == txt2
case 1: // txt1 > txt2
temp := make([]byte, len(txt1))
copy(temp, txt2)
txt2 = temp
case -1: // txt1 < txt2
temp := make([]byte, len(txt2))
copy(temp, txt1)
txt1 = temp
}
if len(txt1) != len(txt2) {
panic("Undefined for sequences of unequal length")
}
count := 0
for idx, b1 := range txt1 {
b2 := txt2[idx]
xor := b1 ^ b2 // 1 if bits are different
//
// bit count (number of 1)
// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive
//
// repeat shifting from left to right (divide by 2)
// until all bits are zero
for x := xor; x > 0; x >>= 1 {
// check if lowest bit is 1
if int(x&1) == 1 {
count++
}
}
}
if count == 0 {
// similarity is 1 for equal texts.
return 1
}
return float64(1) / float64(count)
}