Skip to content

Commit 41ca142

Browse files
authored
Merge pull request #59 from getamis/feature/rpc_api
consensus/pbft,internal: update rpc api and remove UpdateState()
2 parents edebb97 + f2d6b4c commit 41ca142

File tree

7 files changed

+63
-37
lines changed

7 files changed

+63
-37
lines changed

consensus/pbft/backend.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ type Backend interface {
3838
// Broadcast is to send pbft message to all peers
3939
Broadcast(payload []byte) error
4040

41-
// UpdateState is to update the current pbft state to backend
42-
UpdateState(*State) error
43-
4441
// Commit is to deliver a final result to write into blockchain
4542
Commit(Proposal) error
4643

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package simple
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
"github.com/ethereum/go-ethereum/consensus"
58
"github.com/ethereum/go-ethereum/log"
69
)
@@ -11,10 +14,23 @@ type API struct {
1114
backend *simpleBackend
1215
}
1316

14-
func (self *API) ConsensusState() {
15-
log.Info("Dump PBFT state")
17+
// Snapshot returns current state and proposer
18+
func (api *API) Snapshot() {
19+
state, snapshot := api.backend.core.Snapshot()
20+
p := api.backend.valSet.GetProposer().Address()
21+
log.Info("Snapshot", "sequence", snapshot.Sequence, "Round", snapshot.Round,
22+
"state", state, "proposer", p.Hex(),
23+
"hash", snapshot.Preprepare.Proposal.Hash().Hex(),
24+
"prepares", snapshot.Prepares, "commits", snapshot.Commits,
25+
"checkpoint", snapshot.Checkpoints)
1626
}
1727

18-
func (self *API) ViewChange() {
19-
log.Info("Force view change")
28+
// Backlog returns backlogs
29+
func (api *API) Backlog() {
30+
backlog := api.backend.core.Backlog()
31+
logs := make([]string, 0, len(backlog))
32+
for address, q := range backlog {
33+
logs = append(logs, fmt.Sprintf("{%v, %v}", address.Address().Hex(), q.Size()))
34+
}
35+
log.Info("Backlog", "logs", fmt.Sprintf("[%v]", strings.Join(logs, ", ")))
2036
}

consensus/pbft/backends/simple/backend.go

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,20 @@ func New(config *pbft.Config, eventMux *event.TypeMux, privateKey *ecdsa.Private
5454

5555
// ----------------------------------------------------------------------------
5656
type simpleBackend struct {
57-
config *pbft.Config
58-
peerSet *peerSet
59-
valSet pbft.ValidatorSet
60-
eventMux *event.TypeMux
61-
pbftEventMux *event.TypeMux
62-
privateKey *ecdsa.PrivateKey
63-
address common.Address
64-
consensusState *pbft.State
65-
core pbftCore.Engine
66-
logger log.Logger
67-
quitSync chan struct{}
68-
db ethdb.Database
69-
timeout uint64
70-
chain consensus.ChainReader
71-
inserter func(block *types.Block) error
57+
config *pbft.Config
58+
peerSet *peerSet
59+
valSet pbft.ValidatorSet
60+
eventMux *event.TypeMux
61+
pbftEventMux *event.TypeMux
62+
privateKey *ecdsa.PrivateKey
63+
address common.Address
64+
core pbftCore.Engine
65+
logger log.Logger
66+
quitSync chan struct{}
67+
db ethdb.Database
68+
timeout uint64
69+
chain consensus.ChainReader
70+
inserter func(block *types.Block) error
7271

7372
// the channels for pbft engine notifications
7473
viewChange chan bool
@@ -236,12 +235,6 @@ func (sb *simpleBackend) getSignatureAddress(data []byte, sig []byte) (common.Ad
236235
return crypto.PubkeyToAddress(*pubkey), nil
237236
}
238237

239-
// UpdateState implements pbft.Backend.UpdateState
240-
func (sb *simpleBackend) UpdateState(state *pbft.State) error {
241-
sb.consensusState = state
242-
return nil
243-
}
244-
245238
func (sb *simpleBackend) IsProposer() bool {
246239
if sb.valSet == nil {
247240
return false

consensus/pbft/core/core.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ func (s State) String() string {
5959
type Engine interface {
6060
Start(lastSequence *big.Int, lastProposer common.Address) error
6161
Stop() error
62+
63+
// get current state and snapshot
64+
Snapshot() (State, *snapshot)
65+
// get back log
66+
Backlog() map[pbft.Validator]*prque.Prque
6267
}
6368

6469
func New(backend pbft.Backend, config *pbft.Config) Engine {
@@ -201,3 +206,11 @@ func (c *core) setState(state State) {
201206
func (c *core) Address() common.Address {
202207
return c.address
203208
}
209+
210+
func (c *core) Snapshot() (State, *snapshot) {
211+
return c.state, c.current
212+
}
213+
214+
func (c *core) Backlog() map[pbft.Validator]*prque.Prque {
215+
return c.backlogs
216+
}

consensus/pbft/core/message_set.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package core
1818

1919
import (
20+
"fmt"
2021
"io"
2122
"math/big"
23+
"strings"
2224
"sync"
2325

2426
"github.com/ethereum/go-ethereum/common"
@@ -169,3 +171,13 @@ func (ms *messageSet) addVerifiedMessage(msg *message) error {
169171
ms.messages[hash(msg)] = msg
170172
return nil
171173
}
174+
175+
func (ms *messageSet) String() string {
176+
ms.messagesMu.Lock()
177+
defer ms.messagesMu.Unlock()
178+
addresses := make([]string, 0, len(ms.messages))
179+
for _, v := range ms.messages {
180+
addresses = append(addresses, v.Address.Hex())
181+
}
182+
return fmt.Sprintf("[%v]", strings.Join(addresses, ", "))
183+
}

consensus/pbft/core/testbackend_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ func (self *testSystemBackend) Broadcast(message []byte) error {
8181
return nil
8282
}
8383

84-
func (self *testSystemBackend) UpdateState(state *pbft.State) error {
85-
testLogger.Warn("nothing to happen")
86-
return nil
87-
}
88-
8984
func (self *testSystemBackend) ViewChanged(needNewProposal bool) error {
9085
testLogger.Warn("nothing to happen")
9186
return nil

internal/web3ext/web3ext.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,13 +589,13 @@ web3._extend({
589589
methods:
590590
[
591591
new web3._extend.Method({
592-
name: 'consensusState',
593-
call: 'pbft_consensusState',
592+
name: 'snapshot',
593+
call: 'pbft_snapshot',
594594
params: 0
595595
}),
596596
new web3._extend.Method({
597-
name: 'viewChange',
598-
call: 'pbft_viewChange',
597+
name: 'backlog',
598+
call: 'pbft_backlog',
599599
params: 0
600600
}),
601601
],

0 commit comments

Comments
 (0)