Skip to content

Commit

Permalink
Properly rewrite previously added malicious conflict if it's in the s…
Browse files Browse the repository at this point in the history
…torage

`engine.Snapshot.Add` doesn't allow to rewrite storage entity if it's already exist.
Thus, we firstly need to remove it and afterwards to add the updated entity.

Signed-off-by: Anna Shaleva <[email protected]>
  • Loading branch information
AnnaShaleva committed Nov 7, 2023
1 parent 2350393 commit c6105c0
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Neo/SmartContract/Native/LedgerContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,23 @@ internal override ContractTask OnPersist(ApplicationEngine engine)
foreach (TransactionState tx in transactions)
{
// It's possible that there are previously saved malicious conflict records for this transaction.
// These records are garbage in fact and can be removed in a separate flow, but at the same time
// it won't hurt to keep them.

engine.Snapshot.Add(CreateStorageKey(Prefix_Transaction).Add(tx.Transaction.Hash), new StorageItem(tx));
// If so, then remove it and store the relevant transaction itself.
var txKey = CreateStorageKey(Prefix_Transaction).Add(tx.Transaction.Hash);
engine.Snapshot.Delete(txKey);
engine.Snapshot.Add(txKey, new StorageItem(tx));

// Store transaction's conflicits.
var conflictingSigners = tx.Transaction.Signers.Select(s => s.Account);
foreach (var attr in tx.Transaction.GetAttributes<Conflicts>())
{
engine.Snapshot.Add(CreateStorageKey(Prefix_Transaction).Add(attr.Hash), new StorageItem(new TransactionState() { BlockIndex = engine.PersistingBlock.Index }));
var recordKey = CreateStorageKey(Prefix_Transaction).Add(attr.Hash);
engine.Snapshot.Delete(recordKey);
engine.Snapshot.Add(recordKey, new StorageItem(new TransactionState() { BlockIndex = engine.PersistingBlock.Index }));
foreach (var signer in conflictingSigners)
{
engine.Snapshot.Add(CreateStorageKey(Prefix_Transaction).Add(attr.Hash).Add(signer), new StorageItem(new TransactionState() { BlockIndex = engine.PersistingBlock.Index }));
var conflictKey = CreateStorageKey(Prefix_Transaction).Add(attr.Hash).Add(signer);
engine.Snapshot.Delete(conflictKey);
engine.Snapshot.Add(conflictKey, new StorageItem(new TransactionState() { BlockIndex = engine.PersistingBlock.Index }));
}
}
}
Expand Down

0 comments on commit c6105c0

Please sign in to comment.