Skip to content

Commit 5d72b03

Browse files
refactor: Node interface, FullNode, LightNode, FullClient, LightClient (#1932)
Cleaning up rollkit, in preparation for execution api (#1896 #1897 #1898 #1899 #1900)
2 parents 584b187 + ca9bcfe commit 5d72b03

45 files changed

Lines changed: 1660 additions & 8511 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

block/manager.go

Lines changed: 32 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ import (
77
"encoding/hex"
88
"errors"
99
"fmt"
10-
cmbytes "github.com/cometbft/cometbft/libs/bytes"
11-
cmstate "github.com/cometbft/cometbft/proto/tendermint/state"
1210
"sync"
1311
"sync/atomic"
1412
"time"
1513

14+
cmbytes "github.com/cometbft/cometbft/libs/bytes"
15+
cmstate "github.com/cometbft/cometbft/proto/tendermint/state"
16+
1617
secp256k1 "github.com/btcsuite/btcd/btcec/v2"
1718
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
1819
abci "github.com/cometbft/cometbft/abci/types"
1920
cmcrypto "github.com/cometbft/cometbft/crypto"
2021
cmproto "github.com/cometbft/cometbft/proto/tendermint/types"
21-
"github.com/cometbft/cometbft/proxy"
2222
cmtypes "github.com/cometbft/cometbft/types"
2323
ds "github.com/ipfs/go-datastore"
2424
"github.com/libp2p/go-libp2p/core/crypto"
@@ -33,7 +33,6 @@ import (
3333
"github.com/rollkit/go-sequencing/proxy/grpc"
3434
"github.com/rollkit/rollkit/config"
3535
"github.com/rollkit/rollkit/da"
36-
"github.com/rollkit/rollkit/mempool"
3736
"github.com/rollkit/rollkit/state"
3837
"github.com/rollkit/rollkit/store"
3938
"github.com/rollkit/rollkit/third_party/log"
@@ -162,6 +161,7 @@ type Manager struct {
162161
bq *BatchQueue
163162
}
164163

164+
// RollkitGenesis is the genesis state of the rollup
165165
type RollkitGenesis struct {
166166
GenesisTime time.Time
167167
InitialHeight uint64
@@ -170,15 +170,18 @@ type RollkitGenesis struct {
170170
}
171171

172172
// getInitialState tries to load lastState from Store, and if it's not available it reads GenesisDoc.
173-
func getInitialState(ctx context.Context, genesis *RollkitGenesis, store store.Store, exec execution.Executor) (types.State, error) {
173+
func getInitialState(ctx context.Context, genesis *RollkitGenesis, store store.Store, exec execution.Executor, logger log.Logger) (types.State, error) {
174174
// Load the state from store.
175175
s, err := store.GetState(context.Background())
176176

177177
if errors.Is(err, ds.ErrNotFound) {
178+
logger.Info("No state found in store, initializing new state")
179+
178180
// If the user is starting a fresh chain (or hard-forking), we assume the stored state is empty.
179181
// TODO(tzdybal): handle max bytes
180182
stateRoot, _, err := exec.InitChain(ctx, genesis.GenesisTime, genesis.InitialHeight, genesis.ChainID)
181183
if err != nil {
184+
logger.Error("error while initializing chain", "error", err)
182185
return types.State{}, err
183186
}
184187

@@ -202,12 +205,13 @@ func getInitialState(ctx context.Context, genesis *RollkitGenesis, store store.S
202205
}
203206
return s, nil
204207
} else if err != nil {
208+
logger.Error("error while getting state", "error", err)
205209
return types.State{}, err
206210
} else {
207211
// Perform a sanity-check to stop the user from
208212
// using a higher genesis than the last stored state.
209213
// if they meant to hard-fork, they should have cleared the stored State
210-
if uint64(genesis.InitialHeight) > s.LastBlockHeight { //nolint:gosec
214+
if uint64(genesis.InitialHeight) > s.LastBlockHeight { //nolint:unconvert
211215
return types.State{}, fmt.Errorf("genesis.InitialHeight (%d) is greater than last stored state's LastBlockHeight (%d)", genesis.InitialHeight, s.LastBlockHeight)
212216
}
213217
}
@@ -223,10 +227,7 @@ func NewManager(
223227
genesis *RollkitGenesis,
224228
store store.Store,
225229
exec execution.Executor,
226-
mempool mempool.Mempool,
227-
mempoolReaper *mempool.CListMempoolReaper,
228230
seqClient *grpc.Client,
229-
proxyApp proxy.AppConnConsensus,
230231
dalc *da.DAClient,
231232
eventBus *cmtypes.EventBus,
232233
logger log.Logger,
@@ -235,8 +236,9 @@ func NewManager(
235236
seqMetrics *Metrics,
236237
execMetrics *state.Metrics,
237238
) (*Manager, error) {
238-
s, err := getInitialState(ctx, genesis, store, exec)
239+
s, err := getInitialState(ctx, genesis, store, exec, logger)
239240
if err != nil {
241+
logger.Error("error while getting initial state", "error", err)
240242
return nil, err
241243
}
242244
//set block height in store
@@ -273,10 +275,12 @@ func NewManager(
273275
return nil, err
274276
}
275277
// allow buffer for the block header and protocol encoding
278+
//nolint:ineffassign // This assignment is needed
276279
maxBlobSize -= blockProtocolOverhead
277280

278281
isProposer, err := isProposer(proposerKey, s)
279282
if err != nil {
283+
logger.Error("error while checking if proposer", "error", err)
280284
return nil, err
281285
}
282286

@@ -353,18 +357,8 @@ func (m *Manager) SetDALC(dalc *da.DAClient) {
353357
}
354358

355359
// isProposer returns whether or not the manager is a proposer
356-
func isProposer(signerPrivKey crypto.PrivKey, s types.State) (bool, error) {
360+
func isProposer(_ crypto.PrivKey, _ types.State) (bool, error) {
357361
return true, nil
358-
if len(s.Validators.Validators) == 0 {
359-
return false, ErrNoValidatorsInState
360-
}
361-
362-
signerPubBytes, err := signerPrivKey.GetPublic().Raw()
363-
if err != nil {
364-
return false, err
365-
}
366-
367-
return bytes.Equal(s.Validators.Validators[0].PubKey.Bytes(), signerPubBytes), nil
368362
}
369363

370364
// SetLastState is used to set lastState used by Manager.
@@ -421,6 +415,14 @@ func (m *Manager) getRemainingSleep(start time.Time) time.Duration {
421415
return 0
422416
}
423417

418+
// GetExecutor returns the executor used by the manager.
419+
//
420+
// Note: this is a temporary method to allow testing the manager.
421+
// It will be removed once the manager is fully integrated with the execution client.
422+
func (m *Manager) GetExecutor() execution.Executor {
423+
return m.exec
424+
}
425+
424426
// BatchRetrieveLoop is responsible for retrieving batches from the sequencer.
425427
func (m *Manager) BatchRetrieveLoop(ctx context.Context) {
426428
// Initialize batchTimer to fire immediately on start
@@ -478,7 +480,7 @@ func (m *Manager) BatchRetrieveLoop(ctx context.Context) {
478480

479481
// AggregationLoop is responsible for aggregating transactions into rollup-blocks.
480482
func (m *Manager) AggregationLoop(ctx context.Context) {
481-
initialHeight := uint64(m.genesis.InitialHeight) //nolint:gosec
483+
initialHeight := m.genesis.InitialHeight //nolint:gosec
482484
height := m.store.Height()
483485
var delay time.Duration
484486

@@ -1054,7 +1056,7 @@ func (m *Manager) publishBlock(ctx context.Context) error {
10541056
height := m.store.Height()
10551057
newHeight := height + 1
10561058
// this is a special case, when first block is produced - there is no previous commit
1057-
if newHeight == uint64(m.genesis.InitialHeight) { //nolint:gosec
1059+
if newHeight == uint64(m.genesis.InitialHeight) { //nolint:unconvert
10581060
lastSignature = &types.Signature{}
10591061
} else {
10601062
lastSignature, err = m.store.GetSignature(ctx, height)
@@ -1063,7 +1065,7 @@ func (m *Manager) publishBlock(ctx context.Context) error {
10631065
}
10641066
lastHeader, lastData, err := m.store.GetBlockData(ctx, height)
10651067
if err != nil {
1066-
return fmt.Errorf("error while loading last block: %w", err)
1068+
return fmt.Errorf("error while loading last block at height %d: %w", height, err)
10671069
}
10681070
lastHeaderHash = lastHeader.Hash()
10691071
lastDataHash = lastData.Hash()
@@ -1160,10 +1162,6 @@ func (m *Manager) publishBlock(ctx context.Context) error {
11601162
return err
11611163
}
11621164

1163-
if err := m.processVoteExtension(ctx, header, data, newHeight); err != nil {
1164-
return err
1165-
}
1166-
11671165
// set the signature to current block's signed header
11681166
header.Signature = signature
11691167

@@ -1210,6 +1208,7 @@ func (m *Manager) publishBlock(ctx context.Context) error {
12101208
newState.DAHeight = atomic.LoadUint64(&m.daHeight)
12111209
// After this call m.lastState is the NEW state returned from ApplyBlock
12121210
// updateState also commits the DB tx
1211+
m.logger.Debug("updating state", "newState", newState)
12131212
err = m.updateState(ctx, newState)
12141213
if err != nil {
12151214
return err
@@ -1255,48 +1254,9 @@ func (m *Manager) sign(payload []byte) ([]byte, error) {
12551254
}
12561255
}
12571256

1258-
func (m *Manager) processVoteExtension(ctx context.Context, header *types.SignedHeader, data *types.Data, newHeight uint64) error {
1259-
// TODO(tzdybal): remove this function completely
1260-
return nil // noop
1261-
/*
1262-
if !m.voteExtensionEnabled(newHeight) {
1263-
return nil
1264-
}
1265-
1266-
extension, err := m.executor.ExtendVote(ctx, header, data)
1267-
if err != nil {
1268-
return fmt.Errorf("error returned by ExtendVote: %w", err)
1269-
}
1270-
1271-
vote := &cmproto.Vote{
1272-
Height: int64(newHeight), //nolint:gosec
1273-
Round: 0,
1274-
Extension: extension,
1275-
}
1276-
extSignBytes := cmtypes.VoteExtensionSignBytes(m.genesis.ChainID, vote)
1277-
1278-
sign, err := m.sign(extSignBytes)
1279-
if err != nil {
1280-
return fmt.Errorf("failed to sign vote extension: %w", err)
1281-
}
1282-
extendedCommit := buildExtendedCommit(header, extension, sign)
1283-
err = m.store.SaveExtendedCommit(ctx, newHeight, extendedCommit)
1284-
if err != nil {
1285-
return fmt.Errorf("failed to save extended commit: %w", err)
1286-
}
1287-
return nil
1288-
*/
1289-
}
1290-
1291-
func (m *Manager) voteExtensionEnabled(newHeight uint64) bool {
1292-
return false
1293-
enableHeight := m.lastState.ConsensusParams.Abci.VoteExtensionsEnableHeight
1294-
return m.lastState.ConsensusParams.Abci != nil && enableHeight != 0 && uint64(enableHeight) <= newHeight //nolint:gosec
1295-
}
1296-
12971257
func (m *Manager) getExtendedCommit(ctx context.Context, height uint64) (abci.ExtendedCommitInfo, error) {
12981258
emptyExtendedCommit := abci.ExtendedCommitInfo{}
1299-
if !m.voteExtensionEnabled(height) || height <= uint64(m.genesis.InitialHeight) { //nolint:gosec
1259+
if height <= uint64(m.genesis.InitialHeight) { //nolint:unconvert
13001260
return emptyExtendedCommit, nil
13011261
}
13021262
extendedCommit, err := m.store.GetExtendedCommit(ctx, height)
@@ -1306,22 +1266,6 @@ func (m *Manager) getExtendedCommit(ctx context.Context, height uint64) (abci.Ex
13061266
return *extendedCommit, nil
13071267
}
13081268

1309-
func buildExtendedCommit(header *types.SignedHeader, extension []byte, sign []byte) *abci.ExtendedCommitInfo {
1310-
extendedCommit := &abci.ExtendedCommitInfo{
1311-
Round: 0,
1312-
Votes: []abci.ExtendedVoteInfo{{
1313-
Validator: abci.Validator{
1314-
Address: header.Validators.GetProposer().Address,
1315-
Power: header.Validators.GetProposer().VotingPower,
1316-
},
1317-
VoteExtension: extension,
1318-
ExtensionSignature: sign,
1319-
BlockIdFlag: cmproto.BlockIDFlagCommit,
1320-
}},
1321-
}
1322-
return extendedCommit
1323-
}
1324-
13251269
func (m *Manager) recordMetrics(data *types.Data) {
13261270
m.metrics.NumTxs.Set(float64(len(data.Txs)))
13271271
m.metrics.TotalTxs.Add(float64(len(data.Txs)))
@@ -1437,14 +1381,9 @@ func (m *Manager) exponentialBackoff(backoff time.Duration) time.Duration {
14371381
return backoff
14381382
}
14391383

1440-
func (m *Manager) getLastStateValidators() *cmtypes.ValidatorSet {
1441-
m.lastStateMtx.RLock()
1442-
defer m.lastStateMtx.RUnlock()
1443-
return m.lastState.Validators
1444-
}
1445-
14461384
// Updates the state stored in manager's store along the manager's lastState
14471385
func (m *Manager) updateState(ctx context.Context, s types.State) error {
1386+
m.logger.Debug("updating state", "newState", s)
14481387
m.lastStateMtx.Lock()
14491388
defer m.lastStateMtx.Unlock()
14501389
err := m.store.UpdateState(ctx, s)
@@ -1474,17 +1413,17 @@ func (m *Manager) applyBlock(ctx context.Context, header *types.SignedHeader, da
14741413
return m.execApplyBlock(ctx, m.lastState, header, data)
14751414
}
14761415

1477-
func (m *Manager) execValidate(lastState types.State, h *types.SignedHeader, d *types.Data) error {
1416+
func (m *Manager) execValidate(_ types.State, _ *types.SignedHeader, _ *types.Data) error {
14781417
// TODO(tzdybal): implement
14791418
return nil
14801419
}
14811420

1482-
func (m *Manager) execCommit(ctx context.Context, newState types.State, h *types.SignedHeader, d *types.Data, responses *abci.ResponseFinalizeBlock) ([]byte, error) {
1421+
func (m *Manager) execCommit(ctx context.Context, newState types.State, h *types.SignedHeader, _ *types.Data, _ *abci.ResponseFinalizeBlock) ([]byte, error) {
14831422
err := m.exec.SetFinal(ctx, h.Height())
14841423
return newState.AppHash, err
14851424
}
14861425

1487-
func (m *Manager) execCreateBlock(ctx context.Context, height uint64, lastSignature *types.Signature, commit abci.ExtendedCommitInfo, hash types.Hash, lastState types.State, txs cmtypes.Txs, timestamp time.Time) (*types.SignedHeader, *types.Data, error) {
1426+
func (m *Manager) execCreateBlock(_ context.Context, height uint64, lastSignature *types.Signature, _ abci.ExtendedCommitInfo, _ types.Hash, lastState types.State, txs cmtypes.Txs, timestamp time.Time) (*types.SignedHeader, *types.Data, error) {
14881427
// TODO(tzdybal): get rid of cmtypes.Tx, probable we should have common-shared-dep with basic types
14891428
rawTxs := make([]execTypes.Tx, len(txs))
14901429
for i := range txs {
@@ -1562,59 +1501,3 @@ func (m *Manager) nextState(state types.State, header *types.SignedHeader, state
15621501
}
15631502
return s, nil
15641503
}
1565-
1566-
/*
1567-
func updateState(s *types.State, res *abci.ResponseInitChain) error {
1568-
// If the app did not return an app hash, we keep the one set from the genesis doc in
1569-
// the state. We don't set appHash since we don't want the genesis doc app hash
1570-
// recorded in the genesis block. We should probably just remove GenesisDoc.AppHash.
1571-
if len(res.AppHash) > 0 {
1572-
s.AppHash = res.AppHash
1573-
}
1574-
1575-
if res.ConsensusParams != nil {
1576-
params := res.ConsensusParams
1577-
if params.Block != nil {
1578-
s.ConsensusParams.Block.MaxBytes = params.Block.MaxBytes
1579-
s.ConsensusParams.Block.MaxGas = params.Block.MaxGas
1580-
}
1581-
if params.Evidence != nil {
1582-
s.ConsensusParams.Evidence.MaxAgeNumBlocks = params.Evidence.MaxAgeNumBlocks
1583-
s.ConsensusParams.Evidence.MaxAgeDuration = params.Evidence.MaxAgeDuration
1584-
s.ConsensusParams.Evidence.MaxBytes = params.Evidence.MaxBytes
1585-
}
1586-
if params.Validator != nil {
1587-
// Copy params.Validator.PubkeyTypes, and set result's value to the copy.
1588-
// This avoids having to initialize the slice to 0 values, and then write to it again.
1589-
s.ConsensusParams.Validator.PubKeyTypes = append([]string{}, params.Validator.PubKeyTypes...)
1590-
}
1591-
if params.Version != nil {
1592-
s.ConsensusParams.Version.App = params.Version.App
1593-
}
1594-
s.Version.Consensus.App = s.ConsensusParams.Version.App
1595-
}
1596-
// We update the last results hash with the empty hash, to conform with RFC-6962.
1597-
s.LastResultsHash = merkle.HashFromByteSlices(nil)
1598-
1599-
vals, err := cmtypes.PB2TM.ValidatorUpdates(res.Validators)
1600-
if err != nil {
1601-
return err
1602-
}
1603-
1604-
// apply initchain valset change
1605-
nValSet := s.Validators.Copy()
1606-
err = nValSet.UpdateWithChangeSet(vals)
1607-
if err != nil {
1608-
return err
1609-
}
1610-
if len(nValSet.Validators) != 1 {
1611-
return fmt.Errorf("expected exactly one validator")
1612-
}
1613-
1614-
s.Validators = cmtypes.NewValidatorSet(nValSet.Validators)
1615-
s.NextValidators = cmtypes.NewValidatorSet(nValSet.Validators).CopyIncrementProposerPriority(1)
1616-
s.LastValidators = cmtypes.NewValidatorSet(nValSet.Validators)
1617-
1618-
return nil
1619-
}
1620-
*/

0 commit comments

Comments
 (0)