Esempio n. 1
0
        public void Test_GetTransactionValidUntilBlock()
        {
            var contract = _engine.EntryScriptHash;

            using ScriptBuilder sb = new();
            sb.EmitDynamicCall(contract, "getTransactionValidUntilBlock");

            var tx = BuildTransaction(UInt160.Zero, sb.ToArray());

            tx.ValidUntilBlock = 1111;
            var engine = new TestEngine(TriggerType.Application, tx, new TestDataCache());

            engine.Snapshot.ContractAdd(new ContractState()
            {
                Hash     = contract,
                Nef      = _engine.Nef,
                Manifest = ContractManifest.FromJson(_engine.Manifest),
            });
            engine.LoadScript(sb.ToArray());

            Assert.AreEqual(VMState.HALT, engine.Execute());
            Assert.AreEqual(1, engine.ResultStack.Count);

            var item = engine.ResultStack.Pop();

            Assert.IsInstanceOfType(item, typeof(Neo.VM.Types.Integer));
            Assert.AreEqual(tx.ValidUntilBlock, item.GetInteger());
        }
Esempio n. 2
0
        public void Test_GetTransactionSender()
        {
            var contract = _engine.EntryScriptHash;

            using ScriptBuilder sb = new();
            sb.EmitDynamicCall(contract, "getTransactionSender");
            var sender = "NMA2FKN8up2cEwaJgtmAiDrZWB69ApnDfp".ToScriptHash(ProtocolSettings.Default.AddressVersion);
            var tx     = BuildTransaction(sender, sb.ToArray());
            var engine = new TestEngine(TriggerType.Application, tx, new TestDataCache());

            engine.Snapshot.ContractAdd(new ContractState()
            {
                Hash     = contract,
                Nef      = _engine.Nef,
                Manifest = ContractManifest.FromJson(_engine.Manifest),
            });
            engine.LoadScript(sb.ToArray());

            Assert.AreEqual(VMState.HALT, engine.Execute());
            Assert.AreEqual(1, engine.ResultStack.Count);

            var item = engine.ResultStack.Pop();

            Assert.IsInstanceOfType(item, typeof(Neo.VM.Types.ByteString));
            Assert.AreEqual(tx.Sender, new UInt160(item.GetSpan()));
        }
Esempio n. 3
0
        public void Test_GetTransactionScript()
        {
            var contract = _engine.EntryScriptHash;

            using ScriptBuilder sb = new();
            sb.EmitDynamicCall(contract, "getTransactionScript");

            var tx = BuildTransaction(UInt160.Zero, sb.ToArray());

            tx.NetworkFee = 200;
            var engine = new TestEngine(TriggerType.Application, tx, new TestDataCache());

            engine.Snapshot.ContractAdd(new ContractState()
            {
                Hash     = contract,
                Nef      = _engine.Nef,
                Manifest = ContractManifest.FromJson(_engine.Manifest),
            });
            engine.LoadScript(sb.ToArray());

            Assert.AreEqual(VMState.HALT, engine.Execute());
            Assert.AreEqual(1, engine.ResultStack.Count);

            var item = engine.ResultStack.Pop();

            Assert.IsInstanceOfType(item, typeof(ByteString));
            Assert.AreEqual(tx.Script.ToHexString(), item.GetSpan().ToHexString());
        }
Esempio n. 4
0
        public static ContractState FromJson(JObject json)
        {
            ContractState contractState = new ContractState();

            contractState.Script   = Convert.FromBase64String(json["script"].AsString());
            contractState.Manifest = ContractManifest.FromJson(json["manifest"]);
            return(contractState);
        }
