Skip to content

Commit

Permalink
Merge pull request #6 from sourcenetwork/iverc/general-fixes
Browse files Browse the repository at this point in the history
fix: General fixes
  • Loading branch information
iverc authored Nov 23, 2024
2 parents da8473e + 24471fe commit e7bd275
Show file tree
Hide file tree
Showing 22 changed files with 255 additions and 199 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ WORKDIR /app
COPY go.* /app/

# Copy submodules
COPY submodules/acp_core /app/submodules/acp_core
# COPY submodules/acp_core /app/submodules/acp_core

RUN go mod download

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DEMO_BIN = build/token-protocol-demo

.PHONY: build
build:
go build -o ${BIN} ./cmd/sourcehubd
GOOS=linux GOARCH=amd64 go build -o ${BIN} ./cmd/sourcehubd

.PHONY: install
install:
Expand Down
306 changes: 190 additions & 116 deletions api/sourcehub/acp/query.pulsar.go

Large diffs are not rendered by default.

18 changes: 5 additions & 13 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,16 +344,10 @@ func New(
// must be set manually as follow. The upgrade module will de-duplicate the module version map.
//
initChainer := func(ctx sdk.Context, req *abci.RequestInitChain) (*abci.ResponseInitChain, error) {

// TODO: Is there a cleaner way to update the denom without having to unmarshal and marshal the genesis state?
// At the time of validator being created, the denom is still the old one if we just set the params here:
//
// stakingParams := app.StakingKeeper.GetParams(ctx)
// stakingParams.BondDenom = BondDenom
// app.StakingKeeper.SetParams(ctx, stakingParams)
//
// The above approach doesn't work because the params need to be set before InitGenesis runs.
// Hence, we need to update the genesis state directly.
// Temp workaround to set default IBC params until app wiring is fully supported.
if req.InitialHeight == 1 {
app.setDefaultIBCParams(ctx)
}

var genesisState GenesisState
err := json.Unmarshal(req.AppStateBytes, &genesisState)
Expand All @@ -376,8 +370,8 @@ func New(
}

return app.App.InitChainer(ctx, req)

}

app.SetInitChainer(initChainer)

if err := app.Load(loadLatest); err != nil {
Expand Down Expand Up @@ -418,7 +412,6 @@ func (app *App) GetMemKey(storeKey string) *storetypes.MemoryStoreKey {
if !ok {
return nil
}

return key
}

Expand All @@ -430,7 +423,6 @@ func (app *App) kvStoreKeys() map[string]*storetypes.KVStoreKey {
keys[kv.Name()] = kv
}
}

return keys
}

Expand Down
15 changes: 15 additions & 0 deletions app/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"cosmossdk.io/core/appmodule"
storetypes "cosmossdk.io/store/types"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
Expand All @@ -28,6 +29,7 @@ import (
ibc "github.com/cosmos/ibc-go/v8/modules/core"
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
ibcconnectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
ibcchanneltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"
Expand Down Expand Up @@ -179,6 +181,19 @@ func (app *App) registerIBCModules() {
}
}

// setDefaultIBCParams is used to set default IBC params until app wiring is fully supported.
func (app *App) setDefaultIBCParams(ctx sdk.Context) {
app.IBCKeeper.ClientKeeper.SetNextClientSequence(ctx, 0)
app.IBCKeeper.ConnectionKeeper.SetNextConnectionSequence(ctx, 0)
app.IBCKeeper.ChannelKeeper.SetNextChannelSequence(ctx, 0)
app.IBCKeeper.ClientKeeper.SetParams(ctx, ibcclienttypes.DefaultParams())
app.IBCKeeper.ConnectionKeeper.SetParams(ctx, ibcconnectiontypes.DefaultParams())
app.IBCKeeper.ChannelKeeper.SetParams(ctx, ibcchanneltypes.DefaultParams())
app.ICAControllerKeeper.SetParams(ctx, icacontrollertypes.DefaultParams())
app.ICAHostKeeper.SetParams(ctx, icahosttypes.DefaultParams())
app.TransferKeeper.SetParams(ctx, ibctransfertypes.DefaultParams())
}

