Skip to content

Commit

Permalink
Merge pull request Lorenzo-Protocol#23 from Lorenzo-Protocol/feature/…
Browse files Browse the repository at this point in the history
…implement-agent-module

feat: implement agent module
  • Loading branch information
sheldonleedev authored Jun 13, 2024
2 parents 5fa29d7 + 2a7cb75 commit c0d5508
Show file tree
Hide file tree
Showing 31 changed files with 7,135 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ import (

appparams "github.com/Lorenzo-Protocol/lorenzo/app/params"
lrztypes "github.com/Lorenzo-Protocol/lorenzo/types"
agentkeeper "github.com/Lorenzo-Protocol/lorenzo/x/agent/keeper"
agenttypes "github.com/Lorenzo-Protocol/lorenzo/x/agent/types"
btclightclientkeeper "github.com/Lorenzo-Protocol/lorenzo/x/btclightclient/keeper"
btclightclienttypes "github.com/Lorenzo-Protocol/lorenzo/x/btclightclient/types"
feekeeper "github.com/Lorenzo-Protocol/lorenzo/x/fee/keeper"
Expand Down Expand Up @@ -159,6 +161,7 @@ type LorenzoApp struct {

BTCLightClientKeeper btclightclientkeeper.Keeper
FeeKeeper *feekeeper.Keeper
AgentKeeper agentkeeper.Keeper

BTCStakingKeeper btcstakingkeeper.Keeper

Expand Down Expand Up @@ -250,6 +253,7 @@ func NewLorenzoApp(
btclightclienttypes.StoreKey,
feetypes.StoreKey,
btcstakingtypes.StoreKey,
agenttypes.StoreKey,
)

tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey, evmtypes.TransientKey, feemarkettypes.TransientKey)
Expand Down Expand Up @@ -465,6 +469,13 @@ func NewLorenzoApp(
keys[feetypes.StoreKey],
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

app.AgentKeeper = agentkeeper.NewKeeper(
appCodec,
keys[agenttypes.StoreKey],
app.BTCLightClientKeeper,
)

app.BTCStakingKeeper = btcstakingkeeper.NewKeeper(appCodec, keys[btcstakingtypes.StoreKey], app.BTCLightClientKeeper, app.BankKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String())

var transferStack ibcporttypes.IBCModule
Expand Down
112 changes: 112 additions & 0 deletions app/helpers/test_helper.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package helpers

import (
"bytes"
"encoding/json"
"strconv"
"testing"
"time"

Expand All @@ -15,6 +17,7 @@ import (
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtypes "github.com/cometbft/cometbft/types"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand Down Expand Up @@ -53,14 +56,23 @@ var DefaultConsensusParams = &tmproto.ConsensusParams{
},
}

// PV implements PrivValidator interface
type PV struct {
PrivKey cryptotypes.PrivKey
}

// EmptyAppOptions is a stub implementing AppOptions
type EmptyAppOptions struct{}

// Get implements AppOptions
func (EmptyAppOptions) Get(_ string) interface{} { return nil }

// Setup initializes and returns a new instance of app.LorenzoApp for testing purposes.
//
// Parameter:
// - t: A testing.T instance for testing purposes.
// Returns:
// - *app.LorenzoApp: The initialized instance of app.LorenzoApp.
func Setup(t *testing.T) *app.LorenzoApp {
t.Helper()

Expand All @@ -84,6 +96,66 @@ func Setup(t *testing.T) *app.LorenzoApp {
return SetupWithGenesisValSet(t, valSet, genesisAccounts, balance)
}

// SetupWithGenesisMergeFn initializes a new LorenzoApp with override module genesis data.
//
// Parameters:
// - t: A testing.T instance for testing purposes.
// - merge: A function to merge override module genesis data with the default genesis data.
// Returns:
// - *app.LorenzoApp: The initialized instance of app.LorenzoApp.
func SetupWithGenesisMergeFn(t *testing.T, merge func(cdc codec.Codec, state map[string]json.RawMessage)) *app.LorenzoApp {
t.Helper()

privVal := mock.NewPV()
pubKey, err := privVal.GetPubKey()
require.NoError(t, err)
// create validator set with single validator
validator := tmtypes.NewValidator(pubKey, 1)
valSet := tmtypes.NewValidatorSet([]*tmtypes.Validator{validator})

// generate genesis account
senderPrivKey := mock.NewPV()
senderPubKey := senderPrivKey.PrivKey.PubKey()

acc := authtypes.NewBaseAccount(senderPubKey.Address().Bytes(), senderPubKey, 0, 0)
balance := banktypes.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))),
}
genAccs := []authtypes.GenesisAccount{acc}

lorenzoApp, genesisState := setup()
if merge != nil {
merge(lorenzoApp.AppCodec(), genesisState)
}
genesisState = genesisStateWithValSet(t, lorenzoApp, genesisState, valSet, genAccs, balance)

stateBytes, err := json.MarshalIndent(genesisState, "", " ")
require.NoError(t, err)

// init chain will set the validator set and initialize the genesis accounts
lorenzoApp.InitChain(
abci.RequestInitChain{
ChainId: SimAppChainID,
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)

// commit genesis changes
lorenzoApp.Commit()
lorenzoApp.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{
ChainID: SimAppChainID,
Height: lorenzoApp.LastBlockHeight() + 1,
AppHash: lorenzoApp.LastCommitID().Hash,
ValidatorsHash: valSet.Hash(),
NextValidatorsHash: valSet.Hash(),
}})

