From c6105c0d09c63667a44e17f846adb79b98af357d Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 7 Nov 2023 13:52:01 +0300 Subject: [PATCH] Properly rewrite previously added malicious conflict if it's in the storage `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 --- src/Neo/SmartContract/Native/LedgerContract.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Neo/SmartContract/Native/LedgerContract.cs b/src/Neo/SmartContract/Native/LedgerContract.cs index 0f48c5e99f..598464dd23 100644 --- a/src/Neo/SmartContract/Native/LedgerContract.cs +++ b/src/Neo/SmartContract/Native/LedgerContract.cs @@ -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()) { - 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 })); } } }