mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-03 17:44:29 +00:00
329d18ef6f
Reason: - produce and seed snapshots earlier on chain tip. reduce depnedency on "good peers with history" at p2p-network. Some networks have no much archive peers, also ConsensusLayer clients are not-good(not-incentivised) at serving history. - avoiding having too much files: more files(shards) - means "more metadata", "more lookups for non-indexed queries", "more dictionaries", "more bittorrent connections", ... less files - means small files will be removed after merge (no peers for this files). ToDo: [x] Recent 500K - merge up to 100K [x] Older than 500K - merge up to 500K [x] Start seeding 100k files [x] Stop seeding 100k files after merge (right before delete) In next PR: [] Old version of Erigon must be able download recent hashes. To achieve it - at first start erigon will download preverified hashes .toml from s3 - if it's newer that what we have (build-in) - use it.
136 lines
3.8 KiB
Go
136 lines
3.8 KiB
Go
/*
|
|
Copyright 2021 Erigon contributors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package downloader
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
"github.com/ledgerwatch/erigon-lib/gointerfaces"
|
|
proto_downloader "github.com/ledgerwatch/erigon-lib/gointerfaces/downloader"
|
|
prototypes "github.com/ledgerwatch/erigon-lib/gointerfaces/types"
|
|
"github.com/ledgerwatch/log/v3"
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
|
)
|
|
|
|
var (
|
|
_ proto_downloader.DownloaderServer = &GrpcServer{}
|
|
)
|
|
|
|
func NewGrpcServer(d *Downloader) (*GrpcServer, error) {
|
|
return &GrpcServer{d: d}, nil
|
|
}
|
|
|
|
type GrpcServer struct {
|
|
proto_downloader.UnimplementedDownloaderServer
|
|
d *Downloader
|
|
}
|
|
|
|
// Download - create new .torrent ONLY if initialSync, everything else Erigon can generate by itself
|
|
func (s *GrpcServer) Download(ctx context.Context, request *proto_downloader.DownloadRequest) (*emptypb.Empty, error) {
|
|
defer s.d.ReCalcStats(10 * time.Second) // immediately call ReCalc to set stat.Complete flag
|
|
logEvery := time.NewTicker(20 * time.Second)
|
|
defer logEvery.Stop()
|
|
|
|
for i, it := range request.Items {
|
|
if it.Path == "" {
|
|
return nil, fmt.Errorf("field 'path' is required")
|
|
}
|
|
|
|
select {
|
|
case <-logEvery.C:
|
|
log.Info("[snapshots] initializing", "files", fmt.Sprintf("%d/%d", i, len(request.Items)))
|
|
default:
|
|
}
|
|
|
|
if it.TorrentHash == nil {
|
|
// if we don't have the torrent hash then we seed a new snapshot
|
|
if err := s.d.AddNewSeedableFile(ctx, it.Path); err != nil {
|
|
return nil, err
|
|
}
|
|
continue
|
|
}
|
|
|
|
if err := s.d.AddInfoHashAsMagnetLink(ctx, Proto2InfoHash(it.TorrentHash), it.Path); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return &emptypb.Empty{}, nil
|
|
}
|
|
|
|
// Delete - stop seeding, remove file, remove .torrent
|
|
func (s *GrpcServer) Delete(ctx context.Context, request *proto_downloader.DeleteRequest) (*emptypb.Empty, error) {
|
|
defer s.d.ReCalcStats(10 * time.Second) // immediately call ReCalc to set stat.Complete flag
|
|
torrents := s.d.torrentClient.Torrents()
|
|
for _, name := range request.Paths {
|
|
if name == "" {
|
|
return nil, fmt.Errorf("field 'path' is required")
|
|
}
|
|
for _, t := range torrents {
|
|
select {
|
|
case <-t.GotInfo():
|
|
continue
|
|
default:
|
|
}
|
|
if t.Name() == name {
|
|
t.Drop()
|
|
break
|
|
}
|
|
}
|
|
|
|
fPath := filepath.Join(s.d.SnapDir(), name)
|
|
_ = os.Remove(fPath)
|
|
_ = os.Remove(fPath + ".torrent")
|
|
}
|
|
return &emptypb.Empty{}, nil
|
|
}
|
|
|
|
func (s *GrpcServer) Verify(ctx context.Context, request *proto_downloader.VerifyRequest) (*emptypb.Empty, error) {
|
|
err := s.d.VerifyData(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &emptypb.Empty{}, nil
|
|
}
|
|
|
|
func (s *GrpcServer) Stats(ctx context.Context, request *proto_downloader.StatsRequest) (*proto_downloader.StatsReply, error) {
|
|
stats := s.d.Stats()
|
|
return &proto_downloader.StatsReply{
|
|
MetadataReady: stats.MetadataReady,
|
|
FilesTotal: stats.FilesTotal,
|
|
|
|
Completed: stats.Completed,
|
|
Progress: stats.Progress,
|
|
|
|
PeersUnique: stats.PeersUnique,
|
|
ConnectionsTotal: stats.ConnectionsTotal,
|
|
|
|
BytesCompleted: stats.BytesCompleted,
|
|
BytesTotal: stats.BytesTotal,
|
|
UploadRate: stats.UploadRate,
|
|
DownloadRate: stats.DownloadRate,
|
|
}, nil
|
|
}
|
|
|
|
func Proto2InfoHash(in *prototypes.H160) metainfo.Hash {
|
|
return gointerfaces.ConvertH160toAddress(in)
|
|
}
|