Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release: prepare release for v0.2.5-alpha.1 #464

Merged
merged 11 commits into from
Sep 6, 2023
Merged
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog

## v0.2.5-alpha.1
This release contains 4 features and 4 bugfixes.

Features:
* [#435](https://github.com/bnb-chain/greenfield/pull/435) feat: deposit the balance of the bank account to the payment account
* [#448](https://github.com/bnb-chain/greenfield/pull/448) feat: timelock for large amount withdraw from payment
* [#449](https://github.com/bnb-chain/greenfield/pull/449) feat: complete cdc register
* [#457](https://github.com/bnb-chain/greenfield/pull/457) feat: add api for querying last quota update time

Bugfixes:
* [#451](https://github.com/bnb-chain/greenfield/pull/451) fix: audit issues by verichain
* [#456](https://github.com/bnb-chain/greenfield/pull/456) fix: fix parameter init issue
* [#458](https://github.com/bnb-chain/greenfield/pull/458) fix: correct emit event filed
* [#462](https://github.com/bnb-chain/greenfield/pull/462) fix: fix app hash mismatch for genesis block

## v0.2.4
This release contains all the changes in the v0.2.4 alpha versions and 5 new bugfixes.

Expand Down
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ func New(
encodingConfig.TxConfig),
auth.NewAppModule(appCodec, app.AccountKeeper, nil, nil),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper, nil),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper, app.PaymentKeeper, nil),
feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
gov.NewAppModule(appCodec, &app.GovKeeper, app.AccountKeeper, app.BankKeeper, nil),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, nil),
Expand Down
30 changes: 29 additions & 1 deletion app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package app

import (
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"

paymentmodule "github.com/bnb-chain/greenfield/x/payment"
paymenttypes "github.com/bnb-chain/greenfield/x/payment/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

func (app *App) RegisterUpgradeHandlers(chainID string, serverCfg *serverconfig.Config) error {
Expand All @@ -12,7 +18,7 @@ func (app *App) RegisterUpgradeHandlers(chainID string, serverCfg *serverconfig.
}

// Register the upgrade handlers here
// app.registerPublicDelegationUpgradeHandler()
app.registerNagquUpgradeHandler()
// app.register...()
// ...
return nil
Expand All @@ -36,3 +42,25 @@ func (app *App) RegisterUpgradeHandlers(chainID string, serverCfg *serverconfig.
// },
// )
// }

func (app *App) registerNagquUpgradeHandler() {
// Register the upgrade handler
app.UpgradeKeeper.SetUpgradeHandler(upgradetypes.Nagqu,
func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
app.Logger().Info("upgrade to ", plan.Name)
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
})

// Register the upgrade initializer
app.UpgradeKeeper.SetUpgradeInitializer(upgradetypes.Nagqu,
func() error {
app.Logger().Info("Init Nagqu upgrade")
mm, ok := app.mm.Modules[paymenttypes.ModuleName].(*paymentmodule.AppModule)
if !ok {
panic("*paymentmodule.AppModule not found")
}
mm.SetConsensusVersion(2)
return nil

})
}
1 change: 1 addition & 0 deletions deployment/localup/localup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ function generate_genesis() {
sed -i -e "s/\"update_price_disallowed_days\": 2/\"update_price_disallowed_days\": 0/g" ${workspace}/.local/validator${i}/config/genesis.json
#sed -i -e "s/\"community_tax\": \"0.020000000000000000\"/\"community_tax\": \"0\"/g" ${workspace}/.local/validator${i}/config/genesis.json
sed -i -e "s/log_level = \"info\"/\log_level= \"debug\"/g" ${workspace}/.local/validator${i}/config/config.toml
echo -e '[[upgrade]]\nname = "Nagqu"\nheight = 20\ninfo = ""' >> ${workspace}/.local/validator${i}/config/app.toml
done

# enable swagger API for validator0
Expand Down
170 changes: 170 additions & 0 deletions e2e/tests/payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package tests
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"math"
"math/big"
"reflect"
"strconv"
"testing"
"time"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/types/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -375,6 +378,67 @@ func (s *PaymentTestSuite) TestDeposit_ActiveAccount() {
s.Require().Equal(paymentAccountBNBNeeded.MulRaw(3), settledBalance.Add(paymentAccountStreamRecordAfter.StaticBalance.Add(paymentAccountStreamRecordAfter.BufferBalance)))
}

func (s *PaymentTestSuite) TestDeposit_FromBankAccount() {
ctx := context.Background()
user := s.GenAndChargeAccounts(1, 1000000)[0]
userAddr := user.GetAddr().String()
var err error

// derive payment account
paymentAccount := derivePaymentAccount(user.GetAddr(), 0)
// transfer BNB to derived payment account
msgSend := banktypes.NewMsgSend(user.GetAddr(), paymentAccount, sdk.NewCoins(
sdk.NewCoin(s.Config.Denom, sdk.NewInt(1e18)),
))
_ = s.SendTxBlock(user, msgSend)

paymentBalanceBefore, err := s.Client.Balance(ctx, &banktypes.QueryBalanceRequest{
Address: paymentAccount.String(),
Denom: s.Config.Denom,
})
s.Require().NoError(err)
s.Require().Equal(sdk.NewInt(1e18).String(), paymentBalanceBefore.GetBalance().Amount.String())

// create payment account and deposit
msgCreatePaymentAccount := &paymenttypes.MsgCreatePaymentAccount{
Creator: userAddr,
}
_ = s.SendTxBlock(user, msgCreatePaymentAccount)
paymentAccountsReq := &paymenttypes.QueryPaymentAccountsByOwnerRequest{Owner: userAddr}
paymentAccounts, err := s.Client.PaymentQueryClient.PaymentAccountsByOwner(ctx, paymentAccountsReq)
s.Require().NoError(err)
s.T().Logf("paymentAccounts %s", core.YamlString(paymentAccounts))
paymentAddr := paymentAccounts.PaymentAccounts[0]
s.Require().Lenf(paymentAccounts.PaymentAccounts, 1, "paymentAccounts %s", core.YamlString(paymentAccounts))

// transfer BNB to payment account: should not success
msgSend = banktypes.NewMsgSend(user.GetAddr(), sdk.MustAccAddressFromHex(paymentAddr), sdk.NewCoins(
sdk.NewCoin(s.Config.Denom, sdk.NewInt(1e18)),
))
s.SendTxBlockWithExpectErrorString(msgSend, user, "is not allowed to receive funds")

// deposit BNB needed
msgDeposit := &paymenttypes.MsgDeposit{
Creator: user.GetAddr().String(),
To: paymentAddr,
Amount: sdk.NewInt(1e18), // deposit more than needed
}
_ = s.SendTxBlock(user, msgDeposit)

paymentBalanceAfter, err := s.Client.Balance(ctx, &banktypes.QueryBalanceRequest{
Address: paymentAddr,
Denom: s.Config.Denom,
})
s.Require().NoError(err)
s.Require().Equal(sdk.NewInt(0).String(), paymentBalanceAfter.GetBalance().Amount.String())
}

func derivePaymentAccount(owner sdk.AccAddress, index uint64) sdk.AccAddress {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, index)
return address.Derive(owner.Bytes(), b)[:sdk.EthAddressLength]
}

func (s *PaymentTestSuite) TestDeposit_ResumeInOneBlock() {
ctx := context.Background()
sp := s.PickStorageProvider()
Expand Down Expand Up @@ -947,6 +1011,112 @@ func (s *PaymentTestSuite) TestWithdraw() {
s.Require().Equal(paymentAccountStreamRecord.StaticBalance.Sub(paymentAccountStreamRecordAfter.StaticBalance).Int64(), amount.Add(staticBalanceChange).Int64())
}

func (s *PaymentTestSuite) TestWithdrawDelayed() {
defer s.revertParams()

ctx := context.Background()
user := s.GenAndChargeAccounts(1, 1000000)[0]
userAddr := user.GetAddr().String()
var err error

// create payment account and deposit
msgCreatePaymentAccount := &paymenttypes.MsgCreatePaymentAccount{
Creator: userAddr,
}
_ = s.SendTxBlock(user, msgCreatePaymentAccount)
paymentAccountsReq := &paymenttypes.QueryPaymentAccountsByOwnerRequest{Owner: userAddr}
paymentAccounts, err := s.Client.PaymentQueryClient.PaymentAccountsByOwner(ctx, paymentAccountsReq)
s.Require().NoError(err)
s.T().Logf("paymentAccounts %s", core.YamlString(paymentAccounts))
paymentAddr := paymentAccounts.PaymentAccounts[0]
s.Require().Lenf(paymentAccounts.PaymentAccounts, 1, "paymentAccounts %s", core.YamlString(paymentAccounts))

// deposit BNB
msgDeposit := &paymenttypes.MsgDeposit{
Creator: user.GetAddr().String(),
To: paymentAddr,
Amount: sdkmath.NewIntFromBigInt(big.NewInt(1e18)).MulRaw(300),
}
_ = s.SendTxBlock(user, msgDeposit)

paymentAccountStreamRecord := s.getStreamRecord(paymentAddr)
// withdraw less than limit
amount := sdk.NewInt(1000)
withdrawMsg := paymenttypes.NewMsgWithdraw(userAddr, paymentAddr, amount)
s.SendTxBlock(user, withdrawMsg)
paymentAccountStreamRecordAfter := s.getStreamRecord(paymentAddr)
s.T().Logf("paymentAccountStreamRecordAfter %s", core.YamlString(paymentAccountStreamRecordAfter))

staticBalanceChange := paymentAccountStreamRecord.NetflowRate.MulRaw(paymentAccountStreamRecordAfter.CrudTimestamp - paymentAccountStreamRecord.CrudTimestamp).Neg()
s.Require().Equal(paymentAccountStreamRecord.StaticBalance.Sub(paymentAccountStreamRecordAfter.StaticBalance).Int64(), amount.Add(staticBalanceChange).Int64())

paymentAccountStreamRecord = s.getStreamRecord(paymentAddr)
balance, err := s.Client.Balance(ctx, &banktypes.QueryBalanceRequest{
Address: userAddr,
Denom: "BNB",
})
s.Require().NoError(err)

// update params
params := s.queryParams()
params.WithdrawTimeLockDuration = 10
s.updateParams(params)

// withdraw more than limit
amount = sdkmath.NewIntFromBigInt(big.NewInt(1e18)).MulRaw(100)
withdrawMsg = paymenttypes.NewMsgWithdraw(userAddr, paymentAddr, amount)
s.SendTxBlock(user, withdrawMsg)
paymentAccountStreamRecordAfter = s.getStreamRecord(paymentAddr)
s.T().Logf("paymentAccountStreamRecordAfter %s", core.YamlString(paymentAccountStreamRecordAfter))

s.Require().True(paymentAccountStreamRecord.StaticBalance.Sub(paymentAccountStreamRecordAfter.StaticBalance).Equal(amount))

// balance does not increase
balanceAfter, err := s.Client.Balance(ctx, &banktypes.QueryBalanceRequest{
Address: userAddr,
Denom: "BNB",
})
s.Require().NoError(err)
s.Require().True(balanceAfter.Balance.Amount.LTE(balance.Balance.Amount))

// withdraw before time lock duration
amount = sdkmath.NewIntFromBigInt(big.NewInt(1e18)).MulRaw(100)
withdrawMsg = paymenttypes.NewMsgWithdraw(userAddr, "", amount)
s.SendTxBlockWithExpectErrorString(withdrawMsg, user, "does not reach to the delayed duration")

// withdraw another large amount
withdrawMsg = paymenttypes.NewMsgWithdraw(userAddr, paymentAddr, amount)
s.SendTxBlockWithExpectErrorString(withdrawMsg, user, "delayed withdrawal already exists")

// wait after time lock, and withdraw again
time.Sleep(11 * time.Second)

paymentAccountStreamRecord = s.getStreamRecord(paymentAddr)
balance, err = s.Client.Balance(ctx, &banktypes.QueryBalanceRequest{
Address: userAddr,
Denom: "BNB",
})
s.Require().NoError(err)
s.T().Logf("balance %s", core.YamlString(balance))

amount = sdkmath.NewIntFromBigInt(big.NewInt(1e18)).MulRaw(100)
withdrawMsg = paymenttypes.NewMsgWithdraw(userAddr, "", amount)
s.SendTxBlock(user, withdrawMsg)

paymentAccountStreamRecordAfter = s.getStreamRecord(paymentAddr)
s.T().Logf("paymentAccountStreamRecordAfter %s", core.YamlString(paymentAccountStreamRecordAfter))
s.Require().Equal(paymentAccountStreamRecord.StaticBalance, paymentAccountStreamRecordAfter.StaticBalance)

balanceAfter, err = s.Client.Balance(ctx, &banktypes.QueryBalanceRequest{
Address: userAddr,
Denom: "BNB",
})
s.Require().NoError(err)
s.T().Logf("balanceAfter %s", core.YamlString(balanceAfter))

s.Require().True(balanceAfter.Balance.Amount.GT(balance.Balance.Amount))
}

func (s *PaymentTestSuite) TestVirtualGroup_Settle() {
var err error
ctx := context.Background()
Expand Down
9 changes: 6 additions & 3 deletions e2e/tests/storage_bill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,6 @@ func (s *PaymentTestSuite) TestStorageBill_UpdatePaymentAddress() {
// assertions
streamAddresses[0] = paymentAccountAddr
streamRecordsAfter = s.getStreamRecords(streamAddresses)
s.Require().Equal(streamRecordsAfter.User.StaticBalance, sdkmath.ZeroInt())
s.Require().Equal(streamRecordsAfter.User.NetflowRate.Sub(streamRecordsBefore.User.NetflowRate), userTotalRate.Neg())
s.Require().Equal(streamRecordsAfter.GVGFamily.NetflowRate.Sub(streamRecordsBefore.GVGFamily.NetflowRate), readChargeRate)
s.Require().Equal(streamRecordsAfter.Tax.NetflowRate.Sub(streamRecordsBefore.Tax.NetflowRate), taxRate)
Expand Down Expand Up @@ -2235,8 +2234,12 @@ func (s *PaymentTestSuite) CreatePaymentAccount(user keys.KeyManager, amount, de
paymentAccountAddr := paymentAccounts.PaymentAccounts[len(paymentAccounts.PaymentAccounts)-1]
// charge payment account
paymentAcc := sdk.MustAccAddressFromHex(paymentAccountAddr)
msgSend := banktypes.NewMsgSend(user.GetAddr(), paymentAcc, []sdk.Coin{{Denom: "BNB", Amount: types.NewIntFromInt64WithDecimal(amount, decimal)}})
s.SendTxBlock(user, msgSend)
msgDeposit := &paymenttypes.MsgDeposit{
Creator: user.GetAddr().String(),
To: paymentAcc.String(),
Amount: types.NewIntFromInt64WithDecimal(amount, decimal), // deposit more than needed
}
s.SendTxBlock(user, msgDeposit)

return paymentAccountAddr
}
Expand Down
6 changes: 6 additions & 0 deletions e2e/tests/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func (s *StorageTestSuite) TestCreateBucket() {
s.Require().Equal(queryHeadBucketResponse.BucketInfo.Visibility, storagetypes.VISIBILITY_TYPE_PUBLIC_READ)
s.Require().Equal(queryHeadBucketResponse.BucketInfo.SourceType, storagetypes.SOURCE_TYPE_ORIGIN)

queryQuotaUpdateTimeResponse, err := s.Client.QueryQuotaUpdateTime(ctx, &storagetypes.QueryQuoteUpdateTimeRequest{
BucketName: bucketName,
})
s.Require().NoError(err)
s.Require().Equal(queryHeadBucketResponse.BucketInfo.CreateAt, queryQuotaUpdateTimeResponse.UpdateAt)

// UpdateBucketInfo
msgUpdateBucketInfo := storagetypes.NewMsgUpdateBucketInfo(
user.GetAddr(), bucketName, nil, user.GetAddr(), storagetypes.VISIBILITY_TYPE_PRIVATE)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ replace (
github.com/cometbft/cometbft => github.com/bnb-chain/greenfield-cometbft v0.0.3
github.com/cometbft/cometbft-db => github.com/bnb-chain/greenfield-cometbft-db v0.8.1-alpha.1
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/greenfield-cosmos-sdk v0.2.4
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/greenfield-cosmos-sdk v0.2.5-alpha.1
github.com/cosmos/iavl => github.com/bnb-chain/greenfield-iavl v0.20.1
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ github.com/bnb-chain/greenfield-cometbft v0.0.3 h1:tv8NMy3bzX/1urqXGQIIAZSLy83lo
github.com/bnb-chain/greenfield-cometbft v0.0.3/go.mod h1:f35mk/r5ab6yvzlqEWZt68LfUje68sYgMpVlt2CUYMk=
github.com/bnb-chain/greenfield-cometbft-db v0.8.1-alpha.1 h1:XcWulGacHVRiSCx90Q8Y//ajOrLNBQWR/KDB89dy3cU=
github.com/bnb-chain/greenfield-cometbft-db v0.8.1-alpha.1/go.mod h1:ey1CiK4bYo1RBNJLRiVbYr5CMdSxci9S/AZRINLtppI=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.4 h1:09ST+MTEAyjyBSc4ZjZzHxpNLMnIIkZ518jJVRtrKFc=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.4/go.mod h1:y3hDhQhil5hMIhwBTpu07RZBF30ZITkoE+GHhVZChtY=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.5-alpha.1 h1:E6AOXDWIcFC6e4KAqI7XzMWn8nZQcGjl1ESBRwrhVh8=
github.com/bnb-chain/greenfield-cosmos-sdk v0.2.5-alpha.1/go.mod h1:y3hDhQhil5hMIhwBTpu07RZBF30ZITkoE+GHhVZChtY=
github.com/bnb-chain/greenfield-cosmos-sdk/api v0.0.0-20230816082903-b48770f5e210 h1:GHPbV2bC+gmuO6/sG0Tm8oGal3KKSRlyE+zPscDjlA8=
github.com/bnb-chain/greenfield-cosmos-sdk/api v0.0.0-20230816082903-b48770f5e210/go.mod h1:vhsZxXE9tYJeYB5JR4hPhd6Pc/uPf7j1T8IJ7p9FdeM=
github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20230816082903-b48770f5e210 h1:FLVOn4+OVbsKi2+YJX5kmD27/4dRu4FW7xCXFhzDO5s=
Expand Down
22 changes: 22 additions & 0 deletions proto/greenfield/payment/delayed_withdrawal_record.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
syntax = "proto3";
package greenfield.payment;

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

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

message DelayedWithdrawalRecord {
// the withdrawal address
string addr = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// the withdrawal amount
string amount = 2 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
// the withdrawal from payment account address
string from = 3 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// unlock timestamp is the unix timestamp to unlock the withdrawal
int64 unlock_timestamp = 4;
}
7 changes: 7 additions & 0 deletions proto/greenfield/payment/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ message Params {
uint64 max_auto_resume_flow_count = 5 [(gogoproto.moretags) = "yaml:\"max_auto_resume_flow_count\""];
// The denom of fee charged in payment module
string fee_denom = 6 [(gogoproto.moretags) = "yaml:\"fee_denom\""];
// The withdrawal amount threshold to trigger time lock
string withdraw_time_lock_threshold = 7 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int"
];
// The duration of the time lock for a big amount withdrawal
uint64 withdraw_time_lock_duration = 8 [(gogoproto.moretags) = "yaml:\"withdraw_time_lock_duration\""];
}

// VersionedParams defines the parameters with multiple versions, each version is stored with different timestamp.
Expand Down
Loading
Loading