Skip to content

Commit

Permalink
add checks for nil/empty requests on cometbft & tx unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
jordipainan committed Nov 23, 2023
1 parent 95e4df0 commit cc54c3b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
15 changes: 15 additions & 0 deletions vochain/cometbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ func (app *BaseApplication) InitChain(_ context.Context,
// CheckTx unmarshals req.Tx and checks its validity
func (app *BaseApplication) CheckTx(_ context.Context,
req *abcitypes.RequestCheckTx) (*abcitypes.ResponseCheckTx, error) {
if req == nil || req.Tx == nil {
return &abcitypes.ResponseCheckTx{
Code: 1,
Data: []byte("nil request or tx"),
}, fmt.Errorf("nil request or tx")
}
txReference := vochaintx.TxKey(req.Tx)
ref, ok := app.txReferences.Load(txReference)
if !ok {
Expand Down Expand Up @@ -219,6 +225,9 @@ func (app *BaseApplication) FinalizeBlock(_ context.Context,
req *abcitypes.RequestFinalizeBlock) (*abcitypes.ResponseFinalizeBlock, error) {
app.prepareProposalLock.Lock()
defer app.prepareProposalLock.Unlock()
if req == nil {
return nil, fmt.Errorf("nil request")
}
start := time.Now()
height := uint32(req.GetHeight())

Expand Down Expand Up @@ -327,6 +336,9 @@ func (app *BaseApplication) PrepareProposal(ctx context.Context,
req *abcitypes.RequestPrepareProposal) (*abcitypes.ResponsePrepareProposal, error) {
app.prepareProposalLock.Lock()
defer app.prepareProposalLock.Unlock()
if req == nil {
return nil, fmt.Errorf("nil request")
}
startTime := time.Now()

type txInfo struct {
Expand Down Expand Up @@ -431,6 +443,9 @@ func (app *BaseApplication) ProcessProposal(_ context.Context,
req *abcitypes.RequestProcessProposal) (*abcitypes.ResponseProcessProposal, error) {
app.prepareProposalLock.Lock()
defer app.prepareProposalLock.Unlock()
if req == nil {
return nil, fmt.Errorf("nil request")
}
// Check if the node is a validator, if not, just accept the proposal and return (nothing to say)
validator, err := app.State.Validator(app.NodeAddress, true)
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion vochain/transaction/vochaintx/vochaintx.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ func (tx *Tx) Unmarshal(content []byte, chainID string) error {
if err != nil {
return fmt.Errorf("failed to unmarshal transaction: %w", err)
}
tx.TxModelType = string(tx.Tx.ProtoReflect().WhichOneof(tx.Tx.ProtoReflect().Descriptor().Oneofs().Get(0)).Name())
txReflectDescriptor := tx.Tx.ProtoReflect().Descriptor().Oneofs().Get(0)
if txReflectDescriptor == nil {
return fmt.Errorf("failed to determine transaction type")
}
whichOneTxModelType := tx.Tx.ProtoReflect().WhichOneof(txReflectDescriptor)
if whichOneTxModelType == nil {
return fmt.Errorf("failed to determine transaction type")
}
tx.TxModelType = string(whichOneTxModelType.Name())
tx.Signature = stx.GetSignature()
tx.TxID = TxKey(content)
return nil
Expand Down

0 comments on commit cc54c3b

Please sign in to comment.