Skip to content

Commit

Permalink
Merge pull request #3 from ShareRing/develop
Browse files Browse the repository at this point in the history
Decimalization
  • Loading branch information
quocphu committed May 11, 2021
2 parents e6cbdec + f36d06e commit 090eb20
Show file tree
Hide file tree
Showing 33 changed files with 1,129 additions and 704 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## [1.1.1] - 2021-05-11
### Added
- No

### Updated
- Remove old Id module
- Decimalization

### Fixed
- No


## [1.1.0] - 2021-03-03
### Added
- Document module
Expand Down
228 changes: 225 additions & 3 deletions cmd/shareledger/migrate-genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ package main
import (
"fmt"

"github.com/ShareRing/modules/utils"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
auth "github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/supply"
gentlemint "github.com/sharering/shareledger/x/gentlemint"
"github.com/sharering/shareledger/x/identity"
oldId "github.com/sharering/shareledger/x/identity"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -67,14 +74,227 @@ func FromV0_0_1(oldGenesisFile, newGenesisFile, mergeGenesisFile string, cdc *co
// Copy id data: Id data is totally different, can not copy
}

// Add 8 zero to all coin
func FromV1_1_0(inputFilePath, outputFilePath string, cdc *codec.Codec) error {
appState, genDoc, err := genutil.GenesisStateFromGenFile(cdc, inputFilePath)
if err != nil {
return fmt.Errorf("failed to unmarshal old genesis state: %w", err)
}

// Auth module
var authState auth.GenesisState
if appState[auth.ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[auth.ModuleName], &authState)
}
for i := 0; i < len(authState.Accounts); i++ {
acc := authState.Accounts[i]
coins := acc.GetCoins()
coins = coins.Sort()
newCoins := sdk.Coins{}

// SHR
shrAmount := coins.AmountOf("shr")
newShr := sdk.NewCoin("shr", shrAmount.Mul(utils.SHRDecimal))
newCoins = newCoins.Add(newShr)

// SHRP & cent
shrpAmount := coins.AmountOf("shrp")
centAmount := coins.AmountOf("cent")

shrpFromCent := centAmount.Mul(utils.SHRPDecimal).Quo(sdk.NewInt(100))
shrpAmount = shrpAmount.Mul(utils.SHRPDecimal)
shrpAmount = shrpAmount.Add(shrpFromCent)

newShrp := sdk.NewCoin("shrp", shrpAmount)
newCoins = newCoins.Add(newShrp)

acc.SetCoins(newCoins)
}

authStateBz, err := cdc.MarshalJSON(authState)
if err != nil {
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
}
appState[auth.ModuleName] = authStateBz

// Staking module
var stakingState staking.GenesisState
if appState[staking.ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[staking.ModuleName], &stakingState)
}

// The default power = token/10^6 and we apply 10^8 decimal
// so the power will be mutiplied by 10^2 = 100
powerMultiplier := int64(100)
stakingState.LastTotalPower = stakingState.LastTotalPower.Mul(sdk.NewInt(powerMultiplier))
for i := 0; i < len(stakingState.LastValidatorPowers); i++ {
stakingState.LastValidatorPowers[i].Power = stakingState.LastValidatorPowers[i].Power * powerMultiplier
}

for i := 0; i < len(stakingState.Validators); i++ {
stakingState.Validators[i].DelegatorShares = stakingState.Validators[i].DelegatorShares.MulInt(utils.SHRDecimal)
stakingState.Validators[i].Tokens = stakingState.Validators[i].Tokens.Mul(utils.SHRDecimal)
stakingState.Validators[i].UnbondingHeight = 0
}

for i := 0; i < len(stakingState.Delegations); i++ {
stakingState.Delegations[i].Shares = stakingState.Delegations[i].Shares.MulInt(utils.SHRDecimal)
}

for i := 0; i < len(stakingState.UnbondingDelegations); i++ {
for j := 0; j < len(stakingState.UnbondingDelegations[i].Entries); j++ {
stakingState.UnbondingDelegations[i].Entries[j].InitialBalance = stakingState.UnbondingDelegations[i].Entries[j].InitialBalance.Mul(utils.SHRDecimal)
stakingState.UnbondingDelegations[i].Entries[j].Balance = stakingState.UnbondingDelegations[i].Entries[j].Balance.Mul(utils.SHRDecimal)
stakingState.UnbondingDelegations[i].Entries[j].CreationHeight = 0
}
}

