forked from Consensys/quorum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
constellation.go
119 lines (108 loc) · 2.45 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
116
117
118
119
package constellation
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/patrickmn/go-cache"
)
type Constellation struct {
node *Client
c *cache.Cache
isConstellationNotInUse bool
}
var (
ErrConstellationIsntInit = errors.New("Constellation not in use")
)
func (g *Constellation) Send(data []byte, from string, to []string) (out []byte, err error) {
if g.isConstellationNotInUse {
return nil, ErrConstellationIsntInit
}
out, err = g.node.SendPayload(data, from, to)
if err != nil {
return nil, err
}
g.c.Set(string(out), data, cache.DefaultExpiration)
return out, nil
}
func (g *Constellation) SendSignedTx(data []byte, to []string) (out []byte, err error) {
if g.isConstellationNotInUse {
return nil, ErrConstellationIsntInit
}
out, err = g.node.SendSignedPayload(data, to)
if err != nil {
return nil, err
}
return out, nil
}
func (g *Constellation) Receive(data []byte) ([]byte, error) {
if g.isConstellationNotInUse {
return nil, nil
}
if len(data) == 0 {
return data, 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.'
dataStr := string(data)
x, found := g.c.Get(dataStr)
if found {
return x.([]byte), nil
}
pl, _ := g.node.ReceivePayload(data)
g.c.Set(dataStr, pl, cache.DefaultExpiration)
return pl, nil
}
func New(path string) (*Constellation, error) {
info, err := os.Lstat(path)
if err != nil {
return nil, err
}
// We accept either the socket or a configuration file that points to
// a socket.
isSocket := info.Mode()&os.ModeSocket != 0
if !isSocket {
cfg, err := LoadConfig(path)
if err != nil {
return nil, err
}
path = filepath.Join(cfg.WorkDir, cfg.Socket)
}
err = RunNode(path)
if err != nil {
return nil, err
}
n, err := NewClient(path)
if err != nil {
return nil, err
}
return &Constellation{
node: n,
c: cache.New(5*time.Minute, 5*time.Minute),
isConstellationNotInUse: false,
}, nil
}
func MustNew(path string) *Constellation {
if strings.EqualFold(path, "ignore") {
return &Constellation{
node: nil,
c: nil,
isConstellationNotInUse: true,
}
}
g, err := New(path)
if err != nil {
panic(fmt.Sprintf("MustNew: Failed to connect to Constellation (%s): %v", path, err))
}
return g
}
func MaybeNew(path string) *Constellation {
if path == "" {
return nil
}
return MustNew(path)
}