Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F13115515
D3903.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Subscribers
None
D3903.diff
View Options
diff --git a/src/compressor.h b/src/compressor.h
--- a/src/compressor.h
+++ b/src/compressor.h
@@ -14,6 +14,14 @@
class CPubKey;
class CScriptID;
+bool CompressScript(const CScript &script, std::vector<uint8_t> &out);
+unsigned int GetSpecialScriptSize(unsigned int nSize);
+bool DecompressScript(CScript &script, unsigned int nSize,
+ const std::vector<uint8_t> &out);
+
+uint64_t CompressAmount(Amount nAmount);
+Amount DecompressAmount(uint64_t nAmount);
+
/**
* Compact serializer for scripts.
*
@@ -38,28 +46,12 @@
CScript &script;
-protected:
- /**
- * These check for scripts for which a special case with a shorter encoding
- * is defined. They are implemented separately from the CScript test, as
- * these test for exact byte sequence correspondences, and are more strict.
- * For example, IsToPubKey also verifies whether the public key is valid (as
- * invalid ones cannot be represented in compressed form).
- */
- bool IsToKeyID(CKeyID &hash) const;
- bool IsToScriptID(CScriptID &hash) const;
- bool IsToPubKey(CPubKey &pubkey) const;
-
- bool Compress(std::vector<uint8_t> &out) const;
- unsigned int GetSpecialSize(unsigned int nSize) const;
- bool Decompress(unsigned int nSize, const std::vector<uint8_t> &out);
-
public:
explicit CScriptCompressor(CScript &scriptIn) : script(scriptIn) {}
template <typename Stream> void Serialize(Stream &s) const {
std::vector<uint8_t> compr;
- if (Compress(compr)) {
+ if (CompressScript(script, compr)) {
s << CFlatData(compr);
return;
}
@@ -72,9 +64,9 @@
unsigned int nSize = 0;
s >> VARINT(nSize);
if (nSize < nSpecialScripts) {
- std::vector<uint8_t> vch(GetSpecialSize(nSize), 0x00);
+ std::vector<uint8_t> vch(GetSpecialScriptSize(nSize), 0x00);
s >> CFlatData(vch);
- Decompress(nSize, vch);
+ DecompressScript(script, nSize, vch);
return;
}
nSize -= nSpecialScripts;
@@ -95,9 +87,6 @@
CTxOut &txout;
public:
- static uint64_t CompressAmount(Amount nAmount);
- static Amount DecompressAmount(uint64_t nAmount);
-
explicit CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) {}
ADD_SERIALIZE_METHODS;
diff --git a/src/compressor.cpp b/src/compressor.cpp
--- a/src/compressor.cpp
+++ b/src/compressor.cpp
@@ -9,7 +9,15 @@
#include <pubkey.h>
#include <script/standard.h>
-bool CScriptCompressor::IsToKeyID(CKeyID &hash) const {
+/*
+ * These check for scripts for which a special case with a shorter encoding is
+ * defined. They are implemented separately from the CScript test, as these test
+ * for exact byte sequence correspondences, and are more strict. For example,
+ * IsToPubKey also verifies whether the public key is valid (as invalid ones
+ * cannot be represented in compressed form).
+ */
+
+static bool IsToKeyID(const CScript &script, CKeyID &hash) {
if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160 &&
script[2] == 20 && script[23] == OP_EQUALVERIFY &&
script[24] == OP_CHECKSIG) {
@@ -19,7 +27,7 @@
return false;
}
-bool CScriptCompressor::IsToScriptID(CScriptID &hash) const {
+static bool IsToScriptID(const CScript &script, CScriptID &hash) {
if (script.size() == 23 && script[0] == OP_HASH160 && script[1] == 20 &&
script[22] == OP_EQUAL) {
memcpy(&hash, &script[2], 20);
@@ -28,7 +36,7 @@
return false;
}
-bool CScriptCompressor::IsToPubKey(CPubKey &pubkey) const {
+static bool IsToPubKey(const CScript &script, CPubKey &pubkey) {
if (script.size() == 35 && script[0] == 33 && script[34] == OP_CHECKSIG &&
(script[1] == 0x02 || script[1] == 0x03)) {
pubkey.Set(&script[1], &script[34]);
@@ -43,23 +51,23 @@
return false;
}
-bool CScriptCompressor::Compress(std::vector<uint8_t> &out) const {
+bool CompressScript(const CScript &script, std::vector<uint8_t> &out) {
CKeyID keyID;
- if (IsToKeyID(keyID)) {
+ if (IsToKeyID(script, keyID)) {
out.resize(21);
out[0] = 0x00;
memcpy(&out[1], &keyID, 20);
return true;
}
CScriptID scriptID;
- if (IsToScriptID(scriptID)) {
+ if (IsToScriptID(script, scriptID)) {
out.resize(21);
out[0] = 0x01;
memcpy(&out[1], &scriptID, 20);
return true;
}
CPubKey pubkey;
- if (IsToPubKey(pubkey)) {
+ if (IsToPubKey(script, pubkey)) {
out.resize(33);
memcpy(&out[1], &pubkey[1], 32);
if (pubkey[0] == 0x02 || pubkey[0] == 0x03) {
@@ -73,7 +81,7 @@
return false;
}
-unsigned int CScriptCompressor::GetSpecialSize(unsigned int nSize) const {
+unsigned int GetSpecialScriptSize(unsigned int nSize) {
if (nSize == 0 || nSize == 1) {
return 20;
}
@@ -83,8 +91,8 @@
return 0;
}
-bool CScriptCompressor::Decompress(unsigned int nSize,
- const std::vector<uint8_t> &in) {
+bool DecompressScript(CScript &script, unsigned int nSize,
+ const std::vector<uint8_t> &in) {
switch (nSize) {
case 0x00:
script.resize(25);
@@ -138,7 +146,7 @@
// * if e==9, we only know the resulting number is not zero, so output 1 + 10*(n
// - 1) + 9
// (this is decodable, as d is in [1-9] and e is in [0-9])
-uint64_t CTxOutCompressor::CompressAmount(Amount amt) {
+uint64_t CompressAmount(Amount amt) {
uint64_t n = amt / SATOSHI;
if (n == 0) {
return 0;
@@ -158,7 +166,7 @@
}
}
-Amount CTxOutCompressor::DecompressAmount(uint64_t x) {
+Amount DecompressAmount(uint64_t x) {
// x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9
if (x == 0) {
return Amount::zero();
diff --git a/src/test/compress_tests.cpp b/src/test/compress_tests.cpp
--- a/src/test/compress_tests.cpp
+++ b/src/test/compress_tests.cpp
@@ -27,18 +27,15 @@
BOOST_FIXTURE_TEST_SUITE(compress_tests, BasicTestingSetup)
static bool TestEncode(Amount in) {
- return in == CTxOutCompressor::DecompressAmount(
- CTxOutCompressor::CompressAmount(in));
+ return in == DecompressAmount(CompressAmount(in));
}
static bool TestDecode(uint64_t in) {
- return in == CTxOutCompressor::CompressAmount(
- CTxOutCompressor::DecompressAmount(in));
+ return in == CompressAmount(DecompressAmount(in));
}
static bool TestPair(Amount dec, uint64_t enc) {
- return CTxOutCompressor::CompressAmount(dec) == enc &&
- CTxOutCompressor::DecompressAmount(enc) == dec;
+ return CompressAmount(dec) == enc && DecompressAmount(enc) == dec;
}
BOOST_AUTO_TEST_CASE(compress_amounts) {
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Mar 1, 11:17 (15 h, 57 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5187557
Default Alt Text
D3903.diff (6 KB)
Attached To
D3903: Merge #12752: [MOVEONLY] Move compressor utility functions out of class
Event Timeline
Log In to Comment