Skip to content

Commit 3275101

Browse files
authored
Merge pull request #1537 from 0chain/fix/update-trie
check file meta hash before update
2 parents 74d915d + 4bb6093 commit 3275101

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

code/go/0chain.net/blobbercore/allocation/file_changer_update.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,16 @@ func (nf *UpdateFileChanger) ApplyChangeV2(ctx context.Context, allocationRoot,
121121

122122
//find if ref exists
123123
var refResult struct {
124-
ID int64
125-
Type string
126-
NumUpdates int64 `gorm:"column:num_of_updates" json:"num_of_updates"`
127-
Size int64 `gorm:"column:size" json:"size"`
124+
ID int64
125+
Type string
126+
NumUpdates int64 `gorm:"column:num_of_updates" json:"num_of_updates"`
127+
Size int64 `gorm:"column:size" json:"size"`
128+
FileMetaHash string `gorm:"column:file_meta_hash" json:"file_meta_hash"`
128129
}
129130

130131
err := datastore.GetStore().WithNewTransaction(func(ctx context.Context) error {
131132
tx := datastore.GetStore().GetTransaction(ctx)
132-
return tx.Model(&reference.Ref{}).Select("id", "type", "num_of_updates", "size").Where("lookup_hash = ?", nf.LookupHash).Take(&refResult).Error
133+
return tx.Model(&reference.Ref{}).Select("id", "type", "num_of_updates", "size", "file_meta_hash").Where("lookup_hash = ?", nf.LookupHash).Take(&refResult).Error
133134
}, &sql.TxOptions{
134135
ReadOnly: true,
135136
})
@@ -187,9 +188,11 @@ func (nf *UpdateFileChanger) ApplyChangeV2(ctx context.Context, allocationRoot,
187188
}
188189
collector.DeleteRefRecord(deleteRecord)
189190
collector.CreateRefRecord(newFile)
190-
decodedKey, _ := hex.DecodeString(newFile.LookupHash)
191-
decodedValue, _ := hex.DecodeString(newFile.FileMetaHash)
192-
err = trie.Update(decodedKey, decodedValue, uint64(newFile.NumBlocks))
191+
if newFile.FileMetaHash != refResult.FileMetaHash {
192+
decodedKey, _ := hex.DecodeString(newFile.LookupHash)
193+
decodedValue, _ := hex.DecodeString(newFile.FileMetaHash)
194+
err = trie.Update(decodedKey, decodedValue, uint64(newFile.NumBlocks))
195+
}
193196
return refResult.Size - newFile.Size, err
194197
}
195198

0 commit comments

Comments
 (0)