-
Notifications
You must be signed in to change notification settings - Fork 405
/
TxCertifierFilter.cs
95 lines (84 loc) · 3.92 KB
/
TxCertifierFilter.cs
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
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
// The Nethermind 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 Nethermind 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 Nethermind. If not, see <http://www.gnu.org/licenses/>.
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Nethermind.Abi;
using Nethermind.Consensus.AuRa.Contracts;
using Nethermind.Consensus.Transactions;
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Core.Resettables;
using Nethermind.Core.Specs;
using Nethermind.Logging;
using Nethermind.TxPool;
namespace Nethermind.Consensus.AuRa.Transactions
{
public class TxCertifierFilter : ITxFilter
{
private readonly ICertifierContract _certifierContract;
private readonly ITxFilter _notCertifiedFilter;
private readonly ISpecProvider _specProvider;
private readonly ResettableDictionary<Address, bool> _certifiedCache = new ResettableDictionary<Address, bool>(8);
private readonly ILogger _logger;
private Keccak _cachedBlock;
public TxCertifierFilter(ICertifierContract certifierContract, ITxFilter notCertifiedFilter, ISpecProvider specProvider, ILogManager logManager)
{
_certifierContract = certifierContract ?? throw new ArgumentNullException(nameof(certifierContract));
_notCertifiedFilter = notCertifiedFilter ?? throw new ArgumentNullException(nameof(notCertifiedFilter));
_specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider));
_logger = logManager?.GetClassLogger<TxCertifierFilter>() ?? throw new ArgumentNullException(nameof(logManager));
}
public AcceptTxResult IsAllowed(Transaction tx, BlockHeader parentHeader) =>
IsCertified(tx, parentHeader) ? AcceptTxResult.Accepted : _notCertifiedFilter.IsAllowed(tx, parentHeader);
private bool IsCertified(Transaction tx, BlockHeader parentHeader)
{
Address sender = tx.SenderAddress;
if (tx.IsZeroGasPrice(parentHeader, _specProvider) && sender is not null)
{
if (_logger.IsTrace) _logger.Trace($"Checking service transaction checker contract from {sender}.");
IDictionary<Address, bool> cache = GetCache(parentHeader.Hash);
if (cache.TryGetValue(sender, out bool isCertified))
{
tx.IsServiceTransaction = isCertified;
return isCertified;
}
try
{
bool isCertifiedByContract = _certifierContract.Certified(parentHeader, sender);
tx.IsServiceTransaction = isCertifiedByContract;
return cache[sender] = isCertifiedByContract;
}
catch (AbiException e)
{
if (_logger.IsError) _logger.Error($"Call to certifier contract failed on block {parentHeader.ToString(BlockHeader.Format.FullHashAndNumber)}.", e);
return false;
}
}
return false;
}
private IDictionary<Address, bool> GetCache(Keccak blockHash)
{
if (blockHash != _cachedBlock)
{
_certifiedCache.Reset();
_cachedBlock = blockHash;
}
return _certifiedCache;
}
}
}