Skip to content

Commit

Permalink
Refactored node module
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Nov 28, 2022
1 parent d08a729 commit 08cc1d5
Show file tree
Hide file tree
Showing 7 changed files with 541 additions and 181 deletions.
14 changes: 7 additions & 7 deletions x/node/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ func EndBlock(ctx sdk.Context, k keeper.Keeper) []abcitypes.ValidatorUpdate {
k.IterateInactiveNodesAt(ctx, ctx.BlockTime(), func(_ int, item types.Node) bool {
log.Info("inactive node", "value", item)

itemAddress := item.GetAddress()
k.DeleteActiveNode(ctx, itemAddress)
k.SetInactiveNode(ctx, itemAddress)
nodeAddr := item.GetAddress()
k.DeleteActiveNode(ctx, nodeAddr)
k.SetInactiveNode(ctx, nodeAddr)

if item.Provider != "" {
itemProvider := item.GetProvider()
k.DeleteActiveNodeForProvider(ctx, itemProvider, itemAddress)
k.SetInactiveNodeForProvider(ctx, itemProvider, itemAddress)
provAddr := item.GetProvider()
k.DeleteActiveNodeForProvider(ctx, provAddr, nodeAddr)
k.SetInactiveNodeForProvider(ctx, provAddr, nodeAddr)
}

k.DeleteInactiveNodeAt(ctx, item.StatusAt.Add(inactiveDuration), itemAddress)
k.DeleteInactiveNodeAt(ctx, item.StatusAt.Add(inactiveDuration), nodeAddr)

item.Status = hubtypes.StatusInactive
item.StatusAt = ctx.BlockTime()
Expand Down
4 changes: 2 additions & 2 deletions x/node/keeper/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func (k *Keeper) FundCommunityPool(ctx sdk.Context, from sdk.AccAddress, coin sdk.Coin) error {
if coin.IsZero() {
if !coin.IsPositive() {
return nil
}

Expand All @@ -18,6 +18,6 @@ func (k *Keeper) HasProvider(ctx sdk.Context, address hubtypes.ProvAddress) bool
return k.provider.HasProvider(ctx, address)
}

func (k *Keeper) GetCountForNodeByProvider(ctx sdk.Context, p hubtypes.ProvAddress, n hubtypes.NodeAddress) uint64 {
func (k *Keeper) GetPlanCountForNodeByProvider(ctx sdk.Context, p hubtypes.ProvAddress, n hubtypes.NodeAddress) uint64 {
return k.plan.GetCountForNodeByProvider(ctx, p, n)
}
94 changes: 45 additions & 49 deletions x/node/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,52 +24,52 @@ func NewMsgServiceServer(keeper Keeper) types.MsgServiceServer {
func (k *msgServer) MsgRegister(c context.Context, msg *types.MsgRegisterRequest) (*types.MsgRegisterResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

msgFrom, err := sdk.AccAddressFromBech32(msg.From)
fromAddr, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return nil, err
}
if k.HasNode(ctx, msgFrom.Bytes()) {
if k.HasNode(ctx, fromAddr.Bytes()) {
return nil, types.ErrorDuplicateNode
}

if msg.Provider != "" {
msgProvider, err := hubtypes.ProvAddressFromBech32(msg.Provider)
provAddr, err := hubtypes.ProvAddressFromBech32(msg.Provider)
if err != nil {
return nil, err
}
if !k.HasProvider(ctx, msgProvider) {
if !k.HasProvider(ctx, provAddr) {
return nil, types.ErrorProviderDoesNotExist
}
}
if msg.Price != nil && !k.IsValidPrice(ctx, msg.Price) {
return nil, types.ErrorInvalidPrice
if msg.Price != nil {
if !k.IsValidPrice(ctx, msg.Price) {
return nil, types.ErrorInvalidPrice
}
}

deposit := k.Deposit(ctx)
if deposit.IsPositive() {
if err := k.FundCommunityPool(ctx, msgFrom, deposit); err != nil {
return nil, err
}
if err := k.FundCommunityPool(ctx, fromAddr, deposit); err != nil {
return nil, err
}

var (
nodeAddress = hubtypes.NodeAddress(msgFrom.Bytes())
node = types.Node{
Address: nodeAddress.String(),
nodeAddr = hubtypes.NodeAddress(fromAddr.Bytes())
node = types.Node{
Address: nodeAddr.String(),
Provider: msg.Provider,
Price: msg.Price,
RemoteURL: msg.RemoteURL,
Status: hubtypes.StatusInactive,
StatusAt: ctx.BlockTime(),
}
nodeProvider = node.GetProvider()
provAddr = node.GetProvider()
)

k.SetNode(ctx, node)
k.SetInactiveNode(ctx, nodeAddress)
k.SetInactiveNode(ctx, nodeAddr)

if nodeProvider != nil {
k.SetInactiveNodeForProvider(ctx, nodeProvider, nodeAddress)
if provAddr != nil {
k.SetInactiveNodeForProvider(ctx, provAddr, nodeAddr)
}

ctx.EventManager().EmitTypedEvent(
Expand All @@ -85,58 +85,54 @@ func (k *msgServer) MsgRegister(c context.Context, msg *types.MsgRegisterRequest
func (k *msgServer) MsgUpdate(c context.Context, msg *types.MsgUpdateRequest) (*types.MsgUpdateResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

msgFrom, err := hubtypes.NodeAddressFromBech32(msg.From)
fromAddr, err := hubtypes.NodeAddressFromBech32(msg.From)
if err != nil {
return nil, err
}

node, found := k.GetNode(ctx, msgFrom)
node, found := k.GetNode(ctx, fromAddr)
if !found {
return nil, types.ErrorNodeDoesNotExist
}

if node.Provider == msg.Provider {
if msg.Provider == node.Provider {
msg.Provider = ""
}

if node.Provider != "" && (msg.Provider != "" || msg.Price != nil) {
if (msg.Provider != "" || msg.Price != nil) && node.Provider != "" {
var (
nodeAddress = node.GetAddress()
nodeProvider = node.GetProvider()
nodeAddr = node.GetAddress()
provAddr = node.GetProvider()
)

if k.GetCountForNodeByProvider(ctx, nodeProvider, nodeAddress) > 0 {
if k.GetPlanCountForNodeByProvider(ctx, provAddr, nodeAddr) > 0 {
return nil, types.ErrorInvalidPlanCount
}

if node.Status.Equal(hubtypes.StatusActive) {
k.DeleteActiveNodeForProvider(ctx, nodeProvider, nodeAddress)
k.DeleteActiveNodeForProvider(ctx, provAddr, nodeAddr)
} else {
k.DeleteInactiveNodeForProvider(ctx, nodeProvider, nodeAddress)
k.DeleteInactiveNodeForProvider(ctx, provAddr, nodeAddr)
}
}

if msg.Provider != "" {
msgProvider, err := hubtypes.ProvAddressFromBech32(msg.Provider)
provAddr, err := hubtypes.ProvAddressFromBech32(msg.Provider)
if err != nil {
return nil, err
}
if !k.HasProvider(ctx, msgProvider) {
if !k.HasProvider(ctx, provAddr) {
return nil, types.ErrorProviderDoesNotExist
}

node.Price = nil
node.Provider = msg.Provider

var (
nodeAddress = node.GetAddress()
nodeProvider = node.GetProvider()
)

nodeAddr := node.GetAddress()
if node.Status.Equal(hubtypes.StatusActive) {
k.SetActiveNodeForProvider(ctx, nodeProvider, nodeAddress)
k.SetActiveNodeForProvider(ctx, provAddr, nodeAddr)
} else {
k.SetInactiveNodeForProvider(ctx, nodeProvider, nodeAddress)
k.SetInactiveNodeForProvider(ctx, provAddr, nodeAddr)
}
}
if msg.Price != nil {
Expand Down Expand Up @@ -165,42 +161,42 @@ func (k *msgServer) MsgUpdate(c context.Context, msg *types.MsgUpdateRequest) (*
func (k *msgServer) MsgSetStatus(c context.Context, msg *types.MsgSetStatusRequest) (*types.MsgSetStatusResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

msgFrom, err := hubtypes.NodeAddressFromBech32(msg.From)
fromAddr, err := hubtypes.NodeAddressFromBech32(msg.From)
if err != nil {
return nil, err
}

node, found := k.GetNode(ctx, msgFrom)
node, found := k.GetNode(ctx, fromAddr)
if !found {
return nil, types.ErrorNodeDoesNotExist
}

var (
nodeAddress = node.GetAddress()
nodeProvider = node.GetProvider()
nodeAddr = node.GetAddress()
provAddr = node.GetProvider()
inactiveDuration = k.InactiveDuration(ctx)
)

if node.Status.Equal(hubtypes.StatusActive) {
if msg.Status.Equal(hubtypes.StatusInactive) {
k.DeleteActiveNode(ctx, nodeAddress)
k.SetInactiveNode(ctx, nodeAddress)
k.DeleteActiveNode(ctx, nodeAddr)
k.SetInactiveNode(ctx, nodeAddr)

if node.Provider != "" {
k.DeleteActiveNodeForProvider(ctx, nodeProvider, nodeAddress)
k.SetInactiveNodeForProvider(ctx, nodeProvider, nodeAddress)
k.DeleteActiveNodeForProvider(ctx, provAddr, nodeAddr)
k.SetInactiveNodeForProvider(ctx, provAddr, nodeAddr)
}
}

k.DeleteInactiveNodeAt(ctx, node.StatusAt.Add(inactiveDuration), nodeAddress)
k.DeleteInactiveNodeAt(ctx, node.StatusAt.Add(inactiveDuration), nodeAddr)
} else {
if msg.Status.Equal(hubtypes.StatusActive) {
k.DeleteInactiveNode(ctx, nodeAddress)
k.SetActiveNode(ctx, nodeAddress)
k.DeleteInactiveNode(ctx, nodeAddr)
k.SetActiveNode(ctx, nodeAddr)

if node.Provider != "" {
k.DeleteInactiveNodeForProvider(ctx, nodeProvider, nodeAddress)
k.SetActiveNodeForProvider(ctx, nodeProvider, nodeAddress)
k.DeleteInactiveNodeForProvider(ctx, provAddr, nodeAddr)
k.SetActiveNodeForProvider(ctx, provAddr, nodeAddr)
}
}
}
Expand All @@ -209,7 +205,7 @@ func (k *msgServer) MsgSetStatus(c context.Context, msg *types.MsgSetStatusReque
node.StatusAt = ctx.BlockTime()

if node.Status.Equal(hubtypes.StatusActive) {
k.SetInactiveNodeAt(ctx, node.StatusAt.Add(inactiveDuration), nodeAddress)
k.SetInactiveNodeAt(ctx, node.StatusAt.Add(inactiveDuration), nodeAddr)
}

k.SetNode(ctx, node)
Expand Down
10 changes: 5 additions & 5 deletions x/node/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func RandomizedGenesisState(state *module.SimulationState) *types.GenesisState {
func(r *rand.Rand) {
deposit = sdk.NewInt64Coin(
sdk.DefaultBondDenom,
r.Int63n(MaxAmount),
r.Int63n(MaxInt),
)
},
)
Expand All @@ -37,7 +37,7 @@ func RandomizedGenesisState(state *module.SimulationState) *types.GenesisState {
&inactiveDuration,
state.Rand,
func(r *rand.Rand) {
inactiveDuration = time.Duration(r.Int63n(MaxInactiveDuration)) * time.Millisecond
inactiveDuration = time.Duration(r.Int63n(MaxInt)) * time.Millisecond
},
)
state.AppParams.GetOrGenerate(
Expand All @@ -49,7 +49,7 @@ func RandomizedGenesisState(state *module.SimulationState) *types.GenesisState {
maxPrice = sdk.NewCoins(
sdk.NewInt64Coin(
sdk.DefaultBondDenom,
r.Int63n(MaxAmount),
r.Int63n(MaxInt),
),
)
},
Expand All @@ -63,7 +63,7 @@ func RandomizedGenesisState(state *module.SimulationState) *types.GenesisState {
minPrice = sdk.NewCoins(
sdk.NewInt64Coin(
sdk.DefaultBondDenom,
r.Int63n(MaxAmount),
r.Int63n(MaxInt),
),
)
},
Expand All @@ -75,7 +75,7 @@ func RandomizedGenesisState(state *module.SimulationState) *types.GenesisState {
state.Rand,
func(r *rand.Rand) {
stakingShare = sdk.NewDecWithPrec(
r.Int63n(MaxAmount),
r.Int63n(MaxInt),
6,
)
},
Expand Down
41 changes: 37 additions & 4 deletions x/node/simulation/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
)

const (
MaxAmount = 1 << 18
MaxInactiveDuration = 1 << 18
MaxInt = 1 << 18
)

func ParamChanges(_ *rand.Rand) []simulationtypes.ParamChange {
Expand All @@ -25,7 +24,7 @@ func ParamChanges(_ *rand.Rand) []simulationtypes.ParamChange {
func(r *rand.Rand) string {
return sdk.NewInt64Coin(
sdk.DefaultBondDenom,
r.Int63n(MaxAmount),
r.Int63n(MaxInt),
).String()
},
),
Expand All @@ -35,9 +34,43 @@ func ParamChanges(_ *rand.Rand) []simulationtypes.ParamChange {
func(r *rand.Rand) string {
return fmt.Sprintf(
"%s",
time.Duration(r.Int63n(MaxInactiveDuration))*time.Millisecond,
time.Duration(r.Int63n(MaxInt))*time.Millisecond,
)
},
),
simulation.NewSimParamChange(
types.ModuleName,
string(types.KeyMaxPrice),
func(r *rand.Rand) string {
return sdk.NewCoins(
sdk.NewInt64Coin(
sdk.DefaultBondDenom,
r.Int63n(MaxInt),
),
).String()
},
),
simulation.NewSimParamChange(
types.ModuleName,
string(types.KeyMinPrice),
func(r *rand.Rand) string {
return sdk.NewCoins(
sdk.NewInt64Coin(
sdk.DefaultBondDenom,
r.Int63n(MaxInt),
),
).String()
},
),
simulation.NewSimParamChange(
types.ModuleName,
string(types.KeyStakingShare),
func(r *rand.Rand) string {
return sdk.NewDecWithPrec(
MaxInt,
6,
).String()
},
),
}
}
Loading

0 comments on commit 08cc1d5

Please sign in to comment.