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

code optimization #2958

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions src/Neo/SmartContract/ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (C) 2015-2022 The Neo Project.
//
// The neo is free software distributed under the MIT software license,
// Copyright (C) 2015-2023 The Neo Project.
//
// The neo is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project or http://www.opensource.org/licenses/mit-license.php
// project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

Expand Down Expand Up @@ -235,7 +235,7 @@ protected internal bool CheckWitness(byte[] hashOrPubkey)
{
20 => new UInt160(hashOrPubkey),
33 => Contract.CreateSignatureRedeemScript(ECPoint.DecodePoint(hashOrPubkey, ECCurve.Secp256r1)).ToScriptHash(),
_ => throw new ArgumentException(null, nameof(hashOrPubkey))
_ => throw new ArgumentException("Invalid hashOrPubkey.", nameof(hashOrPubkey))
};
return CheckWitnessInternal(hash);
}
Expand Down Expand Up @@ -271,20 +271,15 @@ protected internal bool CheckWitnessInternal(UInt160 hash)
}
return false;
}
else
{
// If we don't have the ScriptContainer, we consider that there are no script hashes for verifying
if (ScriptContainer is null) return false;
}

// Check allow state callflag
// If we don't have the ScriptContainer, we consider that there are no script hashes for verifying
if (ScriptContainer is null) return false;

// Check allow state callflag
ValidateCallFlags(CallFlags.ReadStates);

// only for non-Transaction types (Block, etc)

var hashes_for_verifying = ScriptContainer.GetScriptHashesForVerifying(Snapshot);
return hashes_for_verifying.Contains(hash);
return ScriptContainer.GetScriptHashesForVerifying(Snapshot).Contains(hash);
}

/// <summary>
Expand Down
14 changes: 14 additions & 0 deletions tests/Neo.UnitTests/SmartContract/UT_InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,20 @@ public void TestRuntime_CheckWitness()
action.Should().Throw<ArgumentException>();
}

[TestMethod]
public void TestRuntime_CheckWitness_Null_ScriptContainer()
{
byte[] privateKey = { 0x01,0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
KeyPair keyPair = new(privateKey);
ECPoint pubkey = keyPair.PublicKey;

var engine = GetEngine();

engine.CheckWitness(pubkey.EncodePoint(true)).Should().BeFalse();
engine.CheckWitness(pubkey.EncodePoint(true)).Should().BeFalse();
Copy link
Member

Choose a reason for hiding this comment

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

Two checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can we just leave it this way for now? My bad, did not notice it at all.

Copy link
Member

Choose a reason for hiding this comment

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

Just fix in a new pr

}

[TestMethod]
public void TestRuntime_Log()
{
Expand Down