Skip to content

Commit

Permalink
fix:fix minor warn issues
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <[email protected]>
  • Loading branch information
GrapeBaBa committed Apr 19, 2024
1 parent bcc4d59 commit 3014734
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 60 deletions.
4 changes: 2 additions & 2 deletions p2p/discover/portal_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ func (p *PortalProtocol) lookupSelf() []*enode.Node {

func (p *PortalProtocol) newRandomLookup(ctx context.Context) *lookup {
var target enode.ID
crand.Read(target[:])
_, _ = crand.Read(target[:])
return p.newLookup(ctx, target)
}

Expand Down Expand Up @@ -1395,7 +1395,7 @@ func (p *PortalProtocol) ResolveNodeId(id enode.ID) *enode.Node {
}

// Otherwise do a network lookup.
result := p.Lookup(n.ID())
result := p.Lookup(id)
for _, rn := range result {
if rn.ID() == id {
if n != nil && rn.Seq() <= n.Seq() {
Expand Down
4 changes: 2 additions & 2 deletions portalnetwork/beacon/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func (p *API) BeaconGossip(contentKeyHex, contentHex string) (int, error) {
return p.Gossip(contentKeyHex, contentHex)
}

func (p *API) BeaconTraceRecursiveFindContent(contentKeyHex string) {
p.TraceRecursiveFindContent(contentKeyHex)
func (p *API) BeaconTraceRecursiveFindContent(contentKeyHex string) (*discover.TraceContentResult, error) {
return p.TraceRecursiveFindContent(contentKeyHex)
}

func NewBeaconNetworkAPI(BeaconAPI *discover.PortalProtocolAPI) *API {
Expand Down
14 changes: 11 additions & 3 deletions portalnetwork/beacon/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,25 @@ func (bs *BeaconStorage) getLcUpdateValueByRange(start, end uint64) ([]byte, err
return nil, err
}
hasData := false
defer rows.Close()
defer func(rows *sql.Rows) {
err = rows.Close()
if err != nil {
bs.log.Error("failed to close rows", "err", err)
}
}(rows)
for rows.Next() {
hasData = true
var val []byte
err := rows.Scan(&val)
err = rows.Scan(&val)
if err != nil {
return nil, err
}
update := new(ForkedLightClientUpdate)
dec := codec.NewDecodingReader(bytes.NewReader(val), uint64(len(val)))
update.Deserialize(bs.spec, dec)
err = update.Deserialize(bs.spec, dec)
if err != nil {
return nil, err
}
lightClientUpdateRange = append(lightClientUpdateRange, *update)
}
if !hasData {
Expand Down
6 changes: 4 additions & 2 deletions portalnetwork/beacon/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/portalnetwork/storage"
"github.com/ethereum/go-ethereum/portalnetwork/utils"
"github.com/holiman/uint256"
_ "github.com/mattn/go-sqlite3"
"github.com/protolambda/zrnt/eth2/configs"
Expand Down Expand Up @@ -55,7 +54,10 @@ func TestGetAndPut(t *testing.T) {
}

func genStorage(testDir string) (storage.ContentStorage, error) {
utils.EnsureDir(testDir)
err := os.MkdirAll(testDir, 0755)
if err != nil {
return nil, err
}
db, err := sql.Open("sqlite3", path.Join(testDir, dbName))
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion portalnetwork/beacon/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/protolambda/zrnt/eth2/beacon/capella"
"github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/protolambda/ztyp/codec"
tree "github.com/protolambda/ztyp/tree"
"github.com/protolambda/ztyp/tree"
)

const MaxRequestLightClientUpdates = 128
Expand Down
3 changes: 3 additions & 0 deletions portalnetwork/history/accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ func BuildProof(header types.Header, epochAccumulator EpochAccumulator) (Accumul
// maybe the calculation of index should impl in ssz
proofIndex := epochSize*2 + index*2
sszProof, err := tree.Prove(int(proofIndex))
if err != nil {
return nil, err
}
// the epoch hash root has mix in with epochsize, so we have to add it to proof
hashes := sszProof.Hashes
sizeBytes := make([]byte, 32)
Expand Down
6 changes: 3 additions & 3 deletions portalnetwork/history/history_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (h *HistoryNetwork) GetBlockBody(blockHash []byte) (*types.Body, error) {
res, err := h.portalProtocol.Get(contentKey, contentId)
// other error
// TODO maybe use nil res to replace the ErrContentNotFound
if err != nil && err != storage.ErrContentNotFound {
if err != nil && !errors.Is(err, storage.ErrContentNotFound) {
return nil, err
}
// no error
Expand Down Expand Up @@ -215,7 +215,7 @@ func (h *HistoryNetwork) GetReceipts(blockHash []byte) ([]*types.Receipt, error)

res, err := h.portalProtocol.Get(contentKey, contentId)
// other error
if err != nil && err != storage.ErrContentNotFound {
if err != nil && !errors.Is(err, storage.ErrContentNotFound) {
return nil, err
}
// no error
Expand Down Expand Up @@ -256,7 +256,7 @@ func (h *HistoryNetwork) GetEpochAccumulator(epochHash []byte) (*EpochAccumulato

res, err := h.portalProtocol.Get(contentKey, contentId)
// other error
if err != nil && err != storage.ErrContentNotFound {
if err != nil && !errors.Is(err, storage.ErrContentNotFound) {
return nil, err
}
// no error
Expand Down
2 changes: 1 addition & 1 deletion portalnetwork/history/history_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ func parseDataForBlock14764013() (map[string]contentEntry, error) {
}

contentMap := make(map[string]map[string]string)
json.Unmarshal(content, &contentMap)
_ = json.Unmarshal(content, &contentMap)
res := make(map[string]contentEntry)
for key, val := range contentMap {
entry := contentEntry{}
Expand Down
10 changes: 5 additions & 5 deletions portalnetwork/history/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (p *BlockHeaderProof) MarshalSSZ() ([]byte, error) {
return ssz.MarshalSSZ(p)
}

func (p *BlockHeaderProof) MarshalSSZTo(buf []byte) (dst []byte, err error) {
func (p *BlockHeaderProof) MarshalSSZTo(_ []byte) (dst []byte, err error) {
return ssz.MarshalSSZ(p)
}

Expand All @@ -83,7 +83,7 @@ func (p *BlockHeaderProof) UnmarshalSSZ(buf []byte) (err error) {
proof[i] = proofBytes[i*32 : (i+1)*32]
}

p.Proof = AccumulatorProof(proof)
p.Proof = proof
return
}

Expand All @@ -103,7 +103,7 @@ func (p *BlockHeaderProof) SizeSSZ() (size int) {
return size
}

func (p *BlockHeaderProof) HashTreeRootWith(hh ssz.HashWalker) (err error) {
func (p *BlockHeaderProof) HashTreeRootWith(_ ssz.HashWalker) (err error) {
panic("implement me")
}

Expand Down Expand Up @@ -142,7 +142,7 @@ func (p *PortalReceipts) MarshalSSZTo(buf []byte) (dst []byte, err error) {
return
}

// UnmarshalSSZ ssz unmarshals the PortalReceipts object
// UnmarshalSSZ ssz unmarshal the PortalReceipts object
func (p *PortalReceipts) UnmarshalSSZ(buf []byte) error {
var err error
size := uint64(len(buf))
Expand Down Expand Up @@ -187,6 +187,6 @@ func (p *PortalReceipts) SizeSSZ() (size int) {
}

// HashTreeRootWith ssz hashes the PortalReceipts object with a hasher
func (p *PortalReceipts) HashTreeRootWith(hh ssz.HashWalker) (err error) {
func (p *PortalReceipts) HashTreeRootWith(_ ssz.HashWalker) (err error) {
panic("implement me")
}
50 changes: 39 additions & 11 deletions portalnetwork/storage/sqlite/content_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/portalnetwork/storage"
"github.com/holiman/uint256"
sqlite3 "github.com/mattn/go-sqlite3"
"github.com/mattn/go-sqlite3"
)

const (
Expand All @@ -31,7 +31,7 @@ const (
containSql = "SELECT 1 FROM kvstore WHERE key = (?1);"
getAllOrderedByDistanceSql = "SELECT key, length(value), xor(key, (?1)) as distance FROM kvstore ORDER BY distance DESC;"
deleteOutOfRadiusStmt = "DELETE FROM kvstore WHERE greater(xor(key, (?1)), (?2)) = 1"
XOR_FIND_FARTHEST_QUERY = `SELECT
XorFindFarthestQuery = `SELECT
xor(key, (?1)) as distance
FROM kvstore
ORDER BY distance DESC`
Expand Down Expand Up @@ -148,7 +148,7 @@ func createDir(dir string) error {
func (p *ContentStorage) Get(contentKey []byte, contentId []byte) ([]byte, error) {
var res []byte
err := p.getStmt.QueryRow(contentId).Scan(&res)
if err == sql.ErrNoRows {
if errors.Is(err, sql.ErrNoRows) {
return nil, storage.ErrContentNotFound
}
return res, err
Expand Down Expand Up @@ -208,10 +208,22 @@ func (p *ContentStorage) put(contentId []byte, content []byte) PutResult {
}

func (p *ContentStorage) Close() error {
p.getStmt.Close()
p.putStmt.Close()
p.delStmt.Close()
p.containStmt.Close()
err := p.getStmt.Close()
if err != nil {
return err
}
err = p.putStmt.Close()
if err != nil {
return err
}
err = p.delStmt.Close()
if err != nil {
return err
}
err = p.containStmt.Close()
if err != nil {
return err
}
return p.sqliteDB.Close()
}

Expand All @@ -220,7 +232,12 @@ func (p *ContentStorage) createTable() error {
if err != nil {
return err
}
defer stat.Close()
defer func(stat *sql.Stmt) {
err = stat.Close()
if err != nil {
p.log.Error("failed to close statement", "err", err)
}
}(stat)
_, err = stat.Exec()
return err
}
Expand Down Expand Up @@ -301,7 +318,7 @@ func (p *ContentStorage) queryRowUint64(sql string) (uint64, error) {

// GetLargestDistance find the largest distance
func (p *ContentStorage) GetLargestDistance() (*uint256.Int, error) {
stmt, err := p.sqliteDB.Prepare(XOR_FIND_FARTHEST_QUERY)
stmt, err := p.sqliteDB.Prepare(XorFindFarthestQuery)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -356,7 +373,12 @@ func (p *ContentStorage) deleteContentFraction(fraction float64) (deleteCount in
if err != nil {
return deleteCount, err
}
defer rows.Close()
defer func(rows *sql.Rows) {
err = rows.Close()
if err != nil {
p.log.Error("failed to close rows", "err", err)
}
}(rows)
idsToDelete := make([][]byte, 0)
for deleteBytes < int(bytesToDelete) && rows.Next() {
var contentId []byte
Expand All @@ -376,7 +398,10 @@ func (p *ContentStorage) deleteContentFraction(fraction float64) (deleteCount in
}
// row must close first, or database is locked
// rows.Close() can call multi times
rows.Close()
err = rows.Close()
if err != nil {
return 0, err
}
err = p.batchDel(idsToDelete)
return
}
Expand Down Expand Up @@ -407,6 +432,9 @@ func (p *ContentStorage) ReclaimSpace() error {

func (p *ContentStorage) deleteContentOutOfRadius(radius *uint256.Int) error {
res, err := p.sqliteDB.Exec(deleteOutOfRadiusStmt, p.nodeId[:], radius.Bytes())
if err != nil {
return err
}
count, _ := res.RowsAffected()
p.log.Trace("delete %d items", count)
return err
Expand Down
12 changes: 6 additions & 6 deletions portalnetwork/storage/sqlite/content_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
const nodeDataDir = "./"

func clearNodeData() {
os.Remove(fmt.Sprintf("%s%s", nodeDataDir, sqliteName))
_ = os.Remove(fmt.Sprintf("%s%s", nodeDataDir, sqliteName))
}

func genBytes(length int) []byte {
Expand All @@ -28,7 +28,7 @@ func genBytes(length int) []byte {

func TestBasicStorage(t *testing.T) {
zeroNodeId := uint256.NewInt(0).Bytes32()
storage, err := newContentStorage(math.MaxUint32, enode.ID(zeroNodeId), nodeDataDir)
storage, err := newContentStorage(math.MaxUint32, zeroNodeId, nodeDataDir)
assert.NoError(t, err)
defer clearNodeData()
defer storage.Close()
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestBasicStorage(t *testing.T) {

func TestDBSize(t *testing.T) {
zeroNodeId := uint256.NewInt(0).Bytes32()
storage, err := newContentStorage(math.MaxUint32, enode.ID(zeroNodeId), nodeDataDir)
storage, err := newContentStorage(math.MaxUint32, zeroNodeId, nodeDataDir)
assert.NoError(t, err)
defer clearNodeData()
defer storage.Close()
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestDBPruning(t *testing.T) {
storageCapacity := uint64(100_000)

zeroNodeId := uint256.NewInt(0).Bytes32()
storage, err := newContentStorage(storageCapacity, enode.ID(zeroNodeId), nodeDataDir)
storage, err := newContentStorage(storageCapacity, zeroNodeId, nodeDataDir)
assert.NoError(t, err)
defer clearNodeData()
defer storage.Close()
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestGetLargestDistance(t *testing.T) {
storageCapacity := uint64(100_000)

zeroNodeId := uint256.NewInt(0).Bytes32()
storage, err := newContentStorage(storageCapacity, enode.ID(zeroNodeId), nodeDataDir)
storage, err := newContentStorage(storageCapacity, zeroNodeId, nodeDataDir)
assert.NoError(t, err)
defer clearNodeData()
defer storage.Close()
Expand All @@ -217,7 +217,7 @@ func TestSimpleForcePruning(t *testing.T) {
storageCapacity := uint64(100_000)

zeroNodeId := uint256.NewInt(0).Bytes32()
storage, err := newContentStorage(storageCapacity, enode.ID(zeroNodeId), nodeDataDir)
storage, err := newContentStorage(storageCapacity, zeroNodeId, nodeDataDir)
assert.NoError(t, err)
defer clearNodeData()
defer storage.Close()
Expand Down
24 changes: 0 additions & 24 deletions portalnetwork/utils/util.go

This file was deleted.

0 comments on commit 3014734

Please sign in to comment.