diff --git a/block/manager.go b/block/manager.go index 645e3c5375..517a855fe5 100644 --- a/block/manager.go +++ b/block/manager.go @@ -169,7 +169,7 @@ func NewManager( return nil, err } - exec := state.NewBlockExecutor(proposerAddress, genesis.ChainID, mempool, proxyApp, eventBus, logger, execMetrics) + exec := state.NewBlockExecutor(proposerAddress, genesis.ChainID, mempool, proxyApp, eventBus, logger, execMetrics, valSet.Hash()) if s.LastBlockHeight+1 == uint64(genesis.InitialHeight) { res, err := exec.InitChain(genesis) if err != nil { @@ -633,11 +633,10 @@ func (m *Manager) getRemainingSleep(start time.Time) time.Duration { } 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) + // note: for compatibility with tendermint light client + consensusVote := header.MakeCometBFTVote() + + sign, err := m.proposerKey.Sign(consensusVote) if err != nil { return nil, err } @@ -708,6 +707,9 @@ func (m *Manager) publishBlock(ctx context.Context) error { } m.logger.Debug("block info", "num_tx", len(block.Data.Txs)) + block.SignedHeader.Validators = m.validatorSet + block.SignedHeader.ValidatorHash = m.validatorSet.Hash() + /* here we set the SignedHeader.DataHash, and SignedHeader.Commit as a hack to make the block pass ValidateBasic() when it gets called by applyBlock on line 681 @@ -731,8 +733,6 @@ func (m *Manager) publishBlock(ctx context.Context) error { } } - block.SignedHeader.Validators = m.validatorSet - newState, responses, err := m.applyBlock(ctx, block) if err != nil { if ctx.Err() != nil { @@ -755,7 +755,6 @@ func (m *Manager) publishBlock(ctx context.Context) error { // set the commit to current block's signed header block.SignedHeader.Commit = *commit - // Validate the created block before storing if err := m.executor.Validate(m.lastState, block); err != nil { return fmt.Errorf("failed to validate block: %w", err) @@ -778,10 +777,12 @@ func (m *Manager) publishBlock(ctx context.Context) error { m.pendingBlocks.addPendingBlock(block) // Commit the new state and block which writes to disk on the proxy app - _, _, err = m.executor.Commit(ctx, newState, block, responses) + appHash, _, err := m.executor.Commit(ctx, newState, block, responses) if err != nil { return err } + // Update app hash in state + newState.AppHash = appHash // SaveBlockResponses commits the DB tx err = m.store.SaveBlockResponses(ctx, blockHeight, responses) diff --git a/config/defaults.go b/config/defaults.go index c3d0042dac..44e5e5fe38 100644 --- a/config/defaults.go +++ b/config/defaults.go @@ -11,7 +11,7 @@ const ( DefaultListenAddress = "/ip4/0.0.0.0/tcp/7676" // Version is the current rollkit version // Please keep updated with each new release - Version = "0.12.0" + Version = "0.38.5" ) // DefaultNodeConfig keeps default values of NodeConfig diff --git a/go.mod b/go.mod index ab30800b24..8d48728414 100644 --- a/go.mod +++ b/go.mod @@ -145,6 +145,7 @@ require ( github.com/quic-go/quic-go v0.39.4 // indirect github.com/quic-go/webtransport-go v0.6.0 // indirect github.com/raulk/go-watchdog v1.3.0 // indirect + github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect github.com/rogpeppe/go-internal v1.11.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect diff --git a/node/full_client.go b/node/full_client.go index 304f8acc94..84d0cbf91b 100644 --- a/node/full_client.go +++ b/node/full_client.go @@ -496,7 +496,15 @@ func (c *FullClient) Commit(ctx context.Context, height *int64) (*ctypes.ResultC if err != nil { return nil, err } - commit := com.ToABCICommit(heightValue, b.Hash()) + + // we should have a single validator + if len(b.SignedHeader.Validators.Validators) == 0 { + return nil, errors.New("empty validator set found in block") + } + + val := b.SignedHeader.Validators.Validators[0].Address + commit := com.ToABCICommit(heightValue, b.Hash(), val, b.SignedHeader.Time()) + block, err := abciconv.ToABCIBlock(b) if err != nil { return nil, err @@ -761,7 +769,7 @@ func (c *FullClient) Status(ctx context.Context) (*ctypes.ResultStatus, error) { EarliestAppHash: cmbytes.HexBytes(initial.SignedHeader.AppHash), EarliestBlockHeight: int64(initial.Height()), EarliestBlockTime: initial.Time(), - CatchingUp: true, // the client is always syncing in the background to the latest height + CatchingUp: false, // hard-code this to "false" to pass Go IBC relayer's legacy encoding check }, ValidatorInfo: ctypes.ValidatorInfo{ Address: validator.Address, diff --git a/node/full_client_test.go b/node/full_client_test.go index c994329ba2..6b3e5eeca7 100644 --- a/node/full_client_test.go +++ b/node/full_client_test.go @@ -26,11 +26,15 @@ import ( "github.com/libp2p/go-libp2p/core/crypto" "github.com/libp2p/go-libp2p/core/peer" + "github.com/cometbft/cometbft/light" + "github.com/rollkit/rollkit/config" test "github.com/rollkit/rollkit/test/log" "github.com/rollkit/rollkit/test/mocks" "github.com/rollkit/rollkit/types" abciconv "github.com/rollkit/rollkit/types/abci" + + cmtmath "github.com/cometbft/cometbft/libs/math" ) var expectedInfo = &abci.ResponseInfo{ @@ -380,6 +384,76 @@ func TestGetCommit(t *testing.T) { }) } +func TestCometBFTLightClientCompability(t *testing.T) { + require := require.New(t) + assert := assert.New(t) + mockApp, rpc := getRPC(t) + mockApp.On("FinalizeBlock", mock.Anything, mock.Anything).Return(finalizeBlockResponse) + mockApp.On("Commit", mock.Anything, mock.Anything).Return(&abci.ResponseCommit{}, nil) + + // creat 3 consecutive signed blocks + block1, privKey := types.GetRandomBlockWithKey(1, 1, nil) + + block2 := types.GetRandomNextBlock(block1, privKey, []byte{}, 2) + + block3 := types.GetRandomNextBlock(block2, privKey, []byte{}, 3) + + blocks := []*types.Block{block1, block2, block3} + + // start the node for testing + err := rpc.node.Start() + require.NoError(err) + defer func() { + assert.NoError(rpc.node.Stop()) + }() + ctx := context.Background() + + // save the 3 blocks + for _, b := range blocks { + err = rpc.node.Store.SaveBlock(ctx, b, &b.SignedHeader.Commit) + rpc.node.Store.SetHeight(ctx, b.Height()) + require.NoError(err) + } + + // Check if the block header provided by rpc.Commit() can be verified using tendermint light client + t.Run("checking rollkit ABCI header verifiability", func(t *testing.T) { + var ( + trustingPeriod = 3 * time.Hour + trustLevel = cmtmath.Fraction{Numerator: 2, Denominator: 1} + maxClockDrift = 10 * time.Second + ) + + // trusted header to verify against + var trustedHeader cmtypes.SignedHeader + setTrustedHeader := false + // valset of single sequencer is constant + fixedValSet := block1.SignedHeader.Validators + + // for each block (except block 1), verify it's ABCI header with previous block's ABCI header as trusted header + for _, b := range blocks { + h := int64(b.Height()) + commit, err := rpc.Commit(context.Background(), &h) + require.NoError(err) + require.NotNil(commit) + require.NotNil(commit.SignedHeader) + assert.Equal(h, commit.Height) + + // if there's no trusted header to verify against, set trusted header and skip verifying + if !setTrustedHeader { + trustedHeader = commit.SignedHeader + setTrustedHeader = true + continue + } + + // verify the ABCI header + err = light.Verify(&trustedHeader, fixedValSet, &commit.SignedHeader, fixedValSet, trustingPeriod, b.Time(), maxClockDrift, trustLevel) + require.NoError(err, "failed to pass light.Verify()") + + trustedHeader = commit.SignedHeader + } + }) +} + func TestBlockSearch(t *testing.T) { require := require.New(t) assert := assert.New(t) diff --git a/proto/rollkit/rollkit.proto b/proto/rollkit/rollkit.proto index f6da6e526a..5934a5ffe8 100644 --- a/proto/rollkit/rollkit.proto +++ b/proto/rollkit/rollkit.proto @@ -1,80 +1,85 @@ syntax = "proto3"; package rollkit; -option go_package = "github.com/rollkit/rollkit/types/pb/rollkit"; + import "tendermint/types/validator.proto"; +option go_package = "github.com/rollkit/rollkit/types/pb/rollkit"; + // Version captures the consensus rules for processing a block in the blockchain, // including all blockchain data structures and the rules of the application's // state transition machine. // This is equivalent to the tmversion.Consensus type in Tendermint. message Version { - uint64 block = 1; - uint64 app = 2; + uint64 block = 1; + uint64 app = 2; } message Header { - // Block and App version - Version version = 1; + // Block and App version + Version version = 1; + + // Block height + uint64 height = 2; - // Block height - uint64 height = 2; + // Block creation time + uint64 time = 3; - // Block creation time - uint64 time = 3; + // Previous block info + bytes last_header_hash = 4; - // Previous block info - bytes last_header_hash = 4; + // Commit from aggregator(s) from the last block + bytes last_commit_hash = 5; - // Commit from aggregator(s) from the last block - bytes last_commit_hash = 5; + // Block.Data root aka Transactions + bytes data_hash = 6; - // Block.Data root aka Transactions - bytes data_hash = 6; + // Consensus params for current block + bytes consensus_hash = 7; - // Consensus params for current block - bytes consensus_hash = 7; + // State after applying txs from the current block + bytes app_hash = 8; - // State after applying txs from the current block - bytes app_hash = 8; + // Root hash of all results from the txs from the previous block. + // This is ABCI specific but smart-contract chains require some way of committing + // to transaction receipts/results. + bytes last_results_hash = 9; - // Root hash of all results from the txs from the previous block. - // This is ABCI specific but smart-contract chains require some way of committing - // to transaction receipts/results. - bytes last_results_hash = 9; + // Original proposer of the block + // Note that the address can be derived from the pubkey which can be derived + // from the signature when using secp256k. + // We keep this in case users choose another signature format where the + // pubkey can't be recovered by the signature (e.g. ed25519). + bytes proposer_address = 10; - // Original proposer of the block - // Note that the address can be derived from the pubkey which can be derived - // from the signature when using secp256k. - // We keep this in case users choose another signature format where the - // pubkey can't be recovered by the signature (e.g. ed25519). - bytes proposer_address = 10; + // validatorhash for compatibility with tendermint light client. + bytes validator_hash = 11; - // Chain ID the block belongs to - string chain_id = 11; + // Chain ID the block belongs to + string chain_id = 12; } message Commit { - repeated bytes signatures = 1; + repeated bytes signatures = 1; } message SignedHeader { - Header header = 1; - Commit commit = 2; - tendermint.types.ValidatorSet validators = 3; + Header header = 1; + Commit commit = 2; + tendermint.types.ValidatorSet validators = 3; } message Data { - repeated bytes txs = 1; - repeated bytes intermediate_state_roots = 2; + repeated bytes txs = 1; + repeated bytes intermediate_state_roots = 2; } message Block { - SignedHeader signed_header = 1; - Data data = 2; + SignedHeader signed_header = 1; + Data data = 2; } message TxWithISRs { - bytes pre_isr = 1; - bytes tx = 2; - bytes post_isr = 3; + bytes pre_isr = 1; + bytes tx = 2; + bytes post_isr = 3; } diff --git a/state/executor.go b/state/executor.go index fbf96a6fd6..2c0ae2d08b 100644 --- a/state/executor.go +++ b/state/executor.go @@ -28,6 +28,7 @@ var ErrAddingValidatorToBased = errors.New("cannot add validators to empty valid // BlockExecutor creates and applies blocks and maintains state. type BlockExecutor struct { proposerAddress []byte + valsetHash []byte chainID string proxyApp proxy.AppConnConsensus mempool mempool.Mempool @@ -40,9 +41,10 @@ type BlockExecutor struct { } // NewBlockExecutor creates new instance of BlockExecutor. -func NewBlockExecutor(proposerAddress []byte, chainID string, mempool mempool.Mempool, proxyApp proxy.AppConnConsensus, eventBus *cmtypes.EventBus, logger log.Logger, metrics *Metrics) *BlockExecutor { +func NewBlockExecutor(proposerAddress []byte, chainID string, mempool mempool.Mempool, proxyApp proxy.AppConnConsensus, eventBus *cmtypes.EventBus, logger log.Logger, metrics *Metrics, valsetHash []byte) *BlockExecutor { return &BlockExecutor{ proposerAddress: proposerAddress, + valsetHash: valsetHash, chainID: chainID, proxyApp: proxyApp, mempool: mempool, @@ -141,7 +143,7 @@ func (e *BlockExecutor) CreateBlock(height uint64, lastCommit *types.Commit, las Misbehavior: []abci.Misbehavior{}, Height: int64(block.Height()), Time: block.Time(), - NextValidatorsHash: nil, + NextValidatorsHash: e.valsetHash, ProposerAddress: e.proposerAddress, }, ) @@ -185,7 +187,7 @@ func (e *BlockExecutor) ProcessProposal( }, Misbehavior: []abci.Misbehavior{}, ProposerAddress: e.proposerAddress, - NextValidatorsHash: nil, + NextValidatorsHash: e.valsetHash, }) if err != nil { return false, err @@ -362,7 +364,7 @@ func (e *BlockExecutor) execute(ctx context.Context, state types.State, block *t startTime := time.Now().UnixNano() finalizeBlockResponse, err := e.proxyApp.FinalizeBlock(ctx, &abci.RequestFinalizeBlock{ Hash: block.Hash(), - NextValidatorsHash: nil, + NextValidatorsHash: e.valsetHash, ProposerAddress: abciHeader.ProposerAddress, Height: abciHeader.Height, Time: abciHeader.Time, diff --git a/state/executor_test.go b/state/executor_test.go index 404c963174..c360209efb 100644 --- a/state/executor_test.go +++ b/state/executor_test.go @@ -50,7 +50,7 @@ func doTestCreateBlock(t *testing.T) { fmt.Println("Made NID") mpool := mempool.NewCListMempool(cfg.DefaultMempoolConfig(), proxy.NewAppConnMempool(client, proxy.NopMetrics()), 0) fmt.Println("Made a NewTxMempool") - executor := NewBlockExecutor([]byte("test address"), "test", mpool, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), nil, logger, NopMetrics()) + executor := NewBlockExecutor([]byte("test address"), "test", mpool, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), nil, logger, NopMetrics(), types.GetRandomBytes(32)) fmt.Println("Made a New Block Executor") state := types.State{} @@ -157,7 +157,7 @@ func doTestApplyBlock(t *testing.T) { state.ConsensusParams.Block.MaxBytes = 100 state.ConsensusParams.Block.MaxGas = 100000 chainID := "test" - executor := NewBlockExecutor(vKey.PubKey().Address().Bytes(), chainID, mpool, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), eventBus, logger, NopMetrics()) + executor := NewBlockExecutor(vKey.PubKey().Address().Bytes(), chainID, mpool, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), eventBus, logger, NopMetrics(), types.GetRandomBytes(32)) err = mpool.CheckTx([]byte{1, 2, 3, 4}, func(r *abci.ResponseCheckTx) {}, mempool.TxInfo{}) require.NoError(err) @@ -171,8 +171,8 @@ func doTestApplyBlock(t *testing.T) { block.SignedHeader.DataHash = dataHash // Update the signature on the block to current from last - headerBytes, _ := block.SignedHeader.Header.MarshalBinary() - sig, _ := vKey.Sign(headerBytes) + voteBytes := block.SignedHeader.Header.MakeCometBFTVote() + sig, _ := vKey.Sign(voteBytes) block.SignedHeader.Commit = types.Commit{ Signatures: []types.Signature{sig}, } @@ -200,8 +200,8 @@ func doTestApplyBlock(t *testing.T) { assert.NoError(err) block.SignedHeader.DataHash = dataHash - headerBytes, _ = block.SignedHeader.Header.MarshalBinary() - sig, _ = vKey.Sign(headerBytes) + voteBytes = block.SignedHeader.Header.MakeCometBFTVote() + sig, _ = vKey.Sign(voteBytes) block.SignedHeader.Commit = types.Commit{ Signatures: []types.Signature{sig}, } @@ -260,7 +260,7 @@ func TestUpdateStateConsensusParams(t *testing.T) { mpool := mempool.NewCListMempool(cfg.DefaultMempoolConfig(), proxy.NewAppConnMempool(client, proxy.NopMetrics()), 0) eventBus := cmtypes.NewEventBus() require.NoError(t, eventBus.Start()) - executor := NewBlockExecutor([]byte("test address"), chainID, mpool, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), eventBus, logger, NopMetrics()) + executor := NewBlockExecutor([]byte("test address"), chainID, mpool, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), eventBus, logger, NopMetrics(), types.GetRandomBytes(32)) state := types.State{ ConsensusParams: cmproto.ConsensusParams{ diff --git a/types/abci/block.go b/types/abci/block.go index ce1b61de81..1efe59ea91 100644 --- a/types/abci/block.go +++ b/types/abci/block.go @@ -1,6 +1,8 @@ package abci import ( + "errors" + cmbytes "github.com/cometbft/cometbft/libs/bytes" cmproto "github.com/cometbft/cometbft/proto/tendermint/types" cmversion "github.com/cometbft/cometbft/proto/tendermint/version" @@ -26,14 +28,16 @@ func ToABCIHeaderPB(header *types.Header) (cmproto.Header, error) { Hash: nil, }, }, - LastCommitHash: header.LastHeaderHash[:], - DataHash: header.DataHash[:], - ConsensusHash: header.ConsensusHash[:], - AppHash: header.AppHash[:], - LastResultsHash: header.LastResultsHash[:], - EvidenceHash: types.EmptyEvidenceHash, - ProposerAddress: header.ProposerAddress, - ChainID: header.ChainID(), + LastCommitHash: header.LastHeaderHash[:], + DataHash: header.DataHash[:], + ConsensusHash: header.ConsensusHash[:], + AppHash: header.AppHash[:], + LastResultsHash: header.LastResultsHash[:], + EvidenceHash: new(cmtypes.EvidenceData).Hash(), + ProposerAddress: header.ProposerAddress, + ChainID: header.ChainID(), + ValidatorsHash: header.ValidatorHash, + NextValidatorsHash: header.ValidatorHash, }, nil } @@ -54,14 +58,16 @@ func ToABCIHeader(header *types.Header) (cmtypes.Header, error) { Hash: nil, }, }, - LastCommitHash: cmbytes.HexBytes(header.LastCommitHash), - DataHash: cmbytes.HexBytes(header.DataHash), - ConsensusHash: cmbytes.HexBytes(header.ConsensusHash), - AppHash: cmbytes.HexBytes(header.AppHash), - LastResultsHash: cmbytes.HexBytes(header.LastResultsHash), - EvidenceHash: types.EmptyEvidenceHash, - ProposerAddress: header.ProposerAddress, - ChainID: header.ChainID(), + LastCommitHash: cmbytes.HexBytes(header.LastCommitHash), + DataHash: cmbytes.HexBytes(header.DataHash), + ConsensusHash: cmbytes.HexBytes(header.ConsensusHash), + AppHash: cmbytes.HexBytes(header.AppHash), + LastResultsHash: cmbytes.HexBytes(header.LastResultsHash), + EvidenceHash: new(cmtypes.EvidenceData).Hash(), + ProposerAddress: header.ProposerAddress, + ChainID: header.ChainID(), + ValidatorsHash: cmbytes.HexBytes(header.ValidatorHash), + NextValidatorsHash: cmbytes.HexBytes(header.ValidatorHash), }, nil } @@ -72,7 +78,15 @@ func ToABCIBlock(block *types.Block) (*cmtypes.Block, error) { if err != nil { return nil, err } - abciCommit := block.SignedHeader.Commit.ToABCICommit(block.Height(), block.Hash()) + + // we have one validator + if len(block.SignedHeader.Validators.Validators) == 0 { + return nil, errors.New("empty validator set found in block") + } + + val := block.SignedHeader.Validators.Validators[0].Address + abciCommit := block.SignedHeader.Commit.ToABCICommit(block.Height(), block.Hash(), val, block.Time()) + // This assumes that we have only one signature if len(abciCommit.Signatures) == 1 { abciCommit.Signatures[0].ValidatorAddress = block.SignedHeader.ProposerAddress diff --git a/types/abci/block_test.go b/types/abci/block_test.go index 0796179db0..9b40e245a1 100644 --- a/types/abci/block_test.go +++ b/types/abci/block_test.go @@ -31,14 +31,16 @@ func TestToABCIHeaderPB(t *testing.T) { Hash: nil, }, }, - LastCommitHash: header.LastHeaderHash[:], - DataHash: header.DataHash[:], - ConsensusHash: header.ConsensusHash[:], - AppHash: header.AppHash[:], - LastResultsHash: header.LastResultsHash[:], - EvidenceHash: types.EmptyEvidenceHash, - ProposerAddress: header.ProposerAddress, - ChainID: header.ChainID(), + LastCommitHash: header.LastHeaderHash[:], + DataHash: header.DataHash[:], + ConsensusHash: header.ConsensusHash[:], + AppHash: header.AppHash[:], + LastResultsHash: header.LastResultsHash[:], + EvidenceHash: new(cmtypes.EvidenceData).Hash(), + ProposerAddress: header.ProposerAddress, + ChainID: header.ChainID(), + ValidatorsHash: header.ValidatorHash, + NextValidatorsHash: header.ValidatorHash, } actual, err := ToABCIHeaderPB(&header) @@ -65,14 +67,16 @@ func TestToABCIHeader(t *testing.T) { Hash: nil, }, }, - LastCommitHash: cmbytes.HexBytes(header.LastCommitHash), - DataHash: cmbytes.HexBytes(header.DataHash), - ConsensusHash: cmbytes.HexBytes(header.ConsensusHash), - AppHash: cmbytes.HexBytes(header.AppHash), - LastResultsHash: cmbytes.HexBytes(header.LastResultsHash), - EvidenceHash: types.EmptyEvidenceHash, - ProposerAddress: header.ProposerAddress, - ChainID: header.ChainID(), + LastCommitHash: cmbytes.HexBytes(header.LastCommitHash), + DataHash: cmbytes.HexBytes(header.DataHash), + ConsensusHash: cmbytes.HexBytes(header.ConsensusHash), + AppHash: cmbytes.HexBytes(header.AppHash), + LastResultsHash: cmbytes.HexBytes(header.LastResultsHash), + EvidenceHash: new(cmtypes.EvidenceData).Hash(), + ProposerAddress: header.ProposerAddress, + ChainID: header.ChainID(), + ValidatorsHash: cmbytes.HexBytes(header.ValidatorHash), + NextValidatorsHash: cmbytes.HexBytes(header.ValidatorHash), } actual, err := ToABCIHeader(&header) @@ -91,7 +95,11 @@ func TestToABCIBlock(t *testing.T) { t.Fatal(err) } - abciCommit := block.SignedHeader.Commit.ToABCICommit(block.Height(), block.Hash()) + // we only have one centralize sequencer + assert.Equal(t, 1, len(block.SignedHeader.Validators.Validators)) + val := block.SignedHeader.Validators.Validators[0].Address + + abciCommit := block.SignedHeader.Commit.ToABCICommit(block.Height(), block.Hash(), val, block.Time()) if len(abciCommit.Signatures) == 1 { abciCommit.Signatures[0].ValidatorAddress = block.SignedHeader.ProposerAddress diff --git a/types/block.go b/types/block.go index a82b9148ea..8c9c0235d6 100644 --- a/types/block.go +++ b/types/block.go @@ -59,7 +59,7 @@ type IntermediateStateRoots struct { // ToABCICommit converts Rollkit commit into commit format defined by ABCI. // This function only converts fields that are available in Rollkit commit. // Other fields (especially ValidatorAddress and Timestamp of Signature) has to be filled by caller. -func (c *Commit) ToABCICommit(height uint64, hash Hash) *cmtypes.Commit { +func (c *Commit) ToABCICommit(height uint64, hash Hash, val cmtypes.Address, time time.Time) *cmtypes.Commit { tmCommit := cmtypes.Commit{ Height: int64(height), Round: 0, @@ -71,8 +71,10 @@ func (c *Commit) ToABCICommit(height uint64, hash Hash) *cmtypes.Commit { } for i, sig := range c.Signatures { commitSig := cmtypes.CommitSig{ - BlockIDFlag: cmtypes.BlockIDFlagCommit, - Signature: sig, + BlockIDFlag: cmtypes.BlockIDFlagCommit, + Signature: sig, + ValidatorAddress: val, + Timestamp: time, } tmCommit.Signatures[i] = commitSig } @@ -82,7 +84,8 @@ func (c *Commit) ToABCICommit(height uint64, hash Hash) *cmtypes.Commit { // GetCommitHash returns hash of the commit. func (c *Commit) GetCommitHash(header *Header, proposerAddress []byte) []byte { - lastABCICommit := c.ToABCICommit(header.Height(), header.Hash()) + + lastABCICommit := c.ToABCICommit(header.Height(), header.Hash(), proposerAddress, header.Time()) // Rollkit does not support a multi signature scheme so there can only be one signature if len(c.Signatures) == 1 { lastABCICommit.Signatures[0].ValidatorAddress = proposerAddress diff --git a/types/hashing.go b/types/hashing.go index 8b8f3586f8..b8226f62cd 100644 --- a/types/hashing.go +++ b/types/hashing.go @@ -36,8 +36,8 @@ func (h *Header) Hash() Hash { EvidenceHash: EmptyEvidenceHash, ProposerAddress: h.ProposerAddress, // Backward compatibility - ValidatorsHash: h.ProposerAddress, - NextValidatorsHash: h.ProposerAddress, + ValidatorsHash: cmbytes.HexBytes(h.ValidatorHash), + NextValidatorsHash: cmbytes.HexBytes(h.ValidatorHash), ChainID: h.ChainID(), } return Hash(abciHeader.Hash()) diff --git a/types/header.go b/types/header.go index f00aa2413f..616a9e33c0 100644 --- a/types/header.go +++ b/types/header.go @@ -7,7 +7,11 @@ import ( "fmt" "time" + cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" + cmtypes "github.com/cometbft/cometbft/types" + "github.com/celestiaorg/go-header" + cmbytes "github.com/cometbft/cometbft/libs/bytes" ) // Hash is a 32-byte array which is used to represent a hash result. @@ -51,6 +55,9 @@ type Header struct { // to transaction receipts/results. LastResultsHash Hash + // compatibility with tendermint light client + ValidatorHash Hash + // Note that the address can be derived from the pubkey which can be derived // from the signature when using secp256k. // We keep this in case users choose another signature format where the @@ -116,6 +123,29 @@ func (h *Header) ValidateBasic() error { return nil } +// MakeCometBFTVote make a cometBFT consensus vote for the sequencer to commit +// we have the sequencer signs cometBFT consensus vote for compatibility with cometBFT client +func (h *Header) MakeCometBFTVote() []byte { + vote := cmtproto.Vote{ + Type: cmtproto.PrecommitType, + Height: int64(h.Height()), + Round: 0, + // Header hash = block hash in rollkit + BlockID: cmtproto.BlockID{ + Hash: cmbytes.HexBytes(h.Hash()), + PartSetHeader: cmtproto.PartSetHeader{}, + }, + Timestamp: h.Time(), + // proposerAddress = sequencer = validator + ValidatorAddress: h.ProposerAddress, + ValidatorIndex: 0, + } + chainID := h.ChainID() + consensusVoteBytes := cmtypes.VoteSignBytes(chainID, &vote) + + return consensusVoteBytes +} + var _ header.Header[*Header] = &Header{} var _ encoding.BinaryMarshaler = &Header{} var _ encoding.BinaryUnmarshaler = &Header{} diff --git a/types/pb/rollkit/rollkit.pb.go b/types/pb/rollkit/rollkit.pb.go index dec464fdf6..f11baa2d87 100644 --- a/types/pb/rollkit/rollkit.pb.go +++ b/types/pb/rollkit/rollkit.pb.go @@ -106,8 +106,10 @@ 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"` + // validatorhash for compatibility with tendermint light client. + ValidatorHash []byte `protobuf:"bytes,11,opt,name=validator_hash,json=validatorHash,proto3" json:"validator_hash,omitempty"` // Chain ID the block belongs to - ChainId string `protobuf:"bytes,11,opt,name=chain_id,json=chainId,proto3" json:"chain_id,omitempty"` + ChainId string `protobuf:"bytes,12,opt,name=chain_id,json=chainId,proto3" json:"chain_id,omitempty"` } func (m *Header) Reset() { *m = Header{} } @@ -213,6 +215,13 @@ func (m *Header) GetProposerAddress() []byte { return nil } +func (m *Header) GetValidatorHash() []byte { + if m != nil { + return m.ValidatorHash + } + return nil +} + func (m *Header) GetChainId() string { if m != nil { return m.ChainId @@ -501,45 +510,46 @@ func init() { func init() { proto.RegisterFile("rollkit/rollkit.proto", fileDescriptor_ed489fb7f4d78b3f) } var fileDescriptor_ed489fb7f4d78b3f = []byte{ - // 607 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x5c, 0x93, 0xc1, 0x6e, 0x13, 0x3d, - 0x10, 0xc7, 0xbb, 0x49, 0x9a, 0xb4, 0xd3, 0xb4, 0xcd, 0x67, 0x7d, 0x2d, 0x0b, 0x48, 0xab, 0xb0, - 0x12, 0x22, 0x14, 0x29, 0x11, 0xe5, 0x82, 0x38, 0x20, 0x51, 0x40, 0x6a, 0x6e, 0xc8, 0x41, 0x45, - 0xe2, 0xb2, 0x72, 0xb2, 0x26, 0x6b, 0x35, 0x59, 0x5b, 0xf6, 0xa4, 0x0a, 0x0f, 0xc0, 0x9d, 0x47, - 0xe0, 0x71, 0x38, 0xf6, 0xc8, 0x11, 0xb5, 0x2f, 0x82, 0x3c, 0xde, 0xa4, 0x81, 0x4b, 0xe2, 0xf9, - 0xff, 0x7f, 0xf6, 0xec, 0x78, 0xc6, 0x70, 0x64, 0xf5, 0x6c, 0x76, 0xa9, 0x70, 0x50, 0xfd, 0xf7, - 0x8d, 0xd5, 0xa8, 0x59, 0xab, 0x0a, 0x1f, 0x74, 0x51, 0x96, 0xb9, 0xb4, 0x73, 0x55, 0xe2, 0x00, - 0xbf, 0x1a, 0xe9, 0x06, 0x57, 0x62, 0xa6, 0x72, 0x81, 0xda, 0x06, 0x34, 0x7d, 0x0e, 0xad, 0x0b, - 0x69, 0x9d, 0xd2, 0x25, 0xfb, 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64, - 0xa5, 0x1f, 0x00, 0x3e, 0x2e, 0x3f, 0x29, 0x2c, 0x86, 0x23, 0xee, 0xd8, 0x3d, 0x68, 0x19, 0x2b, - 0x33, 0xe5, 0x42, 0x9a, 0x36, 0x6f, 0x1a, 0x2b, 0x87, 0xce, 0xb2, 0x03, 0xa8, 0xe1, 0x92, 0xce, - 0x69, 0xf3, 0x1a, 0x2e, 0x7d, 0x6b, 0x8d, 0x76, 0x48, 0x64, 0x3d, 0x0c, 0x93, 0x8f, 0x87, 0xce, - 0x9e, 0xbd, 0xff, 0x79, 0x93, 0x44, 0xd7, 0x37, 0x49, 0xf4, 0xfb, 0x26, 0x89, 0xbe, 0xdf, 0x26, - 0x5b, 0xd7, 0xb7, 0xc9, 0xd6, 0xaf, 0xdb, 0x64, 0xeb, 0xf3, 0xb3, 0xa9, 0xc2, 0x62, 0x31, 0xee, - 0x4f, 0xf4, 0x7c, 0xf0, 0xcf, 0xe3, 0xaf, 0x5e, 0xb8, 0x19, 0xaf, 0x84, 0x71, 0x93, 0xde, 0xf8, - 0x8b, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x4f, 0x4f, 0x2e, 0xb0, 0x27, 0x04, 0x00, 0x00, + // 619 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x5c, 0x94, 0xc1, 0x6e, 0x13, 0x3d, + 0x10, 0xc7, 0xbb, 0x49, 0x9a, 0x6d, 0xa7, 0xdb, 0x36, 0x9f, 0xf5, 0xb5, 0x2c, 0x20, 0xad, 0xc2, + 0x4a, 0x88, 0x50, 0xa4, 0x44, 0x94, 0x0b, 0xe2, 0x80, 0x44, 0x01, 0xa9, 0xb9, 0x21, 0x07, 0x15, + 0x89, 0xcb, 0xca, 0xc9, 0x9a, 0xac, 0xd5, 0x64, 0x6d, 0xd9, 0x4e, 0x15, 0xde, 0x82, 0x47, 0xe0, + 0x01, 0x78, 0x10, 0x8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5, 0x4e, 0x04, 0x00, 0x00, } func (m *Version) Marshal() (dAtA []byte, err error) { @@ -600,6 +610,13 @@ 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 + } + if len(m.ValidatorHash) > 0 { + i -= len(m.ValidatorHash) + copy(dAtA[i:], m.ValidatorHash) + i = encodeVarintRollkit(dAtA, i, uint64(len(m.ValidatorHash))) + i-- dAtA[i] = 0x5a } if len(m.ProposerAddress) > 0 { @@ -969,6 +986,10 @@ func (m *Header) Size() (n int) { if l > 0 { n += 1 + l + sovRollkit(uint64(l)) } + l = len(m.ValidatorHash) + if l > 0 { + n += 1 + l + sovRollkit(uint64(l)) + } l = len(m.ChainId) if l > 0 { n += 1 + l + sovRollkit(uint64(l)) @@ -1507,6 +1528,40 @@ func (m *Header) Unmarshal(dAtA []byte) error { } iNdEx = postIndex case 11: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ValidatorHash", 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.ValidatorHash = append(m.ValidatorHash[:0], dAtA[iNdEx:postIndex]...) + if m.ValidatorHash == nil { + m.ValidatorHash = []byte{} + } + iNdEx = postIndex + case 12: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field ChainId", wireType) } diff --git a/types/serialization.go b/types/serialization.go index aec761282a..2099012279 100644 --- a/types/serialization.go +++ b/types/serialization.go @@ -150,6 +150,7 @@ func (h *Header) ToProto() *pb.Header { LastResultsHash: h.LastResultsHash[:], ProposerAddress: h.ProposerAddress[:], ChainId: h.BaseHeader.ChainID, + ValidatorHash: h.ValidatorHash, } } @@ -166,6 +167,7 @@ func (h *Header) FromProto(other *pb.Header) error { h.ConsensusHash = other.ConsensusHash h.AppHash = other.AppHash h.LastResultsHash = other.LastResultsHash + h.ValidatorHash = other.ValidatorHash if len(other.ProposerAddress) > 0 { h.ProposerAddress = make([]byte, len(other.ProposerAddress)) copy(h.ProposerAddress, other.ProposerAddress) diff --git a/types/signed_header.go b/types/signed_header.go index 2c11f0259d..489d187f6b 100644 --- a/types/signed_header.go +++ b/types/signed_header.go @@ -155,11 +155,8 @@ func (sh *SignedHeader) ValidateBasic() error { signature := sh.Commit.Signatures[0] - msg, err := sh.Header.MarshalBinary() - if err != nil { - return fmt.Errorf("signature verification failed, unable to marshal header: %v", err) - } - if !sh.Validators.Validators[0].PubKey.VerifySignature(msg, signature) { + vote := sh.Header.MakeCometBFTVote() + if !sh.Validators.Validators[0].PubKey.VerifySignature(vote, signature) { return ErrSignatureVerificationFailed } return nil diff --git a/types/signed_header_test.go b/types/signed_header_test.go index 5c521fc177..e979f76680 100644 --- a/types/signed_header_test.go +++ b/types/signed_header_test.go @@ -152,7 +152,7 @@ func testValidateBasic(t *testing.T, untrustedAdj *SignedHeader, privKey ed25519 { prepare: func() (*SignedHeader, bool) { untrusted := *untrustedAdj - untrusted.Version.App = untrusted.Version.App + 1 + untrusted.BaseHeader.Height = untrusted.BaseHeader.Height + 1 return &untrusted, false // Signature verification should fail }, err: ErrSignatureVerificationFailed, diff --git a/types/utils.go b/types/utils.go index bf84f7b657..15a37891f1 100644 --- a/types/utils.go +++ b/types/utils.go @@ -32,24 +32,24 @@ func GetRandomValidatorSet() *cmtypes.ValidatorSet { func GetRandomValidatorSetWithPrivKey() (*cmtypes.ValidatorSet, ed25519.PrivKey) { privKey := ed25519.GenPrivKey() pubKey := privKey.PubKey() - return &cmtypes.ValidatorSet{ - Proposer: &cmtypes.Validator{PubKey: pubKey, Address: pubKey.Address()}, - Validators: []*cmtypes.Validator{ - {PubKey: pubKey, Address: pubKey.Address()}, + valset := cmtypes.NewValidatorSet( + []*cmtypes.Validator{ + {PubKey: pubKey, Address: pubKey.Address(), VotingPower: 1}, }, - }, privKey + ) + return valset, privKey } // GetValidatorSet returns a validator set with a single validator // with the given key func GetValidatorSet(privKey ed25519.PrivKey) *cmtypes.ValidatorSet { pubKey := privKey.PubKey() - return &cmtypes.ValidatorSet{ - Proposer: &cmtypes.Validator{PubKey: pubKey, Address: pubKey.Address()}, - Validators: []*cmtypes.Validator{ - {PubKey: pubKey, Address: pubKey.Address()}, + valset := cmtypes.NewValidatorSet( + []*cmtypes.Validator{ + {PubKey: pubKey, Address: pubKey.Address(), VotingPower: 1}, }, - } + ) + return valset } // GetRandomBlock returns a block with random data @@ -124,6 +124,7 @@ func GetRandomHeader() Header { AppHash: GetRandomBytes(32), LastResultsHash: GetRandomBytes(32), ProposerAddress: GetRandomBytes(32), + ValidatorHash: GetRandomBytes(32), } } @@ -135,6 +136,7 @@ func GetRandomNextHeader(header Header) Header { nextHeader.BaseHeader.Time = uint64(time.Now().Add(1 * time.Second).UnixNano()) nextHeader.LastHeaderHash = header.Hash() nextHeader.ProposerAddress = header.ProposerAddress + nextHeader.ValidatorHash = header.ValidatorHash return nextHeader } @@ -159,6 +161,9 @@ func GetRandomSignedHeaderWith(height uint64, dataHash header.Hash, privKey ed25 signedHeader.Header.BaseHeader.Height = height signedHeader.Header.DataHash = dataHash signedHeader.Header.ProposerAddress = valSet.Proposer.Address + signedHeader.Header.ValidatorHash = valSet.Hash() + signedHeader.Header.BaseHeader.Time = uint64(time.Now().UnixNano()) + height*10 + commit, err := GetCommit(signedHeader.Header, privKey) if err != nil { return nil, nil, err @@ -221,6 +226,7 @@ func GetFirstSignedHeader(privkey ed25519.PrivKey, valSet *cmtypes.ValidatorSet) ConsensusHash: GetRandomBytes(32), AppHash: make([]byte, 32), LastResultsHash: GetRandomBytes(32), + ValidatorHash: valSet.Hash(), ProposerAddress: valSet.Proposer.Address.Bytes(), } signedHeader := SignedHeader{ @@ -294,11 +300,8 @@ func GetRandomBytes(n int) []byte { // GetCommit returns a commit with a signature from the given private key over the given header func GetCommit(header Header, privKey ed25519.PrivKey) (*Commit, error) { - headerBytes, err := header.MarshalBinary() - if err != nil { - return nil, err - } - sign, err := privKey.Sign(headerBytes) + consensusVote := header.MakeCometBFTVote() + sign, err := privKey.Sign(consensusVote) if err != nil { return nil, err }