-
Notifications
You must be signed in to change notification settings - Fork 669
/
service.go
111 lines (90 loc) · 3.38 KB
/
service.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package ipcs
import (
"fmt"
"net/http"
"github.com/gorilla/rpc/v2"
"github.com/ava-labs/avalanchego/api"
"github.com/ava-labs/avalanchego/chains"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/ipcs"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/utils/json"
"github.com/ava-labs/avalanchego/utils/logging"
)
// IPCServer maintains the IPCs
type IPCServer struct {
httpServer *api.Server
chainManager chains.Manager
log logging.Logger
ipcs *ipcs.ChainIPCs
}
// NewService returns a new IPCs API service
func NewService(log logging.Logger, chainManager chains.Manager, httpServer *api.Server, ipcs *ipcs.ChainIPCs) (*common.HTTPHandler, error) {
ipcServer := &IPCServer{
log: log,
chainManager: chainManager,
httpServer: httpServer,
ipcs: ipcs,
}
newServer := rpc.NewServer()
codec := json.NewCodec()
newServer.RegisterCodec(codec, "application/json")
newServer.RegisterCodec(codec, "application/json;charset=UTF-8")
return &common.HTTPHandler{Handler: newServer}, newServer.RegisterService(ipcServer, "ipcs")
}
// PublishBlockchainArgs are the arguments for calling PublishBlockchain
type PublishBlockchainArgs struct {
BlockchainID string `json:"blockchainID"`
}
// PublishBlockchainReply are the results from calling PublishBlockchain
type PublishBlockchainReply struct {
ConsensusURL string `json:"consensusURL"`
DecisionsURL string `json:"decisionsURL"`
}
// PublishBlockchain publishes the finalized accepted transactions from the blockchainID over the IPC
func (ipc *IPCServer) PublishBlockchain(r *http.Request, args *PublishBlockchainArgs, reply *PublishBlockchainReply) error {
ipc.log.Info("IPCs: PublishBlockchain called with BlockchainID: %s", args.BlockchainID)
chainID, err := ipc.chainManager.Lookup(args.BlockchainID)
if err != nil {
ipc.log.Error("unknown blockchainID: %s", err)
return err
}
ipcs, err := ipc.ipcs.Publish(chainID)
if err != nil {
ipc.log.Error("couldn't publish blockchainID: %s", err)
return err
}
reply.ConsensusURL = ipcs.ConsensusURL()
reply.DecisionsURL = ipcs.DecisionsURL()
return nil
}
// UnpublishBlockchainArgs are the arguments for calling UnpublishBlockchain
type UnpublishBlockchainArgs struct {
BlockchainID string `json:"blockchainID"`
}
// UnpublishBlockchain closes publishing of a blockchainID
func (ipc *IPCServer) UnpublishBlockchain(r *http.Request, args *UnpublishBlockchainArgs, reply *api.SuccessResponse) error {
ipc.log.Info("IPCs: UnpublishBlockchain called with BlockchainID: %s", args.BlockchainID)
chainID, err := ipc.chainManager.Lookup(args.BlockchainID)
if err != nil {
ipc.log.Error("unknown blockchainID %s: %s", args.BlockchainID, err)
return err
}
ok, err := ipc.ipcs.Unpublish(chainID)
if !ok {
return fmt.Errorf("blockchainID not publishing: %s", chainID)
}
reply.Success = true
return err
}
// GetPublishedBlockchainsReply is the result from calling GetPublishedBlockchains
type GetPublishedBlockchainsReply struct {
Chains []ids.ID `json:"chains"`
}
// GetPublishedBlockchains returns blockchains being published
func (ipc *IPCServer) GetPublishedBlockchains(r *http.Request, args *struct{}, reply *GetPublishedBlockchainsReply) error {
reply.Chains = ipc.ipcs.GetPublishedBlockchains()
return nil
}