Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v1.3-dev' into fix/lint
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Nov 4, 2024
2 parents a0b2032 + a760162 commit 1e8004a
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ updates:
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 10
open-pull-requests-limit: 5
# - package-ecosystem: npm
# directory: "/docs"
# schedule:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
paths:
- "**Dockerfile"
- "**.go"
- "go.sum"
push:
branches:
- master
Expand Down
11 changes: 6 additions & 5 deletions .github/workflows/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ jobs:
- uses: actions/stale@v9
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-pr-message: "This pull request has been automatically marked as stale because it has not had
recent activity. It will be closed if no further activity occurs. Thank you
for your contributions."
stale-pr-message:
"This pull request has been automatically marked as stale because it has not had
recent activity. It will be closed if no further activity occurs. Thank you
for your contributions."
days-before-stale: -1
days-before-close: -1
days-before-pr-stale: 10
days-before-pr-close: 4
days-before-pr-stale: 31
days-before-pr-close: 16
exempt-pr-labels: "S:wip"
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
pull_request:
paths:
- "**.go"
- "go.sum"
push:
branches:
- master
Expand Down
13 changes: 6 additions & 7 deletions DOCKER/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/bash


# TODO: Workaround for busy port problem happening with docker restart policy
# we are trying to start a new container but previous process still not release
# the port.
Expand All @@ -15,9 +14,9 @@ got_signal=false
# Function to handle signals and forward them to the tenderdash process
# shellcheck disable=SC2317
_forward_signal() {
echo "Caught signal! Forwarding to tenderdash process."
got_signal=true
kill -s "$1" "$child"
echo "Caught signal! Forwarding to tenderdash process."
got_signal=true
kill -s "$1" "$child"
}

# Trap signals and forward them to the tenderdash process
Expand Down Expand Up @@ -52,14 +51,14 @@ if [ ! -d "$TMHOME/config" ]; then
fi

# Start tenderdash in the background
tenderdash "$@" &
tenderdash "$@" 2>&1 &
child=$!
wait "$child"
exit_code=$?

if [ $got_signal == false ] && [ $exit_code -ne 0 ] && [ "$1" == "start" ]; then
echo "Sleeping for 10 seconds as workaround for the busy port problem. See entrypoint code for details."
sleep 10
echo "Sleeping for 10 seconds as workaround for the busy port problem. See entrypoint code for details."
sleep 10
fi

exit $exit_code
2 changes: 1 addition & 1 deletion internal/consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func (cs *State) OnStart(ctx context.Context) error {
break LOOP

case !IsDataCorruptionError(err):
cs.logger.Error("error on catchup replay; proceeding to start state anyway", "err", err)
cs.logger.Warn("error on catchup replay; proceeding to start state anyway", "err", err)
break LOOP

case repairAttempted:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *heightProposerSelector) proposerFromStore(height int64) error {
"validators_hash", meta.Header.ValidatorsHash, "quorum_hash", s.valSet.QuorumHash,
"validators", s.valSet)

return fmt.Errorf("quorum hash mismatch at height %d", height)
return fmt.Errorf("validator set hash mismatch at height %d", height)
}

