Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: layer2 module #247

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (

"github.com/CosmWasm/wasmd/x/wasm"

distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"

appante "github.com/bianjieai/irita/app/ante"
evmmodule "github.com/bianjieai/irita/modules/evm"
"github.com/bianjieai/irita/modules/evm/crypto"
evmutils "github.com/bianjieai/irita/modules/evm/utils"
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
layer2module "github.com/bianjieai/irita/modules/layer2"

"github.com/spf13/cast"

Expand Down Expand Up @@ -99,6 +101,9 @@ import (
"github.com/bianjieai/iritamod/modules/identity"
identitykeeper "github.com/bianjieai/iritamod/modules/identity/keeper"
identitytypes "github.com/bianjieai/iritamod/modules/identity/types"
"github.com/bianjieai/iritamod/modules/layer2"
layer2keeper "github.com/bianjieai/iritamod/modules/layer2/keeper"
layer2types "github.com/bianjieai/iritamod/modules/layer2/types"
"github.com/bianjieai/iritamod/modules/node"
nodekeeper "github.com/bianjieai/iritamod/modules/node/keeper"
nodetypes "github.com/bianjieai/iritamod/modules/node/types"
Expand Down Expand Up @@ -179,6 +184,7 @@ var (
tibcnfttransfer.AppModuleBasic{},
tibcmttransfer.AppModuleBasic{},
wasm.AppModuleBasic{},
layer2.AppModuleBasic{},

// evm
evm.AppModuleBasic{},
Expand Down Expand Up @@ -263,6 +269,7 @@ type IritaApp struct {
feeGrantKeeper feegrantkeeper.Keeper
capabilityKeeper *capabilitykeeper.Keeper
wasmKeeper wasm.Keeper
layer2Keeper layer2keeper.Keeper
// tibc
scopedTIBCKeeper capabilitykeeper.ScopedKeeper
scopedTIBCMockKeeper capabilitykeeper.ScopedKeeper
Expand Down Expand Up @@ -325,7 +332,7 @@ func NewIritaApp(
tibcnfttypes.StoreKey,
tibcmttypes.StoreKey,
wasm.StoreKey,

layer2types.StoreKey,
// evm
evmtypes.StoreKey, feemarkettypes.StoreKey,
)
Expand Down Expand Up @@ -410,6 +417,11 @@ func NewIritaApp(
app.GetSubspace(opbtypes.ModuleName),
)

layer2NFTKeeper := layer2module.NewNftKeeper(appCodec, app.nftKeeper)
app.layer2Keeper = layer2keeper.NewKeeper(
appCodec, keys[layer2types.StoreKey],
app.permKeeper, layer2NFTKeeper)

// evm
tracer := cast.ToString(appOpts.Get(srvflags.EVMTracer))

Expand Down Expand Up @@ -508,6 +520,7 @@ func NewIritaApp(
opb.NewAppModule(appCodec, app.opbKeeper),
tibc.NewAppModule(app.tibcKeeper),
wasm.NewAppModule(appCodec, &app.wasmKeeper, app.nodeKeeper),
layer2.NewAppModule(appCodec, app.layer2Keeper),
nfttransferModule,
mttransferModule,
// evm
Expand Down Expand Up @@ -544,6 +557,7 @@ func NewIritaApp(
tibcnfttypes.ModuleName,
tibcmttypes.ModuleName,
wasm.ModuleName,
layer2types.ModuleName,

// evm
evmtypes.ModuleName, feemarkettypes.ModuleName,
Expand Down Expand Up @@ -573,6 +587,7 @@ func NewIritaApp(
tibcnfttypes.ModuleName,
tibcmttypes.ModuleName,
wasm.ModuleName,
layer2types.ModuleName,

// evm
evmtypes.ModuleName, feemarkettypes.ModuleName,
Expand Down Expand Up @@ -608,6 +623,7 @@ func NewIritaApp(
tibcnfttypes.ModuleName,
tibcmttypes.ModuleName,
wasm.ModuleName,
layer2types.ModuleName,

// evm
evmtypes.ModuleName, feemarkettypes.ModuleName,
Expand Down Expand Up @@ -638,6 +654,7 @@ func NewIritaApp(
tibcnfttypes.ModuleName,
tibcmttypes.ModuleName,
wasm.ModuleName,
layer2types.ModuleName,

// evm
evmtypes.ModuleName, feemarkettypes.ModuleName,
Expand Down Expand Up @@ -765,6 +782,15 @@ func NewIritaApp(
},
)

app.RegisterUpgradePlan(
"v3.4.0-wenchangchain-tianzhou", store.StoreUpgrades{
Added: []string{layer2types.StoreKey},
},
func(ctx sdk.Context, plan sdkupgrade.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
},
)

// set peer filter by node ID
app.SetIDPeerFilter(app.nodeKeeper.FilterNodeByID)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/99designs/keyring v1.1.6
github.com/CosmWasm/wasmd v0.0.0-00010101000000-000000000000
github.com/bianjieai/iritamod v1.3.0
github.com/bianjieai/iritamod v1.2.1-0.20230519095601-fac7a7e36ad5
github.com/bianjieai/tibc-go v0.3.1-0.20220412124234-9b75094ff868
github.com/cosmos/cosmos-sdk v0.45.2
github.com/cosmos/go-bip39 v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,8 @@ github.com/bianjieai/ethermint v0.10.2-irita-20230315 h1:OzTtBrRh42b6GqBY/0ec9Nf
github.com/bianjieai/ethermint v0.10.2-irita-20230315/go.mod h1:wY1g0XkLenjcgfuok56QprJPZk8/1Z2DKiDsjqnZWso=
github.com/bianjieai/irismod v1.6.1 h1:ymCZUNt/h/QbT/pFEyjxm/kN2W4sj70im0TpFfe71+o=
github.com/bianjieai/irismod v1.6.1/go.mod h1:EgSmw43lxN13U37IncMc03Ram7AJuXBsyi8juy7ndEY=
github.com/bianjieai/iritamod v1.3.0 h1:tOV1W9Ozz5zZoXQxY2ZY7nEJG2Fk5/465/51hx7XQbA=
github.com/bianjieai/iritamod v1.3.0/go.mod h1:6WLWIGKdgUcJaN7ym6FqSdr80OMW7CP7jT/+u549scU=
github.com/bianjieai/iritamod v1.2.1-0.20230519095601-fac7a7e36ad5 h1:uEz4fNLwRy0t62EW68tJH4ULErf9bYVzhmf0K68Z+EI=
github.com/bianjieai/iritamod v1.2.1-0.20230519095601-fac7a7e36ad5/go.mod h1:5woISMgARzwIysP3WFP5WZ2eod2dDcAGgRGVfpuT08Q=
github.com/bianjieai/tendermint v0.34.8-irita-210413.0.20211012090339-cee6e09e8ae3 h1:M4C+ilCPiP2c2yMdeYaZLwquEBfFa4Uvt1BDviSYD0E=
github.com/bianjieai/tendermint v0.34.8-irita-210413.0.20211012090339-cee6e09e8ae3/go.mod h1:2u6xthTnwDYLhO38cDYp8CxzKq7hwJFvCXeK1pEErtM=
github.com/bianjieai/tibc-go v0.3.1-0.20220412124234-9b75094ff868 h1:dPS+AcwCQsWTMXRgZwO+Yy2rY8y9G+X38MA7toAivok=
Expand Down
29 changes: 29 additions & 0 deletions modules/layer2/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package layer2

import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
nftkeeper "github.com/irisnet/irismod/modules/nft/keeper"
)

type (
NftKeeper struct {
cdc codec.Codec
nk nftkeeper.Keeper
}

NftToken struct {
Owner sdk.AccAddress
}

NftClass struct {
ClassId string
Owner string
MintRestricted bool
}
)

func (c NftClass) GetID() string { return c.ClassId }
func (c NftClass) GetOwner() string { return c.Owner }
func (c NftClass) GetMintRestricted() bool { return c.MintRestricted }
func (t NftToken) GetOwner() sdk.AccAddress { return t.Owner }
109 changes: 109 additions & 0 deletions modules/layer2/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package layer2

import (
"errors"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
nftkeeper "github.com/irisnet/irismod/modules/nft/keeper"

layer2nft "github.com/bianjieai/iritamod/modules/layer2/types"
)

func NewNftKeeper(cdc codec.Codec, nk nftkeeper.Keeper) NftKeeper {
return NftKeeper{
cdc: cdc,
nk: nk,
}
}

func (l2 NftKeeper) SaveNFT(ctx sdk.Context,
classID,
tokenID,
tokenNm,
tokenURI,
tokenUriHash,
tokenData string,
receiver sdk.AccAddress) error {
return l2.nk.MintNFT(ctx, classID, tokenID, tokenNm, tokenURI, tokenUriHash, tokenData, receiver)
}

func (l2 NftKeeper) UpdateNFT(ctx sdk.Context,
classID,
tokenID,
tokenNm,
tokenURI,
tokenUriHash,
tokenData string,
owner sdk.AccAddress) error {
return l2.nk.EditNFT(ctx, classID, tokenID, tokenNm, tokenURI, tokenUriHash, tokenData, owner)
}

func (l2 NftKeeper) RemoveNFT(ctx sdk.Context,
classID,
tokenID string,
owner sdk.AccAddress) error {
return l2.nk.BurnNFT(ctx, classID, tokenID, owner)
}

func (l2 NftKeeper) TransferNFT(ctx sdk.Context,
classID,
tokenID string,
srcOwner,
dstOwner sdk.AccAddress) error {
nft, err := l2.nk.GetNFT(ctx, classID, tokenID)
if err != nil {
return err
}

return l2.nk.TransferOwner(ctx, classID, tokenID, nft.GetName(), nft.GetURI(), nft.GetURIHash(), nft.GetData(), srcOwner, dstOwner)
}

func (l2 NftKeeper) TransferClass(ctx sdk.Context,
classID string,
srcOwner,
dstOwner sdk.AccAddress) error {
return l2.nk.TransferDenomOwner(ctx, classID, srcOwner, dstOwner)
}

func (l2 NftKeeper) UpdateClassMintRestricted(ctx sdk.Context,
classID string,
mintRestricted bool,
owner sdk.AccAddress) error {
denom, exist := l2.nk.GetDenom(ctx, classID)
if !exist {
return errors.New("class not found")
}

denom.MintRestricted = mintRestricted
denom.Creator = owner.String()

return l2.nk.UpdateDenom(ctx, denom)
}

func (l2 NftKeeper) GetClass(ctx sdk.Context,
classID string) (layer2nft.Class, error) {
denom, exist := l2.nk.GetDenom(ctx, classID)
if !exist {
return nil, errors.New("class not found")
}

return NftClass{
ClassId: denom.Id,
Owner: denom.Creator,
MintRestricted: denom.MintRestricted,
}, nil
}

func (l2 NftKeeper) GetNFT(ctx sdk.Context,
classID,
tokenID string) (layer2nft.NFT, error) {
token, err := l2.nk.GetNFT(ctx, classID, tokenID)
if err != nil {
return nil, err
}

return NftToken{
Owner: token.GetOwner(),
}, nil
}