Skip to content

Commit

Permalink
fixed resourceNodeCNT and metaNodeCNT issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hong-pang committed Jun 15, 2022
1 parent 84cfc28 commit 3327605
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 41 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module github.com/stratosnet/stratos-chain

go 1.17

replace google.golang.org/grpc => google.golang.org/grpc v1.33.2


require (
github.com/btcsuite/btcd v0.22.1
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
Expand Down
2 changes: 1 addition & 1 deletion x/register/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func UpdateMetaNodeStakeCmd() *cobra.Command {
// MetaNodeRegistrationVoteCmd Meta node registration need to be approved by 2/3 of existing meta nodes
func MetaNodeRegistrationVoteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "meta_node_reg_vote [flags]",
Use: "meta-node-reg-vote [flags]",
Short: "vote for the registration of a new meta node",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
Expand Down
6 changes: 3 additions & 3 deletions x/register/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data *types.GenesisState
}

// set initial genesis number of resource nodes
keeper.SetInitialGenesisBondedResourceNodeCnt(ctx, sdk.NewInt(lenOfGenesisBondedResourceNode))
keeper.SetBondedResourceNodeCnt(ctx, sdk.NewInt(lenOfGenesisBondedResourceNode))

lenOfGenesisBondedMetaNode := int64(0)
for _, metaNode := range data.GetMetaNodes() {
if metaNode.GetStatus() == stakingtypes.Bonded {
lenOfGenesisBondedResourceNode++
lenOfGenesisBondedMetaNode++
initialStakeTotal = initialStakeTotal.Add(metaNode.Tokens)
}
keeper.SetMetaNode(ctx, metaNode)
}
// set initial genesis number of meta nodes
keeper.SetInitialGenesisBondedMetaNodeCnt(ctx, sdk.NewInt(lenOfGenesisBondedMetaNode))
keeper.SetBondedMetaNodeCnt(ctx, sdk.NewInt(lenOfGenesisBondedMetaNode))

totalUnissuedPrepay := keeper.GetTotalUnissuedPrepay(ctx).Amount
initialUOzonePrice := sdk.ZeroDec()
Expand Down
59 changes: 30 additions & 29 deletions x/register/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (k *Keeper) SetHooks(sh types.RegisterHooks) *Keeper {

func (k Keeper) SetInitialUOzonePrice(ctx sdk.Context, price sdk.Dec) {
store := ctx.KVStore(k.storeKey)
b := types.ModuleCdc.MustMarshalLengthPrefixed(&price)
b := types.ModuleCdc.MustMarshalLengthPrefixed(price)
store.Set(types.InitialUOzonePriceKey, b)
}

Expand Down Expand Up @@ -507,7 +507,9 @@ func (k Keeper) UnbondResourceNode(
k.SetResourceNode(ctx, resourceNode)

// decrease resource node count
k.SetBondedResourceNodeCnt(ctx, sdk.NewInt(-1))
v := k.GetBondedResourceNodeCnt(ctx)
count := v.Sub(sdk.NewInt(1))
k.SetBondedResourceNodeCnt(ctx, count)
}

// set the unbonding mature time and completion height appropriately
Expand Down Expand Up @@ -558,7 +560,10 @@ func (k Keeper) UnbondMetaNode(
if amt.Equal(metaNode.Tokens) {
metaNode.Status = stakingtypes.Unbonding
// decrease meta node count
k.SetBondedMetaNodeCnt(ctx, sdk.NewInt(-1))
v := k.GetBondedMetaNodeCnt(ctx)
count := v.Sub(sdk.NewInt(1))
k.SetBondedMetaNodeCnt(ctx, count)
// set meta node
k.SetMetaNode(ctx, metaNode)
}

Expand Down Expand Up @@ -639,54 +644,50 @@ func (k Keeper) UozSupply(ctx sdk.Context) (remaining, total sdk.Int) {
return remaining, total
}

func (k Keeper) SetInitialGenesisBondedResourceNodeCnt(ctx sdk.Context, count sdk.Int) {
store := ctx.KVStore(k.storeKey)
b := types.ModuleCdc.MustMarshalLengthPrefixed(count)
store.Set(types.ResourceNodeCntKey, b)
}
//func (k Keeper) SetInitialGenesisBondedResourceNodeCnt(ctx sdk.Context, count sdk.Int) {
// store := ctx.KVStore(k.storeKey)
// b := types.ModuleCdc.MustMarshalLengthPrefixed(count)
// store.Set(types.ResourceNodeCntKey, b)
//}

func (k Keeper) SetInitialGenesisBondedMetaNodeCnt(ctx sdk.Context, count sdk.Int) {
store := ctx.KVStore(k.storeKey)
b := types.ModuleCdc.MustMarshalLengthPrefixed(count)
store.Set(types.MetaNodeCntKey, b)
}
//func (k Keeper) SetInitialGenesisBondedMetaNodeCnt(ctx sdk.Context, count sdk.Int) {
// store := ctx.KVStore(k.storeKey)
// b := types.ModuleCdc.MustMarshal(count)
// store.Set(types.MetaNodeCntKey, b)
//}

func (k Keeper) SetBondedResourceNodeCnt(ctx sdk.Context, delta sdk.Int) {
func (k Keeper) SetBondedResourceNodeCnt(ctx sdk.Context, count sdk.Int) {
store := ctx.KVStore(k.storeKey)
oldValue := store.Get(types.ResourceNodeCntKey)
var balance sdk.Int
types.ModuleCdc.MustUnmarshalLengthPrefixed(oldValue, &balance)
b := types.ModuleCdc.MustMarshalLengthPrefixed(balance.Add(delta))
b := types.ModuleCdc.MustMarshalLengthPrefixed(count)
//ctx.Logger().Info("count = " + count.String())
store.Set(types.ResourceNodeCntKey, b)
}

func (k Keeper) SetBondedMetaNodeCnt(ctx sdk.Context, delta sdk.Int) {
func (k Keeper) SetBondedMetaNodeCnt(ctx sdk.Context, count sdk.Int) {
store := ctx.KVStore(k.storeKey)
oldValue := store.Get(types.MetaNodeKey)
var balance sdk.Int
types.ModuleCdc.MustUnmarshalLengthPrefixed(oldValue, &balance)
b := types.ModuleCdc.MustMarshalLengthPrefixed(balance.Add(delta))
b := types.ModuleCdc.MustMarshalLengthPrefixed(count)
//ctx.Logger().Info("count = " + count.String())
store.Set(types.MetaNodeCntKey, b)
}

func (k Keeper) GetBondedResourceNodeCnt(ctx sdk.Context) sdk.Int {
func (k Keeper) GetBondedResourceNodeCnt(ctx sdk.Context) (balance sdk.Int) {
store := ctx.KVStore(k.storeKey)
value := store.Get(types.ResourceNodeCntKey)
if value == nil {
return sdk.ZeroInt()
}
var balance sdk.Int
types.ModuleCdc.MustUnmarshalLengthPrefixed(value, &balance)
return balance
//ctx.Logger().Info("balance = " + balance.String())
return
}

func (k Keeper) GetBondedMetaNodeCnt(ctx sdk.Context) sdk.Int {
func (k Keeper) GetBondedMetaNodeCnt(ctx sdk.Context) (balance sdk.Int) {
store := ctx.KVStore(k.storeKey)
value := store.Get(types.MetaNodeCntKey)
if value == nil {
return sdk.ZeroInt()
}
var balance sdk.Int
types.ModuleCdc.MustUnmarshalLengthPrefixed(value, &balance)
return balance
//ctx.Logger().Info("balance = " + balance.String())
return
}
4 changes: 3 additions & 1 deletion x/register/keeper/meta_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ func (k Keeper) HandleVoteForMetaNodeRegistration(ctx sdk.Context, nodeAddr stra
node.Suspend = false
k.SetMetaNode(ctx, node)
// increase mata node count
k.SetBondedMetaNodeCnt(ctx, sdk.NewInt(1))
v := k.GetBondedMetaNodeCnt(ctx)
count := v.Add(sdk.NewInt(1))
k.SetBondedMetaNodeCnt(ctx, count)
// move stake from not bonded pool to bonded pool
tokenToBond := sdk.NewCoin(k.BondDenom(ctx), node.Tokens)

Expand Down
11 changes: 9 additions & 2 deletions x/register/keeper/node_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,21 @@ func (k Keeper) bondedToUnbonding(ctx sdk.Context, node interface{}, isMetaNode
if temp.GetStatus() != stakingtypes.Bonded {
panic(fmt.Sprintf("bad state transition bondedToUnbonding, metaNode: %v\n", temp))
}
k.SetBondedMetaNodeCnt(ctx, sdk.NewInt(-1))
// decrease meta node count
v := k.GetBondedMetaNodeCnt(ctx)
count := v.Sub(sdk.NewInt(1))
k.SetBondedMetaNodeCnt(ctx, count)

return k.beginUnbondingMetaNode(ctx, &temp, &coin)
default:
temp := node.(types.ResourceNode)
if temp.GetStatus() != stakingtypes.Bonded {
panic(fmt.Sprintf("bad state transition bondedToUnbonding, resourceNode: %v\n", temp))
}
k.SetBondedResourceNodeCnt(ctx, sdk.NewInt(-1))
// decrease resource node count
v := k.GetBondedResourceNodeCnt(ctx)
count := v.Sub(sdk.NewInt(1))
k.SetBondedResourceNodeCnt(ctx, count)
return k.beginUnbondingResourceNode(ctx, &temp, &coin)
}
}
Expand Down
5 changes: 4 additions & 1 deletion x/register/keeper/resource_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ func (k Keeper) AddResourceNodeStake(ctx sdk.Context, resourceNode types.Resourc

k.SetResourceNode(ctx, resourceNode)
// increase resource node count
k.SetBondedResourceNodeCnt(ctx, sdk.NewInt(1))
v := k.GetBondedResourceNodeCnt(ctx)
count := v.Add(sdk.NewInt(1))
k.SetBondedResourceNodeCnt(ctx, count)

ozoneLimitChange = k.increaseOzoneLimitByAddStake(ctx, tokenToAdd.Amount)

return ozoneLimitChange, nil
Expand Down
9 changes: 5 additions & 4 deletions x/register/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ var (
SlashingPrefix = []byte{0x05}
InitialGenesisStakeTotalKey = []byte{0x06} // key of initial genesis deposit by all resource nodes and meta nodes at t=0
InitialUOzonePriceKey = []byte{0x07} // key of initial uoz price at t=0
MetaNodeCntKey = []byte{0x08} // the number of all meta nodes
ResourceNodeCntKey = []byte{0x09} // the number of all resource nodes

UBDNodeKey = []byte{0x11} // prefix for each key to an unbonding node
UBDNodeQueueKey = []byte{0x12} // prefix for the timestamps in unbonding node queue

UBDNodeKey = []byte{0x11} // prefix for each key to an unbonding node
UBDNodeQueueKey = []byte{0x12} // prefix for the timestamps in unbonding node queue
MetaNodeCntKey = []byte{0x13} // the number of all meta nodes
ResourceNodeCntKey = []byte{0x14} // the number of all resource nodes
)

// GetResourceNodeKey gets the key for the resourceNode with address
Expand Down

0 comments on commit 3327605

Please sign in to comment.