Skip to content

Commit 45bf85e

Browse files
authored
Merge pull request #7 from zen-eth/optimize
chore: do some optimization
2 parents 6568332 + e6b2bed commit 45bf85e

7 files changed

Lines changed: 33 additions & 13 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ GOBIN = ./build/bin
88
GO ?= latest
99
GORUN = go run
1010

11-
GIT_COMMIT := $(shell git rev-parse HEAD)
11+
GIT_COMMIT := $(shell git rev-parse --short=8 HEAD)
1212
GIT_DATE := $(shell git log -1 --format=%ci | cut -d ' ' -f 1)
1313

1414
#? shisui: Build shisui
@@ -21,7 +21,7 @@ shisui:
2121

2222
#? shisui-image: Build shisui image
2323
shisui-image:
24-
docker build -t ghcr.io/optimism-java/shisui:latest -f Dockerfile .
24+
docker build -t ghcr.io/zen-eth/shisui:latest -f Dockerfile .
2525

2626
#? fmt: Ensure consistent code formatting.
2727
fmt:

cmd/shisui/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"github.com/optimism-java/shisui2/web3"
4343
"github.com/protolambda/zrnt/eth2/configs"
4444
"github.com/urfave/cli/v2"
45+
"go.uber.org/automaxprocs/maxprocs"
4546
)
4647

