Skip to content

Commit

Permalink
support claimMessage method free claim (#170)
Browse files Browse the repository at this point in the history
* support usdc free claim

* fix lint

* fix lint

* add appolo

* fmt code

* default method

* fix json

* change name usdc to message
  • Loading branch information
chengzhinei authored Apr 10, 2024
1 parent c107cba commit a15796b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/config-file/node-config-doc.html

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions docs/config-file/node-config-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ SecretKey=""
| - [ForkID](#Pool_ForkID ) | No | integer | No | - | ForkID is the current fork ID of the chain |
| - [FreeGasAddress](#Pool_FreeGasAddress ) | No | array of string | No | - | XLayer config<br />FreeGasAddress is the default free gas address |
| - [FreeClaimGasLimit](#Pool_FreeClaimGasLimit ) | No | integer | No | - | FreeClaimGasLimit is the max gas allowed use to do a free claim |
| - [BridgeClaimMethodSigs](#Pool_BridgeClaimMethodSigs ) | No | array of string | No | - | BridgeClaimMethodSignature for tracking BridgeClaimMethodSignature method |

### <a name="Pool_IntervalToRefreshBlockedAddresses"></a>7.1. `Pool.IntervalToRefreshBlockedAddresses`

Expand Down Expand Up @@ -1241,6 +1242,11 @@ FreeGasAddress=["0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"]
FreeClaimGasLimit=150000
```

### <a name="Pool_BridgeClaimMethodSigs"></a>7.17. `Pool.BridgeClaimMethodSigs`

**Type:** : `array of string`
**Description:** BridgeClaimMethodSignature for tracking BridgeClaimMethodSignature method

## <a name="RPC"></a>8. `[RPC]`

**Type:** : `object`
Expand Down
7 changes: 7 additions & 0 deletions docs/config-file/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,13 @@
"type": "integer",
"description": "FreeClaimGasLimit is the max gas allowed use to do a free claim",
"default": 150000
},
"BridgeClaimMethodSigs": {
"items": {
"type": "string"
},
"type": "array",
"description": "BridgeClaimMethodSignature for tracking BridgeClaimMethodSignature method"
}
},
"additionalProperties": false,
Expand Down
36 changes: 31 additions & 5 deletions pool/apollo_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (

// apolloConfig is the apollo pool dynamic config
type apolloConfig struct {
EnableApollo bool
FreeGasAddresses []string
GlobalQueue uint64
AccountQueue uint64
EnableWhitelist bool
EnableApollo bool
FreeGasAddresses []string
GlobalQueue uint64
AccountQueue uint64
EnableWhitelist bool
BridgeClaimMethods []string

sync.RWMutex
}
Expand Down Expand Up @@ -42,6 +43,14 @@ func (c *apolloConfig) setFreeGasAddresses(freeGasAddrs []string) {
copy(c.FreeGasAddresses, freeGasAddrs)
}

func (c *apolloConfig) setBridgeClaimMethods(bridgeClaimMethods []string) {
if c == nil || !c.EnableApollo {
return
}
c.BridgeClaimMethods = make([]string, len(bridgeClaimMethods))
copy(c.BridgeClaimMethods, bridgeClaimMethods)
}

// UpdateConfig updates the apollo config
// GlobalQueue
// AccountQueue
Expand All @@ -54,9 +63,26 @@ func UpdateConfig(apolloConfig Config) {
getApolloConfig().AccountQueue = apolloConfig.AccountQueue
getApolloConfig().setFreeGasAddresses(apolloConfig.FreeGasAddress)
getApolloConfig().EnableWhitelist = apolloConfig.EnableWhitelist
getApolloConfig().setBridgeClaimMethods(apolloConfig.BridgeClaimMethodSigs)
getApolloConfig().Unlock()
}

func getClaimMethod(localBridgeClaimMethods []string) []string {
var methods []string
if getApolloConfig().enable() {
getApolloConfig().RLock()
defer getApolloConfig().RUnlock()
methods = getApolloConfig().BridgeClaimMethods
} else {
methods = localBridgeClaimMethods
}
if len(methods) == 0 {
methods = append(methods, BridgeClaimMethodSignature, BridgeClaimMessageMethodSignature)
}

return methods
}

func isFreeGasAddress(localFreeGasAddrs []string, address common.Address) bool {
if getApolloConfig().enable() {
getApolloConfig().RLock()
Expand Down
2 changes: 2 additions & 0 deletions pool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type Config struct {
FreeGasAddress []string `mapstructure:"FreeGasAddress"`
// FreeClaimGasLimit is the max gas allowed use to do a free claim
FreeClaimGasLimit uint64 `mapstructure:"FreeClaimGasLimit"`
// BridgeClaimMethodSignature for tracking BridgeClaimMethodSignature method
BridgeClaimMethodSigs []string `mapstructure:"BridgeClaimMethodSigs"`
}

// EffectiveGasPriceCfg contains the configuration properties for the effective gas price
Expand Down
2 changes: 2 additions & 0 deletions pool/pool_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
const (
// BridgeClaimMethodSignature for tracking BridgeClaimMethodSignature method
BridgeClaimMethodSignature = "0xccaa2d11"
// BridgeClaimMessageMethodSignature for tracking BridgeClaimMethodSignature method
BridgeClaimMessageMethodSignature = "0xf5efcd79"
)

func contains(s []string, ele common.Address) bool {
Expand Down
2 changes: 1 addition & 1 deletion pool/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewTransaction(tx types.Transaction, ip string, isWIP bool, p *Pool) *Trans
}

// XLayer handler
poolTx.IsClaims = poolTx.IsClaimTx(p.cfg.FreeClaimGasLimit)
poolTx.IsClaims = poolTx.IsClaimTx(p.cfg.FreeClaimGasLimit, p.cfg.BridgeClaimMethodSigs)

return &poolTx
}
14 changes: 8 additions & 6 deletions pool/transaction_xlayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func SetL2BridgeAddr(value common.Address) {
}

// IsClaimTx checks, if tx is a claim tx
func (tx *Transaction) IsClaimTx(freeClaimGasLimit uint64) bool {
func (tx *Transaction) IsClaimTx(freeClaimGasLimit uint64, bridgeClaimMethods []string) bool {
if tx.To() == nil {
return false
}
Expand All @@ -30,11 +30,13 @@ func (tx *Transaction) IsClaimTx(freeClaimGasLimit uint64) bool {
return false
}

if !strings.HasPrefix("0x"+common.Bytes2Hex(tx.Data()), BridgeClaimMethodSignature) {
return false
methods := getClaimMethod(bridgeClaimMethods)
for _, method := range methods {
if strings.HasPrefix("0x"+common.Bytes2Hex(tx.Data()), method) {
log.Infof("Transaction %s is a claim tx", tx.Hash().String())
return true
}
}

log.Infof("Transaction %s is a claim tx", tx.Hash().String())

return true
return false
}

0 comments on commit a15796b

Please sign in to comment.