forked from Consensys/quorum
/
constellation.go
115 lines (97 loc) · 3.56 KB
/
constellation.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
package constellation
import (
"fmt"
"github.com/ethereum/go-ethereum/private/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/private/cache"
gocache "github.com/patrickmn/go-cache"
)
type constellation struct {
node *Client
c *gocache.Cache
}
func Is(ptm interface{}) bool {
_, ok := ptm.(*constellation)
return ok
}
func New(client *engine.Client) *constellation {
return &constellation{
node: &Client{
httpClient: client.HttpClient,
},
c: gocache.New(cache.DefaultExpiration, cache.CleanupInterval),
}
}
func (g *constellation) Send(data []byte, from string, to []string, extra *engine.ExtraMetadata) (string, []string, common.EncryptedPayloadHash, error) {
if extra.PrivacyFlag.IsNotStandardPrivate() {
return "", nil, common.EncryptedPayloadHash{}, engine.ErrPrivateTxManagerDoesNotSupportPrivacyEnhancements
}
out, err := g.node.SendPayload(data, from, to, extra.ACHashes, extra.ACMerkleRoot)
if err != nil {
return "", nil, common.EncryptedPayloadHash{}, err
}
cacheKey := string(out.Bytes())
g.c.Set(cacheKey, cache.PrivateCacheItem{
Payload: data,
Extra: *extra,
}, cache.DefaultExpiration)
return "", nil, out, nil
}
func (g *constellation) EncryptPayload(data []byte, from string, to []string, extra *engine.ExtraMetadata) ([]byte, error) {
return nil, engine.ErrPrivateTxManagerNotSupported
}
func (g *constellation) DecryptPayload(payload common.DecryptRequest) ([]byte, *engine.ExtraMetadata, error) {
return nil, nil, engine.ErrPrivateTxManagerNotSupported
}
func (g *constellation) StoreRaw(data []byte, from string) (common.EncryptedPayloadHash, error) {
return common.EncryptedPayloadHash{}, engine.ErrPrivateTxManagerNotSupported
}
func (g *constellation) SendSignedTx(data common.EncryptedPayloadHash, to []string, extra *engine.ExtraMetadata) (string, []string, []byte, error) {
return "", nil, nil, engine.ErrPrivateTxManagerNotSupported
}
func (g *constellation) ReceiveRaw(data common.EncryptedPayloadHash) ([]byte, string, *engine.ExtraMetadata, error) {
return nil, "", nil, engine.ErrPrivateTxManagerNotSupported
}
func (g *constellation) IsSender(txHash common.EncryptedPayloadHash) (bool, error) {
return false, engine.ErrPrivateTxManagerNotSupported
}
func (g *constellation) GetParticipants(txHash common.EncryptedPayloadHash) ([]string, error) {
return nil, engine.ErrPrivateTxManagerNotSupported
}
func (g *constellation) Receive(data common.EncryptedPayloadHash) (string, []string, []byte, *engine.ExtraMetadata, error) {
if common.EmptyEncryptedPayloadHash(data) {
return "", nil, nil, nil, nil
}
// Ignore this error since not being a recipient of
// a payload isn't an error.
// TODO: Return an error if it's anything OTHER than
// 'you are not a recipient.'
cacheKey := string(data.Bytes())
x, found := g.c.Get(cacheKey)
if found {
cacheItem, ok := x.(cache.PrivateCacheItem)
if !ok {
return "", nil, nil, nil, fmt.Errorf("unknown cache item. expected type PrivateCacheItem")
}
return "", nil, cacheItem.Payload, &cacheItem.Extra, nil
}
privatePayload, acHashes, acMerkleRoot, err := g.node.ReceivePayload(data)
if nil != err {
return "", nil, nil, nil, err
}
extra := engine.ExtraMetadata{
ACHashes: acHashes,
ACMerkleRoot: acMerkleRoot,
}
g.c.Set(cacheKey, cache.PrivateCacheItem{
Payload: privatePayload,
Extra: extra,
}, cache.DefaultExpiration)
return "", nil, privatePayload, &extra, nil
}
func (g *constellation) Name() string {
return "Constellation"
}
func (g *constellation) HasFeature(f engine.PrivateTransactionManagerFeature) bool {
return false
}