Esempio n. 5
0
        public static ContractState FromJson(JObject json)
        {
            ContractState contractState = new ContractState();

            contractState.Script   = json["script"].AsString().HexToBytes();
            contractState.Manifest = ContractManifest.FromJson(json["manifest"]);
            return(contractState);
        }
        public void Test_CreateCallDestroy()
        {
            // Create

            var script   = _engine.Build("./TestClasses/Contract_Create.cs");
            var manifest = ContractManifest.FromJson(JObject.Parse(script.finalManifest));
            var nef      = new NefFile()
            {
                Script = script.finalNEFScript, Compiler = "unit-test", Version = "1.0"
            };
            var hash = Helper.GetContractHash((_engine.ScriptContainer as Transaction).Sender, nef.Script);

            nef.CheckSum = NefFile.ComputeChecksum(nef);

            // Create

            _engine.Reset();
            var result = _engine.ExecuteTestCaseStandard("create", nef.ToArray(), manifest.ToJson().ToString());

            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            var item = result.Pop();

            Assert.IsInstanceOfType(item, typeof(Array));
            var itemArray = item as Array;

            Assert.AreEqual(1, itemArray[0].GetInteger());                  // Id
            Assert.AreEqual(0, itemArray[1].GetInteger());                  // UpdateCounter
            Assert.AreEqual(hash.ToArray(), itemArray[2]);                  // Hash
            Assert.AreEqual(script.finalNEFScript, itemArray[3]);           // Script
            Assert.AreEqual(manifest.ToString(), itemArray[4].GetString()); // Manifest

            // Call

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", hash.ToArray(), "oldContract", new Array());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(123, item.GetInteger());

            // Destroy

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("destroy");
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Check again for failures

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", hash.ToArray());
            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);
        }
Esempio n. 7
0
 public static ContractState ContractStateFromJson(JObject json)
 {
     return(new ContractState
     {
         Id = (int)json["id"].AsNumber(),
         Script = Convert.FromBase64String(json["script"].AsString()),
         Manifest = ContractManifest.FromJson(json["manifest"])
     });
 }
        public void Test_CreateCallDestroy()
        {
            // Create

            var script   = _engine.Build("./TestClasses/Contract_Create.cs");
            var manifest = ContractManifest.FromJson(JObject.Parse(script.finalManifest));

            // Check first

            _engine.Reset();
            var result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", new Array());

            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Create

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("create", script.finalNEF, manifest.ToJson().ToString());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            var item = result.Pop();

            Assert.IsInstanceOfType(item, typeof(Array));
            var itemArray = item as Array;

            Assert.AreEqual(script.finalNEF, itemArray[0]);                 // Script
            Assert.AreEqual(manifest.ToString(), itemArray[1].GetString()); // Manifest
            Assert.AreEqual(false, itemArray[2]);                           // HasStorage
            Assert.AreEqual(false, itemArray[3]);                           // Payable

            // Call

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", new Array());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(123, item.GetInteger());

            // Destroy

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("destroy");
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Check again for failures

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray());
            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);
        }
Esempio n. 9
0
 public static RpcNativeContract FromJson(JObject json)
 {
     return(new RpcNativeContract
     {
         Id = (int)json["id"].AsNumber(),
         Hash = UInt160.Parse(json["hash"].AsString()),
         Nef = RpcNefFile.FromJson(json["nef"]),
         Manifest = ContractManifest.FromJson(json["manifest"]),
         ActiveBlockIndex = (uint)(json["activeblockindex"]?.AsNumber() ?? 0)
     });
 }
Esempio n. 10
0
        public void Test_CreateCallDestroy()
        {
            // Create

            var script   = _engine.Build("./TestClasses/Contract_Create.cs");
            var manifest = ContractManifest.FromJson(JObject.Parse(script.finalManifest));

            // Check first

            _engine.Reset();
            var result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", new Array());

            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Create

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("create", script.finalNEF, manifest.ToJson().ToString());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            var item = result.Pop();

            Assert.IsTrue(item.Type == VM.Types.StackItemType.InteropInterface);
            var ledger = (item as InteropInterface).GetInterface <Ledger.ContractState>();

            Assert.AreEqual(manifest.Hash, ledger.ScriptHash);

            // Call

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", new Array());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(123, item.GetBigInteger());

            // Destroy

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("destroy");
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Check again for failures

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray());
            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);
        }
Esempio n. 11
0
 public static ContractState ContractStateFromJson(JObject json)
 {
     return(new ContractState
     {
         Id = (int)json["id"].AsNumber(),
         UpdateCounter = (ushort)json["updatecounter"].AsNumber(),
         Hash = UInt160.Parse(json["hash"].AsString()),
         Nef = RpcNefFile.FromJson(json["nef"]),
         Manifest = ContractManifest.FromJson(json["manifest"])
     });
 }
Esempio n. 12
0
 public static RpcNativeContract FromJson(JObject json)
 {
     return(new RpcNativeContract
     {
         Id = (int)json["id"].AsNumber(),
         Hash = UInt160.Parse(json["hash"].AsString()),
         Nef = RpcNefFile.FromJson(json["nef"]),
         Manifest = ContractManifest.FromJson(json["manifest"]),
         UpdateHistory = json["updatehistory"].GetArray().Select(u => (uint)u.GetInt32()).ToArray()
     });
 }
