Skip to content

Commit

Permalink
Merge pull request planetarium#81 from longfin/feature/bump-libplanet
Browse files Browse the repository at this point in the history
Bump libplanet to 0.10.0-dev.20200921050842
  • Loading branch information
longfin authored Sep 21, 2020
2 parents 52d33a7 + c7f59d3 commit 4d4f37c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 45 deletions.
10 changes: 3 additions & 7 deletions .Lib9c.Tests/BlockPolicyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ public void DoesTransactionFollowsPolicyWithEmpty()
genesis,
renderers: new[] { blockPolicySource.BlockRenderer }
);
blockPolicySource.ActivatedAccountsStateGetter = () => blockChain.GetState(ActivatedAccountsState.Address);
blockPolicySource.UpdateActivationSet(blockPolicySource.ActivatedAccountsStateGetter());
Transaction<PolymorphicAction<ActionBase>> tx = Transaction<PolymorphicAction<ActionBase>>.Create(
0,
new PrivateKey(),
genesis.Hash,
new PolymorphicAction<ActionBase>[] { });

Assert.True(policy.DoesTransactionFollowsPolicy(tx));
Assert.True(policy.DoesTransactionFollowsPolicy(tx, blockChain));
}

[Fact]
Expand All @@ -71,8 +69,6 @@ public async Task DoesTransactionFollowsPolicy()
genesis,
renderers: new[] { blockPolicySource.BlockRenderer }
);
blockPolicySource.ActivatedAccountsStateGetter = () => blockChain.GetState(ActivatedAccountsState.Address);
blockPolicySource.UpdateActivationSet(blockPolicySource.ActivatedAccountsStateGetter());
Transaction<PolymorphicAction<ActionBase>> txByStranger =
Transaction<PolymorphicAction<ActionBase>>.Create(
0,
Expand All @@ -82,7 +78,7 @@ public async Task DoesTransactionFollowsPolicy()
);

// 새로 만든 키는 활성화 유저 리스트에 없기 때문에 차단됩니다.
Assert.False(policy.DoesTransactionFollowsPolicy(txByStranger));
Assert.False(policy.DoesTransactionFollowsPolicy(txByStranger, blockChain));

var newActivatedPrivateKey = new PrivateKey();
var newActivatedAddress = newActivatedPrivateKey.ToAddress();
Expand All @@ -104,7 +100,7 @@ public async Task DoesTransactionFollowsPolicy()
);

// 활성화 된 계정이기 때문에 테스트에 성공합니다.
Assert.True(policy.DoesTransactionFollowsPolicy(txByNewActivated));
Assert.True(policy.DoesTransactionFollowsPolicy(txByNewActivated, blockChain));
}

[Fact]
Expand Down
6 changes: 4 additions & 2 deletions Lib9c/BlockPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ Func<Block<NCAction>, InvalidBlockException> blockValidator

public IAction BlockAction => _impl.BlockAction;

public bool DoesTransactionFollowsPolicy(Transaction<NCAction> transaction)
=> _impl.DoesTransactionFollowsPolicy(transaction);
public bool DoesTransactionFollowsPolicy(
Transaction<NCAction> transaction,
BlockChain<NCAction> blockChain
) => _impl.DoesTransactionFollowsPolicy(transaction, blockChain);

public long GetNextBlockDifficulty(BlockChain<NCAction> blocks)
=> _impl.GetNextBlockDifficulty(blocks);
Expand Down
53 changes: 19 additions & 34 deletions Lib9c/BlockPolicySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,18 @@ public class BlockPolicySource

public BlockPolicySource(ILogger logger, LogEventLevel logEventLevel = LogEventLevel.Verbose)
{
BlockRenderer
.EveryBlock()
.Subscribe(_ => UpdateActivationSet());

BlockRenderer
.EveryReorg()
.Subscribe(_ => UpdateActivationSet());

LoggedActionRenderer =
new LoggedActionRenderer<NCAction>(ActionRenderer, logger, logEventLevel);

LoggedBlockRenderer =
new LoggedRenderer<NCAction>(BlockRenderer, logger, logEventLevel);
}

public Func<IValue> ActivatedAccountsStateGetter { get; set; }

public IImmutableSet<Address> ActivatedAccounts { get; private set; }

public AuthorizedMinersState AuthorizedMinersState { get; set; }

public void UpdateActivationSet(IValue state)
{
ActivatedAccounts = new ActivatedAccountsState((Dictionary)state).Accounts;
}

// FIXME 남은 설정들도 설정화 해야 할지도?
public IBlockPolicy<NCAction> GetPolicy(int minimumDifficulty)
{
ActivatedAccounts = ActivatedAccounts?.Clear();
#if UNITY_EDITOR
return new DebugPolicy();
#else
Expand All @@ -88,24 +70,27 @@ public IBlockPolicy<NCAction> GetPolicy(int minimumDifficulty)
public IEnumerable<IRenderer<NCAction>> GetRenderers() =>
new IRenderer<NCAction>[] { BlockRenderer, LoggedActionRenderer };

private bool IsSignerAuthorized(Transaction<PolymorphicAction<ActionBase>> transaction)
{
bool isActivateAccountAction =
transaction.Actions.Count == 1
&& transaction.Actions.First().InnerAction is ActivateAccount;

return isActivateAccountAction
|| ActivatedAccounts is null
|| !ActivatedAccounts.Any()
|| ActivatedAccounts.Contains(transaction.Signer);
}

private void UpdateActivationSet()
private bool IsSignerAuthorized(
Transaction<NCAction> transaction,
BlockChain<NCAction> blockChain
)
{
if (!(ActivatedAccountsStateGetter is null))
if (transaction.Actions.Count == 1 &&
transaction.Actions.First().InnerAction is ActivateAccount)
{
return true;
}

if (blockChain.GetState(ActivatedAccountsState.Address) is Dictionary asDict)
{
IImmutableSet<Address> activatedAccounts =
new ActivatedAccountsState(asDict).Accounts;
return !activatedAccounts.Any() ||
activatedAccounts.Contains(transaction.Signer);
}
else
{
IValue state = ActivatedAccountsStateGetter();
UpdateActivationSet(state);
return true;
}
}

Expand Down
3 changes: 2 additions & 1 deletion Lib9c/DebugPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public long GetNextBlockDifficulty(BlockChain<PolymorphicAction<ActionBase>> blo
}

public bool DoesTransactionFollowsPolicy(
Transaction<PolymorphicAction<ActionBase>> transaction
Transaction<PolymorphicAction<ActionBase>> transaction,
BlockChain<PolymorphicAction<ActionBase>> blockChain
) =>
true;
}
Expand Down
2 changes: 1 addition & 1 deletion Lib9c/Lib9c.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<ItemGroup>
<PackageReference Include="DecimalMath.DecimalEx" Version="1.0.1" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="Libplanet" Version="0.10.0-dev.20200917072856" />
<PackageReference Include="Libplanet" Version="0.10.0-dev.20200921050842" />
<PackageReference Include="OptimizedPriorityQueue" Version="4.2.0" />
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.1.0.13383">
Expand Down

0 comments on commit 4d4f37c

Please sign in to comment.