4748
var (
@@ -111,6 +112,10 @@ func init() {
111112
flags.AutoEnvVars(app.Flags, "SHISUI")
112113

113114
app.Before = func(ctx *cli.Context) error {
115+
_, err := maxprocs.Set() // Automatically set GOMAXPROCS to match Linux container CPU quota.
116+
if err != nil {
117+
return err
118+
}
114119
flags.MigrateGlobalFlags(ctx)
115120
if err := debug.Setup(ctx); err != nil {
116121
return err

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/stretchr/testify v1.10.0
2424
github.com/tetratelabs/wabin v0.0.0-20230304001439-f6f874872834
2525
github.com/urfave/cli/v2 v2.27.5
26+
go.uber.org/automaxprocs v1.6.0
2627
go.uber.org/zap v1.27.0
2728
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
2829
golang.org/x/text v0.21.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
193193
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
194194
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
195195
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
196+
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
197+
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
196198
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
197199
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
198200
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
@@ -254,6 +256,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
254256
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
255257
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
256258
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
259+
go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=
260+
go.uber.org/automaxprocs v1.6.0/go.mod h1:ifeIMSnPZuznNm6jmdzmU3/bfk01Fe2fotchwEFJ8r8=
257261
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
258262
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
259263
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=

history/history_network.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (h *Network) GetBlockHeader(blockHash []byte) (*types.Header, error) {
148148
}
149149
err = h.portalProtocol.Put(contentKey, contentId, content)
150150
if err != nil {
151-
h.log.Error("failed to store content in getBlockHeader", "contentKey", hexutil.Encode(contentKey), "content", hexutil.Encode(content))
151+
h.log.Error("failed to store content in getBlockHeader", "contentKey", hexutil.Encode(contentKey), "err", err)
152152
}
153153
return header, nil
154154
}
@@ -199,7 +199,7 @@ func (h *Network) GetBlockBody(blockHash []byte) (*types.Body, error) {
199199
}
200200
err = h.portalProtocol.Put(contentKey, contentId, content)
201201
if err != nil {
202-
h.log.Error("failed to store content in getBlockBody", "contentKey", hexutil.Encode(contentKey), "content", hexutil.Encode(content))
202+
h.log.Error("failed to store content in getBlockBody", "contentKey", hexutil.Encode(contentKey), "err", err)
203203
}
204204
return body, nil
205205
}
@@ -248,7 +248,7 @@ func (h *Network) GetReceipts(blockHash []byte) ([]*types.Receipt, error) {
248248
}
249249
err = h.portalProtocol.Put(contentKey, contentId, content)
250250
if err != nil {
251-
h.log.Error("failed to store content in getReceipts", "contentKey", hexutil.Encode(contentKey), "content", hexutil.Encode(content))
251+
h.log.Error("failed to store content in getReceipts", "contentKey", hexutil.Encode(contentKey), "err", err)
252252
}
253253
return receipts, nil
254254
}
@@ -564,8 +564,7 @@ func (h *Network) validateContents(contentKeys [][]byte, contents [][]byte) erro
564564
contentKey := contentKeys[i]
565565
err := h.validateContent(contentKey, content)
566566
if err != nil {
567-
h.log.Error("content validate failed", "contentKey", hexutil.Encode(contentKey), "content", hexutil.Encode(content), "err", err)
568-
return fmt.Errorf("content validate failed with content key %x and content %x", contentKey, content)
567+
return fmt.Errorf("content validate failed with content key %x and content %x, err is %v", contentKey, content, err)
569568
}
570569
contentId := h.portalProtocol.ToContentId(contentKey)
571570
_ = h.portalProtocol.Put(contentKey, contentId, content)

storage/content_storage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ var ErrContentNotFound = fmt.Errorf("content not found")
1010
var ErrInsufficientRadius = fmt.Errorf("insufficient radius")
1111

1212
var MaxDistance = uint256.MustFromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
13-
14-
var SizeKey = []byte("size")
13+
var smallestDistance = uint256.NewInt(0).Bytes32()
14+
var SizeKey = smallestDistance[:] // smallest key, make sure the key will not be prund
1515

1616
type ContentType byte
1717

storage/pebble/storage.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pebble
33
import (
44
"bytes"
55
"encoding/binary"
6+
"fmt"
67
"runtime"
78
"sync"
89
"sync/atomic"
@@ -237,25 +238,28 @@ func (c *ContentStorage) Close() error {
237238

238239
func (c *ContentStorage) prune() error {
239240
expectSize := uint64(float64(c.storageCapacityInBytes) * contentDeletionFraction)
240-
var curentSize uint64 = 0
241+
var currentSize uint64 = 0
242+
c.log.Debug("start pruning", "expectSize", expectSize)
241243

242244
// get the keys to be deleted order by distance desc
243245
iter, err := c.db.NewIter(nil)
244246
if err != nil {
245247
return err
246248
}
247249

250+
pruneCount := 0
248251
batch := c.db.NewBatch()
249252
for iter.Last(); iter.Valid(); iter.Prev() {
250253
if bytes.Equal(iter.Key(), storage.SizeKey) {
251254
continue
252255
}
253-
if curentSize < expectSize {
256+
if currentSize < expectSize {
254257
err := batch.Delete(iter.Key(), nil)
255258
if err != nil {
256259
return err
257260
}
258-
curentSize += uint64(len(iter.Key())) + uint64(len(iter.Value()))
261+
currentSize += uint64(len(iter.Key())) + uint64(len(iter.Value()))
262+
pruneCount++
259263
} else {
260264
distance := iter.Key()
261265
dis := uint256.NewInt(0)
@@ -267,7 +271,12 @@ func (c *ContentStorage) prune() error {
267271
break
268272
}
269273
}
270-
newSize := c.size.Add(-curentSize)
274+
size := c.size.Load()
275+
if size < currentSize {
276+
return fmt.Errorf("prune error, size < currentSize, got size %d, currentSize %d", size, currentSize)
277+
}
278+
newSize := size - currentSize
279+
c.size.Store(newSize)
271280
buf := c.bytePool.Get().(*[]byte)
272281
defer c.bytePool.Put(buf)
273282
binary.BigEndian.PutUint64(*buf, newSize)
@@ -279,6 +288,8 @@ func (c *ContentStorage) prune() error {
279288
if err != nil {
280289
return err
281290
}
291+
292+
c.log.Debug("prune finished", "pruneCount", pruneCount, "pruneSize", currentSize, "current storage", newSize)
282293
go func() {
283294
start := uint256.NewInt(0).Bytes32()
284295
end := storage.MaxDistance.Bytes32()

0 commit comments

Comments
 (0)