Esempio n. 13
0
    public static RpcContractState FromJson(JObject json)
    {
        RpcContractState state = new RpcContractState();

        state.ContractState = new ContractState
        {
            Id       = (int)json["id"].AsNumber(),
            Script   = Convert.FromBase64String(json["script"].AsString()),
            Manifest = ContractManifest.FromJson(json["manifest"])
        };
        return(state);
    }
        public void Init()
        {
            var hash = UInt160.Parse("0102030405060708090A0102030405060708090A");

            _engine = new TestEngine();
            _engine.Snapshot.Contracts.Add(hash, new Ledger.ContractState()
            {
                Script   = _engine.Build("./TestClasses/Contract1.cs").finalNEF,
                Manifest = ContractManifest.FromJson(JObject.Parse(_engine.Build("./TestClasses/Contract1.cs").finalManifest)),
            });

            //will ContractCall 0102030405060708090A0102030405060708090A
            _engine.AddEntryScript("./TestClasses/Contract_ContractCall.cs");
        }
Esempio n. 15
0
        public async Task <IReadOnlyList <(UInt160 hash, ContractManifest manifest)> > ListContractsAsync()
        {
            var json = await rpcClient.RpcSendAsync("expresslistcontracts").ConfigureAwait(false);

            if (json != null && json is Neo.IO.Json.JArray array)
            {
                return(array
                       .Select(j => (
                                   UInt160.Parse(j["hash"].AsString()),
                                   ContractManifest.FromJson(j["manifest"])))
                       .ToList());
            }

            return(Array.Empty <(UInt160 hash, ContractManifest manifest)>());
        }
        public void Init()
        {
            var hash = UInt160.Parse("0102030405060708090A0102030405060708090A");

            _engine = new TestEngine(snapshot: new TestDataCache());
            _engine.Snapshot.ContractAdd(new ContractState()
            {
                Hash     = hash,
                Nef      = _engine.Build("./TestClasses/Contract1.cs").nefFile,
                Manifest = ContractManifest.FromJson(JObject.Parse(_engine.Build("./TestClasses/Contract1.cs").finalManifest)),
            });

            // will ContractCall 0102030405060708090A0102030405060708090A
            _engine.AddEntryScript("./TestClasses/Contract_ContractCall.cs");
        }
Esempio n. 17
0
        public void Init()
        {
            var _        = TestBlockchain.TheNeoSystem;
            var snapshot = new TestDataCache(null);

            _engine = new TestEngine(TriggerType.Application, snapshot: snapshot);
            Assert.IsTrue(_engine.AddEntryScript("./TestClasses/Contract_StdLib.cs").Success);
            scriptHash = _engine.Nef.Script.ToScriptHash();

            snapshot.ContractAdd(new ContractState()
            {
                Hash     = scriptHash,
                Nef      = _engine.Nef,
                Manifest = ContractManifest.FromJson(_engine.Manifest)
            });
        }
        public void Init()
        {
            var snapshot = new TestDataCache();
            var hash     = UInt160.Parse("0102030405060708090A0102030405060708090A");

            _engine = new TestEngine(snapshot: snapshot);
            _engine.AddEntryScript("./TestClasses/Contract1.cs");
            snapshot.ContractAdd(new ContractState()
            {
                Hash     = hash,
                Nef      = _engine.Nef,
                Manifest = ContractManifest.FromJson(_engine.Manifest),
            });

            // will ContractCall 0102030405060708090A0102030405060708090A
            _engine.AddEntryScript("./TestClasses/Contract_ContractCall.cs");
        }
