Skip to content

Commit 5dbaf4d

Browse files
committed
* main to dev branch (#1) * zjubfd: add no trie options, support fastnode * no truncate * add prune path db * fix destruction error * update parallel param * fix possible prefetch panic * protect from nil root * add the cache size of code * mute unnecessary log * Dev (OffchainLabs#4) * add initial commit of trade * fix root 00 issue * refine the sync to target * refine the sync to target * add tg bots and more * update gas price for test mode * refine the order update * refine the order update * refine fee calculate * update * update * update * update * update * update * update * update better tg message * add pair price track * update test case * add webscoket * decouple trade module * extract seracher as a processer * update trade * add executor wallets * update * remove duplicate ws * add prefetch routine * update v2 calculation method * add test code * add test code * add test code * add crypty code * update some test log * update some test log * update some test log * update some test log * update some test log * update some test log * update * support limit order
1 parent fe26733 commit 5dbaf4d

39 files changed

+883
-250
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030
/geth*.zip.sig
3131
/geth*.zip.asc
3232

33+
./trade/binance_test.go
34+
trade/binance_test.go
35+
trade/crypto_test.go
36+
trade/config/config.json
37+
trade/txhistory.md
38+
trade/pair_executor_test.go
39+
trade/network-info.md
40+
trade/metrics/*
41+
3342

3443
# travis
3544
profile.tmp
@@ -43,3 +52,4 @@ profile.cov
4352
.vscode
4453

4554
tests/spec-tests/
55+
trade/config/config copy.json

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# with Go source code. If you know what GOPATH is then you probably
33
# don't need to bother with make.
44

5-
.PHONY: geth all test lint fmt clean devtools help
5+
.PHONY: geth searcher all test lint fmt clean devtools help
66

77
GOBIN = ./build/bin
88
GO ?= latest
@@ -14,6 +14,12 @@ geth:
1414
@echo "Done building."
1515
@echo "Run \"$(GOBIN)/geth\" to launch geth."
1616

17+
#? searcher: Build the MEV searcher.
18+
searcher:
19+
$(GORUN) build/ci.go install ./cmd/searcher
20+
@echo "Done building."
21+
@echo "Run \"$(GOBIN)/searcher\" to launch the MEV searcher."
22+
1723
#? all: Build all packages and executables.
1824
all:
1925
$(GORUN) build/ci.go install

accounts/abi/bind/base.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,13 @@ func (c *BoundContract) createDynamicTx(opts *TransactOpts, contract *common.Add
334334
gasLimit := opts.GasLimit
335335
if opts.GasLimit == 0 {
336336
var err error
337+
// TODO 这里给一个合适的gas limit, 防止迭代次数太多
337338
gasLimit, err = c.estimateGasLimit(opts, contract, input, nil, gasTipCap, gasFeeCap, value)
338339
if err != nil {
339340
return nil, err
340341
}
342+
// TODO 需要优化
343+
gasLimit = gasLimit * 3 / 2
341344
}
342345
// create the transaction
343346
nonce, err := c.getNonce(opts)
@@ -409,8 +412,10 @@ func (c *BoundContract) estimateGasLimit(opts *TransactOpts, contract *common.Ad
409412
return 0, ErrNoCode
410413
}
411414
}
415+
// Note: 这里给一个比较小的的gas limit, 防止迭代次数太多
412416
msg := ethereum.CallMsg{
413417
From: opts.From,
418+
Gas: 1000000,
414419
To: contract,
415420
GasPrice: gasPrice,
416421
GasTipCap: gasTipCap,

cmd/geth/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ var (
8080
utils.BlobPoolDataCapFlag,
8181
utils.BlobPoolPriceBumpFlag,
8282
utils.SyncModeFlag,
83+
utils.TriesNoVerifyFlag,
8384
utils.SyncTargetFlag,
8485
utils.ExitWhenSyncedFlag,
8586
utils.GCModeFlag,
@@ -256,6 +257,7 @@ func init() {
256257
debug.Flags,
257258
metricsFlags,
258259
)
260+
259261
flags.AutoEnvVars(app.Flags, "GETH")
260262

261263
app.Before = func(ctx *cli.Context) error {

cmd/geth/snapshot.go

Lines changed: 122 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ package main
1818

1919
import (
2020
"bytes"
21+
"encoding/binary"
2122
"encoding/json"
2223
"errors"
2324
"fmt"
25+
"math"
2426
"os"
2527
"slices"
2628
"time"
@@ -33,6 +35,7 @@ import (
3335
"github.com/ethereum/go-ethereum/core/state/snapshot"
3436
"github.com/ethereum/go-ethereum/core/types"
3537
"github.com/ethereum/go-ethereum/crypto"
38+
"github.com/ethereum/go-ethereum/ethdb"
3639
"github.com/ethereum/go-ethereum/log"
3740
"github.com/ethereum/go-ethereum/rlp"
3841
"github.com/ethereum/go-ethereum/trie"
@@ -63,6 +66,29 @@ two version states are available: genesis and the specific one.
6366
The default pruning target is the HEAD-127 state.
6467
6568
WARNING: it's only supported in hash mode(--state.scheme=hash)".
69+
`,
70+
},
71+
{
72+
Name: "insecure-prune-pathdb",
73+
Usage: "Prune all pathdb state data, it will break storage for fullnode, only suitable for fast node " +
74+
"who do not need trie storage at all",
75+
ArgsUsage: "",
76+
Action: prunePathDB,
77+
Category: "MISCELLANEOUS COMMANDS",
78+
Flags: []cli.Flag{
79+
utils.DataDirFlag,
80+
utils.AncientFlag,
81+
},
82+
Description: `
83+
will prune all historical trie state data except genesis block.
84+
All trie nodes will be deleted from the database.
85+
86+
It expects the genesis file as argument.
87+
88+
WARNING: It's necessary to delete the trie clean cache after the pruning.
89+
If you specify another directory for the trie clean cache via "--cache.trie.journal"
90+
during the use of Geth, please also specify it here for correct deletion. Otherwise
91+
the trie clean cache with default directory will be deleted.
6692
`,
6793
},
6894
{
@@ -165,6 +191,99 @@ the expected order for the overlay tree migration.
165191
}
166192
)
167193

194+
// rangeCompactionThreshold is the minimal deleted entry number for
195+
// triggering range compaction. It's a quite arbitrary number but just
196+
// to avoid triggering range compaction because of small deletion.
197+
const rangeCompactionThreshold = 1000000
198+
199+
func prunePathDB(ctx *cli.Context) error {
200+
stack, _ := makeConfigNode(ctx)
201+
defer stack.Close()
202+
203+
pruneDB := utils.MakeChainDatabase(ctx, stack, false)
204+
defer pruneDB.Close()
205+
206+
var (
207+
count int
208+
size common.StorageSize
209+
pstart = time.Now()
210+
logged = time.Now()
211+
batch = pruneDB.NewBatch()
212+
iter = pruneDB.NewIterator(nil, nil)
213+
stateIDPrefix = []byte("L")
214+
)
215+
for iter.Next() {
216+
key := iter.Key()
217+
doDelete := false
218+
switch {
219+
case bytes.HasPrefix(key, stateIDPrefix) && len(key) == len(stateIDPrefix)+common.HashLength:
220+
doDelete = true
221+
case rawdb.IsAccountTrieNode(key):
222+
doDelete = true
223+
case rawdb.IsStorageTrieNode(key):
224+
doDelete = true
225+
default:
226+
}
227+
if doDelete {
228+
count += 1
229+
size += common.StorageSize(len(key) + len(iter.Value()))
230+
batch.Delete(key)
231+
232+
var eta time.Duration // Realistically will never remain uninited
233+
if done := binary.BigEndian.Uint64(key[:8]); done > 0 {
234+
var (
235+
left = math.MaxUint64 - binary.BigEndian.Uint64(key[:8])
236+
speed = done/uint64(time.Since(pstart)/time.Millisecond+1) + 1 // +1s to avoid division by zero
237+
)
238+
eta = time.Duration(left/speed) * time.Millisecond
239+
}
240+
if time.Since(logged) > 8*time.Second {
241+
log.Info("Pruning state data", "nodes", count, "size", size,
242+
"elapsed", common.PrettyDuration(time.Since(pstart)), "eta", common.PrettyDuration(eta))
243+
logged = time.Now()
244+
}
245+
// Recreate the iterator after every batch commit in order
246+
// to allow the underlying compactor to delete the entries.
247+
if batch.ValueSize() >= ethdb.IdealBatchSize {
248+
batch.Write()
249+
batch.Reset()
250+
251+
iter.Release()
252+
iter = pruneDB.NewIterator(nil, key)
253+
}
254+
}
255+
}
256+
if batch.ValueSize() > 0 {
257+
batch.Write()
258+
batch.Reset()
259+
}
260+
iter.Release()
261+
log.Info("Pruned path db data", "nodes", count, "size", size, "elapsed", common.PrettyDuration(time.Since(pstart)))
262+
263+
// Start compactions, will remove the deleted data from the disk immediately.
264+
// Note for small pruning, the compaction is skipped.
265+
if count >= rangeCompactionThreshold {
266+
cstart := time.Now()
267+
for b := 0x00; b <= 0xf0; b += 0x10 {
268+
var (
269+
start = []byte{byte(b)}
270+
end = []byte{byte(b + 0x10)}
271+
)
272+
if b == 0xf0 {
273+
end = nil
274+
}
275+
log.Info("Compacting database", "range", fmt.Sprintf("%#x-%#x", start, end), "elapsed", common.PrettyDuration(time.Since(cstart)))
276+
if err := pruneDB.Compact(start, end); err != nil {
277+
log.Error("Database compaction failed", "error", err)
278+
return err
279+
}
280+
}
281+
log.Info("Database compaction finished", "elapsed", common.PrettyDuration(time.Since(cstart)))
282+
}
283+
284+
return nil
285+
}
286+
168287
// Deprecation: this command should be deprecated once the hash-based
169288
// scheme is deprecated.
170289
func pruneState(ctx *cli.Context) error {
@@ -174,9 +293,6 @@ func pruneState(ctx *cli.Context) error {
174293
chaindb := utils.MakeChainDatabase(ctx, stack, false)
175294
defer chaindb.Close()
176295

177-
if rawdb.ReadStateScheme(chaindb) != rawdb.HashScheme {
178-
log.Crit("Offline pruning is not required for path scheme")
179-
}
180296
prunerconfig := pruner.Config{
181297
Datadir: stack.ResolvePath(""),
182298
BloomSize: ctx.Uint64(utils.BloomFilterSizeFlag.Name),
@@ -230,7 +346,7 @@ func verifyState(ctx *cli.Context) error {
230346
NoBuild: true,
231347
AsyncBuild: false,
232348
}
233-
snaptree, err := snapshot.New(snapConfig, chaindb, triedb, headBlock.Root())
349+
snaptree, err := snapshot.New(snapConfig, chaindb, triedb, headBlock.Root(), false)
234350
if err != nil {
235351
log.Error("Failed to open snapshot tree", "err", err)
236352
return err
@@ -561,7 +677,7 @@ func dumpState(ctx *cli.Context) error {
561677
NoBuild: true,
562678
AsyncBuild: false,
563679
}
564-
snaptree, err := snapshot.New(snapConfig, db, triedb, root)
680+
snaptree, err := snapshot.New(snapConfig, db, triedb, root, false)
565681
if err != nil {
566682
return err
567683
}
@@ -658,7 +774,7 @@ func snapshotExportPreimages(ctx *cli.Context) error {
658774
NoBuild: true,
659775
AsyncBuild: false,
660776
}
661-
snaptree, err := snapshot.New(snapConfig, chaindb, triedb, root)
777+
snaptree, err := snapshot.New(snapConfig, chaindb, triedb, root, false)
662778
if err != nil {
663779
return err
664780
}

cmd/utils/flags.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ var (
222222
Usage: "Reduce key-derivation RAM & CPU usage at some expense of KDF strength",
223223
Category: flags.AccountCategory,
224224
}
225+
226+
TriesNoVerifyFlag = &cli.BoolFlag{
227+
Name: "tries-no-verify",
228+
Usage: "no merkle state root verification at all",
229+
Value: true,
230+
Category: flags.FastNodeCategory,
231+
}
232+
225233
EthRequiredBlocksFlag = &cli.StringFlag{
226234
Name: "eth.requiredblocks",
227235
Usage: "Comma separated block number-to-hash mappings to require for peering (<number>=<hash>)",
@@ -230,7 +238,7 @@ var (
230238
BloomFilterSizeFlag = &cli.Uint64Flag{
231239
Name: "bloomfilter.size",
232240
Usage: "Megabytes of memory allocated to bloom-filter for pruning",
233-
Value: 2048,
241+
Value: 10,
234242
Category: flags.EthCategory,
235243
}
236244
OverrideCancun = &cli.Uint64Flag{
@@ -901,7 +909,6 @@ Please note that --` + MetricsHTTPFlag.Name + ` must be set to start the server.
901909
Value: metrics.DefaultConfig.InfluxDBTags,
902910
Category: flags.MetricsCategory,
903911
}
904-
905912
MetricsEnableInfluxDBV2Flag = &cli.BoolFlag{
906913
Name: "metrics.influxdbv2",
907914
Usage: "Enable metrics export/push to an external InfluxDB v2 database",
@@ -928,6 +935,23 @@ Please note that --` + MetricsHTTPFlag.Name + ` must be set to start the server.
928935
Value: metrics.DefaultConfig.InfluxDBOrganization,
929936
Category: flags.MetricsCategory,
930937
}
938+
939+
// Searcher flag group
940+
SearcherEnabledFlag = &cli.BoolFlag{
941+
Name: "searcher",
942+
Usage: "Enable MEV searcher functionality",
943+
Category: flags.EthCategory,
944+
}
945+
SearcherConfigFlag = &cli.StringFlag{
946+
Name: "searcher.config",
947+
Usage: "Path to the searcher configuration file",
948+
Category: flags.EthCategory,
949+
}
950+
SearcherDecryptKeyFlag = &cli.StringFlag{
951+
Name: "searcher.decryptkey-password",
952+
Usage: "Password for deriving searcher decryption key (will be securely hashed to generate a 32-byte key)",
953+
Category: flags.EthCategory,
954+
}
931955
)
932956

933957
var (
@@ -1621,6 +1645,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
16211645
cfg.StateScheme = rawdb.HashScheme
16221646
log.Warn("Forcing hash state-scheme for archive mode")
16231647
}
1648+
if ctx.IsSet(TriesNoVerifyFlag.Name) {
1649+
cfg.TriesNoVerify = ctx.Bool(TriesNoVerifyFlag.Name)
1650+
}
16241651
if ctx.IsSet(CacheFlag.Name) || ctx.IsSet(CacheTrieFlag.Name) {
16251652
cfg.TrieCleanCache = ctx.Int(CacheFlag.Name) * ctx.Int(CacheTrieFlag.Name) / 100
16261653
}

0 commit comments

Comments
 (0)