Skip to content

Commit

Permalink
test(store): update indexing public key test
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f committed Aug 29, 2024
1 parent 21e091c commit b05c0a8
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 58 deletions.
1 change: 0 additions & 1 deletion crypto/public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ type PublicKey interface {
Decode(io.Reader) error
Verify(msg []byte, sig Signature) error
VerifyAddress(addr Address) error
AccountAddress() Address
EqualsTo(right PublicKey) bool
}
115 changes: 68 additions & 47 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/types/block"
"github.com/pactus-project/pactus/types/tx"
Expand Down Expand Up @@ -136,14 +135,10 @@ func TestIndexingPublicKeys(t *testing.T) {
blk, _ := cBlk.ToBlock()
for _, trx := range blk.Transactions() {
addr := trx.Payload().Signer()
_, err := td.store.PublicKey(addr)
pub, err := td.store.PublicKey(addr)
assert.NoError(t, err)

// if addr.IsAccountAddress() {
// assert.Equal(t, addr, pubKey.AccountAddress())
// } else if addr.IsValidatorAddress() {
// assert.Equal(t, addr, pubKey.ValidatorAddress())
// }
assert.True(t, trx.PublicKey().EqualsTo(pub))
}
})

Expand All @@ -158,71 +153,82 @@ func TestIndexingPublicKeys(t *testing.T) {
func TestStrippedPublicKey(t *testing.T) {
td := setup(t, nil)

// Find a public key that we have already indexed in the database.
cBlkOne, _ := td.store.Block(1)
blkOne, _ := cBlkOne.ToBlock()
trx0PubKey := blkOne.Transactions()[0].PublicKey()
assert.NotNil(t, trx0PubKey)
knownPubKey := trx0PubKey.(*bls.PublicKey)

lastCert := td.store.LastCertificate()
lastHeight := lastCert.Height()
randPubKey, _ := td.RandBLSKeyPair()

trx0 := tx.NewTransferTx(lastHeight, knownPubKey.AccountAddress(), td.RandAccAddress(), 1, 1)
trx1 := tx.NewTransferTx(lastHeight, randPubKey.AccountAddress(), td.RandAccAddress(), 1, 1)
trx2 := tx.NewTransferTx(lastHeight, randPubKey.AccountAddress(), td.RandAccAddress(), 1, 1)
lastHeight := td.store.LastCertificate().Height()
_, blsPrv := td.RandBLSKeyPair()
committedTrx1 := td.GenerateTestTransferTx(
testsuite.TransactionWithBLSSigner(blsPrv),
)
_, ed25519Prv := td.RandEd25519KeyPair()
committedTrx2 := td.GenerateTestTransferTx(
testsuite.TransactionWithEd25519Signer(ed25519Prv),
)
blk0, cert0 := td.GenerateTestBlock(lastHeight+1,
testsuite.BlockWithTransactions([]*tx.Tx{committedTrx1, committedTrx2}))
td.store.SaveBlock(blk0, cert0)
err := td.store.writeBatch()
require.NoError(t, err)

trx0.StripPublicKey()
trx1.SetPublicKey(randPubKey)
trx2.StripPublicKey()
// We have some known and index public key, run tests...
trx1 := td.GenerateTestTransferTx(
testsuite.TransactionWithBLSSigner(blsPrv),
)
trx2 := td.GenerateTestTransferTx(
testsuite.TransactionWithEd25519Signer(ed25519Prv),
)
trx3 := td.GenerateTestTransferTx(
testsuite.TransactionWithBLSSigner(blsPrv),
)
trx4 := td.GenerateTestTransferTx(
testsuite.TransactionWithEd25519Signer(ed25519Prv),
)
trx5 := td.GenerateTestTransferTx()

trx3.StripPublicKey()
trx4.StripPublicKey()
trx5.StripPublicKey()

tests := []struct {
trx *tx.Tx
failed bool
}{
{trx0, false}, // indexed public key and stripped
{trx1, false}, // not stripped
{trx2, true}, // unknown public key and stripped
{trx1, false}, // indexed public key and not stripped
{trx2, false}, // indexed public key and not stripped
{trx3, false}, // indexed public key and stripped
{trx4, false}, // indexed public key and stripped
{trx5, true}, // unknown public key and stripped
}

for _, test := range tests {
for i, test := range tests {
trxs := block.Txs{test.trx}
blk, _ := td.GenerateTestBlock(td.RandHeight(), testsuite.BlockWithTransactions(trxs))
blockHeight := td.store.LastCertificate().Height()
blk, cert := td.GenerateTestBlock(blockHeight+1, testsuite.BlockWithTransactions(trxs))
td.store.SaveBlock(blk, cert)
err := td.store.writeBatch()
require.NoError(t, err)

trxData, _ := test.trx.Bytes()
blkData, _ := blk.Bytes()
cBlk, err := td.store.Block(blockHeight + 1)
require.NoError(t, err)

cTrx := CommittedTx{
store: td.store,
TxID: test.trx.ID(),
Height: lastHeight + 1,
Data: trxData,
}
cBlk := CommittedBlock{
store: td.store,
BlockHash: blk.Hash(),
Height: lastHeight + 1,
Data: blkData,
}
cTrx, err := td.store.Transaction(test.trx.ID())
require.NoError(t, err)

//
if test.failed {
_, err := cBlk.ToBlock()
assert.ErrorIs(t, err, PublicKeyNotFoundError{
Address: test.trx.Payload().Signer(),
})
}, "test %d failed, expected error", i+1)

_, err = cTrx.ToTx()
assert.ErrorIs(t, err, PublicKeyNotFoundError{
Address: test.trx.Payload().Signer(),
})
}, "test %d failed, expected error", i+1)
} else {
_, err := cBlk.ToBlock()
assert.NoError(t, err)
assert.NoError(t, err, "test %d failed, not expected error", i+1)

_, err = cTrx.ToTx()
assert.NoError(t, err)
assert.NoError(t, err, "test %d failed, not expected error", i+1)
}
}
}
Expand Down Expand Up @@ -379,5 +385,20 @@ func TestCancelPrune(t *testing.T) {
func TestRecentTransaction(t *testing.T) {
td := setup(t, nil)

lastHeight := td.store.LastCertificate().Height()
oldTrx := td.GenerateTestTransferTx()
blkOld, certOld := td.GenerateTestBlock(lastHeight+1,
testsuite.BlockWithTransactions([]*tx.Tx{oldTrx}))
td.store.SaveBlock(blkOld, certOld)
err := td.store.writeBatch()
require.NoError(t, err)
assert.True(t, td.store.RecentTransaction(oldTrx.ID()))

blk, cert := td.GenerateTestBlock(lastHeight + td.store.txStore.txCacheWindow + 2)
td.store.SaveBlock(blk, cert)
err = td.store.writeBatch()
require.NoError(t, err)

assert.False(t, td.store.RecentTransaction(oldTrx.ID()))
assert.False(t, td.store.RecentTransaction(td.RandHash()))
}
36 changes: 26 additions & 10 deletions util/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,22 @@ type TransactionMaker struct {
Signer crypto.PrivateKey
}

func (tm *TransactionMaker) SignerAccountAddress() crypto.Address {
blsPub, ok := tm.Signer.PublicKey().(*bls.PublicKey)
if ok {
return blsPub.AccountAddress()
}
ed25519Pub := tm.Signer.PublicKey().(*ed25519.PublicKey)

return ed25519Pub.AccountAddress()
}

func (tm *TransactionMaker) SignerValidatorAddress() crypto.Address {
blsPub := tm.Signer.PublicKey().(*bls.PublicKey)

return blsPub.ValidatorAddress()
}

// NewTransactionMaker creates a new TransactionMaker instance with default values.
func (ts *TestSuite) NewTransactionMaker() *TransactionMaker {
return &TransactionMaker{
Expand Down Expand Up @@ -568,8 +584,8 @@ func (ts *TestSuite) GenerateTestTransferTx(options ...func(tm *TransactionMaker
}
}

pub := tm.Signer.PublicKey()
trx := tx.NewTransferTx(tm.LockTime, pub.AccountAddress(), ts.RandAccAddress(), tm.Amount, tm.Fee)
sender := tm.SignerAccountAddress()
trx := tx.NewTransferTx(tm.LockTime, sender, ts.RandAccAddress(), tm.Amount, tm.Fee)
ts.HelperSignTransaction(tm.Signer, trx)

return trx
Expand All @@ -594,8 +610,8 @@ func (ts *TestSuite) GenerateTestBondTx(options ...func(tm *TransactionMaker)) *
}
}

pub := tm.Signer.PublicKey()
trx := tx.NewBondTx(tm.LockTime, pub.AccountAddress(), ts.RandValAddress(), nil, tm.Amount, tm.Fee)
sender := tm.SignerAccountAddress()
trx := tx.NewBondTx(tm.LockTime, sender, ts.RandValAddress(), nil, tm.Amount, tm.Fee)
ts.HelperSignTransaction(tm.Signer, trx)

return trx
Expand All @@ -615,8 +631,8 @@ func (ts *TestSuite) GenerateTestSortitionTx(options ...func(tm *TransactionMake
}

proof := ts.RandProof()
pub := tm.Signer.PublicKey().(*bls.PublicKey)
trx := tx.NewSortitionTx(tm.LockTime, pub.ValidatorAddress(), proof)
sender := tm.SignerValidatorAddress()
trx := tx.NewSortitionTx(tm.LockTime, sender, proof)
ts.HelperSignTransaction(tm.Signer, trx)

return trx
Expand All @@ -635,8 +651,8 @@ func (ts *TestSuite) GenerateTestUnbondTx(options ...func(tm *TransactionMaker))
tm.Signer = prv
}

pub := tm.Signer.PublicKey().(*bls.PublicKey)
trx := tx.NewUnbondTx(tm.LockTime, pub.ValidatorAddress())
sender := tm.SignerValidatorAddress()
trx := tx.NewUnbondTx(tm.LockTime, sender)
ts.HelperSignTransaction(tm.Signer, trx)

return trx
Expand All @@ -655,8 +671,8 @@ func (ts *TestSuite) GenerateTestWithdrawTx(options ...func(tm *TransactionMaker
tm.Signer = prv
}

pub := tm.Signer.PublicKey().(*bls.PublicKey)
trx := tx.NewWithdrawTx(tm.LockTime, pub.ValidatorAddress(), ts.RandAccAddress(), tm.Amount, tm.Fee)
sender := tm.SignerValidatorAddress()
trx := tx.NewWithdrawTx(tm.LockTime, sender, ts.RandAccAddress(), tm.Amount, tm.Fee)
ts.HelperSignTransaction(tm.Signer, trx)

return trx
Expand Down

0 comments on commit b05c0a8

Please sign in to comment.