Changeset View
Changeset View
Standalone View
Standalone View
src/utxocommit.h
- This file was added.
// Copyright (c) 2017 The Bitcoin developers | |||||
// Distributed under the MIT software license, see the accompanying | |||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | |||||
#ifndef BITCOIN_UTXOCOMMIT_H | |||||
#define BITCOIN_UTXOCOMMIT_H | |||||
#include "coins.h" | |||||
#include "hash.h" | |||||
#include "secp256k1/include/secp256k1_multiset.h" | |||||
#include "streams.h" | |||||
#include <vector> | |||||
secp256k1_context *context = nullptr; | |||||
/* A Utxo Commitment | |||||
* | |||||
* This is maintained as 96-byte multiset value that uniquely defines a UTXO set | |||||
* | |||||
* It wraps the secp256k1 multiset | |||||
* | |||||
* Note that a CUtxoCommit allows "negative sets". That is | |||||
* | |||||
* CUtxoCommit set; // set is an empty set | |||||
* set.Remove(X); // set is empty set "minus" X | |||||
* set.Add(X); // set is an empty set | |||||
* | |||||
* This means a CUtxoCommit can both represent the total UTXO set, or a delta to | |||||
* the UTXO set | |||||
*/ | |||||
class CUtxoCommit { | |||||
private: | |||||
secp256k1_multiset multiset; | |||||
public: | |||||
// Constructs empty CUtxoCommit | |||||
CUtxoCommit() { secp256k1_multiset_init(context, &multiset); } | |||||
// Construct by combining two other CUtxoCommits | |||||
CUtxoCommit(const CUtxoCommit &commit1, const CUtxoCommit &commit2) | |||||
: CUtxoCommit() { | |||||
secp256k1_multiset_combine(context, &this->multiset, &commit1.multiset); | |||||
secp256k1_multiset_combine(context, &this->multiset, &commit2.multiset); | |||||
} | |||||
// Adds a TXO from multiset | |||||
void Add(const COutPoint &out, const Coin &element) { | |||||
CDataStream txo(SER_NETWORK, PROTOCOL_VERSION); | |||||
txo << out << element; | |||||
secp256k1_multiset_add(context, &multiset, (const uint8_t *)&txo[0], | |||||
txo.size()); | |||||
} | |||||
// Removes a TXO from multiset | |||||
void Remove(const COutPoint &out, const Coin &element) { | |||||
CDataStream txo(SER_NETWORK, PROTOCOL_VERSION); | |||||
txo << out << element; | |||||
secp256k1_multiset_remove(context, &multiset, (const uint8_t *)&txo[0], | |||||
txo.size()); | |||||
} | |||||
uint256 GetHash() const { | |||||
std::vector<uint8_t> hash(32); | |||||
secp256k1_multiset_finalize(context, hash.data(), &multiset); | |||||
return uint256(hash); | |||||
} | |||||
// Comparison | |||||
friend bool operator==(const CUtxoCommit &a, const CUtxoCommit &b) { | |||||
return a.GetHash() == b.GetHash(); | |||||
} | |||||
friend bool operator!=(const CUtxoCommit &a, const CUtxoCommit &b) { | |||||
return a.GetHash() != b.GetHash(); | |||||
} | |||||
}; | |||||
#endif // MULTISET_H |