mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-25 13:07:17 +00:00
0be3044b7e
* rename * rename "make grpc" * rename "abi bindings templates" * rename "abi bindings templates"
81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
package snapshotsync
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/anacrolix/torrent"
|
|
"github.com/golang/protobuf/ptypes/empty"
|
|
"github.com/ledgerwatch/erigon/common/dbutils"
|
|
"github.com/ledgerwatch/erigon/ethdb"
|
|
)
|
|
|
|
var (
|
|
ErrNotSupportedNetworkID = errors.New("not supported network id")
|
|
ErrNotSupportedSnapshot = errors.New("not supported snapshot for this network id")
|
|
)
|
|
var (
|
|
_ DownloaderServer = &SNDownloaderServer{}
|
|
)
|
|
|
|
func NewServer(dir string, seeding bool) (*SNDownloaderServer, error) {
|
|
db := ethdb.MustOpen(dir + "/db")
|
|
peerID, err := db.Get(dbutils.BittorrentInfoBucket, []byte(dbutils.BittorrentPeerID))
|
|
if err != nil && !errors.Is(err, ethdb.ErrKeyNotFound) {
|
|
return nil, fmt.Errorf("get peer id: %w", err)
|
|
}
|
|
downloader, err := New(dir, seeding, string(peerID))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(peerID) == 0 {
|
|
err = downloader.SavePeerID(db)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("save peer id: %w", err)
|
|
}
|
|
}
|
|
return &SNDownloaderServer{
|
|
t: downloader,
|
|
db: db,
|
|
}, nil
|
|
}
|
|
|
|
type SNDownloaderServer struct {
|
|
DownloaderServer
|
|
t *Client
|
|
db ethdb.Database
|
|
}
|
|
|
|
func (S *SNDownloaderServer) Download(ctx context.Context, request *DownloadSnapshotRequest) (*empty.Empty, error) {
|
|
err := S.t.AddSnapshotsTorrents(ctx, S.db, request.NetworkId, FromSnapshotTypes(request.Type))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &empty.Empty{}, nil
|
|
}
|
|
func (S *SNDownloaderServer) Load() error {
|
|
return S.t.Load(S.db)
|
|
}
|
|
|
|
func (S *SNDownloaderServer) Snapshots(ctx context.Context, request *SnapshotsRequest) (*SnapshotsInfoReply, error) {
|
|
reply := SnapshotsInfoReply{}
|
|
resp, err := S.t.GetSnapshots(S.db, request.NetworkId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for i := range resp {
|
|
reply.Info = append(reply.Info, resp[i])
|
|
}
|
|
return &reply, nil
|
|
}
|
|
|
|
func (S *SNDownloaderServer) Stats(ctx context.Context) map[string]torrent.TorrentStats {
|
|
stats := map[string]torrent.TorrentStats{}
|
|
torrents := S.t.Cli.Torrents()
|
|
for _, t := range torrents {
|
|
stats[t.Name()] = t.Stats()
|
|
}
|
|
return stats
|
|
}
|