-
Notifications
You must be signed in to change notification settings - Fork 669
/
service.go
130 lines (106 loc) · 3.7 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package ipcs
import (
"net/http"
"github.com/gorilla/rpc/v2"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/api"
"github.com/ava-labs/avalanchego/api/server"
"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 server.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 server.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.Debug("IPCs: PublishBlockchain called",
logging.UserString("blockchainID", args.BlockchainID),
)
chainID, err := ipc.chainManager.Lookup(args.BlockchainID)
if err != nil {
ipc.log.Error("chain lookup failed",
logging.UserString("blockchainID", args.BlockchainID),
zap.Error(err),
)
return err
}
ipcs, err := ipc.ipcs.Publish(chainID)
if err != nil {
ipc.log.Error("couldn't publish chain",
logging.UserString("blockchainID", args.BlockchainID),
zap.Error(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, _ *api.EmptyReply) error {
ipc.log.Debug("IPCs: UnpublishBlockchain called",
logging.UserString("blockchainID", args.BlockchainID),
)
chainID, err := ipc.chainManager.Lookup(args.BlockchainID)
if err != nil {
ipc.log.Error("chain lookup failed",
logging.UserString("blockchainID", args.BlockchainID),
zap.Error(err),
)
return err
}
ok, err := ipc.ipcs.Unpublish(chainID)
if !ok {
ipc.log.Error("couldn't publish chain",
logging.UserString("blockchainID", args.BlockchainID),
zap.Error(err),
)
}
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
}