Skip to content

Commit

Permalink
Merge pull request #2521 from s2quake/refactor/currency-for-dpos
Browse files Browse the repository at this point in the history
Refactor decimal places of currency used in dpos.
  • Loading branch information
s2quake authored Apr 9, 2024
2 parents 443a5d4 + 928513f commit 06b38a2
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 102 deletions.
16 changes: 8 additions & 8 deletions .Lib9c.Tests/Action/DPoS/Control/DelegateCtrlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void InvalidCurrencyTest()
BlockIndex = 1,
},
_delegatorAddress,
Asset.ConsensusToken * 50);
Asset.ConsensusFromGovernance(50));
Assert.Throws<InvalidCurrencyException>(
() => _states = DelegateCtrl.Execute(
_states,
Expand All @@ -53,7 +53,7 @@ public void InvalidCurrencyTest()
},
_delegatorAddress,
_validatorAddress,
Asset.ConsensusToken * 30,
Asset.ConsensusFromGovernance(30),
_nativeTokens));
}

Expand Down Expand Up @@ -86,7 +86,7 @@ public void InvalidShareTest()
BlockIndex = 1,
},
_validatorAddress,
Asset.ConsensusToken * 100);
Asset.ConsensusFromGovernance(100));
Assert.Throws<InvalidExchangeRateException>(
() => _states = DelegateCtrl.Execute(
_states,
Expand Down Expand Up @@ -126,19 +126,19 @@ public void BalanceTest(
Asset.GovernanceToken * 0,
_states.GetBalance(_validatorAddress, Asset.GovernanceToken));
Assert.Equal(
Asset.ConsensusToken * 0,
Asset.ConsensusFromGovernance(0),
_states.GetBalance(_operatorAddress, Asset.ConsensusToken));
Assert.Equal(
Asset.ConsensusToken * 0,
Asset.ConsensusFromGovernance(0),
_states.GetBalance(_delegatorAddress, Asset.ConsensusToken));
Assert.Equal(
Asset.Share * 0,
ShareFromGovernance(0),
_states.GetBalance(_operatorAddress, Asset.Share));
Assert.Equal(
Asset.Share * 0,
ShareFromGovernance(0),
_states.GetBalance(_delegatorAddress, Asset.Share));
Assert.Equal(
Asset.ConsensusToken * (selfDelegateAmount + delegateAmount),
Asset.ConsensusFromGovernance(selfDelegateAmount + delegateAmount),
_states.GetBalance(_validatorAddress, Asset.ConsensusToken));
Assert.Equal(
Asset.GovernanceToken * (operatorMintAmount - selfDelegateAmount),
Expand Down
30 changes: 15 additions & 15 deletions .Lib9c.Tests/Action/DPoS/Control/RedelegateCtrlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void InvalidCurrencyTest()
BlockIndex = 1,
},
_delegatorAddress,
Asset.ConsensusToken * 50);
Asset.ConsensusFromGovernance(50));
Assert.Throws<InvalidCurrencyException>(
() => _states = RedelegateCtrl.Execute(
_states,
Expand All @@ -65,7 +65,7 @@ public void InvalidCurrencyTest()
_delegatorAddress,
_srcValidatorAddress,
_dstValidatorAddress,
Asset.ConsensusToken * 30,
Asset.ConsensusFromGovernance(30),
_nativeTokens));
Assert.Throws<InvalidCurrencyException>(
() => _states = RedelegateCtrl.Execute(
Expand Down Expand Up @@ -97,7 +97,7 @@ public void InvalidValidatorTest()
_delegatorAddress,
CreateAddress(),
_dstValidatorAddress,
Asset.Share * 10,
ShareFromGovernance(10),
_nativeTokens));
Assert.Throws<NullValidatorException>(
() => _states = RedelegateCtrl.Execute(
Expand All @@ -110,7 +110,7 @@ public void InvalidValidatorTest()
_delegatorAddress,
_srcValidatorAddress,
CreateAddress(),
Asset.Share * 10,
ShareFromGovernance(10),
_nativeTokens));
}

Expand All @@ -130,7 +130,7 @@ public void MaxEntriesTest()
_delegatorAddress,
_srcValidatorAddress,
_dstValidatorAddress,
Asset.Share * 1,
ShareFromGovernance(1),
_nativeTokens);
}

Expand All @@ -145,7 +145,7 @@ public void MaxEntriesTest()
_delegatorAddress,
_srcValidatorAddress,
_dstValidatorAddress,
Asset.Share * 1,
ShareFromGovernance(1),
_nativeTokens));
}

Expand All @@ -164,7 +164,7 @@ public void ExceedRedelegateTest()
_delegatorAddress,
_srcValidatorAddress,
_dstValidatorAddress,
Asset.Share * 101,
ShareFromGovernance(101),
_nativeTokens));
}