Esempio n. 19
0
        public void Test_InvocationCounter()
        {
            // Build script

            _engine.Reset();

            var contract = _engine.EntryScriptHash;

            _engine.Snapshot.Contracts.Add(contract, new Ledger.ContractState()
            {
                Script   = _engine.InvocationStack.Peek().Script,
                Manifest = ContractManifest.FromJson(JObject.Parse(_engine.Build("./TestClasses/Contract_Runtime.cs").finalManifest)),
            });

            _engine.InvocationStack.Clear();

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                // First
                sb.EmitAppCall(contract, "getInvocationCounter");
                // Second
                sb.EmitAppCall(contract, "getInvocationCounter");

                _engine.LoadScript(sb.ToArray());
            }

            // Check

            Assert.AreEqual(VMState.HALT, _engine.Execute());
            Assert.AreEqual(2, _engine.ResultStack.Count);

            var item = _engine.ResultStack.Pop();

            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x02, item.GetInteger());

            item = _engine.ResultStack.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x01, item.GetInteger());
        }
        public void Test_Appcall()
        {
            var hash       = UInt160.Parse("0102030405060708090A0102030405060708090A");
            var testengine = new TestEngine();

            testengine.Snapshot.Contracts.Add(hash, new Ledger.ContractState()
            {
                //Manifest = new SmartContract.Manifest.ContractManifest(),
                Script   = testengine.Build("./TestClasses/Contract1.cs").finalNEF,
                Manifest = ContractManifest.FromJson(JObject.Parse(testengine.Build("./TestClasses/Contract1.cs").finalManifest)),
            });

            //will appcall 0102030405060708090A0102030405060708090A
            testengine.AddEntryScript("./TestClasses/Contract_appcall.cs");

            var       result     = testengine.GetMethod("testAppCall").Run().ConvertTo(StackItemType.ByteString);
            StackItem wantresult = new byte[] { 1, 2, 3, 4 };

            var bequal = wantresult.Equals(result);

            Assert.IsTrue(bequal);
        }
Esempio n. 21
0
        public void Test_InvocationCounter()
        {
            _engine.AddEntryScript("./TestClasses/Contract_Runtime.cs");

            // We need a new TestEngine because invocationCounter it's shared between them

            var contract = _engine.EntryScriptHash;
            var engine   = new TestEngine(TriggerType.Application, new DummyVerificable(), new TestDataCache());

            engine.Snapshot.ContractAdd(new ContractState()
            {
                Hash     = contract,
                Nef      = _engine.Nef,
                Manifest = ContractManifest.FromJson(_engine.Manifest),
            });

            using (ScriptBuilder sb = new())
            {
                // First
                sb.EmitDynamicCall(contract, "getInvocationCounter");
                // Second
                sb.EmitDynamicCall(contract, "getInvocationCounter");
                engine.LoadScript(sb.ToArray());
            }

            // Check

            Assert.AreEqual(VMState.HALT, engine.Execute());
            Assert.AreEqual(2, engine.ResultStack.Count);

            var item = engine.ResultStack.Pop();

            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x02, item.GetInteger());

            item = engine.ResultStack.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x01, item.GetInteger());
        }
Esempio n. 22
0
        public void Test_InvocationCounter()
        {
            // We need a new TestEngine because invocationCounter it's shared between them

            var contract = _engine.EntryScriptHash;
            var engine   = new TestEngine(TriggerType.Application, new DummyVerificable());

            engine.Snapshot.Contracts.Add(contract, new Ledger.ContractState()
            {
                Script   = _engine.InvocationStack.Peek().Script,
                Manifest = ContractManifest.FromJson(JObject.Parse(_engine.Build("./TestClasses/Contract_Runtime.cs").finalManifest)),
            });

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                // First
                sb.EmitAppCall(contract, "getInvocationCounter");
                // Second
                sb.EmitAppCall(contract, "getInvocationCounter");

                engine.LoadScript(sb.ToArray());
            }

            // Check

            Assert.AreEqual(VMState.HALT, engine.Execute());
            Assert.AreEqual(2, engine.ResultStack.Count);

            var item = engine.ResultStack.Pop();

            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x02, item.GetInteger());

            item = engine.ResultStack.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x01, item.GetInteger());
        }
