Skip to content

Commit

Permalink
adjust gateway order in init
Browse files Browse the repository at this point in the history
  • Loading branch information
jowenshaw committed May 11, 2021
1 parent 4434c24 commit 178c340
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
4 changes: 4 additions & 0 deletions tokens/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/anyswap/CrossChain-Bridge/tokens/eth"
"github.com/anyswap/CrossChain-Bridge/tokens/fsn"
"github.com/anyswap/CrossChain-Bridge/tokens/ltc"
"github.com/anyswap/CrossChain-Bridge/tokens/tools"
)

// NewCrossChainBridge new bridge according to chain name
Expand Down Expand Up @@ -63,6 +64,9 @@ func InitCrossChainBridge(isServer bool) {
tokens.DstBridge.SetChainAndGateway(dstChain, dstGateway)
log.Info("Init bridge destation", "dest", dstID, "gateway", dstGateway)

tools.AdjustGatewayOrder(true)
tools.AdjustGatewayOrder(false)

tokens.IsDcrmDisabled = cfg.Dcrm.Disable
tokens.LoadTokenPairsConfig(true)

Expand Down
2 changes: 1 addition & 1 deletion tokens/eth/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (b *Bridge) verifyDecimals(tokenCfg *tokens.TokenConfig) error {
if checkToken != "" {
decimals, err := b.GetErc20Decimals(checkToken)
if err != nil {
log.Error("get erc20 decimals failed", "err", err)
log.Error("get erc20 decimals failed", "address", checkToken, "err", err)
return err
}
if decimals != configedDecimals {
Expand Down
27 changes: 27 additions & 0 deletions tokens/tools/swaptools.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,30 @@ func IsAddressRegistered(address string) bool {
}
return false
}

// AdjustGatewayOrder adjust gateway order by block height
func AdjustGatewayOrder(isSrc bool) {
// use block number as weight
var weightedAPIs WeightedStringSlice
bridge := tokens.GetCrossChainBridge(isSrc)
gateway := bridge.GetGatewayConfig()
length := len(gateway.APIAddress)
maxHeight := uint64(0)
for i := length; i > 0; i-- { // query in reverse order
apiAddress := gateway.APIAddress[i-1]
height, _ := bridge.GetLatestBlockNumberOf(apiAddress)
weightedAPIs = weightedAPIs.Add(apiAddress, height)
if height > maxHeight {
maxHeight = height
}
}
tokens.CmpAndSetLatestBlockHeight(maxHeight, isSrc)
weightedAPIs.Reverse() // reverse as iter in reverse order in the above
weightedAPIs = weightedAPIs.Sort()
gateway.APIAddress = weightedAPIs.GetStrings()
if isSrc {
log.Info("adjust source gateways", "result", weightedAPIs)
} else {
log.Info("adjust dest gateways", "result", weightedAPIs)
}
}
35 changes: 2 additions & 33 deletions worker/updatelatest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package worker
import (
"time"

"github.com/anyswap/CrossChain-Bridge/tokens"
"github.com/anyswap/CrossChain-Bridge/tokens/tools"
)

Expand All @@ -13,40 +12,10 @@ var (

// StartUpdateLatestBlockHeightJob update latest block height job
func StartUpdateLatestBlockHeightJob() {
adjustGatewayOrder()
}

func adjustGatewayOrder() {
for {
logWorker("adjustGatewayOrder", "adjust gateway api adddress order")
adjustGatewayOrderImpl(true)
adjustGatewayOrderImpl(false)
tools.AdjustGatewayOrder(true)
tools.AdjustGatewayOrder(false)
time.Sleep(adjustGatewayOrderInterval)
}
}

func adjustGatewayOrderImpl(isSrc bool) {
// use block number as weight
var weightedAPIs tools.WeightedStringSlice
bridge := tokens.GetCrossChainBridge(isSrc)
gateway := bridge.GetGatewayConfig()
length := len(gateway.APIAddress)
maxHeight := uint64(0)
for i := length; i > 0; i-- { // query in reverse order
apiAddress := gateway.APIAddress[i-1]
height, _ := bridge.GetLatestBlockNumberOf(apiAddress)
weightedAPIs = weightedAPIs.Add(apiAddress, height)
if height > maxHeight {
maxHeight = height
}
}
tokens.CmpAndSetLatestBlockHeight(maxHeight, isSrc)
weightedAPIs.Reverse() // reverse as iter in reverse order in the above
weightedAPIs = weightedAPIs.Sort()
gateway.APIAddress = weightedAPIs.GetStrings()
if isSrc {
logWorker("gateway", "adjust source gateways", "result", weightedAPIs)
} else {
logWorker("gateway", "adjust dest gateways", "result", weightedAPIs)
}
}

0 comments on commit 178c340

Please sign in to comment.