Skip to content

Commit

Permalink
feat: 重构赞助逻辑,支持动态检查和更新赞助信息
Browse files Browse the repository at this point in the history
  • Loading branch information
du5 committed Feb 21, 2025
1 parent 8ae5800 commit 31afdf3
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 32 deletions.
63 changes: 53 additions & 10 deletions embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package enhanced_rpc
import (
_ "embed"
"encoding/json"
"math/big"
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)

//go:embed exploiter.json
Expand All @@ -18,24 +21,64 @@ var sponsorship []byte

func GasWhitelist() (addrs []common.Address) {
var _gasWhitelist map[common.Address]string
if json.Unmarshal(gasWhitelist, &_gasWhitelist) != nil {
return
}
unmarshal(gasWhitelist, &_gasWhitelist)

for addr := range _gasWhitelist {
addrs = append(addrs, addr)
}
return
}

func GasExploiter() []common.Address {
return unmarshal(gasExploiter)
func GasExploiter() (res []common.Address) {
unmarshal(gasExploiter, &res)
return
}

func Sponsorship() []common.Address {
return unmarshal(sponsorship)
func unmarshal(data []byte, a any) {
_ = json.Unmarshal(data, a)
}

func unmarshal(data []byte) (addrs []common.Address) {
_ = json.Unmarshal(data, &addrs)
return
type Sponsorship map[common.Address]*struct {
Allow map[string]int `json:"allow"`
CheckAmount bool `json:"checkAmount"`
}

func SponsorshipInit() {
if _sponsorship != nil {
return
}
unmarshal(sponsorship, &_sponsorship)
}

var (
_sponsorship Sponsorship
mu sync.Mutex
)

func IsSponsorable(a *common.Address, data []byte) (b bool) {
SponsorshipInit()

if a == nil || len(data) <= 4 { // 格式不匹配
return
}
mu.Lock()
defer mu.Unlock()
info, ok := _sponsorship[*a]
if !ok { // 不在赞助列表中
return
}

length, ok := info.Allow[hexutil.Encode(data[:4])]
if !ok || length != len(data) { // 函数参数不匹配
return
}

if info.CheckAmount {
amount := new(big.Int).SetBytes(data[length-common.HashLength:])
if big.NewInt(48e16).Cmp(amount) > 0 { // 金额小于 0.48 ether, 不赞助
return
}
}

return true
}
14 changes: 7 additions & 7 deletions embed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/stretchr/testify/assert"
)

func TestPrintEmbed(t *testing.T) {
gasExploiter, sponsorship := GasExploiter(), Sponsorship()
assert.Equal(t, len(gasExploiter), 5)
assert.Equal(t, gasExploiter[0], common.HexToAddress("0x0000000000004946c0e9f43f4dee607b0ef1fa1c"))
assert.Equal(t, len(GasWhitelist()), 182)
assert.Equal(t, len(sponsorship), 4)
assert.Equal(t, sponsorship[0], common.HexToAddress("0x55d398326f99059fF775485246999027B3197955"))
func TestSponsorship(t *testing.T) {
SponsorshipInit()
assert.NotNil(t, _sponsorship)
addr := common.HexToAddress("0xabA39a94091f130f1Eb33d2B04022f9154A5715D")
data := hexutil.MustDecode("0x3e4e1e27000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000080000000000000000000000000ff5c0d4727e6aebfee41c223d0becadb465871f8000000000000000000000000000000000000000000000000000000000000000bcfaa31141b8c6ad0856638353d29922c1d3af4dab16542186f0f7237f277c457cb4b1dba6e450f0b1994429e2636aa9e8cc34b43caa6fb66a43fd4210aa88dad231e23cff3199cb0f1c5fa38684d345f89c1dbdc213d2ee4a53d0f200a6dd1e4513e8dcda2bf58b16e0056d538a598ec9233f21961558de0ed84bf992cc8a1948bd9cec23d2c99456dc6bc4e23f8c413fa4e0e21866e89e47aa4dc6abb9b1aa23bec5c43e7b714494c7fdae8b39fed8ab69b4fff27f2df604c1699024c7e603f49cd66eeb1f734b95b90ce4ce93b26cca745f09d9f7576b5956a3eca11d344e5e48ef462a489574246d30df5b98cfc0c290285bf594907a176c8bbc6baca97fed8863cda680e4839238e021d2cd845f9f2ba919802480cffe2052c8ca9c34f74b0b80eb20e0faf89d2a473755913d447f04ffed5dc2497096f7de5ba6a6403fe992f95e8b8d603ee09bf4c41bec0204e534eda1053ef6292bc9d8e9d3eda7122")
assert.True(t, IsSponsorable(&addr, data))
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/holiman/uint256 v1.3.1 // indirect
github.com/holiman/uint256 v1.3.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/crypto v0.33.0 // indirect
golang.org/x/sys v0.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ethereum/go-ethereum v1.14.8 h1:NgOWvXS+lauK+zFukEvi85UmmsS/OkV0N23UZ1VTIig=
github.com/ethereum/go-ethereum v1.14.8/go.mod h1:TJhyuDq0JDppAkFXgqjwpdlQApywnu/m10kFPxh8vvs=
github.com/holiman/uint256 v1.3.1 h1:JfTzmih28bittyHM8z360dCjIA9dbPIBlcTI6lmctQs=
github.com/holiman/uint256 v1.3.1/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/holiman/uint256 v1.3.2 h1:a9EgMPSC1AAaj1SZL5zIQD3WbwTuHrMGOerLjGmM/TA=
github.com/holiman/uint256 v1.3.2/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
58 changes: 52 additions & 6 deletions sponsorship.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,52 @@
[
"0x55d398326f99059fF775485246999027B3197955",
"0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d",
"0xc5f0f7b66764F6ec8C8Dff7BA683102295E16409",
"0x0782b6d8c4551B9760e74c0545a9bCD90bdc41E5"
]
{
"0x55d398326f99059fF775485246999027B3197955": {
"allow": {
"0x42966c68": 36,
"0xa457c2d7": 68,
"0x39509351": 68,
"0xa9059cbb": 68,
"0x23b872dd": 100
},
"checkAmount": true
},
"0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d": {
"allow": {
"0x42966c68": 36,
"0xa457c2d7": 68,
"0x39509351": 68,
"0xa9059cbb": 68,
"0x23b872dd": 100
},
"checkAmount": true
},
"0xc5f0f7b66764F6ec8C8Dff7BA683102295E16409": {
"allow": {
"0x42966c68": 36,
"0xa457c2d7": 68,
"0x39509351": 68,
"0xa9059cbb": 68,
"0x23b872dd": 100
},
"checkAmount": true
},
"0x0782b6d8c4551B9760e74c0545a9bCD90bdc41E5": {
"allow": {
"0x9dc29fac": 36,
"0xa457c2d7": 68,
"0x39509351": 68,
"0xa9059cbb": 68,
"0x23b872dd": 100,
"0xf2d5d56b": 68,
"0xb753a98c": 68,
"0xbb35783b": 100
},
"checkAmount": true
},
"0xabA39a94091f130f1Eb33d2B04022f9154A5715D": {
"allow": {
"0x3e4e1e27": 516,
"0xb1721a1e": 452
},
"checkAmount": false
}
}

0 comments on commit 31afdf3

Please sign in to comment.