Skip to content

Commit

Permalink
Merge pull request #252 from CortexFoundation/dev
Browse files Browse the repository at this point in the history
Dev 2 Master branch
  • Loading branch information
ucwong authored Nov 25, 2019
2 parents 5865575 + b524246 commit 14fc0a3
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 91 deletions.
27 changes: 14 additions & 13 deletions solution/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,37 @@ PLUGINS_DIR = $(shell pwd)/plugins
#CORTEXPATH = $(shell pwd)/../build/_workspace

all:
build/env.sh go get -tags remote -v ./...
#build/env.sh go get -tags remote -v ./...
$(MAKE) -C ${LIB_CUCKOO_DIR}
build/env.sh go build -buildmode=plugin -o ${PLUGINS_DIR}/cuda_helper.so ./miner/libcuckoo/cuda_helper.go
#build/env.sh go build -buildmode=plugin -o ${PLUGINS_DIR}/opencl_helper.so ./miner/libcuckoo/opencl_helper.go
build/env.sh go build -buildmode=plugin -o ${PLUGINS_DIR}/cpu_helper.so ./miner/libcuckoo/cpu_helper.go
build/env.sh go build -o build/bin/cortex_miner ./cmd/miner
#build/env.sh go build -o build/bin/cortex_miner ./cmd/miner

cuda-miner:
build/env.sh go get -tags remote -v ./...
#build/env.sh go get -tags remote -v ./...
$(MAKE) -C ${LIB_CUCKOO_DIR} cuda
build/env.sh go build -buildmode=plugin -o ${PLUGINS_DIR}/cuda_helper.so ./miner/libcuckoo/cuda_helper.go
build/env.sh go build -o build/bin/cortex_miner ./cmd/miner
#build/env.sh go build -o build/bin/cortex_miner ./cmd/miner
@echo "Done building."

opencl-miner:
build/env.sh go get -tags remote -v ./...
$(MAKE) -C ${LIB_CUCKOO_DIR} opencl
build/env.sh go build -buildmode=plugin -o ${PLUGINS_DIR}/opencl_helper.so ./miner/libcuckoo/opencl_helper.go
build/env.sh go build -o build/bin/cortex_miner ./cmd/miner
@echo "Done building."
#opencl-miner:
# build/env.sh go get -tags remote -v ./...
# $(MAKE) -C ${LIB_CUCKOO_DIR} opencl
# build/env.sh go build -buildmode=plugin -o ${PLUGINS_DIR}/opencl_helper.so ./miner/libcuckoo/opencl_helper.go
#build/env.sh go build -o build/bin/cortex_miner ./cmd/miner
# @echo "Done building."

cpu-miner:
build/env.sh go get -tags remote -v ./...
#build/env.sh go get -tags remote -v ./...
$(MAKE) -C ${LIB_CUCKOO_DIR} cpu
build/env.sh go build -buildmode=plugin -o ${PLUGINS_DIR}/cpu_helper.so ./miner/libcuckoo/cpu_helper.go
build/env.sh go build -o build/bin/cortex_miner ./cmd/miner
#build/env.sh go build -o build/bin/cortex_miner ./cmd/miner
@echo "Done building."

