Skip to content

Commit

Permalink
Fix commit nil issue (cosmos#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manav-Aggarwal authored Dec 12, 2022
1 parent 653d4bf commit d004a92
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,22 @@ func (m *Manager) getRemainingSleep(start time.Time) time.Duration {
return sleepDuration
}

func (m *Manager) getCommit(header types.Header) (*types.Commit, error) {
headerBytes, err := header.MarshalBinary()
if err != nil {
return nil, err
}
sign, err := m.proposerKey.Sign(headerBytes)
if err != nil {
return nil, err
}
return &types.Commit{
Height: header.Height,
HeaderHash: header.Hash(),
Signatures: []types.Signature{sign},
}, nil
}

func (m *Manager) publishBlock(ctx context.Context) error {
var lastCommit *types.Commit
var lastHeaderHash [32]byte
Expand Down Expand Up @@ -438,19 +454,10 @@ func (m *Manager) publishBlock(ctx context.Context) error {
block = m.executor.CreateBlock(newHeight, lastCommit, lastHeaderHash, m.lastState)
m.logger.Debug("block info", "num_tx", len(block.Data.Txs))

headerBytes, err := block.Header.MarshalBinary()
commit, err := m.getCommit(block.Header)
if err != nil {
return err
}
sign, err := m.proposerKey.Sign(headerBytes)
if err != nil {
return err
}
commit = &types.Commit{
Height: block.Header.Height,
HeaderHash: block.Header.Hash(),
Signatures: []types.Signature{sign},
}

// SaveBlock commits the DB tx
err = m.store.SaveBlock(block, commit)
Expand All @@ -466,6 +473,13 @@ func (m *Manager) publishBlock(ctx context.Context) error {
return err
}

if commit == nil {
commit, err = m.getCommit(block.Header)
if err != nil {
return err
}
}

// SaveBlock commits the DB tx
err = m.store.SaveBlock(block, commit)
if err != nil {
Expand Down

0 comments on commit d004a92

Please sign in to comment.