Skip to content

Commit

Permalink
moved CheckNftUsersExists and btClient initialization to endblocker i…
Browse files Browse the repository at this point in the history
…n nft module
  • Loading branch information
avendauz committed Aug 12, 2021
1 parent 3504e09 commit ce3edb7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 37 deletions.
6 changes: 3 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ func NewCRUDApp(


nftFileDir, _ := getNftFileDir(DefaultNodeHome)
nft2P2pPort, _ := getNftP2PPort(DefaultNodeHome)
nftP2PPort, _ := strconv.Atoi(nft2P2pPort)
nftP2PPortString, _ := getNftP2PPort(DefaultNodeHome)
nftP2PPort, _ := strconv.Atoi(nftP2PPortString)

msgBroadcaster := app.curiumKeeper.NewMsgBroadcaster(DefaultCLIHome, cdc)

Expand Down Expand Up @@ -444,7 +444,7 @@ func NewCRUDApp(
staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper),
oracle.NewAppModule(app.oracleKeeper),
aggregator.NewAppModule(app.aggKeeper),
nft.NewAppModule(*app.nftKeeper),
nft.NewAppModule(*app.nftKeeper, nftFileDir, nftP2PPort),
curium.NewAppModule(*app.curiumKeeper),
)

Expand Down
6 changes: 3 additions & 3 deletions x/nft/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func handleMsgCreateNft(goCtx sdk.Context, k keeper.Keeper, msg *types.MsgCreate


if _, err := os.Stat(k.HomeDir+"/nft/"+msg.Hash); err == nil {
metainfo, err := k.BtClient.TorrentFromFile(msg.Hash)
metainfo, err := k.GetBtClient().TorrentFromFile(msg.Hash)
if err != nil {
return nil, sdkerrors.New("nft", 2, fmt.Sprintf("unable to create torrent for file", msg.Hash))
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func handleMsgPublishFile(ctx sdk.Context, k Keeper, msg *types.MsgPublishFile)
k.Logger(ctx).Debug("Publish file message received", "id", msg.Id)
var metainfo metainfo.MetaInfo
bencode.DecodeBytes(msg.Metainfo, &metainfo)
k.BtClient.RetrieveFile(&metainfo)
k.GetBtClient().RetrieveFile(&metainfo)
k.EnsureNftDirExists()


Expand Down Expand Up @@ -122,7 +122,7 @@ func handleMsgRegisterPeer(ctx sdk.Context, k Keeper, msg *types.MsgRegisterPeer
store.Set([]byte(msg.Id), k.Cdc.MustMarshalBinaryBare(&peer))

if k.GetMyNodeId(ctx) != msg.Id {
k.BtClient.AddPeer(msg.Id, msg.Address, int(msg.Port))
k.GetBtClient().AddPeer(msg.Id, msg.Address, int(msg.Port))
}

return &sdk.Result{}, nil
Expand Down
2 changes: 1 addition & 1 deletion x/nft/keeper/createNft.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (


func (k Keeper) SeedFile(metainfo *metainfo.MetaInfo) error {
err := k.BtClient.SeedFile(metainfo)
err := k.btClient.SeedFile(metainfo)
if err != nil {
return err
}
Expand Down
24 changes: 12 additions & 12 deletions x/nft/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type (
HomeDir string
MsgBroadcaster curium.MsgBroadcaster
curiumKeeper *curium.Keeper
KeyringReader *keeper.KeyringReader
BtClient *torrentClient.TorrentClient
KeyringReader *keeper.KeyringReader
btClient *torrentClient.TorrentClient
}
)

Expand All @@ -41,13 +41,6 @@ func NewKeeper (
keyringReader *keeper.KeyringReader,

) *Keeper {
btClient, err := torrentClient.NewTorrentClient(btDirectory, btPort)
if checkNftUserExists(keyringReader) == false {
fmt.Println("***** nft user does not exist in keyring")
}
if err != nil {
fmt.Println("*****", err)
}

return &Keeper{
Cdc: cdc,
Expand All @@ -59,17 +52,24 @@ func NewKeeper (
MsgBroadcaster: msgBroadcaster,
curiumKeeper: curiumKeeper,
KeyringReader: keyringReader,
BtClient: btClient,
}
}

func (k Keeper) GetBtClient() (*torrentClient.TorrentClient) {
return k.btClient
}

func (k Keeper) SetBtClient(btClient *torrentClient.TorrentClient) {
k.btClient = btClient
}

func (k Keeper) GetCdc() *codec.Codec {
return k.Cdc
}

func checkNftUserExists(reader *curium.KeyringReader) bool {
func (k Keeper) CheckNftUserExists(reader *curium.KeyringReader) error {
_, err := reader.GetAddress("nft")
return err == nil
return err
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
Expand Down
60 changes: 42 additions & 18 deletions x/nft/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package nft

import (
"encoding/json"
keeper2 "github.com/bluzelle/curium/x/nft/keeper"
types2 "github.com/bluzelle/curium/x/nft/types"
nft "github.com/bluzelle/curium/x/nft/keeper"
nftTypes "github.com/bluzelle/curium/x/nft/types"
"github.com/bluzelle/curium/x/torrentClient"
"sync"

"github.com/gorilla/mux"
"github.com/spf13/cobra"
Expand All @@ -30,24 +32,24 @@ type AppModuleBasic struct{}

// Name returns the aggregator module's name.
func (AppModuleBasic) Name() string {
return types2.ModuleName
return nftTypes.ModuleName
}

// RegisterCodec registers the nft module's types for the given codec.
func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {
types2.RegisterCodec(cdc)
nftTypes.RegisterCodec(cdc)
}

// DefaultGenesis returns default genesis state as raw bytes for the nft
// module.
func (AppModuleBasic) DefaultGenesis() json.RawMessage {
return types2.ModuleCdc.MustMarshalJSON(types2.DefaultGenesisState())
return nftTypes.ModuleCdc.MustMarshalJSON(nftTypes.DefaultGenesisState())
}

// ValidateGenesis performs genesis state validation for the nft module.
func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error {
var data types2.GenesisState
err := types2.ModuleCdc.UnmarshalJSON(bz, &data)
var data nftTypes.GenesisState
err := nftTypes.ModuleCdc.UnmarshalJSON(bz, &data)
if err != nil {
return err
}
Expand All @@ -66,39 +68,42 @@ func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command {

// GetQueryCmd returns no root query command for the nft module.
func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command {
return cli.GetQueryCmd(types2.ModuleName, cdc)
return cli.GetQueryCmd(nftTypes.ModuleName, cdc)
}

//____________________________________________________________________________

// AppModule implements an application module for the aggregator module.
type AppModule struct {
AppModuleBasic

keeper keeper2.Keeper
keeper nft.Keeper
btDirectory string
btPort int
// TODO: Add keepers that your application depends on
}

// NewAppModule creates a new AppModule object
func NewAppModule(k keeper2.Keeper, /*TODO: Add Keepers that your application depends on*/) AppModule {
func NewAppModule(k nft.Keeper, btDirectory string, btPort int /*TODO: Add Keepers that your application depends on*/) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{},
keeper: k,
btDirectory: btDirectory,
btPort: btPort,
// TODO: Add keepers that your application depends on
}
}

// Name returns the nft module's name.
func (AppModule) Name() string {
return types2.ModuleName
return nftTypes.ModuleName
}

// RegisterInvariants registers the nft module invariants.
func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// Route returns the message routing key for the nft module.
func (AppModule) Route() string {
return types2.RouterKey
return nftTypes.RouterKey
}

// NewHandler returns an sdk.Handler for the nft module.
Expand All @@ -108,19 +113,19 @@ func (am AppModule) NewHandler() sdk.Handler {

// QuerierRoute returns the nft module's querier route name.
func (AppModule) QuerierRoute() string {
return types2.QuerierRoute
return nftTypes.QuerierRoute
}

// NewQuerierHandler returns the nft module sdk.Querier.
func (am AppModule) NewQuerierHandler() sdk.Querier {
return keeper2.NewQuerier(am.keeper)
return nft.NewQuerier(am.keeper)
}

// InitGenesis performs genesis initialization for the aggregator module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState types2.GenesisState
types2.ModuleCdc.MustUnmarshalJSON(data, &genesisState)
var genesisState nftTypes.GenesisState
nftTypes.ModuleCdc.MustUnmarshalJSON(data, &genesisState)
InitGenesis(ctx, am.keeper, genesisState)
return []abci.ValidatorUpdate{}
}
Expand All @@ -129,16 +134,35 @@ func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.Va
// module.
func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage {
gs := ExportGenesis(ctx, am.keeper)
return types2.ModuleCdc.MustMarshalJSON(gs)
return nftTypes.ModuleCdc.MustMarshalJSON(gs)
}

// BeginBlock returns the begin blocker for the nft module.
func (am AppModule) BeginBlock(_ sdk.Context, req abci.RequestBeginBlock) {
}

var once sync.Once

// EndBlock returns the end blocker for the nft module. It returns no validator
// updates.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {

once.Do(func () {
btClient, err := torrentClient.NewTorrentClient(am.btDirectory, am.btPort)

if err != nil {
am.keeper.Logger(ctx).Error("Error creating btClient", "btClient", err)
}

am.keeper.SetBtClient(btClient)
})

err := am.keeper.CheckNftUserExists(am.keeper.KeyringReader)

if err != nil {
am.keeper.Logger(ctx).Error("nft user does not exist in keyring", "nft", err)
}

if ctx.BlockHeight() > 10 {
if !peerRegistered {
go func() {
Expand Down

0 comments on commit ce3edb7

Please sign in to comment.