for i := 0; i < len(stakingState.Redelegations); i++ {
for j := 0; j < len(stakingState.Redelegations[i].Entries); j++ {
stakingState.Redelegations[i].Entries[j].InitialBalance = stakingState.Redelegations[i].Entries[j].InitialBalance.Mul(utils.SHRDecimal)
stakingState.Redelegations[i].Entries[j].SharesDst = stakingState.Redelegations[i].Entries[j].SharesDst.MulInt(utils.SHRDecimal)
stakingState.Redelegations[i].Entries[j].CreationHeight = 0
}
}

stakingStateBz, err := cdc.MarshalJSON(stakingState)
if err != nil {
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
}
appState[staking.ModuleName] = stakingStateBz

// Distribution module
var distState distribution.GenesisState
if appState[distribution.ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[distribution.ModuleName], &distState)
}

feePool := distState.FeePool
feePool.CommunityPool = decimalize(feePool.CommunityPool)
distState.FeePool = feePool

for i := 0; i < len(distState.OutstandingRewards); i++ {
distState.OutstandingRewards[i].OutstandingRewards = decimalize(distState.OutstandingRewards[i].OutstandingRewards)
}

for i := 0; i < len(distState.ValidatorAccumulatedCommissions); i++ {
distState.ValidatorAccumulatedCommissions[i].Accumulated = decimalize(distState.ValidatorAccumulatedCommissions[i].Accumulated)
}

for i := 0; i < len(distState.ValidatorHistoricalRewards); i++ {
distState.ValidatorHistoricalRewards[i].Rewards.CumulativeRewardRatio = decimalize(distState.ValidatorHistoricalRewards[i].Rewards.CumulativeRewardRatio)
}

for i := 0; i < len(distState.ValidatorCurrentRewards); i++ {
distState.ValidatorCurrentRewards[i].Rewards.Rewards = decimalize(distState.ValidatorCurrentRewards[i].Rewards.Rewards)
}

for i := 0; i < len(distState.DelegatorStartingInfos); i++ {
distState.DelegatorStartingInfos[i].StartingInfo.Stake = distState.DelegatorStartingInfos[i].StartingInfo.Stake.MulInt(utils.SHRPDecimal)
distState.DelegatorStartingInfos[i].StartingInfo.Height = 0
}
for i := 0; i < len(distState.ValidatorSlashEvents); i++ {
distState.ValidatorSlashEvents[i].Height = 0
}

distStateBz, err := cdc.MarshalJSON(distState)
if err != nil {
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
}
appState[distribution.ModuleName] = distStateBz

// Slashing module
var slashingState slashing.GenesisState
if appState[slashing.ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[slashing.ModuleName], &slashingState)
}

for k, v := range slashingState.SigningInfos {
v.StartHeight = 0
slashingState.SigningInfos[k] = v
}

slashingStateBz, err := cdc.MarshalJSON(slashingState)
if err != nil {
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
}
appState[slashing.ModuleName] = slashingStateBz

// Supply module
var supplyState supply.GenesisState
if appState[supply.ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[supply.ModuleName], &supplyState)
}

var supplyCoins sdk.Coins
for i := 0; i < supplyState.Supply.Len(); i++ {
coin := supplyState.Supply[i]
var newCoin sdk.Coin
if coin.Denom == "shr" {
newCoin = sdk.NewCoin(coin.Denom, coin.Amount.Mul(utils.SHRDecimal))
} else if coin.Denom == "shrp" {
newCoin = sdk.NewCoin(coin.Denom, coin.Amount.Mul(utils.SHRPDecimal))
}
supplyCoins = supplyCoins.Add(newCoin)
}
supplyState.Supply = supplyCoins

supplyStateBz, err := cdc.MarshalJSON(supplyState)
if err != nil {
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
}
appState[supply.ModuleName] = supplyStateBz