clean:
rm -rf miner/libcuckoo/*.o miner/libcuckoo/*.a
$(MAKE) -C ${LIB_CUCKOO_DIR} clean
#rm -rf miner/libcuckoo/*.o miner/libcuckoo/*.a
rm -fr build/_workspace/pkg $(CBIN)/* $(PLUGINS_DIR)/*
build/env.sh go clean -cache

32 changes: 16 additions & 16 deletions torrentfs/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,9 @@ func (m *Monitor) parseBlockTorrentInfo(b *Block) error {
}
}
elapsed := time.Duration(mclock.Now()) - time.Duration(start)
log.Debug("Transactions scanning", "count", len(b.Txs), "number", b.Number, "elapsed", common.PrettyDuration(elapsed))
if len(b.Txs) > 0 {
log.Debug("Transactions scanning", "count", len(b.Txs), "number", b.Number, "elapsed", common.PrettyDuration(elapsed))
}
}

return nil
Expand Down Expand Up @@ -729,24 +731,20 @@ if m.lastNumber > 256 {
func (m *Monitor) listenLatestBlock() {
defer m.wg.Done()
timer := time.NewTimer(time.Second * defaultTimerInterval)
//progress := uint64(0)
progress := uint64(0)
for {
select {
case <-timer.C:
m.syncLastBlock()
progress = m.syncLastBlock()
// Aviod sync in full mode, fresh interval may be less.
/*if progress > batch {
timer.Reset(time.Millisecond * 100)
} else if progress > batch/2 {
timer.Reset(time.Millisecond * 500)
} else if progress > batch/4 {
timer.Reset(time.Millisecond * 1000)
if progress > batch {
timer.Reset(time.Millisecond * 10)
} else if progress > 6 {
timer.Reset(time.Millisecond * 2000)
timer.Reset(time.Millisecond * 1000)
} else {
timer.Reset(time.Millisecond * 5000)
}*/
timer.Reset(time.Second * defaultTimerInterval)
timer.Reset(time.Millisecond * 2000)
}
//timer.Reset(time.Second * defaultTimerInterval)
case <-m.exitCh:
log.Info("Block listener stopped")
return
Expand Down Expand Up @@ -880,7 +878,7 @@ func (m *Monitor) syncLastBlock() uint64 {
if minNumber > 5 {
minNumber = minNumber - 5
}
log.Info("Torrent scanning ... ...", "from", minNumber, "to", maxNumber, "current", uint64(currentNumber), "range", uint64(maxNumber-minNumber), "behind", uint64(currentNumber)-maxNumber, "progress", float64(maxNumber)/float64(currentNumber))
log.Debug("Torrent scanning ... ...", "from", minNumber, "to", maxNumber, "current", uint64(currentNumber), "range", uint64(maxNumber-minNumber), "behind", uint64(currentNumber)-maxNumber, "progress", float64(maxNumber)/float64(currentNumber))
} else {
return 0
}
Expand All @@ -900,11 +898,13 @@ func (m *Monitor) syncLastBlock() uint64 {
return 0
}
//m.taskCh <- rpcBlock
m.deal(rpcBlock)
if err := m.deal(rpcBlock); err != nil {
return 0
}
}
elapsed := time.Duration(mclock.Now()) - time.Duration(start)
m.lastNumber = maxNumber
log.Info("Torrent scan finished", "from", minNumber, "to", maxNumber, "current", uint64(currentNumber), "progress", float64(maxNumber)/float64(currentNumber), "last", m.lastNumber, "elasped", elapsed)
log.Info("Torrent scan finished", "from", minNumber, "to", maxNumber, "range", uint64(maxNumber-minNumber), "current", uint64(currentNumber), "progress", float64(maxNumber)/float64(currentNumber), "last", m.lastNumber, "elasped", elapsed, "bps", float64(maxNumber-minNumber)*1000*1000*1000/float64(elapsed))
return uint64(maxNumber - minNumber)
}

Expand Down
118 changes: 56 additions & 62 deletions torrentfs/torrentClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/anacrolix/torrent/metainfo"
"github.com/anacrolix/torrent/mmap_span"
"github.com/anacrolix/torrent/storage"
//lru "github.com/hashicorp/golang-lru"
//"github.com/anacrolix/dht/v2"
)

Expand Down Expand Up @@ -111,14 +112,12 @@ func (t *Torrent) ReloadFile(files []string, datas [][]byte, tm *TorrentManager)
/*for _, tracker := range tm.trackers {
spec.Trackers = append(spec.Trackers, tracker)
}*/
torrent, _, err := tm.client.AddTorrentSpec(spec)
if err != nil {
return
if torrent, _, err := tm.client.AddTorrentSpec(spec); err == nil {
<-torrent.GotInfo()
torrent.VerifyData()
t.Torrent = torrent
t.Pause()
}
<-torrent.GotInfo()
torrent.VerifyData()
t.Torrent = torrent
t.Pause()
}

