Skip to content

Commit

Permalink
WIP: use blockchain height for challenge handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nickeskov committed Aug 21, 2024
1 parent 7b99c7b commit 5ffd0d5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
17 changes: 9 additions & 8 deletions pkg/state/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ func (s *balances) newestGeneratingBalance(addr proto.AddressID, height proto.He
startHeight, endHeight := s.sets.RangeForGeneratingBalanceByHeight(height)
gb, err := s.newestMinEffectiveBalanceInRange(addr, startHeight, endHeight)
if err != nil {
return 0, errors.Wrapf(err, "failed get newest min effective balance; startHeight %d, endHeight %d",
return 0, errors.Wrapf(err, "failed get newest min effective balance with startHeight %d and endHeight %d",
startHeight, endHeight)
}
bonus, err := s.newestChallengerBonus(addr, height)
Expand All @@ -650,23 +650,24 @@ func (s *balances) newestGeneratingBalance(addr proto.AddressID, height proto.He

func (s *balances) storeChallenge(
challenger, challenged proto.AddressID,
height proto.Height,
blockchainHeight proto.Height, // for changing generating balance we use current blockchain height
blockID proto.BlockID,
) error {
// Check if challenger and challenged addresses are the same. Self-challenge is not allowed.
if challenger.Equal(challenged) {
return errors.New("challenger and challenged addresses are the same")
}
// Get challenger bonus before storing the challenged penalty.
challengedGeneratingBalance, gbErr := s.newestGeneratingBalance(challenged, height)
// Challenged can't be with bonus and be challenged at the same height because of the check above.
generatingBalance, gbErr := s.newestGeneratingBalance(challenged, blockchainHeight)
if gbErr != nil {
return errors.Wrapf(gbErr, "failed to get generating balance for challenged address at height %d", height)
return errors.Wrapf(gbErr, "failed to get generating balance for challenged address at height %d", blockchainHeight)
}
if err := s.storeChallengeBonusForAddr(challenger, challengedGeneratingBalance, height, blockID); err != nil {
return errors.Wrapf(err, "failed to store challenge bonus for challenger at height %d", height)
if err := s.storeChallengeBonusForAddr(challenger, generatingBalance, blockchainHeight, blockID); err != nil {
return errors.Wrapf(err, "failed to store challenge bonus for challenger at height %d", blockchainHeight)
}
if err := s.storeChallengeHeightForAddr(challenged, height, blockID); err != nil {
return errors.Wrapf(err, "failed to store challenge height for challenged at height %d", height)
if err := s.storeChallengeHeightForAddr(challenged, blockchainHeight, blockID); err != nil {
return errors.Wrapf(err, "failed to store challenge height for challenged at height %d", blockchainHeight)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/state/history_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ func (hs *historyStorage) newestEntriesDataInHeightRange(key []byte, startHeight
}
endBlockNum, err := hs.stateDB.newestBlockNumByHeight(endHeight)
if err != nil {
return nil, err
return nil, err // TODO: end height here is 3236175, so it's throws wrapped io.EOF, i think
}
return hs.blockRangeEntries(history, startBlockNum, endBlockNum), nil
}
Expand Down
12 changes: 8 additions & 4 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ func (s *stateManager) NewestFullWavesBalance(account proto.Recipient) (*proto.F
if err != nil {
return nil, wrapErr(Other, err)
}
height, err := s.NewestHeight()
height, err := s.NewestHeight() // TODO: while adding block should it be block height or blockchain height?
if err != nil {
return nil, wrapErr(RetrievalError, err)
}
Expand Down Expand Up @@ -1256,6 +1256,9 @@ func (s *stateManager) beforeAppendBlock(block *proto.Block, blockHeight proto.H
}

func (s *stateManager) handleChallengedHeaderIfExists(block *proto.Block, blockHeight proto.Height) error {
if blockHeight < 2 { // no challenges for genesis block
return nil
}
challengedHeader, ok := block.GetChallengedHeader()
if !ok { // nothing to do, no challenge to handle
return nil
Expand All @@ -1276,10 +1279,11 @@ func (s *stateManager) handleChallengedHeaderIfExists(block *proto.Block, blockH
challengedHeader.GeneratorPublicKey.String(),
)
}
if chErr := s.stor.balances.storeChallenge(challenger.ID(), challenged.ID(), blockHeight, blockID); chErr != nil {
blockchainHeight := blockHeight - 1
if chErr := s.stor.balances.storeChallenge(challenger.ID(), challenged.ID(), blockchainHeight, blockID); chErr != nil {
return errors.Wrapf(chErr,
"failed to store challenge for block '%s' at height %d with challenger '%s' and challenged '%s'",
blockID.String(), blockHeight, challenger.String(), challenged.String(),
"failed to store challenge with blockchain height %d for block '%s'with challenger '%s' and challenged '%s'",
blockchainHeight, blockID.String(), challenger.String(), challenged.String(),
)
}
return nil
Expand Down

0 comments on commit 5ffd0d5

Please sign in to comment.