mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-27 00:35:18 +00:00
add params validation
This commit is contained in:
parent
2537928ee7
commit
00c1a371d2
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/x/params/subspace"
|
"github.com/cosmos/cosmos-sdk/x/params/subspace"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -19,7 +20,7 @@ const (
|
|||||||
// Parameter keys
|
// Parameter keys
|
||||||
var (
|
var (
|
||||||
// ParamStoreKeyParams Param store key for auction params
|
// ParamStoreKeyParams Param store key for auction params
|
||||||
KeyAuctionBidDuration = []byte("MaxBidDuration")
|
KeyAuctionBidDuration = []byte("BidDuration")
|
||||||
KeyAuctionDuration = []byte("MaxAuctionDuration")
|
KeyAuctionDuration = []byte("MaxAuctionDuration")
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -28,14 +29,14 @@ var _ subspace.ParamSet = &Params{}
|
|||||||
// Params is the governance parameters for the auction module.
|
// Params is the governance parameters for the auction module.
|
||||||
type Params struct {
|
type Params struct {
|
||||||
MaxAuctionDuration time.Duration `json:"max_auction_duration" yaml:"max_auction_duration"` // max length of auction
|
MaxAuctionDuration time.Duration `json:"max_auction_duration" yaml:"max_auction_duration"` // max length of auction
|
||||||
MaxBidDuration time.Duration `json:"max_bid_duration" yaml:"max_bid_duration"` // additional time added to the auction end time after each bid, capped by the expiry.
|
BidDuration time.Duration `json:"bid_duration" yaml:"bid_duration"` // additional time added to the auction end time after each bid, capped by the expiry.
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewParams returns a new Params object.
|
// NewParams returns a new Params object.
|
||||||
func NewParams(maxAuctionDuration time.Duration, bidDuration time.Duration) Params {
|
func NewParams(maxAuctionDuration time.Duration, bidDuration time.Duration) Params {
|
||||||
return Params{
|
return Params{
|
||||||
MaxAuctionDuration: maxAuctionDuration,
|
MaxAuctionDuration: maxAuctionDuration,
|
||||||
MaxBidDuration: bidDuration,
|
BidDuration: bidDuration,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,29 +55,37 @@ func ParamKeyTable() subspace.KeyTable {
|
|||||||
|
|
||||||
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs.
|
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs.
|
||||||
// nolint
|
// nolint
|
||||||
func (ap *Params) ParamSetPairs() subspace.ParamSetPairs {
|
func (p *Params) ParamSetPairs() subspace.ParamSetPairs {
|
||||||
return subspace.ParamSetPairs{
|
return subspace.ParamSetPairs{
|
||||||
{KeyAuctionBidDuration, &ap.MaxBidDuration},
|
{KeyAuctionBidDuration, &p.BidDuration},
|
||||||
{KeyAuctionDuration, &ap.MaxAuctionDuration},
|
{KeyAuctionDuration, &p.MaxAuctionDuration},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Equal returns a boolean determining if two Params types are identical.
|
// Equal returns a boolean determining if two Params types are identical.
|
||||||
func (ap Params) Equal(ap2 Params) bool {
|
func (p Params) Equal(p2 Params) bool {
|
||||||
bz1 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&ap)
|
bz1 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&p)
|
||||||
bz2 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&ap2)
|
bz2 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&p2)
|
||||||
return bytes.Equal(bz1, bz2)
|
return bytes.Equal(bz1, bz2)
|
||||||
}
|
}
|
||||||
|
|
||||||
// String implements stringer interface
|
// String implements stringer interface
|
||||||
func (ap Params) String() string {
|
func (p Params) String() string {
|
||||||
return fmt.Sprintf(`Auction Params:
|
return fmt.Sprintf(`Auction Params:
|
||||||
Max Auction Duration: %s
|
Max Auction Duration: %s
|
||||||
Max Bid Duration: %s`, ap.MaxAuctionDuration, ap.MaxBidDuration)
|
Bid Duration: %s`, p.MaxAuctionDuration, p.BidDuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate checks that the parameters have valid values.
|
// Validate checks that the parameters have valid values.
|
||||||
func (ap Params) Validate() error {
|
func (p Params) Validate() error {
|
||||||
// TODO check durations are within acceptable limits, if needed
|
if p.BidDuration < 0 {
|
||||||
|
return sdk.ErrInternal("bid duration cannot be negative")
|
||||||
|
}
|
||||||
|
if p.MaxAuctionDuration < 0 {
|
||||||
|
return sdk.ErrInternal("max auction duration cannot be negative")
|
||||||
|
}
|
||||||
|
if p.BidDuration > p.MaxAuctionDuration {
|
||||||
|
return sdk.ErrInternal("bid duration param cannot be larger than max auction duration")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
38
x/auction/types/params_test.go
Normal file
38
x/auction/types/params_test.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParams_Validate(t *testing.T) {
|
||||||
|
type fields struct {
|
||||||
|
}
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
MaxAuctionDuration time.Duration
|
||||||
|
BidDuration time.Duration
|
||||||
|
expectErr bool
|
||||||
|
}{
|
||||||
|
{"normal", 24 * time.Hour, 1 * time.Hour, false},
|
||||||
|
{"negativeBid", 24 * time.Hour, -1 * time.Hour, true},
|
||||||
|
{"negativeAuction", -24 * time.Hour, 1 * time.Hour, true},
|
||||||
|
{"bid>auction", 1 * time.Hour, 24 * time.Hour, true},
|
||||||
|
{"zeros", 0, 0, false},
|
||||||
|
}
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
p := Params{
|
||||||
|
MaxAuctionDuration: tc.MaxAuctionDuration,
|
||||||
|
BidDuration: tc.BidDuration,
|
||||||
|
}
|
||||||
|
err := p.Validate()
|
||||||
|
if tc.expectErr {
|
||||||
|
require.Error(t, err)
|
||||||
|
} else {
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user