return lorenzoApp
}

// SetupWithGenesisValSet initializes a new LorenzoApp with a validator set and genesis accounts
// that also act as delegators. For simplicity, each validator is bonded with a delegation
// of one consensus engine unit in the default token of the LorenzoApp from first genesis
Expand Down Expand Up @@ -120,6 +192,25 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs
return lorenzoApp
}

// CreateTestAddrs creates test addresses
func CreateTestAddrs(numAddrs int) []sdk.AccAddress {
var addresses []sdk.AccAddress
var buffer bytes.Buffer

// start at 100 so we can make up to 999 test addresses with valid test addresses
for i := 100; i < (numAddrs + 100); i++ {
numString := strconv.Itoa(i)
buffer.WriteString("A58856F0FD53BF058B4909A21AEC019107BA6") //base address string

buffer.WriteString(numString) //adding on final two digits to make addresses unique
res, _ := sdk.AccAddressFromHexUnsafe(buffer.String())
bech := res.String()
addresses = append(addresses, testAddr(buffer.String(), bech))
buffer.Reset()
}
return addresses
}

func setup() (*app.LorenzoApp, app.GenesisState) {
db := dbm.NewMemDB()
appOptions := make(simtestutil.AppOptionsMap)
Expand Down Expand Up @@ -207,3 +298,24 @@ func genesisStateWithValSet(t *testing.T,

return genesisState
}

func testAddr(addr string, bech string) sdk.AccAddress {
res, err := sdk.AccAddressFromHexUnsafe(addr)
if err != nil {
panic(err)
}
bechexpected := res.String()
if bech != bechexpected {
panic("Bech encoding doesn't match reference")
}

bechres, err := sdk.AccAddressFromBech32(bech)
if err != nil {
panic(err)
}
if !bytes.Equal(bechres, res) {
panic("Bech decode and hex decode don't match")
}

return res
}
7 changes: 7 additions & 0 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package app

import (
appparams "github.com/Lorenzo-Protocol/lorenzo/app/params"
"github.com/Lorenzo-Protocol/lorenzo/x/agent"
agenttypes "github.com/Lorenzo-Protocol/lorenzo/x/agent/types"
"github.com/Lorenzo-Protocol/lorenzo/x/btclightclient"
btclightclienttypes "github.com/Lorenzo-Protocol/lorenzo/x/btclightclient/types"
"github.com/Lorenzo-Protocol/lorenzo/x/btcstaking"
Expand Down Expand Up @@ -108,6 +110,7 @@ var (
btclightclient.AppModuleBasic{},
fee.AppModuleBasic{},
btcstaking.AppModuleBasic{},
agent.AppModuleBasic{},
)
// module account permissions
maccPerms = map[string][]string{
Expand Down Expand Up @@ -203,6 +206,7 @@ func appModules(
btclightclient.NewAppModule(appCodec, app.BTCLightClientKeeper),
fee.NewAppModule(appCodec, app.FeeKeeper),
btcstaking.NewAppModule(appCodec, app.BTCStakingKeeper),
agent.NewAppModule(appCodec, app.AgentKeeper),

// this line is used by starport scaffolding # stargate/app/appModule

Expand Down Expand Up @@ -247,6 +251,7 @@ func orderBeginBlockers() []string {
vestingtypes.ModuleName,
btclightclienttypes.ModuleName,
feetypes.ModuleName,
agenttypes.ModuleName,
//self module
btcstakingtypes.ModuleName,
}
Expand Down Expand Up @@ -284,6 +289,7 @@ func orderEndBlockers() []string {
vestingtypes.ModuleName,
btclightclienttypes.ModuleName,
feetypes.ModuleName,
agenttypes.ModuleName,
//self module
btcstakingtypes.ModuleName,
}
Expand Down Expand Up @@ -326,6 +332,7 @@ func orderInitBlockers() []string {
//self module
btclightclienttypes.ModuleName,
btcstakingtypes.ModuleName,
agenttypes.ModuleName,

// NOTE: crisis module must go at the end to check for invariants on each module
crisistypes.ModuleName,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/cosmos/cosmos-proto v1.0.0-beta.5
github.com/cosmos/gogoproto v1.4.10
github.com/pkg/errors v0.9.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
)
Expand Down Expand Up @@ -183,7 +184,6 @@ require (
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/status-im/keycard-go v0.2.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
Expand Down
20 changes: 20 additions & 0 deletions proto/lorenzo/agent/v1/agent.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
syntax = "proto3";
package lorenzo.agent.v1;

option go_package = "github.com/Lorenzo-Protocol/lorenzo/x/agent/types";

// Agent defines the details of a project
message Agent {
// id is the unique identifier of the agent
uint64 id = 1;
// agent name,required
string name = 2;
// btc_receiving_address is agent’s fund escrow address,required
string btc_receiving_address = 3;
// like 0xBAb28FF7659481F1c8516f616A576339936AFB06
string eth_addr = 4;
// description is a brief description of the agent, optional
string description = 5;
// url is the agent's link, used for detailed introduction, optional
string url = 6;
}
44 changes: 44 additions & 0 deletions proto/lorenzo/agent/v1/event.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
syntax = "proto3";
package lorenzo.agent.v1;

option go_package = "github.com/Lorenzo-Protocol/lorenzo/x/agent/types";

// agent creation event
message EventAddAgent {
// id is the unique identifier of the agent
uint64 id = 1;
// agent name,required
string name = 2;
// btc_receiving_address is agent’s fund escrow address,required
string btc_receiving_address = 3;
// like 0xBAb28FF7659481F1c8516f616A576339936AFB06
string eth_addr = 4;
// description is a brief description of the agent, optional
string description = 5;
// url is the agent's link, used for detailed introduction, optional
string url = 6;
// sender is the address of the governance account or module admin
string sender = 7;
}

// agent edit event
message EventEditAgent{
// id is the unique identifier of the agent
uint64 id = 1;
// agent name,required
string name = 2;
// description is a brief description of the agent, optional
string description = 3;
// url is the agent's link, used for detailed introduction, optional
string url = 4;
// sender is the address of the governance account or module admin
string sender = 5;
}

// agent remove event
message EventRemoveAgent{
// id is the unique identifier of the agent
uint64 id = 1;
// sender is the address of the governance account or module admin
string sender = 2;
}
15 changes: 15 additions & 0 deletions proto/lorenzo/agent/v1/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";
package lorenzo.agent.v1;

import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "lorenzo/agent/v1/agent.proto";

option go_package = "github.com/Lorenzo-Protocol/lorenzo/x/agent/types";

// GenesisState defines the agent module's genesis state.
message GenesisState {
repeated Agent agents = 1 [ (gogoproto.nullable) = false ];
// administrator account of agent module
string admin = 2 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}
Loading

0 comments on commit c0d5508

Please sign in to comment.