-
Notifications
You must be signed in to change notification settings - Fork 669
/
service.go
139 lines (115 loc) · 3.45 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
131
132
133
134
135
136
137
138
139
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package ipcs
import (
"net/http"
"sync"
"github.com/gorilla/rpc/v2"
"go.uber.org/zap"
"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/utils/json"
"github.com/ava-labs/avalanchego/utils/logging"
)
type Service struct {
log logging.Logger
chainManager chains.Manager
lock sync.RWMutex
ipcs *ipcs.ChainIPCs
}
func NewService(log logging.Logger, chainManager chains.Manager, ipcs *ipcs.ChainIPCs) (http.Handler, error) {
server := rpc.NewServer()
codec := json.NewCodec()
server.RegisterCodec(codec, "application/json")
server.RegisterCodec(codec, "application/json;charset=UTF-8")
return server, server.RegisterService(
&Service{
log: log,
chainManager: chainManager,
ipcs: ipcs,
},
"ipcs",
)
}
type PublishBlockchainArgs struct {
BlockchainID string `json:"blockchainID"`
}
type PublishBlockchainReply struct {
ConsensusURL string `json:"consensusURL"`
DecisionsURL string `json:"decisionsURL"`
}
// PublishBlockchain publishes the finalized accepted transactions from the
// blockchainID over the IPC
func (s *Service) PublishBlockchain(_ *http.Request, args *PublishBlockchainArgs, reply *PublishBlockchainReply) error {
s.log.Warn("deprecated API called",
zap.String("service", "ipcs"),
zap.String("method", "publishBlockchain"),
logging.UserString("blockchainID", args.BlockchainID),
)
chainID, err := s.chainManager.Lookup(args.BlockchainID)
if err != nil {
s.log.Error("chain lookup failed",
logging.UserString("blockchainID", args.BlockchainID),
zap.Error(err),
)
return err
}
s.lock.Lock()
defer s.lock.Unlock()
ipcs, err := s.ipcs.Publish(chainID)
if err != nil {
s.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
}
type UnpublishBlockchainArgs struct {
BlockchainID string `json:"blockchainID"`
}
// UnpublishBlockchain closes publishing of a blockchainID
func (s *Service) UnpublishBlockchain(_ *http.Request, args *UnpublishBlockchainArgs, _ *api.EmptyReply) error {
s.log.Warn("deprecated API called",
zap.String("service", "ipcs"),
zap.String("method", "unpublishBlockchain"),
logging.UserString("blockchainID", args.BlockchainID),
)
chainID, err := s.chainManager.Lookup(args.BlockchainID)
if err != nil {
s.log.Error("chain lookup failed",
logging.UserString("blockchainID", args.BlockchainID),
zap.Error(err),
)
return err
}
s.lock.Lock()
defer s.lock.Unlock()
ok, err := s.ipcs.Unpublish(chainID)
if !ok {
s.log.Error("couldn't publish chain",
logging.UserString("blockchainID", args.BlockchainID),
zap.Error(err),
)
}
return err
}
type GetPublishedBlockchainsReply struct {
Chains []ids.ID `json:"chains"`
}
// GetPublishedBlockchains returns blockchains being published
func (s *Service) GetPublishedBlockchains(_ *http.Request, _ *struct{}, reply *GetPublishedBlockchainsReply) error {
s.log.Warn("deprecated API called",
zap.String("service", "ipcs"),
zap.String("method", "getPublishedBlockchains"),
)
s.lock.RLock()
defer s.lock.RUnlock()
reply.Chains = s.ipcs.GetPublishedBlockchains()
return nil
}