-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathinterpreter_tests.cpp
More file actions
84 lines (69 loc) · 2.2 KB
/
Copy pathinterpreter_tests.cpp
File metadata and controls
84 lines (69 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) 2018-2019 The Unit-e developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <boost/test/unit_test.hpp>
#include <esperanza/vote.h>
#include <keystore.h>
#include <test/test_unite.h>
#include <script/interpreter.h>
#include <script/script.h>
#include <util.h>
BOOST_FIXTURE_TEST_SUITE(interpreter_tests, ReducedTestingSetup)
uint256 GetPrevoutHash(const CTransaction& txTo) {
CHashWriter ss(SER_GETHASH, 0);
for (const auto& txin : txTo.vin) {
ss << txin.prevout;
}
return ss.GetHash();
}
uint256 GetSequenceHash(const CTransaction& txTo) {
CHashWriter ss(SER_GETHASH, 0);
for (const auto& txin : txTo.vin) {
ss << txin.nSequence;
}
return ss.GetHash();
}
uint256 GetOutputsHash(const CTransaction& txTo) {
CHashWriter ss(SER_GETHASH, 0);
for (const auto& txout : txTo.vout) {
ss << txout;
}
return ss.GetHash();
}
BOOST_AUTO_TEST_CASE(signaturehash_vote)
{
SeedInsecureRand();
CBasicKeyStore keystore;
CKey k;
InsecureNewKey(k, true);
keystore.AddKey(k);
CPubKey pk = k.GetPubKey();
CScript prevScriptPK = CScript::CreateFinalizerCommitScript(pk);
esperanza::Vote vote{pk.GetID(), GetRandHash(), 10, 100};
CMutableTransaction tx;
tx.SetType(TxType::VOTE);
std::vector<unsigned char> voteSig;
BOOST_CHECK(k.Sign(GetRandHash(), voteSig));
CTxIn txin(GetRandHash(), 0, CScript::EncodeVote(vote, voteSig));
tx.vin.push_back(txin);
CAmount amount = 10000;
CTxOut out;
out.nValue = amount;
out.scriptPubKey = prevScriptPK;
tx.vout.push_back(out);
CHashWriter ss(SER_GETHASH, 0);
ss << tx.nVersion;
ss << GetPrevoutHash(tx);
ss << GetSequenceHash(tx);
ss << tx.vin[0].prevout;
ss << tx.vin[0].scriptSig;
ss << amount;
ss << tx.vin[0].nSequence;
ss << GetOutputsHash(tx);
ss << tx.nLockTime;
ss << (int)SIGHASH_ALL;
uint256 expectedHash = ss.GetHash();
uint256 hash = SignatureHash(prevScriptPK, tx, 0, SIGHASH_ALL, amount, SigVersion::WITNESS_V0);
BOOST_CHECK_EQUAL(hash, expectedHash);
}
BOOST_AUTO_TEST_SUITE_END()