/
adapter_caller.go
100 lines (73 loc) · 1.72 KB
/
adapter_caller.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
package addrspace
import (
"github.com/golang/protobuf/ptypes/empty"
"github.com/golang/protobuf/ptypes/wrappers"
txgen "hyperledger.abchain.org/chaincode/lib/txgen"
"sync"
)
type GeneralCall struct {
txgen.TxCaller
}
const (
Method_Reg = "ADDRSPACE.REG"
Method_Query = "ADDRSPACE.QUERY"
)
func (i GeneralCall) RegisterCC() error {
return i.Invoke(Method_Reg, new(empty.Empty))
}
func (i GeneralCall) QueryPrefix() ([]byte, error) {
ret, err := i.Query(Method_Query, new(empty.Empty))
if err != nil {
return nil, err
}
p := new(wrappers.BytesValue)
err = txgen.SyncQueryResult(p, ret)
if err != nil {
return nil, err
}
return p.GetValue(), nil
}
//notice: there is not really a NormalizeAddress call
//(we just do a query and use the returning result)
func (i GeneralCall) NormalizeAddress(addr []byte) ([]byte, error) {
prefix, err := i.QueryPrefix()
if err != nil {
return nil, err
}
return append(prefix, addr...), nil
}
func (i GeneralCall) CacheImpl(c *generalCallCache) cachedGeneralCall {
return cachedGeneralCall{c, i}
}
type generalCallCache struct {
sync.RWMutex
prefixCache []byte
}
func CallCache() *generalCallCache { return new(generalCallCache) }
type cachedGeneralCall struct {
*generalCallCache
GeneralCall
}
func (i cachedGeneralCall) QueryPrefix() ([]byte, error) {
i.RLock()
if i.prefixCache != nil {
i.RUnlock()
return i.prefixCache, nil
}
i.RUnlock()
ret, err := i.GeneralCall.QueryPrefix()
if err != nil {
return nil, err
}
i.Lock()
i.prefixCache = ret
i.Unlock()
return ret, nil
}
func (i cachedGeneralCall) NormalizeAddress(addr []byte) ([]byte, error) {
prefix, err := i.QueryPrefix()
if err != nil {
return nil, err
}
return append(prefix, addr...), nil
}