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

Fix EmitPush #2106

Merged
merged 2 commits into from
Nov 26, 2020
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
48 changes: 14 additions & 34 deletions src/neo/SmartContract/ContractParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,21 @@ public ContractParameter() { }
public ContractParameter(ContractParameterType type)
{
this.Type = type;
switch (type)
this.Value = type switch
{
case ContractParameterType.Signature:
this.Value = new byte[64];
break;
case ContractParameterType.Boolean:
this.Value = false;
break;
case ContractParameterType.Integer:
this.Value = 0;
break;
case ContractParameterType.Hash160:
this.Value = new UInt160();
break;
case ContractParameterType.Hash256:
this.Value = new UInt256();
break;
case ContractParameterType.ByteArray:
this.Value = Array.Empty<byte>();
break;
case ContractParameterType.PublicKey:
this.Value = ECCurve.Secp256r1.G;
break;
case ContractParameterType.String:
this.Value = "";
break;
case ContractParameterType.Array:
this.Value = new List<ContractParameter>();
break;
case ContractParameterType.Map:
this.Value = new List<KeyValuePair<ContractParameter, ContractParameter>>();
break;
default:
throw new ArgumentException();
}
ContractParameterType.Any => null,
ContractParameterType.Signature => new byte[64],
ContractParameterType.Boolean => false,
ContractParameterType.Integer => 0,
ContractParameterType.Hash160 => new UInt160(),
ContractParameterType.Hash256 => new UInt256(),
ContractParameterType.ByteArray => Array.Empty<byte>(),
ContractParameterType.PublicKey => ECCurve.Secp256r1.G,
ContractParameterType.String => "",
ContractParameterType.Array => new List<ContractParameter>(),
ContractParameterType.Map => new List<KeyValuePair<ContractParameter, ContractParameter>>(),
_ => throw new ArgumentException(),
};
}

public static ContractParameter FromJson(JObject json)
Expand Down
81 changes: 42 additions & 39 deletions src/neo/VM/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,45 +63,48 @@ public static ScriptBuilder EmitPush(this ScriptBuilder sb, ISerializable data)

public static ScriptBuilder EmitPush(this ScriptBuilder sb, ContractParameter parameter)
{
switch (parameter.Type)
{
case ContractParameterType.Signature:
case ContractParameterType.ByteArray:
sb.EmitPush((byte[])parameter.Value);
break;
case ContractParameterType.Boolean:
sb.EmitPush((bool)parameter.Value);
break;
case ContractParameterType.Integer:
if (parameter.Value is BigInteger bi)
sb.EmitPush(bi);
else
sb.EmitPush((BigInteger)typeof(BigInteger).GetConstructor(new[] { parameter.Value.GetType() }).Invoke(new[] { parameter.Value }));
break;
case ContractParameterType.Hash160:
sb.EmitPush((UInt160)parameter.Value);
break;
case ContractParameterType.Hash256:
sb.EmitPush((UInt256)parameter.Value);
break;
case ContractParameterType.PublicKey:
sb.EmitPush((ECPoint)parameter.Value);
break;
case ContractParameterType.String:
sb.EmitPush((string)parameter.Value);
break;
case ContractParameterType.Array:
{
IList<ContractParameter> parameters = (IList<ContractParameter>)parameter.Value;
for (int i = parameters.Count - 1; i >= 0; i--)
sb.EmitPush(parameters[i]);
sb.EmitPush(parameters.Count);
sb.Emit(OpCode.PACK);
}
break;
default:
throw new ArgumentException();
}
if (parameter.Value is null)
sb.Emit(OpCode.PUSHNULL);
else
switch (parameter.Type)
{
case ContractParameterType.Signature:
case ContractParameterType.ByteArray:
sb.EmitPush((byte[])parameter.Value);
break;
case ContractParameterType.Boolean:
sb.EmitPush((bool)parameter.Value);
break;
case ContractParameterType.Integer:
if (parameter.Value is BigInteger bi)
sb.EmitPush(bi);
else
sb.EmitPush((BigInteger)typeof(BigInteger).GetConstructor(new[] { parameter.Value.GetType() }).Invoke(new[] { parameter.Value }));
break;
case ContractParameterType.Hash160:
sb.EmitPush((UInt160)parameter.Value);
break;
case ContractParameterType.Hash256:
sb.EmitPush((UInt256)parameter.Value);
break;
case ContractParameterType.PublicKey:
sb.EmitPush((ECPoint)parameter.Value);
break;
case ContractParameterType.String:
sb.EmitPush((string)parameter.Value);
break;
case ContractParameterType.Array:
{
IList<ContractParameter> parameters = (IList<ContractParameter>)parameter.Value;
for (int i = parameters.Count - 1; i >= 0; i--)
sb.EmitPush(parameters[i]);
sb.EmitPush(parameters.Count);
sb.Emit(OpCode.PACK);
}
break;
default:
throw new ArgumentException();
}
return sb;
}

Expand Down