From 4e17ee4c47d8cb995ea28c6946c274ce757f68af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Eckel?= Date: Tue, 23 Apr 2024 10:52:11 +0200 Subject: [PATCH] * added donation of tokens/denom to the just attestet machine * extended the machine module to have two additional parameters: amount of tokens to be distributed and their denominator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jürgen Eckel --- proto/planetmintgo/machine/params.proto | 2 + tests/e2e/machine/suite.go | 7 +- util/issue_commands.go | 17 +++ x/machine/keeper/msg_server_attest_machine.go | 18 +++ x/machine/types/errors.go | 2 + x/machine/types/params.go | 13 +- x/machine/types/params.pb.go | 128 +++++++++++++++--- 7 files changed, 160 insertions(+), 27 deletions(-) diff --git a/proto/planetmintgo/machine/params.proto b/proto/planetmintgo/machine/params.proto index 08cd122..5b6c43d 100644 --- a/proto/planetmintgo/machine/params.proto +++ b/proto/planetmintgo/machine/params.proto @@ -11,4 +11,6 @@ message Params { string asset_registry_scheme =1; string asset_registry_domain =2; string asset_registry_path =3; + uint64 dao_machine_funding_amount = 4; + string dao_machine_funding_denom = 5; } diff --git a/tests/e2e/machine/suite.go b/tests/e2e/machine/suite.go index 4b1de37..029f8ba 100644 --- a/tests/e2e/machine/suite.go +++ b/tests/e2e/machine/suite.go @@ -88,6 +88,7 @@ func (s *E2ETestSuite) TestAttestMachine() { err = lib.ErrTypeAssertionFailed s.Require().NoError(err) } + assert.Contains(s.T(), preAttestationBalance.String(), "10000") machine := moduleobject.Machine(sample.Name, pubKey, prvKey, addr.String()) msg2 := machinetypes.NewMsgAttestMachine(addr.String(), &machine) @@ -98,6 +99,7 @@ func (s *E2ETestSuite) TestAttestMachine() { s.Require().NoError(s.network.WaitForNextBlock()) s.Require().NoError(s.network.WaitForNextBlock()) s.Require().NoError(s.network.WaitForNextBlock()) + s.Require().NoError(s.network.WaitForNextBlock()) rawLog, err = clitestutil.GetRawLogFromTxOut(val, out) s.Require().NoError(err) @@ -119,7 +121,7 @@ func (s *E2ETestSuite) TestAttestMachine() { assert.Contains(s.T(), txResp.TxHash, txResponse.TxHash) s.Require().NoError(err) - // Check postAttestationBalance as it should be the same as prior to the machine attestation + // Check postAttestationBalance it should be the preAttestationBalance + th 8800 tokens being donated to the machine (no fees are taken) postAttestationBalanceOutput, err := clitestutil.ExecTestCLICmd(val.ClientCtx, bank.GetBalancesCmd(), []string{ addr.String(), }) @@ -129,8 +131,7 @@ func (s *E2ETestSuite) TestAttestMachine() { err = lib.ErrTypeAssertionFailed s.Require().NoError(err) } - - assert.Equal(s.T(), preAttestationBalance, postAttestationBalance) + assert.Contains(s.T(), postAttestationBalance.String(), "18800") } func (s *E2ETestSuite) TestInvalidAttestMachine() { diff --git a/util/issue_commands.go b/util/issue_commands.go index b46bcd4..e11e874 100644 --- a/util/issue_commands.go +++ b/util/issue_commands.go @@ -4,6 +4,7 @@ import ( "context" sdk "github.com/cosmos/cosmos-sdk/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/planetmint/planetmint-go/config" "github.com/planetmint/planetmint-go/lib" daotypes "github.com/planetmint/planetmint-go/x/dao/types" @@ -99,3 +100,19 @@ func SendUpdateRedeemClaim(goCtx context.Context, beneficiary string, id uint64, loggingContext := "redeem claim" buildSignBroadcastTx(goCtx, loggingContext, sendingValidatorAddress, msg) } + +func SendPLMNTTokens(goCtx context.Context, beneficiary sdk.AccAddress, amount uint64, denominator string) { + sendingValidatorAddress := config.GetConfig().ValidatorAddress + + coin := sdk.NewCoin(denominator, sdk.NewIntFromUint64(amount)) + coins := sdk.NewCoins(coin) + orgAddr, err := sdk.AccAddressFromBech32(sendingValidatorAddress) + if err != nil { + ctx := sdk.UnwrapSDKContext(goCtx) + GetAppLogger().Error(ctx, "sending PLMNT tokens: couldn't convert validator address to Address object") + } + msg := banktypes.NewMsgSend(orgAddr, beneficiary, coins) + + loggingContext := "sending PLMNT tokens" + buildSignBroadcastTx(goCtx, loggingContext, sendingValidatorAddress, msg) +} diff --git a/x/machine/keeper/msg_server_attest_machine.go b/x/machine/keeper/msg_server_attest_machine.go index 5176bab..ec6109b 100644 --- a/x/machine/keeper/msg_server_attest_machine.go +++ b/x/machine/keeper/msg_server_attest_machine.go @@ -2,6 +2,7 @@ package keeper import ( "context" + "fmt" config "github.com/planetmint/planetmint-go/config" "github.com/planetmint/planetmint-go/util" @@ -50,6 +51,8 @@ func (k msgServer) AttestMachine(goCtx context.Context, msg *types.MsgAttestMach } else { util.GetAppLogger().Info(ctx, "Machine NFT issuance successful: "+msg.Machine.String()) } + + k.sendInitialFundingTokensToMachine(goCtx, msg.GetMachine().GetAddress(), params) } else { util.GetAppLogger().Info(ctx, "Not block proposer: skipping Machine NFT issuance") } @@ -60,9 +63,24 @@ func (k msgServer) AttestMachine(goCtx context.Context, msg *types.MsgAttestMach if err != nil { return nil, err } + return &types.MsgAttestMachineResponse{}, err } +func (k msgServer) sendInitialFundingTokensToMachine(goCtx context.Context, machineAddressString string, keeperParams types.Params) { + ctx := sdk.UnwrapSDKContext(goCtx) + machineAddress, err := sdk.AccAddressFromBech32(machineAddressString) + if err != nil { + util.GetAppLogger().Error(ctx, "error: for provided address "+machineAddress.String()) + return + } + + logMsg := fmt.Sprintf("transferring %v tokens to address %s", keeperParams.GetDaoMachineFundingAmount(), machineAddress.String()) + util.GetAppLogger().Info(ctx, logMsg) + util.SendPLMNTTokens(goCtx, machineAddress, keeperParams.GetDaoMachineFundingAmount(), keeperParams.DaoMachineFundingDenom) + return +} + func validateExtendedPublicKey(issuer string, cfg chaincfg.Params) bool { xpubKey, err := hdkeychain.NewKeyFromString(issuer) if err != nil { diff --git a/x/machine/types/errors.go b/x/machine/types/errors.go index 1fa6a54..1613f53 100644 --- a/x/machine/types/errors.go +++ b/x/machine/types/errors.go @@ -21,4 +21,6 @@ var ( ErrAssetRegistryReqFailure = errorsmod.Register(ModuleName, 13, "request to asset registry could not be created") ErrAssetRegistryReqSending = errorsmod.Register(ModuleName, 14, "request to asset registry could not be sent") ErrAssetRegistryRepsonse = errorsmod.Register(ModuleName, 15, "request response issue") + ErrInvalidAddress = errorsmod.Register(ModuleName, 16, "invalid address") + ErrTransferFailed = errorsmod.Register(ModuleName, 17, "transfer failed") ) diff --git a/x/machine/types/params.go b/x/machine/types/params.go index 5eecf07..2398da0 100644 --- a/x/machine/types/params.go +++ b/x/machine/types/params.go @@ -13,17 +13,20 @@ func ParamKeyTable() paramtypes.KeyTable { } // NewParams creates a new Params instance -func NewParams(assetRegistryScheme string, assetRegistryDomain string, assetRegistryPath string) Params { +func NewParams(assetRegistryScheme string, assetRegistryDomain string, assetRegistryPath string, + daoMachineFundingAmount uint64, daoMachineFundingDenom string) Params { return Params{ - AssetRegistryScheme: assetRegistryScheme, - AssetRegistryDomain: assetRegistryDomain, - AssetRegistryPath: assetRegistryPath, + AssetRegistryScheme: assetRegistryScheme, + AssetRegistryDomain: assetRegistryDomain, + AssetRegistryPath: assetRegistryPath, + DaoMachineFundingAmount: daoMachineFundingAmount, + DaoMachineFundingDenom: daoMachineFundingDenom, } } // DefaultParams returns a default set of parameters func DefaultParams() Params { - return NewParams("https", "testnet-assets.rddl.io", "register_asset") + return NewParams("https", "testnet-assets.rddl.io", "register_asset", 8800, "plmnt") } // ParamSetPairs get the params.ParamSet diff --git a/x/machine/types/params.pb.go b/x/machine/types/params.pb.go index 67e24e1..9771547 100644 --- a/x/machine/types/params.pb.go +++ b/x/machine/types/params.pb.go @@ -25,9 +25,11 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // Params defines the parameters for the module. type Params struct { - AssetRegistryScheme string `protobuf:"bytes,1,opt,name=asset_registry_scheme,json=assetRegistryScheme,proto3" json:"asset_registry_scheme,omitempty"` - AssetRegistryDomain string `protobuf:"bytes,2,opt,name=asset_registry_domain,json=assetRegistryDomain,proto3" json:"asset_registry_domain,omitempty"` - AssetRegistryPath string `protobuf:"bytes,3,opt,name=asset_registry_path,json=assetRegistryPath,proto3" json:"asset_registry_path,omitempty"` + AssetRegistryScheme string `protobuf:"bytes,1,opt,name=asset_registry_scheme,json=assetRegistryScheme,proto3" json:"asset_registry_scheme,omitempty"` + AssetRegistryDomain string `protobuf:"bytes,2,opt,name=asset_registry_domain,json=assetRegistryDomain,proto3" json:"asset_registry_domain,omitempty"` + AssetRegistryPath string `protobuf:"bytes,3,opt,name=asset_registry_path,json=assetRegistryPath,proto3" json:"asset_registry_path,omitempty"` + DaoMachineFundingAmount uint64 `protobuf:"varint,4,opt,name=dao_machine_funding_amount,json=daoMachineFundingAmount,proto3" json:"dao_machine_funding_amount,omitempty"` + DaoMachineFundingDenom string `protobuf:"bytes,5,opt,name=dao_machine_funding_denom,json=daoMachineFundingDenom,proto3" json:"dao_machine_funding_denom,omitempty"` } func (m *Params) Reset() { *m = Params{} } @@ -83,6 +85,20 @@ func (m *Params) GetAssetRegistryPath() string { return "" } +func (m *Params) GetDaoMachineFundingAmount() uint64 { + if m != nil { + return m.DaoMachineFundingAmount + } + return 0 +} + +func (m *Params) GetDaoMachineFundingDenom() string { + if m != nil { + return m.DaoMachineFundingDenom + } + return "" +} + func init() { proto.RegisterType((*Params)(nil), "planetmintgo.machine.Params") } @@ -90,22 +106,26 @@ func init() { func init() { proto.RegisterFile("planetmintgo/machine/params.proto", fileDescriptor_84cd778d65e6639c) } var fileDescriptor_84cd778d65e6639c = []byte{ - // 233 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x2c, 0xc8, 0x49, 0xcc, - 0x4b, 0x2d, 0xc9, 0xcd, 0xcc, 0x2b, 0x49, 0xcf, 0xd7, 0xcf, 0x4d, 0x4c, 0xce, 0xc8, 0xcc, 0x4b, - 0xd5, 0x2f, 0x48, 0x2c, 0x4a, 0xcc, 0x2d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x41, - 0x56, 0xa2, 0x07, 0x55, 0x22, 0x25, 0x92, 0x9e, 0x9f, 0x9e, 0x0f, 0x56, 0xa0, 0x0f, 0x62, 0x41, - 0xd4, 0x2a, 0x2d, 0x63, 0xe4, 0x62, 0x0b, 0x00, 0x6b, 0x16, 0x32, 0xe2, 0x12, 0x4d, 0x2c, 0x2e, - 0x4e, 0x2d, 0x89, 0x2f, 0x4a, 0x4d, 0xcf, 0x2c, 0x2e, 0x29, 0xaa, 0x8c, 0x2f, 0x4e, 0xce, 0x48, - 0xcd, 0x4d, 0x95, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0c, 0x12, 0x06, 0x4b, 0x06, 0x41, 0xe5, 0x82, - 0xc1, 0x52, 0x58, 0xf4, 0xa4, 0xe4, 0xe7, 0x26, 0x66, 0xe6, 0x49, 0x30, 0x61, 0xd1, 0xe3, 0x02, - 0x96, 0x12, 0xd2, 0xe3, 0x12, 0x46, 0xd3, 0x53, 0x90, 0x58, 0x92, 0x21, 0xc1, 0x0c, 0xd6, 0x21, - 0x88, 0xa2, 0x23, 0x20, 0xb1, 0x24, 0xc3, 0x8a, 0x65, 0xc6, 0x02, 0x79, 0x06, 0x27, 0xdf, 0x13, - 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, 0x8e, 0x71, 0xc2, 0x63, 0x39, 0x86, - 0x0b, 0x8f, 0xe5, 0x18, 0x6e, 0x3c, 0x96, 0x63, 0x88, 0x32, 0x4e, 0xcf, 0x2c, 0xc9, 0x28, 0x4d, - 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x47, 0xf8, 0x1c, 0x89, 0xa9, 0x9b, 0x9e, 0xaf, 0x5f, 0x01, 0x0f, - 0xaa, 0x92, 0xca, 0x82, 0xd4, 0xe2, 0x24, 0x36, 0xb0, 0xf7, 0x8d, 0x01, 0x01, 0x00, 0x00, 0xff, - 0xff, 0x50, 0x39, 0xf4, 0xa3, 0x4f, 0x01, 0x00, 0x00, + // 295 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x91, 0xb1, 0x4e, 0xeb, 0x30, + 0x14, 0x40, 0xe3, 0xbe, 0xbc, 0x4a, 0x78, 0x23, 0x2d, 0x10, 0x3a, 0x98, 0xc2, 0xd4, 0x85, 0x44, + 0xa2, 0x13, 0x30, 0x81, 0x2a, 0xb6, 0x4a, 0x55, 0xd8, 0x58, 0x22, 0x37, 0x31, 0x8e, 0x25, 0xec, + 0x1b, 0xc5, 0x8e, 0x44, 0xff, 0x82, 0x91, 0x09, 0xf1, 0x39, 0x8c, 0x1d, 0x19, 0x51, 0xf2, 0x23, + 0xa8, 0x4e, 0x04, 0x05, 0xb2, 0x5d, 0xe9, 0x9c, 0x23, 0xcb, 0xf7, 0xe2, 0xe3, 0xfc, 0x81, 0x2a, + 0x66, 0xa4, 0x50, 0x86, 0x43, 0x28, 0x69, 0x92, 0x09, 0xc5, 0xc2, 0x9c, 0x16, 0x54, 0xea, 0x20, + 0x2f, 0xc0, 0x80, 0x37, 0xdc, 0x56, 0x82, 0x56, 0x19, 0x0d, 0x39, 0x70, 0xb0, 0x42, 0xb8, 0x99, + 0x1a, 0xf7, 0xe4, 0xa5, 0x87, 0xfb, 0x0b, 0x1b, 0x7b, 0x67, 0x78, 0x8f, 0x6a, 0xcd, 0x4c, 0x5c, + 0x30, 0x2e, 0xb4, 0x29, 0x56, 0xb1, 0x4e, 0x32, 0x26, 0x99, 0x8f, 0xc6, 0x68, 0xb2, 0x13, 0x0d, + 0x2c, 0x8c, 0x5a, 0x76, 0x6b, 0x51, 0x47, 0x93, 0x82, 0xa4, 0x42, 0xf9, 0xbd, 0x8e, 0x66, 0x66, + 0x91, 0x17, 0xe0, 0xc1, 0xaf, 0x26, 0xa7, 0x26, 0xf3, 0xff, 0xd9, 0x62, 0xf7, 0x47, 0xb1, 0xa0, + 0x26, 0xf3, 0x2e, 0xf1, 0x28, 0xa5, 0x10, 0xb7, 0xff, 0x88, 0xef, 0x4b, 0x95, 0x0a, 0xc5, 0x63, + 0x2a, 0xa1, 0x54, 0xc6, 0x77, 0xc7, 0x68, 0xe2, 0x46, 0x07, 0x29, 0x85, 0x79, 0x23, 0xdc, 0x34, + 0xfc, 0xca, 0x62, 0xef, 0x1c, 0x1f, 0x76, 0xc5, 0x29, 0x53, 0x20, 0xfd, 0xff, 0xf6, 0xc9, 0xfd, + 0x3f, 0xed, 0x6c, 0x43, 0x2f, 0xdc, 0xe7, 0xd7, 0x23, 0xe7, 0x7a, 0xfe, 0x56, 0x11, 0xb4, 0xae, + 0x08, 0xfa, 0xa8, 0x08, 0x7a, 0xaa, 0x89, 0xb3, 0xae, 0x89, 0xf3, 0x5e, 0x13, 0xe7, 0x6e, 0xca, + 0x85, 0xc9, 0xca, 0x65, 0x90, 0x80, 0x0c, 0xbf, 0x37, 0xbe, 0x35, 0x9e, 0x72, 0x08, 0x1f, 0xbf, + 0x4e, 0x64, 0x56, 0x39, 0xd3, 0xcb, 0xbe, 0x5d, 0xfb, 0xf4, 0x33, 0x00, 0x00, 0xff, 0xff, 0xb0, + 0xd8, 0x7a, 0xbe, 0xc7, 0x01, 0x00, 0x00, } func (m *Params) Marshal() (dAtA []byte, err error) { @@ -128,6 +148,18 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.DaoMachineFundingDenom) > 0 { + i -= len(m.DaoMachineFundingDenom) + copy(dAtA[i:], m.DaoMachineFundingDenom) + i = encodeVarintParams(dAtA, i, uint64(len(m.DaoMachineFundingDenom))) + i-- + dAtA[i] = 0x2a + } + if m.DaoMachineFundingAmount != 0 { + i = encodeVarintParams(dAtA, i, uint64(m.DaoMachineFundingAmount)) + i-- + dAtA[i] = 0x20 + } if len(m.AssetRegistryPath) > 0 { i -= len(m.AssetRegistryPath) copy(dAtA[i:], m.AssetRegistryPath) @@ -181,6 +213,13 @@ func (m *Params) Size() (n int) { if l > 0 { n += 1 + l + sovParams(uint64(l)) } + if m.DaoMachineFundingAmount != 0 { + n += 1 + sovParams(uint64(m.DaoMachineFundingAmount)) + } + l = len(m.DaoMachineFundingDenom) + if l > 0 { + n += 1 + l + sovParams(uint64(l)) + } return n } @@ -315,6 +354,57 @@ func (m *Params) Unmarshal(dAtA []byte) error { } m.AssetRegistryPath = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex + case 4: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field DaoMachineFundingAmount", wireType) + } + m.DaoMachineFundingAmount = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowParams + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.DaoMachineFundingAmount |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 5: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field DaoMachineFundingDenom", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowParams + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + 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 + } + m.DaoMachineFundingDenom = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipParams(dAtA[iNdEx:])