Esempio n. 23
0
        public void Test_CreateCallDestroy()
        {
            // Create

            var entryScript = _engine.ScriptEntry;
            var script      = _engine.Build("./TestClasses/Contract_Create.cs");
            var manifest    = ContractManifest.FromJson(JObject.Parse(script.finalManifest));
            var nef         = new NefFile()
            {
                Script = script.finalNEFScript, Compiler = "unit-test-1.0", Tokens = System.Array.Empty <MethodToken>()
            };

            nef.CheckSum = NefFile.ComputeChecksum(nef);

            var hash = Helper.GetContractHash((_engine.ScriptContainer as Transaction).Sender, nef.CheckSum, manifest.Name);

            // Create

            _engine.Reset();
            var result = _engine.ExecuteTestCaseStandard("create", nef.ToArray(), manifest.ToJson().ToString());

            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            var item = result.Pop();

            Assert.IsInstanceOfType(item, typeof(Array));
            var itemArray = item as Array;

            Assert.AreEqual(1, itemArray[0].GetInteger());                                                                   // Id
            Assert.AreEqual(0, itemArray[1].GetInteger());                                                                   // UpdateCounter
            Assert.AreEqual(hash.ToArray(), itemArray[2]);                                                                   // Hash
            Assert.AreEqual(nef.ToJson().AsString(), itemArray[3].GetSpan().AsSerializable <NefFile>().ToJson().AsString()); // Nef
            var ritem = new ContractManifest();

            ((IInteroperable)ritem).FromStackItem(itemArray[4]);
            Assert.AreEqual(manifest.ToString(), ritem.ToString()); // Manifest

            // Call

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", hash.ToArray(), "oldContract", (byte)CallFlags.All, new Array());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(123, item.GetInteger());

            // Destroy

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("destroy", entryScript.nefFile);
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Check again for failures

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", hash.ToArray());
            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);
        }
        public void Test_CreateCallDestroy()
        {
            // Create

            TestEngine engine = new();

            engine.AddEntryScript("./TestClasses/Contract_Create.cs");
            var manifest = ContractManifest.FromJson(engine.Manifest);
            var nef      = new NefFile()
            {
                Script = engine.Nef.Script, Compiler = engine.Nef.Compiler, Source = engine.Nef.Source, Tokens = engine.Nef.Tokens
            };

            nef.CheckSum = NefFile.ComputeChecksum(nef);

            var hash = Helper.GetContractHash((_engine.ScriptContainer as Transaction).Sender, nef.CheckSum, manifest.Name);

            // Create

            _engine.Reset();
            var result = _engine.ExecuteTestCaseStandard("create", nef.ToArray(), manifest.ToJson().ToString());

            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            var item = result.Pop();

            Assert.IsInstanceOfType(item, typeof(Array));
            var itemArray = item as Array;

            Assert.AreEqual(1, itemArray[0].GetInteger());                                                                             // Id
            Assert.AreEqual(0, itemArray[1].GetInteger());                                                                             // UpdateCounter
            Assert.AreEqual(hash.ToArray(), itemArray[2]);                                                                             // Hash
            Assert.AreEqual(nef.ToJson().AsString(), itemArray[3].GetSpan().ToArray().AsSerializable <NefFile>().ToJson().AsString()); // Nef
            var ritem = new ContractManifest();

            ((IInteroperable)ritem).FromStackItem(itemArray[4]);
            Assert.AreEqual(manifest.ToString(), ritem.ToString()); // Manifest

            // Call

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", hash.ToArray(), "oldContract", (byte)CallFlags.All, new Array());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(ByteString));
            Assert.AreEqual(manifest.Name, item.GetString());

            // Destroy

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("destroy", _engine.Nef);
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Check again for failures

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", hash.ToArray());
            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);
        }
Esempio n. 25
0
        public void Test_Update()
        {
            // Create

            var script   = _engine.Build("./TestClasses/Contract_CreateAndUpdate.cs");
            var manifest = ContractManifest.FromJson(JObject.Parse(script.finalManifest));

            var scriptUpdate   = _engine.Build("./TestClasses/Contract_Update.cs");
            var manifestUpdate = ContractManifest.FromJson(JObject.Parse(scriptUpdate.finalManifest));

            // Create

            _engine.Reset();
            var result = _engine.ExecuteTestCaseStandard("create", script.finalNEF, manifest.ToJson().ToString());

            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            var item = result.Pop();

            Assert.IsInstanceOfType(item, typeof(Array));
            var itemArray = item as Array;

            Assert.AreEqual(script.finalNEF, itemArray[0]);                 // Script
            Assert.AreEqual(manifest.ToString(), itemArray[1].GetString()); // Manifest

            // Call & Update

            _engine.Reset();
            var args = new Array
            {
                scriptUpdate.finalNEF,
                manifestUpdate.ToJson().ToString()
            };

            result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", args);
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(123, item.GetInteger());

            // Call Again

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", manifestUpdate.Hash.ToArray(), "newContract", new Array());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(124, item.GetInteger());

            // Check again for failures

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", new Array());
            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);
        }