Skip to content

Commit

Permalink
fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing committed Mar 1, 2023
1 parent ef46d55 commit 5c66b4e
Show file tree
Hide file tree
Showing 28 changed files with 563 additions and 619 deletions.
15 changes: 8 additions & 7 deletions e2e/tests/challenge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"
"time"

sdkmath "cosmossdk.io/math"
"github.com/bits-and-blooms/bitset"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -79,7 +80,7 @@ func (s *ChallengeTestSuite) createObject() (string, string, sdk.AccAddress, []s
checksum := sdk.Keccak256(buffer.Bytes())
expectChecksum := [][]byte{checksum, checksum, checksum, checksum, checksum, checksum, checksum}
contextType := "text/event-stream"
msgCreateObject := storagetypes.NewMsgCreateObject(user.GetAddr(), bucketName, objectName, uint64(payloadSize), false, expectChecksum, contextType, math.MaxUint, nil, nil)
msgCreateObject := storagetypes.NewMsgCreateObject(user.GetAddr(), bucketName, objectName, uint64(payloadSize), false, expectChecksum, contextType, storagetypes.REDUNDANCY_EC_TYPE, math.MaxUint, nil, nil)
msgCreateObject.PrimarySpApproval.Sig, err = s.StorageProvider.ApprovalKey.GetPrivKey().Sign(msgCreateObject.GetApprovalBytes())
s.Require().NoError(err)
s.SendTxBlock(msgCreateObject, user)
Expand Down Expand Up @@ -175,8 +176,8 @@ func (s *ChallengeTestSuite) TestAttest() {

valBitset := s.calculateValidatorBitSet(height, s.Relayer.GetPrivKey().PubKey().String())

msgAttest := challengetypes.NewMsgAttest(user.GetAddr(), event.ChallengeId, event.ObjectId, primarySp,
challengetypes.ChallengeResultSucceed, valBitset.Bytes(), nil)
msgAttest := challengetypes.NewMsgAttest(user.GetAddr(), event.ChallengeId, event.ObjectId, primarySp.String(),
challengetypes.CHALLENGE_SUCCEED, user.GetAddr().String(), valBitset.Bytes(), nil)
toSign := msgAttest.GetBlsSignBytes()

voteAggSignature, err := s.Relayer.GetPrivKey().Sign(toSign[:])
Expand Down Expand Up @@ -277,12 +278,12 @@ func filterEventFromBlock(blockRes *ctypes.ResultBlockResults) []challengetypes.
}
}
challengeId, _ := strconv.ParseInt(challengeIdStr, 10, 64)
objectId, _ := strconv.ParseInt(objectIdStr, 10, 64)
objectId := sdkmath.NewUintFromString(objectIdStr)
redundancyIndex, _ := strconv.ParseInt(redundancyIndexStr, 10, 32)
segmentIndex, _ := strconv.ParseInt(segmentIndexStr, 10, 32)
challengeEvents = append(challengeEvents, challengetypes.EventStartChallenge{
ChallengeId: uint64(challengeId),
ObjectId: uint64(objectId),
ObjectId: objectId,
SegmentIndex: uint32(segmentIndex),
SpOperatorAddress: spOpAddress,
RedundancyIndex: int32(redundancyIndex),
Expand Down Expand Up @@ -312,12 +313,12 @@ func filterEventFromTx(txRes *sdk.TxResponse) challengetypes.EventStartChallenge
}
}
challengeId, _ := strconv.ParseInt(challengeIdStr, 10, 64)
objectId, _ := strconv.ParseInt(objectIdStr, 10, 64)
objectId := sdkmath.NewUintFromString(objectIdStr)
redundancyIndex, _ := strconv.ParseInt(redundancyIndexStr, 10, 32)
segmentIndex, _ := strconv.ParseInt(segmentIndexStr, 10, 32)
return challengetypes.EventStartChallenge{
ChallengeId: uint64(challengeId),
ObjectId: uint64(objectId),
ObjectId: objectId,
SegmentIndex: uint32(segmentIndex),
SpOperatorAddress: spOpAddress,
RedundancyIndex: int32(redundancyIndex),
Expand Down
13 changes: 11 additions & 2 deletions proto/greenfield/challenge/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ syntax = "proto3";
package bnbchain.greenfield.challenge;

import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "greenfield/challenge/types.proto";

option go_package = "github.com/bnb-chain/greenfield/x/challenge/types";

Expand All @@ -10,7 +12,11 @@ message EventStartChallenge {
uint64 challenge_id = 1;

// The id of object info to be challenged.
uint64 object_id = 2;
string object_id = 2 [
(cosmos_proto.scalar) = "cosmos.Uint",
(gogoproto.customtype) = "Uint",
(gogoproto.nullable) = false
];

// The segment/piece index of the object info.
uint32 segment_index = 3;
Expand All @@ -20,14 +26,17 @@ message EventStartChallenge {

// The redundancy index, which comes from the index of storage providers.
int32 redundancy_index = 5;

// The challenger who submits the challenge.
string challenger_address = 6 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

message EventCompleteChallenge {
// The id of challenge.
uint64 challenge_id = 1;

// The result of challenge.
uint32 result = 2;
VoteResult result = 2;

// The slashed storage provider address.
string sp_operator_address = 3 [(cosmos_proto.scalar) = "cosmos.AddressString"];
Expand Down
31 changes: 20 additions & 11 deletions proto/greenfield/challenge/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package bnbchain.greenfield.challenge;

import "cosmos/msg/v1/msg.proto";
import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "greenfield/challenge/types.proto";

// this line is used by starport scaffolding # proto/tx/import

Expand All @@ -17,10 +19,10 @@ service Msg {
}

message MsgSubmit {
option (cosmos.msg.v1.signer) = "creator";
option (cosmos.msg.v1.signer) = "challenger";

// The challenger address.
string creator = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
string challenger = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// The storage provider to be challenged.
string sp_operator_address = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
Expand All @@ -41,37 +43,44 @@ message MsgSubmit {
message MsgSubmitResponse {}

message MsgAttest {
option (cosmos.msg.v1.signer) = "creator";
option (cosmos.msg.v1.signer) = "submitter";

// The submitter address.
string creator = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
string submitter = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// The id of the challenge.
uint64 challenge_id = 2;

// The id of the object info.
uint64 object_id = 3;
string object_id = 3 [
(cosmos_proto.scalar) = "cosmos.Uint",
(gogoproto.customtype) = "Uint",
(gogoproto.nullable) = false
];

// The storage provider to be challenged.
string sp_operator_address = 4 [(cosmos_proto.scalar) = "cosmos.AddressString"];
string sp_operator_address = 4;

// Vote result of the attestation.
uint32 vote_result = 5;
VoteResult vote_result = 5;

// The challenger who submits the challenge, which can be empty.
string challenger_address = 6;

// The validators participated in the attestation.
repeated fixed64 vote_validator_set = 6;
repeated fixed64 vote_validator_set = 7;

// The aggregated BLS signature from the validators.
bytes vote_agg_signature = 7;
bytes vote_agg_signature = 8;
}

message MsgAttestResponse {}

message MsgHeartbeat {
option (cosmos.msg.v1.signer) = "creator";
option (cosmos.msg.v1.signer) = "submitter";

// The submitter address.
string creator = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
string submitter = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// The id of the challenge.
uint64 challenge_id = 2;
Expand Down
20 changes: 16 additions & 4 deletions proto/greenfield/challenge/types.proto
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
syntax = "proto3";
package bnbchain.greenfield.challenge;

import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";

option go_package = "github.com/bnb-chain/greenfield/x/challenge/types";

message Challenge {
uint64 id = 1;
string challenger_address = 2;
enum VoteResult {
option (gogoproto.goproto_enum_prefix) = false;

// The challenge failed, for further extension.
CHALLENGE_FAILED = 0;

// The challenge succeed.
CHALLENGE_SUCCEED = 1;
}

message Slash {
string sp_operator_address = 1;
uint64 object_id = 2;
string object_id = 2 [
(cosmos_proto.scalar) = "cosmos.Uint",
(gogoproto.customtype) = "Uint",
(gogoproto.nullable) = false
];
uint64 height = 3;
}
11 changes: 6 additions & 5 deletions x/challenge/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"strings"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"

Expand Down Expand Up @@ -45,8 +44,8 @@ func EndBlocker(ctx sdk.Context, keeper k.Keeper) {
if objectCount.IsZero() {
return
}
objectId := k.RandomObjectId(seed, objectCount.Uint64())
objectInfo, found := keeper.StorageKeeper.GetObjectInfoById(ctx, sdkmath.NewUint(objectId))
objectId := k.RandomObjectId(seed, objectCount)
objectInfo, found := keeper.StorageKeeper.GetObjectInfoById(ctx, objectId)
if !found { // there is no object info yet, cannot generate challenges
return
}
Expand Down Expand Up @@ -86,7 +85,7 @@ func EndBlocker(ctx sdk.Context, keeper k.Keeper) {
}

// check recent slash
if keeper.ExistsSlash(ctx, strings.ToLower(spOperatorAddress), objectInfo.Id.Uint64()) {
if keeper.ExistsSlash(ctx, strings.ToLower(spOperatorAddress), objectInfo.Id) {
continue
}

Expand All @@ -95,14 +94,16 @@ func EndBlocker(ctx sdk.Context, keeper k.Keeper) {
segmentIndex := k.RandomSegmentIndex(seed, segments)

objectMap[mapKey] = struct{}{}

challengeId := keeper.GetOngoingChallengeId(ctx)
keeper.SetOngoingChallengeId(ctx, challengeId+1)
events = append(events, &types.EventStartChallenge{
ChallengeId: challengeId,
ObjectId: objectInfo.Id.Uint64(),
ObjectId: objectInfo.Id,
SegmentIndex: segmentIndex,
SpOperatorAddress: spOperatorAddress,
RedundancyIndex: redundancyIndex,
ChallengerAddress: "",
})

count++
Expand Down
36 changes: 22 additions & 14 deletions x/challenge/client/cli/tx_attest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand All @@ -19,7 +20,7 @@ var _ = strconv.Itoa(0)

func CmdAttest() *cobra.Command {
cmd := &cobra.Command{
Use: "attest [challenge-id] [object-id] [sp-operator-address] [vote-result] [vote-validator-set] [vote-agg-signature]",
Use: "attest [challenge-id] [object-id] [sp-operator-address] [vote-result] [challenger-address] [vote-validator-set] [vote-agg-signature]",
Short: "Broadcast message attest",
Args: cobra.ExactArgs(6),
RunE: func(cmd *cobra.Command, args []string) (err error) {
Expand All @@ -28,37 +29,43 @@ func CmdAttest() *cobra.Command {
return fmt.Errorf("challenge-id %s not a valid uint, please input a valid challenge-id", args[0])
}

argObjectId, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return fmt.Errorf("object-id %s not a valid uint, please input a valid object-id", args[1])
}
argObjectId := sdkmath.NewUintFromString(args[1])

argSpOperatorAddress, err := sdk.AccAddressFromHexUnsafe(args[2])
argSpOperatorAddress := args[2]
_, err = sdk.AccAddressFromHexUnsafe(argSpOperatorAddress)
if err != nil {
return fmt.Errorf("sp-operator-address %s not a valid address, please input a valid sp-operator-address", args[2])
}

argVoteResult, err := strconv.ParseUint(args[3], 10, 64)
if err != nil {
argVoteResult, err := strconv.ParseUint(args[3], 10, 32)
if err != nil || argVoteResult != uint64(types.CHALLENGE_SUCCEED) {
return fmt.Errorf("vote-result %s not a valid uint, please input a valid vote-result", args[3])
}

argChallengerAddress := args[4]
if argChallengerAddress != "" {
_, err = sdk.AccAddressFromHexUnsafe(argChallengerAddress)
if err != nil {
return fmt.Errorf("challenger-address %s not a valid address, please input a valid challenger-address", args[4])
}
}

argVoteValidatorSet := make([]uint64, 0)
splits := strings.Split(args[4], ",")
splits := strings.Split(args[5], ",")
for _, split := range splits {
val, err := strconv.ParseUint(split, 10, 64)
if err != nil {
return fmt.Errorf("vote-validator-set %s not a valid comma seperated uint array, please input a valid vote-validator-set", args[4])
return fmt.Errorf("vote-validator-set %s not a valid comma seperated uint array, please input a valid vote-validator-set", args[5])
}
argVoteValidatorSet = append(argVoteValidatorSet, val)
}
if len(argVoteValidatorSet) == 0 {
return fmt.Errorf("vote-validator-set %s not a valid comma seperated uint array, please input a valid vote-validator-set", args[4])
return fmt.Errorf("vote-validator-set %s not a valid comma seperated uint array, please input a valid vote-validator-set", args[5])
}

argVoteAggSignature, err := hex.DecodeString(args[5])
argVoteAggSignature, err := hex.DecodeString(args[6])
if err != nil {
return fmt.Errorf("vote-agg-signature %s not a hex encoded bytes, please input a valid vote-agg-signature", args[5])
return fmt.Errorf("vote-agg-signature %s not a hex encoded bytes, please input a valid vote-agg-signature", args[6])
}

clientCtx, err := client.GetClientTxContext(cmd)
Expand All @@ -71,7 +78,8 @@ func CmdAttest() *cobra.Command {
argChallengeId,
argObjectId,
argSpOperatorAddress,
uint32(argVoteResult),
types.VoteResult(argVoteResult),
argChallengerAddress,
argVoteValidatorSet,
argVoteAggSignature,
)
Expand Down
Loading

0 comments on commit 5c66b4e

Please sign in to comment.