Skip to content

Commit 45933fe

Browse files
authored
IOTEX-69 Remove iotexaddress.ChainID and clean up its dependency (iotexproject#149)
1 parent 688a667 commit 45933fe

29 files changed

+430
-387
lines changed

accounts/keystore/accountmanager.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ import (
1212

1313
"github.com/pkg/errors"
1414

15+
"github.com/iotexproject/iotex-core/address"
1516
"github.com/iotexproject/iotex-core/blockchain/action"
17+
"github.com/iotexproject/iotex-core/config"
1618
"github.com/iotexproject/iotex-core/crypto"
1719
"github.com/iotexproject/iotex-core/iotxaddress"
20+
"github.com/iotexproject/iotex-core/pkg/keypair"
1821
)
1922

2023
var (
@@ -60,14 +63,22 @@ func (m *AccountManager) Remove(rawAddr string) error {
6063

6164
// NewAccount creates a new account
6265
func (m *AccountManager) NewAccount() (*iotxaddress.Address, error) {
63-
addr, err := iotxaddress.NewAddress(iotxaddress.IsTestnet, iotxaddress.ChainID)
66+
pk, sk, err := crypto.EC283.NewKeyPair()
6467
if err != nil {
65-
return nil, errors.Wrap(err, "failed to generate a new iotxaddress")
68+
return nil, errors.Wrap(err, "failed to generate key pair")
6669
}
67-
if err := m.keystore.Store(addr.RawAddress, addr); err != nil {
68-
return nil, errors.Wrapf(err, "failed to store account %s", addr.RawAddress)
70+
// TODO: need to fix the chain ID
71+
pkHash := keypair.HashPubKey(pk)
72+
addr := address.New(config.Default.Chain.ID, pkHash[:])
73+
iotxAddr := iotxaddress.Address{
74+
PublicKey: pk,
75+
PrivateKey: sk,
76+
RawAddress: addr.IotxAddress(),
6977
}
70-
return addr, nil
78+
if err := m.keystore.Store(iotxAddr.RawAddress, &iotxAddr); err != nil {
79+
return nil, errors.Wrapf(err, "failed to store account %s", iotxAddr.RawAddress)
80+
}
81+
return &iotxAddr, nil
7182
}
7283

7384
// Import imports key bytes and stores it as a new account

actpool/actpool.go

+4-17
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,7 @@ func (ap *actPool) AddVote(vote *action.Vote) error {
233233
return errors.Wrapf(ErrActPool, "insufficient space for vote")
234234
}
235235

236-
voter, _ := iotxaddress.GetAddressByPubkey(iotxaddress.IsTestnet, iotxaddress.ChainID, vote.VoterPublicKey())
237-
// Wrap vote as an action
238-
action := vote.ConvertToActionPb()
239-
return ap.enqueueAction(voter.RawAddress, action, hash, vote.Nonce())
236+
return ap.enqueueAction(vote.Voter(), vote.ConvertToActionPb(), hash, vote.Nonce())
240237
}
241238

242239
// AddExecution inserts a new execution into account queue if it passes validation
@@ -418,18 +415,13 @@ func (ap *actPool) validateExecution(exec *action.Execution) error {
418415
}
419416
}
420417

421-
executor, err := iotxaddress.GetAddressByPubkey(iotxaddress.IsTestnet, iotxaddress.ChainID, exec.ExecutorPublicKey())
422-
if err != nil {
423-
logger.Error().Err(err).Msg("Error when validating executor's public key")
424-
return errors.Wrapf(err, "invalid address")
425-
}
426418
// Verify transfer using executor's public key
427419
if err := action.Verify(exec); err != nil {
428420
logger.Error().Err(err).Msg("Error when validating execution's signature")
429421
return errors.Wrapf(err, "failed to verify Execution signature")
430422
}
431423
// Reject transfer if nonce is too low
432-
confirmedNonce, err := ap.bc.Nonce(executor.RawAddress)
424+
confirmedNonce, err := ap.bc.Nonce(exec.Executor())
433425
if err != nil {
434426
logger.Error().Err(err).Msg("Error when validating execution's nonce")
435427
return errors.Wrapf(err, "invalid nonce value")
@@ -473,19 +465,14 @@ func (ap *actPool) validateVote(vote *action.Vote) error {
473465
}
474466
}
475467

476-
voter, err := iotxaddress.GetAddressByPubkey(iotxaddress.IsTestnet, iotxaddress.ChainID, vote.VoterPublicKey())
477-
if err != nil {
478-
logger.Error().Err(err).Msg("Error when validating voter's public key")
479-
return errors.Wrapf(err, "invalid address")
480-
}
481468
// Verify vote using voter's public key
482469
if err := action.Verify(vote); err != nil {
483470
logger.Error().Err(err).Msg("Error when validating vote's signature")
484471
return errors.Wrapf(err, "failed to verify vote signature")
485472
}
486473

487474
// Reject vote if nonce is too low
488-
confirmedNonce, err := ap.bc.Nonce(voter.RawAddress)
475+
confirmedNonce, err := ap.bc.Nonce(vote.Voter())
489476
if err != nil {
490477
logger.Error().Err(err).Msg("Error when validating vote's nonce")
491478
return errors.Wrapf(err, "invalid nonce value")
@@ -502,7 +489,7 @@ func (ap *actPool) validateVote(vote *action.Vote) error {
502489
Msg("Error when validating votee's state")
503490
return errors.Wrapf(err, "cannot find votee's state: %s", vote.Votee())
504491
}
505-
if voter.RawAddress != vote.Votee() && !voteeState.IsCandidate {
492+
if vote.Voter() != vote.Votee() && !voteeState.IsCandidate {
506493
logger.Error().
507494
Err(ErrVotee).
508495
Str("voter", vote.Voter()).

actpool/actpool_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"github.com/iotexproject/iotex-core/blockchain"
2121
"github.com/iotexproject/iotex-core/blockchain/action"
2222
"github.com/iotexproject/iotex-core/config"
23-
"github.com/iotexproject/iotex-core/iotxaddress"
24-
"github.com/iotexproject/iotex-core/pkg/enc"
2523
"github.com/iotexproject/iotex-core/proto"
2624
"github.com/iotexproject/iotex-core/test/mock/mock_blockchain"
2725
"github.com/iotexproject/iotex-core/testutil"
@@ -46,7 +44,7 @@ const (
4644
)
4745

4846
var (
49-
chainID = enc.MachineEndian.Uint32(iotxaddress.ChainID)
47+
chainID = config.Default.Chain.ID
5048
addr1 = testutil.ConstructAddress(chainID, pubkeyA, prikeyA)
5149
addr2 = testutil.ConstructAddress(chainID, pubkeyB, prikeyB)
5250
addr3 = testutil.ConstructAddress(chainID, pubkeyC, prikeyC)

address/address_v1_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/iotexproject/iotex-core/crypto"
1818
"github.com/iotexproject/iotex-core/iotxaddress"
19+
"github.com/iotexproject/iotex-core/pkg/enc"
1920
"github.com/iotexproject/iotex-core/pkg/keypair"
2021
)
2122

@@ -83,7 +84,9 @@ func TestAddressError(t *testing.T) {
8384
func TestConvertFromAndToIotxAddress(t *testing.T) {
8485
t.Parallel()
8586

86-
iotxAddr1, err := iotxaddress.NewAddress(iotxaddress.IsTestnet, iotxaddress.ChainID)
87+
var chainID [4]byte
88+
enc.MachineEndian.PutUint32(chainID[:], 1)
89+
iotxAddr1, err := iotxaddress.NewAddress(iotxaddress.IsTestnet, chainID[:])
8790
require.NoError(t, err)
8891
addr, err := V1.IotxAddressToAddress(iotxAddr1.RawAddress)
8992
require.NoError(t, err)

blockchain/block_test.go

+23-17
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/iotexproject/iotex-core/config"
2525
"github.com/iotexproject/iotex-core/crypto"
2626
"github.com/iotexproject/iotex-core/iotxaddress"
27-
"github.com/iotexproject/iotex-core/pkg/enc"
2827
"github.com/iotexproject/iotex-core/pkg/hash"
2928
"github.com/iotexproject/iotex-core/pkg/keypair"
3029
"github.com/iotexproject/iotex-core/pkg/version"
@@ -69,27 +68,33 @@ func TestMerkle(t *testing.T) {
6968
require.NotNil(cbtsf4)
7069

7170
// verify tx hash
72-
hash0, _ := hex.DecodeString("c42f754fdf676a6ac4cdccba96f2dc1055c41c25effc72ac9477e120712e5634")
71+
hash0, e := hex.DecodeString("057f3817e6eefc3eadfb14a3bee75aafd7d1913a54798cc339e394b9860c8bcd")
72+
require.NoError(e)
7373
actual := cbtsf0.Hash()
7474
require.Equal(hash0, actual[:])
7575
t.Logf("actual hash = %x", actual[:])
7676

77-
hash1, _ := hex.DecodeString("2c4bcfb59297b3e472f7c15ff31a3ed080b749a952c18bb585ef517542c8381d")
77+
hash1, e := hex.DecodeString("c5363bbfd115ca7457e34883a0af2274334561e4cc4b7f977df9890c918eeb0e")
78+
require.NoError(e)
7879
actual = cbtsf1.Hash()
7980
require.Equal(hash1, actual[:])
8081
t.Logf("actual hash = %x", actual[:])
8182

82-
hash2, _ := hex.DecodeString("46e07d8753a07d66f9b76797a0e3257fd2b70b019722dfb3394ba51db2b21b62")
83+
hash2, e := hex.DecodeString("a587acce738d152fbf6aee0422ad9d9b69a0c78a8b362fe618a8a5f6b386ea0e")
84+
require.NoError(e)
8385
actual = cbtsf2.Hash()
8486
require.Equal(hash2, actual[:])
87+
//require.Equal(hash2, actual[:])
8588
t.Logf("actual hash = %x", actual[:])
8689

87-
hash3, _ := hex.DecodeString("d300718263371fb0218a2616f8822866547dade0f0b1dbe3d326950c4488f6de")
90+
hash3, e := hex.DecodeString("95326afce0c2b6e86c8bece513675306cd690c86b35ba7c0d41c1bc3611976d1")
91+
require.NoError(e)
8892
actual = cbtsf3.Hash()
8993
require.Equal(hash3, actual[:])
9094
t.Logf("actual hash = %x", actual[:])
9195

92-
hash4, _ := hex.DecodeString("75b315ef2baaa13af4579876d018db0f512e132d3c4b41b5ebe9d0b75e9cf054")
96+
hash4, e := hex.DecodeString("9e2e1f21422978399a5626ee80f53ec30f1c056a5b924275cb408befe2ac0837")
97+
require.NoError(e)
9398
actual = cbtsf4.Hash()
9499
require.Equal(hash4, actual[:])
95100
t.Logf("actual hash = %x", actual[:])
@@ -243,15 +248,13 @@ func TestWrongNonce(t *testing.T) {
243248
require.Nil(err)
244249
require.Nil(sf.Commit())
245250

246-
chainID := enc.MachineEndian.Uint32(iotxaddress.ChainID)
247-
248251
// correct nonce
249252
coinbaseTsf := action.NewCoinBaseTransfer(big.NewInt(int64(Gen.BlockReward)), ta.Addrinfo["producer"].RawAddress)
250253
tsf1, err := action.NewTransfer(1, big.NewInt(20), ta.Addrinfo["producer"].RawAddress, ta.Addrinfo["alfa"].RawAddress, []byte{}, uint64(100000), big.NewInt(10))
251254
require.NoError(err)
252255
require.NoError(action.Sign(tsf1, ta.Addrinfo["producer"].PrivateKey))
253256
hash := tsf1.Hash()
254-
blk := NewBlock(chainID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf, tsf1}, nil, nil)
257+
blk := NewBlock(cfg.Chain.ID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf, tsf1}, nil, nil)
255258
err = blk.SignBlock(ta.Addrinfo["producer"])
256259
require.NoError(err)
257260
require.Nil(val.Validate(blk, 2, hash, true))
@@ -264,7 +267,7 @@ func TestWrongNonce(t *testing.T) {
264267
require.NoError(err)
265268
require.NoError(action.Sign(tsf2, ta.Addrinfo["producer"].PrivateKey))
266269
hash = tsf1.Hash()
267-
blk = NewBlock(chainID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf, tsf1, tsf2}, nil, nil)
270+
blk = NewBlock(cfg.Chain.ID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf, tsf1, tsf2}, nil, nil)
268271
err = blk.SignBlock(ta.Addrinfo["producer"])
269272
require.NoError(err)
270273
err = val.Validate(blk, 2, hash, true)
@@ -274,7 +277,7 @@ func TestWrongNonce(t *testing.T) {
274277
require.NoError(err)
275278
require.NoError(action.Sign(vote, ta.Addrinfo["producer"].PrivateKey))
276279
hash = tsf1.Hash()
277-
blk = NewBlock(chainID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf}, []*action.Vote{vote}, nil)
280+
blk = NewBlock(cfg.Chain.ID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf}, []*action.Vote{vote}, nil)
278281
err = blk.SignBlock(ta.Addrinfo["producer"])
279282
require.NoError(err)
280283
err = val.Validate(blk, 2, hash, true)
@@ -289,7 +292,7 @@ func TestWrongNonce(t *testing.T) {
289292
require.NoError(err)
290293
require.NoError(action.Sign(tsf4, ta.Addrinfo["producer"].PrivateKey))
291294
hash = tsf1.Hash()
292-
blk = NewBlock(chainID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf, tsf3, tsf4}, nil, nil)
295+
blk = NewBlock(cfg.Chain.ID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf, tsf3, tsf4}, nil, nil)
293296
err = blk.SignBlock(ta.Addrinfo["producer"])
294297
require.NoError(err)
295298
err = val.Validate(blk, 2, hash, true)
@@ -303,7 +306,7 @@ func TestWrongNonce(t *testing.T) {
303306
require.NoError(err)
304307
require.NoError(action.Sign(vote3, ta.Addrinfo["producer"].PrivateKey))
305308
hash = tsf1.Hash()
306-
blk = NewBlock(chainID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf}, []*action.Vote{vote2, vote3}, nil)
309+
blk = NewBlock(cfg.Chain.ID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf}, []*action.Vote{vote2, vote3}, nil)
307310
err = blk.SignBlock(ta.Addrinfo["producer"])
308311
require.NoError(err)
309312
err = val.Validate(blk, 2, hash, true)
@@ -318,7 +321,7 @@ func TestWrongNonce(t *testing.T) {
318321
require.NoError(err)
319322
require.NoError(action.Sign(tsf6, ta.Addrinfo["producer"].PrivateKey))
320323
hash = tsf1.Hash()
321-
blk = NewBlock(chainID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf, tsf5, tsf6}, nil, nil)
324+
blk = NewBlock(cfg.Chain.ID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf, tsf5, tsf6}, nil, nil)
322325
err = blk.SignBlock(ta.Addrinfo["producer"])
323326
require.NoError(err)
324327
err = val.Validate(blk, 2, hash, true)
@@ -332,7 +335,7 @@ func TestWrongNonce(t *testing.T) {
332335
require.NoError(err)
333336
require.NoError(action.Sign(vote5, ta.Addrinfo["producer"].PrivateKey))
334337
hash = tsf1.Hash()
335-
blk = NewBlock(chainID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf}, []*action.Vote{vote4, vote5}, nil)
338+
blk = NewBlock(cfg.Chain.ID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf}, []*action.Vote{vote4, vote5}, nil)
336339
err = blk.SignBlock(ta.Addrinfo["producer"])
337340
require.NoError(err)
338341
err = val.Validate(blk, 2, hash, true)
@@ -467,8 +470,11 @@ func TestValidateSecretBlock(t *testing.T) {
467470
idList := make([][]uint8, 0)
468471
delegates := []string{ta.Addrinfo["producer"].RawAddress}
469472
for i := 0; i < 20; i++ {
470-
addr, _ := iotxaddress.NewAddress(iotxaddress.IsTestnet, iotxaddress.ChainID)
471-
delegates = append(delegates, addr.RawAddress)
473+
pk, _, err := crypto.EC283.NewKeyPair()
474+
require.NoError(err)
475+
pkHash := keypair.HashPubKey(pk)
476+
addr := address.New(cfg.Chain.ID, pkHash[:])
477+
delegates = append(delegates, addr.IotxAddress())
472478
}
473479

474480
for _, delegate := range delegates {

blockchain/blockchain.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/facebookgo/clock"
1616
"github.com/pkg/errors"
1717

18+
"github.com/iotexproject/iotex-core/address"
1819
"github.com/iotexproject/iotex-core/blockchain/action"
1920
"github.com/iotexproject/iotex-core/config"
2021
"github.com/iotexproject/iotex-core/crypto"
@@ -239,12 +240,13 @@ func NewBlockchain(cfg *config.Config, opts ...Option) Blockchain {
239240
logger.Error().Err(err).Msg("Failed to get key pair of producer")
240241
return nil
241242
}
242-
address, err := iotxaddress.GetAddressByPubkey(iotxaddress.IsTestnet, iotxaddress.ChainID, pubKey)
243+
pkHash := keypair.HashPubKey(pubKey)
244+
address := address.New(cfg.Chain.ID, pkHash[:])
243245
if err != nil {
244246
logger.Error().Err(err).Msg("Failed to get producer's address by public key")
245247
return nil
246248
}
247-
chain.validator = &validator{sf: chain.sf, validatorAddr: address.RawAddress}
249+
chain.validator = &validator{sf: chain.sf, validatorAddr: address.IotxAddress()}
248250

249251
if chain.dao != nil {
250252
chain.lifecycle.Add(chain.dao)
@@ -292,7 +294,7 @@ func (bc *blockchain) startEmptyBlockchain() error {
292294
}
293295
// add producer into Trie
294296
if bc.sf != nil {
295-
if _, err := bc.sf.LoadOrCreateState(Gen.CreatorAddr, Gen.TotalSupply); err != nil {
297+
if _, err := bc.sf.LoadOrCreateState(Gen.CreatorAddr(bc.ChainID()), Gen.TotalSupply); err != nil {
296298
return errors.Wrap(err, "failed to add Creator into StateFactory")
297299
}
298300
}
@@ -326,7 +328,7 @@ func (bc *blockchain) startExistingBlockchain(recoveryHeight uint64) error {
326328
}
327329
// If restarting factory from fresh db, first create creator's state
328330
if startHeight == 0 {
329-
if _, err := bc.sf.LoadOrCreateState(Gen.CreatorAddr, Gen.TotalSupply); err != nil {
331+
if _, err := bc.sf.LoadOrCreateState(Gen.CreatorAddr(bc.ChainID()), Gen.TotalSupply); err != nil {
330332
return err
331333
}
332334
}

0 commit comments

Comments
 (0)