-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Miner] chore: scaffold create-claim message (#43)
* chore: scaffold create claim message `ignite scaffold message create-claim session_header:SessionHeader root_hash --signer SupplierAddress --module supplier` (NB: generated a temporary SessionHeader duplicate in the supplier module so the above would work; subsequently, removed and updated protobuf import and usage.) * refactor: use `bytes` type for `MsgCreateClaim#root_hash` field * chore: add TODO comment in msg handler * chore: add TODOs Co-authored-by: Daniel Olshansky <[email protected]> --------- Co-authored-by: Daniel Olshansky <[email protected]>
- Loading branch information
1 parent
0405651
commit ab0a1f1
Showing
10 changed files
with
291 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cli | ||
|
||
import ( | ||
"encoding/base64" | ||
"strconv" | ||
|
||
"encoding/json" | ||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
"github.com/cosmos/cosmos-sdk/client/tx" | ||
"github.com/spf13/cobra" | ||
|
||
sessiontypes "pocket/x/session/types" | ||
"pocket/x/supplier/types" | ||
) | ||
|
||
// TODO(@bryanchriswhite): Add unit tests for the CLI command when implementing the business logic. | ||
|
||
var _ = strconv.Itoa(0) | ||
|
||
func CmdCreateClaim() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "create-claim [session-header] [root-hash-base64]", | ||
Short: "Broadcast message create-claim", | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
argSessionHeader := new(sessiontypes.SessionHeader) | ||
err = json.Unmarshal([]byte(args[0]), argSessionHeader) | ||
if err != nil { | ||
return err | ||
} | ||
argRootHash, err := base64.StdEncoding.DecodeString(args[1]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
clientCtx, err := client.GetClientTxContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
msg := types.NewMsgCreateClaim( | ||
clientCtx.GetFromAddress().String(), | ||
argSessionHeader, | ||
argRootHash, | ||
) | ||
if err := msg.ValidateBasic(); err != nil { | ||
return err | ||
} | ||
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) | ||
}, | ||
} | ||
|
||
flags.AddTxFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"pocket/x/supplier/types" | ||
) | ||
|
||
func (k msgServer) CreateClaim(goCtx context.Context, msg *types.MsgCreateClaim) (*types.MsgCreateClaimResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
/* | ||
INCOMPLETE: Handling the message | ||
## Validation | ||
### Session validation | ||
1. [ ] claimed session ID == retrieved session ID | ||
2. [ ] this supplier is in the session's suppliers list | ||
### Msg distribution validation (depends on session validation) | ||
1. [ ] pseudo-randomize earliest block offset | ||
2. [ ] governance-based earliest block offset | ||
### Claim validation | ||
1. [ ] session validation | ||
2. [ ] msg distribution validation | ||
## Persistence | ||
1. [ ] create claim message | ||
- supplier address | ||
- session header | ||
- claim | ||
2. [ ] last block height commitment; derives: | ||
- last block committed hash, must match proof path | ||
- session ID (?) | ||
*/ | ||
_ = ctx | ||
|
||
return &types.MsgCreateClaimResponse{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package simulation | ||
|
||
import ( | ||
"math/rand" | ||
|
||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation" | ||
"pocket/x/supplier/keeper" | ||
"pocket/x/supplier/types" | ||
) | ||
|
||
func SimulateMsgCreateClaim( | ||
ak types.AccountKeeper, | ||
bk types.BankKeeper, | ||
k keeper.Keeper, | ||
) simtypes.Operation { | ||
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, | ||
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { | ||
simAccount, _ := simtypes.RandomAcc(r, accs) | ||
msg := &types.MsgCreateClaim{ | ||
SupplierAddress: simAccount.Address.String(), | ||
} | ||
|
||
// TODO: Handling the CreateClaim simulation | ||
|
||
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "CreateClaim simulation not implemented"), nil, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package types | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
|
||
sessiontypes "pocket/x/session/types" | ||
) | ||
|
||
const TypeMsgCreateClaim = "create_claim" | ||
|
||
var _ sdk.Msg = &MsgCreateClaim{} | ||
|
||
func NewMsgCreateClaim(supplierAddress string, sessionHeader *sessiontypes.SessionHeader, rootHash []byte) *MsgCreateClaim { | ||
return &MsgCreateClaim{ | ||
SupplierAddress: supplierAddress, | ||
SessionHeader: sessionHeader, | ||
RootHash: rootHash, | ||
} | ||
} | ||
|
||
func (msg *MsgCreateClaim) Route() string { | ||
return RouterKey | ||
} | ||
|
||
func (msg *MsgCreateClaim) Type() string { | ||
return TypeMsgCreateClaim | ||
} | ||
|
||
func (msg *MsgCreateClaim) GetSigners() []sdk.AccAddress { | ||
supplierAddress, err := sdk.AccAddressFromBech32(msg.SupplierAddress) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return []sdk.AccAddress{supplierAddress} | ||
} | ||
|
||
func (msg *MsgCreateClaim) GetSignBytes() []byte { | ||
bz := ModuleCdc.MustMarshalJSON(msg) | ||
return sdk.MustSortJSON(bz) | ||
} | ||
|
||
func (msg *MsgCreateClaim) ValidateBasic() error { | ||
_, err := sdk.AccAddressFromBech32(msg.SupplierAddress) | ||
if err != nil { | ||
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid supplierAddress address (%s)", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/stretchr/testify/require" | ||
"pocket/testutil/sample" | ||
) | ||
|
||
// TODO(@bryanchriswhite): Add unit tests for message validation when adding the business logic. | ||
|
||
func TestMsgCreateClaim_ValidateBasic(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
msg MsgCreateClaim | ||
err error | ||
}{ | ||
{ | ||
name: "invalid address", | ||
msg: MsgCreateClaim{ | ||
SupplierAddress: "invalid_address", | ||
}, | ||
err: sdkerrors.ErrInvalidAddress, | ||
}, { | ||
name: "valid address", | ||
msg: MsgCreateClaim{ | ||
SupplierAddress: sample.AccAddress(), | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.msg.ValidateBasic() | ||
if tt.err != nil { | ||
require.ErrorIs(t, err, tt.err) | ||
return | ||
} | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} |