Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Benjamin Allred
crown-core
Commits
829e2173
Commit
829e2173
authored
13 years ago
by
Gavin Andresen
Browse files
Options
Download
Email Patches
Plain Diff
CHECKMULTISIG unit tests.
parent
1224a14a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/makefile.mingw
+2
-2
src/makefile.mingw
src/makefile.osx
+2
-2
src/makefile.osx
src/makefile.unix
+2
-2
src/makefile.unix
src/test/script_tests.cpp
+136
-0
src/test/script_tests.cpp
with
142 additions
and
6 deletions
+142
-6
src/makefile.mingw
View file @
829e2173
...
...
@@ -83,8 +83,8 @@ obj/nogui/%.o: %.cpp $(HEADERS)
bitcoind.exe
:
$(OBJS:obj/%=obj/nogui/%) obj/ui_res.o
g++
$(CFLAGS)
-o
$@
$(LIBPATHS)
$^
$(LIBS)
obj/test/
%.o
:
obj/
test/
%
.cpp $(HEADERS)
g++
-c
$(CFLAGS)
-o
$@
$<
obj/test/
test_bitcoin.o
:
$(wildcard
test/
*
.cpp
)
$(HEADERS)
g++
-c
$(CFLAGS)
-o
$@
test
/test_bitcoin.cpp
test_bitcoin.exe
:
obj/test/test_bitcoin.o $(filter-out obj/nogui/init.o
,
$(OBJS:obj/%=obj/nogui/%))
g++
$(CFLAGS)
-o
$@
$(LIBPATHS)
$^
$(LIBS)
...
...
This diff is collapsed.
Click to expand it.
src/makefile.osx
View file @
829e2173
...
...
@@ -76,8 +76,8 @@ obj/nogui/%.o: %.cpp $(HEADERS)
bitcoind
:
$(OBJS:obj/%=obj/nogui/%)
$(CXX)
$(CFLAGS)
-o
$@
$(LIBPATHS)
$^
$(LIBS)
obj/test/
%.o
:
test/
%
.cpp $(HEADERS)
$(CXX)
-c
$(CFLAGS)
-o
$@
$<
obj/test/
test_bitcoin.o
:
$(wildcard
test/
*
.cpp
)
$(HEADERS)
$(CXX)
-c
$(CFLAGS)
-o
$@
test
/test_bitcoin.cpp
test_bitcoin
:
obj/test/test_bitcoin.o $(filter-out obj/nogui/init.o
,
$(OBJS:obj/%=obj/nogui/%))
$(CXX)
$(CFLAGS)
-o
$@
$(LIBPATHS)
$^
$(LIBS)
-lboost_unit_test_framework
...
...
This diff is collapsed.
Click to expand it.
src/makefile.unix
View file @
829e2173
...
...
@@ -77,8 +77,8 @@ obj/nogui/%.o: %.cpp $(HEADERS)
bitcoind
:
$(OBJS:obj/%=obj/nogui/%)
$(CXX)
$(CXXFLAGS)
-o
$@
$^
$(LIBS)
obj/test/
%.o
:
test/
%
.cpp $(HEADERS)
$(CXX)
-c
$(C
XX
FLAGS)
-o
$@
$<
obj/test/
test_bitcoin.o
:
$(wildcard
test/
*
.cpp
)
$(HEADERS)
$(CXX)
-c
$(CFLAGS)
-o
$@
test
/test_bitcoin.cpp
test_bitcoin
:
obj/test/test_bitcoin.o $(filter-out obj/nogui/init.o
,
$(OBJS:obj/%=obj/nogui/%))
$(CXX)
$(CXXFLAGS)
-o
$@
$(LIBPATHS)
$^
-Wl
,-Bstatic
-lboost_unit_test_framework
$(LIBS)
...
...
This diff is collapsed.
Click to expand it.
src/test/script_tests.cpp
View file @
829e2173
...
...
@@ -6,6 +6,9 @@
#include "../wallet.h"
using
namespace
std
;
extern
uint256
SignatureHash
(
CScript
scriptCode
,
const
CTransaction
&
txTo
,
unsigned
int
nIn
,
int
nHashType
);
extern
bool
VerifyScript
(
const
CScript
&
scriptSig
,
const
CScript
&
scriptPubKey
,
const
CTransaction
&
txTo
,
unsigned
int
nIn
,
int
nHashType
);
extern
bool
VerifySignature
(
const
CTransaction
&
txFrom
,
const
CTransaction
&
txTo
,
unsigned
int
nIn
,
int
nHashType
);
BOOST_AUTO_TEST_SUITE
(
script_tests
)
...
...
@@ -34,4 +37,137 @@ BOOST_AUTO_TEST_CASE(script_PushData)
BOOST_CHECK
(
pushdata4Stack
==
directStack
);
}
CScript
sign_multisig
(
CScript
scriptPubKey
,
std
::
vector
<
CKey
>
keys
,
CTransaction
transaction
)
{
uint256
hash
=
SignatureHash
(
scriptPubKey
,
transaction
,
0
,
SIGHASH_ALL
);
CScript
result
;
//
// NOTE: CHECKMULTISIG has an unfortunate bug; it requires
// one extra item on the stack, before the signatures.
// Putting OP_0 on the stack is the workaround;
// fixing the bug would mean splitting the blockchain (old
// clients would not accept new CHECKMULTISIG transactions,
// and vice-versa)
//
result
<<
OP_0
;
BOOST_FOREACH
(
CKey
key
,
keys
)
{
vector
<
unsigned
char
>
vchSig
;
BOOST_CHECK
(
key
.
Sign
(
hash
,
vchSig
));
vchSig
.
push_back
((
unsigned
char
)
SIGHASH_ALL
);
result
<<
vchSig
;
}
return
result
;
}
CScript
sign_multisig
(
CScript
scriptPubKey
,
CKey
key
,
CTransaction
transaction
)
{
std
::
vector
<
CKey
>
keys
;
keys
.
push_back
(
key
);
return
sign_multisig
(
scriptPubKey
,
keys
,
transaction
);
}
BOOST_AUTO_TEST_CASE
(
script_CHECKMULTISIG12
)
{
CKey
key1
,
key2
,
key3
;
key1
.
MakeNewKey
();
key2
.
MakeNewKey
();
key3
.
MakeNewKey
();
CScript
scriptPubKey12
;
scriptPubKey12
<<
OP_1
<<
key1
.
GetPubKey
()
<<
key2
.
GetPubKey
()
<<
OP_2
<<
OP_CHECKMULTISIG
;
CTransaction
txFrom12
;
txFrom12
.
vout
.
resize
(
1
);
txFrom12
.
vout
[
0
].
scriptPubKey
=
scriptPubKey12
;
CTransaction
txTo12
;
txTo12
.
vin
.
resize
(
1
);
txTo12
.
vout
.
resize
(
1
);
txTo12
.
vin
[
0
].
prevout
.
n
=
0
;
txTo12
.
vin
[
0
].
prevout
.
hash
=
txFrom12
.
GetHash
();
txTo12
.
vout
[
0
].
nValue
=
1
;
CScript
goodsig1
=
sign_multisig
(
scriptPubKey12
,
key1
,
txTo12
);
BOOST_CHECK
(
VerifyScript
(
goodsig1
,
scriptPubKey12
,
txTo12
,
0
,
0
));
txTo12
.
vout
[
0
].
nValue
=
2
;
BOOST_CHECK
(
!
VerifyScript
(
goodsig1
,
scriptPubKey12
,
txTo12
,
0
,
0
));
CScript
goodsig2
=
sign_multisig
(
scriptPubKey12
,
key2
,
txTo12
);
BOOST_CHECK
(
VerifyScript
(
goodsig2
,
scriptPubKey12
,
txTo12
,
0
,
0
));
CScript
badsig1
=
sign_multisig
(
scriptPubKey12
,
key3
,
txTo12
);
BOOST_CHECK
(
!
VerifyScript
(
badsig1
,
scriptPubKey12
,
txTo12
,
0
,
0
));
}
BOOST_AUTO_TEST_CASE
(
script_CHECKMULTISIG23
)
{
CKey
key1
,
key2
,
key3
,
key4
;
key1
.
MakeNewKey
();
key2
.
MakeNewKey
();
key3
.
MakeNewKey
();
key4
.
MakeNewKey
();
CScript
scriptPubKey23
;
scriptPubKey23
<<
OP_2
<<
key1
.
GetPubKey
()
<<
key2
.
GetPubKey
()
<<
key3
.
GetPubKey
()
<<
OP_3
<<
OP_CHECKMULTISIG
;
CTransaction
txFrom23
;
txFrom23
.
vout
.
resize
(
1
);
txFrom23
.
vout
[
0
].
scriptPubKey
=
scriptPubKey23
;
CTransaction
txTo23
;
txTo23
.
vin
.
resize
(
1
);
txTo23
.
vout
.
resize
(
1
);
txTo23
.
vin
[
0
].
prevout
.
n
=
0
;
txTo23
.
vin
[
0
].
prevout
.
hash
=
txFrom23
.
GetHash
();
txTo23
.
vout
[
0
].
nValue
=
1
;
std
::
vector
<
CKey
>
keys
;
keys
.
push_back
(
key1
);
keys
.
push_back
(
key2
);
CScript
goodsig1
=
sign_multisig
(
scriptPubKey23
,
keys
,
txTo23
);
BOOST_CHECK
(
VerifyScript
(
goodsig1
,
scriptPubKey23
,
txTo23
,
0
,
0
));
keys
.
clear
();
keys
.
push_back
(
key1
);
keys
.
push_back
(
key3
);
CScript
goodsig2
=
sign_multisig
(
scriptPubKey23
,
keys
,
txTo23
);
BOOST_CHECK
(
VerifyScript
(
goodsig2
,
scriptPubKey23
,
txTo23
,
0
,
0
));
keys
.
clear
();
keys
.
push_back
(
key2
);
keys
.
push_back
(
key3
);
CScript
goodsig3
=
sign_multisig
(
scriptPubKey23
,
keys
,
txTo23
);
BOOST_CHECK
(
VerifyScript
(
goodsig3
,
scriptPubKey23
,
txTo23
,
0
,
0
));
keys
.
clear
();
keys
.
push_back
(
key2
);
keys
.
push_back
(
key2
);
// Can't re-use sig
CScript
badsig1
=
sign_multisig
(
scriptPubKey23
,
keys
,
txTo23
);
BOOST_CHECK
(
!
VerifyScript
(
badsig1
,
scriptPubKey23
,
txTo23
,
0
,
0
));
keys
.
clear
();
keys
.
push_back
(
key2
);
keys
.
push_back
(
key1
);
// sigs must be in correct order
CScript
badsig2
=
sign_multisig
(
scriptPubKey23
,
keys
,
txTo23
);
BOOST_CHECK
(
!
VerifyScript
(
badsig2
,
scriptPubKey23
,
txTo23
,
0
,
0
));
keys
.
clear
();
keys
.
push_back
(
key3
);
keys
.
push_back
(
key2
);
// sigs must be in correct order
CScript
badsig3
=
sign_multisig
(
scriptPubKey23
,
keys
,
txTo23
);
BOOST_CHECK
(
!
VerifyScript
(
badsig3
,
scriptPubKey23
,
txTo23
,
0
,
0
));
keys
.
clear
();
keys
.
push_back
(
key4
);
keys
.
push_back
(
key2
);
// sigs must match pubkeys
CScript
badsig4
=
sign_multisig
(
scriptPubKey23
,
keys
,
txTo23
);
BOOST_CHECK
(
!
VerifyScript
(
badsig4
,
scriptPubKey23
,
txTo23
,
0
,
0
));
keys
.
clear
();
keys
.
push_back
(
key1
);
keys
.
push_back
(
key4
);
// sigs must match pubkeys
CScript
badsig5
=
sign_multisig
(
scriptPubKey23
,
keys
,
txTo23
);
BOOST_CHECK
(
!
VerifyScript
(
badsig5
,
scriptPubKey23
,
txTo23
,
0
,
0
));
keys
.
clear
();
// Must have signatures
CScript
badsig6
=
sign_multisig
(
scriptPubKey23
,
keys
,
txTo23
);
BOOST_CHECK
(
!
VerifyScript
(
badsig6
,
scriptPubKey23
,
txTo23
,
0
,
0
));
}
BOOST_AUTO_TEST_SUITE_END
()
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help