Skip to content

Commit

Permalink
fix test error
Browse files Browse the repository at this point in the history
  • Loading branch information
skyargos committed Nov 13, 2023
1 parent fe1f7ae commit bf820a4
Show file tree
Hide file tree
Showing 24 changed files with 323 additions and 232 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ require (
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/ibc-go/v4 v4.2.1
github.com/cosmos/tools/cmd/runsim v1.0.0
github.com/ethereum/go-ethereum v1.9.25
github.com/gogo/protobuf v1.3.3
github.com/golang/protobuf v1.5.3
github.com/golangci/golangci-lint v1.50.1
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/hyperledger/burrow v0.31.0
Expand All @@ -22,7 +22,6 @@ require (
github.com/rs/zerolog v1.28.0
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.6.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.14.0
github.com/stretchr/testify v1.8.2
github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15
Expand Down Expand Up @@ -248,6 +247,7 @@ require (
github.com/sourcegraph/go-diff v0.6.1 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ssgreg/nlreturn/v2 v2.2.1 // indirect
github.com/stbenjam/no-sprintf-host-port v0.1.1 // indirect
github.com/stretchr/objx v0.5.0 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/esimonov/ifshort v1.0.4 h1:6SID4yGWfRae/M7hkVDVVyppy8q/v9OuxNdmjLQStBA=
github.com/esimonov/ifshort v1.0.4/go.mod h1:Pe8zjlRrJ80+q2CxHLfEOfTwxCZ4O+MuhcHcfgNWTk0=
github.com/ethereum/go-ethereum v1.9.25 h1:mMiw/zOOtCLdGLWfcekua0qPrJTe7FVIiHJ4IKNTfR0=
github.com/ethereum/go-ethereum v1.9.25/go.mod h1:vMkFiYLHI4tgPw4k2j4MHKoovchFE8plZ0M9VMk4/oM=
github.com/ettle/strcase v0.1.1 h1:htFueZyVeE1XNnMEfbqp5r67qAN/4r6ya1ysq8Q+Zcw=
github.com/ettle/strcase v0.1.1/go.mod h1:hzDLsPC7/lwKyBOywSHEP89nt2pDgdy+No1NBA9o9VY=
Expand Down Expand Up @@ -543,6 +542,8 @@ github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
Expand Down
4 changes: 2 additions & 2 deletions proto/shentu/bounty/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ message MsgEditFinding {
// MsgEditFindingResponse defines the MsgEditFinding response type.
message MsgEditFindingResponse {}

// MsgConfirmFinding defines a message to accept a finding to an existing finding.
// MsgConfirmFinding defines a message to confirm a finding to an existing finding.
message MsgConfirmFinding {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

string finding_id = 1 [(gogoproto.moretags) = "yaml:\"finding_id\""];
string operator_address = 2 [(gogoproto.moretags) = "yaml:\"operator_address\""];
FindingStatus status = 3 [(gogoproto.moretags) = "yaml:\"status\""];
string finger_print = 3 [(gogoproto.moretags) = "yaml:\"finger_print\""];
}

// MsgConfirmFindingResponse defines the Msg/AcceptFinding response type.
Expand Down
1 change: 0 additions & 1 deletion x/bounty/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/stretchr/testify/require"

tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

shentuapp "github.com/shentufoundation/shentu/v2/app"
Expand Down
4 changes: 2 additions & 2 deletions x/bounty/keeper/finding.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func (k Keeper) GetFinding(ctx sdk.Context, id string) (types.Finding, bool) {

func (k Keeper) SetFinding(ctx sdk.Context, finding types.Finding) {
store := ctx.KVStore(k.storeKey)
findingData := k.cdc.MustMarshal(&finding)
store.Set(types.GetFindingKey(finding.FindingId), findingData)
bz := k.cdc.MustMarshal(&finding)
store.Set(types.GetFindingKey(finding.FindingId), bz)
}

func (k Keeper) DeleteFinding(ctx sdk.Context, id string) {
Expand Down
10 changes: 6 additions & 4 deletions x/bounty/keeper/finding_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package keeper_test

import (
"github.com/shentufoundation/shentu/v2/x/bounty/types"
"time"

"github.com/google/uuid"

"github.com/shentufoundation/shentu/v2/x/bounty/types"
)

func (suite *KeeperTestSuite) TestSetGetFinding() {
Expand All @@ -13,12 +16,11 @@ func (suite *KeeperTestSuite) TestSetGetFinding() {
shouldPass bool
contains string
}

// create program
program := types.Program{
ProgramId: "1",
ProgramId: uuid.NewString(),
Name: "name",
Description: "desc",
Detail: "detail",
AdminAddress: suite.address[0].String(),
Status: types.ProgramStatusInactive,
}
Expand Down
2 changes: 1 addition & 1 deletion x/bounty/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (k Keeper) Programs(c context.Context, req *types.QueryProgramsRequest) (*t
ctx := sdk.UnwrapSDKContext(c)

store := ctx.KVStore(k.storeKey)
programStore := prefix.NewStore(store, types.ProgramsKey)
programStore := prefix.NewStore(store, types.ProgramKey)

pageRes, err := query.FilteredPaginate(programStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
var p types.Program
Expand Down
12 changes: 7 additions & 5 deletions x/bounty/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/google/uuid"

"github.com/shentufoundation/shentu/v2/x/bounty/types"
)
Expand Down Expand Up @@ -117,8 +118,9 @@ func (suite *KeeperTestSuite) TestGRPCQueryFinding() {
queryClient := suite.queryClient

// create programs
suite.InitCreateProgram("1")
suite.InitOpenProgram("1")
pid := uuid.NewString()
suite.InitCreateProgram(pid)
suite.InitActivateProgram(pid)
var (
req *types.QueryFindingRequest
)
Expand Down Expand Up @@ -164,7 +166,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryFinding() {
suite.InitSubmitFinding("1", "2")

ctx := sdk.WrapSDKContext(suite.ctx)
suite.msgServer.ReleaseFinding(ctx, types.NewMsgReleaseFinding("2", "release desc", suite.address[0]))
suite.msgServer.ReleaseFinding(ctx, types.NewMsgReleaseFinding("2", "desc", "poc", suite.address[0]))
},
true,
},
Expand All @@ -190,9 +192,9 @@ func (suite *KeeperTestSuite) TestGRPCQueryFindings() {
queryClient := suite.queryClient

// create programs
pid := "1"
pid := uuid.NewString()
suite.InitCreateProgram(pid)
suite.InitOpenProgram(pid)
suite.InitActivateProgram(pid)

var (
req *types.QueryFindingsRequest
Expand Down
20 changes: 14 additions & 6 deletions x/bounty/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package keeper_test

import (
certTypes "github.com/shentufoundation/shentu/v2/x/cert/types"
"testing"

"github.com/stretchr/testify/suite"

tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

shentuapp "github.com/shentufoundation/shentu/v2/app"
"github.com/shentufoundation/shentu/v2/x/bounty/keeper"
"github.com/shentufoundation/shentu/v2/x/bounty/types"
certTypes "github.com/shentufoundation/shentu/v2/x/cert/types"
)

type KeeperTestSuite struct {
Expand All @@ -37,7 +35,17 @@ func (suite *KeeperTestSuite) SetupTest() {
suite.queryClient = types.NewQueryClient(queryHelper)
suite.msgServer = keeper.NewMsgServerImpl(suite.keeper)

suite.app.CertKeeper.SetCertifier(suite.ctx, certTypes.NewCertifier(suite.address[3], "", suite.address[3], ""))
suite.app.CertKeeper.SetCertifier(suite.ctx, certTypes.NewCertifier(suite.address[2], "", suite.address[2], ""))
certificate, err := certTypes.NewCertificate("bountyadmin", suite.address[3].String(), "", "", "", suite.address[2])
if err != nil {
panic(err)
}
id := suite.app.CertKeeper.GetNextCertificateID(suite.ctx)
certificate.CertificateId = id
// set the cert and its ID in the store
suite.app.CertKeeper.SetNextCertificateID(suite.ctx, id+1)
suite.app.CertKeeper.SetCertificate(suite.ctx, certificate)

}

func TestKeeperTestSuite(t *testing.T) {
Expand Down
33 changes: 19 additions & 14 deletions x/bounty/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,13 @@ func (k msgServer) EditFinding(goCtx context.Context, msg *types.MsgEditFinding)
func (k msgServer) ConfirmFinding(goCtx context.Context, msg *types.MsgConfirmFinding) (*types.MsgConfirmFindingResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

finding, err := k.hostProcess(ctx, msg.FindingId, msg.OperatorAddress)
finding, err := k.hostProcess(ctx, msg)
if err != nil {
return nil, err
}

finding.Status = types.FindingStatusConfirmed
k.SetFinding(ctx, *finding)
k.SetFinding(ctx, finding)

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
Expand All @@ -289,29 +289,34 @@ func (k msgServer) ConfirmFinding(goCtx context.Context, msg *types.MsgConfirmFi
return &types.MsgConfirmFindingResponse{}, nil
}

func (k msgServer) hostProcess(ctx sdk.Context, fid, hostAddr string) (*types.Finding, error) {

func (k msgServer) hostProcess(ctx sdk.Context, msg *types.MsgConfirmFinding) (types.Finding, error) {
var finding types.Finding
// get finding
finding, found := k.GetFinding(ctx, fid)
finding, found := k.GetFinding(ctx, msg.FindingId)
if !found {
return nil, types.ErrFindingNotExists
return finding, types.ErrFindingNotExists
}
// get program
program, isExist := k.GetProgram(ctx, finding.ProgramId)
if !isExist {
return nil, types.ErrProgramNotExists
return finding, types.ErrProgramNotExists
}
if program.Status != types.ProgramStatusActive {
return nil, types.ErrProgramNotActive
return finding, types.ErrProgramNotActive
}

// only host can update finding comment
if program.AdminAddress != hostAddr {
return nil, types.ErrProgramCreatorInvalid
if program.AdminAddress != msg.OperatorAddress {
return finding, types.ErrProgramCreatorInvalid
}

// todo fingerprint comparison
return &finding, nil
// fingerprint comparison
bz := k.cdc.MustMarshal(&finding)
hash := sha256.Sum256(bz)
if msg.FingerPrint != hex.EncodeToString(hash[:]) {
return finding, types.ErrFindingHashInvalid
}
return finding, nil
}

func (k msgServer) CloseFinding(goCtx context.Context, msg *types.MsgCloseFinding) (*types.MsgCloseFindingResponse, error) {
Expand Down Expand Up @@ -340,7 +345,7 @@ func (k msgServer) CloseFinding(goCtx context.Context, msg *types.MsgCloseFindin
// check operator
// program, certificate, finding owner
if finding.SubmitterAddress != msg.OperatorAddress && !k.certKeeper.IsBountyAdmin(ctx, operatorAddr) && finding.SubmitterAddress != program.AdminAddress {
return nil, types.ErrFindingSubmitterInvalid
return nil, types.ErrFindingOperatorNotAllowed
}

k.DeleteFidFromFidList(ctx, finding.ProgramId, finding.FindingId)
Expand Down Expand Up @@ -395,7 +400,7 @@ func (k msgServer) ReleaseFinding(goCtx context.Context, msg *types.MsgReleaseFi
// check hash
hash := sha256.Sum256([]byte(msg.Description + msg.ProofOfConcept + finding.SubmitterAddress))
if finding.FindingHash != hex.EncodeToString(hash[:]) {
return nil, types.ErrProgramCreatorInvalid
return nil, types.ErrFindingHashInvalid
}

finding.Description = msg.Description
Expand Down
Loading

0 comments on commit bf820a4

Please sign in to comment.