Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change staking overlay #4720

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func NewBee(
}
}

var changedOverlay bool
if targetNeighborhood != "" {
neighborhood, err := swarm.ParseBitStrAddress(targetNeighborhood)
if err != nil {
Expand Down Expand Up @@ -311,6 +312,7 @@ func NewBee(
if err != nil {
return nil, fmt.Errorf("statestore: save new overlay: %w", err)
}
changedOverlay = true
}
}

Expand Down Expand Up @@ -966,6 +968,21 @@ func NewBee(

stakingContract := staking.New(overlayEthAddress, stakingContractAddress, abiutil.MustParseABI(chainCfg.StakingABI), bzzTokenAddress, transactionService, common.BytesToHash(nonce))

if chainEnabled && changedOverlay {
stake, err := stakingContract.GetStake(ctx)
if err != nil {
return nil, errors.New("getting stake balance")
}
if stake.Cmp(big.NewInt(0)) > 0 {
logger.Debug("changing overlay address in staking contract")
tx, err := stakingContract.ChangeStakeOverlay(ctx, common.BytesToHash(nonce))
if err != nil {
return nil, fmt.Errorf("cannot change staking overlay address: %v", err.Error())
}
logger.Info("overlay address changed in staking contract", "transaction", tx)
}
}

var (
pullerService *puller.Puller
agent *storageincentives.Agent
Expand Down
11 changes: 11 additions & 0 deletions pkg/storageincentives/staking/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (

type Contract interface {
DepositStake(ctx context.Context, stakedAmount *big.Int) (common.Hash, error)
ChangeStakeOverlay(ctx context.Context, nonce common.Hash) (common.Hash, error)
GetStake(ctx context.Context) (*big.Int, error)
WithdrawAllStake(ctx context.Context) (common.Hash, error)
RedistributionStatuser
Expand Down Expand Up @@ -223,6 +224,16 @@ func (c *contract) DepositStake(ctx context.Context, stakedAmount *big.Int) (com
return receipt.TxHash, nil
}

// ChangeStakeOverlay only changes the overlay address used in the redistribution game.
func (c *contract) ChangeStakeOverlay(ctx context.Context, nonce common.Hash) (common.Hash, error) {
nugaon marked this conversation as resolved.
Show resolved Hide resolved
receipt, err := c.sendDepositStakeTransaction(ctx, new(big.Int), c.overlayNonce)
if err != nil {
return common.Hash{}, err
}

return receipt.TxHash, nil
}

func (c *contract) GetStake(ctx context.Context) (*big.Int, error) {
stakedAmount, err := c.getStake(ctx)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/storageincentives/staking/mock/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (s *stakingContractMock) DepositStake(ctx context.Context, stakedAmount *bi
return s.depositStake(ctx, stakedAmount)
}

func (s *stakingContractMock) ChangeStakeOverlay(_ context.Context, h common.Hash) (common.Hash, error) {
return h, nil
}

func (s *stakingContractMock) GetStake(ctx context.Context) (*big.Int, error) {
return s.getStake(ctx)
}
Expand Down
Loading