forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
335 additions
and
113 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// (c) 2019-2020, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package ipcs | ||
|
||
import ( | ||
"fmt" | ||
"path" | ||
|
||
"github.com/ava-labs/gecko/ids" | ||
"github.com/ava-labs/gecko/snow/triggers" | ||
"github.com/ava-labs/gecko/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) { | ||
chainIDKey := chainID.Key() | ||
|
||
if es, ok := cipcs.chains[chainIDKey]; 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[chainIDKey] = 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.Key()] | ||
if !ok { | ||
return false, nil | ||
} | ||
return true, chainIPCs.stop() | ||
} | ||
|
||
func ipcURL(ctx context, chainID ids.ID, eventType string) string { | ||
return path.Join(ctx.path, fmt.Sprintf("%d-%s-%s", ctx.networkID, chainID.String(), eventType)) | ||
} |
Oops, something went wrong.