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

R4R: Implement BEP84 #809

Merged
merged 23 commits into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from 18 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
4 changes: 3 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,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 @@ -310,6 +321,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 @@ -385,6 +400,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 @@ -421,6 +440,7 @@ func (pubCfg PublicationConfig) ShouldPublishAny() bool {
pubCfg.PublishStaking ||
pubCfg.PublishSlashing ||
pubCfg.PublishCrossTransfer ||
pubCfg.PublishMirror ||
pubCfg.PublishSideProposal ||
pubCfg.PublishBreatheBlock
}
Expand Down Expand Up @@ -500,8 +520,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 @@ -523,6 +546,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
65 changes: 65 additions & 0 deletions app/pub/msgs_mirror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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
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["totalSupply"] = msg.TotalSupply
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
}
34 changes: 34 additions & 0 deletions app/pub/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,40 @@ 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,
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
37 changes: 37 additions & 0 deletions app/pub/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,43 @@ const (
}
`

mirrorSchema = `
{
"type": "record",
"name": "Mirror",
"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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't redefine: org.binance.dex.explorer.work.domain.avro.mirror.Mirror , the name is duplicate to line 895
Can you change it to "MirrorData"?

"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": "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
29 changes: 29 additions & 0 deletions app/pub/sub/mirror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package sub

import (
"github.com/cosmos/cosmos-sdk/pubsub"

"github.com/binance-chain/node/plugins/bridge"
)

func SubscribeMirrorEvent(sub *pubsub.Subscriber) error {
err := sub.Subscribe(bridge.MirrorTopic, func(event pubsub.Event) {
switch event.(type) {
case bridge.MirrorEvent:
mirrorEvent := event.(bridge.MirrorEvent)
if stagingArea.MirrorData == nil {
stagingArea.MirrorData = make([]bridge.MirrorEvent, 0, 1)
}
stagingArea.MirrorData = append(stagingArea.MirrorData, mirrorEvent)
default:
sub.Logger.Info("unknown event type")
}
})
return err
}

func commitMirror() {
if len(stagingArea.MirrorData) > 0 {
toPublish.EventData.MirrorData = append(toPublish.EventData.MirrorData, stagingArea.MirrorData...)
}
}
Loading