From 4039cbe60f63ae3b4623f6edb518e919a3dd6f1d Mon Sep 17 00:00:00 2001 From: nashqueue <99758629+nashqueue@users.noreply.github.com> Date: Wed, 15 Mar 2023 18:33:09 +0100 Subject: [PATCH] Refactor: Header -> Signed Header (#771) Fixes: #773 --------- Co-authored-by: Ganesha Upadhyaya --- block/manager.go | 33 ++-- conv/abci/block.go | 8 +- da/mock/mock.go | 6 +- da/test/da_test.go | 15 +- node/full_client.go | 20 +-- node/full_client_test.go | 51 +++--- proto/rollkit/rollkit.proto | 15 +- state/executor.go | 72 ++++---- state/executor_test.go | 8 +- store/store.go | 4 +- store/store_test.go | 29 ++- types/block.go | 5 +- types/hashing.go | 2 +- types/pb/rollkit/rollkit.pb.go | 316 ++++++++++++--------------------- types/serialization.go | 23 +-- types/serialization_test.go | 49 ++--- types/validation.go | 4 +- 17 files changed, 282 insertions(+), 378 deletions(-) diff --git a/block/manager.go b/block/manager.go index 990b8634b3d..65cfe984446 100644 --- a/block/manager.go +++ b/block/manager.go @@ -287,11 +287,11 @@ func (m *Manager) SyncLoop(ctx context.Context, cancel context.CancelFunc) { block := blockEvent.block daHeight := blockEvent.daHeight m.logger.Debug("block body retrieved from DALC", - "height", block.Header.Height(), + "height", block.SignedHeader.Header.Height(), "daHeight", daHeight, "hash", block.Hash(), ) - m.syncCache[block.Header.BaseHeader.Height] = block + m.syncCache[block.SignedHeader.Header.BaseHeader.Height] = block m.retrieveCond.Signal() err := m.trySyncNextBlock(ctx, daHeight) @@ -346,7 +346,7 @@ func (m *Manager) trySyncNextBlock(ctx context.Context, daHeight uint64) error { b2, ok2 := m.syncCache[currentHeight+2] if ok2 { m.logger.Debug("using last commit from next block") - commit = &b2.LastCommit + commit = &b2.SignedHeader.Commit } else { lastCommit := m.getLastCommit() if lastCommit != nil && lastCommit.Height == currentHeight+1 { @@ -356,7 +356,7 @@ func (m *Manager) trySyncNextBlock(ctx context.Context, daHeight uint64) error { } if b1 != nil && commit != nil { - m.logger.Info("Syncing block", "height", b1.Header.Height()) + m.logger.Info("Syncing block", "height", b1.SignedHeader.Header.Height()) newState, responses, err := m.executor.ApplyBlock(ctx, m.lastState, b1) if err != nil { return fmt.Errorf("failed to ApplyBlock: %w", err) @@ -369,9 +369,9 @@ func (m *Manager) trySyncNextBlock(ctx context.Context, daHeight uint64) error { if err != nil { return fmt.Errorf("failed to Commit: %w", err) } - m.store.SetHeight(uint64(b1.Header.Height())) + m.store.SetHeight(uint64(b1.SignedHeader.Header.Height())) - err = m.store.SaveBlockResponses(uint64(b1.Header.Height()), responses) + err = m.store.SaveBlockResponses(uint64(b1.SignedHeader.Header.Height()), responses) if err != nil { return fmt.Errorf("failed to save block responses: %w", err) } @@ -514,7 +514,7 @@ func (m *Manager) publishBlock(ctx context.Context) error { if err != nil { return fmt.Errorf("error while loading last block: %w", err) } - lastHeaderHash = lastBlock.Header.Hash() + lastHeaderHash = lastBlock.SignedHeader.Header.Hash() } var block *types.Block @@ -531,11 +531,14 @@ 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)) - commit, err = m.getCommit(block.Header) + commit, err = m.getCommit(block.SignedHeader.Header) if err != nil { return err } + // set the commit to current block's signed header + block.SignedHeader.Commit = *commit + // SaveBlock commits the DB tx err = m.store.SaveBlock(block, commit) if err != nil { @@ -550,7 +553,7 @@ func (m *Manager) publishBlock(ctx context.Context) error { } if commit == nil { - commit, err = m.getCommit(block.Header) + commit, err = m.getCommit(block.SignedHeader.Header) if err != nil { return err } @@ -575,7 +578,7 @@ func (m *Manager) publishBlock(ctx context.Context) error { } // SaveBlockResponses commits the DB tx - err = m.store.SaveBlockResponses(uint64(block.Header.Height()), responses) + err = m.store.SaveBlockResponses(uint64(block.SignedHeader.Header.Height()), responses) if err != nil { return err } @@ -591,13 +594,13 @@ func (m *Manager) publishBlock(ctx context.Context) error { } // SaveValidators commits the DB tx - err = m.store.SaveValidators(uint64(block.Header.Height()), m.lastState.Validators) + err = m.store.SaveValidators(uint64(block.SignedHeader.Header.Height()), m.lastState.Validators) if err != nil { return err } // Only update the stored height after successfully submitting to DA layer and committing to the DB - m.store.SetHeight(uint64(block.Header.Height())) + m.store.SetHeight(uint64(block.SignedHeader.Header.Height())) m.publishSignedHeader(block, commit) @@ -605,14 +608,14 @@ func (m *Manager) publishBlock(ctx context.Context) error { } func (m *Manager) submitBlockToDA(ctx context.Context, block *types.Block) error { - m.logger.Info("submitting block to DA layer", "height", block.Header.Height()) + m.logger.Info("submitting block to DA layer", "height", block.SignedHeader.Header.Height()) submitted := false backoff := initialBackoff for attempt := 1; ctx.Err() == nil && !submitted && attempt <= maxSubmitAttempts; attempt++ { res := m.dalc.SubmitBlock(ctx, block) if res.Code == da.StatusSuccess { - m.logger.Info("successfully submitted Rollkit block to DA layer", "rollkitHeight", block.Header.Height(), "daHeight", res.DAHeight) + m.logger.Info("successfully submitted Rollkit block to DA layer", "rollkitHeight", block.SignedHeader.Header.Height(), "daHeight", res.DAHeight) submitted = true } else { m.logger.Error("DA layer submission failed", "error", res.Message, "attempt", attempt) @@ -638,7 +641,7 @@ func (m *Manager) exponentialBackoff(backoff time.Duration) time.Duration { // TODO(tzdybal): consider inlining func (m *Manager) publishSignedHeader(block *types.Block, commit *types.Commit) { - m.HeaderOutCh <- &types.SignedHeader{Header: block.Header, Commit: *commit} + m.HeaderOutCh <- &types.SignedHeader{Header: block.SignedHeader.Header, Commit: *commit} } func updateState(s *types.State, res *abci.ResponseInitChain) { diff --git a/conv/abci/block.go b/conv/abci/block.go index a20ebd52cf2..9d672d535db 100644 --- a/conv/abci/block.go +++ b/conv/abci/block.go @@ -72,14 +72,14 @@ func ToABCIHeader(header *types.Header) (tmtypes.Header, error) { // ToABCIBlock converts Rolkit block into block format defined by ABCI. // Returned block should pass `ValidateBasic`. func ToABCIBlock(block *types.Block) (*tmtypes.Block, error) { - abciHeader, err := ToABCIHeader(&block.Header) + abciHeader, err := ToABCIHeader(&block.SignedHeader.Header) if err != nil { return nil, err } - abciCommit := ToABCICommit(&block.LastCommit) + abciCommit := ToABCICommit(&block.SignedHeader.Commit) // This assumes that we have only one signature if len(abciCommit.Signatures) == 1 { - abciCommit.Signatures[0].ValidatorAddress = block.Header.ProposerAddress + abciCommit.Signatures[0].ValidatorAddress = block.SignedHeader.Header.ProposerAddress } abciBlock := tmtypes.Block{ Header: abciHeader, @@ -92,7 +92,7 @@ func ToABCIBlock(block *types.Block) (*tmtypes.Block, error) { for i := range block.Data.Txs { abciBlock.Data.Txs[i] = tmtypes.Tx(block.Data.Txs[i]) } - abciBlock.Header.DataHash = tmbytes.HexBytes(block.Header.DataHash) + abciBlock.Header.DataHash = tmbytes.HexBytes(block.SignedHeader.Header.DataHash) return &abciBlock, nil } diff --git a/da/mock/mock.go b/da/mock/mock.go index c9f22a886a7..7bb77c81e59 100644 --- a/da/mock/mock.go +++ b/da/mock/mock.go @@ -73,15 +73,15 @@ func (m *DataAvailabilityLayerClient) Stop() error { // triggers a state transition in the DA layer. func (m *DataAvailabilityLayerClient) SubmitBlock(ctx context.Context, block *types.Block) da.ResultSubmitBlock { daHeight := atomic.LoadUint64(&m.daHeight) - m.logger.Debug("Submitting block to DA layer!", "height", block.Header.Height(), "dataLayerHeight", daHeight) + m.logger.Debug("Submitting block to DA layer!", "height", block.SignedHeader.Header.Height(), "dataLayerHeight", daHeight) - hash := block.Header.Hash() + hash := block.SignedHeader.Header.Hash() blob, err := block.MarshalBinary() if err != nil { return da.ResultSubmitBlock{BaseResult: da.BaseResult{Code: da.StatusError, Message: err.Error()}} } - err = m.dalcKV.Put(ctx, getKey(daHeight, uint64(block.Header.Height())), hash[:]) + err = m.dalcKV.Put(ctx, getKey(daHeight, uint64(block.SignedHeader.Header.Height())), hash[:]) if err != nil { return da.ResultSubmitBlock{BaseResult: da.BaseResult{Code: da.StatusError, Message: err.Error()}} } diff --git a/da/test/da_test.go b/da/test/da_test.go index cc5e6b55e77..0f168332f0b 100644 --- a/da/test/da_test.go +++ b/da/test/da_test.go @@ -240,12 +240,13 @@ func doTestRetrieve(t *testing.T, dalc da.DataAvailabilityLayerClient) { // copy-pasted from store/store_test.go func getRandomBlock(height uint64, nTxs int) *types.Block { block := &types.Block{ - Header: types.Header{ - BaseHeader: types.BaseHeader{ - Height: height, - }, - AggregatorsHash: make([]byte, 32), - }, + SignedHeader: types.SignedHeader{ + Header: types.Header{ + BaseHeader: types.BaseHeader{ + Height: height, + }, + AggregatorsHash: make([]byte, 32), + }}, Data: types.Data{ Txs: make(types.Txs, nTxs), IntermediateStateRoots: types.IntermediateStateRoots{ @@ -253,7 +254,7 @@ func getRandomBlock(height uint64, nTxs int) *types.Block { }, }, } - block.Header.AppHash = getRandomBytes(32) + block.SignedHeader.Header.AppHash = getRandomBytes(32) for i := 0; i < nTxs; i++ { block.Data.Txs[i] = getRandomTx() diff --git a/node/full_client.go b/node/full_client.go index 6ac25224d80..8cc379eb8a7 100644 --- a/node/full_client.go +++ b/node/full_client.go @@ -703,11 +703,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.Header.Height())) + validators, err := c.node.Store.LoadValidators(uint64(latest.SignedHeader.Header.Height())) if err != nil { return nil, fmt.Errorf("failed to fetch the validator info at latest block: %w", err) } - _, validator := validators.GetByAddress(latest.Header.ProposerAddress) + _, validator := validators.GetByAddress(latest.SignedHeader.Header.ProposerAddress) state, err := c.node.Store.LoadState() if err != nil { @@ -735,14 +735,14 @@ func (c *FullClient) Status(ctx context.Context) (*ctypes.ResultStatus, error) { }, }, SyncInfo: ctypes.SyncInfo{ - LatestBlockHash: tmbytes.HexBytes(latest.Header.DataHash), - LatestAppHash: tmbytes.HexBytes(latest.Header.AppHash), - LatestBlockHeight: latest.Header.Height(), - LatestBlockTime: latest.Header.Time(), - EarliestBlockHash: tmbytes.HexBytes(initial.Header.DataHash), - EarliestAppHash: tmbytes.HexBytes(initial.Header.AppHash), - EarliestBlockHeight: initial.Header.Height(), - EarliestBlockTime: initial.Header.Time(), + LatestBlockHash: tmbytes.HexBytes(latest.SignedHeader.Header.DataHash), + LatestAppHash: tmbytes.HexBytes(latest.SignedHeader.Header.AppHash), + LatestBlockHeight: latest.SignedHeader.Header.Height(), + LatestBlockTime: latest.SignedHeader.Header.Time(), + EarliestBlockHash: tmbytes.HexBytes(initial.SignedHeader.Header.DataHash), + EarliestAppHash: tmbytes.HexBytes(initial.SignedHeader.Header.AppHash), + EarliestBlockHeight: initial.SignedHeader.Header.Height(), + EarliestBlockTime: initial.SignedHeader.Header.Time(), CatchingUp: true, // the client is always syncing in the background to the latest height }, ValidatorInfo: ctypes.ValidatorInfo{ diff --git a/node/full_client_test.go b/node/full_client_test.go index 7badd106008..f74d500b2f7 100644 --- a/node/full_client_test.go +++ b/node/full_client_test.go @@ -247,7 +247,7 @@ func TestGetBlock(t *testing.T) { block := getRandomBlock(1, 10) err = rpc.node.Store.SaveBlock(block, &types.Commit{}) - rpc.node.Store.SetHeight(uint64(block.Header.Height())) + rpc.node.Store.SetHeight(uint64(block.SignedHeader.Header.Height())) require.NoError(err) blockResp, err := rpc.Block(context.Background(), nil) @@ -273,17 +273,17 @@ func TestGetCommit(t *testing.T) { require.NoError(err) for _, b := range blocks { - err = rpc.node.Store.SaveBlock(b, &types.Commit{Height: uint64(b.Header.Height())}) - rpc.node.Store.SetHeight(uint64(b.Header.Height())) + err = rpc.node.Store.SaveBlock(b, &types.Commit{Height: uint64(b.SignedHeader.Header.Height())}) + rpc.node.Store.SetHeight(uint64(b.SignedHeader.Header.Height())) require.NoError(err) } t.Run("Fetch all commits", func(t *testing.T) { for _, b := range blocks { - h := b.Header.Height() + h := b.SignedHeader.Header.Height() commit, err := rpc.Commit(context.Background(), &h) require.NoError(err) require.NotNil(commit) - assert.Equal(b.Header.Height(), commit.Height) + assert.Equal(b.SignedHeader.Header.Height(), commit.Height) } }) @@ -291,7 +291,7 @@ func TestGetCommit(t *testing.T) { commit, err := rpc.Commit(context.Background(), nil) require.NoError(err) require.NotNil(commit) - assert.Equal(blocks[3].Header.Height(), commit.Height) + assert.Equal(blocks[3].SignedHeader.Header.Height(), commit.Height) }) err = rpc.node.Stop() @@ -310,7 +310,7 @@ func TestBlockSearch(t *testing.T) { block := getRandomBlock(uint64(h), 5) err := rpc.node.Store.SaveBlock(block, &types.Commit{ Height: uint64(h), - HeaderHash: block.Header.Hash(), + HeaderHash: block.SignedHeader.Header.Hash(), }) require.NoError(err) } @@ -377,14 +377,14 @@ func TestGetBlockByHash(t *testing.T) { abciBlock, err := abciconv.ToABCIBlock(block) require.NoError(err) - height := block.Header.Height() + height := block.SignedHeader.Header.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.Header.Hash() + blockHash := block.SignedHeader.Header.Hash() blockResp, err := rpc.BlockByHash(context.Background(), blockHash[:]) require.NoError(err) require.NotNil(blockResp) @@ -564,9 +564,9 @@ func TestBlockchainInfo(t *testing.T) { block := getRandomBlock(uint64(h), 5) err := rpc.node.Store.SaveBlock(block, &types.Commit{ Height: uint64(h), - HeaderHash: block.Header.Hash(), + HeaderHash: block.SignedHeader.Header.Hash(), }) - rpc.node.Store.SetHeight(uint64(block.Header.Height())) + rpc.node.Store.SetHeight(uint64(block.SignedHeader.Header.Height())) require.NoError(err) } @@ -719,14 +719,15 @@ func getRandomBlock(height uint64, nTxs int) *types.Block { func getRandomBlockWithProposer(height uint64, nTxs int, proposerAddr []byte) *types.Block { block := &types.Block{ - Header: types.Header{ - BaseHeader: types.BaseHeader{ - Height: height, - }, - Version: types.Version{Block: types.InitStateVersion.Consensus.Block}, - ProposerAddress: proposerAddr, - AggregatorsHash: make([]byte, 32), - }, + SignedHeader: types.SignedHeader{ + Header: types.Header{ + BaseHeader: types.BaseHeader{ + Height: height, + }, + Version: types.Version{Block: types.InitStateVersion.Consensus.Block}, + ProposerAddress: proposerAddr, + AggregatorsHash: make([]byte, 32), + }}, Data: types.Data{ Txs: make(types.Txs, nTxs), IntermediateStateRoots: types.IntermediateStateRoots{ @@ -734,7 +735,7 @@ func getRandomBlockWithProposer(height uint64, nTxs int, proposerAddr []byte) *t }, }, } - block.Header.AppHash = getRandomBytes(32) + block.SignedHeader.Header.AppHash = getRandomBytes(32) for i := 0; i < nTxs; i++ { block.Data.Txs[i] = getRandomTx() @@ -753,7 +754,7 @@ func getRandomBlockWithProposer(height uint64, nTxs int, proposerAddr []byte) *t } lastCommitHash := make(types.Hash, 32) copy(lastCommitHash, tmprotoLC.Hash().Bytes()) - block.Header.LastCommitHash = lastCommitHash + block.SignedHeader.Header.LastCommitHash = lastCommitHash return block } @@ -987,13 +988,13 @@ func TestStatus(t *testing.T) { assert.NotNil(rpc) earliestBlock := getRandomBlockWithProposer(1, 1, validators[0].Address.Bytes()) - err = rpc.node.Store.SaveBlock(earliestBlock, &types.Commit{Height: uint64(earliestBlock.Header.Height())}) - rpc.node.Store.SetHeight(uint64(earliestBlock.Header.Height())) + err = rpc.node.Store.SaveBlock(earliestBlock, &types.Commit{Height: uint64(earliestBlock.SignedHeader.Header.Height())}) + rpc.node.Store.SetHeight(uint64(earliestBlock.SignedHeader.Header.Height())) require.NoError(err) latestBlock := getRandomBlockWithProposer(2, 1, validators[1].Address.Bytes()) - err = rpc.node.Store.SaveBlock(latestBlock, &types.Commit{Height: uint64(latestBlock.Header.Height())}) - rpc.node.Store.SetHeight(uint64(latestBlock.Header.Height())) + err = rpc.node.Store.SaveBlock(latestBlock, &types.Commit{Height: uint64(latestBlock.SignedHeader.Header.Height())}) + rpc.node.Store.SetHeight(uint64(latestBlock.SignedHeader.Header.Height())) require.NoError(err) err = node.Start() diff --git a/proto/rollkit/rollkit.proto b/proto/rollkit/rollkit.proto index abd174f272b..f959c395439 100644 --- a/proto/rollkit/rollkit.proto +++ b/proto/rollkit/rollkit.proto @@ -49,19 +49,19 @@ message Header { // pubkey can't be recovered by the signature (e.g. ed25519). bytes proposer_address = 10; + // public key of the proposer so we can verify the signature + bytes proposer_public_key = 11; + // Hash of block aggregator set, at a time of block creation - bytes aggregators_hash = 11; + bytes aggregators_hash = 12; // Chain ID the block belongs to - string chain_id = 12; + string chain_id = 13; } message Commit { - uint64 height = 1; - bytes header_hash = 2; - // Note: most of the time this will be a single sinature - repeated bytes signatures = 3; + repeated bytes signatures = 1; } message SignedHeader { @@ -76,7 +76,6 @@ message Data { } message Block { - Header header = 1; + SignedHeader signed_header = 1; Data data = 2; - Commit last_commit = 3; } diff --git a/state/executor.go b/state/executor.go index fe466cda443..ab825d5d25b 100644 --- a/state/executor.go +++ b/state/executor.go @@ -99,34 +99,36 @@ func (e *BlockExecutor) CreateBlock(height uint64, lastCommit *types.Commit, las mempoolTxs := e.mempool.ReapMaxBytesMaxGas(maxBytes, maxGas) block := &types.Block{ - Header: types.Header{ - Version: types.Version{ - Block: state.Version.Consensus.Block, - App: state.Version.Consensus.App, + SignedHeader: types.SignedHeader{ + Header: types.Header{ + Version: types.Version{ + Block: state.Version.Consensus.Block, + App: state.Version.Consensus.App, + }, + BaseHeader: types.BaseHeader{ + ChainID: e.chainID, + Height: height, + Time: uint64(time.Now().Unix()), // TODO(tzdybal): how to get TAI64? + }, + //LastHeaderHash: lastHeaderHash, + //LastCommitHash: lastCommitHash, + DataHash: make(types.Hash, 32), + ConsensusHash: make(types.Hash, 32), + AppHash: state.AppHash, + LastResultsHash: state.LastResultsHash, + ProposerAddress: e.proposerAddress, }, - BaseHeader: types.BaseHeader{ - ChainID: e.chainID, - Height: height, - Time: uint64(time.Now().Unix()), // TODO(tzdybal): how to get TAI64? - }, - //LastHeaderHash: lastHeaderHash, - //LastCommitHash: lastCommitHash, - DataHash: make(types.Hash, 32), - ConsensusHash: make(types.Hash, 32), - AppHash: state.AppHash, - LastResultsHash: state.LastResultsHash, - ProposerAddress: e.proposerAddress, + Commit: *lastCommit, }, Data: types.Data{ Txs: toRollkitTxs(mempoolTxs), IntermediateStateRoots: types.IntermediateStateRoots{RawRootsList: nil}, Evidence: types.EvidenceData{Evidence: nil}, }, - LastCommit: *lastCommit, } - block.Header.LastCommitHash = e.getLastCommitHash(lastCommit, &block.Header) - block.Header.LastHeaderHash = lastHeaderHash - block.Header.AggregatorsHash = state.Validators.Hash() + block.SignedHeader.Header.LastCommitHash = e.getLastCommitHash(lastCommit, &block.SignedHeader.Header) + block.SignedHeader.Header.LastHeaderHash = lastHeaderHash + block.SignedHeader.Header.AggregatorsHash = state.Validators.Hash() return block } @@ -211,7 +213,7 @@ func (e *BlockExecutor) updateState(state types.State, block *types.Block, abciR return state, nil } // Change results from this height but only applies to the next next height. - lastHeightValSetChanged = block.Header.Height() + 1 + 1 + lastHeightValSetChanged = block.SignedHeader.Header.Height() + 1 + 1 } // TODO(tzdybal): right now, it's for backward compatibility, may need to change this @@ -222,10 +224,10 @@ func (e *BlockExecutor) updateState(state types.State, block *types.Block, abciR Version: state.Version, ChainID: state.ChainID, InitialHeight: state.InitialHeight, - LastBlockHeight: block.Header.Height(), - LastBlockTime: block.Header.Time(), + LastBlockHeight: block.SignedHeader.Header.Height(), + LastBlockTime: block.SignedHeader.Header.Time(), LastBlockID: tmtypes.BlockID{ - Hash: tmbytes.HexBytes(block.Header.Hash()), + Hash: tmbytes.HexBytes(block.SignedHeader.Header.Hash()), // for now, we don't care about part set headers }, NextValidators: nValSet, @@ -257,7 +259,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.Header.Height()), fromRollkitTxs(block.Data.Txs), deliverTxs, mempool.PreCheckMaxBytes(maxBytes), mempool.PostCheckMaxGas(maxGas)) + err = e.mempool.Update(int64(block.SignedHeader.Header.Height()), fromRollkitTxs(block.Data.Txs), deliverTxs, mempool.PreCheckMaxBytes(maxBytes), mempool.PostCheckMaxGas(maxGas)) if err != nil { return nil, 0, err } @@ -270,21 +272,21 @@ func (e *BlockExecutor) validate(state types.State, block *types.Block) error { if err != nil { return err } - if block.Header.Version.App != state.Version.Consensus.App || - block.Header.Version.Block != state.Version.Consensus.Block { + if block.SignedHeader.Header.Version.App != state.Version.Consensus.App || + block.SignedHeader.Header.Version.Block != state.Version.Consensus.Block { return errors.New("block version mismatch") } - if state.LastBlockHeight <= 0 && block.Header.Height() != state.InitialHeight { + if state.LastBlockHeight <= 0 && block.SignedHeader.Header.Height() != state.InitialHeight { return errors.New("initial block height mismatch") } - if state.LastBlockHeight > 0 && block.Header.Height() != state.LastBlockHeight+1 { + if state.LastBlockHeight > 0 && block.SignedHeader.Header.Height() != state.LastBlockHeight+1 { return errors.New("block height mismatch") } - if !bytes.Equal(block.Header.AppHash[:], state.AppHash[:]) { + if !bytes.Equal(block.SignedHeader.Header.AppHash[:], state.AppHash[:]) { return errors.New("AppHash mismatch") } - if !bytes.Equal(block.Header.LastResultsHash[:], state.LastResultsHash[:]) { + if !bytes.Equal(block.SignedHeader.Header.LastResultsHash[:], state.LastResultsHash[:]) { return errors.New("LastResultsHash mismatch") } @@ -351,7 +353,7 @@ func (e *BlockExecutor) execute(ctx context.Context, state types.State, block *t } hash := block.Hash() - abciHeader, err := abciconv.ToABCIHeaderPB(&block.Header) + abciHeader, err := abciconv.ToABCIHeaderPB(&block.SignedHeader.Header) if err != nil { return nil, err } @@ -390,7 +392,7 @@ func (e *BlockExecutor) execute(ctx context.Context, state types.State, block *t return nil, err } } - endBlockRequest := abci.RequestEndBlock{Height: block.Header.Height()} + endBlockRequest := abci.RequestEndBlock{Height: block.SignedHeader.Header.Height()} abciResponses.EndBlock, err = e.proxyApp.EndBlockSync(endBlockRequest) if err != nil { return nil, err @@ -477,13 +479,13 @@ func (e *BlockExecutor) publishEvents(resp *tmstate.ABCIResponses, block *types. for _, ev := range abciBlock.Evidence.Evidence { err = multierr.Append(err, e.eventBus.PublishEventNewEvidence(tmtypes.EventDataNewEvidence{ Evidence: ev, - Height: block.Header.Height(), + Height: block.SignedHeader.Header.Height(), })) } for i, dtx := range resp.DeliverTxs { err = multierr.Append(err, e.eventBus.PublishEventTx(tmtypes.EventDataTx{ TxResult: abci.TxResult{ - Height: block.Header.Height(), + Height: block.SignedHeader.Header.Height(), Index: uint32(i), Tx: abciBlock.Data.Txs[i], Result: *dtx, diff --git a/state/executor_test.go b/state/executor_test.go index 65d154a276e..bb0a9788107 100644 --- a/state/executor_test.go +++ b/state/executor_test.go @@ -50,14 +50,14 @@ func doTestCreateBlock(t *testing.T, fraudProofsEnabled bool) { block := executor.CreateBlock(1, &types.Commit{}, []byte{}, state) require.NotNil(block) assert.Empty(block.Data.Txs) - assert.Equal(int64(1), block.Header.Height()) + assert.Equal(int64(1), block.SignedHeader.Header.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.Header.Height()) + assert.Equal(int64(2), block.SignedHeader.Header.Height()) assert.Len(block.Data.Txs, 1) // now there are 3 Txs, and only two can fit into single block @@ -138,7 +138,7 @@ func doTestApplyBlock(t *testing.T, fraudProofsEnabled bool) { require.NoError(err) block := executor.CreateBlock(1, &types.Commit{}, []byte{}, state) require.NotNil(block) - assert.Equal(int64(1), block.Header.Height()) + assert.Equal(int64(1), block.SignedHeader.Header.Height()) assert.Len(block.Data.Txs, 1) newState, resp, err := executor.ApplyBlock(context.Background(), state, block) @@ -156,7 +156,7 @@ func doTestApplyBlock(t *testing.T, fraudProofsEnabled bool) { require.NoError(mpool.CheckTx(make([]byte, 90), func(r *abci.Response) {}, mempool.TxInfo{})) block = executor.CreateBlock(2, &types.Commit{}, []byte{}, newState) require.NotNil(block) - assert.Equal(int64(2), block.Header.Height()) + assert.Equal(int64(2), block.SignedHeader.Header.Height()) assert.Len(block.Data.Txs, 3) newState, resp, err = executor.ApplyBlock(context.Background(), newState, block) diff --git a/store/store.go b/store/store.go index cd6629a49aa..34070944d30 100644 --- a/store/store.go +++ b/store/store.go @@ -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.Header.Hash() + hash := block.SignedHeader.Header.Hash() blockBlob, err := block.MarshalBinary() if err != nil { return fmt.Errorf("failed to marshal Block to binary: %w", err) @@ -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.Header.Height()))), hash[:])) + err = multierr.Append(err, bb.Put(s.ctx, ds.NewKey(getIndexKey(uint64(block.SignedHeader.Header.Height()))), hash[:])) if err != nil { bb.Discard(s.ctx) diff --git a/store/store_test.go b/store/store_test.go index a2902def927..e24271c537f 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -51,7 +51,7 @@ func TestStoreHeight(t *testing.T) { for _, block := range c.blocks { err := bstore.SaveBlock(block, &types.Commit{}) - bstore.SetHeight(uint64(block.Header.Height())) + bstore.SetHeight(uint64(block.SignedHeader.Header.Height())) assert.NoError(err) } @@ -102,28 +102,22 @@ func TestStoreLoad(t *testing.T) { lastCommit := &types.Commit{} for _, block := range c.blocks { - commit := &types.Commit{Height: uint64(block.Header.Height()), HeaderHash: block.Header.Hash()} - block.LastCommit = *lastCommit + commit := &types.Commit{} + block.SignedHeader.Commit = *lastCommit err := bstore.SaveBlock(block, commit) require.NoError(err) lastCommit = commit } for _, expected := range c.blocks { - block, err := bstore.LoadBlock(uint64(expected.Header.Height())) + block, err := bstore.LoadBlock(uint64(expected.SignedHeader.Header.Height())) assert.NoError(err) assert.NotNil(block) assert.Equal(expected, block) - assert.Equal(expected.Header.Height()-1, int64(block.LastCommit.Height)) - assert.Equal(expected.LastCommit.Height, block.LastCommit.Height) - assert.Equal(expected.LastCommit.HeaderHash, block.LastCommit.HeaderHash) - commit, err := bstore.LoadCommit(uint64(expected.Header.Height())) + commit, err := bstore.LoadCommit(uint64(expected.SignedHeader.Header.Height())) assert.NoError(err) assert.NotNil(commit) - assert.Equal(uint64(expected.Header.Height()), commit.Height) - headerHash := expected.Header.Hash() - assert.Equal(headerHash, commit.HeaderHash) } }) } @@ -201,12 +195,13 @@ func TestBlockResponses(t *testing.T) { func getRandomBlock(height uint64, nTxs int) *types.Block { block := &types.Block{ - Header: types.Header{ - BaseHeader: types.BaseHeader{ - Height: height, - }, - AggregatorsHash: make([]byte, 32), - }, + SignedHeader: types.SignedHeader{ + Header: types.Header{ + BaseHeader: types.BaseHeader{ + Height: height, + }, + AggregatorsHash: make([]byte, 32), + }}, Data: types.Data{ Txs: make(types.Txs, nTxs), IntermediateStateRoots: types.IntermediateStateRoots{ diff --git a/types/block.go b/types/block.go index 6948e90ed0a..118c86b5652 100644 --- a/types/block.go +++ b/types/block.go @@ -17,9 +17,8 @@ type Version struct { // Block defines the structure of Rollkit block. type Block struct { - Header Header - Data Data - LastCommit Commit + SignedHeader SignedHeader + Data Data } var _ encoding.BinaryMarshaler = &Block{} diff --git a/types/hashing.go b/types/hashing.go index 80530740eea..01ed4d8d171 100644 --- a/types/hashing.go +++ b/types/hashing.go @@ -38,5 +38,5 @@ func (h *Header) Hash() Hash { // Hash returns ABCI-compatible hash of a block. func (b *Block) Hash() Hash { - return b.Header.Hash() + return b.SignedHeader.Header.Hash() } diff --git a/types/pb/rollkit/rollkit.pb.go b/types/pb/rollkit/rollkit.pb.go index 862d3432649..41d5f296027 100644 --- a/types/pb/rollkit/rollkit.pb.go +++ b/types/pb/rollkit/rollkit.pb.go @@ -106,10 +106,12 @@ type Header struct { // We keep this in case users choose another signature format where the // pubkey can't be recovered by the signature (e.g. ed25519). ProposerAddress []byte `protobuf:"bytes,10,opt,name=proposer_address,json=proposerAddress,proto3" json:"proposer_address,omitempty"` + // public key of the proposer so we can verify the signature + ProposerPublicKey []byte `protobuf:"bytes,11,opt,name=proposer_public_key,json=proposerPublicKey,proto3" json:"proposer_public_key,omitempty"` // Hash of block aggregator set, at a time of block creation - AggregatorsHash []byte `protobuf:"bytes,11,opt,name=aggregators_hash,json=aggregatorsHash,proto3" json:"aggregators_hash,omitempty"` + AggregatorsHash []byte `protobuf:"bytes,12,opt,name=aggregators_hash,json=aggregatorsHash,proto3" json:"aggregators_hash,omitempty"` // Chain ID the block belongs to - ChainId string `protobuf:"bytes,12,opt,name=chain_id,json=chainId,proto3" json:"chain_id,omitempty"` + ChainId string `protobuf:"bytes,13,opt,name=chain_id,json=chainId,proto3" json:"chain_id,omitempty"` } func (m *Header) Reset() { *m = Header{} } @@ -215,6 +217,13 @@ func (m *Header) GetProposerAddress() []byte { return nil } +func (m *Header) GetProposerPublicKey() []byte { + if m != nil { + return m.ProposerPublicKey + } + return nil +} + func (m *Header) GetAggregatorsHash() []byte { if m != nil { return m.AggregatorsHash @@ -230,10 +239,7 @@ func (m *Header) GetChainId() string { } type Commit struct { - Height uint64 `protobuf:"varint,1,opt,name=height,proto3" json:"height,omitempty"` - HeaderHash []byte `protobuf:"bytes,2,opt,name=header_hash,json=headerHash,proto3" json:"header_hash,omitempty"` - // Note: most of the time this will be a single sinature - Signatures [][]byte `protobuf:"bytes,3,rep,name=signatures,proto3" json:"signatures,omitempty"` + Signatures [][]byte `protobuf:"bytes,1,rep,name=signatures,proto3" json:"signatures,omitempty"` } func (m *Commit) Reset() { *m = Commit{} } @@ -269,20 +275,6 @@ func (m *Commit) XXX_DiscardUnknown() { var xxx_messageInfo_Commit proto.InternalMessageInfo -func (m *Commit) GetHeight() uint64 { - if m != nil { - return m.Height - } - return 0 -} - -func (m *Commit) GetHeaderHash() []byte { - if m != nil { - return m.HeaderHash - } - return nil -} - func (m *Commit) GetSignatures() [][]byte { if m != nil { return m.Signatures @@ -403,9 +395,8 @@ func (m *Data) GetEvidence() []*types.Evidence { } type Block struct { - Header *Header `protobuf:"bytes,1,opt,name=header,proto3" json:"header,omitempty"` - Data *Data `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` - LastCommit *Commit `protobuf:"bytes,3,opt,name=last_commit,json=lastCommit,proto3" json:"last_commit,omitempty"` + SignedHeader *SignedHeader `protobuf:"bytes,1,opt,name=signed_header,json=signedHeader,proto3" json:"signed_header,omitempty"` + Data *Data `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` } func (m *Block) Reset() { *m = Block{} } @@ -441,9 +432,9 @@ func (m *Block) XXX_DiscardUnknown() { var xxx_messageInfo_Block proto.InternalMessageInfo -func (m *Block) GetHeader() *Header { +func (m *Block) GetSignedHeader() *SignedHeader { if m != nil { - return m.Header + return m.SignedHeader } return nil } @@ -455,13 +446,6 @@ func (m *Block) GetData() *Data { return nil } -func (m *Block) GetLastCommit() *Commit { - if m != nil { - return m.LastCommit - } - return nil -} - func init() { proto.RegisterType((*Version)(nil), "rollkit.Version") proto.RegisterType((*Header)(nil), "rollkit.Header") @@ -474,44 +458,45 @@ func init() { func init() { proto.RegisterFile("rollkit/rollkit.proto", fileDescriptor_ed489fb7f4d78b3f) } var fileDescriptor_ed489fb7f4d78b3f = []byte{ - // 587 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x94, 0x41, 0x6f, 0xd3, 0x30, - 0x14, 0xc7, 0x97, 0xb5, 0x6b, 0xda, 0xd7, 0x6e, 0x2b, 0x11, 0x4c, 0x19, 0x93, 0x42, 0x89, 0x84, - 0x28, 0x43, 0x4a, 0x61, 0x08, 0x89, 0x2b, 0x83, 0x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x04, 0x00, 0x00, + // 595 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x5c, 0x93, 0xc1, 0x6f, 0xd3, 0x3e, + 0x14, 0xc7, 0x97, 0xb5, 0x6b, 0xbb, 0xb7, 0x76, 0xeb, 0xf2, 0xfb, 0x6d, 0xca, 0x98, 0x14, 0x95, + 0x4a, 0x88, 0x30, 0xa4, 0x54, 0x0c, 0x21, 0x21, 0x6e, 0x0c, 0x26, 0x0d, 0x71, 0x41, 0x99, 0xc4, + 0x81, 0x4b, 0x70, 0x13, 0x93, 0x58, 0x6b, 0x63, 0xcb, 0x76, 0x27, 0xf6, 0x27, 0x70, 0xe3, 0xcf, + 0xe2, 0xb8, 0x23, 0xc7, 0x69, 0xfd, 0x47, 0x90, 0x9f, 0x9d, 0x2c, 0x70, 0x69, 0xed, 0xef, 0xf7, + 0xe3, 0xe7, 0xf7, 0x9c, 0xf7, 0xe0, 0x40, 0xf2, 0xc5, 0xe2, 0x8a, 0xe9, 0x99, 0xfb, 0x8f, 0x85, + 0xe4, 0x9a, 0xfb, 0x7d, 0xb7, 0x7d, 0x74, 0xac, 0x69, 0x95, 0x53, 0xb9, 0x64, 0x95, 0x9e, 0x91, + 0x79, 0xc6, 0x66, 0xfa, 0x46, 0x50, 0x65, 0xa9, 0xe9, 0x0b, 0xe8, 0x7f, 0xa6, 0x52, 0x31, 0x5e, + 0xf9, 0xff, 0xc3, 0xd6, 0x7c, 0xc1, 0xb3, 0xab, 0xc0, 0x9b, 0x78, 0x51, 0x37, 0xb1, 0x1b, 0x7f, + 0x0c, 0x1d, 0x22, 0x44, 0xb0, 0x89, 0x9a, 0x59, 0x4e, 0xef, 0x3a, 0xd0, 0xbb, 0xa0, 0x24, 0xa7, + 0xd2, 0x3f, 0x81, 0xfe, 0xb5, 0x3d, 0x8d, 0x87, 0x76, 0x4e, 0xc7, 0x71, 0x9d, 0x84, 0x8b, 0x9a, + 0xd4, 0x80, 0x7f, 0x08, 0xbd, 0x92, 0xb2, 0xa2, 0xd4, 0x2e, 0x96, 0xdb, 0xf9, 0x3e, 0x74, 0x35, + 0x5b, 0xd2, 0xa0, 0x83, 0x2a, 0xae, 0xfd, 0x08, 0xc6, 0x0b, 0xa2, 0x74, 0x5a, 0xe2, 0x35, 0x69, + 0x49, 0x54, 0x19, 0x74, 0x27, 0x5e, 0x34, 0x4c, 0x76, 0x8d, 0x6e, 0x6f, 0xbf, 0x20, 0xaa, 0x6c, + 0xc8, 0x8c, 0x2f, 0x97, 0x4c, 0x5b, 0x72, 0xeb, 0x81, 0x7c, 0x87, 0x32, 0x92, 0xc7, 0xb0, 0x9d, + 0x13, 0x4d, 0x2c, 0xd2, 0x43, 0x64, 0x60, 0x04, 0x34, 0x9f, 0xc0, 0x6e, 0xc6, 0x2b, 0x45, 0x2b, + 0xb5, 0x52, 0x96, 0xe8, 0x23, 0x31, 0x6a, 0x54, 0xc4, 0x8e, 0x60, 0x40, 0x84, 0xb0, 0xc0, 0x00, + 0x81, 0x3e, 0x11, 0x02, 0xad, 0x13, 0xd8, 0xc7, 0x44, 0x24, 0x55, 0xab, 0x85, 0x76, 0x41, 0xb6, + 0x91, 0xd9, 0x33, 0x46, 0x62, 0x75, 0x64, 0x9f, 0xc1, 0x58, 0x48, 0x2e, 0xb8, 0xa2, 0x32, 0x25, + 0x79, 0x2e, 0xa9, 0x52, 0x01, 0x58, 0xb4, 0xd6, 0xdf, 0x5a, 0xd9, 0x8f, 0xe1, 0xbf, 0x06, 0x15, + 0xab, 0xf9, 0x82, 0x65, 0xe9, 0x15, 0xbd, 0x09, 0x76, 0x90, 0xde, 0xaf, 0xad, 0x4f, 0xe8, 0x7c, + 0xa4, 0x37, 0x26, 0x34, 0x29, 0x0a, 0x49, 0x0b, 0xa2, 0xb9, 0x74, 0x59, 0x0c, 0x6d, 0xe8, 0x96, + 0x5e, 0x17, 0x93, 0x95, 0x84, 0x55, 0x29, 0xcb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x43, 0xef, 0xee, + 0x3e, 0xf4, 0x7e, 0xae, 0xc3, 0x8d, 0xdb, 0x75, 0xb8, 0xf1, 0x7b, 0x1d, 0x6e, 0x7c, 0x79, 0x5e, + 0x30, 0x5d, 0xae, 0xe6, 0x71, 0xc6, 0x97, 0xb3, 0x7f, 0xe6, 0xde, 0xce, 0xf5, 0x4c, 0xcc, 0x6b, + 0x61, 0xde, 0xc3, 0x19, 0x7f, 0xf9, 0x27, 0x00, 0x00, 0xff, 0xff, 0x4a, 0x6d, 0x58, 0xf2, 0x22, + 0x04, 0x00, 0x00, } func (m *Version) Marshal() (dAtA []byte, err error) { @@ -572,13 +557,20 @@ func (m *Header) MarshalToSizedBuffer(dAtA []byte) (int, error) { copy(dAtA[i:], m.ChainId) i = encodeVarintRollkit(dAtA, i, uint64(len(m.ChainId))) i-- - dAtA[i] = 0x62 + dAtA[i] = 0x6a } if len(m.AggregatorsHash) > 0 { i -= len(m.AggregatorsHash) copy(dAtA[i:], m.AggregatorsHash) i = encodeVarintRollkit(dAtA, i, uint64(len(m.AggregatorsHash))) i-- + dAtA[i] = 0x62 + } + if len(m.ProposerPublicKey) > 0 { + i -= len(m.ProposerPublicKey) + copy(dAtA[i:], m.ProposerPublicKey) + i = encodeVarintRollkit(dAtA, i, uint64(len(m.ProposerPublicKey))) + i-- dAtA[i] = 0x5a } if len(m.ProposerAddress) > 0 { @@ -681,21 +673,9 @@ func (m *Commit) MarshalToSizedBuffer(dAtA []byte) (int, error) { copy(dAtA[i:], m.Signatures[iNdEx]) i = encodeVarintRollkit(dAtA, i, uint64(len(m.Signatures[iNdEx]))) i-- - dAtA[i] = 0x1a + dAtA[i] = 0xa } } - if len(m.HeaderHash) > 0 { - i -= len(m.HeaderHash) - copy(dAtA[i:], m.HeaderHash) - i = encodeVarintRollkit(dAtA, i, uint64(len(m.HeaderHash))) - i-- - dAtA[i] = 0x12 - } - if m.Height != 0 { - i = encodeVarintRollkit(dAtA, i, uint64(m.Height)) - i-- - dAtA[i] = 0x8 - } return len(dAtA) - i, nil } @@ -821,18 +801,6 @@ func (m *Block) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - if m.LastCommit != nil { - { - size, err := m.LastCommit.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintRollkit(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x1a - } if m.Data != nil { { size, err := m.Data.MarshalToSizedBuffer(dAtA[:i]) @@ -845,9 +813,9 @@ func (m *Block) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x12 } - if m.Header != nil { + if m.SignedHeader != nil { { - size, err := m.Header.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.SignedHeader.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -930,6 +898,10 @@ func (m *Header) Size() (n int) { if l > 0 { n += 1 + l + sovRollkit(uint64(l)) } + l = len(m.ProposerPublicKey) + if l > 0 { + n += 1 + l + sovRollkit(uint64(l)) + } l = len(m.AggregatorsHash) if l > 0 { n += 1 + l + sovRollkit(uint64(l)) @@ -947,13 +919,6 @@ func (m *Commit) Size() (n int) { } var l int _ = l - if m.Height != 0 { - n += 1 + sovRollkit(uint64(m.Height)) - } - l = len(m.HeaderHash) - if l > 0 { - n += 1 + l + sovRollkit(uint64(l)) - } if len(m.Signatures) > 0 { for _, b := range m.Signatures { l = len(b) @@ -1013,18 +978,14 @@ func (m *Block) Size() (n int) { } var l int _ = l - if m.Header != nil { - l = m.Header.Size() + if m.SignedHeader != nil { + l = m.SignedHeader.Size() n += 1 + l + sovRollkit(uint64(l)) } if m.Data != nil { l = m.Data.Size() n += 1 + l + sovRollkit(uint64(l)) } - if m.LastCommit != nil { - l = m.LastCommit.Size() - n += 1 + l + sovRollkit(uint64(l)) - } return n } @@ -1464,6 +1425,40 @@ func (m *Header) Unmarshal(dAtA []byte) error { } iNdEx = postIndex case 11: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ProposerPublicKey", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowRollkit + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthRollkit + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthRollkit + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.ProposerPublicKey = append(m.ProposerPublicKey[:0], dAtA[iNdEx:postIndex]...) + if m.ProposerPublicKey == nil { + m.ProposerPublicKey = []byte{} + } + iNdEx = postIndex + case 12: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field AggregatorsHash", wireType) } @@ -1497,7 +1492,7 @@ func (m *Header) Unmarshal(dAtA []byte) error { m.AggregatorsHash = []byte{} } iNdEx = postIndex - case 12: + case 13: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field ChainId", wireType) } @@ -1580,59 +1575,6 @@ func (m *Commit) Unmarshal(dAtA []byte) error { } switch fieldNum { case 1: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Height", wireType) - } - m.Height = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowRollkit - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.Height |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field HeaderHash", wireType) - } - var byteLen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowRollkit - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - byteLen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if byteLen < 0 { - return ErrInvalidLengthRollkit - } - postIndex := iNdEx + byteLen - if postIndex < 0 { - return ErrInvalidLengthRollkit - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.HeaderHash = append(m.HeaderHash[:0], dAtA[iNdEx:postIndex]...) - if m.HeaderHash == nil { - m.HeaderHash = []byte{} - } - iNdEx = postIndex - case 3: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Signatures", wireType) } @@ -1986,7 +1928,7 @@ func (m *Block) Unmarshal(dAtA []byte) error { switch fieldNum { case 1: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Header", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field SignedHeader", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -2013,10 +1955,10 @@ func (m *Block) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if m.Header == nil { - m.Header = &Header{} + if m.SignedHeader == nil { + m.SignedHeader = &SignedHeader{} } - if err := m.Header.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.SignedHeader.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex @@ -2056,42 +1998,6 @@ func (m *Block) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex - case 3: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field LastCommit", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowRollkit - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthRollkit - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthRollkit - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if m.LastCommit == nil { - m.LastCommit = &Commit{} - } - if err := m.LastCommit.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipRollkit(dAtA[iNdEx:]) diff --git a/types/serialization.go b/types/serialization.go index 7206d9f2515..e49bb7e4b13 100644 --- a/types/serialization.go +++ b/types/serialization.go @@ -78,6 +78,12 @@ func (h *SignedHeader) FromProto(other *pb.SignedHeader) error { if err != nil { return err } + + if len(h.Commit.Signatures) > 0 { + h.Commit.HeaderHash = h.Header.Hash() + h.Commit.Height = uint64(h.Header.Height()) + } + return nil } @@ -146,9 +152,8 @@ func (h *Header) FromProto(other *pb.Header) error { // ToProto converts Block into protobuf representation and returns it. func (b *Block) ToProto() *pb.Block { return &pb.Block{ - Header: b.Header.ToProto(), - Data: b.Data.ToProto(), - LastCommit: b.LastCommit.ToProto(), + SignedHeader: b.SignedHeader.ToProto(), + Data: b.Data.ToProto(), } } @@ -163,19 +168,13 @@ func (d *Data) ToProto() *pb.Data { // FromProto fills Block with data from its protobuf representation. func (b *Block) FromProto(other *pb.Block) error { - err := b.Header.FromProto(other.Header) + err := b.SignedHeader.FromProto(other.SignedHeader) if err != nil { return err } b.Data.Txs = byteSlicesToTxs(other.Data.Txs) b.Data.IntermediateStateRoots.RawRootsList = other.Data.IntermediateStateRoots b.Data.Evidence = evidenceFromProto(other.Data.Evidence) - if other.LastCommit != nil { - err := b.LastCommit.FromProto(other.LastCommit) - if err != nil { - return err - } - } return nil } @@ -183,16 +182,12 @@ func (b *Block) FromProto(other *pb.Block) error { // ToProto converts Commit into protobuf representation and returns it. func (c *Commit) ToProto() *pb.Commit { return &pb.Commit{ - Height: c.Height, - HeaderHash: c.HeaderHash, Signatures: signaturesToByteSlices(c.Signatures), } } // FromProto fills Commit with data from its protobuf representation. func (c *Commit) FromProto(other *pb.Commit) error { - c.Height = other.Height - c.HeaderHash = other.HeaderHash c.Signatures = byteSlicesToSignatures(other.Signatures) return nil diff --git a/types/serialization_test.go b/types/serialization_test.go index 1a09b2d6d31..8274b2db18c 100644 --- a/types/serialization_test.go +++ b/types/serialization_test.go @@ -32,41 +32,44 @@ func TestBlockSerializationRoundTrip(t *testing.T) { h = append(h, h1) } + h1 := Header{ + Version: Version{ + Block: 1, + App: 2, + }, + BaseHeader: BaseHeader{ + Height: 3, + Time: 4567, + }, + LastHeaderHash: h[0], + LastCommitHash: h[1], + DataHash: h[2], + ConsensusHash: h[3], + AppHash: h[4], + LastResultsHash: h[5], + ProposerAddress: []byte{4, 3, 2, 1}, + AggregatorsHash: h[6], + } + cases := []struct { name string input *Block }{ {"empty block", &Block{}}, {"full", &Block{ - Header: Header{ - Version: Version{ - Block: 1, - App: 2, - }, - BaseHeader: BaseHeader{ - Height: 3, - Time: 4567, - }, - LastHeaderHash: h[0], - LastCommitHash: h[1], - DataHash: h[2], - ConsensusHash: h[3], - AppHash: h[4], - LastResultsHash: h[5], - ProposerAddress: []byte{4, 3, 2, 1}, - AggregatorsHash: h[6], - }, + SignedHeader: SignedHeader{ + Header: h1, + Commit: Commit{ + Height: 3, + HeaderHash: h1.Hash(), + Signatures: []Signature{Signature([]byte{1, 1, 1}), Signature([]byte{2, 2, 2})}, + }}, Data: Data{ Txs: nil, IntermediateStateRoots: IntermediateStateRoots{RawRootsList: [][]byte{{0x1}}}, // TODO(tzdybal): update when we have actual evidence types Evidence: EvidenceData{Evidence: nil}, }, - LastCommit: Commit{ - Height: 8, - HeaderHash: h[7][:], - Signatures: []Signature{Signature([]byte{1, 1, 1}), Signature([]byte{2, 2, 2})}, - }, }}, } diff --git a/types/validation.go b/types/validation.go index ee8503956c2..2d99df277b8 100644 --- a/types/validation.go +++ b/types/validation.go @@ -9,7 +9,7 @@ import ( // ValidateBasic performs basic validation of a block. func (b *Block) ValidateBasic() error { - err := b.Header.ValidateBasic() + err := b.SignedHeader.Header.ValidateBasic() if err != nil { return err } @@ -19,7 +19,7 @@ func (b *Block) ValidateBasic() error { return err } - err = b.LastCommit.ValidateBasic() + err = b.SignedHeader.Commit.ValidateBasic() if err != nil { return err }