// Reset old id module
var identityState identity.GenesisState
identityStateBz, err := cdc.MarshalJSON(identityState)
if err != nil {
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
}
appState[oldId.ModuleName] = identityStateBz

appStateJSON, err := cdc.MarshalJSON(appState)
if err != nil {
return fmt.Errorf("failed to marshal application genesis state: %w", err)
}

genDoc.AppState = appStateJSON

// Update gendoc
// Validators info
for i := 0; i < len(genDoc.Validators); i++ {
genDoc.Validators[i].Power = genDoc.Validators[i].Power * powerMultiplier
}

return genutil.ExportGenesisFile(genDoc, outputFilePath)
}

func decimalize(input sdk.DecCoins) sdk.DecCoins {
var newCoins sdk.DecCoins
for i := 0; i < len(input); i++ {
coin := input[i]
var newCoin sdk.DecCoin
if coin.Denom == "shr" {
newCoin = sdk.NewDecCoinFromDec(coin.Denom, coin.Amount.MulInt(utils.SHRDecimal))
} else if coin.Denom == "shrp" {
newCoin = sdk.NewDecCoinFromDec(coin.Denom, coin.Amount.MulInt(utils.SHRPDecimal))
}
newCoins = newCoins.Add(newCoin)
}
return newCoins
}

func AddGenesisCustomMigrate(
ctx *server.Context, cdc *codec.Codec, defaultNodeHome, defaultClientHome string,
) *cobra.Command {

cmd := &cobra.Command{
Use: "custom-migrate <version> <old file> <new file> <merge file>",
Short: "Copy data from old genesis file the new one.",
Long: `Copy data from old genesis file the new one. This command generates merge file.`,
Use: "custom-migrate <version> <old file> <new file> <output file>",
Short: "Migrate genesis file.",
Long: `Copy data from old genesis file the new one. This command generates merge file.
version 0.0.1: custom-migrate 0.0.1 <old file> <new file> <output file>
version 1.1.0: custom-migrate 1.1.0 <old file> <output file>
`,
// Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
config := ctx.Config
Expand All @@ -83,6 +303,8 @@ func AddGenesisCustomMigrate(
switch v := args[0]; v {
case "0.0.1":
return FromV0_0_1(args[1], args[2], args[3], cdc)
case "1.1.0":
return FromV1_1_0(args[1], args[2], cdc)
default:
fmt.Println("No match version " + v)
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/shareledger/testnetCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/sharering/shareledger/x/electoral"
"github.com/sharering/shareledger/x/gentlemint"
"github.com/sharering/shareledger/x/myutils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/go-amino"
Expand All @@ -33,6 +32,8 @@ import (
tmos "github.com/tendermint/tendermint/libs/os"
"github.com/tendermint/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"

shareringUtils "github.com/ShareRing/modules/utils"
)

const (
Expand Down Expand Up @@ -326,7 +327,7 @@ func collectGenFiles(
func createKeyAndGenesisAccount(addr sdk.AccAddress, keyHome, keyName string, inBuf *bufio.Reader, power int64) (authexported.GenesisAccount, sdk.AccAddress, string, error) {
var err error
if addr == nil {
addr, err = myutils.CreateKeySeed(keyHome, keyName)
addr, err = shareringUtils.CreateKeySeed(keyHome, keyName)
if err != nil {
return nil, nil, "", err
}
Expand All @@ -352,11 +353,11 @@ func writeStakingGenTx(gentxsDir string, power int64, addr sdk.AccAddress, valPu
sdk.OneInt(),
)
tx := auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, []auth.StdSignature{}, peerAddr)
seed, err := myutils.GetKeeySeedFromFile(seedPath)
seed, err := shareringUtils.GetKeeySeedFromFile(seedPath)
if err != nil {
return err
}
_, txBldr, err := myutils.GetTxBldrAndCtxFromSeed(inBuf, cdc, seed)
_, txBldr, err := shareringUtils.GetTxBldrAndCtxFromSeed(inBuf, cdc, seed)
if err != nil {
return err
}
Expand Down

0 comments on commit 090eb20

Please sign in to comment.