Expand Down Expand Up @@ -248,22 +248,22 @@ public void BalanceTest(
Asset.GovernanceToken * 0,
_states.GetBalance(_dstValidatorAddress, Asset.GovernanceToken));
Assert.Equal(
Asset.ConsensusToken * 0,
Asset.ConsensusFromGovernance(0),
_states.GetBalance(_srcOperatorAddress, Asset.ConsensusToken));
Assert.Equal(
Asset.ConsensusToken * 0,
Asset.ConsensusFromGovernance(0),
_states.GetBalance(_dstOperatorAddress, Asset.ConsensusToken));
Assert.Equal(
Asset.ConsensusToken * 0,
Asset.ConsensusFromGovernance(0),
_states.GetBalance(_delegatorAddress, Asset.ConsensusToken));
Assert.Equal(
Asset.Share * 0,
ShareFromGovernance(0),
_states.GetBalance(_srcOperatorAddress, Asset.Share));
Assert.Equal(
Asset.Share * 0,
ShareFromGovernance(0),
_states.GetBalance(_dstOperatorAddress, Asset.Share));
Assert.Equal(
Asset.Share * 0,
ShareFromGovernance(0),
_states.GetBalance(_delegatorAddress, Asset.Share));
Assert.Equal(
Asset.GovernanceToken * (operatorMintAmount - selfDelegateAmount),
Expand Down Expand Up @@ -308,11 +308,11 @@ public void BalanceTest(
RedelegateCtrl.GetRedelegation(_states, _redelegationAddress)!
.RedelegationEntryAddresses[0])!);
Assert.Equal(
Asset.ConsensusToken * (selfDelegateAmount + delegateAmount)
Asset.ConsensusFromGovernance(selfDelegateAmount + delegateAmount)
- entry.UnbondingConsensusToken,
_states.GetBalance(_srcValidatorAddress, Asset.ConsensusToken));
Assert.Equal(
Asset.ConsensusToken * selfDelegateAmount
Asset.ConsensusFromGovernance(selfDelegateAmount)
+ entry.UnbondingConsensusToken,
_states.GetBalance(_dstValidatorAddress, Asset.ConsensusToken));
}
Expand Down
44 changes: 22 additions & 22 deletions .Lib9c.Tests/Action/DPoS/Control/UndelegateCtrlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void InvalidCurrencyTest()
BlockIndex = 1,
},
_delegatorAddress,
Asset.ConsensusToken * 50);
Asset.ConsensusFromGovernance(50));
Assert.Throws<InvalidCurrencyException>(
() => _states = UndelegateCtrl.Execute(
_states,
Expand All @@ -55,7 +55,7 @@ public void InvalidCurrencyTest()
},
_delegatorAddress,
_validatorAddress,
Asset.ConsensusToken * 30,
Asset.ConsensusFromGovernance(30),
_nativeTokens));
Assert.Throws<InvalidCurrencyException>(
() => _states = UndelegateCtrl.Execute(
Expand Down Expand Up @@ -85,7 +85,7 @@ public void InvalidValidatorTest()
},
_delegatorAddress,
CreateAddress(),
Asset.Share * 10,
ShareFromGovernance(10),
_nativeTokens));
}

Expand All @@ -104,7 +104,7 @@ public void MaxEntriesTest()
},
_delegatorAddress,
_validatorAddress,
Asset.Share * 1,
ShareFromGovernance(1),
_nativeTokens);
}

Expand All @@ -118,7 +118,7 @@ public void MaxEntriesTest()
},
_delegatorAddress,
_validatorAddress,
Asset.Share * 1,
ShareFromGovernance(1),
_nativeTokens));
}

Expand All @@ -136,7 +136,7 @@ public void ExceedUndelegateTest()
},
_delegatorAddress,
_validatorAddress,
Asset.Share * 101,
ShareFromGovernance(101),
_nativeTokens));
}

