Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: migrate native contract API tests to neotest framework #2299

Merged
merged 13 commits into from
Jan 14, 2022

Conversation

AnnaShaleva
Copy link
Member

@AnnaShaleva AnnaShaleva commented Dec 3, 2021

Part of #1472.

Migration goes well, but some tests need to be adjusted, so test refactoring is being made along the way. Progress:

  • Policy
  • Ledger
  • GAS
  • Designate
  • Management
  • Oracle
  • Notary
  • NEO
  • StdLib (no tests need to be migrated)
  • Refactor tests structure and rebase onto curretn master (in progress)

pkg/nativetest/common_test.go Outdated Show resolved Hide resolved
}

func TestBlockedAccounts(t *testing.T) {
chain := newTestChain(t)
account := util.Uint160{1, 2, 3}
policyHash := chain.contracts.Policy.Metadata().Hash

transferTokenFromMultisigAccount(t, chain, testchain.CommitteeScriptHash(),
chain.contracts.GAS.Hash, 100_00000000)

t.Run("isBlocked, internal method", func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These checks can be moved too, I think. We're testing native package here anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're testing internal method here and it uses dao to retrieve blocked accounts. Our native tests can't be dependant on dao.

@AnnaShaleva
Copy link
Member Author

AnnaShaleva commented Dec 9, 2021

Depends on #2298, still in progress.

@codecov
Copy link

codecov bot commented Dec 15, 2021

Codecov Report

Merging #2299 (45f543a) into master (501ca0d) will increase coverage by 0.02%.
The diff coverage is 79.74%.

❗ Current head 45f543a differs from pull request most recent head 0f85b4e. Consider uploading reports for the commit 0f85b4e to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2299      +/-   ##
==========================================
+ Coverage   84.01%   84.03%   +0.02%     
==========================================
  Files         307      308       +1     
  Lines       29840    29913      +73     
==========================================
+ Hits        25070    25138      +68     
+ Misses       3350     3347       -3     
- Partials     1420     1428       +8     
Impacted Files Coverage Δ
...ve/nativetest/helpers/policyhelper/policyhelper.go 0.00% <0.00%> (ø)
pkg/vm/emit/emit.go 88.88% <22.22%> (-11.12%) ⬇️
pkg/neotest/basic.go 92.78% <100.00%> (+5.14%) ⬆️
pkg/neotest/chain/chain.go 92.77% <100.00%> (+0.87%) ⬆️
pkg/neotest/client.go 100.00% <100.00%> (ø)
pkg/core/native/oracle.go 73.85% <0.00%> (-3.27%) ⬇️
pkg/core/native/management.go 90.30% <0.00%> (-0.56%) ⬇️
pkg/vm/stackitem/item.go 91.63% <0.00%> (-0.38%) ⬇️
pkg/network/server.go 73.83% <0.00%> (+0.11%) ⬆️
pkg/core/native/ledger.go 94.82% <0.00%> (+0.86%) ⬆️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 501ca0d...0f85b4e. Read the comment docs.

newGAS := e.Chain.GetUtilityTokenBalance(voters[i].ScriptHash())
newGAS.Sub(newGAS, gasBalance[i])
// TODO: deal with unexported CalculateNEOHolderReward
gasForHold, err := e.Chain.CalculateNEOHolderReward(voters[i].ScriptHash(), e.Chain.BlockHeight())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roman-khimov, I'm not sure how to deal with this part of test. Here we need to compare voter rewards (second part), but CalculateNEOHolderReward is unexported.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Check the value against non-voter (holder) account that holds NEO for the same number of blocks.
  2. Hardcode exact GAS values generated for the tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that hardcoded value is good and maintainable, so let's go with the first suggestion. I added these reference accounts and comment on why do we need them. Fixed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it's a test, it can expect some exact value given known conditions, but comparing to non-voter is more flexible, of course.

bc.getNodesByRole(t, true, noderoles.NeoFSAlphabet, bc.BlockHeight()+1, 1)
})
}

func TestDesignate_DesignateAsRole(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is aimed to test exported contract methods (not via transaction invocation). However, it can easily be converted to use neotest. So do we need to convert it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a counterpart for it that does on-chain invocations? We can have tests like this if for some reason testing them from on-chain call is inappropriate or not possible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried to convert this tests to on-chain-based, but occurs that we need to check best designated index and it's not possible with on-chain invocations. So I think we'd better leave this test as is.

Comment on lines 68 to 69
h, err := e.Chain.GetNativeContractScriptHash(name)
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reuse e.NativeHash(t, name) ?

Comment on lines 25 to 27
for i := 0; i < height-1; i++ {
e.AddNewBlock(t)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use GenerateNewBlocks from the first commit?

Comment on lines 63 to 65
for i := 0; i < int(e.Chain.GetConfig().MaxTraceableBlocks); i++ {
e.AddNewBlock(t)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

pkg/vm/emit/emit.go Outdated Show resolved Hide resolved
newGAS := e.Chain.GetUtilityTokenBalance(voters[i].ScriptHash())
newGAS.Sub(newGAS, gasBalance[i])
// TODO: deal with unexported CalculateNEOHolderReward
gasForHold, err := e.Chain.CalculateNEOHolderReward(voters[i].ScriptHash(), e.Chain.BlockHeight())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Check the value against non-voter (holder) account that holds NEO for the same number of blocks.
  2. Hardcode exact GAS values generated for the tests.

pkg/core/native_neo_test.go Outdated Show resolved Hide resolved
pkg/core/oracle_test.go Outdated Show resolved Hide resolved
pkg/core/native_neo_test.go Outdated Show resolved Hide resolved
@AnnaShaleva
Copy link
Member Author

Not yet ready for review, I need to rebase on master and carefully inspect the whole set of changes.

@AnnaShaleva AnnaShaleva marked this pull request as draft January 13, 2022 15:34
Added several methods that are useful for testing.
AnnaShaleva and others added 2 commits January 13, 2022 19:12
GAS and NEO tokens are sent to validators account (not the committee's
one). For single-node chain they are the same, but for four-nodes chain
they are different. Thus, use validators multisig address to create new
accounts and to deploy contracts.

Also, allow to provide desired account balance while creating new
account.
@AnnaShaleva AnnaShaleva marked this pull request as ready for review January 13, 2022 17:49
Copy link
Member

@roman-khimov roman-khimov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I think this makes tests much more readable, for example notary contract test became shorter. It also proves testing framework viability, which is important. Still, we have some hairy parts also that can be improved in future.

pkg/core/native/native_test/common_test.go Outdated Show resolved Hide resolved
pkg/core/native/native_test/common_test.go Outdated Show resolved Hide resolved
pkg/core/native/native_test/ledger_test.go Show resolved Hide resolved
pkg/core/native_management_test.go Show resolved Hide resolved
pkg/core/native/native_test/management_test.go Outdated Show resolved Hide resolved
pkg/core/native/native_test/oracle_test.go Outdated Show resolved Hide resolved
@roman-khimov roman-khimov merged commit 0b0531d into master Jan 14, 2022
@roman-khimov roman-khimov deleted the tests-migration branch January 14, 2022 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants