Skip to content

Commit

Permalink
Refactor code to use block's methods (cosmos#1154)
Browse files Browse the repository at this point in the history
<!--
Please read and fill out this form before submitting your PR.

Please make sure you have reviewed our contributors guide before
submitting your
first PR.
-->

## Overview
Closes: cosmos#1160 

<!-- 
Please provide an explanation of the PR, including the appropriate
context,
background, goal, and rationale. If there is an issue with this
information,
please provide a tl;dr and link the issue. 
-->

## Checklist

<!-- 
Please complete the checklist to ensure that the PR is ready to be
reviewed.

IMPORTANT:
PRs should be left in Draft until the below checklist is completed.
-->

- [ ] New and updated code has appropriate documentation
- [ ] New and updated code has new and/or updated testing
- [x] Required CI checks are passing
- [ ] Visual proof for any user facing features like CLI or
documentation updates
- [x] Linked issues closed with keywords
  • Loading branch information
Manav-Aggarwal authored Aug 31, 2023
1 parent 997572c commit 344d382
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 59 deletions.
2 changes: 1 addition & 1 deletion block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ func (m *Manager) publishBlock(ctx context.Context) error {
if err != nil {
return fmt.Errorf("error while loading last block: %w", err)
}
lastHeaderHash = lastBlock.SignedHeader.Header.Hash()
lastHeaderHash = lastBlock.Hash()
}

var block *types.Block
Expand Down
6 changes: 3 additions & 3 deletions conv/abci/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ func ToABCIBlock(block *types.Block) (*cmtypes.Block, error) {
if err != nil {
return nil, err
}
abciCommit := block.SignedHeader.Commit.ToABCICommit(int64(block.SignedHeader.Header.BaseHeader.Height), block.SignedHeader.Hash())
abciCommit := block.SignedHeader.Commit.ToABCICommit(int64(block.Height()), block.Hash())
// This assumes that we have only one signature
if len(abciCommit.Signatures) == 1 {
abciCommit.Signatures[0].ValidatorAddress = block.SignedHeader.Header.ProposerAddress
abciCommit.Signatures[0].ValidatorAddress = block.SignedHeader.ProposerAddress
}
abciBlock := cmtypes.Block{
Header: abciHeader,
Expand All @@ -92,7 +92,7 @@ func ToABCIBlock(block *types.Block) (*cmtypes.Block, error) {
for i := range block.Data.Txs {
abciBlock.Data.Txs[i] = cmtypes.Tx(block.Data.Txs[i])
}
abciBlock.Header.DataHash = cmbytes.HexBytes(block.SignedHeader.Header.DataHash)
abciBlock.Header.DataHash = cmbytes.HexBytes(block.SignedHeader.DataHash)

return &abciBlock, nil
}
Expand Down
7 changes: 4 additions & 3 deletions da/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,16 @@ func (m *DataAvailabilityLayerClient) SubmitBlocks(ctx context.Context, blocks [
daHeight := atomic.LoadUint64(&m.daHeight)

for _, block := range blocks {
m.logger.Debug("Submitting blocks to DA layer!", "height", block.SignedHeader.Header.Height(), "dataLayerHeight", daHeight)
blockHeight := uint64(block.Height())
m.logger.Debug("Submitting blocks to DA layer!", "height", blockHeight, "dataLayerHeight", daHeight)

hash := block.SignedHeader.Header.Hash()
hash := block.Hash()
blob, err := block.MarshalBinary()
if err != nil {
return da.ResultSubmitBlocks{BaseResult: da.BaseResult{Code: da.StatusError, Message: err.Error()}}
}

err = m.dalcKV.Put(ctx, getKey(daHeight, uint64(block.SignedHeader.Header.Height())), hash[:])
err = m.dalcKV.Put(ctx, getKey(daHeight, blockHeight), hash[:])
if err != nil {
return da.ResultSubmitBlocks{BaseResult: da.BaseResult{Code: da.StatusError, Message: err.Error()}}
}
Expand Down
2 changes: 1 addition & 1 deletion da/test/da_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func getRandomBlock(height uint64, nTxs int) *types.Block {
},
},
}
block.SignedHeader.Header.AppHash = types.GetRandomBytes(32)
block.SignedHeader.AppHash = types.GetRandomBytes(32)

for i := 0; i < nTxs; i++ {
block.Data.Txs[i] = types.GetRandomTx()
Expand Down
22 changes: 11 additions & 11 deletions node/full_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ func (c *FullClient) Commit(ctx context.Context, height *int64) (*ctypes.ResultC
if err != nil {
return nil, err
}
commit := com.ToABCICommit(int64(heightValue), b.SignedHeader.Hash())
commit := com.ToABCICommit(int64(heightValue), b.Hash())
block, err := abciconv.ToABCIBlock(b)
if err != nil {
return nil, err
Expand Down Expand Up @@ -701,11 +701,11 @@ func (c *FullClient) Status(ctx context.Context) (*ctypes.ResultStatus, error) {
return nil, fmt.Errorf("failed to find earliest block: %w", err)
}

validators, err := c.node.Store.LoadValidators(uint64(latest.SignedHeader.Header.Height()))
validators, err := c.node.Store.LoadValidators(uint64(latest.Height()))
if err != nil {
return nil, fmt.Errorf("failed to fetch the validator info at latest block: %w", err)
}
_, validator := validators.GetByAddress(latest.SignedHeader.Header.ProposerAddress)
_, validator := validators.GetByAddress(latest.SignedHeader.ProposerAddress)

state, err := c.node.Store.LoadState()
if err != nil {
Expand Down Expand Up @@ -736,14 +736,14 @@ func (c *FullClient) Status(ctx context.Context) (*ctypes.ResultStatus, error) {
},
},
SyncInfo: ctypes.SyncInfo{
LatestBlockHash: cmbytes.HexBytes(latest.SignedHeader.Header.DataHash),
LatestAppHash: cmbytes.HexBytes(latest.SignedHeader.Header.AppHash),
LatestBlockHeight: latest.SignedHeader.Header.Height(),
LatestBlockTime: latest.SignedHeader.Header.Time(),
EarliestBlockHash: cmbytes.HexBytes(initial.SignedHeader.Header.DataHash),
EarliestAppHash: cmbytes.HexBytes(initial.SignedHeader.Header.AppHash),
EarliestBlockHeight: initial.SignedHeader.Header.Height(),
EarliestBlockTime: initial.SignedHeader.Header.Time(),
LatestBlockHash: cmbytes.HexBytes(latest.SignedHeader.DataHash),
LatestAppHash: cmbytes.HexBytes(latest.SignedHeader.AppHash),
LatestBlockHeight: latest.Height(),
LatestBlockTime: latest.Time(),
EarliestBlockHash: cmbytes.HexBytes(initial.SignedHeader.DataHash),
EarliestAppHash: cmbytes.HexBytes(initial.SignedHeader.AppHash),
EarliestBlockHeight: initial.Height(),
EarliestBlockTime: initial.Time(),
CatchingUp: true, // the client is always syncing in the background to the latest height
},
ValidatorInfo: ctypes.ValidatorInfo{
Expand Down
24 changes: 12 additions & 12 deletions node/full_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func getRandomBlockWithProposer(height uint64, nTxs int, proposerAddr []byte) *t
},
},
}
block.SignedHeader.Header.AppHash = types.GetRandomBytes(32)
block.SignedHeader.AppHash = types.GetRandomBytes(32)

for i := 0; i < nTxs; i++ {
block.Data.Txs[i] = types.GetRandomTx()
Expand All @@ -87,7 +87,7 @@ func getRandomBlockWithProposer(height uint64, nTxs int, proposerAddr []byte) *t
}
lastCommitHash := make(types.Hash, 32)
copy(lastCommitHash, cmprotoLC.Hash().Bytes())
block.SignedHeader.Header.LastCommitHash = lastCommitHash
block.SignedHeader.LastCommitHash = lastCommitHash

block.SignedHeader.Validators = types.GetRandomValidatorSet()

Expand Down Expand Up @@ -371,7 +371,7 @@ func TestGetBlock(t *testing.T) {
}()
block := getRandomBlock(1, 10)
err = rpc.node.Store.SaveBlock(block, &types.Commit{})
rpc.node.Store.SetHeight(uint64(block.SignedHeader.Header.Height()))
rpc.node.Store.SetHeight(uint64(block.Height()))
require.NoError(err)

blockResp, err := rpc.Block(context.Background(), nil)
Expand All @@ -397,24 +397,24 @@ func TestGetCommit(t *testing.T) {
}()
for _, b := range blocks {
err = rpc.node.Store.SaveBlock(b, &types.Commit{})
rpc.node.Store.SetHeight(uint64(b.SignedHeader.Header.Height()))
rpc.node.Store.SetHeight(uint64(b.Height()))
require.NoError(err)
}
t.Run("Fetch all commits", func(t *testing.T) {
for _, b := range blocks {
h := b.SignedHeader.Header.Height()
h := b.Height()
commit, err := rpc.Commit(context.Background(), &h)
require.NoError(err)
require.NotNil(commit)
assert.Equal(b.SignedHeader.Header.Height(), commit.Height)
assert.Equal(h, commit.Height)
}
})

t.Run("Fetch commit for nil height", func(t *testing.T) {
commit, err := rpc.Commit(context.Background(), nil)
require.NoError(err)
require.NotNil(commit)
assert.Equal(blocks[3].SignedHeader.Header.Height(), commit.Height)
assert.Equal(blocks[3].Height(), commit.Height)
})
}

Expand Down Expand Up @@ -496,14 +496,14 @@ func TestGetBlockByHash(t *testing.T) {
abciBlock, err := abciconv.ToABCIBlock(block)
require.NoError(err)

height := block.SignedHeader.Header.Height()
height := block.Height()
retrievedBlock, err := rpc.Block(context.Background(), &height)
require.NoError(err)
require.NotNil(retrievedBlock)
assert.Equal(abciBlock, retrievedBlock.Block)
assert.Equal(abciBlock.Hash(), retrievedBlock.Block.Hash())

blockHash := block.SignedHeader.Header.Hash()
blockHash := block.Hash()
blockResp, err := rpc.BlockByHash(context.Background(), blockHash[:])
require.NoError(err)
require.NotNil(blockResp)
Expand Down Expand Up @@ -685,7 +685,7 @@ func TestBlockchainInfo(t *testing.T) {
for _, h := range heights {
block := getRandomBlock(uint64(h), 5)
err := rpc.node.Store.SaveBlock(block, &types.Commit{})
rpc.node.Store.SetHeight(uint64(block.SignedHeader.Header.Height()))
rpc.node.Store.SetHeight(uint64(block.Height()))
require.NoError(err)
}

Expand Down Expand Up @@ -1065,12 +1065,12 @@ func TestStatus(t *testing.T) {

earliestBlock := getRandomBlockWithProposer(1, 1, validators[0].Address.Bytes())
err = rpc.node.Store.SaveBlock(earliestBlock, &types.Commit{})
rpc.node.Store.SetHeight(uint64(earliestBlock.SignedHeader.Header.Height()))
rpc.node.Store.SetHeight(uint64(earliestBlock.Height()))
require.NoError(err)

latestBlock := getRandomBlockWithProposer(2, 1, validators[1].Address.Bytes())
err = rpc.node.Store.SaveBlock(latestBlock, &types.Commit{})
rpc.node.Store.SetHeight(uint64(latestBlock.SignedHeader.Header.Height()))
rpc.node.Store.SetHeight(uint64(latestBlock.Height()))
require.NoError(err)

err = node.Start()
Expand Down
36 changes: 18 additions & 18 deletions state/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ func (e *BlockExecutor) CreateBlock(height uint64, lastCommit *types.Commit, las
// Evidence: types.EvidenceData{Evidence: nil},
},
}
block.SignedHeader.Header.LastCommitHash = lastCommit.GetCommitHash(&block.SignedHeader.Header, e.proposerAddress)
block.SignedHeader.Header.LastHeaderHash = lastHeaderHash
block.SignedHeader.Header.AggregatorsHash = state.Validators.Hash()
block.SignedHeader.LastCommitHash = lastCommit.GetCommitHash(&block.SignedHeader.Header, e.proposerAddress)
block.SignedHeader.LastHeaderHash = lastHeaderHash
block.SignedHeader.AggregatorsHash = state.Validators.Hash()

return block
}
Expand Down Expand Up @@ -203,7 +203,7 @@ func (e *BlockExecutor) updateState(state types.State, block *types.Block, abciR
}
}
// Change results from this height but only applies to the next next height.
lastHeightValSetChanged = block.SignedHeader.Header.Height() + 1 + 1
lastHeightValSetChanged = block.Height() + 1 + 1
}

if len(nValSet.Validators) > 0 {
Expand All @@ -218,10 +218,10 @@ func (e *BlockExecutor) updateState(state types.State, block *types.Block, abciR
Version: state.Version,
ChainID: state.ChainID,
InitialHeight: state.InitialHeight,
LastBlockHeight: block.SignedHeader.Header.Height(),
LastBlockTime: block.SignedHeader.Header.Time(),
LastBlockHeight: block.Height(),
LastBlockTime: block.Time(),
LastBlockID: cmtypes.BlockID{
Hash: cmbytes.HexBytes(block.SignedHeader.Header.Hash()),
Hash: cmbytes.HexBytes(block.Hash()),
// for now, we don't care about part set headers
},
NextValidators: nValSet,
Expand Down Expand Up @@ -253,7 +253,7 @@ func (e *BlockExecutor) commit(ctx context.Context, state types.State, block *ty

maxBytes := state.ConsensusParams.Block.MaxBytes
maxGas := state.ConsensusParams.Block.MaxGas
err = e.mempool.Update(int64(block.SignedHeader.Header.Height()), fromRollkitTxs(block.Data.Txs), deliverTxs, mempool.PreCheckMaxBytes(maxBytes), mempool.PostCheckMaxGas(maxGas))
err = e.mempool.Update(int64(block.Height()), fromRollkitTxs(block.Data.Txs), deliverTxs, mempool.PreCheckMaxBytes(maxBytes), mempool.PostCheckMaxGas(maxGas))
if err != nil {
return nil, 0, err
}
Expand All @@ -266,25 +266,25 @@ func (e *BlockExecutor) validate(state types.State, block *types.Block) error {
if err != nil {
return err
}
if block.SignedHeader.Header.Version.App != state.Version.Consensus.App ||
block.SignedHeader.Header.Version.Block != state.Version.Consensus.Block {
if block.SignedHeader.Version.App != state.Version.Consensus.App ||
block.SignedHeader.Version.Block != state.Version.Consensus.Block {
return errors.New("block version mismatch")
}
if state.LastBlockHeight <= 0 && block.SignedHeader.Header.Height() != state.InitialHeight {
if state.LastBlockHeight <= 0 && block.Height() != state.InitialHeight {
return errors.New("initial block height mismatch")
}
if state.LastBlockHeight > 0 && block.SignedHeader.Header.Height() != state.LastBlockHeight+1 {
if state.LastBlockHeight > 0 && block.Height() != state.LastBlockHeight+1 {
return errors.New("block height mismatch")
}
if !bytes.Equal(block.SignedHeader.Header.AppHash[:], state.AppHash[:]) {
if !bytes.Equal(block.SignedHeader.AppHash[:], state.AppHash[:]) {
return errors.New("AppHash mismatch")
}

if !bytes.Equal(block.SignedHeader.Header.LastResultsHash[:], state.LastResultsHash[:]) {
if !bytes.Equal(block.SignedHeader.LastResultsHash[:], state.LastResultsHash[:]) {
return errors.New("LastResultsHash mismatch")
}

if !bytes.Equal(block.SignedHeader.Header.AggregatorsHash[:], state.Validators.Hash()) {
if !bytes.Equal(block.SignedHeader.AggregatorsHash[:], state.Validators.Hash()) {
return errors.New("AggregatorsHash mismatch")
}

Expand Down Expand Up @@ -341,7 +341,7 @@ func (e *BlockExecutor) execute(ctx context.Context, state types.State, block *t
}

}
endBlockRequest := abci.RequestEndBlock{Height: block.SignedHeader.Header.Height()}
endBlockRequest := abci.RequestEndBlock{Height: block.Height()}
abciResponses.EndBlock, err = e.proxyApp.EndBlockSync(endBlockRequest)
if err != nil {
return nil, err
Expand Down Expand Up @@ -375,13 +375,13 @@ func (e *BlockExecutor) publishEvents(resp *cmstate.ABCIResponses, block *types.
for _, ev := range abciBlock.Evidence.Evidence {
err = multierr.Append(err, e.eventBus.PublishEventNewEvidence(cmtypes.EventDataNewEvidence{
Evidence: ev,
Height: block.SignedHeader.Header.Height(),
Height: block.Height(),
}))
}
for i, dtx := range resp.DeliverTxs {
err = multierr.Append(err, e.eventBus.PublishEventTx(cmtypes.EventDataTx{
TxResult: abci.TxResult{
Height: block.SignedHeader.Header.Height(),
Height: block.Height(),
Index: uint32(i),
Tx: abciBlock.Data.Txs[i],
Result: *dtx,
Expand Down
8 changes: 4 additions & 4 deletions state/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ func doTestCreateBlock(t *testing.T) {
block := executor.CreateBlock(1, &types.Commit{}, []byte{}, state)
require.NotNil(block)
assert.Empty(block.Data.Txs)
assert.Equal(int64(1), block.SignedHeader.Header.Height())
assert.Equal(int64(1), block.Height())

// one small Tx
err = mpool.CheckTx([]byte{1, 2, 3, 4}, func(r *abci.Response) {}, mempool.TxInfo{})
require.NoError(err)
block = executor.CreateBlock(2, &types.Commit{}, []byte{}, state)
require.NotNil(block)
assert.Equal(int64(2), block.SignedHeader.Header.Height())
assert.Equal(int64(2), block.Height())
assert.Len(block.Data.Txs, 1)

// now there are 3 Txs, and only two can fit into single block
Expand Down Expand Up @@ -158,7 +158,7 @@ func doTestApplyBlock(t *testing.T) {
require.NoError(err)
block := executor.CreateBlock(1, &types.Commit{Signatures: []types.Signature{types.Signature([]byte{1, 1, 1})}}, []byte{}, state)
require.NotNil(block)
assert.Equal(int64(1), block.SignedHeader.Header.Height())
assert.Equal(int64(1), block.Height())
assert.Len(block.Data.Txs, 1)

// Update the signature on the block to current from last
Expand All @@ -184,7 +184,7 @@ func doTestApplyBlock(t *testing.T) {
require.NoError(mpool.CheckTx(make([]byte, 90), func(r *abci.Response) {}, mempool.TxInfo{}))
block = executor.CreateBlock(2, &types.Commit{Signatures: []types.Signature{types.Signature([]byte{1, 1, 1})}}, []byte{}, newState)
require.NotNil(block)
assert.Equal(int64(2), block.SignedHeader.Header.Height())
assert.Equal(int64(2), block.Height())
assert.Len(block.Data.Txs, 3)

headerBytes, _ = block.SignedHeader.Header.MarshalBinary()
Expand Down
4 changes: 2 additions & 2 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *DefaultStore) Height() uint64 {
// SaveBlock adds block to the store along with corresponding commit.
// Stored height is updated if block height is greater than stored value.
func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error {
hash := block.SignedHeader.Header.Hash()
hash := block.Hash()
blockBlob, err := block.MarshalBinary()
if err != nil {
return fmt.Errorf("failed to marshal Block to binary: %w", err)
Expand All @@ -80,7 +80,7 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error

err = multierr.Append(err, bb.Put(s.ctx, ds.NewKey(getBlockKey(hash)), blockBlob))
err = multierr.Append(err, bb.Put(s.ctx, ds.NewKey(getCommitKey(hash)), commitBlob))
err = multierr.Append(err, bb.Put(s.ctx, ds.NewKey(getIndexKey(uint64(block.SignedHeader.Header.Height()))), hash[:]))
err = multierr.Append(err, bb.Put(s.ctx, ds.NewKey(getIndexKey(uint64(block.Height()))), hash[:]))

if err != nil {
bb.Discard(s.ctx)
Expand Down
6 changes: 3 additions & 3 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestStoreHeight(t *testing.T) {

for _, block := range c.blocks {
err := bstore.SaveBlock(block, &types.Commit{})
bstore.SetHeight(uint64(block.SignedHeader.Header.Height()))
bstore.SetHeight(uint64(block.Height()))
assert.NoError(err)
}

Expand Down Expand Up @@ -112,12 +112,12 @@ func TestStoreLoad(t *testing.T) {
}

for _, expected := range c.blocks {
block, err := bstore.LoadBlock(uint64(expected.SignedHeader.Header.Height()))
block, err := bstore.LoadBlock(uint64(expected.Height()))
assert.NoError(err)
assert.NotNil(block)
assert.Equal(expected, block)

commit, err := bstore.LoadCommit(uint64(expected.SignedHeader.Header.Height()))
commit, err := bstore.LoadCommit(uint64(expected.Height()))
assert.NoError(err)
assert.NotNil(commit)
}
Expand Down
2 changes: 1 addition & 1 deletion types/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func GetNextRandomHeader(signedHeader *SignedHeader, privKey ed25519.PrivKey) (*
},
Validators: valSet,
}
newSignedHeader.Header.LastCommitHash = signedHeader.Commit.GetCommitHash(
newSignedHeader.LastCommitHash = signedHeader.Commit.GetCommitHash(
&newSignedHeader.Header, signedHeader.ProposerAddress,
)
commit, err := getCommit(newSignedHeader.Header, privKey)
Expand Down

0 comments on commit 344d382

Please sign in to comment.