[NOD-250] Calculate UTXOCommitment when loading DAG + add UTXOCommitment to getBlockDagInfo (#347)

This commit is contained in:
Svarog 2019-07-16 16:53:28 +03:00 committed by stasatdaglabs
parent 5ce8875ce0
commit 96842353de
6 changed files with 35 additions and 9 deletions

View File

@ -1333,6 +1333,11 @@ func (dag *BlockDAG) BlockConfirmationsByHashNoLock(hash *daghash.Hash) (uint64,
return dag.blockConfirmations(node)
}
// UTXOCommitment returns a commitment to the dag's current UTXOSet
func (dag *BlockDAG) UTXOCommitment() string {
return dag.UTXOSet().UTXOMultiset.Hash().String()
}
// blockConfirmations returns the current confirmations number of the given node
// The confirmations number is defined as follows:
// * If the node is red -> 0

View File

@ -538,7 +538,10 @@ func (dag *BlockDAG) initDAGState() error {
}
// Apply the loaded utxoCollection to the virtual block.
dag.virtual.utxoSet.utxoCollection = fullUTXOCollection
dag.virtual.utxoSet, err = newFullUTXOSetFromUTXOCollection(fullUTXOCollection)
if err != nil {
return AssertError(fmt.Sprintf("Error loading UTXOSet: %s", err))
}
// Apply the stored tips to the virtual block.
tips := newSet()

View File

@ -524,6 +524,21 @@ func NewFullUTXOSet() *FullUTXOSet {
}
}
func newFullUTXOSetFromUTXOCollection(collection utxoCollection) (*FullUTXOSet, error) {
var err error
multiset := btcec.NewMultiset(btcec.S256())
for outpoint, utxoEntry := range collection {
multiset, err = addUTXOToMultiset(multiset, utxoEntry, &outpoint)
if err != nil {
return nil, err
}
}
return &FullUTXOSet{
utxoCollection: collection,
UTXOMultiset: multiset,
}, nil
}
// diffFrom returns the difference between this utxoSet and another
// diffFrom can only work when other is a diffUTXOSet, and its base utxoSet is this.
func (fus *FullUTXOSet) diffFrom(other UTXOSet) (*UTXODiff, error) {

View File

@ -106,6 +106,7 @@ type GetBlockDAGInfoResult struct {
TipHashes []string `json:"tipHashes"`
Difficulty float64 `json:"difficulty"`
MedianTime int64 `json:"medianTime"`
UTXOCommitment string `json:"utxoCommitment"`
VerificationProgress float64 `json:"verificationProgress,omitempty"`
Pruned bool `json:"pruned"`
PruneHeight uint64 `json:"pruneHeight,omitempty"`

View File

@ -1257,14 +1257,15 @@ func handleGetBlockDAGInfo(s *Server, cmd interface{}, closeChan <-chan struct{}
dag := s.cfg.DAG
dagInfo := &btcjson.GetBlockDAGInfoResult{
DAG: params.Name,
Blocks: dag.BlockCount(),
Headers: dag.BlockCount(),
TipHashes: daghash.Strings(dag.TipHashes()),
Difficulty: getDifficultyRatio(dag.CurrentBits(), params),
MedianTime: dag.CalcPastMedianTime().Unix(),
Pruned: false,
Bip9SoftForks: make(map[string]*btcjson.Bip9SoftForkDescription),
DAG: params.Name,
Blocks: dag.BlockCount(),
Headers: dag.BlockCount(),
TipHashes: daghash.Strings(dag.TipHashes()),
Difficulty: getDifficultyRatio(dag.CurrentBits(), params),
MedianTime: dag.CalcPastMedianTime().Unix(),
UTXOCommitment: dag.UTXOCommitment(),
Pruned: false,
Bip9SoftForks: make(map[string]*btcjson.Bip9SoftForkDescription),
}
// Finally, query the BIP0009 version bits state for all currently

View File

@ -176,6 +176,7 @@ var helpDescsEnUS = map[string]string{
"getBlockDagInfoResult-tipHashes": "The block hashes for the tips in the DAG",
"getBlockDagInfoResult-difficulty": "The current chain difficulty",
"getBlockDagInfoResult-medianTime": "The median time from the PoV of the best block in the chain",
"getBlockDagInfoResult-utxoCommitment": "Commitment to the dag's UTXOSet",
"getBlockDagInfoResult-verificationProgress": "An estimate for how much of the best chain we've verified",
"getBlockDagInfoResult-pruned": "A bool that indicates if the node is pruned or not",
"getBlockDagInfoResult-pruneHeight": "The lowest block retained in the current pruned chain",