Skip to content

Commit

Permalink
Merge pull request #48
Browse files Browse the repository at this point in the history
Add node begin block logic
  • Loading branch information
ironman0x7b2 authored Jul 1, 2020
2 parents f0edc49 + 19f8c18 commit a768649
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func NewApp(logger log.Logger, db db.DB, tracer io.Writer, latest bool, invarChe
)

// NOTE: order is very important here
app.manager.SetOrderBeginBlockers(mint.ModuleName, distribution.ModuleName, slashing.ModuleName)
app.manager.SetOrderBeginBlockers(mint.ModuleName, distribution.ModuleName, slashing.ModuleName, dvpn.ModuleName)
app.manager.SetOrderEndBlockers(crisis.ModuleName, gov.ModuleName, staking.ModuleName)
app.manager.SetOrderInitGenesis(
genaccounts.ModuleName, distribution.ModuleName, staking.ModuleName, auth.ModuleName,
Expand Down
7 changes: 7 additions & 0 deletions x/dvpn/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import (
"github.com/sentinel-official/hub/x/dvpn/types"
)

func BeginBlock(ctx sdk.Context, k keeper.Keeper) {
ctx, write := ctx.CacheContext()
defer write()

node.BeginBlock(ctx, k.Node)
}

func NewHandler(k keeper.Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
Expand Down
4 changes: 3 additions & 1 deletion x/dvpn/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ func (a AppModule) NewQuerierHandler() sdk.Querier {
return querier.NewQuerier(a.k)
}

func (a AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
func (a AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
BeginBlock(ctx, a.k)
}

func (a AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return nil
Expand Down
16 changes: 10 additions & 6 deletions x/dvpn/node/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ var (
NewGenesisState = types.NewGenesisState
DefaultGenesisState = types.DefaultGenesisState
NodeKey = types.NodeKey
NodeForProviderKeyPrefix = types.NodeForProviderKeyPrefix
NodeForProviderByProviderKey = types.NodeForProviderByProviderKey
NodeForProviderKey = types.NodeForProviderKey
ActiveNodeAtByTimeKey = types.ActiveNodeAtByTimeKey
ActiveNodeAtKey = types.ActiveNodeAtKey
NewMsgRegisterNode = types.NewMsgRegisterNode
NewMsgUpdateNode = types.NewMsgUpdateNode
NewMsgSetNodeStatus = types.NewMsgSetNodeStatus
Expand All @@ -60,11 +62,13 @@ var (
Querier = querier.Querier

// variable aliases
ModuleCdc = types.ModuleCdc
RouterKey = types.RouterKey
StoreKey = types.StoreKey
NodeKeyPrefix = types.NodeKeyPrefix
KeyInactiveDuration = types.KeyInactiveDuration
ModuleCdc = types.ModuleCdc
RouterKey = types.RouterKey
StoreKey = types.StoreKey
NodeKeyPrefix = types.NodeKeyPrefix
NodeForProviderKeyPrefix = types.NodeForProviderKeyPrefix
ActiveNodeAtKeyPrefix = types.ActiveNodeAtKeyPrefix
KeyInactiveDuration = types.KeyInactiveDuration
)

type (
Expand Down
16 changes: 16 additions & 0 deletions x/dvpn/node/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ import (
"github.com/sentinel-official/hub/x/dvpn/node/types"
)

func BeginBlock(ctx sdk.Context, k keeper.Keeper) {
end := ctx.BlockTime().Add(-1 * k.InactiveDuration(ctx))
k.IterateActiveNodes(ctx, end, func(_ int, node types.Node) (stop bool) {
k.DeleteActiveNodeAt(ctx, node.StatusAt, node.Address)

node.Status = hub.StatusInactive
node.StatusAt = ctx.BlockTime()
k.SetNode(ctx, node)

return false
})
}

func HandleRegisterNode(ctx sdk.Context, k keeper.Keeper, msg types.MsgRegisterNode) sdk.Result {
if !k.HasProvider(ctx, msg.Provider) {
return types.ErrorProviderDoesNotExist().Result()
Expand Down Expand Up @@ -103,6 +116,9 @@ func HandleSetNodeStatus(ctx sdk.Context, k keeper.Keeper, msg types.MsgSetNodeS
return types.ErrorNodeDoesNotExist().Result()
}

k.DeleteActiveNodeAt(ctx, node.StatusAt, node.Address)
k.SetActiveNodeAt(ctx, ctx.BlockTime(), node.Address)

node.Status = msg.Status
node.StatusAt = ctx.BlockTime()

Expand Down
37 changes: 36 additions & 1 deletion x/dvpn/node/keeper/node.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

hub "github.com/sentinel-official/hub/types"
Expand Down Expand Up @@ -68,7 +70,7 @@ func (k Keeper) GetNodes(ctx sdk.Context) (items types.Nodes) {
func (k Keeper) GetNodesForProvider(ctx sdk.Context, address hub.ProvAddress) (items types.Nodes) {
store := k.Store(ctx)

iter := sdk.KVStorePrefixIterator(store, types.NodeForProviderKeyPrefix(address))
iter := sdk.KVStorePrefixIterator(store, types.NodeForProviderByProviderKey(address))
defer iter.Close()

for ; iter.Valid(); iter.Next() {
Expand Down Expand Up @@ -98,3 +100,36 @@ func (k Keeper) IterateNodes(ctx sdk.Context, f func(index int, item types.Node)
i++
}
}

func (k Keeper) SetActiveNodeAt(ctx sdk.Context, at time.Time, address hub.NodeAddress) {
key := types.ActiveNodeAtKey(at, address)
value := k.cdc.MustMarshalBinaryLengthPrefixed(address)

store := k.Store(ctx)
store.Set(key, value)
}

func (k Keeper) DeleteActiveNodeAt(ctx sdk.Context, at time.Time, address hub.NodeAddress) {
key := types.ActiveNodeAtKey(at, address)

store := k.Store(ctx)
store.Delete(key)
}

func (k Keeper) IterateActiveNodes(ctx sdk.Context, end time.Time, f func(index int, item types.Node) (stop bool)) {
store := k.Store(ctx)

iter := store.Iterator(types.ActiveNodeAtKeyPrefix, sdk.PrefixEndBytes(types.ActiveNodeAtByTimeKey(end)))
defer iter.Close()

for i := 0; iter.Valid(); iter.Next() {
var address hub.NodeAddress
k.cdc.MustUnmarshalBinaryLengthPrefixed(iter.Value(), &address)

node, _ := k.GetNode(ctx, address)
if stop := f(i, node); stop {
break
}
i++
}
}
22 changes: 18 additions & 4 deletions x/dvpn/node/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package types

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

hub "github.com/sentinel-official/hub/types"
)

Expand All @@ -16,17 +20,27 @@ var (
)

var (
NodeKeyPrefix = []byte{0x00}
NodeKeyPrefix = []byte{0x00}
NodeForProviderKeyPrefix = []byte{0x01}
ActiveNodeAtKeyPrefix = []byte{0x02}
)

func NodeKey(address hub.NodeAddress) []byte {
return append(NodeKeyPrefix, address.Bytes()...)
}

func NodeForProviderKeyPrefix(address hub.ProvAddress) []byte {
return append([]byte{0x01}, address.Bytes()...)
func NodeForProviderByProviderKey(address hub.ProvAddress) []byte {
return append(NodeForProviderKeyPrefix, address.Bytes()...)
}

func NodeForProviderKey(p hub.ProvAddress, n hub.NodeAddress) []byte {
return append(NodeForProviderKeyPrefix(p), n.Bytes()...)
return append(NodeForProviderByProviderKey(p), n.Bytes()...)
}

func ActiveNodeAtByTimeKey(at time.Time) []byte {
return append(ActiveNodeAtKeyPrefix, sdk.FormatTimeBytes(at)...)
}

func ActiveNodeAtKey(at time.Time, address hub.NodeAddress) []byte {
return append(ActiveNodeAtByTimeKey(at), address.Bytes()...)
}
3 changes: 2 additions & 1 deletion x/dvpn/node/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func (m MsgRegisterNode) ValidateBasic() sdk.Error {
}

// Provider and Price both shouldn't nil at the same time
if m.Provider != nil && m.Price != nil {
if (m.Provider != nil && m.Price != nil) ||
(m.Provider == nil && m.Price == nil) {
return ErrorInvalidField("provider and price")
}

Expand Down

0 comments on commit a768649

Please sign in to comment.