Skip to content

Commit

Permalink
add enable flag; config use list
Browse files Browse the repository at this point in the history
  • Loading branch information
chengzhinei committed Aug 8, 2024
1 parent e927692 commit 1828422
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 25 deletions.
42 changes: 36 additions & 6 deletions pool/apollo_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ type apolloConfig struct {
FreeGasExAddress []string
FreeGasCountPerAddr uint64
FreeGasLimit uint64
FreeGasList []FreeGasInfo

// for special project
EnableFreeGasList bool
FreeGasFromNameMap map[string]string // map[from]projectName
FreeGasList map[string]*FreeGasInfo // map[projectName]FreeGasInfo

BlockedList []string

Expand Down Expand Up @@ -48,8 +52,14 @@ func (c *apolloConfig) setFreeGasList(freeGasList []FreeGasInfo) {
if c == nil || !c.EnableApollo {
return
}
c.FreeGasList = make([]FreeGasInfo, len(freeGasList))
copy(c.FreeGasList, freeGasList)
c.FreeGasFromNameMap = make(map[string]string)
c.FreeGasList = make(map[string]*FreeGasInfo, len(freeGasList))
for _, info := range freeGasList {
for _, from := range info.FromList {
c.FreeGasFromNameMap[from] = info.Name
}
c.FreeGasList[info.Name] = &info

Check failure on line 61 in pool/apollo_xlayer.go

View workflow job for this annotation

GitHub Actions / lint

G601: Implicit memory aliasing in for loop. (gosec)
}
}

func (c *apolloConfig) setFreeGasAddresses(freeGasAddrs []string) {
Expand Down Expand Up @@ -104,6 +114,7 @@ func UpdateConfig(apolloConfig Config) {
getApolloConfig().setFreeGasExAddresses(apolloConfig.FreeGasExAddress)
getApolloConfig().FreeGasCountPerAddr = apolloConfig.FreeGasCountPerAddr
getApolloConfig().FreeGasLimit = apolloConfig.FreeGasLimit
getApolloConfig().EnableFreeGasList = apolloConfig.EnableFreeGasList
getApolloConfig().setFreeGasList(apolloConfig.FreeGasList)

getApolloConfig().Unlock()
Expand Down Expand Up @@ -213,13 +224,32 @@ func isBlockedAddress(localBlockedList []string, address common.Address) bool {
return contains(localBlockedList, address)
}

// GetEnableSpecialFreeGasList returns enable flag of FreeGasList
func GetEnableSpecialFreeGasList(enableFreeGasList bool) bool {
if getApolloConfig().enable() {
getApolloConfig().RLock()
defer getApolloConfig().RUnlock()
return getApolloConfig().EnableFreeGasList
}
return enableFreeGasList
}

// GetSpecialFreeGasList returns the special project in XLayer for free gas
func GetSpecialFreeGasList(freeGasList []FreeGasInfo) []FreeGasInfo {
func GetSpecialFreeGasList(freeGasList []FreeGasInfo) (map[string]string, map[string]*FreeGasInfo) {
if getApolloConfig().enable() {
getApolloConfig().RLock()
defer getApolloConfig().RUnlock()
return getApolloConfig().FreeGasList
return getApolloConfig().FreeGasFromNameMap, getApolloConfig().FreeGasList
}

freeGasFromNameMap := make(map[string]string)
freeGasMap := make(map[string]*FreeGasInfo, len(freeGasList))
for _, info := range freeGasList {
for _, from := range info.FromList {
freeGasFromNameMap[from] = info.Name
}
freeGasMap[info.Name] = &info

Check failure on line 251 in pool/apollo_xlayer.go

View workflow job for this annotation

GitHub Actions / lint

G601: Implicit memory aliasing in for loop. (gosec)
}

return freeGasList
return freeGasFromNameMap, freeGasMap
}
11 changes: 7 additions & 4 deletions pool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,19 @@ type Config struct {
FreeGasCountPerAddr uint64 `mapstructure:"FreeGasCountPerAddr"`
// FreeGasLimit is the max gas allowed use to do a free gas tx
FreeGasLimit uint64 `mapstructure:"FreeGasLimit"`
// EnableFreeGasList enable the special project of XLayer for free gas
EnableFreeGasList bool `mapstructure:"EnableFreeGasList"`
// FreeGasList is the special project of XLayer
FreeGasList []FreeGasInfo `mapstructure:"FreeGasList"`
}

// FreeGasInfo contains the details for what tx should be free
type FreeGasInfo struct {
From string `mapstructure:"From"`
To string `mapstructure:"To"`
MethodSig string `mapstructure:"MethodSig"`
GasPriceMultiple float64 `mapstructure:"GasPriceMultiple"`
Name string `mapstructure:"Name"`
FromList []string `mapstructure:"FromList"`
ToList []string `mapstructure:"ToList"`
MethodSigs []string `mapstructure:"MethodSigs"`
GasPriceMultiple float64 `mapstructure:"GasPriceMultiple"`
}

// EffectiveGasPriceCfg contains the configuration properties for the effective gas price
Expand Down
29 changes: 22 additions & 7 deletions pool/pool_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ func contains(s []string, ele common.Address) bool {
return false
}

func containsMethod(data string, methods []string) bool {
for _, m := range methods {
if strings.HasPrefix(data, m) {
return true
}
}
return false
}

// StartRefreshingWhiteAddressesPeriodically will make this instance of the pool
// to check periodically(accordingly to the configuration) for updates regarding
// the white address and update the in memory blocked addresses
Expand Down Expand Up @@ -113,17 +122,23 @@ func (p *Pool) GetDynamicGasPrice() *big.Int {
}

func (p *Pool) checkFreeGp(ctx context.Context, poolTx Transaction, from common.Address) (bool, error) {
if isFreeGasAddress(p.cfg.FreeGasAddress, from) && poolTx.IsClaims { // claim tx
// claim tx
if isFreeGasAddress(p.cfg.FreeGasAddress, from) && poolTx.IsClaims {
return true, nil
}
freeGpList := GetSpecialFreeGasList(p.cfg.FreeGasList)
for _, info := range freeGpList {
if from.String() == info.From {
if poolTx.To().String() == info.To && strings.HasPrefix("0x"+common.Bytes2Hex(poolTx.Data()), info.MethodSig) {
return true, nil
}

// special project
if GetEnableSpecialFreeGasList(p.cfg.EnableFreeGasList) {
fromToName, freeGpList := GetSpecialFreeGasList(p.cfg.FreeGasList)
info := freeGpList[fromToName[from.String()]]
if info != nil &&
contains(info.ToList, *poolTx.To()) &&
containsMethod("0x"+common.Bytes2Hex(poolTx.Data()), info.MethodSigs) {
return true, nil
}
}

// new bridge address
if getEnableFreeGasByNonce(p.cfg.EnableFreeGasByNonce) && poolTx.GasPrice().Cmp(big.NewInt(0)) == 0 { // free-gas tx by count
isFreeAddr, err := p.storage.IsFreeGasAddr(ctx, from)
if err != nil {
Expand Down
35 changes: 27 additions & 8 deletions sequencer/sequencer_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ import (

var countinterval = 10

func contains(s []string, ele common.Address) bool {
for _, e := range s {
if common.HexToAddress(e) == ele {
return true
}
}
return false
}

func containsMethod(data string, methods []string) bool {
for _, m := range methods {
if strings.HasPrefix(data, m) {
return true
}
}
return false
}

func (s *Sequencer) countPendingTx() {
for {
<-time.After(time.Second * time.Duration(countinterval))
Expand Down Expand Up @@ -49,14 +67,15 @@ func (s *Sequencer) checkFreeGas(tx pool.Transaction, txTracker *TxTracker) (fre
claimTx = true
}

// special project of XLayer
freeGpList := pool.GetSpecialFreeGasList(s.poolCfg.FreeGasList)
for _, info := range freeGpList {
if txTracker.From.String() == info.From {
if tx.To().String() == info.To && strings.HasPrefix("0x"+common.Bytes2Hex(tx.Data()), info.MethodSig) {
gpMul = info.GasPriceMultiple
return
}
// check if tx is from special project
if pool.GetEnableSpecialFreeGasList(s.poolCfg.EnableFreeGasList) {
fromToName, freeGpList := pool.GetSpecialFreeGasList(s.poolCfg.FreeGasList)
info := freeGpList[fromToName[txTracker.FromStr]]
if info != nil &&
contains(info.ToList, *tx.To()) &&
containsMethod("0x"+common.Bytes2Hex(tx.Data()), info.MethodSigs) {
gpMul = info.GasPriceMultiple
return
}
}

Expand Down

0 comments on commit 1828422

Please sign in to comment.