func (t *Torrent) ReloadTorrent(data []byte, tm *TorrentManager) {
Expand Down Expand Up @@ -146,14 +145,12 @@ func (t *Torrent) ReloadTorrent(data []byte, tm *TorrentManager) {
spec.Trackers = append(spec.Trackers, tracker)
}*/
spec.Trackers = append(spec.Trackers, tm.trackers...)
torrent, _, err := tm.client.AddTorrentSpec(spec)
if err != nil {
return
if torrent, _, err := tm.client.AddTorrentSpec(spec); err == nil {
<-torrent.GotInfo()
torrent.VerifyData()
t.Torrent = torrent
t.Pause()
}
<-torrent.GotInfo()
torrent.VerifyData()
t.Torrent = torrent
t.Pause()
}

/*func (t *Torrent) GetFile(subpath string) ([]byte, error) {
Expand Down Expand Up @@ -181,32 +178,30 @@ func (t *Torrent) IsAvailable() bool {
return false
}

func (t *Torrent) HasTorrent() bool {
return t.status != torrentPending
}
//func (t *Torrent) HasTorrent() bool {
// return t.status != torrentPending
//}

func (t *Torrent) WriteTorrent() {
f, err := os.Create(path.Join(t.filepath, "torrent"))
if err != nil {
return
}
defer f.Close()
log.Debug("Write torrent file", "path", t.filepath)
if err := t.Metainfo().Write(f); err != nil {
log.Error("Error while write torrent file", "error", err)
return
}
if f, err := os.Create(path.Join(t.filepath, "torrent")); err == nil {
defer f.Close()
log.Debug("Write torrent file", "path", t.filepath)
if err := t.Metainfo().Write(f); err != nil {
log.Error("Error while write torrent file", "error", err)
return
}

t.Pause()
t.Pause()
}
}

func (t *Torrent) SeedInQueue() {
t.status = torrentSeedingInQueue
if t.currentConns != 0 {
t.currentConns = 0
t.Torrent.SetMaxEstablishedConns(0)
}
t.Torrent.CancelPieces(0, t.Torrent.NumPieces())
t.status = torrentSeedingInQueue
}

func (t *Torrent) BoostOff() {
Expand All @@ -217,14 +212,13 @@ func (t *Torrent) Seed() {
if t.status == torrentSeeding {
return
}

t.status = torrentSeeding
if t.currentConns == 0 {
t.currentConns = t.maxEstablishedConns
t.Torrent.SetMaxEstablishedConns(t.currentConns)
}

t.Torrent.DownloadAll()
t.status = torrentSeeding
log.Info("Download success, seeding(s)", "hash", t.InfoHash(), "size", common.StorageSize(t.BytesCompleted()), "files", len(t.Files()), "pieces", t.Torrent.NumPieces(), "seg", len(t.Torrent.PieceStateRuns()), "cited", t.cited)
}

Expand All @@ -239,9 +233,9 @@ func (t *Torrent) Pause() {
t.Torrent.SetMaxEstablishedConns(0)
}
if t.status != torrentPaused {
t.status = torrentPaused
t.maxPieces = 0
t.Torrent.CancelPieces(0, t.Torrent.NumPieces())
t.status = torrentPaused
}
}

Expand Down Expand Up @@ -327,11 +321,11 @@ func (tm *TorrentManager) CreateTorrent(t *torrent.Torrent, requested int64, sta
func (tm *TorrentManager) GetTorrent(ih metainfo.Hash) *Torrent {
tm.lock.RLock()
defer tm.lock.RUnlock()
torrent, ok := tm.torrents[ih]
if !ok {
if torrent, ok := tm.torrents[ih]; !ok {
return nil
} else {
return torrent
}
return torrent
}

func (tm *TorrentManager) SetTorrent(ih metainfo.Hash, torrent *Torrent) {
Expand Down Expand Up @@ -389,6 +383,7 @@ func GetMagnetURI(infohash metainfo.Hash) string {

func (tm *TorrentManager) UpdateDynamicTrackers(trackers []string) {
tm.lock.Lock()
defer tm.lock.Unlock()
if len(tm.trackers) == 0 {
tm.trackers = append(tm.trackers, trackers)
} else if len(tm.trackers) == 1 {
Expand All @@ -401,8 +396,6 @@ func (tm *TorrentManager) UpdateDynamicTrackers(trackers []string) {
for _, t := range tm.pendingTorrents {
t.AddTrackers(newTrackers)
}

tm.lock.Unlock()
}

func (tm *TorrentManager) SetTrackers(trackers []string) {
Expand Down Expand Up @@ -496,36 +489,32 @@ func (tm *TorrentManager) AddTorrent(filePath string, BytesRequested int64) *Tor
// spec.Trackers = append(spec.Trackers, tracker)
//}
spec.Trackers = append(spec.Trackers, tm.trackers...)
t, _, err := tm.client.AddTorrentSpec(spec)
if err != nil {
return nil
if t, _, err := tm.client.AddTorrentSpec(spec); err == nil {
//var ss []string
//slices.MakeInto(&ss, mi.Nodes)
//tm.client.AddDHTNodes(ss)
<-t.GotInfo()
t.VerifyData()
torrent := tm.CreateTorrent(t, BytesRequested, torrentPending, ih)
torrent.Pause() //SeedInQueue()
return torrent
}
//var ss []string
//slices.MakeInto(&ss, mi.Nodes)
//tm.client.AddDHTNodes(ss)
<-t.GotInfo()
t.VerifyData()
torrent := tm.CreateTorrent(t, BytesRequested, torrentPending, ih)
torrent.Pause() //SeedInQueue()
return torrent
} else {
spec.Storage = storage.NewFile(TmpDir)
/*for _, tracker := range tm.trackers {
spec.Trackers = append(spec.Trackers, tracker)
}*/
spec.Trackers = append(spec.Trackers, tm.trackers...)
t, _, err := tm.client.AddTorrentSpec(spec)
if err != nil {
return nil
if t, _, err := tm.client.AddTorrentSpec(spec); err == nil {
//var ss []string
//slices.MakeInto(&ss, mi.Nodes)
//tm.client.AddDHTNodes(ss)
<-t.GotInfo()
t.VerifyData()
torrent := tm.CreateTorrent(t, BytesRequested, torrentPending, ih)
torrent.Pause()
return torrent
}
//var ss []string
//slices.MakeInto(&ss, mi.Nodes)
//tm.client.AddDHTNodes(ss)
<-t.GotInfo()
t.VerifyData()
torrent := tm.CreateTorrent(t, BytesRequested, torrentPending, ih)
torrent.Pause()
return torrent
}
return nil
}
Expand Down Expand Up @@ -847,7 +836,6 @@ func (tm *TorrentManager) listenTorrentProgress() {
path.Join(defaultTmpFilePath, t.InfoHash()),
path.Join(tm.DataDir, t.InfoHash()),
)

if err != nil {
//log.Warn("Seeding path error", "hash", t.Torrent.InfoHash(), "size", t.bytesCompleted, "miss", t.bytesMissing, "loop", log_counter)
err = os.Remove(
Expand Down Expand Up @@ -954,6 +942,7 @@ func (tm *TorrentManager) listenTorrentProgress() {

if len(activeTorrents) <= tm.maxActiveTask {
for _, t := range activeTorrents {
//log.Info("Active torrent", "hash", t.Torrent.InfoHash().String(), "request", t.bytesRequested, "complete", t.bytesCompleted)
t.Run()
active_running += 1
}
Expand All @@ -964,8 +953,13 @@ func (tm *TorrentManager) listenTorrentProgress() {
active_running += 1
}
for i := tm.maxActiveTask; i < len(activeTorrents); i++ {
activeTorrents[i].Pause()
active_paused += 1
if activeTorrents[i].bytesRequested > activeTorrents[i].bytesCompleted {
activeTorrents[i].Run()
active_running += 1
} else {
activeTorrents[i].Pause()
active_paused += 1
}
}
}

Expand Down

0 comments on commit 14fc0a3

Please sign in to comment.