Expand All @@ -160,7 +160,7 @@ public void CompleteUnbondingTest(
},
_delegatorAddress,
_validatorAddress,
Asset.Share * undelegateAmount,
ShareFromGovernance(undelegateAmount),
_nativeTokens);
Assert.Single(
UndelegateCtrl.GetUndelegation(_states, _undelegationAddress)!
Expand Down Expand Up @@ -226,13 +226,13 @@ public void CancelUndelegateTest(
},
_delegatorAddress,
_validatorAddress,
Asset.Share * undelegateAmount,
ShareFromGovernance(undelegateAmount),
_nativeTokens);
Assert.Equal(
Asset.GovernanceToken * (delegatorMintAmount - delegateAmount),
_states.GetBalance(_delegatorAddress, Asset.GovernanceToken));
Assert.Equal(
Asset.ConsensusToken * (selfDelegateAmount + delegateAmount - undelegateAmount),
Asset.ConsensusFromGovernance(selfDelegateAmount + delegateAmount - undelegateAmount),
_states.GetBalance(_validatorAddress, Asset.ConsensusToken));
_states = UndelegateCtrl.Cancel(
_states,
Expand All @@ -242,14 +242,14 @@ public void CancelUndelegateTest(
BlockIndex = 2,
},
_undelegationAddress,
Asset.ConsensusToken * cancelAmount,
Asset.ConsensusFromGovernance(cancelAmount),
_nativeTokens);
Assert.Equal(
Asset.GovernanceToken * (delegatorMintAmount - delegateAmount),
_states.GetBalance(_delegatorAddress, Asset.GovernanceToken));
Assert.Equal(
Asset.ConsensusToken
* (selfDelegateAmount + delegateAmount - undelegateAmount + cancelAmount),
Asset.ConsensusFromGovernance(
selfDelegateAmount + delegateAmount - undelegateAmount + cancelAmount),
_states.GetBalance(_validatorAddress, Asset.ConsensusToken));
_states = UndelegateCtrl.Complete(
_states,
Expand All @@ -263,8 +263,8 @@ public void CancelUndelegateTest(
Asset.GovernanceToken * (delegatorMintAmount - delegateAmount),
_states.GetBalance(_delegatorAddress, Asset.GovernanceToken));
Assert.Equal(
Asset.ConsensusToken
* (selfDelegateAmount + delegateAmount - undelegateAmount + cancelAmount),
Asset.ConsensusFromGovernance(
selfDelegateAmount + delegateAmount - undelegateAmount + cancelAmount),
_states.GetBalance(_validatorAddress, Asset.ConsensusToken));
_states = UndelegateCtrl.Complete(
_states,
Expand All @@ -279,8 +279,8 @@ public void CancelUndelegateTest(
* (delegatorMintAmount - delegateAmount + undelegateAmount - cancelAmount),
_states.GetBalance(_delegatorAddress, Asset.GovernanceToken));
Assert.Equal(
Asset.ConsensusToken
* (selfDelegateAmount + delegateAmount - undelegateAmount + cancelAmount),
Asset.ConsensusFromGovernance(
selfDelegateAmount + delegateAmount - undelegateAmount + cancelAmount),
_states.GetBalance(_validatorAddress, Asset.ConsensusToken));
}

Expand All @@ -304,22 +304,22 @@ public void BalanceTest(
},
_delegatorAddress,
_validatorAddress,
Asset.Share * undelegateAmount,
ShareFromGovernance(undelegateAmount),
_nativeTokens);
Assert.Equal(
Asset.GovernanceToken * 0,
_states.GetBalance(_validatorAddress, Asset.GovernanceToken));
Assert.Equal(
Asset.ConsensusToken * 0,
Asset.ConsensusFromGovernance(0),
_states.GetBalance(_operatorAddress, Asset.ConsensusToken));
Assert.Equal(
Asset.ConsensusToken * 0,
Asset.ConsensusFromGovernance(0),
_states.GetBalance(_delegatorAddress, Asset.ConsensusToken));
Assert.Equal(
Asset.Share * 0,
ShareFromGovernance(0),
_states.GetBalance(_operatorAddress, Asset.Share));
Assert.Equal(
Asset.Share * 0,
ShareFromGovernance(0),
_states.GetBalance(_delegatorAddress, Asset.Share));
Assert.Equal(
Asset.GovernanceToken * (operatorMintAmount - selfDelegateAmount),
Expand All @@ -344,7 +344,7 @@ public void BalanceTest(
ValidatorCtrl.GetValidator(_states, _validatorAddress)!.DelegatorShares,
balanceA + balanceB);
Assert.Equal(
Asset.ConsensusToken * (selfDelegateAmount + delegateAmount - undelegateAmount),
Asset.ConsensusFromGovernance(selfDelegateAmount + delegateAmount - undelegateAmount),
_states.GetBalance(_validatorAddress, Asset.ConsensusToken));
}

Expand Down
10 changes: 5 additions & 5 deletions .Lib9c.Tests/Action/DPoS/Control/ValidatorCtrlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void InvalidCurrencyTest()
BlockIndex = 1,
},
_operatorAddress,
Asset.ConsensusToken * 50);
Asset.ConsensusFromGovernance(50));
Assert.Throws<InvalidCurrencyException>(
() => _states = ValidatorCtrl.Create(
_states,
Expand All @@ -51,7 +51,7 @@ public void InvalidCurrencyTest()
},
_operatorAddress,
_operatorPublicKey,
Asset.ConsensusToken * 30,
Asset.ConsensusFromGovernance(30),
_nativeTokens));
}

Expand Down Expand Up @@ -107,17 +107,17 @@ public void BalanceTest(int mintAmount, int selfDelegateAmount)
Asset.GovernanceToken * selfDelegateAmount,
_nativeTokens);
Assert.Equal(
Asset.ConsensusToken * selfDelegateAmount,
Asset.ConsensusFromGovernance(selfDelegateAmount),
_states.GetBalance(_validatorAddress, Asset.ConsensusToken));
Assert.Equal(
Asset.GovernanceToken * (mintAmount - selfDelegateAmount),
_states.GetBalance(_operatorAddress, Asset.GovernanceToken));
Assert.Equal(
Asset.Share * selfDelegateAmount,
ShareFromGovernance(selfDelegateAmount),
_states.GetBalance(
Delegation.DeriveAddress(_operatorAddress, _validatorAddress), Asset.Share));
Assert.Equal(
Asset.Share * selfDelegateAmount,
ShareFromGovernance(selfDelegateAmount),
ValidatorCtrl.GetValidator(_states, _validatorAddress)!.DelegatorShares);
}
}
Expand Down
Loading

0 comments on commit 06b38a2

Please sign in to comment.