Skip to content
41 changes: 25 additions & 16 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"sync/atomic"
"time"

"github.com/cometbft/cometbft/crypto/ed25519"

goheaderstore "github.com/celestiaorg/go-header/store"
abci "github.com/cometbft/cometbft/abci/types"
cmcrypto "github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/crypto/merkle"
cmstate "github.com/cometbft/cometbft/proto/tendermint/state"
"github.com/cometbft/cometbft/proxy"
Expand Down Expand Up @@ -125,11 +126,6 @@ func NewManager(
s.DAHeight = conf.DAStartHeight
}

proposerAddress, err := getAddress(proposerKey)
if err != nil {
return nil, err
}

if conf.DABlockTime == 0 {
logger.Info("Using default DA block time", "DABlockTime", defaultDABlockTime)
conf.DABlockTime = defaultDABlockTime
Expand All @@ -140,7 +136,11 @@ func NewManager(
conf.BlockTime = defaultBlockTime
}

exec := state.NewBlockExecutor(proposerAddress, conf.NamespaceID, genesis.ChainID, mempool, proxyApp, eventBus, logger)
proposerPubkeyBytes, err := proposerKey.GetPublic().Raw()
if err != nil {
return nil, err
}
exec := state.NewBlockExecutor(proposerPubkeyBytes, conf.NamespaceID, genesis.ChainID, mempool, proxyApp, eventBus, logger)
if s.LastBlockHeight+1 == uint64(genesis.InitialHeight) {
res, err := exec.InitChain(genesis)
if err != nil {
Expand Down Expand Up @@ -188,14 +188,6 @@ func NewManager(
return agg, nil
}

func getAddress(key crypto.PrivKey) ([]byte, error) {
rawKey, err := key.GetPublic().Raw()
if err != nil {
return nil, err
}
return cmcrypto.AddressHash(rawKey), nil
}

// SetDALC is used to set DataAvailabilityLayerClient used by Manager.
func (m *Manager) SetDALC(dalc da.DataAvailabilityLayerClient) {
m.dalc = dalc
Expand Down Expand Up @@ -362,6 +354,10 @@ func (m *Manager) trySyncNextBlock(ctx context.Context, daHeight uint64) error {
var commit *types.Commit
currentHeight := m.store.Height() // TODO(tzdybal): maybe store a copy in memory

lastBlock, ok := m.blockCache.getBlock(currentHeight)
if !ok {
return nil
}
b, ok := m.blockCache.getBlock(currentHeight + 1)
if !ok {
return nil
Expand All @@ -373,6 +369,10 @@ func (m *Manager) trySyncNextBlock(ctx context.Context, daHeight uint64) error {
}

if b != nil && commit != nil {
err := lastBlock.SignedHeader.Verify(&b.SignedHeader)
if err != nil {
return fmt.Errorf("failed to validate block: %w", err)
}
bHeight := uint64(b.Height())
m.logger.Info("Syncing block", "height", bHeight)
// Validate the received block before applying
Expand Down Expand Up @@ -501,6 +501,14 @@ func (m *Manager) processNextDABlock(ctx context.Context) error {
}
m.logger.Debug("retrieved potential blocks", "n", len(blockResp.Blocks), "daHeight", daHeight)
for _, block := range blockResp.Blocks {
// Don't waste resources on blocks with unexpected signers
if !bytes.Equal(block.SignedHeader.ProposerPubkey, m.genesis.Validators[0].PubKey.Bytes()) {
continue
}
// Don't waste resources on invalid blocks
if block.ValidateBasic() != nil {
continue
}
blockHash := block.Hash().String()
m.blockCache.setDAIncluded(blockHash)
m.logger.Info("block marked as DA included", "blockHeight", block.Height(), "blockHash", blockHash)
Expand Down Expand Up @@ -704,7 +712,8 @@ func (m *Manager) publishBlock(ctx context.Context) error {
// Publish block to channel so that block exchange service can broadcast
m.BlockCh <- block

m.logger.Debug("successfully proposed block", "proposer", hex.EncodeToString(block.SignedHeader.ProposerAddress), "height", blockHeight)
var pubkey ed25519.PubKey = block.SignedHeader.ProposerPubkey
m.logger.Debug("successfully proposed block", "proposer", hex.EncodeToString(pubkey.Address().Bytes()), "height", blockHeight)

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions node/full_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ const (

var mockTxProcessingTime = 10 * time.Millisecond

func getRandomBlockWithProposer(height uint64, nTxs int, proposerAddr []byte) *types.Block {
func getRandomBlockWithProposer(height uint64, nTxs int, proposerPubkey []byte) *types.Block {
block := types.GetRandomBlock(height, nTxs)
block.SignedHeader.ProposerAddress = proposerAddr
block.SignedHeader.ProposerPubkey = proposerPubkey
return block
}

Expand Down
15 changes: 13 additions & 2 deletions node/full_node_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/assert"

abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/libs/log"
"github.com/cometbft/cometbft/proxy"
cmtypes "github.com/cometbft/cometbft/types"
Expand Down Expand Up @@ -639,7 +640,17 @@ func createNode(ctx context.Context, n int, aggregator bool, isLight bool, keys
ctx = context.Background()
}

genesisValidators, signingKey := types.GetGenesisValidatorSetWithSigner()
pubkeyBytes, err := keys[0].GetPublic().Raw()
require.NoError(err)
var pubkey ed25519.PubKey = pubkeyBytes
genesisValidators := []cmtypes.GenesisValidator{
{
Address: pubkey.Address(),
PubKey: pubkey,
Power: int64(1),
Name: "sequencer",
},
}
genesis := &cmtypes.GenesisDoc{ChainID: "test", Validators: genesisValidators}
// TODO: need to investigate why this needs to be done for light nodes
genesis.InitialHeight = 1
Expand All @@ -653,7 +664,7 @@ func createNode(ctx context.Context, n int, aggregator bool, isLight bool, keys
Light: isLight,
},
keys[n],
signingKey,
keys[n],
proxy.NewLocalClientCreator(app),
genesis,
test.NewFileLoggerCustom(t, test.TempLogFileName(t, fmt.Sprintf("node%v", n))).With("node", n))
Expand Down
3 changes: 1 addition & 2 deletions proto/rollkit/rollkit.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ message Header {
// 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;
bytes proposer_pubkey = 10;

// Chain ID the block belongs to
string chain_id = 11;
Expand All @@ -60,7 +60,6 @@ message Commit {
message SignedHeader {
Header header = 1;
Commit commit = 2;
tendermint.types.ValidatorSet validators = 3;
}

message Data {
Expand Down
32 changes: 17 additions & 15 deletions state/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/crypto/ed25519"
cryptoenc "github.com/cometbft/cometbft/crypto/encoding"
cmbytes "github.com/cometbft/cometbft/libs/bytes"
cmstate "github.com/cometbft/cometbft/proto/tendermint/state"
Expand All @@ -30,11 +31,11 @@ var ErrAddingValidatorToBased = errors.New("cannot add validators to empty valid

// BlockExecutor creates and applies blocks and maintains state.
type BlockExecutor struct {
proposerAddress []byte
namespaceID types.NamespaceID
chainID string
proxyApp proxy.AppConnConsensus
mempool mempool.Mempool
proposerPubkey []byte
namespaceID types.NamespaceID
chainID string
proxyApp proxy.AppConnConsensus
mempool mempool.Mempool

eventBus *cmtypes.EventBus

Expand All @@ -43,15 +44,15 @@ type BlockExecutor struct {

// NewBlockExecutor creates new instance of BlockExecutor.
// Proposer address and namespace ID will be used in all newly created blocks.
func NewBlockExecutor(proposerAddress []byte, namespaceID [8]byte, chainID string, mempool mempool.Mempool, proxyApp proxy.AppConnConsensus, eventBus *cmtypes.EventBus, logger log.Logger) *BlockExecutor {
func NewBlockExecutor(proposerPubkey []byte, namespaceID [8]byte, chainID string, mempool mempool.Mempool, proxyApp proxy.AppConnConsensus, eventBus *cmtypes.EventBus, logger log.Logger) *BlockExecutor {
return &BlockExecutor{
proposerAddress: proposerAddress,
namespaceID: namespaceID,
chainID: chainID,
proxyApp: proxyApp,
mempool: mempool,
eventBus: eventBus,
logger: logger,
proposerPubkey: proposerPubkey,
namespaceID: namespaceID,
chainID: chainID,
proxyApp: proxyApp,
mempool: mempool,
eventBus: eventBus,
logger: logger,
}
}

Expand Down Expand Up @@ -115,7 +116,7 @@ func (e *BlockExecutor) CreateBlock(height uint64, lastCommit *types.Commit, las
ConsensusHash: make(types.Hash, 32),
AppHash: state.AppHash,
LastResultsHash: state.LastResultsHash,
ProposerAddress: e.proposerAddress,
ProposerPubkey: e.proposerPubkey,
},
Commit: *lastCommit,
},
Expand All @@ -126,7 +127,8 @@ func (e *BlockExecutor) CreateBlock(height uint64, lastCommit *types.Commit, las
// Evidence: types.EvidenceData{Evidence: nil},
},
}
block.SignedHeader.LastCommitHash = lastCommit.GetCommitHash(&block.SignedHeader.Header, e.proposerAddress)
var pk ed25519.PubKey = e.proposerPubkey
block.SignedHeader.LastCommitHash = lastCommit.GetCommitHash(&block.SignedHeader.Header, pk.Address().Bytes())
block.SignedHeader.LastHeaderHash = lastHeaderHash

return block
Expand Down
17 changes: 4 additions & 13 deletions state/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func doTestCreateBlock(t *testing.T) {
fmt.Println("Made NID")
mpool := mempoolv1.NewTxMempool(logger, cfg.DefaultMempoolConfig(), proxy.NewAppConnMempool(client, proxy.NopMetrics()), 0)
fmt.Println("Made a NewTxMempool")
executor := NewBlockExecutor([]byte("test address"), nsID, "test", mpool, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), nil, logger)
proposerKey := ed25519.GenPrivKey().PubKey()
executor := NewBlockExecutor(proposerKey.Bytes(), nsID, "test", mpool, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), nil, logger)
fmt.Println("Made a New Block Executor")

state := types.State{}
Expand Down Expand Up @@ -118,7 +119,8 @@ func doTestApplyBlock(t *testing.T) {
mpool := mempoolv1.NewTxMempool(logger, cfg.DefaultMempoolConfig(), proxy.NewAppConnMempool(client, proxy.NopMetrics()), 0)
eventBus := cmtypes.NewEventBus()
require.NoError(eventBus.Start())
executor := NewBlockExecutor([]byte("test address"), nsID, chainID, mpool, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), eventBus, logger)
vKey := ed25519.GenPrivKey()
executor := NewBlockExecutor(vKey.PubKey().Bytes(), nsID, chainID, mpool, proxy.NewAppConnConsensus(client, proxy.NopMetrics()), eventBus, logger)

txQuery, err := query.New("tm.event='Tx'")
require.NoError(err)
Expand All @@ -132,15 +134,6 @@ func doTestApplyBlock(t *testing.T) {
require.NoError(err)
require.NotNil(headerSub)

vKey := ed25519.GenPrivKey()
validators := []*cmtypes.Validator{
{
Address: vKey.PubKey().Address(),
PubKey: vKey.PubKey(),
VotingPower: int64(100),
ProposerPriority: int64(1),
},
}
state := types.State{}
state.InitialHeight = 1
state.LastBlockHeight = 0
Expand All @@ -164,7 +157,6 @@ func doTestApplyBlock(t *testing.T) {
block.SignedHeader.Commit = types.Commit{
Signatures: []types.Signature{sig},
}
block.SignedHeader.Validators = cmtypes.NewValidatorSet(validators)

newState, resp, err := executor.ApplyBlock(context.Background(), state, block)
require.NoError(err)
Expand Down Expand Up @@ -192,7 +184,6 @@ func doTestApplyBlock(t *testing.T) {
block.SignedHeader.Commit = types.Commit{
Signatures: []types.Signature{sig},
}
block.SignedHeader.Validators = cmtypes.NewValidatorSet(validators)

newState, resp, err = executor.ApplyBlock(context.Background(), newState, block)
require.NoError(err)
Expand Down
1 change: 0 additions & 1 deletion store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func TestStoreLoad(t *testing.T) {
for _, block := range c.blocks {
commit := &types.Commit{}
block.SignedHeader.Commit = *lastCommit
block.SignedHeader.Validators = types.GetRandomValidatorSet()
err := bstore.SaveBlock(block, commit)
require.NoError(err)
lastCommit = commit
Expand Down
10 changes: 7 additions & 3 deletions types/abci/block.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package abci

import (
"github.com/cometbft/cometbft/crypto/ed25519"
cmbytes "github.com/cometbft/cometbft/libs/bytes"
cmproto "github.com/cometbft/cometbft/proto/tendermint/types"
cmversion "github.com/cometbft/cometbft/proto/tendermint/version"
Expand All @@ -12,6 +13,7 @@ import (
// ToABCIHeaderPB converts Rollkit header to Header format defined in ABCI.
// Caller should fill all the fields that are not available in Rollkit header (like ChainID).
func ToABCIHeaderPB(header *types.Header) (cmproto.Header, error) {
var pubkey ed25519.PubKey = header.ProposerPubkey
return cmproto.Header{
Version: cmversion.Consensus{
Block: header.Version.Block,
Expand All @@ -32,14 +34,15 @@ func ToABCIHeaderPB(header *types.Header) (cmproto.Header, error) {
AppHash: header.AppHash[:],
LastResultsHash: header.LastResultsHash[:],
EvidenceHash: new(cmtypes.EvidenceData).Hash(),
ProposerAddress: header.ProposerAddress,
ProposerAddress: pubkey.Address().Bytes(),
ChainID: header.ChainID(),
}, nil
}

// ToABCIHeader converts Rollkit header to Header format defined in ABCI.
// Caller should fill all the fields that are not available in Rollkit header (like ChainID).
func ToABCIHeader(header *types.Header) (cmtypes.Header, error) {
var pubkey ed25519.PubKey = header.ProposerPubkey
return cmtypes.Header{
Version: cmversion.Consensus{
Block: header.Version.Block,
Expand All @@ -60,22 +63,23 @@ func ToABCIHeader(header *types.Header) (cmtypes.Header, error) {
AppHash: cmbytes.HexBytes(header.AppHash),
LastResultsHash: cmbytes.HexBytes(header.LastResultsHash),
EvidenceHash: new(cmtypes.EvidenceData).Hash(),
ProposerAddress: header.ProposerAddress,
ProposerAddress: pubkey.Address().Bytes(),
ChainID: header.ChainID(),
}, nil
}

// ToABCIBlock converts Rolkit block into block format defined by ABCI.
// Returned block should pass `ValidateBasic`.
func ToABCIBlock(block *types.Block) (*cmtypes.Block, error) {
var pubkey ed25519.PubKey = block.SignedHeader.Header.ProposerPubkey
abciHeader, err := ToABCIHeader(&block.SignedHeader.Header)
if err != nil {
return nil, err
}
abciCommit := block.SignedHeader.Commit.ToABCICommit(block.Height(), block.Hash())
// This assumes that we have only one signature
if len(abciCommit.Signatures) == 1 {
abciCommit.Signatures[0].ValidatorAddress = block.SignedHeader.ProposerAddress
abciCommit.Signatures[0].ValidatorAddress = pubkey.Address()
}
abciBlock := cmtypes.Block{
Header: abciHeader,
Expand Down
Loading