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

Bootstrap delegations consolidation #1016

Merged
merged 3 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 7 additions & 17 deletions builtin/plugins/dposv2/dpos.go
Original file line number Diff line number Diff line change
Expand Up @@ -2141,11 +2141,11 @@ func Dump(ctx contract.Context, dposv3Address loom.Address) (*dposv3.Initializat
var v3Statistics []*dposv3.ValidatorStatistic
for _, statistic := range statistics {
v3Statistic := &dposv3.ValidatorStatistic{
Address: statistic.Address,
PubKey: statistic.PubKey,
WhitelistAmount: statistic.WhitelistAmount,
DelegationTotal: statistic.DelegationTotal,
SlashPercentage: statistic.SlashPercentage,
Address: statistic.Address,
PubKey: statistic.PubKey,
WhitelistAmount: statistic.WhitelistAmount,
DelegationTotal: statistic.DelegationTotal,
SlashPercentage: statistic.SlashPercentage,
}
v3Statistics = append(v3Statistics, v3Statistic)
}
Expand Down Expand Up @@ -2183,10 +2183,11 @@ func Dump(ctx contract.Context, dposv3Address loom.Address) (*dposv3.Initializat
validatorString := delegation.Validator.Local.String()
delegatorString := delegation.Delegator.Local.String()
delegationKey := validatorString + delegatorString
validator := adjustValidatorIfInPlasmaValidators(*delegation)
amount := adjustDoubledDelegationAmount(*delegation)

v3Delegation := &dposv3.Delegation{
Validator: delegation.Validator,
Validator: validator,
Delegator: delegation.Delegator,
Index: dposv3.DELEGATION_START_INDEX + indices[delegationKey],
Amount: amount,
Expand Down Expand Up @@ -2225,14 +2226,3 @@ func Dump(ctx contract.Context, dposv3Address loom.Address) (*dposv3.Initializat

return initializationState, nil
}

func adjustDoubledDelegationAmount(delegation Delegation) *types.BigUInt {
amount := delegation.Amount.Value
validatorMatch := doubledValidator.Local.Compare(delegation.Validator.Local) == 0
delegatorMatch := doubledDelegator.Local.Compare(delegation.Delegator.Local) == 0
if validatorMatch && delegatorMatch {
amount = *common.BigZero()
amount.Div(&delegation.Amount.Value, loom.NewBigUIntFromInt(2))
}
return &types.BigUInt{Value: amount}
}
40 changes: 34 additions & 6 deletions builtin/plugins/dposv2/dpos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2277,9 +2277,9 @@ func TestPostLocktimeRewards(t *testing.T) {
func TestDedoublingDelegation(t *testing.T) {
originalAmount := loom.NewBigUIntFromInt(100)
doubledDelegation := Delegation{
Validator: doubledValidator.MarshalPB(),
Delegator: doubledDelegator.MarshalPB(),
Amount: &types.BigUInt{Value: *originalAmount},
Validator: doubledValidator.MarshalPB(),
Delegator: doubledDelegator.MarshalPB(),
Amount: &types.BigUInt{Value: *originalAmount},
}
adjustedAmount := adjustDoubledDelegationAmount(doubledDelegation)
expectedAmount := common.BigZero()
Expand All @@ -2288,14 +2288,42 @@ func TestDedoublingDelegation(t *testing.T) {

// test that adjustDoubledDelegationAmount does not halve the amount of a delegation which does not match the doubledDelegator & doubledValidator
nonDoubledDelegation := Delegation{
Validator: delegatorAddress1.MarshalPB(),
Delegator: doubledDelegator.MarshalPB(),
Amount: &types.BigUInt{Value: *originalAmount},
Validator: delegatorAddress1.MarshalPB(),
Delegator: doubledDelegator.MarshalPB(),
Amount: &types.BigUInt{Value: *originalAmount},
}
adjustedAmount = adjustDoubledDelegationAmount(nonDoubledDelegation)
assert.True(t, adjustedAmount.Value.Cmp(originalAmount) == 0)
}

// after we migrate we want to have all delegations that were in plasma-* nodes, on plasma-0
func TestPlasmaDelegationMigration(t *testing.T) {
expectedValidator := plasmaValidators[0]
someDelegator := loom.MustParseAddress("default:0xDc93E46f6d22D47De9D7E6d26ce8c3b7A13d89Cb")
someAmount := types.BigUInt{Value: *loom.NewBigUIntFromInt(100)}

// check that all plasma validators get reset to plasma-0
for _, v := range plasmaValidators {
migratedDelegation := Delegation{
Validator: v.MarshalPB(),
Delegator: someDelegator.MarshalPB(),
Amount: &someAmount,
}
migratedValidator := adjustValidatorIfInPlasmaValidators(migratedDelegation)
assert.True(t, migratedValidator.Local.Compare(expectedValidator.Local) == 0)
}

// non-plasma validators should be left as they are
someValidator := loom.MustParseAddress("default:0xDc93E46f6d22D47De9D7E6d26ce8c312314589Cb")
migratedDelegationWithoutChange := Delegation{
Validator: someValidator.MarshalPB(),
Delegator: someDelegator.MarshalPB(),
Amount: &someAmount,
}
migratedValidator := adjustValidatorIfInPlasmaValidators(migratedDelegationWithoutChange)
assert.True(t, migratedValidator.Local.Compare(someValidator.Local) == 0)
}

// UTILITIES

func makeAccount(owner loom.Address, bal uint64) *coin.InitialAccount {
Expand Down
36 changes: 33 additions & 3 deletions builtin/plugins/dposv2/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ import (
loom "github.com/loomnetwork/go-loom"
"github.com/loomnetwork/go-loom/common"
contract "github.com/loomnetwork/go-loom/plugin/contractpb"
types "github.com/loomnetwork/go-loom/types"
)


const billionthsBasisPointRatio = 100000

var (
plasmaValidators = []loom.Address{
loom.MustParseAddress("default:0x0e99fc16e32e568971908f2ce54b967a42663a26"), // plasma-0
loom.MustParseAddress("default:0xac3211caecc45940a6d2ba006ca465a647d8464f"), // plasma-1
loom.MustParseAddress("default:0x69c48768dbac492908161be787b7a5658192df35"), // plasma-2
loom.MustParseAddress("default:0x2a3a7c850586d4f80a12ac1952f88b1b69ef48e1"), // plasma-3
loom.MustParseAddress("default:0x4a1b8b15e50ce63cc6f65603ea79be09206cae70"), // plasma-4
loom.MustParseAddress("default:0x0ce7b61c97a6d5083356f115288f9266553e191e"), // plasma-5
}
doubledDelegator = loom.MustParseAddress("default:0xDc93E46f6d22D47De9D7E6d26ce8c3b7A13d89Cb")
doubledValidator = loom.MustParseAddress("default:0xa38c27e8cf4a443e805065065aefb250b1e1cef2")
basisPoints = loom.BigUInt{big.NewInt(1e4)} // do not change
billionth = loom.BigUInt{big.NewInt(1e9)}
basisPoints = loom.BigUInt{big.NewInt(1e4)} // do not change
billionth = loom.BigUInt{big.NewInt(1e9)}
)

var TierMap = map[uint64]LocktimeTier{
Expand All @@ -39,6 +47,28 @@ var TierBonusMap = map[LocktimeTier]loom.BigUInt{
TIER_THREE: loom.BigUInt{big.NewInt(40000)}, // one year
}

/// If the validator is one of the plasma nodes, it sets it to plasma-0
func adjustValidatorIfInPlasmaValidators(delegation Delegation) *types.Address {
validator := delegation.Validator
for _, plasmaValidator := range plasmaValidators {
if validator.Local.Compare(plasmaValidator.Local) == 0 {
return plasmaValidators[0].MarshalPB()
}
}
return validator
}

func adjustDoubledDelegationAmount(delegation Delegation) *types.BigUInt {
amount := delegation.Amount.Value
validatorMatch := doubledValidator.Local.Compare(delegation.Validator.Local) == 0
delegatorMatch := doubledDelegator.Local.Compare(delegation.Delegator.Local) == 0
if validatorMatch && delegatorMatch {
amount = *common.BigZero()
amount.Div(&delegation.Amount.Value, loom.NewBigUIntFromInt(2))
}
return &types.BigUInt{Value: amount}
}

// frac is expressed in basis points if granular == false
// or billionths if granular == true
func CalculateFraction(frac loom.BigUInt, total loom.BigUInt, granular bool) loom.BigUInt {
Expand Down