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

P-chain UTs consolidation 3 - Fork Handling #2169

Closed

Conversation

abi87
Copy link
Contributor

@abi87 abi87 commented Oct 15, 2023

Why this should be merged

P-chain unit tests needs to be easier to maintain.
Specifically I currently find non-trivial to understand what specific fork a unit test is testing and possibly to update the tests to the latest fork.
Also fork times do not make sense and vm clock is not set to fullfil them.
This PR, centralizes and simplifies fork setting in UTs. It also fixes a few subtly broken UTs.
Thanks @joshua-kim and @marun for the discussions that brought me to these changes.

This PR replaces #1530, a very similar, previous attempt which lacked centralization of testing utilities in test setup package

How this works

Just refactor tests so that the latest active fork is explictly set upon vm creation and vm.clock is set to make the fork active.

How this was tested

Testing test is tricky. I guess I tested by manual inspection

@abi87 abi87 marked this pull request as ready for review October 17, 2023 08:44
@abi87 abi87 force-pushed the pchain_UTs_forks_consolidation branch from 44e4105 to 269be20 Compare October 17, 2023 09:10
@@ -164,8 +173,8 @@ func addSubnet(t *testing.T, env *environment) {
ts.Keys[1].PublicKey().Address(),
ts.Keys[2].PublicKey().Address(),
},
[]*secp256k1.PrivateKey{ts.Keys[0]},
ts.Keys[0].PublicKey().Address(),
[]*secp256k1.PrivateKey{ts.Keys[4]},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we used to use key 0 to fund a subnet creation. This messes up the key 0 balance (it reduces its initial balanche ts.Balance of the subnet creation fee) which can be annoying in some UTs where we do balance comparison.
In such tests we rarely use key 4 instead, so it's easier to use key 4 here to fund the subnet creation and keep balance checks simpler

@@ -216,13 +226,6 @@ func defaultState(
return state
}

func defaultClock() *mockable.Clock {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved to test setup package

Comment on lines 86 to 87
config: ts.Config(fork, forkTime),
clk: ts.Clock(forkTime),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

so finally local clock and chain time are aligned and selected fork is active.

@@ -183,6 +189,7 @@ func addSubnet(t *testing.T, env *environment) {

stateDiff.AddTx(testSubnet1, status.Committed)
require.NoError(stateDiff.Apply(env.state))
require.NoError(env.state.Commit())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this commit is needed to properly consume utxos used to fund subnet creation. Without this, a tx creation may end up using consumed utxos (not properly stored on state) and fail.
The reason that didn't happen before is that subnet creation fee used to be set to zero, which masked the issue.

@@ -87,7 +88,8 @@ func TestCreateChainTxWrongControlSig(t *testing.T) {
// Replace a valid signature with one from another key
sig, err := key.SignHash(hashing.ComputeHash256(tx.Unsigned.Bytes()))
require.NoError(err)
copy(tx.Creds[0].(*secp256k1fx.Credential).Sigs[0][:], sig)
sigPos := len(tx.Creds) - 1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

subnet creation fees are non zero, which implies that credentials changed position. Tx sign credential are always append last, so this change should make test less brittle

@@ -1123,7 +1129,14 @@ func TestValidatorSetAtCacheOverwriteRegression(t *testing.T) {
func TestAddDelegatorTxAddBeforeRemove(t *testing.T) {
require := require.New(t)

validatorStartTime := banffForkTime.Add(executor.SyncBound).Add(1 * time.Second)
Copy link
Contributor Author

@abi87 abi87 Oct 17, 2023

Choose a reason for hiding this comment

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

we don't want package-scoped fork times anymore.
Default fork times must be in test setup package; if a test needs a different fork time, it should scope it locally.

@abi87 abi87 self-assigned this Oct 17, 2023
@abi87 abi87 requested review from marun and dhrubabasu October 17, 2023 10:29
@abi87 abi87 changed the title Pchain UTs forks consolidation P-chain UTs consolidation - Fork Handling Oct 17, 2023
@abi87 abi87 changed the title P-chain UTs consolidation - Fork Handling P-chain UTs consolidation 3 - Fork Handling Nov 29, 2023
@abi87 abi87 linked an issue Dec 9, 2023 that may be closed by this pull request
@@ -63,30 +63,23 @@ import (
txexecutor "github.com/ava-labs/avalanchego/vms/platformvm/txs/executor"
)

var (
latestForkTime = genesistest.ValidateEndTime.Add(-5 * configtest.MinStakingDuration)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

no global latestForkTime anymore. So we don't have to worry to reset it across UTs

Copy link

github-actions bot commented Mar 3, 2024

This PR has become stale because it has been open for 30 days with no activity. Adding the lifecycle/frozen label will cause this PR to ignore lifecycle events.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Status: In Review 👀
Development

Successfully merging this pull request may close these issues.

Make P-chain UTs maintanable
2 participants