Skip to content

Commit 3191b13

Browse files
calmbeingunclezoro
andcommitted
integrate vote pool related components into eth backend (#13)
* integrate vote pool related components into eth backend * update * update * init parlia with votepool Co-authored-by: zjubfd <296179868@qq.com>
1 parent f1507cb commit 3191b13

16 files changed

Lines changed: 192 additions & 88 deletions

File tree

cmd/geth/blsaccountcmd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"path/filepath"
1111
"strings"
1212

13-
"github.com/ethereum/go-ethereum/cmd/utils"
1413
"github.com/google/uuid"
1514
"github.com/logrusorgru/aurora"
1615
"github.com/prysmaticlabs/prysm/crypto/bls"
@@ -25,6 +24,8 @@ import (
2524
"github.com/prysmaticlabs/prysm/validator/keymanager/imported"
2625
keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4"
2726
"gopkg.in/urfave/cli.v1"
27+
28+
"github.com/ethereum/go-ethereum/cmd/utils"
2829
)
2930

3031
const (

cmd/geth/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ var (
168168
utils.CatalystFlag,
169169
utils.BlockAmountReserved,
170170
utils.CheckSnapshotWithMPT,
171+
utils.BLSPassWordDirFlag,
172+
utils.BLSWalletDirFlag,
173+
utils.VoteJournalDirFlag,
171174
}
172175

173176
rpcFlags = []cli.Flag{

cmd/geth/usage.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ var AppHelpFlagGroups = []flags.FlagGroup{
6060
utils.TriesInMemoryFlag,
6161
utils.BlockAmountReserved,
6262
utils.CheckSnapshotWithMPT,
63+
utils.BLSPassWordDirFlag,
64+
utils.BLSWalletDirFlag,
65+
utils.VoteJournalDirFlag,
6366
},
6467
},
6568
{

cmd/utils/flags.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,21 @@ var (
842842
Name: "check-snapshot-with-mpt",
843843
Usage: "Enable checking between snapshot and MPT ",
844844
}
845+
846+
BLSPassWordDirFlag = DirectoryFlag{
847+
Name: "blspassword",
848+
Usage: "Directory for the BLS password (default = inside the datadir)",
849+
}
850+
851+
BLSWalletDirFlag = DirectoryFlag{
852+
Name: "blswallet",
853+
Usage: "Directory for the bls wallet path of fast finality (default = inside the datadir)",
854+
}
855+
856+
VoteJournalDirFlag = DirectoryFlag{
857+
Name: "vote-journal-path",
858+
Usage: "Directory for the journal path of fast finality (default = inside the datadir)",
859+
}
845860
)
846861

847862
// MakeDataDir retrieves the currently requested data directory, terminating
@@ -1293,6 +1308,8 @@ func SetNodeConfig(ctx *cli.Context, cfg *node.Config) {
12931308
setNodeUserIdent(ctx, cfg)
12941309
setDataDir(ctx, cfg)
12951310
setSmartCard(ctx, cfg)
1311+
setBLSWalletDir(ctx, cfg)
1312+
setVoteJournalDir(ctx, cfg)
12961313

12971314
if ctx.GlobalIsSet(ExternalSignerFlag.Name) {
12981315
cfg.ExternalSigner = ctx.GlobalString(ExternalSignerFlag.Name)
@@ -1322,6 +1339,10 @@ func SetNodeConfig(ctx *cli.Context, cfg *node.Config) {
13221339
if ctx.GlobalIsSet(InsecureUnlockAllowedFlag.Name) {
13231340
cfg.InsecureUnlockAllowed = ctx.GlobalBool(InsecureUnlockAllowedFlag.Name)
13241341
}
1342+
1343+
if ctx.GlobalIsSet(BLSPassWordDirFlag.Name) {
1344+
cfg.BLSPassWordDir = ctx.GlobalString(BLSPassWordDirFlag.Name)
1345+
}
13251346
}
13261347

13271348
func setSmartCard(ctx *cli.Context, cfg *node.Config) {
@@ -1371,6 +1392,24 @@ func setDataDir(ctx *cli.Context, cfg *node.Config) {
13711392
}
13721393
}
13731394

1395+
func setVoteJournalDir(ctx *cli.Context, cfg *node.Config) {
1396+
dataDir := cfg.DataDir
1397+
if ctx.GlobalIsSet(VoteJournalDirFlag.Name) {
1398+
cfg.VoteJournalDir = ctx.GlobalString(VoteJournalDirFlag.Name)
1399+
} else {
1400+
cfg.VoteJournalDir = filepath.Join(dataDir, "voteJournal")
1401+
}
1402+
}
1403+
1404+
func setBLSWalletDir(ctx *cli.Context, cfg *node.Config) {
1405+
dataDir := cfg.DataDir
1406+
if ctx.GlobalIsSet(BLSWalletDirFlag.Name) {
1407+
cfg.BLSWalletDir = ctx.GlobalString(BLSWalletDirFlag.Name)
1408+
} else {
1409+
cfg.BLSWalletDir = filepath.Join(dataDir, "bls/wallet")
1410+
}
1411+
}
1412+
13741413
func setGPO(ctx *cli.Context, cfg *gasprice.Config, light bool) {
13751414
// If we are running the light client, apply another group
13761415
// settings for gas oracle.

consensus/consensus.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ type ChainHeaderReader interface {
5454
GetHighestVerifiedHeader() *types.Header
5555
}
5656

57+
type VotePool interface {
58+
FetchVoteByHash(blockHash common.Hash) []*types.VoteEnvelope
59+
}
60+
5761
// ChainReader defines a small collection of methods needed to access the local
5862
// blockchain during header and/or uncle verification.
5963
type ChainReader interface {
@@ -145,7 +149,7 @@ type PoSA interface {
145149
EnoughDistance(chain ChainReader, header *types.Header) bool
146150
IsLocalBlock(header *types.Header) bool
147151
AllowLightProcess(chain ChainReader, currentHeader *types.Header) bool
148-
149152
// VerifyVote will verify if the vote comes from valid validators based on voteAddress (BLSPublicKey).
150153
VerifyVote(chain ChainHeaderReader, vote *types.VoteEnvelope) bool
154+
SetVotePool(votePool VotePool)
151155
}

consensus/parlia/parlia.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,6 @@ func ParliaRLP(header *types.Header, chainId *big.Int) []byte {
194194
return b.Bytes()
195195
}
196196

197-
type VotePool interface {
198-
FetchVoteByHash(blockHash common.Hash) []*types.VoteEnvelope
199-
}
200-
201197
// Parlia is the consensus engine of BSC
202198
type Parlia struct {
203199
chainConfig *params.ChainConfig // Chain config
@@ -217,7 +213,7 @@ type Parlia struct {
217213
lock sync.RWMutex // Protects the signer fields
218214

219215
ethAPI *ethapi.PublicBlockChainAPI
220-
votePool VotePool
216+
votePool consensus.VotePool
221217
validatorSetABI abi.ABI
222218
slashABI abi.ABI
223219

@@ -684,7 +680,7 @@ func (p *Parlia) PrepareValidators(chain consensus.ChainHeaderReader, header *ty
684680
}
685681

686682
func (p *Parlia) PrepareVoteAttestation(chain consensus.ChainHeaderReader, header *types.Header) error {
687-
if !p.chainConfig.IsBoneh(header.Number) {
683+
if !p.chainConfig.IsBoneh(header.Number) || p.votePool == nil {
688684
return nil
689685
}
690686

@@ -1515,6 +1511,10 @@ func (p *Parlia) applyTransaction(
15151511
return nil
15161512
}
15171513

1514+
func (p *Parlia) SetVotePool(votePool consensus.VotePool) {
1515+
p.votePool = votePool
1516+
}
1517+
15181518
// =========================== utility function ==========================
15191519
// SealHash returns the hash of a block prior to it being sealed.
15201520
func SealHash(header *types.Header, chainId *big.Int) (hash common.Hash) {

core/vote/vote_manager.go

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
package vote
1717

1818
import (
19+
"context"
20+
"io/ioutil"
21+
22+
"github.com/prysmaticlabs/prysm/validator/accounts/iface"
23+
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
24+
1925
"github.com/ethereum/go-ethereum/core"
2026
"github.com/ethereum/go-ethereum/core/types"
2127
"github.com/ethereum/go-ethereum/eth/downloader"
@@ -43,19 +49,59 @@ type VoteManager struct {
4349
journal *VoteJournal
4450
}
4551

46-
func NewVoteManager(mux *event.TypeMux, chainconfig *params.ChainConfig, chain *core.BlockChain, journal *VoteJournal, signer *VoteSigner, pool *VotePool) (*VoteManager, error) {
52+
func NewVoteManager(mux *event.TypeMux, chainconfig *params.ChainConfig, chain *core.BlockChain, pool *VotePool, journalPath, bLSPassWordPath, bLSWalletPath string) (*VoteManager, error) {
4753
voteManager := &VoteManager{
4854
mux: mux,
4955

5056
chain: chain,
5157
chainconfig: chainconfig,
5258
chainHeadCh: make(chan core.ChainHeadEvent, chainHeadChanSize),
5359

54-
signer: signer,
55-
journal: journal,
56-
pool: pool,
60+
pool: pool,
61+
}
62+
63+
dirExists, err := wallet.Exists(bLSWalletPath)
64+
if err != nil {
65+
log.Error("Check BLS wallet exists error: %v.", err)
66+
}
67+
if !dirExists {
68+
log.Error("BLS wallet did not exists.")
69+
}
70+
71+
walletPassword, err := ioutil.ReadFile(bLSPassWordPath)
72+
if err != nil {
73+
log.Error("Read BLS wallet password error: %v.", err)
74+
return nil, err
75+
}
76+
77+
w, err := wallet.OpenWallet(context.Background(), &wallet.Config{
78+
WalletDir: bLSWalletPath,
79+
WalletPassword: string(walletPassword),
80+
})
81+
if err != nil {
82+
log.Error("Open BLS wallet failed: %v.", err)
83+
return nil, err
84+
}
85+
86+
km, err := w.InitializeKeymanager(context.Background(), iface.InitKeymanagerConfig{ListenForChanges: false})
87+
if err != nil {
88+
log.Error("Initialize key manager failed: %v.", err)
89+
return nil, err
90+
}
91+
92+
voteJournal, err := NewVoteJournal(journalPath)
93+
if err != nil {
94+
return nil, err
95+
}
96+
voteManager.journal = voteJournal
97+
98+
voteSigner, err := NewVoteSigner(&km)
99+
if err != nil {
100+
return nil, err
57101
}
102+
voteManager.signer = voteSigner
58103

104+
// Subscribe to chain head event.
59105
voteManager.chainHeadSub = voteManager.chain.SubscribeChainHeadEvent(voteManager.chainHeadCh)
60106

61107
go voteManager.loop()

core/vote/vote_pool.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ func NewVotePool(chainconfig *params.ChainConfig, chain *core.BlockChain, engine
9393

9494
// Subscribe events from blockchain and start the main event loop.
9595
votePool.chainHeadSub = votePool.chain.SubscribeChainHeadEvent(votePool.chainHeadCh)
96+
if posa, ok := engine.(consensus.PoSA); ok {
97+
posa.SetVotePool(votePool)
98+
}
9699

97100
go votePool.loop()
98-
99101
return votePool
100102
}
101103

core/vote/vote_pool_test.go

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,6 @@ var (
5858
timeThreshold = 30
5959
)
6060

61-
func setUpVoteJournal(t *testing.T) *VoteJournal {
62-
// Create a temporary file for the votes journal
63-
file, err := ioutil.TempFile("", "")
64-
if err != nil {
65-
t.Fatalf("failed to create temporary file path: %v", err)
66-
}
67-
journal := file.Name()
68-
defer os.Remove(journal)
69-
70-
// Clean up the temporary file, we only need the path for now
71-
file.Close()
72-
os.Remove(journal)
73-
74-
voteJournal, err := NewVoteJournal(journal)
75-
if err != nil {
76-
t.Fatalf("failed to create temporary votes journal: %v", err)
77-
}
78-
79-
return voteJournal
80-
}
81-
8261
func (pool *VotePool) verifyStructureSizeOfVotePool(receivedVotes, curVotes, futureVotes, curVotesPq, futureVotesPq int) bool {
8362
for i := 0; i < timeThreshold; i++ {
8463
time.Sleep(1 * time.Second)
@@ -104,10 +83,7 @@ func (journal *VoteJournal) verifyJournal(size, lastLatestVoteNumber int) bool {
10483
}
10584

10685
func TestVotePool(t *testing.T) {
107-
km := setUpKeyManager(t)
108-
109-
// Create vote Signer
110-
voteSigner, _ := NewVoteSigner(km)
86+
walletPasswordDir, walletDir := setUpKeyManager(t)
11187

11288
// Create a database pre-initialize with a genesis block
11389
db := rawdb.NewMemoryDatabase()
@@ -119,18 +95,29 @@ func TestVotePool(t *testing.T) {
11995

12096
mux := new(event.TypeMux)
12197

122-
// Create vote journal
123-
voteJournal := setUpVoteJournal(t)
124-
12598
// Create vote pool
12699
votePool := NewVotePool(params.TestChainConfig, chain, ethash.NewFaker())
127100

128101
// Create vote manager
129-
voteManager, err := NewVoteManager(mux, params.TestChainConfig, chain, voteJournal, voteSigner, votePool)
102+
// Create a temporary file for the votes journal
103+
file, err := ioutil.TempFile("", "")
104+
if err != nil {
105+
t.Fatalf("failed to create temporary file path: %v", err)
106+
}
107+
journal := file.Name()
108+
defer os.Remove(journal)
109+
110+
// Clean up the temporary file, we only need the path for now
111+
file.Close()
112+
os.Remove(journal)
113+
114+
voteManager, err := NewVoteManager(mux, params.TestChainConfig, chain, votePool, journal, walletPasswordDir, walletDir)
130115
if err != nil {
131116
t.Fatalf("failed to create vote managers")
132117
}
133118

119+
voteJournal := voteManager.journal
120+
134121
// Send the done event of downloader
135122
time.Sleep(10 * time.Millisecond)
136123
mux.Post(downloader.DoneEvent{})
@@ -267,15 +254,24 @@ func TestVotePool(t *testing.T) {
267254
}
268255
}
269256

270-
func setUpKeyManager(t *testing.T) *keymanager.IKeymanager {
257+
func setUpKeyManager(t *testing.T) (string, string) {
258+
walletDir := filepath.Join(t.TempDir(), "wallet")
271259
walletConfig := &accounts.CreateWalletConfig{
272260
WalletCfg: &wallet.Config{
273-
WalletDir: filepath.Join(t.TempDir(), "wallet"),
261+
WalletDir: walletDir,
274262
KeymanagerKind: keymanager.Imported,
275263
WalletPassword: password,
276264
},
277265
SkipMnemonicConfirm: true,
278266
}
267+
walletPasswordDir := filepath.Join(t.TempDir(), "password")
268+
if err := os.MkdirAll(filepath.Dir(walletPasswordDir), 0700); err != nil {
269+
t.Fatalf("failed to create walletPassword dir: %v", err)
270+
}
271+
if err := ioutil.WriteFile(walletPasswordDir, []byte(password), 0600); err != nil {
272+
t.Fatalf("failed to write wallet password dir: %v", err)
273+
}
274+
279275
w, err := accounts.CreateWalletWithKeymanager(context.Background(), walletConfig)
280276
if err != nil {
281277
t.Fatalf("failed to create wallet: %v", err)
@@ -308,5 +304,5 @@ func setUpKeyManager(t *testing.T) *keymanager.IKeymanager {
308304
Keystores: []*keymanager.Keystore{keystore},
309305
AccountPassword: password,
310306
})
311-
return &km
307+
return walletPasswordDir, walletDir
312308
}

0 commit comments

Comments
 (0)