-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel_srv.go
More file actions
196 lines (165 loc) · 6.08 KB
/
model_srv.go
File metadata and controls
196 lines (165 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2023 The CortexTheseus Authors
// This file is part of the CortexTheseus library.
//
// The CortexTheseus library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The CortexTheseus library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the CortexTheseus library. If not, see <http://www.gnu.org/licenses/>.
package robot
import (
"context"
"errors"
"time"
"github.com/CortexFoundation/CortexTheseus/common"
"github.com/CortexFoundation/CortexTheseus/common/mclock"
"github.com/CortexFoundation/CortexTheseus/log"
"github.com/CortexFoundation/torrentfs/params"
"github.com/CortexFoundation/torrentfs/types"
)
func (m *Monitor) parseFileMeta(tx *types.Transaction, meta *types.FileMeta, b *types.Block) error {
log.Debug("Parsing FileMeta", "infoHash", meta.InfoHash)
// Step 1: Get transaction receipt
receipt, err := m.getReceipt(tx.Hash.String())
if err != nil {
log.Error("Failed to get receipt", "txHash", tx.Hash.String(), "err", err)
return err
}
// Step 2: Validate receipt
if receipt.ContractAddr == nil {
// More descriptive error message is better
err = errors.New("contract address is nil")
log.Warn("Contract address is nil, unable to proceed", "txHash", tx.Hash.String())
return err
}
if receipt.Status != 1 {
log.Warn("Transaction receipt status is not successful", "txHash", tx.Hash.String(), "status", receipt.Status)
return nil // Return nil for unsuccessful transactions as it's a valid state
}
log.Debug("Transaction receipt OK", "address", receipt.ContractAddr.String(), "gas", receipt.GasUsed)
// Step 3: Create and add file information
info := m.fs.NewFileInfo(meta)
info.LeftSize = meta.RawSize
info.ContractAddr = receipt.ContractAddr
info.Relate = append(info.Relate, *info.ContractAddr)
op, update, err := m.fs.AddFile(info)
if err != nil {
log.Warn("Failed to add file to filesystem", "infoHash", meta.InfoHash, "err", err)
return err
}
// Step 4: Handle file download if it's a new file
if update && op == 1 {
log.Debug("New file created, initiating download", "infoHash", meta.InfoHash)
sizeLimit := uint64(0)
if m.mode == params.FULL {
sizeLimit = 512 * 1024 // Set a specific size limit for full mode
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return m.download(ctx, meta.InfoHash, sizeLimit)
}
return nil
}
func (m *Monitor) parseBlockTorrentInfo(b *types.Block) (bool, error) {
start := mclock.Now()
var (
record bool
final []types.Transaction
)
for _, tx := range b.Txs {
// Attempt to parse transaction for file metadata
if meta := tx.Parse(); meta != nil {
log.Debug("Data encounter", "infoHash", meta.InfoHash, "blockNumber", b.Number)
if err := m.parseFileMeta(&tx, meta, b); err != nil {
log.Error("Parse file meta failed", "error", err, "blockNumber", b.Number, "txHash", tx.Hash)
return false, err
}
record = true
final = append(final, tx)
continue
}
// Handle flow control transactions
if tx.IsFlowControl() {
// Use guard clauses to reduce nesting
if tx.Recipient == nil {
continue
}
file := m.fs.GetFileByAddr(*tx.Recipient)
if file == nil {
continue
}
receipt, err := m.getReceipt(tx.Hash.String())
if err != nil {
log.Error("Failed to get receipt for flow control tx", "error", err, "txHash", tx.Hash)
return false, err
}
if receipt.Status != 1 || receipt.GasUsed != params.UploadGas {
continue
}
// All checks passed, process the flow control transaction
remainingSize, err := m.getRemainingSize((*tx.Recipient).String())
if err != nil {
log.Error("Get remaining size failed", "error", err, "addr", (*tx.Recipient).String())
return false, err
}
if file.LeftSize > remainingSize {
file.LeftSize = remainingSize
_, progress, err := m.fs.AddFile(file)
if err != nil {
return false, err
}
if progress {
bytesRequested := uint64(0)
if file.Meta.RawSize > file.LeftSize {
bytesRequested = file.Meta.RawSize - file.LeftSize
}
logMsg := "Data processing..."
if file.LeftSize == 0 {
logMsg = "Data processing completed!"
}
log.Debug(logMsg,
"infoHash", file.Meta.InfoHash,
"addr", (*tx.Recipient).String(),
"remain", common.StorageSize(remainingSize),
"request", common.StorageSize(bytesRequested),
"raw", common.StorageSize(file.Meta.RawSize),
"blockNumber", b.Number)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
if err := m.download(ctx, file.Meta.InfoHash, bytesRequested); err != nil {
cancel() // Ensure cancel is called on error
return false, err
}
cancel() // Call cancel when download is successful
}
}
record = true
final = append(final, tx)
}
}
// Update block transactions if necessary
if len(final) > 0 && len(final) < len(b.Txs) {
log.Debug("Txs filtered", "total", len(b.Txs), "final", len(final), "blockNumber", b.Number)
b.Txs = final
}
// Add block to filesystem if any relevant transactions were found
if record {
if err := m.fs.AddBlock(b); err != nil {
log.Warn("Block added failed", "blockNumber", b.Number, "blockHash", b.Hash, "root", m.fs.Root(), "error", err)
return false, err // Return the error if adding the block fails
}
log.Debug("Block added to filesystem", "blockNumber", b.Number, "blockHash", b.Hash, "root", m.fs.Root())
}
// Log transaction scanning time
if len(b.Txs) > 0 {
elapsed := time.Duration(mclock.Now()) - time.Duration(start)
log.Trace("Transaction scanning complete", "count", len(b.Txs), "blockNumber", b.Number, "elapsed", common.PrettyDuration(elapsed))
}
return record, nil
}