Skip to content

Commit a9cca56

Browse files
authored
Merge pull request #2319 from planetarium/release/1.7.1
main merge release/1.7.1
2 parents eb4e882 + 84ce195 commit a9cca56

File tree

3 files changed

+72
-21
lines changed

3 files changed

+72
-21
lines changed

.Lib9c.Tests/Action/BattleArenaTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,37 @@ public void Execute_ValidateDuplicateTicketPurchaseException()
10371037
}));
10381038
}
10391039

1040+
[Theory]
1041+
[InlineData(8, null)]
1042+
[InlineData(100, null)]
1043+
[InlineData(0, typeof(ArgumentException))]
1044+
[InlineData(-1, typeof(ArgumentException))]
1045+
public void PlainValue(int ticket, Type exc)
1046+
{
1047+
BattleArena action = new BattleArena
1048+
{
1049+
myAvatarAddress = _avatar1Address,
1050+
enemyAvatarAddress = _avatar2Address,
1051+
championshipId = 1,
1052+
round = 1,
1053+
ticket = ticket,
1054+
costumes = new List<Guid>(),
1055+
equipments = new List<Guid>(),
1056+
runeInfos = new List<RuneSlotInfo>(),
1057+
};
1058+
IValue plainValue = action.PlainValue;
1059+
BattleArena des = new BattleArena();
1060+
if (exc is null)
1061+
{
1062+
des.LoadPlainValue(plainValue);
1063+
Assert.Equal(plainValue, des.PlainValue);
1064+
}
1065+
else
1066+
{
1067+
Assert.Throws(exc, () => des.LoadPlainValue(plainValue));
1068+
}
1069+
}
1070+
10401071
private static (AgentState AgentState, AvatarState AvatarState) GetAgentStateWithAvatarState(
10411072
IReadOnlyDictionary<string, string> sheets,
10421073
TableSheets tableSheets,

Lib9c.Policy/NCStagePolicy.cs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class NCStagePolicy : IStagePolicy
1515
private readonly VolatileStagePolicy _impl;
1616
private readonly ConcurrentDictionary<Address, SortedList<Transaction, TxId>> _txs;
1717
private readonly int _quotaPerSigner;
18-
private readonly Dictionary<Address, int> _quotaPerSignerList;
18+
private readonly ConcurrentDictionary<Address, int> _quotaPerSignerList;
1919
private IAccessControlService? _accessControlService;
2020

2121
public NCStagePolicy(TimeSpan txLifeTime, int quotaPerSigner, IAccessControlService? accessControlService = null)
@@ -32,7 +32,7 @@ public NCStagePolicy(TimeSpan txLifeTime, int quotaPerSigner, IAccessControlServ
3232
? new VolatileStagePolicy()
3333
: new VolatileStagePolicy(txLifeTime);
3434

35-
_quotaPerSignerList = new Dictionary<Address, int>();
35+
_quotaPerSignerList = new ConcurrentDictionary<Address, int>();
3636
_accessControlService = accessControlService;
3737
}
3838

@@ -52,7 +52,7 @@ public IEnumerable<Transaction> Iterate(BlockChain blockChain, bool filtered = t
5252
{
5353
if (filtered)
5454
{
55-
var txsPerSigner = new Dictionary<Address, SortedSet<Transaction>>();
55+
var txsPerSigner = new ConcurrentDictionary<Address, SortedSet<Transaction>>();
5656
foreach (Transaction tx in _impl.Iterate(blockChain, filtered))
5757
{
5858
if (!txsPerSigner.TryGetValue(tx.Signer, out var s))
@@ -88,29 +88,39 @@ public IEnumerable<Transaction> Iterate(BlockChain blockChain, bool filtered = t
8888

8989
public bool Stage(BlockChain blockChain, Transaction transaction)
9090
{
91-
if (_accessControlService?.GetTxQuota(transaction.Signer) is { } acsTxQuota)
91+
try
9292
{
93-
_quotaPerSignerList[transaction.Signer] = acsTxQuota;
93+
if (_accessControlService?.GetTxQuota(transaction.Signer) is { } acsTxQuota)
94+
{
95+
_quotaPerSignerList[transaction.Signer] = acsTxQuota;
96+
97+
if (acsTxQuota == 0)
98+
{
99+
return false;
100+
}
101+
}
94102

95-
if (acsTxQuota == 0)
103+
var deniedTxs = new[]
104+
{
105+
// CreatePledge Transaction with 50000 addresses
106+
TxId.FromHex(
107+
"300826da62b595d8cd663dadf04995a7411534d1cdc17dac75ce88754472f774"),
108+
// CreatePledge Transaction with 5000 addresses
109+
TxId.FromHex(
110+
"210d1374d8f068de657de6b991e63888da9cadbc68e505ac917b35568b5340f8"),
111+
};
112+
if (deniedTxs.Contains(transaction.Id))
96113
{
97114
return false;
98115
}
99-
}
100116

101-
var deniedTxs = new[]
102-
{
103-
// CreatePledge Transaction with 50000 addresses
104-
TxId.FromHex("300826da62b595d8cd663dadf04995a7411534d1cdc17dac75ce88754472f774"),
105-
// CreatePledge Transaction with 5000 addresses
106-
TxId.FromHex("210d1374d8f068de657de6b991e63888da9cadbc68e505ac917b35568b5340f8"),
107-
};
108-
if (deniedTxs.Contains(transaction.Id))
117+
return _impl.Stage(blockChain, transaction);
118+
}
119+
catch (Exception ex)
109120
{
110-
return false;
121+
Console.WriteLine("[NCStagePolicy-ACS] {0} {1}", ex.Message, ex.StackTrace);
122+
return _impl.Stage(blockChain, transaction);
111123
}
112-
113-
return _impl.Stage(blockChain, transaction);
114124
}
115125

116126
public bool Unstage(BlockChain blockChain, TxId id)

Lib9c/Action/BattleArena.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,16 @@ protected override void LoadPlainValueInternal(
8383
championshipId = plainValue[ChampionshipIdKey].ToInteger();
8484
round = plainValue[RoundKey].ToInteger();
8585
ticket = plainValue[TicketKey].ToInteger();
86-
costumes = ((List)plainValue[CostumesKey]).Select(e => e.ToGuid()).ToList();
87-
equipments = ((List)plainValue[EquipmentsKey]).Select(e => e.ToGuid()).ToList();
88-
runeInfos = plainValue[RuneInfos].ToList(x => new RuneSlotInfo((List)x));
86+
costumes = ((List) plainValue[CostumesKey]).Select(e => e.ToGuid()).ToList();
87+
equipments = ((List) plainValue[EquipmentsKey]).Select(e => e.ToGuid()).ToList();
88+
runeInfos = plainValue[RuneInfos].ToList(x => new RuneSlotInfo((List) x));
89+
ValidateTicket();
8990
}
9091

9192
public override IAccount Execute(IActionContext context)
9293
{
9394
context.UseGas(1);
95+
ValidateTicket();
9496
var states = context.PreviousState;
9597
var addressesHex = GetSignerAndOtherAddressesHex(
9698
context,
@@ -446,5 +448,13 @@ public override IAccount Execute(IActionContext context)
446448
myAvatarAddress.Derive(LegacyInventoryKey),
447449
avatarState.inventory.Serialize());
448450
}
451+
452+
private void ValidateTicket()
453+
{
454+
if (ticket <= 0)
455+
{
456+
throw new ArgumentException("ticket must be greater than 0");
457+
}
458+
}
449459
}
450460
}

0 commit comments

Comments
 (0)