Ejemplo n.º 1
0
        public void Scripts()
        {
            var tv2s = new List <TV2>();
            var json = JArray.Parse(File.ReadAllText(@"..\..\..\data\script_tests.json"));

            foreach (var r in json.Children <JToken>().Where(c => c.Count() >= 4))
            {
                if (r[0].Type == JTokenType.String)
                {
                    var sig   = r[0].Value <string>();
                    var pub   = r[1].Value <string>();
                    var flags = r[2].Value <string>();
                    var error = r[3].Value <string>();
                    tv2s.Add(new TV2(sig, pub, flags, error));
                }
            }

            var tv2sSorted = tv2s.OrderBy(tv => tv.opcodes.Length + (int)tv.opcodes.LastOrDefault() / 256.0).ToList();

            var opcodes = tv2sSorted.Select(tv => tv.keyopcode).Distinct().OrderBy(o => o).ToArray();

            var noOpcode = new List <TV2>();
            var byOpcode = new Dictionary <Opcode, List <TV2> >();

            foreach (var tv in tv2sSorted)
            {
                var o    = tv.keyopcode;
                var list = o.HasValue ? null : noOpcode;
                if (list == null && !byOpcode.TryGetValue(o.Value, out list))
                {
                    list = new List <TV2>();
                    byOpcode.Add(o.Value, list);
                }
                list.Add(tv);
            }

            var i = 0;

            foreach (var opcode in opcodes)
            {
                var list = opcode.HasValue ? byOpcode[opcode.Value] : noOpcode;
                foreach (var tv in list)
                {
                    i++;
                    var tv2 = new TV2(tv.sig, tv.pub, tv.flags, tv.error);
                    _testOutputHelper.WriteLine($"{opcode} {i}");
                    _testOutputHelper.WriteLine($"Sig: {tv.scriptSig.ToHexString()} => {tv.scriptSig}");
                    _testOutputHelper.WriteLine($"Pub: {tv.scriptPub.ToHexString()} => {tv.scriptPub}");

                    var checker = new TransactionSignatureChecker(new Transaction(), 0, Amount.Zero);
                    var ok      = ScriptInterpreter.VerifyScript(tv.scriptSig, tv.scriptPub, tv.scriptFlags, checker, out var error);

                    var correct = (ok && tv.scriptError == ScriptError.OK) || tv.scriptError == error;

                    // All test cases do not pass yet. This condition is here to make sure things don't get worse :-)
                    if (i < 900)
                    {
                        Assert.True(correct);
                    }
                }
            }
        }