Skip to content

Commit

Permalink
R4R: Implement BEP84 (#809)
Browse files Browse the repository at this point in the history
* implement bep100

update cosmos dependency

* add upgrade height for FixFailAckPackage

register channel

* convert to bep20 supply in sync

update token supply

* distribute fee

* the mirror and sync fee in package is already converted into BC amount

* update dependency

* enable account scripts for cross chain transfer

* register upgrade height

* send only no script execution error

* include refund package into result

* add transfer in tag

* use idx other than address as refund exclude flag

* use full refund instead of partial refund

* publish mirror related events (#813)

* update mirror config

* move deduct fee from deliverTx

* fix comments

* add missing tags

* fix transfer out fail ack handler issue

* update schmema

* update dependency

* add old total supply for mirror sync event

* fix mirror event

Co-authored-by: HaoyangLiu <[email protected]>
  • Loading branch information
2 people authored and forcodedancing committed May 19, 2022
1 parent 9b2dfd4 commit e017bfe
Show file tree
Hide file tree
Showing 23 changed files with 908 additions and 59 deletions.
4 changes: 3 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,15 @@ func SetUpgradeConfig(upgradeConfig *config.UpgradeConfig) {
upgrade.Mgr.AddUpgradeHeight(upgrade.ListingRuleUpgrade, upgradeConfig.ListingRuleUpgradeHeight)
upgrade.Mgr.AddUpgradeHeight(upgrade.FixZeroBalance, upgradeConfig.FixZeroBalanceHeight)
upgrade.Mgr.AddUpgradeHeight(upgrade.LaunchBscUpgrade, upgradeConfig.LaunchBscUpgradeHeight)
upgrade.Mgr.AddUpgradeHeight(upgrade.EnableAccountScriptsForCrossChainTransfer, upgradeConfig.EnableAccountScriptsForCrossChainTransferHeight)

upgrade.Mgr.AddUpgradeHeight(upgrade.BEP8, upgradeConfig.BEP8Height)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP67, upgradeConfig.BEP67Height)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP70, upgradeConfig.BEP70Height)

upgrade.Mgr.AddUpgradeHeight(upgrade.AdjustTokenSymbolLength, upgradeConfig.AdjustTokenSymbolLengthHeight)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP82, upgradeConfig.BEP82Height)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP84, upgradeConfig.BEP84Height)
upgrade.Mgr.AddUpgradeHeight(upgrade.FixFailAckPackage, upgradeConfig.FixFailAckPackageHeight)

