public void PrisonOperations() { var p = new Prison(); var id1 = BitcoinFactory.CreateUint256(); var utxo = BitcoinFactory.CreateOutPoint(); p.Punish(utxo, Punishment.Noted, id1); Assert.Single(p.GetInmates()); Assert.Equal(1, p.CountInmates().noted); Assert.Equal(0, p.CountInmates().banned); Assert.True(p.TryGet(utxo, out _)); // Updates to banned. p.Punish(utxo, Punishment.Banned, id1); Assert.Single(p.GetInmates()); Assert.Equal(0, p.CountInmates().noted); Assert.Equal(1, p.CountInmates().banned); Assert.True(p.TryGet(utxo, out _)); // Removes. Assert.True(p.TryRelease(utxo, out _)); Assert.Empty(p.GetInmates()); // Noting twice flips to banned. p.Punish(utxo, Punishment.Noted, id1); p.Punish(utxo, Punishment.Noted, id1); Assert.Single(p.GetInmates()); Assert.Equal(0, p.CountInmates().noted); Assert.Equal(1, p.CountInmates().banned); Assert.True(p.TryGet(utxo, out _)); Assert.True(p.TryRelease(utxo, out _)); // Updates round. var id2 = BitcoinFactory.CreateUint256(); p.Punish(utxo, Punishment.Banned, id1); p.Punish(utxo, Punishment.Banned, id2); Assert.Single(p.GetInmates()); Assert.True(p.TryGet(utxo, out var inmate)); Assert.Equal(id2, inmate !.LastDisruptedRoundId); Assert.True(p.TryRelease(utxo, out _)); }
public void PrisonChangeTracking() { var p = new Prison(); var currentChangeId = p.ChangeId; // Make sure we set them to the past so the release method that looks at the time evaluates to true. var past = DateTimeOffset.UtcNow - TimeSpan.FromMilliseconds(2); var id1 = BitcoinFactory.CreateUint256(); p.Punish(new Inmate(BitcoinFactory.CreateOutPoint(), Punishment.Banned, past, id1)); Assert.NotEqual(currentChangeId, p.ChangeId); currentChangeId = p.ChangeId; p.Punish(new Inmate(BitcoinFactory.CreateOutPoint(), Punishment.Noted, past, id1)); Assert.NotEqual(currentChangeId, p.ChangeId); currentChangeId = p.ChangeId; var op = BitcoinFactory.CreateOutPoint(); var id2 = BitcoinFactory.CreateUint256(); p.Punish(new Inmate(op, Punishment.Noted, past, id2)); Assert.NotEqual(currentChangeId, p.ChangeId); currentChangeId = p.ChangeId; p.Punish(new Inmate(op, Punishment.Noted, past, id1)); Assert.NotEqual(currentChangeId, p.ChangeId); currentChangeId = p.ChangeId; Assert.True(p.TryRelease(op, out _)); Assert.NotEqual(currentChangeId, p.ChangeId); currentChangeId = p.ChangeId; p.ReleaseEligibleInmates(TimeSpan.FromMilliseconds(1)); Assert.NotEqual(currentChangeId, p.ChangeId); }