Skip to content

Commit

Permalink
test: Remove tests with obsolete actions
Browse files Browse the repository at this point in the history
Co-authored-by: Chanhyuck Ko <[email protected]>
  • Loading branch information
OnedgeLee and limebell committed Oct 16, 2023
1 parent f4a733e commit c290aa9
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,39 +128,5 @@ public void ClaimStakeRewardWithBlockIndex(long blockIndex, Type expectedActionT
string type = (Text)decoded[0];
Assert.Equal(type, actionType.Name);
}

[Theory]
[InlineData(0, 0, -1)]
[InlineData(1, 9, 0)]
[InlineData(10, 10, -1)]
public void ClaimStakeRewardWithActionVersion(
int actionVersionMin,
int actionVersionMax,
int expectedCode)
{
for (var i = actionVersionMin; i < actionVersionMax + 1; i++)
{
var filePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
var addr = new PrivateKey().ToAddress();
var resultCode = _command.ClaimStakeReward(
addr.ToHex(),
filePath,
actionVersion: i);
Assert.Equal(expectedCode, resultCode);

if (expectedCode < 0)
{
continue;
}

var rawAction = Convert.FromBase64String(File.ReadAllText(filePath));
var decoded = (List)_codec.Decode(rawAction);
var plainValue = Assert.IsType<Dictionary>(decoded[1]);
var action = new ClaimStakeReward(addr);
action.LoadPlainValue(plainValue);
string type = (Text)decoded[0];
Assert.Equal(action.GetType().Name, type);
}
}
}
}
3 changes: 1 addition & 2 deletions NineChronicles.Headless.Tests/AccountStateExtensionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public void GetAvatarState(bool backward, bool inventoryExist, bool worldInforma

if (exc)
{
Assert.Throws<InvalidAddressException>(
() => AvatarModule.GetAvatarState(mockWorld, default));
Assert.Null(AvatarModule.GetAvatarState(mockWorld, default));
}
else
{
Expand Down
268 changes: 0 additions & 268 deletions NineChronicles.Headless.Tests/GraphTypes/StandaloneQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -558,100 +558,6 @@ public async Task MinerAddress()
);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task MonsterCollectionStatus_AgentState_Null(bool miner)
{
var userPrivateKey = new PrivateKey();
var userAddress = userPrivateKey.ToAddress();
var service = MakeNineChroniclesNodeService(userPrivateKey);
StandaloneContextFx.NineChroniclesNodeService = service;
StandaloneContextFx.BlockChain = service.Swarm!.BlockChain;
if (!miner)
{
StandaloneContextFx.NineChroniclesNodeService.MinerPrivateKey = null;
}
else
{
Assert.Equal(userPrivateKey, StandaloneContextFx.NineChroniclesNodeService.MinerPrivateKey!);
}
string queryArgs = miner ? "" : $@"(address: ""{userAddress}"")";
string query = $@"query {{
monsterCollectionStatus{queryArgs} {{
fungibleAssetValue {{
quantity
currency
}}
rewardInfos {{
itemId
quantity
}}
}}
}}";
var queryResult = await ExecuteQueryAsync(query);
Assert.NotNull(queryResult.Errors);
Assert.Single(queryResult.Errors!);
Assert.Equal($"{nameof(AgentState)} Address: {userAddress} is null.", queryResult.Errors!.First().Message);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task MonsterCollectionStatus_MonsterCollectionState_Null(bool miner)
{
var userPrivateKey = new PrivateKey();
var userAddress = userPrivateKey.ToAddress();
var validators = new List<PrivateKey>
{
ProposerPrivateKey, userPrivateKey
}.OrderBy(x => x.ToAddress()).ToList();
var service = MakeNineChroniclesNodeService(userPrivateKey);
StandaloneContextFx.NineChroniclesNodeService = service;
StandaloneContextFx.BlockChain = service.Swarm!.BlockChain;
if (!miner)
{
StandaloneContextFx.NineChroniclesNodeService.MinerPrivateKey = null;
}
var action = new CreateAvatar
{
index = 0,
hair = 1,
lens = 2,
ear = 3,
tail = 4,
name = "action",
};
var blockChain = StandaloneContextFx.BlockChain;
var transaction = blockChain.MakeTransaction(userPrivateKey, new ActionBase[] { action });
blockChain.StageTransaction(transaction);
Block block = blockChain.ProposeBlock(
userPrivateKey,
lastCommit: GenerateBlockCommit(BlockChain.Tip.Index, BlockChain.Tip.Hash, validators));
blockChain.Append(block, GenerateBlockCommit(block.Index, block.Hash, validators));

string queryArgs = miner ? "" : $@"(address: ""{userAddress}"")";
string query = $@"query {{
monsterCollectionStatus{queryArgs} {{
fungibleAssetValue {{
quantity
currency
}}
rewardInfos {{
itemId
quantity
}}
}}
}}";
var queryResult = await ExecuteQueryAsync(query);
Assert.NotNull(queryResult.Errors);
Assert.Single(queryResult.Errors!);
Assert.Equal(
$"{nameof(MonsterCollectionState)} Address: {MonsterCollectionState.DeriveAddress(userAddress, 0)} is null.",
queryResult.Errors!.First().Message
);
}

