From 67fdd563f62ca5a7f1bf6d45b1058f7e68f44c87 Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Thu, 6 Jul 2017 20:47:01 +0800 Subject: [PATCH] add new example: ContractAsset --- AgencyTransaction/AgencyTransaction.csproj | 5 +- AgencyTransaction/AgencyTransaction.txt | 252 ++++++++++++++++++++ AgencyTransaction/packages.config | 2 +- AntShares.SmartContract.Contracts.sln | 6 + ContractAsset/AntShares.ILConvertPlugin.dll | Bin 0 -> 6656 bytes ContractAsset/ContractAsset.cs | 53 ++++ ContractAsset/ContractAsset.csproj | 65 +++++ ContractAsset/Properties/AssemblyInfo.cs | 36 +++ ContractAsset/build.tasks | 4 + ContractAsset/packages.config | 4 + Domain/Domain.cs | 20 +- Domain/Domain.csproj | 4 +- Domain/packages.config | 2 +- HelloWorld/HelloWorld.csproj | 4 +- HelloWorld/packages.config | 2 +- Lock/Lock.csproj | 4 +- Lock/packages.config | 2 +- StructExample/StructExample.csproj | 7 +- StructExample/packages.config | 2 +- 19 files changed, 447 insertions(+), 27 deletions(-) create mode 100644 AgencyTransaction/AgencyTransaction.txt create mode 100644 ContractAsset/AntShares.ILConvertPlugin.dll create mode 100644 ContractAsset/ContractAsset.cs create mode 100644 ContractAsset/ContractAsset.csproj create mode 100644 ContractAsset/Properties/AssemblyInfo.cs create mode 100644 ContractAsset/build.tasks create mode 100644 ContractAsset/packages.config diff --git a/AgencyTransaction/AgencyTransaction.csproj b/AgencyTransaction/AgencyTransaction.csproj index 492da99..6fe3952 100644 --- a/AgencyTransaction/AgencyTransaction.csproj +++ b/AgencyTransaction/AgencyTransaction.csproj @@ -32,8 +32,8 @@ 4 - - ..\packages\AntShares.SmartContract.Framework.2.0.0\lib\net40\AntShares.SmartContract.Framework.dll + + ..\packages\AntShares.SmartContract.Framework.2.0.0.4\lib\net40\AntShares.SmartContract.Framework.dll @@ -53,6 +53,7 @@ + diff --git a/AgencyTransaction/AgencyTransaction.txt b/AgencyTransaction/AgencyTransaction.txt new file mode 100644 index 0000000..0ffa032 --- /dev/null +++ b/AgencyTransaction/AgencyTransaction.txt @@ -0,0 +1,252 @@ +//public static bool Main(byte[] agent, byte[] assetId, byte[] valueId, byte[] client, bool way, BigInteger price, byte[] signature) + +//if (VerifySignature(client, signature)) return true; +PUSH 6 +PICK +PUSH 4 +ROLL +CHECKSIG +JMPIFNOT S01 +PUSH true +TOALTSTACK +JMP CLEAR + +//if (!VerifySignature(agent, signature)) return false; +S01: +PUSH 5 +ROLL +SWAP +CHECKSIG +JMPIF S02 +PUSH false +TOALTSTACK +JMP CLEAR + +//set inputId and outputId +S02: +PUSH 2 +PICK +JMPIF S03 +SWAP + +//BigInteger inputSum = 0, outputSum = 0; +S03: +PUSH 0 +PUSH 0 + +//TransactionOutput[] references = ((Transaction)ExecutionEngine.ScriptContainer).GetReferences(); +SYSCALL System.ExecutionEngine.GetScriptContainer +SYSCALL AntShares.Transaction.GetReferences + +//foreach (TransactionOutput reference in references) +//{ + +//initialize loop +DUP +ARRAYSIZE +PUSH 0 + +//exit test +LOOP1: +OVER +OVER +NUMEQUAL +JMPIF S04 + +//set reference +PUSH 2 +PICK +OVER +PICKITEM + +//if (reference.ScriptHash.Equals(ExecutionEngine.EntryScriptHash)) +//{ +DUP +SYSCALL AntShares.Output.GetScriptHash +SYSCALL System.ExecutionEngine.GetEntryScriptHash +EQUAL +JMPIFNOT LOOP1S1 + +//if (!reference.AssetId.Equals(inputId)) +DUP +SYSCALL AntShares.Output.GetAssetId +PUSH 7 +PICK +EQUAL +JMPIF LOOP1S2 + +//return false; +PUSH false +TOALTSTACK +JMP CLEAR + +//else +LOOP1S2: + +//inputSum += reference.Value; +DUP +SYSCALL AntShares.Output.GetValue +PUSH 5 +PICK +ADD +PUSH 5 +XSWAP +DROP +//} + +LOOP1S1: +DROP +INC +JMP LOOP1 +S04: +DROP +DROP +DROP +//} + +//TransactionOutput[] outputs = ((Transaction)ExecutionEngine.ScriptContainer).GetOutputs(); +SYSCALL System.ExecutionEngine.GetScriptContainer +SYSCALL AntShares.Transaction.GetOutputs + +//foreach (TransactionOutput output in outputs) +//{ + +//initialize loop +DUP +ARRAYSIZE +PUSH 0 + +//exit test +LOOP2: +OVER +OVER +NUMEQUAL +JMPIF S05 + +//set output +PUSH 2 +PICK +OVER +PICKITEM + +//if (output.ScriptHash.Equals(ExecutionEngine.EntryScriptHash)) +//{ +DUP +SYSCALL AntShares.Output.GetScriptHash +SYSCALL System.ExecutionEngine.GetEntryScriptHash +EQUAL +JMPIFNOT LOOP2S1 + +//if (output.AssetId.Equals(inputId)) +DUP +SYSCALL AntShares.Output.GetAssetId +PUSH 7 +PICK +EQUAL +JMPIFNOT LOOP2S2 + +//inputSum -= output.Value; +PUSH 4 +PICK +OVER +SYSCALL AntShares.Output.GetValue +SUB +PUSH 5 +XSWAP +DROP +JMP LOOP2S1 + +//else if (output.AssetId.Equals(outputId)) +LOOP2S2: +DUP +SYSCALL AntShares.Output.GetAssetId +PUSH 8 +PICK +EQUAL +JMPIFNOT LOOP2S1 + +//outputSum += output.Value; +PUSH 5 +PICK +OVER +SYSCALL AntShares.Output.GetValue +ADD +PUSH 6 +XSWAP +DROP +//} + +LOOP2S1: +DROP +INC +JMP LOOP2 +S05: +DROP +DROP +DROP +//} + +//if (inputSum <= 0) return true; +DUP +PUSH 0 +LTE +JMPIFNOT S06 +PUSH true +TOALTSTACK +JMP CLEAR + +//if (way) +//{ +S06: +PUSH 4 +ROLL +JMPIFNOT S07 + +//if (outputSum * 100000000 < inputSum * price) return false; +PUSH 4 +ROLL +MUL +SWAP +PUSH 100000000 +MUL +GT +JMPIFNOT S08 +PUSH false +TOALTSTACK +JMP CLEAR +//} + +//else +//{ +S07: + +//if (inputSum * 100000000 > outputSum * price) return false; +PUSH 100000000 +MUL +SWAP +PUSH 4 +ROLL +MUL +GT +JMPIFNOT S08 +PUSH false +TOALTSTACK +JMP CLEAR +//} + +//return true; +S08: +DROP +DROP +PUSH true +RET + +//clear stack and return +CLEAR: +DEPTH +JMPIFNOT EXIT +DROP +JMP CLEAR +EXIT: +FROMALTSTACK +RET diff --git a/AgencyTransaction/packages.config b/AgencyTransaction/packages.config index 31107e7..beb4540 100644 --- a/AgencyTransaction/packages.config +++ b/AgencyTransaction/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/AntShares.SmartContract.Contracts.sln b/AntShares.SmartContract.Contracts.sln index b9a4c31..c2e1668 100644 --- a/AntShares.SmartContract.Contracts.sln +++ b/AntShares.SmartContract.Contracts.sln @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Domain", "Domain\Domain.csp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StructExample", "StructExample\StructExample.csproj", "{6777CA72-89CB-4FDD-84C3-BC82A440CD58}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ContractAsset", "ContractAsset\ContractAsset.csproj", "{743F4206-82CC-4807-9189-AC6B8B64A563}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {6777CA72-89CB-4FDD-84C3-BC82A440CD58}.Debug|Any CPU.Build.0 = Debug|Any CPU {6777CA72-89CB-4FDD-84C3-BC82A440CD58}.Release|Any CPU.ActiveCfg = Release|Any CPU {6777CA72-89CB-4FDD-84C3-BC82A440CD58}.Release|Any CPU.Build.0 = Release|Any CPU + {743F4206-82CC-4807-9189-AC6B8B64A563}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {743F4206-82CC-4807-9189-AC6B8B64A563}.Debug|Any CPU.Build.0 = Debug|Any CPU + {743F4206-82CC-4807-9189-AC6B8B64A563}.Release|Any CPU.ActiveCfg = Release|Any CPU + {743F4206-82CC-4807-9189-AC6B8B64A563}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ContractAsset/AntShares.ILConvertPlugin.dll b/ContractAsset/AntShares.ILConvertPlugin.dll new file mode 100644 index 0000000000000000000000000000000000000000..d5e927dffe023a434aa3842022c4789d77dc35ab GIT binary patch literal 6656 zcmeHLYiu0V6+Sb&Uhl5sP1a8AB+g^ddTd| zGBcaRKp_Z0p$!Ejq_zlDf%rkH{vZ;flnSc0YUv9K5^Yrp1XMz8RU!UV6|E}L@7&q3 z*GWi4|4}ik`R=*roO|xM=bn3JW=F4om_kGp#(n-g(FXUeRI1O=P{fh~DF81n% z7nG4#H)LlGS1UVq(a}plo3Jfc`YCHt)I|6-EErn(--5obu3bUkZRs`d0NDJ$Y$F$5M{# zad*f*8xXyqp4Km(B? zxt|`^skdWjc>6{*^c6|KL~~~YeFy%apH$m{5gR)Zk4M^*Xc}BBC%(l2zic-%8EIdR z#$Lg6i`~LatKI51SQ7VEanrsUXtx2{*8rA8?X_rH?R7w(b}&J?zI{Cywzj|>;HkL3 zys@P@+|pdv`!~!UwKuTdMnH2U8E!q&98QK;?|U7h*zQ*tZ3HLYvhRmXx7bMG`1(D4 z4{^^KWs_UL>Cn{nt!QIw>@TqPgIJ4Vw}VVZ8hh`N$~Ro3+*Yf+t*U%jDi;IgtIr^o zRkK@TP9@+s*T<6etw*GV=4fn9vVP4`dpm5bd=QaPwzRBoe_~7P`fX3xSAf|ZYiW)r zW8W}ZyOtzlt&cUu+ph$BtM9cl2dfK^jI}(LjJLG3B%{@ZbX0wNk$rJ)4}7@#0q*;& zSmawnEZGqFwj|b;Y-qcjZ`h7RveEag5xylZ=UXFuOR#TC7W;O^hM4o!z_E@{Yja(7 zM_Ozg_V`xpLv^c>`|;g%AD=%TwL5uTb=%je?OmX@)BenL`<3b`;4^iiC)Jti?(FW` zjSkL<3A{fFaq zhdxl7$P2$8O3>T%ff}KE!Uv)Mw%Us_b!X^((D$kThW^W;Ud;Qv%FqE!FdnK*VL3)wKHP*Q=I!7;qd!*XCpMHv7LH2%5 zH>*!0K1*?Up}F!M%-~0BMvc)sh*T07p;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tUlsv$N3E zo$CPZ=;+y*%XR48-8(zFX1>@nqwnmQ>Cy2bdqAPpsY65A`SNpRwa)G3M=sWrSUxWr zG~BYO%SYWZ9;3}mw4Sj4!b>0F6+gI|_|;JhdW+lOyVvJV57^G2X^!fKqHva#5{Dv+6HaNX_ZT^=KzL75CvmXC@pE${MVF2Wo9+_2%!$8)+T%IeMPdwcx8+X{= zy|6t3yG3c0BUZ*Y1xLJyRiVP;dq_it=h + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {743F4206-82CC-4807-9189-AC6B8B64A563} + Library + Properties + AntShares.SmartContract + ContractAsset + v4.6.1 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + none + true + bin\Release\ + + + prompt + 4 + + + + ..\packages\AntShares.SmartContract.Framework.2.0.0.4\lib\net40\AntShares.SmartContract.Framework.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ContractAsset/Properties/AssemblyInfo.cs b/ContractAsset/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..9ead5e0 --- /dev/null +++ b/ContractAsset/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ContractAsset")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ContractAsset")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("743f4206-82cc-4807-9189-ac6b8b64a563")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ContractAsset/build.tasks b/ContractAsset/build.tasks new file mode 100644 index 0000000..050b86a --- /dev/null +++ b/ContractAsset/build.tasks @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/ContractAsset/packages.config b/ContractAsset/packages.config new file mode 100644 index 0000000..42622cb --- /dev/null +++ b/ContractAsset/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Domain/Domain.cs b/Domain/Domain.cs index 12952b5..0338f5a 100644 --- a/Domain/Domain.cs +++ b/Domain/Domain.cs @@ -12,11 +12,11 @@ public static object Main(string operation, params object[] args) case "query": return Query((string)args[0]); case "register": - return Register((string)args[0], (byte[])args[1], (byte[])args[2]); + return Register((string)args[0], (byte[])args[1]); case "transfer": - return Transfer((string)args[0], (byte[])args[1], (byte[])args[2], (byte[])args[2]); + return Transfer((string)args[0], (byte[])args[1]); case "delete": - return Delete((string)args[0], (byte[])args[1]); + return Delete((string)args[0]); default: return false; } @@ -27,30 +27,30 @@ private static byte[] Query(string domain) return Storage.Get(Storage.CurrentContext, domain); } - private static bool Register(string domain, byte[] owner, byte[] signature) + private static bool Register(string domain, byte[] owner) { - if (!VerifySignature(owner, signature)) return false; + if (!Runtime.CheckWitness(owner)) return false; byte[] value = Storage.Get(Storage.CurrentContext, domain); if (value != null) return false; Storage.Put(Storage.CurrentContext, domain, owner); return true; } - private static bool Transfer(string domain, byte[] signature_from, byte[] to, byte[] signature_to) + private static bool Transfer(string domain, byte[] to) { - if (!VerifySignature(to, signature_to)) return false; + if (!Runtime.CheckWitness(to)) return false; byte[] from = Storage.Get(Storage.CurrentContext, domain); if (from == null) return false; - if (!VerifySignature(from, signature_from)) return false; + if (!Runtime.CheckWitness(from)) return false; Storage.Put(Storage.CurrentContext, domain, to); return true; } - private static bool Delete(string domain, byte[] signature) + private static bool Delete(string domain) { byte[] owner = Storage.Get(Storage.CurrentContext, domain); if (owner == null) return false; - if (!VerifySignature(owner, signature)) return false; + if (!Runtime.CheckWitness(owner)) return false; Storage.Delete(Storage.CurrentContext, domain); return true; } diff --git a/Domain/Domain.csproj b/Domain/Domain.csproj index 388d675..24610e6 100644 --- a/Domain/Domain.csproj +++ b/Domain/Domain.csproj @@ -32,8 +32,8 @@ 4 - - ..\packages\AntShares.SmartContract.Framework.2.0.0\lib\net40\AntShares.SmartContract.Framework.dll + + ..\packages\AntShares.SmartContract.Framework.2.0.0.4\lib\net40\AntShares.SmartContract.Framework.dll diff --git a/Domain/packages.config b/Domain/packages.config index 31107e7..beb4540 100644 --- a/Domain/packages.config +++ b/Domain/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/HelloWorld/HelloWorld.csproj b/HelloWorld/HelloWorld.csproj index 83ee1bb..0191814 100644 --- a/HelloWorld/HelloWorld.csproj +++ b/HelloWorld/HelloWorld.csproj @@ -32,8 +32,8 @@ 4 - - ..\packages\AntShares.SmartContract.Framework.2.0.0\lib\net40\AntShares.SmartContract.Framework.dll + + ..\packages\AntShares.SmartContract.Framework.2.0.0.4\lib\net40\AntShares.SmartContract.Framework.dll diff --git a/HelloWorld/packages.config b/HelloWorld/packages.config index 31107e7..beb4540 100644 --- a/HelloWorld/packages.config +++ b/HelloWorld/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/Lock/Lock.csproj b/Lock/Lock.csproj index bff5b76..8742844 100644 --- a/Lock/Lock.csproj +++ b/Lock/Lock.csproj @@ -32,8 +32,8 @@ 4 - - ..\packages\AntShares.SmartContract.Framework.2.0.0\lib\net40\AntShares.SmartContract.Framework.dll + + ..\packages\AntShares.SmartContract.Framework.2.0.0.4\lib\net40\AntShares.SmartContract.Framework.dll diff --git a/Lock/packages.config b/Lock/packages.config index 31107e7..beb4540 100644 --- a/Lock/packages.config +++ b/Lock/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/StructExample/StructExample.csproj b/StructExample/StructExample.csproj index ac3100e..13bb273 100644 --- a/StructExample/StructExample.csproj +++ b/StructExample/StructExample.csproj @@ -32,9 +32,8 @@ 4 - - ..\packages\AntShares.SmartContract.Framework.2.0.0\lib\net40\AntShares.SmartContract.Framework.dll - True + + ..\packages\AntShares.SmartContract.Framework.2.0.0.4\lib\net40\AntShares.SmartContract.Framework.dll @@ -51,8 +50,8 @@ - + diff --git a/StructExample/packages.config b/StructExample/packages.config index 711a54a..42622cb 100644 --- a/StructExample/packages.config +++ b/StructExample/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file