Skip to content

Commit

Permalink
str fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ucwong committed Sep 24, 2023
1 parent 01afc82 commit 05c3925
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
5 changes: 5 additions & 0 deletions backend/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ package backend

import (
"encoding/binary"
"strconv"
)

func uint64ToBytes(i uint64) []byte {
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], i)
return buf[:]
}

func uint64ToHex(i uint64) []byte {
return []byte(strconv.FormatUint(i, 16))
}
4 changes: 2 additions & 2 deletions backend/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func (fs *ChainDB) SetTorrentProgress(ih string, size uint64) (bool, uint64, err
v := buk.Get([]byte(ih))

if v == nil {
err = buk.Put([]byte(ih), []byte(strconv.FormatUint(size, 16)))
err = buk.Put([]byte(ih), uint64ToHex(size))
} else {
s, err := strconv.ParseUint(string(v), 16, 64)
if err != nil {
return err
}
if size > s {
err = buk.Put([]byte(ih), []byte(strconv.FormatUint(size, 16)))
err = buk.Put([]byte(ih), uint64ToHex(size))
} else {
size = s
}
Expand Down
8 changes: 8 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package robot

import (
"github.com/CortexFoundation/CortexTheseus/metrics"
"time"
)

Expand All @@ -25,3 +26,10 @@ const (
delay = 12 //params.Delay
timeout = 30 * time.Second
)

var (
rpcBlockMeter = metrics.NewRegisteredMeter("torrent/block/call", nil)
rpcCurrentMeter = metrics.NewRegisteredMeter("torrent/current/call", nil)
rpcUploadMeter = metrics.NewRegisteredMeter("torrent/upload/call", nil)
rpcReceiptMeter = metrics.NewRegisteredMeter("torrent/receipt/call", nil)
)
7 changes: 6 additions & 1 deletion model_srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (m *Monitor) parseBlockTorrentInfo(b *types.Block) (bool, error) {
start = mclock.Now()
final []types.Transaction
)

for _, tx := range b.Txs {
if meta := tx.Parse(); meta != nil {
log.Debug("Data encounter", "ih", meta.InfoHash, "number", b.Number, "meta", meta)
Expand Down Expand Up @@ -112,7 +113,7 @@ func (m *Monitor) parseBlockTorrentInfo(b *types.Block) (bool, error) {
file.LeftSize = remainingSize
if _, progress, err := m.fs.AddFile(file); err != nil {
return false, err
} else if progress { // && progress {
} else if progress {
log.Debug("Update storage success", "ih", file.Meta.InfoHash, "left", file.LeftSize)
var bytesRequested uint64
if file.Meta.RawSize > file.LeftSize {
Expand All @@ -134,20 +135,24 @@ func (m *Monitor) parseBlockTorrentInfo(b *types.Block) (bool, error) {
final = append(final, tx)
}
}

if len(final) > 0 && len(final) < len(b.Txs) {
log.Debug("Final txs layout", "total", len(b.Txs), "final", len(final), "num", b.Number, "txs", m.fs.Txs())
b.Txs = final
}

if record {
if err := m.fs.AddBlock(b); err == nil {
log.Info("Root has been changed", "number", b.Number, "hash", b.Hash, "root", m.fs.Root())
} else {
log.Warn("Block added failed", "number", b.Number, "hash", b.Hash, "root", m.fs.Root(), "err", err)
}
}

if len(b.Txs) > 0 {
elapsed := time.Duration(mclock.Now()) - time.Duration(start)
log.Trace("Transactions scanning", "count", len(b.Txs), "number", b.Number, "elapsed", common.PrettyDuration(elapsed))
}

return record, nil
}
12 changes: 2 additions & 10 deletions monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/CortexFoundation/CortexTheseus/common"
"github.com/CortexFoundation/CortexTheseus/common/mclock"
"github.com/CortexFoundation/CortexTheseus/log"
"github.com/CortexFoundation/CortexTheseus/metrics"
"github.com/CortexFoundation/CortexTheseus/rpc"
"github.com/CortexFoundation/robot/backend"
"github.com/CortexFoundation/torrentfs/params"
Expand All @@ -38,13 +37,6 @@ import (
"time"
)

var (
rpcBlockMeter = metrics.NewRegisteredMeter("torrent/block/call", nil)
rpcCurrentMeter = metrics.NewRegisteredMeter("torrent/current/call", nil)
rpcUploadMeter = metrics.NewRegisteredMeter("torrent/upload/call", nil)
rpcReceiptMeter = metrics.NewRegisteredMeter("torrent/receipt/call", nil)
)

// Monitor observes the data changes on the blockchain and synchronizes.
// cl for ipc/rpc communication, dl for download manager, and fs for data storage.
type Monitor struct {
Expand Down Expand Up @@ -188,7 +180,7 @@ func (m *Monitor) Callback() chan any {
return m.callback
}

func (m *Monitor) loadHistory() error {
/*func (m *Monitor) loadHistory() error {
torrents, _ := m.fs.InitTorrents()
if m.mode != params.LAZY {
for k, v := range torrents {
Expand All @@ -206,7 +198,7 @@ func (m *Monitor) loadHistory() error {
}
return nil
}
}*/

func (m *Monitor) download(ctx context.Context, k string, v uint64) error {
if m.mode != params.LAZY && m.callback != nil {
Expand Down

0 comments on commit 05c3925

Please sign in to comment.