Skip to content

Commit b3a138c

Browse files
committed
Add unit-test for cross-aa
1 parent f16179d commit b3a138c

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

protobuf/test_virtual_address_contract.proto

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ service VirtualAddressContract {
1414
option (aelf.base) = "acs1.proto";
1515

1616
rpc VirtualAddressVote(VirtualAddressVoteInput) returns (google.protobuf.Empty);
17+
rpc VirtualAddressVoteWithInline(VirtualAddressVoteWithCountInput) returns (google.protobuf.Empty);
1718
rpc VirtualAddressWithdraw(aelf.Hash) returns (google.protobuf.Empty);
1819
rpc VirtualAddressChangeVotingOption(VirtualAddressChangeVotingOptionInput) returns (google.protobuf.Empty);
1920
rpc VirtualAddressClaimProfit(VirtualAddressClaimProfitInput) returns (google.protobuf.Empty);
@@ -32,6 +33,11 @@ message VirtualAddressVoteInput {
3233
aelf.Hash token = 4;
3334
}
3435

36+
message VirtualAddressVoteWithCountInput {
37+
VirtualAddressVoteInput vote_input = 1; // Existing input type
38+
int32 count = 2; // Additional count input
39+
}
40+
3541
message VirtualAddressChangeVotingOptionInput {
3642
bool is_reset = 1;
3743
aelf.Hash vote_id = 2;

test/AElf.Contracts.Election.Tests/BVT/ElectionTests.cs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,6 +1984,133 @@ await VirtualAddressContractStub.VirtualAddressVote.SendAsync(new VirtualAddress
19841984
return result.ActiveVotingRecords.First().VoteId;
19851985
}
19861986

1987+
// [Fact]
1988+
// public async Task<Hash> VirtualAddress_Vote_WithInlineTxIdAndCountTest()
1989+
// {
1990+
// var amount = 100;
1991+
// const int lockTime = 100 * 60 * 60 * 24;
1992+
// var candidatesKeyPairs = await ElectionContract_AnnounceElection_Test();
1993+
// var candidateKeyPair = candidatesKeyPairs[0];
1994+
//
1995+
// var address = await VirtualAddressContractStub.GetVirtualAddress.CallAsync(new Empty());
1996+
// var initBalance = 100000;
1997+
//
1998+
// await TokenContractStub.Transfer.SendAsync(new TransferInput
1999+
// {
2000+
// Amount = initBalance,
2001+
// Symbol = "ELF",
2002+
// To = address,
2003+
// Memo = "test"
2004+
// });
2005+
//
2006+
// CheckBalance(address, "ELF", initBalance);
2007+
// CheckBalance(address, "SHARE", 0);
2008+
// CheckBalance(address, "VOTE", 0);
2009+
//
2010+
// var virtualAddressVoteInput = new VirtualAddressVoteInput
2011+
// {
2012+
// PubKey = candidateKeyPair.PublicKey.ToHex(),
2013+
// Amount = amount,
2014+
// EndTimestamp = TimestampHelper.GetUtcNow().AddSeconds(lockTime),
2015+
// Token = HashHelper.ComputeFrom("token A")
2016+
// };
2017+
//
2018+
// var resultawait = await VirtualAddressContractStub.VirtualAddressVoteWithInline.SendAsync(new VirtualAddressVoteWithCountInput
2019+
// {
2020+
// VoteInput = virtualAddressVoteInput,
2021+
// Count = 6
2022+
// });
2023+
// return null;
2024+
// }
2025+
//
2026+
[Fact]
2027+
public async Task<Hash> VirtualAddress_Vote_WithInlineTxIdTest()
2028+
{
2029+
var amount = 100;
2030+
const int lockTime = 100 * 60 * 60 * 24;
2031+
var candidatesKeyPairs = await ElectionContract_AnnounceElection_Test();
2032+
var candidateKeyPair = candidatesKeyPairs[0];
2033+
2034+
var address = await VirtualAddressContractStub.GetVirtualAddress.CallAsync(new Empty());
2035+
var initBalance = 100000;
2036+
2037+
await TokenContractStub.Transfer.SendAsync(new TransferInput
2038+
{
2039+
Amount = initBalance,
2040+
Symbol = "ELF",
2041+
To = address,
2042+
Memo = "test"
2043+
});
2044+
2045+
CheckBalance(address, "ELF", initBalance);
2046+
CheckBalance(address, "SHARE", 0);
2047+
CheckBalance(address, "VOTE", 0);
2048+
2049+
var virtualAddressVoteInput = new VirtualAddressVoteInput
2050+
{
2051+
PubKey = candidateKeyPair.PublicKey.ToHex(),
2052+
Amount = amount,
2053+
EndTimestamp = TimestampHelper.GetUtcNow().AddSeconds(lockTime),
2054+
Token = HashHelper.ComputeFrom("token A")
2055+
};
2056+
2057+
await VirtualAddressContractStub.VirtualAddressVoteWithInline.SendAsync(new VirtualAddressVoteWithCountInput
2058+
{
2059+
VoteInput = virtualAddressVoteInput,
2060+
Count = 1
2061+
});
2062+
2063+
var result = await ElectionContractStub.GetElectorVote.CallAsync(new StringValue
2064+
{
2065+
Value = address.ToBase58()
2066+
});
2067+
result.ActiveVotedVotesAmount.ShouldBe(amount);
2068+
result = await ElectionContractStub.GetElectorVoteWithRecords.CallAsync(new StringValue
2069+
{
2070+
Value = address.ToBase58()
2071+
});
2072+
result.ActiveVotedVotesAmount.ShouldBe(amount);
2073+
result = await ElectionContractStub.GetElectorVoteWithAllRecords.CallAsync(new StringValue
2074+
{
2075+
Value = address.ToBase58()
2076+
});
2077+
result.AllVotedVotesAmount.ShouldBe(amount);
2078+
2079+
CheckBalance(address, "ELF", initBalance - amount);
2080+
CheckBalance(address, "SHARE", amount);
2081+
CheckBalance(address, "VOTE", amount);
2082+
2083+
await VirtualAddressContractStub.VirtualAddressVote.SendAsync(new VirtualAddressVoteInput
2084+
{
2085+
PubKey = candidateKeyPair.PublicKey.ToHex(),
2086+
Amount = amount,
2087+
EndTimestamp = TimestampHelper.GetUtcNow().AddSeconds(lockTime),
2088+
Token = HashHelper.ComputeFrom("token A")
2089+
});
2090+
2091+
result = await ElectionContractStub.GetElectorVote.CallAsync(new StringValue
2092+
{
2093+
Value = address.ToBase58()
2094+
});
2095+
result.ActiveVotedVotesAmount.ShouldBe(amount + amount);
2096+
result = await ElectionContractStub.GetElectorVoteWithRecords.CallAsync(new StringValue
2097+
{
2098+
Value = address.ToBase58()
2099+
});
2100+
result.ActiveVotedVotesAmount.ShouldBe(amount + amount);
2101+
result = await ElectionContractStub.GetElectorVoteWithAllRecords.CallAsync(new StringValue
2102+
{
2103+
Value = address.ToBase58()
2104+
});
2105+
result.AllVotedVotesAmount.ShouldBe(amount + amount);
2106+
2107+
CheckBalance(address, "ELF", initBalance - amount - amount);
2108+
CheckBalance(address, "SHARE", amount + amount);
2109+
CheckBalance(address, "VOTE", amount + amount);
2110+
2111+
return result.ActiveVotingRecords.First().VoteId;
2112+
}
2113+
19872114
[Fact]
19882115
public async Task VirtualAddress_Withdraw_Test()
19892116
{

test/AElf.Contracts.TestContract.VirtualAddress/Action.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@ namespace AElf.Contracts.TestContract.VirtualAddress;
99

1010
public partial class Action : VirtualAddressContractContainer.VirtualAddressContractBase
1111
{
12+
13+
public override Empty VirtualAddressVoteWithInline(VirtualAddressVoteWithCountInput input)
14+
{
15+
if (input.Count == null) return new Empty();
16+
Initialize();
17+
18+
for (int i = 0; i < input.Count; i++)
19+
{
20+
Context.SendVirtualInline(HashHelper.ComputeFrom("test"), State.ElectionContract.Value, ".Vote", new VoteMinerInput
21+
{
22+
CandidatePubkey = input.VoteInput.PubKey,
23+
Amount = input.VoteInput.Amount,
24+
EndTimestamp = input.VoteInput.EndTimestamp,
25+
Token = input.VoteInput.Token
26+
}.ToByteString());
27+
}
28+
29+
30+
return new Empty();
31+
}
32+
1233
public override Empty VirtualAddressVote(VirtualAddressVoteInput input)
1334
{
1435
Initialize();

0 commit comments

Comments
 (0)