Implement CBLSLazySignature for lazy serialization/deserialization

In some cases it takes too much time to perform full deserialization of
BLS signatures in the message handler thread. Better to just read the
buffer and do the actual deserialization when the signature is needed for
the first time (which is can be in another thread).
This commit is contained in:
Alexander Block 2019-02-15 14:11:47 +01:00
parent 6e8f50aa55
commit 02b68885a0
2 changed files with 67 additions and 0 deletions

View File

@ -423,6 +423,34 @@ bool CBLSSignature::Recover(const std::vector<CBLSSignature>& sigs, const std::v
return true;
}
CBLSLazySignature::CBLSLazySignature(CBLSSignature& _sig) :
bufValid(false),
sigInitialized(true),
sig(_sig)
{
}
void CBLSLazySignature::SetSig(const CBLSSignature& _sig)
{
bufValid = false;
sigInitialized = true;
sig = _sig;
}
const CBLSSignature& CBLSLazySignature::GetSig() const
{
if (!bufValid && !sigInitialized) {
static CBLSSignature invalidSig;
return invalidSig;
}
if (!sigInitialized) {
sig.SetBuf(buf, sizeof(buf));
sigInitialized = true;
}
return sig;
}
#ifndef BUILD_BITCOIN_INTERNAL
static std::once_flag init_flag;

View File

@ -308,6 +308,45 @@ protected:
bool InternalGetBuf(void* buf) const;
};
class CBLSLazySignature
{
private:
mutable char buf[BLS_CURVE_SIG_SIZE];
mutable bool bufValid{false};
mutable CBLSSignature sig;
mutable bool sigInitialized{false};
public:
template<typename Stream>
inline void Serialize(Stream& s) const
{
if (!sigInitialized && !bufValid) {
throw std::ios_base::failure("sig and buf not initialized");
}
if (!bufValid) {
sig.GetBuf(buf, sizeof(buf));
bufValid = true;
}
s.write(buf, sizeof(buf));
}
template<typename Stream>
inline void Unserialize(Stream& s)
{
s.read(buf, sizeof(buf));
bufValid = true;
sigInitialized = false;
}
public:
CBLSLazySignature() = default;
CBLSLazySignature(CBLSSignature& _sig);
void SetSig(const CBLSSignature& _sig);
const CBLSSignature& GetSig() const;
};
typedef std::vector<CBLSId> BLSIdVector;
typedef std::vector<CBLSPublicKey> BLSVerificationVector;
typedef std::vector<CBLSPublicKey> BLSPublicKeyVector;