// register store keys of upgrade
upgrade.Mgr.RegisterStoreKeys(upgrade.BEP9, common.TimeLockStoreKey.Name())
Expand Down
30 changes: 28 additions & 2 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ BEP70Height = {{ .UpgradeConfig.BEP70Height }}
AdjustTokenSymbolLengthHeight = {{ .UpgradeConfig.AdjustTokenSymbolLengthHeight }}
# Block height of BEP82 upgrade
BEP82Height = {{ .UpgradeConfig.BEP82Height }}
# Block height of BEP84 upgrade
BEP84Height = {{ .UpgradeConfig.BEP84Height }}
# Block height of FixFailAckPackage upgrade
FixFailAckPackageHeight = {{ .UpgradeConfig.FixFailAckPackageHeight }}
# Block height of EnableAccountScriptsForCrossChainTransferHeight upgrade
EnableAccountScriptsForCrossChainTransferHeight = {{ .UpgradeConfig.EnableAccountScriptsForCrossChainTransferHeight }}
[query]
# ABCI query interface black list, suggested value: ["custom/gov/proposals", "custom/timelock/timelocks", "custom/atomicSwap/swapcreator", "custom/atomicSwap/swaprecipient"]
Expand Down Expand Up @@ -151,6 +157,11 @@ publishCrossTransfer = {{ .PublicationConfig.PublishCrossTransfer }}
crossTransferTopic = "{{ .PublicationConfig.CrossTransferTopic }}"
crossTransferKafka = "{{ .PublicationConfig.CrossTransferKafka }}"
# Whether we want publish mirror events
publishMirror = {{ .PublicationConfig.PublishMirror }}
mirrorTopic = "{{ .PublicationConfig.MirrorTopic }}"
mirrorKafka = "{{ .PublicationConfig.MirrorKafka }}"
# Whether we want publish side proposals
publishSideProposal = {{ .PublicationConfig.PublishSideProposal }}
sideProposalTopic = "{{ .PublicationConfig.SideProposalTopic }}"
Expand Down Expand Up @@ -305,6 +316,10 @@ type PublicationConfig struct {
CrossTransferTopic string `mapstructure:"crossTransferTopic"`
CrossTransferKafka string `mapstructure:"crossTransferKafka"`

PublishMirror bool `mapstructure:"publishMirror"`
MirrorTopic string `mapstructure:"mirrorTopic"`
MirrorKafka string `mapstructure:"mirrorKafka"`

PublishSideProposal bool `mapstructure:"publishSideProposal"`
SideProposalTopic string `mapstructure:"sideProposalTopic"`
SideProposalKafka string `mapstructure:"sideProposalKafka"`
Expand Down Expand Up @@ -380,6 +395,10 @@ func defaultPublicationConfig() *PublicationConfig {
CrossTransferTopic: "crossTransfer",
CrossTransferKafka: "127.0.0.1:9092",

PublishMirror: false,
MirrorTopic: "mirror",
MirrorKafka: "127.0.0.1:9092",

PublishSideProposal: false,
SideProposalTopic: "sideProposal",
SideProposalKafka: "127.0.0.1:9092",
Expand Down Expand Up @@ -416,6 +435,7 @@ func (pubCfg PublicationConfig) ShouldPublishAny() bool {
pubCfg.PublishStaking ||
pubCfg.PublishSlashing ||
pubCfg.PublishCrossTransfer ||
pubCfg.PublishMirror ||
pubCfg.PublishSideProposal ||
pubCfg.PublishBreatheBlock
}
Expand Down Expand Up @@ -495,8 +515,11 @@ type UpgradeConfig struct {
BEP67Height int64 `mapstructure:"BEP67Height"`
BEP70Height int64 `mapstructure:"BEP70Height"`

AdjustTokenSymbolLengthHeight int64 `mapstructure:"AdjustTokenSymbolLengthHeight"`
BEP82Height int64 `mapstructure:"BEP82Height"`
AdjustTokenSymbolLengthHeight int64 `mapstructure:"AdjustTokenSymbolLengthHeight"`
BEP82Height int64 `mapstructure:"BEP82Height"`
BEP84Height int64 `mapstructure:"BEP84Height"`
FixFailAckPackageHeight int64 `mapstructure:"FixFailAckPackageHeight"`
EnableAccountScriptsForCrossChainTransferHeight int64 `mapstructure:"EnableAccountScriptsForCrossChainTransferHeight"`
}

func defaultUpgradeConfig() *UpgradeConfig {
Expand All @@ -518,6 +541,9 @@ func defaultUpgradeConfig() *UpgradeConfig {
LaunchBscUpgradeHeight: 1,
AdjustTokenSymbolLengthHeight: math.MaxInt64,
BEP82Height: math.MaxInt64,
BEP84Height: math.MaxInt64,
FixFailAckPackageHeight: math.MaxInt64,
EnableAccountScriptsForCrossChainTransferHeight: math.MaxInt64,
}
}

Expand Down
7 changes: 6 additions & 1 deletion app/pub/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"fmt"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/binance-chain/node/common/types"
orderPkg "github.com/binance-chain/node/plugins/dex/order"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type msgType int8
Expand All @@ -23,6 +24,7 @@ const (
distributionTpe
slashingTpe
crossTransferTpe
mirrorTpe
sideProposalType
breatheBlockTpe
)
Expand Down Expand Up @@ -56,6 +58,8 @@ func (this msgType) String() string {
return "Slashing"
case crossTransferTpe:
return "CrossTransfer"
case mirrorTpe:
return "Mirror"
case sideProposalType:
return "SideProposal"
case breatheBlockTpe:
Expand All @@ -80,6 +84,7 @@ var latestSchemaVersions = map[msgType]int{
distributionTpe: 0,
slashingTpe: 0,
crossTransferTpe: 0,
mirrorTpe: 0,
sideProposalType: 0,
breatheBlockTpe: 0,
}
Expand Down
66 changes: 66 additions & 0 deletions app/pub/msgs_mirror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package pub

import "fmt"

type Mirror struct {
TxHash string
ChainId string
Type string
RelayerFee int64
Sender string
Contract string
BEP20Name string
BEP20Symbol string
BEP2Symbol string
OldTotalSupply int64
TotalSupply int64
Decimals int
Fee int64
}

func (msg Mirror) String() string {
return fmt.Sprintf("Mirror: txHash: %s, sender: %s, bep2Symbol: %s", msg.TxHash, msg.Sender, msg.BEP2Symbol)
}

func (msg Mirror) ToNativeMap() map[string]interface{} {
var native = make(map[string]interface{})
native["txHash"] = msg.TxHash
native["chainId"] = msg.ChainId
native["type"] = msg.Type
native["relayerFee"] = msg.RelayerFee
native["sender"] = msg.Sender
native["contract"] = msg.Contract
native["bep20Name"] = msg.BEP20Name
native["bep20Symbol"] = msg.BEP20Symbol
native["bep2Symbol"] = msg.BEP2Symbol
native["oldTotalSupply"] = msg.OldTotalSupply
native["totalSupply"] = msg.TotalSupply
native["decimals"] = msg.Decimals
native["fee"] = msg.Fee
return native
}

// deliberated not implemented Ess
type Mirrors struct {
Height int64
Num int
Timestamp int64
Mirrors []Mirror
}

func (msg Mirrors) String() string {
return fmt.Sprintf("Mirrors in block %d, num: %d", msg.Height, msg.Num)
}

func (msg Mirrors) ToNativeMap() map[string]interface{} {
var native = make(map[string]interface{})
native["height"] = msg.Height
mirrors := make([]map[string]interface{}, len(msg.Mirrors), len(msg.Mirrors))
for idx, t := range msg.Mirrors {
mirrors[idx] = t.ToNativeMap()
}
native["timestamp"] = msg.Timestamp
native["num"] = msg.Num
native["mirrors"] = mirrors
return native
}
35 changes: 35 additions & 0 deletions app/pub/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,41 @@ func PublishEvent(
publisher.publish(&crossTransferMsg, crossTransferTpe, toPublish.Height, toPublish.Timestamp.UnixNano())
}

if cfg.PublishMirror {
var msgNum int
mirrors := make([]Mirror, 0)

for _, mirror := range eventData.MirrorData {
msgNum++
mr := Mirror{
TxHash: mirror.TxHash,
ChainId: mirror.ChainId,
Type: mirror.Type,
RelayerFee: mirror.RelayerFee,
Sender: mirror.Sender,
Contract: mirror.Contract,
BEP20Name: mirror.BEP20Name,
BEP20Symbol: mirror.BEP20Symbol,
BEP2Symbol: mirror.BEP2Symbol,
OldTotalSupply: mirror.OldTotalSupply,
TotalSupply: mirror.TotalSupply,
Decimals: mirror.Decimals,
Fee: mirror.Fee,
}

mirrors = append(mirrors, mr)
}

mirrorsMsg := Mirrors{
Num: msgNum,
Height: toPublish.Height,
Timestamp: toPublish.Timestamp.Unix(),
Mirrors: mirrors,
}
publisher.publish(&mirrorsMsg, mirrorTpe, toPublish.Height, toPublish.Timestamp.UnixNano())

}

if cfg.PublishBreatheBlock && toPublish.IsBreatheBlock {
breatheBlockMsg := BreatheBlockMsg{
Height: toPublish.Height,
Expand Down
17 changes: 17 additions & 0 deletions app/pub/publisher_kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type KafkaMarketDataPublisher struct {
distributionCodec *goavro.Codec
slashingCodec *goavro.Codec
crossTransferCodec *goavro.Codec
mirrorCodec *goavro.Codec
sideProposalCodec *goavro.Codec
breatheBlockCodec *goavro.Codec

Expand Down Expand Up @@ -184,6 +185,16 @@ func (publisher *KafkaMarketDataPublisher) newProducers() (config *sarama.Config
return
}
}
if Cfg.PublishMirror {
if _, ok := publisher.producers[Cfg.MirrorTopic]; !ok {
publisher.producers[Cfg.MirrorTopic], err =
publisher.connectWithRetry(strings.Split(Cfg.MirrorKafka, KafkaBrokerSep), config)
}
if err != nil {
Logger.Error("failed to create mirror producer", "err", err)
return
}
}
if Cfg.PublishSideProposal {
if _, ok := publisher.producers[Cfg.SideProposalTopic]; !ok {
publisher.producers[Cfg.SideProposalTopic], err =
Expand Down Expand Up @@ -295,6 +306,8 @@ func (publisher KafkaMarketDataPublisher) resolveTopic(tpe msgType) (topic strin
topic = Cfg.SlashingTopic
case crossTransferTpe:
topic = Cfg.CrossTransferTopic
case mirrorTpe:
topic = Cfg.MirrorTopic
case sideProposalType:
topic = Cfg.SideProposalTopic
case breatheBlockTpe:
Expand Down Expand Up @@ -385,6 +398,8 @@ func (publisher *KafkaMarketDataPublisher) marshal(msg AvroOrJsonMsg, tpe msgTyp
codec = publisher.slashingCodec
case crossTransferTpe:
codec = publisher.crossTransferCodec
case mirrorTpe:
codec = publisher.mirrorCodec
case sideProposalType:
codec = publisher.sideProposalCodec
case breatheBlockTpe:
Expand Down Expand Up @@ -420,6 +435,8 @@ func (publisher *KafkaMarketDataPublisher) initAvroCodecs() (err error) {
return err
} else if publisher.crossTransferCodec, err = goavro.NewCodec(crossTransferSchema); err != nil {
return err
} else if publisher.mirrorCodec, err = goavro.NewCodec(mirrorSchema); err != nil {
return err
} else if publisher.sideProposalCodec, err = goavro.NewCodec(sideProposalsSchema); err != nil {
return err
} else if publisher.breatheBlockCodec, err = goavro.NewCodec(breatheBlockSchema); err != nil {
Expand Down
17 changes: 17 additions & 0 deletions app/pub/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,23 @@ func TestCrossTransferMarsha(t *testing.T) {
}
}

func TestMirrorMarsha(t *testing.T) {
publisher := NewKafkaMarketDataPublisher(Logger, "", false)
msg := Mirrors{
Height: 10,
Num: 2,
Timestamp: time.Now().Unix(),
Mirrors: []Mirror{
{TxHash: "xxxx", ChainId: "rialto", Type: "xx", Sender: "xxxx", RelayerFee: 1, Contract: "xxxx", BEP20Name: "ad", BEP20Symbol: "xxx", BEP2Symbol: "xx", TotalSupply: 1000, Decimals: 9, Fee: 100},
{TxHash: "xxxx", ChainId: "rialto", Type: "xx", Sender: "xxxx", RelayerFee: 0, Contract: "xxxx", BEP20Name: "ad", BEP20Symbol: "xxx", BEP2Symbol: "xx", TotalSupply: 1000, Decimals: 9, Fee: 100},
},
}
_, err := publisher.marshal(&msg, mirrorTpe)
if err != nil {
t.Fatal(err)
}
}

func TestSideProposalMarsha(t *testing.T) {
publisher := NewKafkaMarketDataPublisher(Logger, "", false)
msg := SideProposals{
Expand Down
38 changes: 38 additions & 0 deletions app/pub/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,44 @@ const (
}
`

mirrorSchema = `
{
"type": "record",
"name": "Mirrors",
"namespace": "com.company",
"fields": [
{ "name": "height", "type": "long"},
{ "name": "num", "type": "int" },
{ "name": "timestamp", "type": "long" },
{ "name": "mirrors",
"type": {
"type": "array",
"items": {
"type": "record",
"name": "Mirror",
"namespace": "com.company",
"fields": [
{ "name": "txHash", "type": "string" },
{ "name": "chainId", "type": "string" },
{ "name": "type", "type": "string" },
{ "name": "relayerFee", "type": "long" },
{ "name": "sender", "type": "string" },
{ "name": "contract", "type": "string" },
{ "name": "bep20Name", "type": "string" },
{ "name": "bep20Symbol", "type": "string" },
{ "name": "bep2Symbol", "type": "string" },
{ "name": "oldTotalSupply", "type": "long" },
{ "name": "totalSupply", "type": "long" },
{ "name": "decimals", "type": "int" },
{ "name": "fee", "type": "long" }
]
}
}
}
]
}
`

sideProposalsSchema = `
{
"type": "record",
Expand Down
2 changes: 1 addition & 1 deletion app/pub/sub/cross.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func SubscribeCrossTransferEvent(sub *pubsub.Subscriber) error {
err := sub.Subscribe(bridge.Topic, func(event pubsub.Event) {
err := sub.Subscribe(bridge.CrossTransferTopic, func(event pubsub.Event) {
switch event.(type) {
case bridge.CrossTransferEvent:
crossTransferEvent := event.(bridge.CrossTransferEvent)
Expand Down
Loading

0 comments on commit e017bfe

Please sign in to comment.