-
Notifications
You must be signed in to change notification settings - Fork 662
/
chainipc.go
89 lines (76 loc) · 2.56 KB
/
chainipc.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package ipcs
import (
"fmt"
"path/filepath"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/triggers"
"github.com/ava-labs/avalanchego/utils/logging"
)
const (
// DefaultBaseURL can be used as a reasonable default value for the base URL
DefaultBaseURL = "/tmp"
ipcIdentifierPrefix = "ipc"
ipcConsensusIdentifier = "consensus"
ipcDecisionsIdentifier = "decisions"
)
type context struct {
log logging.Logger
networkID uint32
path string
}
// ChainIPCs maintains IPCs for a set of chains
type ChainIPCs struct {
context
chains map[[32]byte]*EventSockets
consensusEvents *triggers.EventDispatcher
decisionEvents *triggers.EventDispatcher
}
// NewChainIPCs creates a new *ChainIPCs that writes consensus and decision
// events to IPC sockets
func NewChainIPCs(log logging.Logger, path string, networkID uint32, consensusEvents *triggers.EventDispatcher, decisionEvents *triggers.EventDispatcher, defaultChainIDs []ids.ID) (*ChainIPCs, error) {
cipcs := &ChainIPCs{
context: context{
log: log,
networkID: networkID,
path: path,
},
chains: make(map[[32]byte]*EventSockets),
consensusEvents: consensusEvents,
decisionEvents: decisionEvents,
}
for _, chainID := range defaultChainIDs {
if _, err := cipcs.Publish(chainID); err != nil {
return nil, err
}
}
return cipcs, nil
}
// Publish creates a set of eventSockets for the given chainID
func (cipcs *ChainIPCs) Publish(chainID ids.ID) (*EventSockets, error) {
if es, ok := cipcs.chains[chainID]; ok {
cipcs.log.Info("returning existing blockchainID %s", chainID.String())
return es, nil
}
es, err := newEventSockets(cipcs.context, chainID, cipcs.consensusEvents, cipcs.decisionEvents)
if err != nil {
cipcs.log.Error("can't create ipcs: %s", err)
return nil, err
}
cipcs.chains[chainID] = es
cipcs.log.Info("created IPC sockets for blockchain %s at %s and %s", chainID.String(), es.ConsensusURL(), es.DecisionsURL())
return es, nil
}
// Unpublish stops the eventSocket for the given chain if it exists. It returns
// whether or not the socket existed and errors when trying to close it
func (cipcs *ChainIPCs) Unpublish(chainID ids.ID) (bool, error) {
chainIPCs, ok := cipcs.chains[chainID]
if !ok {
return false, nil
}
return true, chainIPCs.stop()
}
func ipcURL(ctx context, chainID ids.ID, eventType string) string {
return filepath.Join(ctx.path, fmt.Sprintf("%d-%s-%s", ctx.networkID, chainID.String(), eventType))
}