[Fact]
public async Task Avatar()
{
Expand Down Expand Up @@ -700,180 +606,6 @@ public async Task Avatar()
Assert.Null(queryResult.Errors);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task ActivationKeyNonce(bool trim)
{
var adminPrivateKey = new PrivateKey();
var adminAddress = adminPrivateKey.ToAddress();
var activatedAccounts = ImmutableHashSet<Address>.Empty;
var nonce = new byte[] { 0x00, 0x01, 0x02, 0x03 };
var privateKey = new PrivateKey();
(ActivationKey activationKey, PendingActivationState pendingActivation) =
ActivationKey.Create(privateKey, nonce);
var pendingActivationStates = new List<PendingActivationState>
{
pendingActivation,
};
var actionEvaluator = new ActionEvaluator(
_ => null,
new BlockChainStates(new MemoryStore(), new TrieStateStore(new MemoryKeyValueStore())),
new NCActionLoader());
Block genesis =
BlockChain.ProposeGenesisBlock(
actionEvaluator,
transactions: ImmutableList<Transaction>.Empty.Add(
Transaction.Create(0, new PrivateKey(), null,
new ActionBase[]
{
new InitializeStates(
rankingState: new RankingState0(),
shopState: new ShopState(),
gameConfigState: new GameConfigState(),
redeemCodeState: new RedeemCodeState(Bencodex.Types.Dictionary.Empty
.Add("address", RedeemCodeState.Address.Serialize())
.Add("map", Bencodex.Types.Dictionary.Empty)
),
adminAddressState: new AdminState(adminAddress, 1500000),
activatedAccountsState: new ActivatedAccountsState(activatedAccounts),
#pragma warning disable CS0618
// Use of obsolete method Currency.Legacy():
// https://github.com/planetarium/lib9c/discussions/1319
goldCurrencyState: new GoldCurrencyState(Currency.Legacy("NCG", 2, null)),
#pragma warning restore CS0618
goldDistributions: new GoldDistribution[0],
tableSheets: _sheets,
pendingActivationStates: pendingActivationStates.ToArray()
),
}.ToPlainValues()))
);

var apvPrivateKey = new PrivateKey();
var apv = AppProtocolVersion.Sign(apvPrivateKey, 0);
var userPrivateKey = new PrivateKey();
var consensusPrivateKey = new PrivateKey();
var properties = new LibplanetNodeServiceProperties
{
Host = System.Net.IPAddress.Loopback.ToString(),
AppProtocolVersion = apv,
GenesisBlock = genesis,
StorePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()),
StoreStatesCacheSize = 2,
SwarmPrivateKey = new PrivateKey(),
ConsensusPrivateKey = consensusPrivateKey,
ConsensusPort = null,
Port = null,
NoMiner = true,
Render = false,
Peers = ImmutableHashSet<BoundPeer>.Empty,
TrustedAppProtocolVersionSigners = null,
IceServers = ImmutableList<IceServer>.Empty,
ConsensusSeeds = ImmutableList<BoundPeer>.Empty,
ConsensusPeers = ImmutableList<BoundPeer>.Empty
};

var blockPolicy = NineChroniclesNodeService.GetBlockPolicy(NetworkType.Test, StaticActionLoaderSingleton.Instance);
var service = new NineChroniclesNodeService(userPrivateKey, properties, blockPolicy, NetworkType.Test, StaticActionLoaderSingleton.Instance);
StandaloneContextFx.NineChroniclesNodeService = service;
StandaloneContextFx.BlockChain = service.Swarm?.BlockChain;

var code = activationKey.Encode();
if (trim)
{
code += " ";
}
var query = $"query {{ activationKeyNonce(invitationCode: \"{code}\") }}";
var queryResult = await ExecuteQueryAsync(query);
var data = (Dictionary<string, object>)((ExecutionNode)queryResult.Data!).ToValue()!;
var result = (string)data["activationKeyNonce"];
Assert.Equal(nonce, ByteUtil.ParseHex(result));
}

