Skip to content

Commit

Permalink
Updated inflation and reward functions with timing instead of blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jclapis committed May 13, 2021
1 parent 9e182fa commit e313519
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 109 deletions.
12 changes: 6 additions & 6 deletions node/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ func GetNodeMinimumRPLStake(rp *rocketpool.RocketPool, nodeAddress common.Addres
}


// Get the block a node last staked RPL at
func GetNodeRPLStakedBlock(rp *rocketpool.RocketPool, nodeAddress common.Address, opts *bind.CallOpts) (uint64, error) {
// Get the time a node last staked RPL
func GetNodeRPLStakedTime(rp *rocketpool.RocketPool, nodeAddress common.Address, opts *bind.CallOpts) (uint64, error) {
rocketNodeStaking, err := getRocketNodeStaking(rp)
if err != nil {
return 0, err
}
nodeRplStakedBlock := new(*big.Int)
if err := rocketNodeStaking.Call(opts, nodeRplStakedBlock, "getNodeRPLStakedBlock", nodeAddress); err != nil {
return 0, fmt.Errorf("Could not get node RPL staked block: %w", err)
nodeRplStakedTime := new(*big.Int)
if err := rocketNodeStaking.Call(opts, nodeRplStakedTime, "getNodeRPLStakedTime", nodeAddress); err != nil {
return 0, fmt.Errorf("Could not get node RPL staked time: %w", err)
}
return (*nodeRplStakedBlock).Uint64(), nil
return (*nodeRplStakedTime).Uint64(), nil
}


Expand Down
20 changes: 10 additions & 10 deletions settings/protocol/inflation.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,36 @@ func BootstrapInflationIntervalRate(rp *rocketpool.RocketPool, value float64, op
}


// RPL inflation interval in blocks
func GetInflationIntervalBlocks(rp *rocketpool.RocketPool, opts *bind.CallOpts) (uint64, error) {
// RPL inflation interval time
func GetInflationIntervalTime(rp *rocketpool.RocketPool, opts *bind.CallOpts) (uint64, error) {
inflationSettingsContract, err := getInflationSettingsContract(rp)
if err != nil {
return 0, err
}
value := new(*big.Int)
if err := inflationSettingsContract.Call(opts, value, "getInflationIntervalBlocks"); err != nil {
if err := inflationSettingsContract.Call(opts, value, "getInflationIntervalTime"); err != nil {
return 0, fmt.Errorf("Could not get inflation interval: %w", err)
}
return (*value).Uint64(), nil
}
func BootstrapInflationIntervalBlocks(rp *rocketpool.RocketPool, value uint64, opts *bind.TransactOpts) (common.Hash, error) {
return protocoldao.BootstrapUint(rp, InflationSettingsContractName, "rpl.inflation.interval.blocks", big.NewInt(int64(value)), opts)
func BootstrapInflationIntervalTime(rp *rocketpool.RocketPool, value uint64, opts *bind.TransactOpts) (common.Hash, error) {
return protocoldao.BootstrapUint(rp, InflationSettingsContractName, "rpl.inflation.interval.time", big.NewInt(int64(value)), opts)
}


// RPL inflation start block
func GetInflationStartBlock(rp *rocketpool.RocketPool, opts *bind.CallOpts) (uint64, error) {
// RPL inflation start time
func GetInflationStartTime(rp *rocketpool.RocketPool, opts *bind.CallOpts) (uint64, error) {
inflationSettingsContract, err := getInflationSettingsContract(rp)
if err != nil {
return 0, err
}
value := new(*big.Int)
if err := inflationSettingsContract.Call(opts, value, "getInflationIntervalStartBlock"); err != nil {
return 0, fmt.Errorf("Could not get inflation start block: %w", err)
if err := inflationSettingsContract.Call(opts, value, "getInflationIntervalStartTime"); err != nil {
return 0, fmt.Errorf("Could not get inflation start time: %w", err)
}
return (*value).Uint64(), nil
}
func BootstrapInflationStartBlock(rp *rocketpool.RocketPool, value uint64, opts *bind.TransactOpts) (common.Hash, error) {
func BootstrapInflationStartTime(rp *rocketpool.RocketPool, value uint64, opts *bind.TransactOpts) (common.Hash, error) {
return protocoldao.BootstrapUint(rp, InflationSettingsContractName, "rpl.inflation.interval.start", big.NewInt(int64(value)), opts)
}

Expand Down
18 changes: 9 additions & 9 deletions settings/protocol/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ func GetRewardsClaimerPerc(rp *rocketpool.RocketPool, contractName string, opts
}


// The block that a claimer's share was last updated at
func GetRewardsClaimerPercBlockUpdated(rp *rocketpool.RocketPool, contractName string, opts *bind.CallOpts) (uint64, error) {
// The time that a claimer's share was last updated
func GetRewardsClaimerPercTimeUpdated(rp *rocketpool.RocketPool, contractName string, opts *bind.CallOpts) (uint64, error) {
rewardsSettingsContract, err := getRewardsSettingsContract(rp)
if err != nil {
return 0, err
}
value := new(*big.Int)
if err := rewardsSettingsContract.Call(opts, value, "getRewardsClaimerPercBlockUpdated", contractName); err != nil {
return 0, fmt.Errorf("Could not get rewards claimer updated block: %w", err)
if err := rewardsSettingsContract.Call(opts, value, "getRewardsClaimerPercTimeUpdated", contractName); err != nil {
return 0, fmt.Errorf("Could not get rewards claimer updated time: %w", err)
}
return (*value).Uint64(), nil
}
Expand All @@ -59,20 +59,20 @@ func GetRewardsClaimersPercTotal(rp *rocketpool.RocketPool, opts *bind.CallOpts)
}


// Rewards claim interval in blocks
func GetRewardsClaimIntervalBlocks(rp *rocketpool.RocketPool, opts *bind.CallOpts) (uint64, error) {
// Rewards claim interval time
func GetRewardsClaimIntervalTime(rp *rocketpool.RocketPool, opts *bind.CallOpts) (uint64, error) {
rewardsSettingsContract, err := getRewardsSettingsContract(rp)
if err != nil {
return 0, err
}
value := new(*big.Int)
if err := rewardsSettingsContract.Call(opts, value, "getRewardsClaimIntervalBlocks"); err != nil {
if err := rewardsSettingsContract.Call(opts, value, "getRewardsClaimIntervalTime"); err != nil {
return 0, fmt.Errorf("Could not get rewards claim interval: %w", err)
}
return (*value).Uint64(), nil
}
func BootstrapRewardsClaimIntervalBlocks(rp *rocketpool.RocketPool, value uint64, opts *bind.TransactOpts) (common.Hash, error) {
return protocoldao.BootstrapUint(rp, RewardsSettingsContractName, "rpl.rewards.claim.period.blocks", big.NewInt(int64(value)), opts)
func BootstrapRewardsClaimIntervalTime(rp *rocketpool.RocketPool, value uint64, opts *bind.TransactOpts) (common.Hash, error) {
return protocoldao.BootstrapUint(rp, RewardsSettingsContractName, "rpl.rewards.claim.period.time", big.NewInt(int64(value)), opts)
}


Expand Down
42 changes: 21 additions & 21 deletions tests/node/staking_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package node

import (
"math/big"
"testing"

"github.com/rocket-pool/rocketpool-go/node"
"github.com/rocket-pool/rocketpool-go/settings/protocol"
"github.com/rocket-pool/rocketpool-go/tokens"
"github.com/rocket-pool/rocketpool-go/utils/eth"

"github.com/rocket-pool/rocketpool-go/tests/testutils/evm"
minipoolutils "github.com/rocket-pool/rocketpool-go/tests/testutils/minipool"
nodeutils "github.com/rocket-pool/rocketpool-go/tests/testutils/node"
rplutils "github.com/rocket-pool/rocketpool-go/tests/testutils/tokens/rpl"
"math/big"
"testing"

"github.com/rocket-pool/rocketpool-go/node"
"github.com/rocket-pool/rocketpool-go/settings/protocol"
"github.com/rocket-pool/rocketpool-go/tokens"
"github.com/rocket-pool/rocketpool-go/utils/eth"

"github.com/rocket-pool/rocketpool-go/tests/testutils/evm"
minipoolutils "github.com/rocket-pool/rocketpool-go/tests/testutils/minipool"
nodeutils "github.com/rocket-pool/rocketpool-go/tests/testutils/node"
rplutils "github.com/rocket-pool/rocketpool-go/tests/testutils/tokens/rpl"
)


Expand Down Expand Up @@ -65,10 +65,10 @@ func TestStakeRPL(t *testing.T) {
} else if nodeMinimumRplStake.Cmp(big.NewInt(0)) != 0 {
t.Errorf("Incorrect initial node minimum RPL stake %s", nodeMinimumRplStake.String())
}
if nodeRplStakedBlock, err := node.GetNodeRPLStakedBlock(rp, nodeAccount.Address, nil); err != nil {
if nodeRplStakedTime, err := node.GetNodeRPLStakedTime(rp, nodeAccount.Address, nil); err != nil {
t.Error(err)
} else if nodeRplStakedBlock != 0 {
t.Errorf("Incorrect initial node RPL staked block %d", nodeRplStakedBlock)
} else if nodeRplStakedTime != 0 {
t.Errorf("Incorrect initial node RPL staked time %d", nodeRplStakedTime)
}
if nodeMinipoolLimit, err := node.GetNodeMinipoolLimit(rp, nodeAccount.Address, nil); err != nil {
t.Error(err)
Expand Down Expand Up @@ -107,10 +107,10 @@ func TestStakeRPL(t *testing.T) {
} else if nodeMinimumRplStake.Cmp(big.NewInt(0)) != 0 {
t.Errorf("Incorrect updated node minimum RPL stake 1 %s", nodeMinimumRplStake.String())
}
if nodeRplStakedBlock, err := node.GetNodeRPLStakedBlock(rp, nodeAccount.Address, nil); err != nil {
if nodeRplStakedTime, err := node.GetNodeRPLStakedTime(rp, nodeAccount.Address, nil); err != nil {
t.Error(err)
} else if nodeRplStakedBlock == 0 {
t.Errorf("Incorrect updated node RPL staked block 1 %d", nodeRplStakedBlock)
} else if nodeRplStakedTime == 0 {
t.Errorf("Incorrect updated node RPL staked time 1 %d", nodeRplStakedTime)
}
if nodeMinipoolLimit, err := node.GetNodeMinipoolLimit(rp, nodeAccount.Address, nil); err != nil {
t.Error(err)
Expand Down Expand Up @@ -161,9 +161,9 @@ func TestWithdrawRPL(t *testing.T) {
if err := nodeutils.StakeRPL(rp, ownerAccount, nodeAccount, rplAmount); err != nil { t.Fatal(err) }

// Get & set rewards claim interval
rewardsClaimIntervalBlocks, err := protocol.GetRewardsClaimIntervalBlocks(rp, nil)
rewardsClaimIntervalTime, err := protocol.GetRewardsClaimIntervalTime(rp, nil)
if err != nil { t.Fatal(err) }
if _, err := protocol.BootstrapRewardsClaimIntervalBlocks(rp, 0, ownerAccount.GetTransactor()); err != nil { t.Fatal(err) }
if _, err := protocol.BootstrapRewardsClaimIntervalTime(rp, 0, ownerAccount.GetTransactor()); err != nil { t.Fatal(err) }

// Check initial staking details
if totalRplStake, err := node.GetTotalRPLStake(rp, nil); err != nil {
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestWithdrawRPL(t *testing.T) {
}

// Reset rewards claim interval
if _, err := protocol.BootstrapRewardsClaimIntervalBlocks(rp, rewardsClaimIntervalBlocks, ownerAccount.GetTransactor()); err != nil { t.Fatal(err) }
if _, err := protocol.BootstrapRewardsClaimIntervalTime(rp, rewardsClaimIntervalTime, ownerAccount.GetTransactor()); err != nil { t.Fatal(err) }

}

34 changes: 18 additions & 16 deletions tests/rewards/node_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package rewards

import (
"context"
"math/big"
"testing"

"github.com/rocket-pool/rocketpool-go/node"
"github.com/rocket-pool/rocketpool-go/rewards"
"github.com/rocket-pool/rocketpool-go/settings/protocol"
"github.com/rocket-pool/rocketpool-go/tokens"
"github.com/rocket-pool/rocketpool-go/utils/eth"

"github.com/rocket-pool/rocketpool-go/tests/testutils/evm"
minipoolutils "github.com/rocket-pool/rocketpool-go/tests/testutils/minipool"
"context"
"math/big"
"testing"

"github.com/rocket-pool/rocketpool-go/node"
"github.com/rocket-pool/rocketpool-go/rewards"
"github.com/rocket-pool/rocketpool-go/settings/protocol"
"github.com/rocket-pool/rocketpool-go/tokens"
"github.com/rocket-pool/rocketpool-go/utils/eth"

"github.com/rocket-pool/rocketpool-go/tests/testutils/evm"
minipoolutils "github.com/rocket-pool/rocketpool-go/tests/testutils/minipool"
)


func TestNodeRewards(t *testing.T) {

var secondsPerBlock uint64 = 12

// State snapshotting
if err := evm.TakeSnapshot(); err != nil { t.Fatal(err) }
t.Cleanup(func() { if err := evm.RevertSnapshot(); err != nil { t.Fatal(err) } })
Expand All @@ -26,8 +28,8 @@ func TestNodeRewards(t *testing.T) {
if _, err := node.RegisterNode(rp, "Australia/Brisbane", nodeAccount.GetTransactor()); err != nil { t.Fatal(err) }

// Set network parameters
if _, err := protocol.BootstrapRewardsClaimIntervalBlocks(rp, 5, ownerAccount.GetTransactor()); err != nil { t.Fatal(err) }
if _, err := protocol.BootstrapInflationIntervalBlocks(rp, 5, ownerAccount.GetTransactor()); err != nil { t.Fatal(err) }
if _, err := protocol.BootstrapRewardsClaimIntervalTime(rp, 5 * secondsPerBlock, ownerAccount.GetTransactor()); err != nil { t.Fatal(err) }
if _, err := protocol.BootstrapInflationIntervalTime(rp, 5 * secondsPerBlock, ownerAccount.GetTransactor()); err != nil { t.Fatal(err) }

// Get & check node claims enabled status
if claimsEnabled, err := rewards.GetNodeClaimsEnabled(rp, nil); err != nil {
Expand All @@ -44,7 +46,7 @@ func TestNodeRewards(t *testing.T) {
}

// Mine blocks until node claims are possible
if err := evm.MineBlocks(5); err != nil { t.Fatal(err) }
if err := evm.MineBlocks(10); err != nil { t.Fatal(err) }

// Get & check updated node claim possible status
if nodeClaimPossible, err := rewards.GetNodeClaimPossible(rp, nodeAccount.Address, nil); err != nil {
Expand Down Expand Up @@ -80,7 +82,7 @@ func TestNodeRewards(t *testing.T) {
// Start RPL inflation
if header, err := rp.Client.HeaderByNumber(context.Background(), nil); err != nil {
t.Fatal(err)
} else if _, err := protocol.BootstrapInflationStartBlock(rp, header.Number.Uint64() + 2, ownerAccount.GetTransactor()); err != nil {
} else if _, err := protocol.BootstrapInflationStartTime(rp, (header.Number.Uint64() + 2) * secondsPerBlock, ownerAccount.GetTransactor()); err != nil {
t.Fatal(err)
}

Expand Down
24 changes: 13 additions & 11 deletions tests/rewards/trusted_node_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package rewards

import (
"context"
"math/big"
"testing"
"context"
"math/big"
"testing"

"github.com/rocket-pool/rocketpool-go/rewards"
"github.com/rocket-pool/rocketpool-go/settings/protocol"
"github.com/rocket-pool/rocketpool-go/tokens"
"github.com/rocket-pool/rocketpool-go/rewards"
"github.com/rocket-pool/rocketpool-go/settings/protocol"
"github.com/rocket-pool/rocketpool-go/tokens"

"github.com/rocket-pool/rocketpool-go/tests/testutils/evm"
nodeutils "github.com/rocket-pool/rocketpool-go/tests/testutils/node"
"github.com/rocket-pool/rocketpool-go/tests/testutils/evm"
nodeutils "github.com/rocket-pool/rocketpool-go/tests/testutils/node"
)


func TestTrustedNodeRewards(t *testing.T) {

var secondsPerBlock uint64 = 12

// State snapshotting
if err := evm.TakeSnapshot(); err != nil { t.Fatal(err) }
t.Cleanup(func() { if err := evm.RevertSnapshot(); err != nil { t.Fatal(err) } })
Expand All @@ -24,8 +26,8 @@ func TestTrustedNodeRewards(t *testing.T) {
if err := nodeutils.RegisterTrustedNode(rp, ownerAccount, trustedNodeAccount); err != nil { t.Fatal(err) }

// Set network parameters
if _, err := protocol.BootstrapRewardsClaimIntervalBlocks(rp, 5, ownerAccount.GetTransactor()); err != nil { t.Fatal(err) }
if _, err := protocol.BootstrapInflationIntervalBlocks(rp, 5, ownerAccount.GetTransactor()); err != nil { t.Fatal(err) }
if _, err := protocol.BootstrapRewardsClaimIntervalTime(rp, 5 * secondsPerBlock, ownerAccount.GetTransactor()); err != nil { t.Fatal(err) }
if _, err := protocol.BootstrapInflationIntervalTime(rp, 5 * secondsPerBlock, ownerAccount.GetTransactor()); err != nil { t.Fatal(err) }

// Get & check trusted node claims enabled status
if claimsEnabled, err := rewards.GetTrustedNodeClaimsEnabled(rp, nil); err != nil {
Expand Down Expand Up @@ -68,7 +70,7 @@ func TestTrustedNodeRewards(t *testing.T) {
// Start RPL inflation
if header, err := rp.Client.HeaderByNumber(context.Background(), nil); err != nil {
t.Fatal(err)
} else if _, err := protocol.BootstrapInflationStartBlock(rp, header.Number.Uint64() + 2, ownerAccount.GetTransactor()); err != nil {
} else if _, err := protocol.BootstrapInflationStartTime(rp, (header.Number.Uint64() + 2) * secondsPerBlock, ownerAccount.GetTransactor()); err != nil {
t.Fatal(err)
}

Expand Down
30 changes: 16 additions & 14 deletions tests/settings/protocol/inflation_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package protocol

import (
"testing"
"testing"

"github.com/rocket-pool/rocketpool-go/settings/protocol"
"github.com/rocket-pool/rocketpool-go/settings/protocol"

"github.com/rocket-pool/rocketpool-go/tests/testutils/evm"
"github.com/rocket-pool/rocketpool-go/tests/testutils/evm"
)


func TestInflationSettings(t *testing.T) {

var secondsPerBlock uint64 = 12

// State snapshotting
if err := evm.TakeSnapshot(); err != nil { t.Fatal(err) }
t.Cleanup(func() { if err := evm.RevertSnapshot(); err != nil { t.Fatal(err) } })
Expand All @@ -25,24 +27,24 @@ func TestInflationSettings(t *testing.T) {
t.Error("Incorrect inflation interval rate value")
}

// Set & get inflation interval blocks
var inflationIntervalBlocks uint64 = 1
if _, err := protocol.BootstrapInflationIntervalBlocks(rp, inflationIntervalBlocks, ownerAccount.GetTransactor()); err != nil {
// Set & get inflation interval time
var inflationIntervalTime uint64 = 1 * secondsPerBlock
if _, err := protocol.BootstrapInflationIntervalTime(rp, inflationIntervalTime, ownerAccount.GetTransactor()); err != nil {
t.Error(err)
} else if value, err := protocol.GetInflationIntervalBlocks(rp, nil); err != nil {
} else if value, err := protocol.GetInflationIntervalTime(rp, nil); err != nil {
t.Error(err)
} else if value != inflationIntervalBlocks {
t.Error("Incorrect inflation interval blocks value")
} else if value != inflationIntervalTime {
t.Error("Incorrect inflation interval time value")
}

// Set & get inflation start block
var inflationStartBlock uint64 = 1000000
if _, err := protocol.BootstrapInflationStartBlock(rp, inflationStartBlock, ownerAccount.GetTransactor()); err != nil {
var inflationStartTime uint64 = 1000000 * secondsPerBlock
if _, err := protocol.BootstrapInflationStartTime(rp, inflationStartTime, ownerAccount.GetTransactor()); err != nil {
t.Error(err)
} else if value, err := protocol.GetInflationStartBlock(rp, nil); err != nil {
} else if value, err := protocol.GetInflationStartTime(rp, nil); err != nil {
t.Error(err)
} else if value != inflationStartBlock {
t.Error("Incorrect inflation start block value")
} else if value != inflationStartTime {
t.Error("Incorrect inflation start time value")
}

}
Expand Down
Loading

0 comments on commit e313519

Please sign in to comment.