Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implementing pip-0004 indexing public key #671

Merged
merged 10 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions consensus/propose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestSetProposalInvalidProposer(t *testing.T) {
assert.Nil(t, td.consY.RoundProposal(0))

addr := td.signers[tIndexB].Address()
b := td.GenerateTestBlock(&addr, nil)
b := td.GenerateTestBlock(&addr)
p := proposal.NewProposal(1, 0, b)

td.consY.SetProposal(p)
Expand All @@ -36,8 +36,8 @@ func TestSetProposalInvalidProposer(t *testing.T) {
func TestSetProposalInvalidBlock(t *testing.T) {
td := setup(t)

a := td.signers[tIndexB].Address()
invBlock := td.GenerateTestBlock(&a, nil)
addr := td.signers[tIndexB].Address()
invBlock := td.GenerateTestBlock(&addr)
p := proposal.NewProposal(1, 2, invBlock)
td.signers[tIndexB].SignMsg(p)

Expand All @@ -52,8 +52,8 @@ func TestSetProposalInvalidBlock(t *testing.T) {
func TestSetProposalInvalidHeight(t *testing.T) {
td := setup(t)

a := td.signers[tIndexB].Address()
invBlock := td.GenerateTestBlock(&a, nil)
addr := td.signers[tIndexB].Address()
invBlock := td.GenerateTestBlock(&addr)
p := proposal.NewProposal(2, 0, invBlock)
td.signers[tIndexB].SignMsg(p)

Expand Down
157 changes: 78 additions & 79 deletions execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/sandbox"
"github.com/pactus-project/pactus/types/tx"
"github.com/pactus-project/pactus/types/tx/payload"
"github.com/pactus-project/pactus/util/errors"
"github.com/pactus-project/pactus/util/testsuite"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -143,84 +142,84 @@ func TestChecker(t *testing.T) {
})
}

func TestLockTime(t *testing.T) {
ts := testsuite.NewTestSuite(t)

executor := NewExecutor()
checker := NewChecker()
sb := sandbox.MockingSandbox(ts)

curHeight := 2 * sb.TestParams.TransactionToLiveInterval
sb.TestStore.AddTestBlock(curHeight)

t.Run("Should reject sortition transactions with lock time", func(t *testing.T) {
pub, prv := ts.RandBLSKeyPair()
signer := crypto.NewSigner(prv)
val := sb.MakeNewValidator(pub)
sb.UpdateValidator(val)

sb.TestAcceptSortition = true
pld := &payload.SortitionPayload{
Address: pub.Address(),
Proof: ts.RandProof(),
}
trx := tx.NewLockTimeTx(curHeight+10, 1, pld, 0, "")
signer.SignMsg(trx)
err := executor.Execute(trx, sb)
assert.Error(t, err)
})

t.Run("Should reject subsidy transactions with lock time", func(t *testing.T) {
pld := &payload.TransferPayload{
Sender: crypto.TreasuryAddress,
Receiver: ts.RandAddress(),
Amount: 1234,
}
trx := tx.NewLockTimeTx(curHeight+10, 1, pld, 0, "")
err := executor.Execute(trx, sb)
assert.Error(t, err)
})

t.Run("Should reject expired transactions", func(t *testing.T) {
signer := ts.RandSigner()
acc := sb.MakeNewAccount(signer.Address())
acc.AddToBalance(10000)
sb.UpdateAccount(signer.Address(), acc)
pld := &payload.TransferPayload{
Sender: signer.Address(),
Receiver: ts.RandAddress(),
Amount: 1234,
}

trx := tx.NewLockTimeTx(curHeight-sb.TestParams.TransactionToLiveInterval, 1,
pld, sb.TestParams.MinimumFee, "")
signer.SignMsg(trx)
err := executor.Execute(trx, sb)
assert.Error(t, err)
})

t.Run("Not finalized transaction", func(t *testing.T) {
signer := ts.RandSigner()
acc := sb.MakeNewAccount(signer.Address())
acc.AddToBalance(10000)
sb.UpdateAccount(signer.Address(), acc)
pld := &payload.TransferPayload{
Sender: signer.Address(),
Receiver: ts.RandAddress(),
Amount: 1234,
}

trx1 := tx.NewLockTimeTx(curHeight+sb.TestParams.TransactionToLiveInterval, 1,
pld, sb.TestParams.MinimumFee, "")
signer.SignMsg(trx1)
err := executor.Execute(trx1, sb)
assert.Error(t, err)

// In non-strict mode this transaction remains in pool
err = checker.Execute(trx1, sb)
assert.NoError(t, err)
})
}
// func TestLockTime(t *testing.T) {
// ts := testsuite.NewTestSuite(t)

// executor := NewExecutor()
// checker := NewChecker()
// sb := sandbox.MockingSandbox(ts)

// curHeight := 2 * sb.TestParams.TransactionToLiveInterval
// sb.TestStore.AddTestBlock(curHeight)

// t.Run("Should reject sortition transactions with lock time", func(t *testing.T) {
// pub, prv := ts.RandomBLSKeyPair()
// signer := crypto.NewSigner(prv)
// val := sb.MakeNewValidator(pub)
// sb.UpdateValidator(val)

// sb.TestAcceptSortition = true
// pld := &payload.SortitionPayload{
// Address: pub.Address(),
// Proof: ts.RandomProof(),
// }
// trx := tx.NewLockTimeTx(curHeight+10, 1, pld, 0, "")
// signer.SignMsg(trx)
// err := executor.Execute(trx, sb)
// assert.Error(t, err)
// })

// t.Run("Should reject subsidy transactions with lock time", func(t *testing.T) {
// pld := &payload.TransferPayload{
// Sender: crypto.TreasuryAddress,
// Receiver: ts.RandomAddress(),
// Amount: 1234,
// }
// trx := tx.NewLockTimeTx(curHeight+10, 1, pld, 0, "")
// err := executor.Execute(trx, sb)
// assert.Error(t, err)
// })

// t.Run("Should reject expired transactions", func(t *testing.T) {
// signer := ts.RandSigner()
// acc := sb.MakeNewAccount(signer.Address())
// acc.AddToBalance(10000)
// sb.UpdateAccount(signer.Address(), acc)
// pld := &payload.TransferPayload{
// Sender: signer.Address(),
// Receiver: ts.RandAddress(),
// Amount: 1234,
// }

// trx := tx.NewLockTimeTx(curHeight-sb.TestParams.TransactionToLiveInterval, 1,
// pld, sb.TestParams.MinimumFee, "")
// signer.SignMsg(trx)
// err := executor.Execute(trx, sb)
// assert.Error(t, err)
// })

// t.Run("Not finalized transaction", func(t *testing.T) {
// signer := ts.RandomSigner()
// acc := sb.MakeNewAccount(signer.Address())
// acc.AddToBalance(10000)
// sb.UpdateAccount(signer.Address(), acc)
// pld := &payload.TransferPayload{
// Sender: signer.Address(),
// Receiver: ts.RandomAddress(),
// Amount: 1234,
// }

// trx1 := tx.NewLockTimeTx(curHeight+sb.TestParams.TransactionToLiveInterval, 1,
// pld, sb.TestParams.MinimumFee, "")
// signer.SignMsg(trx1)
// err := executor.Execute(trx1, sb)
// assert.Error(t, err)

// // In non-strict mode this transaction remains in pool
// err = checker.Execute(trx1, sb)
// assert.NoError(t, err)
// })
// }

func TestFee(t *testing.T) {
ts := testsuite.NewTestSuite(t)
Expand Down
2 changes: 1 addition & 1 deletion sandbox/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func setup(t *testing.T) *testData {
assert.Equal(t, sandbox.CurrentHeight(), uint32(1))
lastHeight := uint32(21)
for i := uint32(1); i < lastHeight; i++ {
b := ts.GenerateTestBlock(nil, nil)
b := ts.GenerateTestBlock(nil)
c := ts.GenerateTestCertificate()
store.SaveBlock(i, b, c)
}
Expand Down
5 changes: 3 additions & 2 deletions state/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ type Facade interface {
PendingTx(id tx.ID) *tx.Tx
AddPendingTx(trx *tx.Tx) error
AddPendingTxAndBroadcast(trx *tx.Tx) error
StoredBlock(height uint32) *store.StoredBlock
StoredTx(id tx.ID) *store.StoredTx
MakeCommittedBlock(data []byte, height uint32, blockHash hash.Hash) *store.CommittedBlock
CommittedBlock(height uint32) *store.CommittedBlock
CommittedTx(id tx.ID) *store.CommittedTx
BlockHash(height uint32) hash.Hash
BlockHeight(hash hash.Hash) uint32
AccountByAddress(addr crypto.Address) *account.Account
Expand Down
5 changes: 4 additions & 1 deletion state/lastinfo/last_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@
return nil, fmt.Errorf("unable to retrieve block %v: %w", height, err)
}

lastBlock := sb.ToBlock()
lastBlock, err := sb.ToBlock()
if err != nil {
return nil, err

Check warning on line 128 in state/lastinfo/last_info.go

View check run for this annotation

Codecov / codecov/patch

state/lastinfo/last_info.go#L128

Added line #L128 was not covered by tests
}

li.lastBlockHeight = height
li.lastCert = cert
Expand Down
4 changes: 2 additions & 2 deletions state/lastinfo/last_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestRestoreFailed(t *testing.T) {

li := NewLastInfo()

td.store.Validators = make(map[crypto.Address]validator.Validator) // Reset Validators
td.store.Validators = make(map[crypto.Address]*validator.Validator) // Reset Validators
_, err := li.RestoreLastInfo(td.store, 4)
assert.Error(t, err)
})
Expand All @@ -134,7 +134,7 @@ func TestRestoreFailed(t *testing.T) {

li := NewLastInfo()

td.store.Blocks = make(map[uint32]block.Block) // Reset Blocks
td.store.Blocks = make(map[uint32]*block.Block) // Reset Blocks
_, err := li.RestoreLastInfo(td.store, 4)
assert.Error(t, err)
})
Expand Down
18 changes: 13 additions & 5 deletions state/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ func MockingState(ts *testsuite.TestSuite) *MockState {

func (m *MockState) CommitTestBlocks(num int) {
for i := 0; i < num; i++ {
lastHash := m.LastBlockHash()
blk := m.ts.GenerateTestBlock(nil, &lastHash)
blk := m.ts.GenerateTestBlock(nil)
cert := m.ts.GenerateTestCertificate()

m.TestStore.SaveBlock(m.LastBlockHeight()+1, blk, cert)
Expand Down Expand Up @@ -113,7 +112,7 @@ func (m *MockState) Close() error {
}

func (m *MockState) ProposeBlock(_ crypto.Signer, _ crypto.Address, _ int16) (*block.Block, error) {
b := m.ts.GenerateTestBlock(nil, nil)
b := m.ts.GenerateTestBlock(nil)
return b, nil
}

Expand Down Expand Up @@ -162,15 +161,24 @@ func (m *MockState) CommitteePower() int64 {
return m.TestCommittee.TotalPower()
}

func (m *MockState) StoredBlock(height uint32) *store.StoredBlock {
func (m *MockState) MakeCommittedBlock(data []byte, height uint32, blockHash hash.Hash) *store.CommittedBlock {
return &store.CommittedBlock{
Store: m.TestStore,
Data: data,
BlockHash: blockHash,
Height: height,
}
}

func (m *MockState) CommittedBlock(height uint32) *store.CommittedBlock {
m.lk.RLock()
defer m.lk.RUnlock()

b, _ := m.TestStore.Block(height)
return b
}

func (m *MockState) StoredTx(id tx.ID) *store.StoredTx {
func (m *MockState) CommittedTx(id tx.ID) *store.CommittedTx {
m.lk.RLock()
defer m.lk.RUnlock()

Expand Down
20 changes: 17 additions & 3 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@
if err != nil {
return err
}
blockOne := blockOneInfo.ToBlock()

blockOne, err := blockOneInfo.ToBlock()
if err != nil {
return err

Check warning on line 113 in state/state.go

View check run for this annotation

Codecov / codecov/patch

state/state.go#L113

Added line #L113 was not covered by tests
}

if !genStateRoot.EqualsTo(blockOne.Header().StateRoot()) {
return fmt.Errorf("invalid genesis doc")
}
Expand Down Expand Up @@ -620,7 +625,16 @@
return st.store.HasValidator(addr)
}

func (st *state) StoredBlock(height uint32) *store.StoredBlock {
func (st *state) MakeCommittedBlock(data []byte, height uint32, blockHash hash.Hash) *store.CommittedBlock {
return &store.CommittedBlock{
Store: st.store,
Data: data,
BlockHash: blockHash,
Height: height,
}
}

func (st *state) CommittedBlock(height uint32) *store.CommittedBlock {
b, err := st.store.Block(height)
if err != nil {
st.logger.Trace("error on retrieving block", "err", err)
Expand All @@ -629,7 +643,7 @@
return b
}

func (st *state) StoredTx(id tx.ID) *store.StoredTx {
func (st *state) CommittedTx(id tx.ID) *store.CommittedTx {
tx, err := st.store.Transaction(id)
if err != nil {
st.logger.Trace("searching transaction in local store failed", "id", id, "err", err)
Expand Down
17 changes: 15 additions & 2 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func TestCommitBlocks(t *testing.T) {
td := setup(t)

b1, c1 := td.makeBlockAndCertificate(t, 1, td.valSigner1, td.valSigner2, td.valSigner3)
invBlock := td.GenerateTestBlock(nil, nil)
invBlock := td.GenerateTestBlock(nil)
assert.Error(t, td.state1.CommitBlock(1, invBlock, c1))
// No error here but block is ignored, because the height is invalid
assert.NoError(t, td.state1.CommitBlock(2, b1, c1))
Expand Down Expand Up @@ -366,7 +366,7 @@ func TestBlockProposal(t *testing.T) {
func TestInvalidBlock(t *testing.T) {
td := setup(t)

b := td.GenerateTestBlock(nil, nil)
b := td.GenerateTestBlock(nil)
assert.Error(t, td.state1.ValidateBlock(b))
}

Expand Down Expand Up @@ -779,3 +779,16 @@ func TestCalcFee(t *testing.T) {
assert.Error(t, err)
}
}

func TestMakeCommittedBlock(t *testing.T) {
td := setup(t)

data := td.RandBytes(128)
height := td.RandHeight()
hash := td.RandHash()
cb := td.state1.MakeCommittedBlock(data, height, hash)
assert.Equal(t, data, cb.Data)
assert.Equal(t, hash, cb.BlockHash)
assert.Equal(t, height, cb.Height)
assert.NotNil(t, cb.Store)
}
Loading
Loading