[Theory]
[InlineData("1", "invitationCode format is invalid.")]
[InlineData("9330b3287bd2bbc38770c69ae7cd380350c60a1dff9ec41254f3048d5b3eb01c", "invitationCode format is invalid.")]
[InlineData("9330b3287bd2bbc38770c69ae7cd380350c60a1dff9ec41254f3048d5b3eb01c/44C889Af1e1e90213Cff5d69C9086c34ecCb60B0", "invitationCode is invalid.")]
public async Task ActivationKeyNonce_Throw_ExecutionError(string code, string msg)
{
var adminPrivateKey = new PrivateKey();
var adminAddress = adminPrivateKey.ToAddress();
var activatedAccounts = ImmutableHashSet<Address>.Empty;
var pendingActivationStates = new List<PendingActivationState>();

var actionEvaluator = new ActionEvaluator(
_ => null,
new BlockChainStates(new MemoryStore(), new TrieStateStore(new MemoryKeyValueStore())),
new NCActionLoader());
Block genesis =
BlockChain.ProposeGenesisBlock(
actionEvaluator,
transactions: ImmutableList<Transaction>.Empty
.Add(Transaction.Create(
0,
new PrivateKey(),
null,
new ActionBase[]
{
new InitializeStates(
rankingState: new RankingState0(),
shopState: new ShopState(),
gameConfigState: new GameConfigState(),
redeemCodeState: new RedeemCodeState(Bencodex.Types.Dictionary.Empty
.Add("address", RedeemCodeState.Address.Serialize())
.Add("map", Bencodex.Types.Dictionary.Empty)
),
adminAddressState: new AdminState(adminAddress, 1500000),
activatedAccountsState: new ActivatedAccountsState(activatedAccounts),
#pragma warning disable CS0618
// Use of obsolete method Currency.Legacy():
// https://github.com/planetarium/lib9c/discussions/1319
goldCurrencyState: new GoldCurrencyState(Currency.Legacy("NCG", 2, null)),
#pragma warning restore CS0618
goldDistributions: new GoldDistribution[0],
tableSheets: _sheets,
pendingActivationStates: pendingActivationStates.ToArray()
),
}.ToPlainValues()))
);

var apvPrivateKey = new PrivateKey();
var apv = AppProtocolVersion.Sign(apvPrivateKey, 0);
var userPrivateKey = new PrivateKey();
var consensusPrivateKey = new PrivateKey();
var properties = new LibplanetNodeServiceProperties
{
Host = System.Net.IPAddress.Loopback.ToString(),
AppProtocolVersion = apv,
GenesisBlock = genesis,
StorePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()),
StoreStatesCacheSize = 2,
SwarmPrivateKey = new PrivateKey(),
ConsensusPrivateKey = consensusPrivateKey,
ConsensusPort = null,
Port = null,
NoMiner = true,
Render = false,
Peers = ImmutableHashSet<BoundPeer>.Empty,
TrustedAppProtocolVersionSigners = null,
IceServers = ImmutableList<IceServer>.Empty,
ConsensusSeeds = ImmutableList<BoundPeer>.Empty,
ConsensusPeers = ImmutableList<BoundPeer>.Empty
};
var blockPolicy = NineChroniclesNodeService.GetTestBlockPolicy();

var service = new NineChroniclesNodeService(userPrivateKey, properties, blockPolicy, NetworkType.Test, StaticActionLoaderSingleton.Instance);
StandaloneContextFx.NineChroniclesNodeService = service;
StandaloneContextFx.BlockChain = service.Swarm?.BlockChain;

var query = $"query {{ activationKeyNonce(invitationCode: \"{code}\") }}";
var queryResult = await ExecuteQueryAsync(query);
Assert.NotNull(queryResult.Errors);
Assert.Single(queryResult.Errors!);
Assert.Equal(msg, queryResult.Errors!.First().Message);

}

[Fact]
public async Task Balance()
{
Expand Down

0 comments on commit c290aa9

Please sign in to comment.