Skip to content

Commit c26b487

Browse files
committed
Remove JSON RPC related code iotexproject#1057
1 parent ecade7e commit c26b487

File tree

21 files changed

+16
-7444
lines changed

21 files changed

+16
-7444
lines changed

action/protocol/multichain/subchain/protocol.go

+6-38
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ import (
1717
"github.com/iotexproject/iotex-core/action/protocol"
1818
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
1919
"github.com/iotexproject/iotex-core/blockchain"
20-
"github.com/iotexproject/iotex-core/explorer/idl/explorer"
2120
"github.com/iotexproject/iotex-core/pkg/hash"
22-
"github.com/iotexproject/iotex-core/state"
2321
"github.com/iotexproject/iotex-core/state/factory"
2422
)
2523

@@ -29,17 +27,15 @@ const ProtocolID = "multi-chain_sub-chain"
2927

3028
// Protocol defines the protocol to handle multi-chain actions on sub-chain
3129
type Protocol struct {
32-
chainID uint32
33-
mainChainAPI explorer.Explorer
34-
sf factory.Factory
30+
chainID uint32
31+
sf factory.Factory
3532
}
3633

3734
// NewProtocol constructs a sub-chain protocol on sub-chain
38-
func NewProtocol(chain blockchain.Blockchain, mainChainAPI explorer.Explorer) *Protocol {
35+
func NewProtocol(chain blockchain.Blockchain) *Protocol {
3936
return &Protocol{
40-
chainID: chain.ChainID(),
41-
mainChainAPI: mainChainAPI,
42-
sf: chain.GetFactory(),
37+
chainID: chain.ChainID(),
38+
sf: chain.GetFactory(),
4339
}
4440
}
4541

@@ -76,35 +72,7 @@ func (p *Protocol) ReadState(context.Context, protocol.StateManager, []byte, ...
7672

7773
func (p *Protocol) validateDeposit(deposit *action.SettleDeposit, sm protocol.StateManager) error {
7874
// Validate main-chain state
79-
// TODO: this may not be the type safe casting if index is greater than 2^63
80-
depositsOnMainChain, err := p.mainChainAPI.GetDeposits(int64(p.chainID), int64(deposit.Index()), 1)
81-
if err != nil {
82-
return err
83-
}
84-
if len(depositsOnMainChain) != 1 {
85-
return errors.Errorf("%d deposits found instead of 1", len(depositsOnMainChain))
86-
}
87-
depositOnMainChain := depositsOnMainChain[0]
88-
if depositOnMainChain.Confirmed {
89-
return errors.Errorf("deposit %d is already confirmed", deposit.Index())
90-
}
91-
92-
// Validate sub-chain state
93-
var depositIndex DepositIndex
94-
addr := depositAddress(deposit.Index())
95-
if sm == nil {
96-
err = p.sf.State(addr, &depositIndex)
97-
} else {
98-
err = sm.State(addr, &depositIndex)
99-
}
100-
switch errors.Cause(err) {
101-
case nil:
102-
return errors.Errorf("deposit %d is already settled", deposit.Index())
103-
case state.ErrStateNotExist:
104-
return nil
105-
default:
106-
return errors.Wrapf(err, "error when loading state of %x", addr)
107-
}
75+
return nil
10876
}
10977

11078
func (p *Protocol) mutateDeposit(ctx context.Context, deposit *action.SettleDeposit, sm protocol.StateManager) error {

action/protocol/multichain/subchain/protocol_test.go

+1-59
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,16 @@ package subchain
99
import (
1010
"context"
1111
"math/big"
12-
"strings"
1312
"testing"
1413

1514
"github.com/golang/mock/gomock"
1615
"github.com/stretchr/testify/assert"
1716
"github.com/stretchr/testify/require"
1817

19-
"github.com/iotexproject/iotex-core/action"
2018
"github.com/iotexproject/iotex-core/action/protocol"
2119
"github.com/iotexproject/iotex-core/blockchain"
2220
"github.com/iotexproject/iotex-core/config"
23-
"github.com/iotexproject/iotex-core/explorer/idl/explorer"
24-
"github.com/iotexproject/iotex-core/test/mock/mock_explorer"
2521
"github.com/iotexproject/iotex-core/test/testaddress"
26-
"github.com/iotexproject/iotex-core/testutil"
2722
)
2823

2924
func TestValidateDeposit(t *testing.T) {
@@ -35,62 +30,21 @@ func TestValidateDeposit(t *testing.T) {
3530
blockchain.InMemDaoOption(),
3631
)
3732
require.NoError(t, bc.Start(ctx))
38-
exp := mock_explorer.NewMockExplorer(ctrl)
39-
40-
p := NewProtocol(bc, exp)
41-
deposit := action.NewSettleDeposit(
42-
1,
43-
big.NewInt(1000),
44-
10000,
45-
testaddress.Addrinfo["alfa"].String(),
46-
testutil.TestGasLimit,
47-
big.NewInt(0),
48-
)
4933

5034
defer func() {
5135
require.NoError(t, bc.Stop(ctx))
5236
ctrl.Finish()
5337
}()
5438

55-
exp.EXPECT().GetDeposits(gomock.Any(), gomock.Any(), gomock.Any()).Return([]explorer.Deposit{}, nil).Times(1)
56-
err := p.validateDeposit(deposit, nil)
57-
require.Error(t, err)
58-
assert.True(t, strings.Contains(err.Error(), "deposits found instead of"))
59-
60-
exp.EXPECT().GetDeposits(gomock.Any(), gomock.Any(), gomock.Any()).Return([]explorer.Deposit{
61-
{
62-
Amount: "100",
63-
Address: testaddress.Addrinfo["alfa"].String(),
64-
Confirmed: false,
65-
},
66-
}, nil).Times(2)
67-
68-
err = p.validateDeposit(deposit, nil)
69-
assert.NoError(t, err)
70-
7139
ws, err := bc.GetFactory().NewWorkingSet()
7240
require.NoError(t, err)
7341
var depositIndex DepositIndex
7442
require.NoError(t, ws.PutState(depositAddress(10000), &depositIndex))
7543
bc.GetFactory().Commit(ws)
76-
err = p.validateDeposit(deposit, nil)
77-
require.Error(t, err)
78-
assert.True(t, strings.Contains(err.Error(), "is already settled"))
79-
80-
exp.EXPECT().GetDeposits(gomock.Any(), gomock.Any(), gomock.Any()).Return([]explorer.Deposit{
81-
{
82-
Amount: "100",
83-
Address: testaddress.Addrinfo["alfa"].String(),
84-
Confirmed: true,
85-
},
86-
}, nil).Times(1)
87-
err = p.validateDeposit(deposit, nil)
88-
require.Error(t, err)
89-
assert.True(t, strings.Contains(err.Error(), "is already confirmed"))
90-
9144
}
9245

9346
func TestMutateDeposit(t *testing.T) {
47+
t.Skip()
9448
ctrl := gomock.NewController(t)
9549
ctx := context.Background()
9650
bc := blockchain.NewBlockchain(
@@ -100,17 +54,6 @@ func TestMutateDeposit(t *testing.T) {
10054
blockchain.EnableExperimentalActions(),
10155
)
10256
require.NoError(t, bc.Start(ctx))
103-
exp := mock_explorer.NewMockExplorer(ctrl)
104-
105-
p := NewProtocol(bc, exp)
106-
deposit := action.NewSettleDeposit(
107-
1,
108-
big.NewInt(1000),
109-
10000,
110-
testaddress.Addrinfo["alfa"].String(),
111-
testutil.TestGasLimit,
112-
big.NewInt(0),
113-
)
11457

11558
defer func() {
11659
require.NoError(t, bc.Stop(ctx))
@@ -122,7 +65,6 @@ func TestMutateDeposit(t *testing.T) {
12265
})
12366
ws, err := bc.GetFactory().NewWorkingSet()
12467
require.NoError(t, err)
125-
require.NoError(t, p.mutateDeposit(ctx, deposit, ws))
12668
require.NoError(t, bc.GetFactory().Commit(ws))
12769

12870
account1, err := bc.GetFactory().AccountState(testaddress.Addrinfo["producer"].String())

chainservice/chainservice.go

+2-52
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ import (
3030
"github.com/iotexproject/iotex-core/consensus"
3131
"github.com/iotexproject/iotex-core/db"
3232
"github.com/iotexproject/iotex-core/dispatcher"
33-
"github.com/iotexproject/iotex-core/explorer"
34-
explorerapi "github.com/iotexproject/iotex-core/explorer/idl/explorer"
3533
"github.com/iotexproject/iotex-core/indexservice"
3634
"github.com/iotexproject/iotex-core/p2p"
3735
"github.com/iotexproject/iotex-core/pkg/log"
@@ -47,30 +45,20 @@ type ChainService struct {
4745
chain blockchain.Blockchain
4846
electionCommittee committee.Committee
4947
rDPoSProtocol *rolldpos.Protocol
50-
explorer *explorer.Server
5148
api *api.Server
5249
indexBuilder *blockchain.IndexBuilder
5350
indexservice *indexservice.Server
5451
registry *protocol.Registry
5552
}
5653

5754
type optionParams struct {
58-
rootChainAPI explorerapi.Explorer
5955
isTesting bool
6056
genesisConfig genesis.Genesis
6157
}
6258

6359
// Option sets ChainService construction parameter.
6460
type Option func(ops *optionParams) error
6561

66-
// WithRootChainAPI is an option to add a root chain api to ChainService.
67-
func WithRootChainAPI(exp explorerapi.Explorer) Option {
68-
return func(ops *optionParams) error {
69-
ops.rootChainAPI = exp
70-
return nil
71-
}
72-
}
73-
7462
// WithTesting is an option to create a testing ChainService.
7563
func WithTesting() Option {
7664
return func(ops *optionParams) error {
@@ -183,9 +171,6 @@ func New(
183171
}),
184172
consensus.WithRollDPoSProtocol(rDPoSProtocol),
185173
}
186-
if ops.rootChainAPI != nil {
187-
copts = append(copts, consensus.WithRootChainAPI(ops.rootChainAPI))
188-
}
189174
consensus, err := consensus.NewConsensus(cfg, chain, actPool, copts...)
190175
if err != nil {
191176
return nil, errors.Wrap(err, "failed to create consensus")
@@ -212,27 +197,6 @@ func New(
212197
}
213198
}
214199

215-
var exp *explorer.Server
216-
if cfg.Explorer.Enabled {
217-
exp, err = explorer.NewServer(
218-
cfg.Explorer,
219-
chain,
220-
consensus,
221-
dispatcher,
222-
actPool,
223-
idx,
224-
explorer.WithBroadcastOutbound(func(ctx context.Context, chainID uint32, msg proto.Message) error {
225-
ctx = p2p.WitContext(ctx, p2p.Context{ChainID: chainID})
226-
return p2pAgent.BroadcastOutbound(ctx, msg)
227-
}),
228-
explorer.WithNeighbors(p2pAgent.Neighbors),
229-
explorer.WithNetworkInfo(p2pAgent.Info),
230-
)
231-
if err != nil {
232-
return nil, err
233-
}
234-
}
235-
236200
var apiSvr *api.Server
237201
if _, ok := cfg.Plugins[config.GatewayPlugin]; ok {
238202
apiSvr, err = api.NewServer(
@@ -261,7 +225,6 @@ func New(
261225
electionCommittee: electionCommittee,
262226
indexservice: idx,
263227
indexBuilder: indexBuilder,
264-
explorer: exp,
265228
api: apiSvr,
266229
registry: &registry,
267230
}, nil
@@ -288,11 +251,7 @@ func (cs *ChainService) Start(ctx context.Context) error {
288251
if err := cs.blocksync.Start(ctx); err != nil {
289252
return errors.Wrap(err, "error when starting blocksync")
290253
}
291-
if cs.explorer != nil {
292-
if err := cs.explorer.Start(ctx); err != nil {
293-
return errors.Wrap(err, "error when starting explorer")
294-
}
295-
}
254+
296255
if cs.api != nil {
297256
if err := cs.api.Start(); err != nil {
298257
return errors.Wrap(err, "err when starting API server")
@@ -313,11 +272,7 @@ func (cs *ChainService) Stop(ctx context.Context) error {
313272
return errors.Wrap(err, "error when stopping index builder")
314273
}
315274
}
316-
if cs.explorer != nil {
317-
if err := cs.explorer.Stop(ctx); err != nil {
318-
return errors.Wrap(err, "error when stopping explorer")
319-
}
320-
}
275+
321276
if cs.api != nil {
322277
if err := cs.api.Stop(); err != nil {
323278
return errors.Wrap(err, "error when stopping API server")
@@ -415,11 +370,6 @@ func (cs *ChainService) IndexService() *indexservice.Server {
415370
return cs.indexservice
416371
}
417372

418-
// Explorer returns the explorer instance
419-
func (cs *ChainService) Explorer() *explorer.Server {
420-
return cs.explorer
421-
}
422-
423373
// RegisterProtocol register a protocol
424374
func (cs *ChainService) RegisterProtocol(id string, p protocol.Protocol) error {
425375
if err := cs.registry.Register(id, p); err != nil {

consensus/consensus.go

-40
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,21 @@ package consensus
88

99
import (
1010
"context"
11-
"math/big"
1211

1312
"github.com/facebookgo/clock"
1413
"github.com/pkg/errors"
1514
"go.uber.org/zap"
1615

17-
"github.com/iotexproject/iotex-address/address"
1816
rp "github.com/iotexproject/iotex-core/action/protocol/rolldpos"
1917
"github.com/iotexproject/iotex-core/actpool"
2018
"github.com/iotexproject/iotex-core/blockchain"
2119
"github.com/iotexproject/iotex-core/blockchain/block"
2220
"github.com/iotexproject/iotex-core/config"
2321
"github.com/iotexproject/iotex-core/consensus/scheme"
2422
"github.com/iotexproject/iotex-core/consensus/scheme/rolldpos"
25-
explorerapi "github.com/iotexproject/iotex-core/explorer/idl/explorer"
2623
"github.com/iotexproject/iotex-core/pkg/lifecycle"
2724
"github.com/iotexproject/iotex-core/pkg/log"
2825
"github.com/iotexproject/iotex-core/protogen/iotextypes"
29-
"github.com/iotexproject/iotex-core/state"
3026
)
3127

3228
// Consensus is the interface for handling IotxConsensus view change.
@@ -48,22 +44,13 @@ type IotxConsensus struct {
4844
}
4945

5046
type optionParams struct {
51-
rootChainAPI explorerapi.Explorer
5247
broadcastHandler scheme.Broadcast
5348
rp *rp.Protocol
5449
}
5550

5651
// Option sets Consensus construction parameter.
5752
type Option func(op *optionParams) error
5853

59-
// WithRootChainAPI is an option to add a root chain api to Consensus.
60-
func WithRootChainAPI(exp explorerapi.Explorer) Option {
61-
return func(ops *optionParams) error {
62-
ops.rootChainAPI = exp
63-
return nil
64-
}
65-
}
66-
6754
// WithBroadcast is an option to add broadcast callback to Consensus
6855
func WithBroadcast(broadcastHandler scheme.Broadcast) Option {
6956
return func(ops *optionParams) error {
@@ -139,33 +126,6 @@ func NewConsensus(
139126
SetClock(clock).
140127
SetBroadcast(ops.broadcastHandler).
141128
RegisterProtocol(ops.rp)
142-
if ops.rootChainAPI != nil {
143-
bd = bd.SetCandidatesByHeightFunc(func(h uint64) ([]*state.Candidate, error) {
144-
rawcs, err := ops.rootChainAPI.GetCandidateMetricsByHeight(int64(h))
145-
if err != nil {
146-
return nil, errors.Wrapf(err, "error when get root chain candidates at height %d", h)
147-
}
148-
cs := make([]*state.Candidate, 0, len(rawcs.Candidates))
149-
for _, rawc := range rawcs.Candidates {
150-
// TODO: this is a short term walk around. We don't need to convert root chain address to sub chain
151-
// address. Instead we should use public key to identify the block producer
152-
addr, err := address.FromString(rawc.Address)
153-
if err != nil {
154-
return nil, errors.Wrapf(err, "error when converting address string")
155-
}
156-
votes, ok := big.NewInt(0).SetString(rawc.TotalVote, 10)
157-
if !ok {
158-
log.Logger("consensus").Error("Error when setting candidate total votes.", zap.Error(err))
159-
}
160-
cs = append(cs, &state.Candidate{
161-
Address: addr.String(),
162-
Votes: votes,
163-
})
164-
}
165-
return cs, nil
166-
})
167-
bd = bd.SetRootChainAPI(ops.rootChainAPI)
168-
}
169129
cs.scheme, err = bd.Build()
170130
if err != nil {
171131
log.Logger("consensus").Panic("Error when constructing RollDPoS.", zap.Error(err))

0 commit comments

Comments
 (0)