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

feat: bc fusion hardfork implementation #983

Merged
merged 17 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/bnb-chain/node/common/upgrade"
"github.com/bnb-chain/node/common/utils"
"github.com/bnb-chain/node/plugins/account"
"github.com/bnb-chain/node/plugins/airdrop"
"github.com/bnb-chain/node/plugins/bridge"
bTypes "github.com/bnb-chain/node/plugins/bridge/types"
"github.com/bnb-chain/node/plugins/dex"
Expand Down Expand Up @@ -918,6 +919,7 @@ func (app *BNBBeaconChain) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock)
tokens.EndBreatheBlock(ctx, app.swapKeeper)
} else {
app.Logger.Debug("normal block", "height", height)
tokens.EndBlocker(ctx, app.timeLockKeeper, app.swapKeeper)
}

app.DexKeeper.StoreTradePrices(ctx)
Expand Down Expand Up @@ -1156,6 +1158,7 @@ func MakeCodec() *wire.Codec {
bridge.RegisterWire(cdc)
oracle.RegisterWire(cdc)
ibc.RegisterWire(cdc)
airdrop.RegisterWire(cdc)
return cdc
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/bnbcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
sidecmd "github.com/cosmos/cosmos-sdk/x/sidechain/client/cli"
slashingcmd "github.com/cosmos/cosmos-sdk/x/slashing/client/cli"
stakecmd "github.com/cosmos/cosmos-sdk/x/stake/client/cli"

"github.com/spf13/cobra"
"github.com/tendermint/tendermint/libs/cli"

Expand All @@ -22,6 +23,7 @@ import (
"github.com/bnb-chain/node/common"
"github.com/bnb-chain/node/common/types"
accountcmd "github.com/bnb-chain/node/plugins/account/client/cli"
airdropcmd "github.com/bnb-chain/node/plugins/airdrop/client/cli"
apiserv "github.com/bnb-chain/node/plugins/api"
bridgecmd "github.com/bnb-chain/node/plugins/bridge/client/cli"
dexcmd "github.com/bnb-chain/node/plugins/dex/client/cli"
Expand Down Expand Up @@ -98,6 +100,7 @@ func main() {
admin.AddCommands(rootCmd, cdc)
bridgecmd.AddCommands(rootCmd, cdc)
sidecmd.AddCommands(rootCmd, cdc)
airdropcmd.AddCommands(rootCmd, cdc)

// prepare and add flags
executor := cli.PrepareMainCmd(rootCmd, "BC", app.DefaultCLIHome)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ require (

replace (
github.com/Shopify/sarama v1.26.1 => github.com/Shopify/sarama v1.21.0
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/bnc-cosmos-sdk v0.26.6
github.com/cosmos/cosmos-sdk => github.com/j75689/bnc-cosmos-sdk v0.0.0-20231116015058-f4c4b6cf4304 //github.com/bnb-chain/bnc-cosmos-sdk v0.26.6
github.com/grpc-ecosystem/grpc-gateway/v2 => github.com/prysmaticlabs/grpc-gateway/v2 v2.3.1-0.20210702154020-550e1cd83ec1
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/tendermint/go-amino => github.com/bnb-chain/bnc-go-amino v0.14.1-binance.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJm
github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c=
github.com/bnb-chain/bnc-cosmos-sdk v0.26.6 h1:5NrdUXplI2cqWZlLOovSqm+xO5ja03ZPCPT4bQqCD1Y=
github.com/bnb-chain/bnc-cosmos-sdk v0.26.6/go.mod h1:OjdXpDHofs6gcPLM9oGD+mm8DPc6Lsevz0Kia53zt3Q=
github.com/bnb-chain/bnc-go-amino v0.14.1-binance.2 h1:iAlp9gqG0f2LGAauf3ZiijWlT6NI+W2r9y70HH9LI3k=
github.com/bnb-chain/bnc-go-amino v0.14.1-binance.2/go.mod h1:LiCO7jev+3HwLGAiN9gpD0z+jTz95RqgSavbse55XOY=
github.com/bnb-chain/bnc-tendermint v0.32.3-bc.10 h1:E4iSwEbJCLYchHiHE1gnOM3jjmJXLBxARhy/RCl8CpI=
Expand Down Expand Up @@ -789,6 +787,8 @@ github.com/iris-contrib/jade v1.1.4/go.mod h1:EDqR+ur9piDl6DUgs6qRrlfzmlx/D5Uybo
github.com/iris-contrib/pongo2 v0.0.1/go.mod h1:Ssh+00+3GAZqSQb30AvBRNxBx7rf0GqwkjqxNd0u65g=
github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw=
github.com/iris-contrib/schema v0.0.6/go.mod h1:iYszG0IOsuIsfzjymw1kMzTL8YQcCWlm65f3wX8J5iA=
github.com/j75689/bnc-cosmos-sdk v0.0.0-20231116015058-f4c4b6cf4304 h1:pRwSJU1SnmKNmpK8yIdTEoeny2MbKoQPvmvoY/BUaA8=
github.com/j75689/bnc-cosmos-sdk v0.0.0-20231116015058-f4c4b6cf4304/go.mod h1:OjdXpDHofs6gcPLM9oGD+mm8DPc6Lsevz0Kia53zt3Q=
github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus=
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA=
Expand Down
22 changes: 22 additions & 0 deletions plugins/airdrop/client/cli/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/spf13/cobra"
)

func AddCommands(cmd *cobra.Command, cdc *codec.Codec) {
airdropCmd := &cobra.Command{
Use: "airdrop",
Short: "airdrop commands",
}

airdropCmd.AddCommand(
client.PostCommands(
GetApprovalCmd(cdc),
)...,
)

cmd.AddCommand(airdropCmd)
}
107 changes: 107 additions & 0 deletions plugins/airdrop/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package cli

import (
"encoding/hex"
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"

"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/crypto/tmhash"
cmn "github.com/tendermint/tendermint/libs/common"

"github.com/bnb-chain/node/plugins/airdrop"
)

const (
flagAmount = "amount"
flagTokenSymbol = "token-symbol"
flagRecipient = "recipient"
)

func GetApprovalCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "approval",
Short: "get airdrop approval sign data",
RunE: func(cmd *cobra.Command, args []string) error {
txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc)
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(authcmd.GetAccountDecoder(cdc))

amount := viper.GetInt64(flagAmount)
tokenSymbol := viper.GetString(flagTokenSymbol)
recipient := viper.GetString(flagRecipient)
msg := airdrop.NewAirdropApprovalMsg(tokenSymbol, uint64(amount), common.HexToAddress(recipient).Hex())

sdkErr := msg.ValidateBasic()
if sdkErr != nil {
return fmt.Errorf("%v", sdkErr.Data())
}
return SignAndPrint(cliCtx, txBldr, msg)
},
}

cmd.Flags().String(flagTokenSymbol, "", "owner token symbol")
cmd.Flags().Int64(flagAmount, 0, "amount of token")
cmd.Flags().String(flagRecipient, "", "bsc recipient address")

return cmd
}

func SignAndPrint(ctx context.CLIContext, builder authtxb.TxBuilder, msg sdk.Msg) error {
name, err := ctx.GetFromName()
if err != nil {
return err
}

passphrase, err := keys.GetPassphrase(name)
if err != nil {
return err
}

// build and sign the transaction
stdMsg, err := builder.Build([]sdk.Msg{msg})
if err != nil {
return err
}
txBytes, err := builder.Sign(name, passphrase, stdMsg)
if err != nil {
return err
}

var tx auth.StdTx
if err = builder.Codec.UnmarshalBinaryLengthPrefixed(txBytes, &tx); err == nil {
json, err := builder.Codec.MarshalJSON(tx)
if err == nil {
fmt.Printf("TX JSON: %s\n", json)
}
}
hexBytes := make([]byte, len(txBytes)*2)
hex.Encode(hexBytes, txBytes)
txHash := cmn.HexBytes(tmhash.Sum(txBytes)).String()
fmt.Printf("Transaction hash: %s, Transaction hex: %s\n", txHash, hexBytes)

fmt.Println("Sign Message: ", string(stdMsg.Bytes()))
fmt.Println("Sign Message Hash: ", hex.EncodeToString(crypto.Sha256(stdMsg.Bytes())))
sig := tx.GetSignatures()[0]
fmt.Printf("Signature: %s\n", hex.EncodeToString(sig.Signature))
var originPubKey secp256k1.PubKeySecp256k1
err = builder.Codec.UnmarshalBinaryBare(sig.PubKey.Bytes(), &originPubKey)
if err != nil {
return err
}
fmt.Printf("PubKey: %s\n", hex.EncodeToString(originPubKey))
return nil
}
96 changes: 96 additions & 0 deletions plugins/airdrop/msg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package airdrop

import (
"encoding/hex"
"encoding/json"
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
)

const (
Route = "airdrop"
MsgType = "airdrop_approval"
)

var _ sdk.Msg = AirdropApproval{}

func NewAirdropApprovalMsg(tokenSymbol string, amount uint64, recipient string) AirdropApproval {
return AirdropApproval{
TokenSymbol: tokenSymbol,
Amount: amount,
Recipient: recipient,
}
}

func newAirDropApprovalSignData(tokenSymbol string, amount uint64, recipient string) airDropApprovalSignData {
var tokenSymbolBytes [32]byte
copy(tokenSymbolBytes[:], []byte(tokenSymbol))

return airDropApprovalSignData{
TokenSymbol: hex.EncodeToString(tokenSymbolBytes[:]),
Amount: hex.EncodeToString(big.NewInt(int64(amount)).FillBytes(make([]byte, 32))),
Recipient: recipient,
}
}

type airDropApprovalSignData struct {
TokenSymbol string `json:"token_symbol"` // hex string(32 bytes)
Amount string `json:"amount"` // hex string(32 bytes)
Recipient string `json:"recipient"` // eth address(20 bytes)
}

type AirdropApproval struct {
TokenSymbol string `json:"token_symbol"`
Amount uint64 `json:"amount"`
Recipient string `json:"recipient"` // eth address
}

// GetInvolvedAddresses implements types.Msg.
func (msg AirdropApproval) GetInvolvedAddresses() []sdk.AccAddress {
return msg.GetSigners()
}

// GetSignBytes implements types.Msg.
func (msg AirdropApproval) GetSignBytes() []byte {
b, err := json.Marshal(newAirDropApprovalSignData(msg.TokenSymbol, msg.Amount, msg.Recipient))
if err != nil {
panic(err)
}
return b
}

// GetSigners implements types.Msg.
func (m AirdropApproval) GetSigners() []sdk.AccAddress {
// This is not a real on-chain transaction
// We can get signer from the public key.
return []sdk.AccAddress{}
}

// Route implements types.Msg.
func (AirdropApproval) Route() string {
return Route
}

// Type implements types.Msg.
func (AirdropApproval) Type() string {
return MsgType
}

// ValidateBasic implements types.Msg.
func (msg AirdropApproval) ValidateBasic() sdk.Error {
if msg.TokenSymbol == "" {
return sdk.ErrUnknownRequest("Invalid token symbol")
}

if msg.Amount == 0 {
return sdk.ErrUnknownRequest("Invalid amount, should be greater than 0")
}

if !common.IsHexAddress(msg.Recipient) {
return sdk.ErrInvalidAddress("Invalid recipient address")
}

return nil
}
10 changes: 10 additions & 0 deletions plugins/airdrop/wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package airdrop

import (
"github.com/bnb-chain/node/wire"
)

// Register concrete types on wire codec
func RegisterWire(cdc *wire.Codec) {
cdc.RegisterConcrete(AirdropApproval{}, "airdrop/AirdropApproval", nil)
}
1 change: 1 addition & 0 deletions plugins/dex/order/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func validateQtyAndLockBalance(ctx sdk.Context, keeper *DexKeeper, acc common.Na
func handleNewOrder(
ctx sdk.Context, dexKeeper *DexKeeper, msg NewOrderMsg,
) sdk.Result {

if _, ok := dexKeeper.OrderExists(msg.Symbol, msg.Id); ok {
errString := fmt.Sprintf("Duplicated order [%v] on symbol [%v]", msg.Id, msg.Symbol)
return sdk.NewError(types.DefaultCodespace, types.CodeDuplicatedOrder, errString).Result()
Expand Down
6 changes: 6 additions & 0 deletions plugins/tokens/freeze/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func NewHandler(tokenMapper store.Mapper, accKeeper auth.AccountKeeper, keeper b
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case FreezeMsg:
if sdk.IsUpgrade(sdk.BCFusionFirstHardFork) {
return sdk.ErrMsgNotSupported("").Result()
}
return handleFreezeToken(ctx, tokenMapper, accKeeper, keeper, msg)
case UnfreezeMsg:
return handleUnfreezeToken(ctx, tokenMapper, accKeeper, keeper, msg)
Expand All @@ -30,6 +33,9 @@ func NewHandler(tokenMapper store.Mapper, accKeeper auth.AccountKeeper, keeper b
}

func handleFreezeToken(ctx sdk.Context, tokenMapper store.Mapper, accKeeper auth.AccountKeeper, keeper bank.Keeper, msg FreezeMsg) sdk.Result {
if sdk.IsUpgrade(sdk.BCFusionFirstHardFork) {
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
return sdk.ErrMsgNotSupported("").Result()
}
freezeAmount := msg.Amount
symbol := strings.ToUpper(msg.Symbol)
logger := log.With("module", "token", "symbol", symbol, "amount", freezeAmount, "addr", msg.From)
Expand Down
5 changes: 4 additions & 1 deletion plugins/tokens/issue/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
// NewHandler creates a new token issue message handler
func NewHandler(tokenMapper store.Mapper, keeper bank.Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
if sdk.IsUpgrade(sdk.BCFusionFirstHardFork) {
return sdk.ErrMsgNotSupported("").Result()
}
switch msg := msg.(type) {
case IssueMsg:
return handleIssueToken(ctx, tokenMapper, keeper, msg)
Expand Down Expand Up @@ -62,7 +65,7 @@ func handleIssueToken(ctx sdk.Context, tokenMapper store.Mapper, bankKeeper bank
return issue(ctx, logger, tokenMapper, bankKeeper, token)
}

//Mint MiniToken is also handled by this function
// Mint MiniToken is also handled by this function
func handleMintToken(ctx sdk.Context, tokenMapper store.Mapper, bankKeeper bank.Keeper, msg MintMsg) sdk.Result {
symbol := strings.ToUpper(msg.Symbol)
logger := log.With("module", "token", "symbol", symbol, "amount", msg.Amount, "minter", msg.From)
Expand Down
Loading