Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[Supplier] Basic validation logic for CreateClaim #148

Merged
merged 30 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1dd022f
Beginning to rename ServiceId to Service
Olshansk Nov 6, 2023
60f0fcd
Fix tests
Olshansk Nov 6, 2023
2b95bc4
Rename renaming components
Olshansk Nov 6, 2023
e8a50ad
Added unit tests for the claim
Olshansk Nov 6, 2023
a54c90e
Update pkg/relayer/proxy/interface.go
Olshansk Nov 7, 2023
cbd39ab
Update pkg/relayer/proxy/jsonrpc.go
Olshansk Nov 7, 2023
a5e7986
Update proto/pocket/shared/service.proto
Olshansk Nov 7, 2023
5ddb28e
Update x/shared/helpers/service_test.go
Olshansk Nov 7, 2023
a33b42e
Update x/supplier/keeper/supplier.go
Olshansk Nov 7, 2023
16c8145
Update pkg/relayer/proxy/jsonrpc.go
Olshansk Nov 7, 2023
d204d14
fix: flaky block client test (#132)
bryanchriswhite Nov 6, 2023
21edfe7
[CI] Build container images (#107)
okdas Nov 6, 2023
51ae802
[Miner] feat: add `TxClient` (#94)
bryanchriswhite Nov 7, 2023
36b6de2
[Off-chain] refactor: keyring errors & helpers (#131)
bryanchriswhite Nov 7, 2023
5496e42
[Miner] feat: add supplier client (#42)
bryanchriswhite Nov 7, 2023
4d49a1a
Fixed some tests based on bryan's comments
Olshansk Nov 7, 2023
ee3d8cb
Fix lingering serviceId rename
Olshansk Nov 7, 2023
39e1f3c
Merge with main again
Olshansk Nov 7, 2023
5f6a9ec
Merge branch 'issues/140/rename_serviceid_to_service' into issues/140…
Olshansk Nov 7, 2023
f4b3e7e
Reply to some review comments
Olshansk Nov 7, 2023
334f9b4
Update x/supplier/types/errors.go
Olshansk Nov 8, 2023
ad081ee
[Testing, Docs] refactor: move /internal/... to /testutil/... for god…
bryanchriswhite Nov 7, 2023
9dea6fc
[Code health] rename pocketd to poktroll (#157)
okdas Nov 8, 2023
3fb0e6e
feat: implement relayerSessions and sessionTree (#105)
red-0ne Nov 8, 2023
a67b4ad
Added 'go 1.20.10' to '.tool-versions'
Olshansk Nov 8, 2023
cbc8e8a
[Service] Rename the `ServiceId` proto to `Service` (#150)
Olshansk Nov 8, 2023
1cfc3bb
feat: Add session_end_block_height to SessionHeader (#162)
red-0ne Nov 8, 2023
a92c334
Merge with main
Olshansk Nov 8, 2023
e71a243
Merge with main
Olshansk Nov 8, 2023
a0be005
Ran go_impors
Olshansk Nov 8, 2023
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
66 changes: 66 additions & 0 deletions pkg/client/gomock_reflect_3526400147/prog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"encoding/gob"
"flag"
"fmt"
"os"
"path"
"reflect"

"github.com/golang/mock/mockgen/model"

pkg_ "github.com/pokt-network/poktroll/pkg/client"
)

var output = flag.String("output", "", "The output file name, or empty to use stdout.")

func main() {
flag.Parse()

its := []struct {
sym string
typ reflect.Type
}{

{"TxContext", reflect.TypeOf((*pkg_.TxContext)(nil)).Elem()},

{"TxClient", reflect.TypeOf((*pkg_.TxClient)(nil)).Elem()},
}
pkg := &model.Package{
// NOTE: This behaves contrary to documented behaviour if the

Check warning on line 31 in pkg/client/gomock_reflect_3526400147/prog.go

View workflow job for this annotation

GitHub Actions / misspell

[misspell] pkg/client/gomock_reflect_3526400147/prog.go#L31

"behaviour" is a misspelling of "behavior"
Raw output
./pkg/client/gomock_reflect_3526400147/prog.go:31:47: "behaviour" is a misspelling of "behavior"
// package name is not the final component of the import path.
// The reflect package doesn't expose the package name, though.
Name: path.Base("github.com/pokt-network/poktroll/pkg/client"),
}

for _, it := range its {
intf, err := model.InterfaceFromInterfaceType(it.typ)
if err != nil {
fmt.Fprintf(os.Stderr, "Reflection: %v\n", err)
os.Exit(1)
}
intf.Name = it.sym
pkg.Interfaces = append(pkg.Interfaces, intf)
}

outfile := os.Stdout
if len(*output) != 0 {
var err error
outfile, err = os.Create(*output)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open output file %q", *output)
}
defer func() {
if err := outfile.Close(); err != nil {
fmt.Fprintf(os.Stderr, "failed to close output file %q", *output)
os.Exit(1)
}
}()
}

if err := gob.NewEncoder(outfile).Encode(pkg); err != nil {
fmt.Fprintf(os.Stderr, "gob encode: %v\n", err)
os.Exit(1)
}
}
2 changes: 2 additions & 0 deletions x/supplier/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ var (
ErrSupplierInvalidServiceConfig = sdkerrors.Register(ModuleName, 5, "invalid service config")
ErrSupplierInvalidSessionStartHeight = sdkerrors.Register(ModuleName, 6, "invalid session start height")
ErrSupplierInvalidSessionId = sdkerrors.Register(ModuleName, 7, "invalid session ID")
ErrSupplierInvalidService = sdkerrors.Register(ModuleName, 8, "invalid service in supplier")
ErrSupplierInvalidClaimRootHash = sdkerrors.Register(ModuleName, 9, "invalid root hash")
)
21 changes: 21 additions & 0 deletions x/supplier/types/message_create_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

sessiontypes "github.com/pokt-network/poktroll/x/session/types"
sharedhelpers "github.com/pokt-network/poktroll/x/shared/helpers"
)

const TypeMsgCreateClaim = "create_claim"
Expand Down Expand Up @@ -41,9 +42,29 @@ func (msg *MsgCreateClaim) GetSignBytes() []byte {
}

func (msg *MsgCreateClaim) ValidateBasic() error {
// Validate the supplier address
_, err := sdk.AccAddressFromBech32(msg.SupplierAddress)
if err != nil {
return sdkerrors.Wrapf(ErrSupplierInvalidAddress, "invalid supplierAddress address (%s)", err)
}

// Validate the session header
sessionHeader := msg.SessionHeader
if sessionHeader.SessionStartBlockHeight < 1 {
return sdkerrors.Wrapf(ErrSupplierInvalidSessionStartHeight, "invalid session start block height (%d)", sessionHeader.SessionStartBlockHeight)
}
if len(sessionHeader.SessionId) == 0 {
return sdkerrors.Wrapf(ErrSupplierInvalidSessionId, "invalid session ID (%v)", sessionHeader.SessionId)
}
if !sharedhelpers.IsValidService(sessionHeader.Service) {
return sdkerrors.Wrapf(ErrSupplierInvalidService, "invalid service (%v)", sessionHeader.Service)
}

// Validate the root hash
// TODO_IMPROVE: Only checking to make sure a non-nil hash was provided for now, but we can validate the length as well.
if len(msg.RootHash) == 0 {
return sdkerrors.Wrapf(ErrSupplierInvalidClaimRootHash, "invalid root hash (%v)", msg.RootHash)
}

return nil
}
89 changes: 78 additions & 11 deletions x/supplier/types/message_create_claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,104 @@ import (
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/testutil/sample"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)

// 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
desc string

msg MsgCreateClaim
err error
}{
{
name: "invalid address",
desc: "invalid address",

msg: MsgCreateClaim{
SupplierAddress: "invalid_address",
},
err: ErrSupplierInvalidAddress,
}, {
name: "valid address",
},
{
desc: "valid address but invalid session start height",

msg: MsgCreateClaim{
SupplierAddress: sample.AccAddress(),
SessionHeader: &sessiontypes.SessionHeader{
SessionStartBlockHeight: 0, // Invalid start height
},
},
err: ErrSupplierInvalidSessionStartHeight,
},
{
desc: "valid address and session start height but invalid session ID",

msg: MsgCreateClaim{
SupplierAddress: sample.AccAddress(),
SessionHeader: &sessiontypes.SessionHeader{
SessionStartBlockHeight: 100,
SessionId: "", // Invalid session ID
},
},
err: ErrSupplierInvalidSessionId,
},
{
desc: "valid address, session start height, session ID but invalid service",

msg: MsgCreateClaim{
SupplierAddress: sample.AccAddress(),
SessionHeader: &sessiontypes.SessionHeader{
SessionStartBlockHeight: 100,
SessionId: "valid_session_id",
Service: &sharedtypes.Service{
Id: "invalid_service_id", // Assuming this ID is invalid
}, // Should trigger error
},
},
err: ErrSupplierInvalidService,
},
{
desc: "valid address, session start height, session ID, service but invalid root hash",

msg: MsgCreateClaim{
SupplierAddress: sample.AccAddress(),
SessionHeader: &sessiontypes.SessionHeader{
SessionStartBlockHeight: 100,
SessionId: "valid_session_id",
Service: &sharedtypes.Service{
Id: "svcId", // Assuming this ID is valid
},
},
RootHash: []byte(""), // Invalid root hash
},
err: ErrSupplierInvalidClaimRootHash,
},
{
desc: "all valid inputs",

msg: MsgCreateClaim{
SupplierAddress: sample.AccAddress(),
SessionHeader: &sessiontypes.SessionHeader{
SessionStartBlockHeight: 100,
SessionId: "valid_session_id",
Service: &sharedtypes.Service{
Id: "svcId", // Assuming this ID is valid
},
},
RootHash: []byte("valid_root_hash"), // Assuming this is valid
},
err: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Run(tt.desc, func(t *testing.T) {
err := tt.msg.ValidateBasic()
if tt.err != nil {
require.ErrorIs(t, err, tt.err)
return
} else {
require.NoError(t, err)
}
require.NoError(t, err)
})
}
}