Skip to content

Commit d718079

Browse files
authored
IOTEX-69 Get rid of config.ProducerAddr() (iotexproject#119)
1 parent 513df9d commit d718079

File tree

5 files changed

+46
-55
lines changed

5 files changed

+46
-55
lines changed

config/config.go

+9-27
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ var (
154154

155155
// Validates is the collection config validation functions
156156
Validates = []Validate{
157-
ValidateAddr,
157+
ValidateKeyPair,
158158
ValidateConsensusScheme,
159159
ValidateRollDPoS,
160160
ValidateDispatcher,
@@ -389,24 +389,6 @@ func (cfg *Config) IsLightweight() bool {
389389
return cfg.NodeType == LightweightType
390390
}
391391

392-
// ProducerAddr returns address struct based on the data from producer pub/pri-key in the config
393-
func (cfg *Config) ProducerAddr() (*iotxaddress.Address, error) {
394-
priKey, err := keypair.DecodePrivateKey(cfg.Chain.ProducerPrivKey)
395-
if err != nil {
396-
return nil, err
397-
}
398-
pubKey, err := keypair.DecodePublicKey(cfg.Chain.ProducerPubKey)
399-
if err != nil {
400-
return nil, err
401-
}
402-
addr, err := iotxaddress.GetAddressByPubkey(iotxaddress.IsTestnet, iotxaddress.ChainID, pubKey)
403-
if err != nil {
404-
return nil, err
405-
}
406-
addr.PrivateKey = priKey
407-
return addr, nil
408-
}
409-
410392
// BlockchainAddress returns the address derived from the configured chain ID and public key
411393
func (cfg *Config) BlockchainAddress() (address.Address, error) {
412394
pk, err := keypair.DecodePublicKey(cfg.Chain.ProducerPubKey)
@@ -434,20 +416,20 @@ func (cfg *Config) KeyPair() (keypair.PublicKey, keypair.PrivateKey, error) {
434416
return pk, sk, nil
435417
}
436418

437-
// ValidateAddr validates the block producer address
438-
func ValidateAddr(cfg *Config) error {
439-
addr, err := cfg.ProducerAddr()
419+
// ValidateKeyPair validates the block producer address
420+
func ValidateKeyPair(cfg *Config) error {
421+
priKey, err := keypair.DecodePrivateKey(cfg.Chain.ProducerPrivKey)
440422
if err != nil {
441423
return err
442424
}
443-
// Validate producer's address
444-
if _, err := iotxaddress.GetPubkeyHash(addr.RawAddress); err != nil {
445-
return errors.Wrap(ErrInvalidCfg, "invalid block producer address")
425+
pubKey, err := keypair.DecodePublicKey(cfg.Chain.ProducerPubKey)
426+
if err != nil {
427+
return err
446428
}
447429
// Validate producer pubkey and prikey by signing a dummy message and verify it
448430
validationMsg := "connecting the physical world block by block"
449-
sig := crypto.EC283.Sign(addr.PrivateKey, []byte(validationMsg))
450-
if !crypto.EC283.Verify(addr.PublicKey, []byte(validationMsg), sig) {
431+
sig := crypto.EC283.Sign(priKey, []byte(validationMsg))
432+
if !crypto.EC283.Verify(pubKey, []byte(validationMsg), sig) {
451433
return errors.Wrap(ErrInvalidCfg, "block producer has unmatched pubkey and prikey")
452434
}
453435
return nil

config/config_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,17 @@ chain:
165165
require.Equal(t, LightweightType, cfg.NodeType)
166166
}
167167

168-
func TestValidateAddr(t *testing.T) {
168+
func TestValidateKeyPair(t *testing.T) {
169169
cfg := Default
170170
cfg.Chain.ProducerPubKey = "hello world"
171171
cfg.Chain.ProducerPrivKey = "world hello"
172-
err := ValidateAddr(&cfg)
172+
err := ValidateKeyPair(&cfg)
173173
assert.NotNil(t, err)
174174
assert.True(t, strings.Contains(err.Error(), "encoding/hex:"), err.Error())
175175

176176
cfg.Chain.ProducerPubKey = keypair.EncodePublicKey(testaddress.Addrinfo["alfa"].PublicKey)
177177
cfg.Chain.ProducerPrivKey = keypair.EncodePrivateKey(testaddress.Addrinfo["bravo"].PrivateKey)
178-
err = ValidateAddr(&cfg)
178+
err = ValidateKeyPair(&cfg)
179179
assert.NotNil(t, err)
180180
require.Equal(t, ErrInvalidCfg, errors.Cause(err))
181181
require.True(

consensus/consensus.go

+27-11
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import (
1717
"github.com/iotexproject/iotex-core/config"
1818
"github.com/iotexproject/iotex-core/consensus/scheme"
1919
"github.com/iotexproject/iotex-core/consensus/scheme/rolldpos"
20+
"github.com/iotexproject/iotex-core/iotxaddress"
2021
"github.com/iotexproject/iotex-core/logger"
2122
"github.com/iotexproject/iotex-core/network"
2223
"github.com/iotexproject/iotex-core/pkg/errcode"
24+
"github.com/iotexproject/iotex-core/pkg/keypair"
2325
"github.com/iotexproject/iotex-core/pkg/lifecycle"
2426
)
2527

@@ -57,11 +59,8 @@ func NewConsensus(
5759
Int("votes", len(votes)).
5860
Int("Executions", len(executions)).
5961
Msg("pick actions")
60-
addr, err := cfg.ProducerAddr()
61-
if err != nil {
62-
return nil, err
63-
}
64-
blk, err := bc.MintNewBlock(transfers, votes, executions, addr, "")
62+
63+
blk, err := bc.MintNewBlock(transfers, votes, executions, GetAddr(cfg), "")
6564
if err != nil {
6665
logger.Error().Msg("Failed to mint a block")
6766
return nil, err
@@ -90,15 +89,11 @@ func NewConsensus(
9089
return nil
9190
}
9291

93-
addr, err := cfg.ProducerAddr()
94-
if err != nil {
95-
logger.Panic().Err(err).Msg("Fail to create new consensus")
96-
}
97-
92+
var err error
9893
switch cfg.Consensus.Scheme {
9994
case config.RollDPoSScheme:
10095
cs.scheme, err = rolldpos.NewRollDPoSBuilder().
101-
SetAddr(addr).
96+
SetAddr(GetAddr(cfg)).
10297
SetConfig(cfg.Consensus.RollDPoS).
10398
SetBlockchain(bc).
10499
SetActPool(ap).
@@ -172,3 +167,24 @@ func (c *IotxConsensus) HandleBlockPropose(m proto.Message) error {
172167
func (c *IotxConsensus) Scheme() scheme.Scheme {
173168
return c.scheme
174169
}
170+
171+
// GetAddr returns the iotex address
172+
func GetAddr(cfg *config.Config) *iotxaddress.Address {
173+
addr, err := cfg.BlockchainAddress()
174+
if err != nil {
175+
logger.Panic().Err(err).Msg("Fail to create new consensus")
176+
}
177+
pk, err := keypair.DecodePublicKey(cfg.Chain.ProducerPubKey)
178+
if err != nil {
179+
logger.Panic().Err(err).Msg("Fail to create new consensus")
180+
}
181+
sk, err := keypair.DecodePrivateKey(cfg.Chain.ProducerPrivKey)
182+
if err != nil {
183+
logger.Panic().Err(err).Msg("Fail to create new consensus")
184+
}
185+
return &iotxaddress.Address{
186+
PublicKey: pk,
187+
PrivateKey: sk,
188+
RawAddress: addr.IotxAddress(),
189+
}
190+
}

consensus/sim/consensus_sim.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/iotexproject/iotex-core/actpool"
1616
"github.com/iotexproject/iotex-core/blockchain"
1717
"github.com/iotexproject/iotex-core/config"
18+
consensus "github.com/iotexproject/iotex-core/consensus"
1819
"github.com/iotexproject/iotex-core/consensus/scheme"
1920
"github.com/iotexproject/iotex-core/consensus/scheme/rolldpos"
2021
pbsim "github.com/iotexproject/iotex-core/consensus/sim/proto"
@@ -106,13 +107,9 @@ func NewSim(
106107
}
107108
*/
108109

109-
addr, err := cfg.ProducerAddr()
110-
if err != nil {
111-
logger.Panic().Err(err).Msg("Fail to create new consensus")
112-
}
113-
110+
var err error
114111
cs.scheme, err = rolldpos.NewRollDPoSBuilder().
115-
SetAddr(addr).
112+
SetAddr(consensus.GetAddr(cfg)).
116113
SetConfig(cfg.Consensus.RollDPoS).
117114
SetBlockchain(bc).
118115
SetActPool(ap).
@@ -180,13 +177,9 @@ func NewSimByzantine(
180177
}
181178
*/
182179

183-
addr, err := cfg.ProducerAddr()
184-
if err != nil {
185-
logger.Panic().Err(err).Msg("Fail to create new consensus")
186-
}
187-
180+
var err error
188181
cs.scheme, err = rolldpos.NewRollDPoSBuilder().
189-
SetAddr(addr).
182+
SetAddr(consensus.GetAddr(cfg)).
190183
SetConfig(cfg.Consensus.RollDPoS).
191184
SetBlockchain(bc).
192185
SetActPool(ap).

server/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func main() {
116116
}
117117

118118
func initLogger(cfg *config.Config) {
119-
iotxAddr, err := cfg.ProducerAddr()
119+
addr, err := cfg.BlockchainAddress()
120120
if err != nil {
121121
logger.Fatal().Err(err).Msg("Failed to get producer address from pub/kri key.")
122122
return
@@ -127,7 +127,7 @@ func initLogger(cfg *config.Config) {
127127
} else {
128128
logger.SetLogger(
129129
l.With().
130-
Str("iotexAddr", iotxAddr.RawAddress).
130+
Str("iotxAddr", addr.IotxAddress()).
131131
Str("networkAddress", fmt.Sprintf("%s:%d", cfg.Network.Host, cfg.Network.Port)).
132132
Str("nodeType", cfg.NodeType).Logger(),
133133
)

0 commit comments

Comments
 (0)