diff --git a/make/codegen.mk b/make/codegen.mk index 52e8231187..2e81b599ce 100644 --- a/make/codegen.mk +++ b/make/codegen.mk @@ -30,7 +30,7 @@ kubetypes: $(K8S_GENERATE_GROUPS) akash.network:v1,v2beta1 .PHONY: proto-gen -proto-gen: $(PROTOC) $(GRPC_GATEWAY) $(PROTOC_GEN_COSMOS) modvendor +proto-gen: $(PROTOC) $(GRPC_GATEWAY) $(PROTOC_GEN_GOCOSMOS) modvendor ./script/protocgen.sh .PHONY: proto-swagger-gen diff --git a/make/init.mk b/make/init.mk index ce1c501ba8..f420863b92 100644 --- a/make/init.mk +++ b/make/init.mk @@ -14,42 +14,42 @@ GO := GO111MODULE=$(GO111MODULE) go # setup .cache bins first in paths to have precedence over already installed same tools for system wide use PATH := "$(PATH):$(AKASH_DEVCACHE_BIN):$(AKASH_DEVCACHE_NODE_BIN)" -BUF_VERSION ?= 0.35.1 -PROTOC_VERSION ?= 3.13.0 -PROTOC_GEN_COSMOS_VERSION ?= v0.3.1 -GRPC_GATEWAY_VERSION := $(shell $(GO) list -mod=readonly -m -f '{{ .Version }}' github.com/grpc-ecosystem/grpc-gateway) -PROTOC_SWAGGER_GEN_VERSION := $(GRPC_GATEWAY_VERSION) -GOLANGCI_LINT_VERSION ?= v1.38.0 -GOLANG_VERSION ?= 1.16.1 -STATIK_VERSION ?= v0.1.7 -GIT_CHGLOG_VERSION ?= v0.15.1 -MODVENDOR_VERSION ?= v0.3.0 -MOCKERY_VERSION ?= 2.5.1 -K8S_CODE_GEN_VERSION ?= v0.19.3 +BUF_VERSION ?= 0.35.1 +PROTOC_VERSION ?= 3.13.0 +PROTOC_GEN_GOCOSMOS_VERSION ?= v0.3.1 +GRPC_GATEWAY_VERSION := $(shell $(GO) list -mod=readonly -m -f '{{ .Version }}' github.com/grpc-ecosystem/grpc-gateway) +PROTOC_SWAGGER_GEN_VERSION := $(GRPC_GATEWAY_VERSION) +GOLANGCI_LINT_VERSION ?= v1.38.0 +GOLANG_VERSION ?= 1.16.1 +STATIK_VERSION ?= v0.1.7 +GIT_CHGLOG_VERSION ?= v0.15.1 +MODVENDOR_VERSION ?= v0.3.0 +MOCKERY_VERSION ?= 2.5.1 +K8S_CODE_GEN_VERSION ?= v0.19.3 # _VERSION_FILE points to the marker file for the installed version. # If _VERSION_FILE is changed, the binary will be re-downloaded. -PROTOC_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/protoc/$(PROTOC_VERSION) -GRPC_GATEWAY_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/protoc-gen-grpc-gateway/$(GRPC_GATEWAY_VERSION) -PROTOC_GEN_COSMOS_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/protoc-gen-cosmos/$(PROTOC_GEN_COSMOS_VERSION) -STATIK_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/statik/$(STATIK_VERSION) -MODVENDOR_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/modvendor/$(MODVENDOR_VERSION) -GIT_CHGLOG_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/git-chglog/$(GIT_CHGLOG_VERSION) -MOCKERY_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/mockery/v$(MOCKERY_VERSION) -K8S_CODE_GEN_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/k8s-codegen/$(K8S_CODE_GEN_VERSION) - -MODVENDOR = $(AKASH_DEVCACHE_BIN)/modvendor -SWAGGER_COMBINE = $(AKASH_DEVCACHE_NODE_BIN)/swagger-combine -PROTOC_SWAGGER_GEN := $(AKASH_DEVCACHE_BIN)/protoc-swagger-gen -PROTOC := $(AKASH_DEVCACHE_BIN)/protoc -STATIK := $(AKASH_DEVCACHE_BIN)/statik -PROTOC_GEN_COSMOS := $(AKASH_DEVCACHE_BIN)/protoc-gen-cosmos -GRPC_GATEWAY := $(AKASH_DEVCACHE_BIN)/protoc-gen-grpc-gateway -GIT_CHGLOG := $(AKASH_DEVCACHE_BIN)/git-chglog -MOCKERY := $(AKASH_DEVCACHE_BIN)/mockery -K8S_GENERATE_GROUPS := $(AKASH_ROOT)/vendor/k8s.io/code-generator/generate-groups.sh -K8S_GO_TO_PROTOBUF := $(AKASH_DEVCACHE_BIN)/go-to-protobuf -KIND := kind -NPM := npm +PROTOC_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/protoc/$(PROTOC_VERSION) +GRPC_GATEWAY_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/protoc-gen-grpc-gateway/$(GRPC_GATEWAY_VERSION) +PROTOC_GEN_GOCOSMOS_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/protoc-gen-gocosmos/$(PROTOC_GEN_GOCOSMOS_VERSION) +STATIK_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/statik/$(STATIK_VERSION) +MODVENDOR_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/modvendor/$(MODVENDOR_VERSION) +GIT_CHGLOG_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/git-chglog/$(GIT_CHGLOG_VERSION) +MOCKERY_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/mockery/v$(MOCKERY_VERSION) +K8S_CODE_GEN_VERSION_FILE := $(AKASH_DEVCACHE_VERSIONS)/k8s-codegen/$(K8S_CODE_GEN_VERSION) + +MODVENDOR = $(AKASH_DEVCACHE_BIN)/modvendor +SWAGGER_COMBINE = $(AKASH_DEVCACHE_NODE_BIN)/swagger-combine +PROTOC_SWAGGER_GEN := $(AKASH_DEVCACHE_BIN)/protoc-swagger-gen +PROTOC := $(AKASH_DEVCACHE_BIN)/protoc +STATIK := $(AKASH_DEVCACHE_BIN)/statik +PROTOC_GEN_GOCOSMOS := $(AKASH_DEVCACHE_BIN)/protoc-gen-gocosmos +GRPC_GATEWAY := $(AKASH_DEVCACHE_BIN)/protoc-gen-grpc-gateway +GIT_CHGLOG := $(AKASH_DEVCACHE_BIN)/git-chglog +MOCKERY := $(AKASH_DEVCACHE_BIN)/mockery +K8S_GENERATE_GROUPS := $(AKASH_ROOT)/vendor/k8s.io/code-generator/generate-groups.sh +K8S_GO_TO_PROTOBUF := $(AKASH_DEVCACHE_BIN)/go-to-protobuf +KIND := kind +NPM := npm include $(AKASH_ROOT)/make/setup-cache.mk diff --git a/make/setup-cache.mk b/make/setup-cache.mk index 7203592f4c..7cc3bdac44 100644 --- a/make/setup-cache.mk +++ b/make/setup-cache.mk @@ -21,14 +21,14 @@ $(PROTOC_VERSION_FILE): $(AKASH_DEVCACHE) touch $@ $(PROTOC): $(PROTOC_VERSION_FILE) -$(PROTOC_GEN_COSMOS_VERSION_FILE): $(AKASH_DEVCACHE) - @echo "installing protoc-gen-cosmos $(PROTOC_GEN_COSMOS_VERSION) ..." - rm -f $(PROTOC_GEN_COSMOS) - GOBIN=$(AKASH_DEVCACHE_BIN) go get github.com/regen-network/cosmos-proto/protoc-gen-gocosmos@$(PROTOC_GEN_COSMOS_VERSION) +$(PROTOC_GEN_GOCOSMOS_VERSION_FILE): $(AKASH_DEVCACHE) modvendor + @echo "installing protoc-gen-gocosmos $(PROTOC_GEN_GOCOSMOS_VERSION) ..." + rm -f $(PROTOC_GEN_GOCOSMOS) + GOBIN=$(AKASH_DEVCACHE_BIN) $(GO) install $(ROOT_DIR)/vendor/github.com/regen-network/cosmos-proto/protoc-gen-gocosmos rm -rf "$(dir $@)" mkdir -p "$(dir $@)" touch $@ -$(PROTOC_GEN_COSMOS): $(PROTOC_GEN_COSMOS_VERSION_FILE) +$(PROTOC_GEN_GOCOSMOS): $(PROTOC_GEN_GOCOSMOS_VERSION_FILE) $(GRPC_GATEWAY_VERSION_FILE): $(AKASH_DEVCACHE) @echo "Installing protoc-gen-grpc-gateway $(GRPC_GATEWAY_VERSION) ..." diff --git a/proto/akash/deployment/v1beta2/deploymentmsg.proto b/proto/akash/deployment/v1beta2/deploymentmsg.proto index 6dd2a1cb17..2f877995f3 100644 --- a/proto/akash/deployment/v1beta2/deploymentmsg.proto +++ b/proto/akash/deployment/v1beta2/deploymentmsg.proto @@ -81,11 +81,6 @@ message MsgUpdateDeployment { (gogoproto.jsontag) = "id", (gogoproto.moretags) = "yaml:\"id\"" ]; - repeated GroupSpec groups = 2 [ - (gogoproto.nullable) = false, - (gogoproto.jsontag) = "groups", - (gogoproto.moretags) = "yaml:\"groups\"" - ]; bytes version = 3 [ (gogoproto.jsontag) = "version", (gogoproto.moretags) = "yaml:\"version\"" diff --git a/proto/akash/inflation/v1beta2/params.proto b/proto/akash/inflation/v1beta2/params.proto index a752223713..329296d8bb 100644 --- a/proto/akash/inflation/v1beta2/params.proto +++ b/proto/akash/inflation/v1beta2/params.proto @@ -7,7 +7,9 @@ option go_package = "github.com/ovrclk/akash/x/inflation/types/v1beta2"; // Params defines the parameters for the x/deployment package message Params { // InflationDecayFactor is the number of years it takes inflation to halve. - uint32 inflation_decay_factor = 1 [ + string inflation_decay_factor = 1 [ + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", + (gogoproto.nullable) = false, (gogoproto.customname) = "InflationDecayFactor", (gogoproto.jsontag) = "inflation_decay_factor", (gogoproto.moretags) = "yaml:\"inflation_decay_factor\"" diff --git a/sdl/_testdata/simple-double-ram.yaml b/sdl/_testdata/simple-double-ram.yaml new file mode 100644 index 0000000000..6e1bdb5cb6 --- /dev/null +++ b/sdl/_testdata/simple-double-ram.yaml @@ -0,0 +1,45 @@ +--- +version: "2.0" +services: + web: + image: nginx + expose: + - port: 80 + accept: + - ahostname.com + to: + - global: true + - port: 12345 + to: + - global: true + proto: udp +profiles: + compute: + web: + resources: + cpu: + units: "100m" + memory: + size: "256Mi" + storage: + size: "1Gi" + placement: + westcoast: + attributes: + region: us-west + signedBy: + anyOf: + - 1 + - 2 + allOf: + - 3 + - 4 + pricing: + web: + denom: uakt + amount: 50 +deployment: + web: + westcoast: + profile: web + count: 2 diff --git a/sdl/sdl_test.go b/sdl/sdl_test.go index 45dcec20da..de0257061d 100644 --- a/sdl/sdl_test.go +++ b/sdl/sdl_test.go @@ -4,26 +4,56 @@ import ( "testing" "github.com/stretchr/testify/require" - "gopkg.in/yaml.v3" ) -func TestSdlVersion(t *testing.T) { - stream := ` -version: v2.0 -services: - web: - image: quay.io/ovrclk/demo-app - expose: - - port: 80 - as: 80 - accept: - - hello.localhost - to: - - global: true -` - - var sdl sdl - - err := yaml.Unmarshal([]byte(stream), &sdl) +func TestSDLManifestVersion(t *testing.T) { + obj, err := ReadFile("_testdata/simple.yaml") require.NoError(t, err) + + m, err := obj.Manifest() + require.NoError(t, err) + + version, err := ManifestVersion(m) + require.NoError(t, err) + // Should return a value + require.NotEmpty(t, version) + + obj, err = ReadFile("_testdata/private_service.yaml") + require.NoError(t, err) + + m, err = obj.Manifest() + require.NoError(t, err) + + secondVersion, err := ManifestVersion(m) + require.NoError(t, err) + // Should return a value + require.NotEmpty(t, secondVersion) + // Should be different than the first + require.NotEqual(t, secondVersion, version) +} + +func TestSDLManifestVersionChangesWithVersion(t *testing.T) { + obj, err := ReadFile("_testdata/simple.yaml") + require.NoError(t, err) + + m, err := obj.Manifest() + require.NoError(t, err) + + version, err := ManifestVersion(m) + require.NoError(t, err) + // Should return a value + require.NotEmpty(t, version) + + obj, err = ReadFile("_testdata/simple-double-ram.yaml") + require.NoError(t, err) + + m, err = obj.Manifest() + require.NoError(t, err) + + secondVersion, err := ManifestVersion(m) + require.NoError(t, err) + // Should return a value + require.NotEmpty(t, secondVersion) + // Should be different than the first + require.NotEqual(t, secondVersion, version) } diff --git a/tools.go b/tools.go index 00060e1cb9..009b24d680 100644 --- a/tools.go +++ b/tools.go @@ -3,8 +3,9 @@ package tools -//nolint +// nolint import ( + _ "github.com/regen-network/cosmos-proto/protoc-gen-gocosmos" _ "k8s.io/code-generator" _ "sigs.k8s.io/kind" ) diff --git a/x/deployment/client/cli/tx.go b/x/deployment/client/cli/tx.go index 925065d1b2..fc21617ae6 100644 --- a/x/deployment/client/cli/tx.go +++ b/x/deployment/client/cli/tx.go @@ -3,6 +3,7 @@ package cli import ( "fmt" "os" + "reflect" "strings" "time" @@ -22,6 +23,11 @@ import ( "github.com/spf13/cobra" ) +var ( + errDeploymentUpdate = errors.New("deployment update failed") + errDeploymentUpdateGroupsChanged = fmt.Errorf("%w: groups are different than existing deployment, you cannot update groups", errDeploymentUpdate) +) + // GetTxCmd returns the transaction commands for this module func GetTxCmd(key string) *cobra.Command { cmd := &cobra.Command{ @@ -219,26 +225,43 @@ func cmdUpdate(key string) *cobra.Command { if err != nil { return err } + + version, err := sdl.Version(sdlManifest) + if err != nil { + return err + } + groups, err := sdlManifest.DeploymentGroups() if err != nil { return err } - version, err := sdl.Version(sdlManifest) + // Query the RPC node to make sure the existing groups are identical + queryClient := types.NewQueryClient(clientCtx) + existingDeployment, err := queryClient.Deployment(cmd.Context(), &types.QueryDeploymentRequest{ + ID: id, + }) if err != nil { return err } + // do not send the transaction if the groups have changed + existingGroups := existingDeployment.GetGroups() + if len(existingGroups) != len(groups) { + return errDeploymentUpdateGroupsChanged + } + + for i, existingGroup := range existingGroups { + if reflect.DeepEqual(groups[i], existingGroup.GroupSpec) { + return errDeploymentUpdateGroupsChanged + } + } + warnIfGroupVolumesExceeds(clientCtx, groups) msg := &types.MsgUpdateDeployment{ ID: id, Version: version, - Groups: make([]types.GroupSpec, 0, len(groups)), - } - - for _, group := range groups { - msg.Groups = append(msg.Groups, *group) } return sdkutil.BroadcastTX(cmd.Context(), clientCtx, cmd.Flags(), msg) diff --git a/x/deployment/handler/handler_test.go b/x/deployment/handler/handler_test.go index 0c07d9088f..45235a34a7 100644 --- a/x/deployment/handler/handler_test.go +++ b/x/deployment/handler/handler_test.go @@ -2,6 +2,7 @@ package handler_test import ( "crypto/sha256" + "github.com/ovrclk/akash/sdl" "testing" "time" @@ -117,8 +118,17 @@ func TestCreateDeployment(t *testing.T) { require.Equal(t, msg.ID, dev.ID) }) - _, exists := suite.dkeeper.GetDeployment(suite.ctx, deployment.ID()) + deploymentResult, exists := suite.dkeeper.GetDeployment(suite.ctx, deployment.ID()) require.True(t, exists) + require.Equal(t, deploymentResult.Version, msg.Version) + + groupsResult := suite.dkeeper.GetGroups(suite.ctx, deployment.ID()) + require.NotEmpty(t, groupsResult) + require.Equal(t, len(groupsResult), len(groups)) + + for i, g := range groupsResult { + require.Equal(t, groups[i].GetName(), g.GetName()) + } res, err = suite.handler(suite.ctx, msg) require.EqualError(t, err, types.ErrDeploymentExists.Error()) @@ -159,9 +169,22 @@ func TestUpdateDeploymentExisting(t *testing.T) { deployment, groups := suite.createDeployment() + sdlObj, err := sdl.ReadFile("../../../sdl/_testdata/simple.yaml") + require.NoError(t, err) + + dgroups, err := sdlObj.DeploymentGroups() + require.NoError(t, err) + + msgGroups := make([]types.GroupSpec, 0) + for _, g := range dgroups { + msgGroups = append(msgGroups, *g) + } + require.NotEmpty(t, msgGroups) + require.Equal(t, len(msgGroups), 1) + msg := &types.MsgCreateDeployment{ ID: deployment.ID(), - Groups: make([]types.GroupSpec, 0, len(groups)), + Groups: msgGroups, Version: testutil.DefaultDeploymentVersion[:], Deposit: types.DefaultDeploymentMinDeposit, Depositor: deployment.ID().Owner, @@ -181,16 +204,16 @@ func TestUpdateDeploymentExisting(t *testing.T) { assert.Equal(t, d.Version, testutil.DefaultDeploymentVersion[:]) }) + // Change the version depSum := sha256.Sum256(testutil.DefaultDeploymentVersion[:]) msgUpdate := &types.MsgUpdateDeployment{ - ID: deployment.ID(), + ID: msg.ID, Version: depSum[:], } - res, err = suite.handler(suite.ctx, msgUpdate) - require.NotNil(t, res) require.NoError(t, err) + require.NotNil(t, res) t.Run("ensure event created", func(t *testing.T) { t.Skip("now has more events") @@ -206,6 +229,12 @@ func TestUpdateDeploymentExisting(t *testing.T) { require.True(t, ok) assert.Equal(t, d.Version, depSum[:]) }) + + // Run the same update, should fail since nothing is different + res, err = suite.handler(suite.ctx, msgUpdate) + require.Error(t, err) + require.Contains(t, err.Error(), "Invalid: deployment version") + } func TestCloseDeploymentNonExisting(t *testing.T) { diff --git a/x/deployment/handler/server.go b/x/deployment/handler/server.go index 58c0167c4b..19bb22ee56 100644 --- a/x/deployment/handler/server.go +++ b/x/deployment/handler/server.go @@ -3,10 +3,8 @@ package handler import ( "bytes" "context" - - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/pkg/errors" "github.com/ovrclk/akash/x/deployment/keeper" @@ -179,14 +177,18 @@ func (ms msgServer) UpdateDeployment(goCtx context.Context, msg *types.MsgUpdate return nil, types.ErrDeploymentNotFound } + // If the deployment is not active, do not allow it to be updated if deployment.State != types.DeploymentActive { return &types.MsgUpdateDeploymentResponse{}, types.ErrDeploymentClosed } - if !bytes.Equal(msg.Version, deployment.Version) { - deployment.Version = msg.Version + // If the version is not identical do not allow the update, there is nothing to change in this transaction + if bytes.Equal(msg.Version, deployment.Version) { + return &types.MsgUpdateDeploymentResponse{}, types.ErrInvalidVersion } + deployment.Version = msg.Version + if err := ms.deployment.UpdateDeployment(ctx, deployment); err != nil { return &types.MsgUpdateDeploymentResponse{}, errors.Wrap(types.ErrInternal, err.Error()) } diff --git a/x/deployment/simulation/operations.go b/x/deployment/simulation/operations.go index 651f705c96..f7d1c92545 100644 --- a/x/deployment/simulation/operations.go +++ b/x/deployment/simulation/operations.go @@ -200,11 +200,6 @@ func SimulateMsgUpdateDeployment(ak govtypes.AccountKeeper, bk bankkeeper.Keeper return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeUpdateDeployment, "unable to read config file"), nil, readError } - groupSpecs, groupErr := sdl.DeploymentGroups() - if groupErr != nil { - return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeUpdateDeployment, "unable to parse groups"), nil, groupErr - } - sdlSum, err := sdlv1.Version(sdl) if err != nil { return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeUpdateDeployment, "error parsing deployment version sum"), @@ -219,11 +214,7 @@ func SimulateMsgUpdateDeployment(ak govtypes.AccountKeeper, bk bankkeeper.Keeper return simtypes.NoOpMsg(types.ModuleName, types.MsgTypeUpdateDeployment, "unable to generate fees"), nil, err } - msg := types.NewMsgUpdateDeployment(deployment.ID(), make([]types.GroupSpec, 0, len(groupSpecs)), sdlSum) - - for _, spec := range groupSpecs { - msg.Groups = append(msg.Groups, *spec) - } + msg := types.NewMsgUpdateDeployment(deployment.ID(), sdlSum) txGen := simappparams.MakeTestEncodingConfig().TxConfig tx, err := helpers.GenTx( diff --git a/x/deployment/types/v1beta2/deploymentmsg.pb.go b/x/deployment/types/v1beta2/deploymentmsg.pb.go index 06d1e58415..41b5658a64 100644 --- a/x/deployment/types/v1beta2/deploymentmsg.pb.go +++ b/x/deployment/types/v1beta2/deploymentmsg.pb.go @@ -241,7 +241,6 @@ var xxx_messageInfo_MsgDepositDeploymentResponse proto.InternalMessageInfo // MsgUpdateDeployment defines an SDK message for updating deployment type MsgUpdateDeployment struct { ID DeploymentID `protobuf:"bytes,1,opt,name=id,proto3" json:"id" yaml:"id"` - Groups []GroupSpec `protobuf:"bytes,2,rep,name=groups,proto3" json:"groups" yaml:"groups"` Version []byte `protobuf:"bytes,3,opt,name=version,proto3" json:"version" yaml:"version"` } @@ -285,13 +284,6 @@ func (m *MsgUpdateDeployment) GetID() DeploymentID { return DeploymentID{} } -func (m *MsgUpdateDeployment) GetGroups() []GroupSpec { - if m != nil { - return m.Groups - } - return nil -} - func (m *MsgUpdateDeployment) GetVersion() []byte { if m != nil { return m.Version @@ -434,40 +426,41 @@ func init() { } var fileDescriptor_5e09213efb52c240 = []byte{ - // 527 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe4, 0x54, 0x41, 0x8b, 0xd3, 0x40, - 0x14, 0x6e, 0xd2, 0x75, 0x97, 0xce, 0xaa, 0x48, 0xdc, 0x43, 0xac, 0xdb, 0x4c, 0x77, 0x04, 0x89, - 0x20, 0x09, 0xad, 0x07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 0x8d, 0x82, 0x25, 0xc7, 0xad, 0xa9, 0x17, - 0x8f, 0x0e, 0x49, 0x14, 0x10, 0x57, 0x8d, 0x02, 0xed, 0x1d, 0xda, 0x16, 0x15, 0xea, 0x6a, 0xb7, - 0x69, 0xee, 0xf6, 0x1f, 0x6d, 0x4e, 0xfd, 0xb2, 0xe0, 0xbd, 0x4e, 0xa9, 0xef, 0xe0, 0x22, 0xef, - 0x82, 0x63, 0x29, 0x5d, 0x72, 0x7c, 0x47, 0x64, 0x15, 0x77, 0xe2, 0x4a, 0x40, 0x7b, 0x8e, 0x76, - 0x26, 0x34, 0x63, 0x11, 0x24, 0x7a, 0xb3, 0xab, 0x98, 0xb7, 0x9d, 0xce, 0x82, 0xe3, 0x2a, 0xb4, - 0xe4, 0xf8, 0xae, 0x90, 0xc9, 0x00, 0x71, 0x2b, 0x48, 0x7b, 0x83, 0x76, 0x02, 0x9a, 0x02, 0x8b, - 0x72, 0x7d, 0xab, 0xfc, 0xf2, 0x03, 0x4b, 0xf8, 0x66, 0x15, 0xbe, 0xc9, 0x92, 0x7a, 0xd6, 0x11, - 0x44, 0x89, 0x73, 0x20, 0xab, 0xa9, 0x14, 0x75, 0x5e, 0x19, 0x20, 0x6e, 0x05, 0x69, 0x2f, 0x50, - 0x4b, 0x1e, 0x21, 0xd3, 0x6f, 0x75, 0x15, 0xb3, 0xe5, 0x1c, 0x2c, 0x38, 0xae, 0x83, 0x4b, 0x8e, - 0xef, 0xad, 0x89, 0x21, 0x23, 0x6e, 0x0d, 0x1f, 0x6e, 0xfd, 0xf8, 0x82, 0x1b, 0xa4, 0x83, 0x1e, - 0x5e, 0xd3, 0x23, 0x97, 0xb2, 0x14, 0x12, 0x46, 0xc9, 0x47, 0x15, 0xed, 0x9d, 0xb0, 0x70, 0x20, - 0x54, 0x37, 0xdf, 0x44, 0x17, 0x6d, 0x7b, 0x31, 0x8c, 0x93, 0x5c, 0x57, 0xff, 0x64, 0xd6, 0xaa, - 0x75, 0x42, 0x50, 0xb7, 0x4e, 0xdc, 0x89, 0x2b, 0x81, 0x75, 0xa7, 0x9a, 0xff, 0xec, 0x94, 0x81, - 0xf6, 0xaf, 0x73, 0x62, 0x65, 0xd5, 0x27, 0xb5, 0x1c, 0xf7, 0xd3, 0x34, 0xf8, 0x8f, 0xc7, 0x7d, - 0x6d, 0xaa, 0x7e, 0xb5, 0x62, 0x65, 0xd5, 0x7b, 0xa4, 0x15, 0x43, 0x37, 0x02, 0x76, 0xf3, 0x46, - 0xc9, 0x8a, 0xf6, 0x51, 0xfb, 0xf7, 0x27, 0xab, 0x82, 0x9c, 0x57, 0x17, 0x33, 0x43, 0xb9, 0x9c, - 0x19, 0xca, 0xf7, 0x99, 0xa1, 0x7c, 0x9e, 0x1b, 0x8d, 0xcb, 0xb9, 0xd1, 0xf8, 0x36, 0x37, 0x1a, - 0x6f, 0xfb, 0x61, 0x94, 0x9f, 0x8d, 0x87, 0x96, 0x0f, 0xb1, 0x0d, 0x93, 0xcc, 0x1f, 0x9d, 0xdb, - 0x62, 0x41, 0x7e, 0xb8, 0xba, 0x22, 0xf3, 0x69, 0x4a, 0x59, 0xb5, 0x28, 0x87, 0xdb, 0xe5, 0xfe, - 0x7b, 0xf6, 0x33, 0x00, 0x00, 0xff, 0xff, 0x0e, 0x05, 0x44, 0xee, 0xd4, 0x05, 0x00, 0x00, + // 530 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xbc, 0x54, 0xc1, 0x6e, 0xd3, 0x40, + 0x10, 0x8d, 0x9d, 0x92, 0x2a, 0x5b, 0x40, 0xc8, 0xf4, 0x60, 0x42, 0xe3, 0x4d, 0x17, 0x09, 0x05, + 0x09, 0xd9, 0x4a, 0x38, 0x20, 0xf5, 0x82, 0xe4, 0x46, 0x42, 0x95, 0xe8, 0xc5, 0xa8, 0x1c, 0x10, + 0x17, 0xc7, 0x5e, 0xb9, 0x56, 0x63, 0x8f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 0x95, 0x55, 0xdf, 0x95, 0x62, 0xdd, 0x4f, 0x12, 0xff, 0x56, 0xd6, 0xfd, + 0xba, 0x0b, 0xb9, 0x36, 0xf7, 0x3f, 0x8b, 0x5d, 0x35, 0xf3, 0x11, 0x69, 0xf9, 0x5a, 0x8c, 0x80, + 0xdd, 0x7c, 0x2b, 0xb2, 0xa2, 0x3d, 0xd4, 0xfa, 0xfb, 0x97, 0x65, 0x41, 0xf6, 0x9b, 0xf3, 0x99, + 0xa1, 0x5c, 0xcc, 0x0c, 0xe5, 0xe7, 0xcc, 0x50, 0xbe, 0xcc, 0x8d, 0xda, 0xc5, 0xdc, 0xa8, 0xfd, + 0x98, 0x1b, 0xb5, 0xf7, 0xfd, 0x20, 0xcc, 0x4e, 0xc7, 0x43, 0xd3, 0x83, 0xc8, 0x82, 0x49, 0xea, + 0x8d, 0xce, 0x2c, 0x71, 0xc2, 0x3e, 0x5d, 0x3e, 0x62, 0xd9, 0x34, 0xa1, 0xac, 0x3c, 0x65, 0xc3, + 0x46, 0x71, 0xa1, 0x5e, 0xfc, 0x0e, 0x00, 0x00, 0xff, 0xff, 0x12, 0x92, 0xa9, 0xcb, 0x76, 0x05, + 0x00, 0x00, } func (m *MsgCreateDeployment) Marshal() (dAtA []byte, err error) { @@ -664,20 +657,6 @@ func (m *MsgUpdateDeployment) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x1a } - if len(m.Groups) > 0 { - for iNdEx := len(m.Groups) - 1; iNdEx >= 0; iNdEx-- { - { - size, err := m.Groups[iNdEx].MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintDeploymentmsg(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x12 - } - } { size, err := m.ID.MarshalToSizedBuffer(dAtA[:i]) if err != nil { @@ -851,12 +830,6 @@ func (m *MsgUpdateDeployment) Size() (n int) { _ = l l = m.ID.Size() n += 1 + l + sovDeploymentmsg(uint64(l)) - if len(m.Groups) > 0 { - for _, e := range m.Groups { - l = e.Size() - n += 1 + l + sovDeploymentmsg(uint64(l)) - } - } l = len(m.Version) if l > 0 { n += 1 + l + sovDeploymentmsg(uint64(l)) @@ -1425,40 +1398,6 @@ func (m *MsgUpdateDeployment) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Groups", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowDeploymentmsg - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthDeploymentmsg - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthDeploymentmsg - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Groups = append(m.Groups, GroupSpec{}) - if err := m.Groups[len(m.Groups)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex case 3: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Version", wireType) diff --git a/x/deployment/types/v1beta2/msgs.go b/x/deployment/types/v1beta2/msgs.go index b4706bbb6d..dd1bc1bbf0 100644 --- a/x/deployment/types/v1beta2/msgs.go +++ b/x/deployment/types/v1beta2/msgs.go @@ -134,10 +134,9 @@ func (msg MsgDepositDeployment) ValidateBasic() error { } // NewMsgUpdateDeployment creates a new MsgUpdateDeployment instance -func NewMsgUpdateDeployment(id DeploymentID, groups []GroupSpec, version []byte) *MsgUpdateDeployment { +func NewMsgUpdateDeployment(id DeploymentID, version []byte) *MsgUpdateDeployment { return &MsgUpdateDeployment{ ID: id, - Groups: groups, Version: version, } } diff --git a/x/deployment/types/v1beta2/msgs_test.go b/x/deployment/types/v1beta2/msgs_test.go index 7b207c595d..01d66e8226 100644 --- a/x/deployment/types/v1beta2/msgs_test.go +++ b/x/deployment/types/v1beta2/msgs_test.go @@ -53,9 +53,6 @@ func TestVersionValidation(t *testing.T) { msg: &types.MsgUpdateDeployment{ ID: testutil.DeploymentID(t), Version: testutil.DeploymentVersion(t), - Groups: []types.GroupSpec{ - testutil.GroupSpec(t), - }, }, err: nil, }, @@ -63,9 +60,6 @@ func TestVersionValidation(t *testing.T) { msg: &types.MsgUpdateDeployment{ ID: testutil.DeploymentID(t), Version: []byte(""), - Groups: []types.GroupSpec{ - testutil.GroupSpec(t), - }, }, err: types.ErrEmptyVersion, }, @@ -73,9 +67,6 @@ func TestVersionValidation(t *testing.T) { msg: &types.MsgUpdateDeployment{ ID: testutil.DeploymentID(t), Version: []byte("invalidversion"), - Groups: []types.GroupSpec{ - testutil.GroupSpec(t), - }, }, err: types.ErrInvalidVersion, }, diff --git a/x/inflation/simulation/genesis.go b/x/inflation/simulation/genesis.go index 76cb613df2..e35158a8e8 100644 --- a/x/inflation/simulation/genesis.go +++ b/x/inflation/simulation/genesis.go @@ -3,6 +3,7 @@ package simulation import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" + types "github.com/ovrclk/akash/x/inflation/types/v1beta2" ) @@ -10,7 +11,7 @@ import ( func RandomizedGenState(simState *module.SimulationState) { deploymentGenesis := &types.GenesisState{ Params: types.Params{ - InflationDecayFactor: 2, + InflationDecayFactor: types.DefaultInflationDecayFactor(), InitialInflation: sdk.NewDec(100), Variance: sdk.MustNewDecFromStr("0.05"), }, diff --git a/x/inflation/types/v1beta2/inflation_calculator.go b/x/inflation/types/v1beta2/inflation_calculator.go index 8aa52757c7..4e139a1b58 100644 --- a/x/inflation/types/v1beta2/inflation_calculator.go +++ b/x/inflation/types/v1beta2/inflation_calculator.go @@ -1,6 +1,7 @@ package v1beta2 import ( + "fmt" "time" sdk "github.com/cosmos/cosmos-sdk/types" @@ -17,74 +18,84 @@ func GetInflationCalculator( var inflationParams Params inflationParamSubspace.GetParamSet(ctx, &inflationParams) - // years passed since genesis = seconds passed since genesis / number of seconds per year - // can be a fraction, eg: 0.5 - yearsPassed := decimal.WithPrecision(sdk.Precision). - Quo( - // seconds since genesis - decimal.WithPrecision(sdk.Precision). - Sub( - decimal.New(ctx.BlockTime().Unix(), 0), - decimal.New(genesisTime.Unix(), 0), - ), - // Number of hours in an year = 8766 (averaging the leap year hours) - // Number of minutes in an hour = 60 - // Number of seconds in a minute = 60 - // => Number of seconds per year = 60 * 60 * 8766 = 31557600 - decimal.New(31557600, 0), - ) - // 2^(-t/tHalf) - pow := decimal.WithPrecision(sdk.Precision) - pow = pow.Context. - Pow( - pow, - decimal.New(2, 0), - decimal.WithPrecision(sdk.Precision). - Mul( - decimal.New(-1, 0), - decimal.WithPrecision(sdk.Precision). - Quo( - yearsPassed, - decimal.New(int64(inflationParams.InflationDecayFactor), 0), - ), - ), - ) - // convert pow to sdk.Dec with a 6 unit precision: sdk.Decimal(big.Int(pow * 10^6)) / 10^6 - powDec := sdk.NewDecFromBigInt( + return inflationCalculator(ctx.BlockTime(), genesisTime, minter, params, inflationParams, bondedRatio) + } +} + +// inflationCalculator calculate current inflation value +// - btime - block time from sdk.Context +// - gtime - genesis time +func inflationCalculator(btime, gtime time.Time, minter minttypes.Minter, mparams minttypes.Params, iparams Params, bondedRatio sdk.Dec) sdk.Dec { + inflationDecayFactor := new(decimal.Big) + if _, valid := inflationDecayFactor.SetString(iparams.InflationDecayFactor.String()); !valid { + panic(fmt.Sprintf("InflationDecayFactor contains invalid value [%s]. expected integer/float", iparams.InflationDecayFactor.String())) + } + + // years passed since genesis = seconds passed since genesis / number of seconds per year + // can be a fraction, eg: 0.5 + yearsPassed := decimal.WithPrecision(sdk.Precision). + Quo( + // seconds since genesis + decimal.WithPrecision(sdk.Precision). + Sub( + decimal.New(btime.Unix(), 0), + decimal.New(gtime.Unix(), 0), + ), + // Number of hours in an year = 8766 (averaging the leap year hours) + // Number of minutes in an hour = 60 + // Number of seconds in a minute = 60 + // => Number of seconds per year = 60 * 60 * 8766 = 31557600 + decimal.New(31557600, 0), + ) + // 2^(-t/tHalf) + inflationCoefDec := decimal.WithPrecision(sdk.Precision) + inflationCoefDec = inflationCoefDec.Context. + Pow( + inflationCoefDec, + decimal.New(2, 0), decimal.WithPrecision(sdk.Precision). - Mul(pow, decimal.New(1000000, 0)). - Int(nil), - ).QuoInt64(1000000) + Mul( + decimal.New(-1, 0), + decimal.WithPrecision(sdk.Precision). + Quo(yearsPassed, inflationDecayFactor), + ), + ) + // convert inflationCoefDec to sdk.Dec with a 6 unit precision: sdk.Decimal(big.Int(pow * 10^6)) / 10^6 + inflationCoef := sdk.NewDecFromBigInt( + decimal.WithPrecision(sdk.Precision). + Mul(inflationCoefDec, decimal.New(1000000, 0)). + Int(nil), + ).QuoInt64(1000000) - idealInflation := inflationParams.InitialInflation.Mul(powDec) + idealInflation := iparams.InitialInflation.Mul(inflationCoef) - // (1 - bondedRatio/GoalBonded) * InflationRateChange - inflationRateChangePerYear := sdk.OneDec(). - Sub(bondedRatio.Quo(params.GoalBonded)). - Mul(params.InflationRateChange) - inflationRateChange := inflationRateChangePerYear.Quo(sdk.NewDec(int64(params.BlocksPerYear))) + // (1 - bondedRatio/GoalBonded) * InflationRateChange + inflationRateChangePerYear := sdk.OneDec(). + Sub(bondedRatio.Quo(mparams.GoalBonded)). + Mul(mparams.InflationRateChange) - // note inflationRateChange may be negative - currentInflation := minter.Inflation.Add(inflationRateChange) + inflationRateChange := inflationRateChangePerYear.Quo(sdk.NewDecFromInt(sdk.NewIntFromUint64(mparams.BlocksPerYear))) - // min, max currentInflation based on a defined range parameter 'r' - // currentInflation range = [I(t) - I(t) * R, I(t) + I(t) * R] - r := inflationParams.Variance - minInflation := idealInflation.Sub(idealInflation.Mul(r)) - maxInflation := idealInflation.Add(idealInflation.Mul(r)) + sdk.NewDecFromInt(sdk.NewIntFromUint64(mparams.BlocksPerYear)) - // minInflation >= minimumMinInflation - minimumMinInflation := sdk.ZeroDec() // 0 for now - if minInflation.LT(minimumMinInflation) { - minInflation = minimumMinInflation - } + // note inflationRateChange may be negative + currentInflation := minter.Inflation.Add(inflationRateChange) - if currentInflation.LT(minInflation) { - currentInflation = minInflation - } else if currentInflation.GT(maxInflation) { - currentInflation = maxInflation - } + // min, max currentInflation based on a defined range parameter 'r' + // currentInflation range = [I(t) - I(t) * R, I(t) + I(t) * R] + // R is from iparams.Variance + minInflation := idealInflation.Sub(idealInflation.Mul(iparams.Variance)) + maxInflation := idealInflation.Add(idealInflation.Mul(iparams.Variance)) - return currentInflation + // the lowest possible value of minInflation is set for 0 + // tho it can be set to higher value in the future + minInflation = sdk.MaxDec(sdk.ZeroDec(), minInflation) + + if currentInflation.LT(minInflation) { + currentInflation = minInflation + } else if currentInflation.GT(maxInflation) { + currentInflation = maxInflation } + + return currentInflation } diff --git a/x/inflation/types/v1beta2/inflation_calculator_test.go b/x/inflation/types/v1beta2/inflation_calculator_test.go new file mode 100644 index 0000000000..1bdf7eaadd --- /dev/null +++ b/x/inflation/types/v1beta2/inflation_calculator_test.go @@ -0,0 +1,82 @@ +package v1beta2 + +import ( + "testing" + "time" + + sdk "github.com/cosmos/cosmos-sdk/types" + minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" + "github.com/stretchr/testify/suite" +) + +const ( + blocksPerYear = 5256000 +) + +type InflationCalculatorTestSuite struct { + suite.Suite + params Params + genesistime time.Time +} + +func (s *InflationCalculatorTestSuite) SetupSuite() { + var err error + s.genesistime, err = time.Parse(time.RFC3339, "2021-03-08T15:00:00Z") + s.Require().NoError(err) + + s.params.InflationDecayFactor, err = sdk.NewDecFromStr("2.10306569") + s.Require().NoError(err) + + s.params.InitialInflation, err = sdk.NewDecFromStr("48.546257") + s.Require().NoError(err) + + s.params.Variance, err = sdk.NewDecFromStr("0.05") + s.Require().NoError(err) +} + +func TestIntegrationTestSuite(t *testing.T) { + suite.Run(t, new(InflationCalculatorTestSuite)) +} + +func (s *InflationCalculatorTestSuite) TestInflationCalculatorInvalidDecayFactor() { + testFn := func() { + inflationCalculator( + time.Time{}, + time.Time{}, + minttypes.Minter{}, + minttypes.Params{}, + Params{}, + sdk.Dec{}) + } + + s.Panics(testFn) +} + +func (s *InflationCalculatorTestSuite) TestInflationCalculator1() { + goalBonded, err := sdk.NewDecFromStr("0.67") + s.Require().NoError(err) + + currBonded, err := sdk.NewDecFromStr("0.7324") + s.Require().NoError(err) + + currInflation, err := sdk.NewDecFromStr("0.230326319830867266") + s.Require().NoError(err) + + blockTime, _ := time.Parse(time.RFC3339, "2022-04-18T18:28:26+00:00") + + res := inflationCalculator( + blockTime, + s.genesistime, + minttypes.Minter{ + Inflation: currInflation, + }, + minttypes.Params{ + BlocksPerYear: blocksPerYear, + GoalBonded: goalBonded, + InflationRateChange: s.params.Variance, + }, + s.params, + currBonded) + + s.Require().Equal("31.967899564902300000", res.String()) +} diff --git a/x/inflation/types/v1beta2/params.go b/x/inflation/types/v1beta2/params.go index 9d3406643c..e5aaa85c1c 100644 --- a/x/inflation/types/v1beta2/params.go +++ b/x/inflation/types/v1beta2/params.go @@ -10,13 +10,13 @@ import ( var _ paramtypes.ParamSet = (*Params)(nil) const ( - DefaultInflationDecayFactor uint32 = 2 // years - keyInflationDecayFactor = "InflationDecayFactor" keyInitialInflation = "InitialInflation" keyVariance = "Variance" ) +func DefaultInflationDecayFactor() sdk.Dec { return sdk.NewDec(2) } // years + func DefaultInitialInflation() sdk.Dec { return sdk.NewDec(100) } func DefaultVarince() sdk.Dec { return sdk.MustNewDecFromStr("0.05") } @@ -40,7 +40,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { func DefaultParams() Params { return Params{ - InflationDecayFactor: DefaultInflationDecayFactor, + InflationDecayFactor: DefaultInflationDecayFactor(), InitialInflation: DefaultInitialInflation(), Variance: DefaultVarince(), } @@ -61,8 +61,8 @@ func (p Params) Validate() error { } func validateInflationDecayFactor(i interface{}) error { - v, ok := i.(uint32) - if !ok || v < 1 { + v, ok := i.(sdk.Dec) + if !ok || v.LT(sdk.NewDec(1)) { return errors.Wrapf(ErrInvalidParam, "%T", i) } diff --git a/x/inflation/types/v1beta2/params.pb.go b/x/inflation/types/v1beta2/params.pb.go index 494d36b175..2cd66c5753 100644 --- a/x/inflation/types/v1beta2/params.pb.go +++ b/x/inflation/types/v1beta2/params.pb.go @@ -27,7 +27,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // Params defines the parameters for the x/deployment package type Params struct { // InflationDecayFactor is the number of years it takes inflation to halve. - InflationDecayFactor uint32 `protobuf:"varint,1,opt,name=inflation_decay_factor,json=inflationDecayFactor,proto3" json:"inflation_decay_factor" yaml:"inflation_decay_factor"` + InflationDecayFactor github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,1,opt,name=inflation_decay_factor,json=inflationDecayFactor,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"inflation_decay_factor" yaml:"inflation_decay_factor"` // InitialInflation is the rate at which inflation starts at genesis. // It is a decimal value in the range [0.0, 100.0]. InitialInflation github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=initial_inflation,json=initialInflation,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"initial_inflation" yaml:"initial_inflation"` @@ -69,13 +69,6 @@ func (m *Params) XXX_DiscardUnknown() { var xxx_messageInfo_Params proto.InternalMessageInfo -func (m *Params) GetInflationDecayFactor() uint32 { - if m != nil { - return m.InflationDecayFactor - } - return 0 -} - func init() { proto.RegisterType((*Params)(nil), "akash.inflation.v1beta2.Params") } @@ -85,29 +78,29 @@ func init() { } var fileDescriptor_fea313162cb1e23f = []byte{ - // 349 bytes of a gzipped FileDescriptorProto + // 347 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x49, 0xcc, 0x4e, 0x2c, 0xce, 0xd0, 0xcf, 0xcc, 0x4b, 0xcb, 0x49, 0x2c, 0xc9, 0xcc, 0xcf, 0xd3, 0x2f, 0x33, 0x4c, 0x4a, 0x2d, 0x49, 0x34, 0xd2, 0x2f, 0x48, 0x2c, 0x4a, 0xcc, 0x2d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x07, 0xab, 0xd2, 0x83, 0xab, 0xd2, 0x83, 0xaa, 0x92, 0x12, 0x49, 0xcf, 0x4f, 0xcf, - 0x07, 0xab, 0xd1, 0x07, 0xb1, 0x20, 0xca, 0x95, 0xd6, 0x31, 0x73, 0xb1, 0x05, 0x80, 0xf5, 0x0b, - 0x75, 0x32, 0x72, 0x89, 0xc1, 0xb5, 0xc5, 0xa7, 0xa4, 0x26, 0x27, 0x56, 0xc6, 0xa7, 0x25, 0x26, - 0x97, 0xe4, 0x17, 0x49, 0x30, 0x2a, 0x30, 0x6a, 0xf0, 0x3a, 0x05, 0x3f, 0xba, 0x27, 0x2f, 0xe2, - 0x09, 0x53, 0xe1, 0x02, 0x52, 0xe0, 0x06, 0x96, 0x7f, 0x75, 0x4f, 0x1e, 0x87, 0xce, 0x4f, 0xf7, - 0xe4, 0x65, 0x2b, 0x13, 0x73, 0x73, 0xac, 0x94, 0xb0, 0xcb, 0x2b, 0x05, 0x89, 0x64, 0x62, 0x31, - 0x50, 0x68, 0x01, 0x23, 0x97, 0x60, 0x66, 0x5e, 0x66, 0x49, 0x66, 0x62, 0x4e, 0x3c, 0x5c, 0x81, - 0x04, 0x93, 0x02, 0xa3, 0x06, 0xa7, 0x53, 0xf1, 0x89, 0x7b, 0xf2, 0x0c, 0xb7, 0xee, 0xc9, 0xab, - 0xa5, 0x67, 0x96, 0x64, 0x94, 0x26, 0xe9, 0x25, 0xe7, 0xe7, 0xea, 0x27, 0xe7, 0x17, 0xe7, 0xe6, - 0x17, 0x43, 0x29, 0xdd, 0xe2, 0x94, 0x6c, 0xfd, 0x92, 0xca, 0x82, 0xd4, 0x62, 0x3d, 0x97, 0xd4, - 0xe4, 0x47, 0xf7, 0xe4, 0x05, 0x3c, 0x21, 0x46, 0xc1, 0xdd, 0xfe, 0xea, 0x9e, 0x3c, 0xa6, 0xf1, - 0x9f, 0xee, 0xc9, 0x4b, 0xc0, 0xdc, 0x8a, 0x26, 0xa5, 0x14, 0x24, 0x90, 0x89, 0x66, 0x84, 0x50, - 0x29, 0x17, 0x47, 0x59, 0x62, 0x51, 0x66, 0x62, 0x5e, 0x72, 0xaa, 0x04, 0x33, 0xd8, 0x61, 0x91, - 0x24, 0x3b, 0x8c, 0x23, 0x0c, 0x6a, 0xc2, 0xab, 0x7b, 0xf2, 0x70, 0xd3, 0x3e, 0xdd, 0x93, 0xe7, - 0x87, 0xb8, 0x03, 0x26, 0xa2, 0x14, 0x04, 0x97, 0x74, 0xf2, 0x3e, 0xf1, 0x48, 0x8e, 0xf1, 0xc2, - 0x23, 0x39, 0xc6, 0x07, 0x8f, 0xe4, 0x18, 0x27, 0x3c, 0x96, 0x63, 0xb8, 0xf0, 0x58, 0x8e, 0xe1, - 0xc6, 0x63, 0x39, 0x86, 0x28, 0x43, 0x24, 0x6b, 0xf3, 0xcb, 0x8a, 0x92, 0x73, 0xb2, 0xf5, 0x21, - 0x29, 0xa6, 0x02, 0x29, 0xcd, 0x80, 0x6d, 0x87, 0xa5, 0x9c, 0x24, 0x36, 0x70, 0x22, 0x30, 0x06, - 0x04, 0x00, 0x00, 0xff, 0xff, 0x24, 0x5e, 0xdd, 0xdf, 0x5b, 0x02, 0x00, 0x00, + 0x07, 0xab, 0xd1, 0x07, 0xb1, 0x20, 0xca, 0x95, 0xee, 0x32, 0x73, 0xb1, 0x05, 0x80, 0xf5, 0x0b, + 0xed, 0x60, 0xe4, 0x12, 0x83, 0x6b, 0x8b, 0x4f, 0x49, 0x4d, 0x4e, 0xac, 0x8c, 0x4f, 0x4b, 0x4c, + 0x2e, 0xc9, 0x2f, 0x92, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x74, 0x6a, 0x64, 0x3c, 0x71, 0x4f, 0x9e, + 0x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1c, 0xc3, 0x85, 0xc7, 0x72, 0x0c, 0x37, 0x1e, + 0xcb, 0x31, 0x44, 0x19, 0x22, 0x59, 0x9b, 0x5f, 0x56, 0x94, 0x9c, 0x93, 0xad, 0x0f, 0x49, 0x60, + 0x15, 0x48, 0x49, 0x0c, 0x6c, 0x3b, 0x2c, 0xa1, 0x25, 0xb1, 0x81, 0xd3, 0x8c, 0x31, 0x20, 0x00, + 0x00, 0xff, 0xff, 0x25, 0x66, 0x23, 0xab, 0x8a, 0x02, 0x00, 0x00, } func (m *Params) Marshal() (dAtA []byte, err error) { @@ -150,11 +143,16 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { } i-- dAtA[i] = 0x12 - if m.InflationDecayFactor != 0 { - i = encodeVarintParams(dAtA, i, uint64(m.InflationDecayFactor)) - i-- - dAtA[i] = 0x8 + { + size := m.InflationDecayFactor.Size() + i -= size + if _, err := m.InflationDecayFactor.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintParams(dAtA, i, uint64(size)) } + i-- + dAtA[i] = 0xa return len(dAtA) - i, nil } @@ -175,9 +173,8 @@ func (m *Params) Size() (n int) { } var l int _ = l - if m.InflationDecayFactor != 0 { - n += 1 + sovParams(uint64(m.InflationDecayFactor)) - } + l = m.InflationDecayFactor.Size() + n += 1 + l + sovParams(uint64(l)) l = m.InitialInflation.Size() n += 1 + l + sovParams(uint64(l)) l = m.Variance.Size() @@ -221,10 +218,10 @@ func (m *Params) Unmarshal(dAtA []byte) error { } switch fieldNum { case 1: - if wireType != 0 { + if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field InflationDecayFactor", wireType) } - m.InflationDecayFactor = 0 + var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowParams @@ -234,11 +231,26 @@ func (m *Params) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - m.InflationDecayFactor |= uint32(b&0x7F) << shift + stringLen |= uint64(b&0x7F) << shift if b < 0x80 { break } } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthParams + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthParams + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.InflationDecayFactor.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex case 2: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field InitialInflation", wireType)