proposer = meta.Header.ProposerProTxHash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func NewHeightRoundProposerSelector(vset *types.ValidatorSet, currentHeight int6

// proposerFromStore determines the proposer for the given height and round using current or previous block read from
// the block store.
//
// Requires s.valSet to be set to correct validator set.
func (s *heightRoundProposerSelector) proposerFromStore(height int64, round int32) error {
if s.bs == nil {
return fmt.Errorf("block store is nil")
Expand All @@ -85,15 +87,18 @@ func (s *heightRoundProposerSelector) proposerFromStore(height int64, round int3

meta := s.bs.LoadBlockMeta(height)
if meta != nil {
valSetHash := s.valSet.Hash()
// block already saved to store, just take the proposer
if !meta.Header.ValidatorsHash.Equal(s.valSet.Hash()) {
if !meta.Header.ValidatorsHash.Equal(valSetHash) {
// we loaded the same block, so quorum should be the same
s.logger.Error("quorum rotation detected but not expected",
"height", height,
"validators_hash", meta.Header.ValidatorsHash, "quorum_hash", s.valSet.QuorumHash,
"validators_hash", meta.Header.ValidatorsHash,
"expected_validators_hash", valSetHash,
"next_validators_hash", meta.Header.NextValidatorsHash,
"validators", s.valSet)

return fmt.Errorf("quorum hash mismatch at height %d", height)
return fmt.Errorf("validators hash mismatch at height %d", height)
}

proposer = meta.Header.ProposerProTxHash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func NewProposerSelector(cp types.ConsensusParams, valSet *types.ValidatorSet, v
case int32(tmtypes.VersionParams_CONSENSUS_VERSION_0):
return NewHeightProposerSelector(valSet, valsetHeight, bs, logger)
case int32(tmtypes.VersionParams_CONSENSUS_VERSION_1):

return NewHeightRoundProposerSelector(valSet, valsetHeight, valsetRound, bs, logger)
default:
return nil, fmt.Errorf("unknown consensus version: %v", cp.Version.ConsensusVersion)
Expand Down
2 changes: 1 addition & 1 deletion internal/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func NewFromConfig(logger log.Logger, cfg *config.Config) (*Inspector, error) {
if err != nil {
return nil, err
}
ss := state.NewStore(sDB)
ss := state.NewStore(sDB, logger.With("module", "state_store"))
return New(cfg.RPC, bs, ss, sinks, logger), nil
}

Expand Down
111 changes: 83 additions & 28 deletions internal/state/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,31 @@ type dbStore struct {
var _ Store = (*dbStore)(nil)

// NewStore creates the dbStore of the state pkg.
func NewStore(db dbm.DB) Store {
return dbStore{db, log.NewNopLogger()}
//
// ## Parameters
//
// - `db` - the database to use
// - `logger` - the logger to use; optional, defaults to a nop logger if not provided; if more than one is provided,
// it will panic
//
// ##Panics
//
// If more than one logger is provided.
func NewStore(db dbm.DB, logger ...log.Logger) Store {
// To avoid changing the API, we use `logger ...log.Logger` in function signature, so that old code can
// provide only `db`. In this case, we use NopLogger.
if len(logger) == 0 || logger[0] == nil {
logger = []log.Logger{log.NewNopLogger()}
}

if len(logger) > 1 {
panic("NewStore(): maximum one logger is allowed")
}

return dbStore{db, logger[0]}
}

// LoadState loads the State from the database.
// Load loads the State from the database.
func (store dbStore) Load() (State, error) {
return store.loadState(stateKey)
}
Expand Down Expand Up @@ -498,30 +518,43 @@ func (store dbStore) SaveValidatorSets(lowerHeight, upperHeight int64, vals *typ
return batch.WriteSync()
}

//-----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

// LoadValidators loads the ValidatorSet for a given height and round 0.
//
// Returns ErrNoValSetForHeight if the validator set can't be found for this height.
func (store dbStore) LoadValidators(height int64, bs selectproposer.BlockStore) (*types.ValidatorSet, error) {
// loadValidators is a helper that loads the validator set from height or last stored height.
// It does NOT set a correct proposer.
func (store dbStore) loadValidators(height int64) (*tmstate.ValidatorsInfo, error) {
valInfo, err := loadValidatorsInfo(store.db, height)
if err != nil {
return nil, ErrNoValSetForHeight{Height: height, Err: err}
return nil, ErrNoValSetForHeight{Height: height, Err: fmt.Errorf("failed to load validators info: %w", err)}
}

if valInfo.ValidatorSet == nil {
lastStoredHeight := lastStoredHeightFor(height, valInfo.LastHeightChanged)
store.logger.Debug("Validator set is nil, loading last stored height", "height", height, "last_height_changed", valInfo.LastHeightChanged, "last_stored_height", lastStoredHeight)
valInfo, err = loadValidatorsInfo(store.db, lastStoredHeight)
if err != nil || valInfo.ValidatorSet == nil {
return nil,
fmt.Errorf("couldn't find validators at height %d (height %d was originally requested): %w",
lastStoredHeight,
height,
err,
)
return nil, ErrNoValSetForHeight{Height: height, Err: fmt.Errorf("couldn't find validators at height %d (height %d was originally requested): %w",
lastStoredHeight,
height,
err,
)}
}
}

return valInfo, nil
}

// LoadValidators loads the ValidatorSet for a given height and round 0.
//
// Returns ErrNoValSetForHeight if the validator set can't be found for this height.
func (store dbStore) LoadValidators(height int64, bs selectproposer.BlockStore) (*types.ValidatorSet, error) {
store.logger.Debug("Loading validators", "height", height)

valInfo, err := store.loadValidators(height)
if err != nil {
return nil, ErrNoValSetForHeight{Height: height, Err: err}
}

valSet, err := types.ValidatorSetFromProto(valInfo.ValidatorSet)
if err != nil {
return nil, err
Expand Down Expand Up @@ -562,27 +595,48 @@ func (store dbStore) LoadValidators(height int64, bs selectproposer.BlockStore)
return strategy.ValidatorSet(), nil
}

// If we have that height in the block store, we just take proposer from previous block and advance it.
// If we don't have that height in the block store, we just take proposer from previous block and advance it.
// We don't use current height block because we want to return proposer at round 0.
prevMeta := bs.LoadBlockMeta(height - 1)
if prevMeta == nil {
return nil, fmt.Errorf("could not find block meta for height %d", height-1)
}
// Configure proposer strategy; first set proposer from previous block
if err := valSet.SetProposer(prevMeta.Header.ProposerProTxHash); err != nil {
return nil, fmt.Errorf("could not set proposer: %w", err)
}
strategy, err := selectproposer.NewProposerSelector(cp, valSet, prevMeta.Header.Height, prevMeta.Round, bs, store.logger)
if err != nil {
return nil, fmt.Errorf("failed to create validator scoring strategy: %w", err)
}
// Configure proposer strategy; first check if we have a quorum change
if !prevMeta.Header.ValidatorsHash.Equal(prevMeta.Header.NextValidatorsHash) {
// rotation happened - we select 1st validator as proposer
valSetHash := valSet.Hash()
if !prevMeta.Header.NextValidatorsHash.Equal(valSetHash) {
return nil, ErrNoValSetForHeight{
Height: height,
Err: fmt.Errorf("next validators hash mismatch at height %d, expected %X, got %X", height,
prevMeta.Header.NextValidatorsHash, valSetHash),
}
}

if err = valSet.SetProposer(valSet.GetByIndex(0).ProTxHash); err != nil {
return nil, ErrNoValSetForHeight{height, fmt.Errorf("could not set proposer: %w", err)}
}

// now, advance to (height,0)
if err := strategy.UpdateHeightRound(height, 0); err != nil {
return nil, fmt.Errorf("failed to update validator scores at height %d, round 0: %w", height, err)
return valSet, nil
} else {

// set proposer from previous block
if err := valSet.SetProposer(prevMeta.Header.ProposerProTxHash); err != nil {
return nil, fmt.Errorf("could not set proposer: %w", err)
}
strategy, err := selectproposer.NewProposerSelector(cp, valSet, prevMeta.Header.Height, prevMeta.Round, bs, store.logger)
if err != nil {
return nil, fmt.Errorf("failed to create validator scoring strategy: %w", err)
}

// now, advance to (height,0)
if err := strategy.UpdateHeightRound(height, 0); err != nil {
return nil, fmt.Errorf("failed to update validator scores at height %d, round 0: %w", height, err)
}

return strategy.ValidatorSet(), nil
}

return strategy.ValidatorSet(), nil
}

func lastStoredHeightFor(height, lastHeightChanged int64) int64 {
Expand Down Expand Up @@ -643,6 +697,7 @@ func (store dbStore) saveValidatorsInfo(
return err
}

store.logger.Debug("saving validator set", "height", height, "last_height_changed", lastHeightChanged, "validators", valSet)
return batch.Set(validatorsKey(height), bz)
}

Expand Down
Loading

0 comments on commit 1e8004a

Please sign in to comment.