diff --git a/block/manager.go b/block/manager.go index 6c5ee1acd..761c789d5 100644 --- a/block/manager.go +++ b/block/manager.go @@ -113,7 +113,7 @@ func NewManager( return nil, err } - // TODO(mtsitrin): Probably should be validated and manage default on config init + // TODO((#119): Probably should be validated and manage default on config init. // TODO(omritoptix): Think about the default batchSize and default DABlockTime proper location. if conf.DABlockTime == 0 { logger.Info("WARNING: using default DA block time", "DABlockTime", config.DefaultNodeConfig.DABlockTime) @@ -431,6 +431,7 @@ func (m *Manager) syncUntilTarget(ctx context.Context, syncTarget uint64) { // In case the following doesn't hold true, it means we crashed after the commit and before updating the store height. // In that case we'll want to align the store with the app state and continue to the next block. func (m *Manager) applyBlock(ctx context.Context, block *types.Block, commit *types.Commit, blockMetaData blockMetaData) error { + //TODO: make it more go idiomatic, indent left the main logic if block.Header.Height == m.store.Height()+1 { m.logger.Info("Applying block", "height", block.Header.Height, "source", blockMetaData.source) @@ -488,16 +489,28 @@ func (m *Manager) applyBlock(ctx context.Context, block *types.Block, commit *ty } // Commit block to app - err = m.executor.Commit(ctx, &newState, block, responses) + retainHeight, err := m.executor.Commit(ctx, &newState, block, responses) if err != nil { m.logger.Error("Failed to commit to the block", "error", err) return err } + // Prune old heights, if requested by ABCI app. + if retainHeight > 0 { + pruned, err := m.pruneBlocks(retainHeight) + if err != nil { + m.logger.Error("failed to prune blocks", "retain_height", retainHeight, "err", err) + } else { + m.logger.Debug("pruned blocks", "pruned", pruned, "retain_height", retainHeight) + } + } + // Update the state with the new app hash, last validators and store height from the commit. // Every one of those, if happens before commit, prevents us from re-executing the block in case failed during commit. newState.LastValidators = m.lastState.Validators.Copy() newState.LastStoreHeight = block.Header.Height + newState.BaseHeight = m.store.Base() + _, err = m.store.UpdateState(newState, nil) if err != nil { m.logger.Error("Failed to update state", "error", err) diff --git a/block/pruning.go b/block/pruning.go new file mode 100644 index 000000000..8e0f4b49c --- /dev/null +++ b/block/pruning.go @@ -0,0 +1,23 @@ +package block + +import ( + "fmt" + "sync/atomic" +) + +func (m *Manager) pruneBlocks(retainHeight int64) (uint64, error) { + syncTarget := atomic.LoadUint64(&m.syncTarget) + + if retainHeight > int64(syncTarget) { + return 0, fmt.Errorf("cannot prune uncommitted blocks") + } + + pruned, err := m.store.PruneBlocks(retainHeight) + if err != nil { + return 0, fmt.Errorf("failed to prune block store: %w", err) + } + + //TODO: prune state/indexer and state/txindexer?? + + return pruned, nil +} diff --git a/node/node.go b/node/node.go index e156fd009..2ddc85483 100644 --- a/node/node.go +++ b/node/node.go @@ -137,6 +137,7 @@ func NewNode(ctx context.Context, conf config.NodeConfig, p2pKey crypto.PrivKey, baseKV = store.NewDefaultKVStore(conf.RootDir, conf.DBPath, "dymint") } mainKV := store.NewPrefixKV(baseKV, mainPrefix) + // TODO: dalcKV is needed for mock only. Initilize only if mock used dalcKV := store.NewPrefixKV(baseKV, dalcPrefix) indexerKV := store.NewPrefixKV(baseKV, indexerPrefix) diff --git a/proto/types/dymint/state.proto b/proto/types/dymint/state.proto index fb37f87fe..f5ff6d6cd 100755 --- a/proto/types/dymint/state.proto +++ b/proto/types/dymint/state.proto @@ -37,4 +37,5 @@ message State { bytes app_hash = 15; uint64 last_store_height = 16 [(gogoproto.customname) = "LastStoreHeight"]; + uint64 base_height = 17; } diff --git a/state/executor.go b/state/executor.go index fdd4b53d2..3d9a2819d 100644 --- a/state/executor.go +++ b/state/executor.go @@ -168,10 +168,10 @@ func (e *BlockExecutor) UpdateStateFromResponses(resp *tmstate.ABCIResponses, st } // Commit commits the block -func (e *BlockExecutor) Commit(ctx context.Context, state *types.State, block *types.Block, resp *tmstate.ABCIResponses) error { - appHash, err := e.commit(ctx, state, block, resp.DeliverTxs) +func (e *BlockExecutor) Commit(ctx context.Context, state *types.State, block *types.Block, resp *tmstate.ABCIResponses) (int64, error) { + appHash, retainHeight, err := e.commit(ctx, state, block, resp.DeliverTxs) if err != nil { - return err + return 0, err } copy(state.AppHash[:], appHash[:]) @@ -179,8 +179,9 @@ func (e *BlockExecutor) Commit(ctx context.Context, state *types.State, block *t err = e.publishEvents(resp, block, *state) if err != nil { e.logger.Error("failed to fire block events", "error", err) + return 0, err } - return nil + return retainHeight, nil } func (e *BlockExecutor) updateState(state types.State, block *types.Block, abciResponses *tmstate.ABCIResponses, validatorUpdates []*tmtypes.Validator) (types.State, error) { @@ -233,28 +234,28 @@ func (e *BlockExecutor) GetAppInfo() (*abcitypes.ResponseInfo, error) { return e.proxyAppQueryConn.InfoSync(abcitypes.RequestInfo{}) } -func (e *BlockExecutor) commit(ctx context.Context, state *types.State, block *types.Block, deliverTxs []*abci.ResponseDeliverTx) ([]byte, error) { +func (e *BlockExecutor) commit(ctx context.Context, state *types.State, block *types.Block, deliverTxs []*abci.ResponseDeliverTx) ([]byte, int64, error) { e.mempool.Lock() defer e.mempool.Unlock() err := e.mempool.FlushAppConn() if err != nil { - return nil, err + return nil, 0, err } resp, err := e.proxyAppConsensusConn.CommitSync() if err != nil { - return nil, err + return nil, 0, err } maxBytes := state.ConsensusParams.Block.MaxBytes maxGas := state.ConsensusParams.Block.MaxGas err = e.mempool.Update(int64(block.Header.Height), fromDymintTxs(block.Data.Txs), deliverTxs, mempool.PreCheckMaxBytes(maxBytes), mempool.PostCheckMaxGas(maxGas)) if err != nil { - return nil, err + return nil, 0, err } - return resp.Data, err + return resp.Data, resp.RetainHeight, err } func (e *BlockExecutor) validateBlock(state types.State, block *types.Block) error { diff --git a/state/executor_test.go b/state/executor_test.go index cb282f761..426e3135b 100644 --- a/state/executor_test.go +++ b/state/executor_test.go @@ -180,7 +180,7 @@ func TestApplyBlock(t *testing.T) { require.NoError(err) require.NotNil(newState) assert.Equal(int64(1), newState.LastBlockHeight) - err = executor.Commit(context.Background(), &newState, block, resp) + _, err = executor.Commit(context.Background(), &newState, block, resp) require.NoError(err) assert.Equal(mockAppHash, newState.AppHash) newState.LastStoreHeight = uint64(newState.LastBlockHeight) @@ -212,7 +212,11 @@ func TestApplyBlock(t *testing.T) { // Apply the block with an invalid commit err = executor.Validate(state, block, invalidCommit, proposer) - require.Error(types.ErrInvalidSignature) + + // FIXME: This test didn't check for specific error. It was just checking for error. + // If checking for this specific error, it fails + // require.ErrorIs(err, types.ErrInvalidSignature) + require.Error(err) // Create a valid commit for the block signature, err = proposerKey.Sign(abciHeaderBytes) @@ -233,7 +237,7 @@ func TestApplyBlock(t *testing.T) { require.NoError(err) require.NotNil(newState) assert.Equal(int64(2), newState.LastBlockHeight) - err = executor.Commit(context.Background(), &newState, block, resp) + _, err = executor.Commit(context.Background(), &newState, block, resp) require.NoError(err) // wait for at least 4 Tx events, for up to 3 second. diff --git a/store/pruning.go b/store/pruning.go new file mode 100644 index 000000000..8799cfc86 --- /dev/null +++ b/store/pruning.go @@ -0,0 +1,73 @@ +package store + +import "fmt" + +// PruneBlocks removes block up to (but not including) a height. It returns number of blocks pruned. +func (s *DefaultStore) PruneBlocks(heightInt int64) (uint64, error) { + if heightInt <= 0 { + return 0, fmt.Errorf("height must be greater than 0") + } + + height := uint64(heightInt) + if height > s.Height() { + return 0, fmt.Errorf("cannot prune beyond the latest height %v", s.height) + } + base := s.Base() + if height < base { + return 0, fmt.Errorf("cannot prune to height %v, it is lower than base height %v", + height, base) + } + + pruned := uint64(0) + batch := s.db.NewBatch() + defer batch.Discard() + + flush := func(batch Batch, base uint64) error { + err := batch.Commit() + if err != nil { + return fmt.Errorf("failed to prune up to height %v: %w", base, err) + } + s.SetBase(base) + return nil + } + + for h := base; h < height; h++ { + hash, err := s.loadHashFromIndex(h) + if err != nil { + continue + } + if err := batch.Delete(getBlockKey(hash)); err != nil { + return 0, err + } + if err := batch.Delete(getCommitKey(hash)); err != nil { + return 0, err + } + if err := batch.Delete(getIndexKey(h)); err != nil { + return 0, err + } + if err := batch.Delete(getResponsesKey(h)); err != nil { + return 0, err + } + if err := batch.Delete(getValidatorsKey(h)); err != nil { + return 0, err + } + + pruned++ + + // flush every 1000 blocks to avoid batches becoming too large + if pruned%1000 == 0 && pruned > 0 { + err := flush(batch, h) + if err != nil { + return 0, err + } + batch = s.db.NewBatch() + defer batch.Discard() + } + } + + err := flush(batch, height) + if err != nil { + return 0, err + } + return pruned, nil +} diff --git a/store/pruning_test.go b/store/pruning_test.go new file mode 100644 index 000000000..4132c3904 --- /dev/null +++ b/store/pruning_test.go @@ -0,0 +1,90 @@ +package store_test + +import ( + "testing" + + "github.com/dymensionxyz/dymint/store" + "github.com/dymensionxyz/dymint/testutil" + "github.com/dymensionxyz/dymint/types" + "github.com/stretchr/testify/assert" +) + +func TestStorePruning(t *testing.T) { + t.Parallel() + + pruningHeight := uint64(3) + + cases := []struct { + name string + blocks []*types.Block + pruningHeight uint64 + expectedBase uint64 + expectedHeight uint64 + shouldError bool + }{ + {"blocks with pruning", []*types.Block{ + testutil.GetRandomBlock(1, 0), + testutil.GetRandomBlock(2, 0), + testutil.GetRandomBlock(3, 0), + testutil.GetRandomBlock(4, 0), + testutil.GetRandomBlock(5, 0), + }, pruningHeight, pruningHeight, 5, false}, + {"blocks out of order", []*types.Block{ + testutil.GetRandomBlock(2, 0), + testutil.GetRandomBlock(3, 0), + testutil.GetRandomBlock(1, 0), + }, pruningHeight, pruningHeight, 3, false}, + {"with a gap", []*types.Block{ + testutil.GetRandomBlock(1, 0), + testutil.GetRandomBlock(9, 0), + testutil.GetRandomBlock(10, 0), + }, pruningHeight, pruningHeight, 10, false}, + {"pruning beyond latest height", []*types.Block{ + testutil.GetRandomBlock(1, 0), + testutil.GetRandomBlock(2, 0), + }, pruningHeight, 1, 2, true}, // should error because pruning height > latest height + {"pruning height 0", []*types.Block{ + testutil.GetRandomBlock(1, 0), + testutil.GetRandomBlock(2, 0), + testutil.GetRandomBlock(3, 0), + }, 0, 1, 3, true}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + assert := assert.New(t) + bstore := store.New(store.NewDefaultInMemoryKVStore()) + assert.Equal(uint64(0), bstore.Height()) + + for _, block := range c.blocks { + _, err := bstore.SaveBlock(block, &types.Commit{}, nil) + bstore.SetHeight(block.Header.Height) + assert.NoError(err) + } + + _, err := bstore.PruneBlocks(int64(c.pruningHeight)) + if c.shouldError { + assert.Error(err) + } else { + assert.NoError(err) + assert.Equal(pruningHeight, bstore.Base()) + assert.Equal(c.expectedHeight, bstore.Height()) + assert.Equal(c.expectedBase, bstore.Base()) + + // Check if pruned blocks are really removed from the store + for h := uint64(1); h < pruningHeight; h++ { + _, err := bstore.LoadBlock(h) + assert.Error(err, "Block at height %d should be pruned", h) + + _, err = bstore.LoadBlockResponses(h) + assert.Error(err, "BlockResponse at height %d should be pruned", h) + + _, err = bstore.LoadCommit(h) + assert.Error(err, "Commit at height %d should be pruned", h) + } + } + }) + } +} + +//TODO: prune twice diff --git a/store/store.go b/store/store.go index d74c63ed8..6322b9a58 100644 --- a/store/store.go +++ b/store/store.go @@ -28,7 +28,8 @@ var ( type DefaultStore struct { db KVStore - height uint64 + height uint64 + baseHeight uint64 } var _ Store = &DefaultStore{} @@ -58,6 +59,19 @@ func (s *DefaultStore) Height() uint64 { return atomic.LoadUint64(&s.height) } +// SetBase sets the height saved in the Store of the earliest block +func (s *DefaultStore) SetBase(height uint64) { + baseHeight := atomic.LoadUint64(&s.baseHeight) + if height > baseHeight { + _ = atomic.CompareAndSwapUint64(&s.baseHeight, baseHeight, height) + } +} + +// Base returns height of the earliest block saved in the Store. +func (s *DefaultStore) Base() uint64 { + return atomic.LoadUint64(&s.baseHeight) +} + // SaveBlock adds block to the store along with corresponding commit. // Stored height is updated if block height is greater than stored value. // In case a batch is provided, the block and commit are added to the batch and not saved. @@ -73,28 +87,29 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit, batch return batch, fmt.Errorf("failed to marshal Commit to binary: %w", err) } - bb := batch - if bb == nil { - bb = s.db.NewBatch() + //Not sure it's neeeded, as it's not used anywhere + if batch != nil { + err = multierr.Append(err, batch.Set(getBlockKey(hash), blockBlob)) + err = multierr.Append(err, batch.Set(getCommitKey(hash), commitBlob)) + err = multierr.Append(err, batch.Set(getIndexKey(block.Header.Height), hash[:])) + return batch, err } + + bb := s.db.NewBatch() + defer bb.Discard() + err = multierr.Append(err, bb.Set(getBlockKey(hash), blockBlob)) err = multierr.Append(err, bb.Set(getCommitKey(hash), commitBlob)) err = multierr.Append(err, bb.Set(getIndexKey(block.Header.Height), hash[:])) - if err != nil { - if batch == nil { - bb.Discard() - } - return batch, err + return batch, fmt.Errorf("failed to create db batch: %w", err) } - if batch == nil { - if err = bb.Commit(); err != nil { - return nil, fmt.Errorf("failed to commit transaction: %w", err) - } + if err = bb.Commit(); err != nil { + return nil, fmt.Errorf("failed to commit db batch: %w", err) } - return batch, nil + return bb, nil } // LoadBlock returns block at given height, or error if it's not found in Store. @@ -212,6 +227,7 @@ func (s *DefaultStore) LoadState() (types.State, error) { } atomic.StoreUint64(&s.height, state.LastStoreHeight) + atomic.StoreUint64(&s.baseHeight, state.BaseHeight) return state, nil } diff --git a/store/store_test.go b/store/store_test.go index 4a41e0697..49f48a836 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -1,15 +1,14 @@ -package store +package store_test import ( - "math/rand" "os" "testing" abcitypes "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/crypto/ed25519" tmstate "github.com/tendermint/tendermint/proto/tendermint/state" - tmtypes "github.com/tendermint/tendermint/types" + "github.com/dymensionxyz/dymint/store" + "github.com/dymensionxyz/dymint/testutil" "github.com/dymensionxyz/dymint/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -22,27 +21,27 @@ func TestStoreHeight(t *testing.T) { blocks []*types.Block expected uint64 }{ - {"single block", []*types.Block{getRandomBlock(1, 0)}, 1}, + {"single block", []*types.Block{testutil.GetRandomBlock(1, 0)}, 1}, {"two consecutive blocks", []*types.Block{ - getRandomBlock(1, 0), - getRandomBlock(2, 0), + testutil.GetRandomBlock(1, 0), + testutil.GetRandomBlock(2, 0), }, 2}, {"blocks out of order", []*types.Block{ - getRandomBlock(2, 0), - getRandomBlock(3, 0), - getRandomBlock(1, 0), + testutil.GetRandomBlock(2, 0), + testutil.GetRandomBlock(3, 0), + testutil.GetRandomBlock(1, 0), }, 3}, {"with a gap", []*types.Block{ - getRandomBlock(1, 0), - getRandomBlock(9, 0), - getRandomBlock(10, 0), + testutil.GetRandomBlock(1, 0), + testutil.GetRandomBlock(9, 0), + testutil.GetRandomBlock(10, 0), }, 10}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { assert := assert.New(t) - bstore := New(NewDefaultInMemoryKVStore()) + bstore := store.New(store.NewDefaultInMemoryKVStore()) assert.Equal(uint64(0), bstore.Height()) for _, block := range c.blocks { @@ -62,10 +61,10 @@ func TestStoreLoad(t *testing.T) { name string blocks []*types.Block }{ - {"single block", []*types.Block{getRandomBlock(1, 10)}}, + {"single block", []*types.Block{testutil.GetRandomBlock(1, 10)}}, {"two consecutive blocks", []*types.Block{ - getRandomBlock(1, 10), - getRandomBlock(2, 20), + testutil.GetRandomBlock(1, 10), + testutil.GetRandomBlock(2, 20), }}, // TODO(tzdybal): this test needs extra handling because of lastCommits //{"blocks out of order", []*types.Block{ @@ -86,13 +85,13 @@ func TestStoreLoad(t *testing.T) { } }() - for _, kv := range []KVStore{NewDefaultInMemoryKVStore(), NewDefaultKVStore(tmpDir, "db", "test")} { + for _, kv := range []store.KVStore{store.NewDefaultInMemoryKVStore(), store.NewDefaultKVStore(tmpDir, "db", "test")} { for _, c := range cases { t.Run(c.name, func(t *testing.T) { assert := assert.New(t) require := require.New(t) - bstore := New(kv) + bstore := store.New(kv) lastCommit := &types.Commit{} for _, block := range c.blocks { @@ -129,10 +128,10 @@ func TestRestart(t *testing.T) { assert := assert.New(t) - validatorSet := getRandomValidatorSet() + validatorSet := testutil.GetRandomValidatorSet() - kv := NewDefaultInMemoryKVStore() - s1 := New(kv) + kv := store.NewDefaultInMemoryKVStore() + s1 := store.New(kv) expectedHeight := uint64(10) _, err := s1.UpdateState(types.State{ LastBlockHeight: int64(expectedHeight), @@ -143,7 +142,7 @@ func TestRestart(t *testing.T) { }, nil) assert.NoError(err) - s2 := New(kv) + s2 := store.New(kv) _, err = s2.LoadState() assert.NoError(err) @@ -154,8 +153,8 @@ func TestBlockResponses(t *testing.T) { t.Parallel() assert := assert.New(t) - kv := NewDefaultInMemoryKVStore() - s := New(kv) + kv := store.NewDefaultInMemoryKVStore() + s := store.New(kv) expected := &tmstate.ABCIResponses{ BeginBlock: &abcitypes.ResponseBeginBlock{ @@ -197,8 +196,8 @@ func TestBatch(t *testing.T) { t.Parallel() assert := assert.New(t) - kv := NewDefaultInMemoryKVStore() - s := New(kv) + kv := store.NewDefaultInMemoryKVStore() + s := store.New(kv) expected := &tmstate.ABCIResponses{ BeginBlock: &abcitypes.ResponseBeginBlock{ @@ -239,46 +238,3 @@ func TestBatch(t *testing.T) { assert.NotNil(resp) assert.Equal(expected, resp) } - -func getRandomBlock(height uint64, nTxs int) *types.Block { - block := &types.Block{ - Header: types.Header{ - Height: height, - }, - Data: types.Data{ - Txs: make(types.Txs, nTxs), - IntermediateStateRoots: types.IntermediateStateRoots{ - RawRootsList: make([][]byte, nTxs), - }, - }, - } - - for i := 0; i < nTxs; i++ { - block.Data.Txs[i] = getRandomTx() - block.Data.IntermediateStateRoots.RawRootsList[i] = getRandomBytes(32) - } - - return block -} - -func getRandomTx() types.Tx { - size := rand.Int()%100 + 100 - return types.Tx(getRandomBytes(size)) -} - -func getRandomBytes(n int) []byte { - data := make([]byte, n) - _, _ = rand.Read(data) - return data -} - -// TODO(tzdybal): extract to some common place -func getRandomValidatorSet() *tmtypes.ValidatorSet { - pubKey := ed25519.GenPrivKey().PubKey() - return &tmtypes.ValidatorSet{ - Proposer: &tmtypes.Validator{PubKey: pubKey, Address: pubKey.Address()}, - Validators: []*tmtypes.Validator{ - {PubKey: pubKey, Address: pubKey.Address()}, - }, - } -} diff --git a/store/types.go b/store/types.go index 5679fc7b4..789ebe8f3 100644 --- a/store/types.go +++ b/store/types.go @@ -19,6 +19,12 @@ type Store interface { // SetHeight sets the height saved in the Store if it is higher than the existing height. SetHeight(height uint64) + // Base returns height of the lowest block in store. + Base() uint64 + + // SetBase sets the height saved in the Store for the lowest block + SetBase(height uint64) + // SaveBlock saves block along with its seen commit (which will be included in the next block). SaveBlock(block *types.Block, commit *types.Commit, batch Batch) (Batch, error) @@ -48,4 +54,7 @@ type Store interface { SaveValidators(height uint64, validatorSet *tmtypes.ValidatorSet, batch Batch) (Batch, error) LoadValidators(height uint64) (*tmtypes.ValidatorSet, error) + + // Pruning functions + PruneBlocks(height int64) (uint64, error) } diff --git a/testutil/block.go b/testutil/block.go new file mode 100644 index 000000000..a666408dc --- /dev/null +++ b/testutil/block.go @@ -0,0 +1,55 @@ +package testutil + +import ( + "crypto/rand" + "math/big" + + "github.com/tendermint/tendermint/crypto/ed25519" + tmtypes "github.com/tendermint/tendermint/types" + + "github.com/dymensionxyz/dymint/types" +) + +func GetRandomBlock(height uint64, nTxs int) *types.Block { + block := &types.Block{ + Header: types.Header{ + Height: height, + }, + Data: types.Data{ + Txs: make(types.Txs, nTxs), + IntermediateStateRoots: types.IntermediateStateRoots{ + RawRootsList: make([][]byte, nTxs), + }, + }, + } + + for i := 0; i < nTxs; i++ { + block.Data.Txs[i] = GetRandomTx() + block.Data.IntermediateStateRoots.RawRootsList[i] = GetRandomBytes(32) + } + + return block +} + +func GetRandomTx() types.Tx { + n, _ := rand.Int(rand.Reader, big.NewInt(100)) + size := int(n.Int64()) + 100 + return types.Tx(GetRandomBytes(size)) +} + +func GetRandomBytes(n int) []byte { + data := make([]byte, n) + _, _ = rand.Read(data) + return data +} + +// TODO(tzdybal): extract to some common place +func GetRandomValidatorSet() *tmtypes.ValidatorSet { + pubKey := ed25519.GenPrivKey().PubKey() + return &tmtypes.ValidatorSet{ + Proposer: &tmtypes.Validator{PubKey: pubKey, Address: pubKey.Address()}, + Validators: []*tmtypes.Validator{ + {PubKey: pubKey, Address: pubKey.Address()}, + }, + } +} diff --git a/types/pb/dymint/state.pb.go b/types/pb/dymint/state.pb.go index d7d1b816f..4cb1db495 100644 --- a/types/pb/dymint/state.pb.go +++ b/types/pb/dymint/state.pb.go @@ -47,6 +47,7 @@ type State struct { LastResultsHash []byte `protobuf:"bytes,14,opt,name=last_results_hash,json=lastResultsHash,proto3" json:"last_results_hash,omitempty"` AppHash []byte `protobuf:"bytes,15,opt,name=app_hash,json=appHash,proto3" json:"app_hash,omitempty"` LastStoreHeight uint64 `protobuf:"varint,16,opt,name=last_store_height,json=lastStoreHeight,proto3" json:"last_store_height,omitempty"` + BaseHeight uint64 `protobuf:"varint,17,opt,name=base_height,json=baseHeight,proto3" json:"base_height,omitempty"` } func (m *State) Reset() { *m = State{} } @@ -194,6 +195,13 @@ func (m *State) GetLastStoreHeight() uint64 { return 0 } +func (m *State) GetBaseHeight() uint64 { + if m != nil { + return m.BaseHeight + } + return 0 +} + func init() { proto.RegisterType((*State)(nil), "dymint.State") } @@ -201,47 +209,48 @@ func init() { func init() { proto.RegisterFile("types/dymint/state.proto", fileDescriptor_4b679420add07272) } var fileDescriptor_4b679420add07272 = []byte{ - // 631 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x94, 0x4d, 0x6b, 0xdb, 0x3e, - 0x1c, 0xc7, 0xe3, 0x7f, 0x1f, 0xd2, 0x2a, 0x0f, 0xee, 0xdf, 0xdd, 0xc1, 0xed, 0xc0, 0x76, 0xbb, - 0x07, 0xc2, 0x0e, 0x36, 0xac, 0xb0, 0xe3, 0x06, 0x6e, 0x61, 0x0d, 0x84, 0x31, 0x9c, 0xd1, 0xc3, - 0x2e, 0x46, 0xb6, 0x35, 0x5b, 0xcc, 0xb1, 0x8c, 0xa5, 0x94, 0x64, 0xaf, 0xa2, 0xef, 0x6a, 0x3d, - 0xf6, 0xb8, 0x53, 0x36, 0x9c, 0x37, 0x32, 0x24, 0xd9, 0x89, 0xd7, 0x10, 0xe8, 0x2d, 0xfa, 0xea, - 0xf3, 0xfb, 0xe6, 0x6b, 0xfd, 0x7e, 0x12, 0xd0, 0xd9, 0x3c, 0x47, 0xd4, 0x89, 0xe6, 0x13, 0x9c, - 0x31, 0x87, 0x32, 0xc8, 0x90, 0x9d, 0x17, 0x84, 0x11, 0x6d, 0x5f, 0x6a, 0xa7, 0xcf, 0x62, 0x12, - 0x13, 0x21, 0x39, 0xfc, 0x97, 0xdc, 0x3d, 0x35, 0x63, 0x42, 0xe2, 0x14, 0x39, 0x62, 0x15, 0x4c, - 0xbf, 0x39, 0x0c, 0x4f, 0x10, 0x65, 0x70, 0x92, 0x57, 0xc0, 0x99, 0x34, 0x66, 0x28, 0x8b, 0x50, - 0x21, 0xcc, 0x61, 0x10, 0x62, 0x47, 0xa8, 0x15, 0x72, 0xbe, 0x81, 0x54, 0x42, 0x83, 0x79, 0xbd, - 0x85, 0xb9, 0x85, 0x29, 0x8e, 0x20, 0x23, 0x45, 0xc5, 0xbd, 0xd8, 0xc2, 0xe5, 0xb0, 0x80, 0x93, - 0xed, 0x7f, 0x28, 0x3e, 0xb8, 0xf9, 0x87, 0xe7, 0x3f, 0xdb, 0x60, 0x6f, 0xcc, 0x55, 0xed, 0x02, - 0xb4, 0x6f, 0x51, 0x41, 0x31, 0xc9, 0x74, 0xc5, 0x52, 0x06, 0x9d, 0xb7, 0x27, 0xf6, 0xba, 0xd2, - 0x96, 0x47, 0x75, 0x23, 0x01, 0xaf, 0x26, 0xb5, 0x13, 0x70, 0x10, 0x26, 0x10, 0x67, 0x3e, 0x8e, - 0xf4, 0xff, 0x2c, 0x65, 0x70, 0xe8, 0xb5, 0xc5, 0x7a, 0x18, 0x69, 0xaf, 0x40, 0x1f, 0x67, 0x98, - 0x61, 0x98, 0xfa, 0x09, 0xc2, 0x71, 0xc2, 0xf4, 0x1d, 0x4b, 0x19, 0xec, 0x78, 0xbd, 0x4a, 0xbd, - 0x16, 0xa2, 0xf6, 0x06, 0xfc, 0x9f, 0x42, 0xca, 0xfc, 0x20, 0x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// 643 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x94, 0x4d, 0x6b, 0xdb, 0x30, + 0x1c, 0xc6, 0xe3, 0xf5, 0x25, 0xad, 0xf2, 0xd6, 0xaa, 0x3b, 0xb8, 0x1d, 0xd8, 0x69, 0xf7, 0x42, + 0xd8, 0xc1, 0x81, 0x15, 0x76, 0xdc, 0xc0, 0x2d, 0xac, 0x81, 0x32, 0x86, 0x32, 0x7a, 0xd8, 0xc5, + 0xc8, 0xb6, 0x66, 0x8b, 0x39, 0x96, 0xb1, 0x94, 0xd2, 0xee, 0x53, 0xf4, 0x63, 0xf5, 0xd8, 0xe3, + 0x4e, 0xd9, 0x70, 0x2f, 0xfb, 0x18, 0x43, 0x92, 0xed, 0x78, 0x0d, 0x85, 0xde, 0xac, 0x47, 0xbf, + 0xff, 0xe3, 0x47, 0xfa, 0x4b, 0x02, 0xa6, 0xb8, 0xce, 0x08, 0x1f, 0x87, 0xd7, 0x33, 0x9a, 0x8a, + 0x31, 0x17, 0x58, 0x10, 0x27, 0xcb, 0x99, 0x60, 0x70, 0x53, 0x6b, 0x07, 0xcf, 0x23, 0x16, 0x31, + 0x25, 0x8d, 0xe5, 0x97, 0x9e, 0x3d, 0xb0, 0x23, 0xc6, 0xa2, 0x84, 0x8c, 0xd5, 0xc8, 0x9f, 0x7f, + 0x1f, 0x0b, 0x3a, 0x23, 0x5c, 0xe0, 0x59, 0x56, 0x02, 0x87, 0xda, 0x58, 0x90, 0x34, 0x24, 0xb9, + 0x32, 0xc7, 0x7e, 0x40, 0xc7, 0x4a, 0x2d, 0x91, 0xa3, 0x15, 0xa4, 0x14, 0x1a, 0xcc, 0x9b, 0x47, + 0x98, 0x4b, 0x9c, 0xd0, 0x10, 0x0b, 0x96, 0x97, 0xdc, 0xcb, 0x47, 0xb8, 0x0c, 0xe7, 0x78, 0xf6, + 0xf8, 0x0f, 0xd5, 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 0x3d, 0xd8, 0x51, 0x87, 0x67, 0xaf, 0x58, 0xd8, 0x03, 0x79, 0x80, 0xa7, 0x72, 0x4e, 0x87, 0xd1, + 0xde, 0x0d, 0x01, 0xda, 0xa0, 0xe3, 0x63, 0x5e, 0x97, 0xee, 0xca, 0x52, 0x04, 0xa4, 0xa4, 0x01, + 0xf7, 0xec, 0xb6, 0xb0, 0x8c, 0xbb, 0xc2, 0x32, 0xfe, 0x14, 0x96, 0x71, 0x73, 0x6f, 0xb5, 0xee, + 0xee, 0xad, 0xd6, 0xaf, 0x7b, 0xab, 0xf5, 0xcd, 0x89, 0xa8, 0x88, 0xe7, 0xbe, 0x13, 0xb0, 0x99, + 0x7c, 0x1a, 0x49, 0x2a, 0xef, 0xf6, 0xd5, 0xf5, 0xcf, 0xea, 0x9d, 0x2c, 0xdf, 0x16, 0xbf, 0x1c, + 0xfb, 0x9b, 0xea, 0x4a, 0x1c, 0xff, 0x0b, 0x00, 0x00, 0xff, 0xff, 0x08, 0x6d, 0x58, 0xb4, 0x4e, + 0x05, 0x00, 0x00, } func (m *State) Marshal() (dAtA []byte, err error) { @@ -264,6 +273,13 @@ func (m *State) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.BaseHeight != 0 { + i = encodeVarintState(dAtA, i, uint64(m.BaseHeight)) + i-- + dAtA[i] = 0x1 + i-- + dAtA[i] = 0x88 + } if m.LastStoreHeight != 0 { i = encodeVarintState(dAtA, i, uint64(m.LastStoreHeight)) i-- @@ -465,6 +481,9 @@ func (m *State) Size() (n int) { if m.LastStoreHeight != 0 { n += 2 + sovState(uint64(m.LastStoreHeight)) } + if m.BaseHeight != 0 { + n += 2 + sovState(uint64(m.BaseHeight)) + } return n } @@ -960,6 +979,25 @@ func (m *State) Unmarshal(dAtA []byte) error { break } } + case 17: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field BaseHeight", wireType) + } + m.BaseHeight = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowState + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.BaseHeight |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipState(dAtA[iNdEx:]) diff --git a/types/serialization.go b/types/serialization.go index 8f79d43f2..8cb3266a0 100644 --- a/types/serialization.go +++ b/types/serialization.go @@ -252,6 +252,7 @@ func (s *State) ToProto() (*pb.State, error) { Validators: validators, LastValidators: lastValidators, LastStoreHeight: s.LastStoreHeight, + BaseHeight: s.BaseHeight, LastHeightValidatorsChanged: s.LastHeightValidatorsChanged, ConsensusParams: s.ConsensusParams, LastHeightConsensusParamsChanged: s.LastHeightConsensusParamsChanged, @@ -274,6 +275,7 @@ func (s *State) FromProto(other *pb.State) error { } else { s.LastStoreHeight = other.LastStoreHeight } + s.BaseHeight = other.BaseHeight s.SLStateIndex = other.SLStateIndex lastBlockID, err := types.BlockIDFromProto(&other.LastBlockID) if err != nil { diff --git a/types/state.go b/types/state.go index 2e69f176a..a82dc3048 100644 --- a/types/state.go +++ b/types/state.go @@ -57,6 +57,9 @@ type State struct { // LastStore height is the last height we've saved to the store. LastStoreHeight uint64 + // BaseHeight is the height of the first block we have in store after pruning. + BaseHeight uint64 + // the latest AppHash we've received from calling abci.Commit() AppHash [32]byte }