// Since the IBC modules don't support dependency injection, we need to
// manually register the modules on the client side.
// This needs to be removed after IBC supports App Wiring.
Expand Down
1 change: 0 additions & 1 deletion app/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ var denomMetadatas = []banktypes.Metadata{

// RegisterDenoms registers token denoms.
func RegisterDenoms(ctx context.Context, bk bankkeeper.Keeper) {

for _, denomMetadata := range denomMetadatas {
bk.SetDenomMetaData(ctx, denomMetadata)
}
Expand Down
24 changes: 24 additions & 0 deletions app/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package app

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

func SetConfig(seal bool) {
// set prefixes
accountPubKeyPrefix := AccountAddressPrefix + "pub"
validatorAddressPrefix := AccountAddressPrefix + "valoper"
validatorPubKeyPrefix := AccountAddressPrefix + "valoperpub"
consNodeAddressPrefix := AccountAddressPrefix + "valcons"
consNodePubKeyPrefix := AccountAddressPrefix + "valconspub"

// init config
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(AccountAddressPrefix, accountPubKeyPrefix)
config.SetBech32PrefixForValidator(validatorAddressPrefix, validatorPubKeyPrefix)
config.SetBech32PrefixForConsensusNode(consNodeAddressPrefix, consNodePubKeyPrefix)

if seal {
config.Seal()
}
}
19 changes: 0 additions & 19 deletions cmd/sourcehubd/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,8 @@ package cmd
import (
cmtcfg "github.com/cometbft/cometbft/config"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/sourcenetwork/sourcehub/app"
)

func initSDKConfig() {
// Set prefixes
accountPubKeyPrefix := app.AccountAddressPrefix + "pub"
validatorAddressPrefix := app.AccountAddressPrefix + "valoper"
validatorPubKeyPrefix := app.AccountAddressPrefix + "valoperpub"
consNodeAddressPrefix := app.AccountAddressPrefix + "valcons"
consNodePubKeyPrefix := app.AccountAddressPrefix + "valconspub"

// Set and seal config
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(app.AccountAddressPrefix, accountPubKeyPrefix)
config.SetBech32PrefixForValidator(validatorAddressPrefix, validatorPubKeyPrefix)
config.SetBech32PrefixForConsensusNode(consNodeAddressPrefix, consNodePubKeyPrefix)
config.Seal()
}

// initCometBFTConfig helps to override default CometBFT Config values.
// return cmtcfg.DefaultConfig if no custom configuration is required for the application.
func initCometBFTConfig() *cmtcfg.Config {
Expand Down
2 changes: 1 addition & 1 deletion cmd/sourcehubd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

// NewRootCmd creates a new root command for sourcehubd. It is called once in the main function.
func NewRootCmd() *cobra.Command {
initSDKConfig()
app.SetConfig(true)

var (
txConfigOpts tx.ConfigOptions
Expand Down
2 changes: 1 addition & 1 deletion devnet/configs/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ timeout_precommit_delta = "500ms"
# How long we wait after committing a block, before starting on the new
# height (this gives us a chance to receive some more precommits, even
# though we already have +2/3).
timeout_commit = "5s"
timeout_commit = "1s"

# How many blocks to look back to check existence of the node's consensus votes before joining consensus
# When non-zero, the node will panic upon restart
Expand Down
2 changes: 1 addition & 1 deletion devnet/dial-peers.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/bash
# dial-peers.sh informs node1 of the addresses of its peers.
# This script performs a tenedermint / cometbft rpc call to the remaining validators and queries their Id.
# This script performs a tendermint / cometbft rpc call to the remaining validators and queries their Id.
# The ID is used to construct the peer address of the persistent peers in node1.
#
# Note: this script makes use of an 'unsafe' Tendermint RPC call, which is disabled by default.
Expand Down
2 changes: 1 addition & 1 deletion example/bearer_token_gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {

var cmd = cobra.Command{
Use: "bearer-token authorized_account",
Short: "bearer-token generates a oneshot did / bearer token the given account address",
Short: "bearer-token generates a oneshot did / bearer token for the given account address",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
addr := args[0]
Expand Down
6 changes: 3 additions & 3 deletions manual/acp/cmd_auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ All the aforementioned claims MUST be present in the JWS.

### Claims

The `exp` (expires) and `isa` (issued at) claims are used as defined in the JWT RFC, an Unix time stamp (ie seconds since the epoch).
The `exp` (expires) and `isa` (issued at) claims are used as defined in the JWT RFC, a Unix time stamp (ie seconds since the epoch).

The `iss` claim is as defined by the JWT RFC, it identifies the entity that issued the JWS through a string.
This string MUST be the DID of the Actor issuing the JWS.
Expand All @@ -36,7 +36,7 @@ This public key is used to validate the JWS signature.
The `authorized_account` claim MUST contain a valid SourceHub address (ie a bech32 encoded Cosmos Account with the `source` prefix).
The address specified in this claim is used to limit which account can use the Bearer JWS.
A Policy Cmd is only accepted and processed if it is submitted in a Tx signed by the SAME account as the one set in the claim.
This is done to prevent mallicious actors from extracting Bearer JWSs from the public ledger and impersonating an Actor.
This is done to prevent malicious actors from extracting Bearer JWSs from the public ledger and impersonating an Actor.

The following is an example of a acceptable Bearer JWS payload:
```json
Expand Down Expand Up @@ -66,7 +66,7 @@ eyJhbGciOiJFZERTQSJ9.eyJpc3MiOiJkaWQ6a2V5Ono2TWtrSHNRYnAzdFhFQ3FtVUpvQ0p3eXV4U0t

### Usage Recommendation

A Bearer JWS allows the holder with the `auhtorized_account` to issue *any* Policy Commands on behalf of the issuer for the lifespan of the token.
A Bearer JWS allows the holder with the `authorized_account` to issue *any* Policy Commands on behalf of the issuer for the lifespan of the token.
As such, we recommend that SourceHub users use this method ONLY if they truly 100% trust the Account owner to act on their behalf.

Furthermore, a short JWS validity (~15 mins) is still RECOMMENDED to minimize the risk if the `authorized_account` gets compromised.
Expand Down
8 changes: 4 additions & 4 deletions proto/sourcehub/acp/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ service Msg {
// CheckAccess executes an Access Request for an User and stores the result of the evaluation in SourceHub.
// The resulting evaluation is used to generate a cryptographic proof that the given Access Request
// was valid at a particular block height.
rpc CheckAccess (MsgCheckAccess) returns (MsgCheckAccessResponse);
rpc CheckAccess(MsgCheckAccess) returns (MsgCheckAccessResponse);

// SignedPolicyCmd is a wrapper for a Command which is executed within the Context of a Policy.
// The Command is signed by the Actor issuing it.
rpc SignedPolicyCmd (MsgSignedPolicyCmd) returns (MsgSignedPolicyCmdResponse);
rpc SignedPolicyCmd(MsgSignedPolicyCmd) returns (MsgSignedPolicyCmdResponse);

// BearerPolicyCmd is a Msg containing an instruction which changes the authorization
// rules in a Policy.
Expand All @@ -44,9 +44,9 @@ service Msg {
// This token MUST be issued and signed by some DID Actor, the verification of the signature
// is used as authentication proof.
// Lastly, the Bearer token MUST be bound to some SourceHub account.
rpc BearerPolicyCmd (MsgBearerPolicyCmd) returns (MsgBearerPolicyCmdResponse);
rpc BearerPolicyCmd(MsgBearerPolicyCmd) returns (MsgBearerPolicyCmdResponse);

rpc DirectPolicyCmd (MsgDirectPolicyCmd) returns (MsgDirectPolicyCmdResponse);
rpc DirectPolicyCmd(MsgDirectPolicyCmd) returns (MsgDirectPolicyCmdResponse);
}

// MsgUpdateParams is the Msg/UpdateParams request type.
Expand Down
1 change: 1 addition & 0 deletions scripts/dev-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

if [ ! -e "~/INITIALIZED" ]; then
scripts/genesis-setup.sh
sed -i 's/^timeout_commit = .*/timeout_commit = "1s"/' ~/.sourcehub/config/config.toml
touch "~/INITIALIZED"
fi

Expand Down
2 changes: 1 addition & 1 deletion scripts/genesis-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ BIN="build/sourcehubd"
$BIN init $NODE_NAME --chain-id $CHAIN_ID

$BIN keys add $VALIDATOR --keyring-backend test
VALIDATOR_ADDR=$(build/sourcehubd keys show $VALIDATOR -a --keyring-backend test)
VALIDATOR_ADDR=$($BIN keys show $VALIDATOR -a --keyring-backend test)
$BIN genesis add-genesis-account $VALIDATOR_ADDR 100000000000open
$BIN genesis gentx $VALIDATOR 100000000open --chain-id $CHAIN_ID --keyring-backend test

Expand Down
2 changes: 1 addition & 1 deletion sdk/acp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// NewCmdBuilder returns a builder for PolicyCmd objects from a client.
//
// The client is used to fetch the latests acp module params from SourceHub
// The client is used to fetch the latest ACP module params from SourceHub
// and as a block height fetcher.
func NewCmdBuilder(ctx context.Context, client *Client) (*signed_policy_cmd.CmdBuilder, error) {
acpClient := client.ACPQueryClient()
Expand Down
3 changes: 2 additions & 1 deletion sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
type Opt func(*Client) error

// WithGRPCAddr sets the GRPC Address of a SourceHub node which the Client will connect to.
// If not specified defines to DefaultGRPCAddr
// If not specified defaults to DefaultGRPCAddr
// Note: Cosmos queries are not verifiable therefore only trusted RPC nodes should be used.
func WithGRPCAddr(addr string) Opt {
return func(c *Client) error {
Expand Down Expand Up @@ -82,6 +82,7 @@ func NewClient(opts ...Opt) (*Client, error) {
client.txClient = txClient
client.conn = conn
client.cometClient = cometClient

return client, nil
}

Expand Down
14 changes: 1 addition & 13 deletions sdk/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,5 @@ func WithSDKClient(client *Client) TxBuilderOpt {

// FIXME what's a better way of doing this for a lib?
func init() {
// Set prefixes
accountPubKeyPrefix := app.AccountAddressPrefix + "pub"
validatorAddressPrefix := app.AccountAddressPrefix + "valoper"
validatorPubKeyPrefix := app.AccountAddressPrefix + "valoperpub"
consNodeAddressPrefix := app.AccountAddressPrefix + "valcons"
consNodePubKeyPrefix := app.AccountAddressPrefix + "valconspub"

// Set and seal config
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(app.AccountAddressPrefix, accountPubKeyPrefix)
config.SetBech32PrefixForValidator(validatorAddressPrefix, validatorPubKeyPrefix)
config.SetBech32PrefixForConsensusNode(consNodeAddressPrefix, consNodePubKeyPrefix)
//config.Seal()
app.SetConfig(false)
}
16 changes: 1 addition & 15 deletions tests/integration/acp/init.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
package test

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sourcenetwork/sourcehub/app"
)

var initialized bool = false

func initTest() {
if !initialized {
setPrefix()
app.SetConfig(false)
initialized = true
}
}

func setPrefix() {
accountPubKeyPrefix := app.AccountAddressPrefix + "pub"
validatorAddressPrefix := app.AccountAddressPrefix + "valoper"
validatorPubKeyPrefix := app.AccountAddressPrefix + "valoperpub"
consNodeAddressPrefix := app.AccountAddressPrefix + "valcons"
consNodePubKeyPrefix := app.AccountAddressPrefix + "valconspub"

config := sdk.GetConfig()
config.SetBech32PrefixForAccount(app.AccountAddressPrefix, accountPubKeyPrefix)
config.SetBech32PrefixForValidator(validatorAddressPrefix, validatorPubKeyPrefix)
config.SetBech32PrefixForConsensusNode(consNodeAddressPrefix, consNodePubKeyPrefix)
}
3 changes: 0 additions & 3 deletions x/bulletin/keeper/bulletin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func (k Keeper) AddPost(ctx sdk.Context, post types.Post) {

storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.PostKey))
bz := k.cdc.MustMarshal(&post)
Expand All @@ -18,11 +17,9 @@ func (k Keeper) AddPost(ctx sdk.Context, post types.Post) {
}

func (k Keeper) GetPost(ctx sdk.Context, namespace string) (post types.Post, found bool) {

storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.PostKey))

k.storeService.OpenKVStore(ctx)
b := store.Get([]byte(namespace))
if b == nil {
return post, false
Expand Down
4 changes: 1 addition & 3 deletions x/tier/keeper/epoch_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ func (EpochHooks) GetModuleName() string {

// BeforeEpochStart is the epoch start hook.
func (h EpochHooks) BeforeEpochStart(ctx context.Context, epochIdentifier string, epochNumber int64) error {

if epochIdentifier != types.EpochIdentifier {
return nil
}

h.keeper.Logger().Info("reseting all crdits", "epochID", epochIdentifier, "epochNumber", epochNumber)
h.keeper.Logger().Info("resetting all credits", "epochID", epochIdentifier, "epochNumber", epochNumber)

err := h.keeper.burnAllCredits(ctx)
if err != nil {
Expand All @@ -47,7 +46,6 @@ func (h EpochHooks) BeforeEpochStart(ctx context.Context, epochIdentifier string
}

func (h EpochHooks) AfterEpochEnd(ctx context.Context, epochIdentifier string, epochNumber int64) error {

if epochIdentifier != types.EpochIdentifier {
return nil
}
Expand Down

0 comments on commit e7bd275

Please sign in to comment.