Skip to content

Commit

Permalink
Simplify state transactions function in FSM (#1312)
Browse files Browse the repository at this point in the history
* Simplify state transactions, as it creates only bridge commitment transaction

* Address comment

* Address comment

* Removed check
  • Loading branch information
Stefan-Ethernal committed Mar 22, 2023
1 parent 05d6186 commit 308fe5b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 48 deletions.
35 changes: 19 additions & 16 deletions consensus/polybft/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,8 @@ func (f *fsm) BuildProposal(currentRound uint64) ([]byte, error) {
}

if f.config.IsBridgeEnabled() {
for _, tx := range f.stateTransactions() {
if err := f.blockBuilder.WriteTx(tx); err != nil {
return nil, fmt.Errorf("failed to apply state transaction. Error: %w", err)
}
if err := f.applyBridgeCommitmentTx(); err != nil {
return nil, err
}
}

Expand Down Expand Up @@ -192,25 +190,30 @@ func (f *fsm) BuildProposal(currentRound uint64) ([]byte, error) {
return stateBlock.Block.MarshalRLP(), nil
}

func (f *fsm) stateTransactions() []*types.Transaction {
var txns []*types.Transaction

// applyBridgeCommitmentTx builds state transaction which contains data for bridge commitment registration
func (f *fsm) applyBridgeCommitmentTx() error {
if f.proposerCommitmentToRegister != nil {
// add register commitment transaction
inputData, err := f.proposerCommitmentToRegister.EncodeAbi()
bridgeCommitmentTx, err := f.createBridgeCommitmentTx()
if err != nil {
f.logger.Error("StateTransactions failed to encode input data for state sync commitment registration", "Error", err)

return nil
return fmt.Errorf("creation of bridge commitment transaction failed: %w", err)
}

txns = append(txns,
createStateTransactionWithData(f.config.StateReceiverAddr, inputData))
if err := f.blockBuilder.WriteTx(bridgeCommitmentTx); err != nil {
return fmt.Errorf("failed to apply bridge commitment state transaction. Error: %w", err)
}
}

f.logger.Debug("Apply state transaction", "num", len(txns))
return nil
}

// createBridgeCommitmentTx builds bridge commitment registration transaction
func (f *fsm) createBridgeCommitmentTx() (*types.Transaction, error) {
inputData, err := f.proposerCommitmentToRegister.EncodeAbi()
if err != nil {
return nil, fmt.Errorf("failed to encode input data for bridge commitment registration: %w", err)
}

return txns
return createStateTransactionWithData(f.config.StateReceiverAddr, inputData), nil
}

// getValidatorsTransition applies delta to the current validators,
Expand Down
61 changes: 29 additions & 32 deletions consensus/polybft/fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,16 +521,13 @@ func TestFSM_VerifyStateTransactions_StateTransactionPass(t *testing.T) {
commitEpochInput: createTestCommitEpochInput(t, 0, nil, 10),
logger: hclog.NewNullLogger(),
}
txs := fsm.stateTransactions()

// add commit epoch tx to the end of transactions list
tx, err := fsm.createCommitEpochTx()
assert.NoError(t, err)

txs = append([]*types.Transaction{tx}, txs...)
// add commit epoch commitEpochTx to the end of transactions list
commitEpochTx, err := fsm.createCommitEpochTx()
require.NoError(t, err)

err = fsm.VerifyStateTransactions(txs)
assert.NoError(t, err)
err = fsm.VerifyStateTransactions([]*types.Transaction{commitEpochTx})
require.NoError(t, err)
}

func TestFSM_VerifyStateTransactions_StateTransactionQuorumNotReached(t *testing.T) {
Expand All @@ -555,16 +552,17 @@ func TestFSM_VerifyStateTransactions_StateTransactionQuorumNotReached(t *testing
logger: hclog.NewNullLogger(),
}

txs := fsm.stateTransactions()
bridgeCommitmentTx, err := fsm.createBridgeCommitmentTx()
require.NoError(t, err)

// add commit epoch tx to the end of transactions list
tx, err := fsm.createCommitEpochTx()
assert.NoError(t, err)
// add commit epoch commitEpochTx to the end of transactions list
commitEpochTx, err := fsm.createCommitEpochTx()
require.NoError(t, err)

txs = append([]*types.Transaction{tx}, txs...)
stateTxs := []*types.Transaction{commitEpochTx, bridgeCommitmentTx}

err = fsm.VerifyStateTransactions(txs)
assert.ErrorContains(t, err, "quorum size not reached")
err = fsm.VerifyStateTransactions(stateTxs)
require.ErrorContains(t, err, "quorum size not reached")
}

func TestFSM_VerifyStateTransactions_StateTransactionInvalidSignature(t *testing.T) {
Expand Down Expand Up @@ -596,16 +594,15 @@ func TestFSM_VerifyStateTransactions_StateTransactionInvalidSignature(t *testing
logger: hclog.NewNullLogger(),
}

txs := fsm.stateTransactions()

// add commit epoch tx to the end of transactions list
tx, err := fsm.createCommitEpochTx()
assert.NoError(t, err)
bridgeCommitmentTx, err := fsm.createBridgeCommitmentTx()
require.NoError(t, err)

txs = append([]*types.Transaction{tx}, txs...)
// add commit epoch commitEpochTx to the end of transactions list
commitEpochTx, err := fsm.createCommitEpochTx()
require.NoError(t, err)

err = fsm.VerifyStateTransactions(txs)
assert.ErrorContains(t, err, "invalid signature")
err = fsm.VerifyStateTransactions([]*types.Transaction{commitEpochTx, bridgeCommitmentTx})
require.ErrorContains(t, err, "invalid signature")
}

func TestFSM_ValidateCommit_WrongValidator(t *testing.T) {
Expand Down Expand Up @@ -657,7 +654,7 @@ func TestFSM_ValidateCommit_InvalidHash(t *testing.T) {
validators: validators.toValidatorSet(), exitEventRootHash: types.ZeroHash, logger: hclog.NewNullLogger()}

_, err := fsm.BuildProposal(0)
assert.NoError(t, err)
require.NoError(t, err)

nonValidatorAcc := newTestValidator(t, "non_validator", 1)
wrongSignature, err := nonValidatorAcc.mustSign([]byte("Foo")).Marshal()
Expand Down Expand Up @@ -1018,15 +1015,15 @@ func TestFSM_DecodeCommitmentStateTxs(t *testing.T) {
logger: hclog.NewNullLogger(),
}

for i, tx := range f.stateTransactions() {
decodedData, err := decodeStateTransaction(tx.Input)
require.NoError(t, err)
bridgeCommitmentTx, err := f.createBridgeCommitmentTx()
require.NoError(t, err)

decodedCommitmentMsg, ok := decodedData.(*CommitmentMessageSigned)
require.True(t, ok)
require.Equal(t, 0, i, "failed for tx number %d", i)
require.Equal(t, signedCommitment, decodedCommitmentMsg, "failed for tx number %d", i)
}
decodedData, err := decodeStateTransaction(bridgeCommitmentTx.Input)
require.NoError(t, err)

decodedCommitmentMsg, ok := decodedData.(*CommitmentMessageSigned)
require.True(t, ok)
require.Equal(t, signedCommitment, decodedCommitmentMsg)
}

func TestFSM_DecodeCommitEpochStateTx(t *testing.T) {
Expand Down

0 comments on commit 308fe5b

Please sign in to comment.