This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
/
rollback.go
105 lines (84 loc) · 3.27 KB
/
rollback.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package cdbblkstorage
import (
"strings"
couchdb "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/statecouchdb"
"github.com/pkg/errors"
)
// Rollback reverts changes made to the block store beyond a given block number.
func Rollback(couchInstance *couchdb.CouchInstance, internalQueryLimit int, ledgerID string, targetBlockNum uint64) error {
if err := recordHeightIfGreaterThanPreviousRecording(couchInstance, ledgerID); err != nil {
return err
}
id := strings.ToLower(ledgerID)
blockStoreDBName := couchdb.ConstructBlockchainDBName(id, blockStoreName)
blockStoreDB := couchdb.CouchDatabase{CouchInstance: couchInstance, DBName: blockStoreDBName}
blockStore := newCDBBlockStore(&blockStoreDB, nil, ledgerID)
docs, errReadDoc := readDocRange(&blockStoreDB, blockNumberToKey(targetBlockNum+1),
blockNumberToKey(blockStore.cpInfo.lastBlockNumber+1), int32(internalQueryLimit))
if errReadDoc != nil {
return errReadDoc
}
var deletedDocs []*couchdb.CouchDoc
for _, doc := range docs {
deletedDoc, err := addDeletedFlagToCouchDoc(doc.Value)
if err != nil {
return err
}
deletedDocs = append(deletedDocs, deletedDoc)
}
_, err := blockStoreDB.BatchUpdateDocuments(deletedDocs)
if err != nil {
return err
}
doc, _, err := blockStoreDB.ReadDoc(blockNumberToKey(targetBlockNum))
if err != nil {
return err
}
lastBlock, err := couchAttachmentsToBlock(doc.Attachments)
if err != nil {
return err
}
return blockStore.CheckpointBlock(lastBlock, noOp)
}
// ValidateRollbackParams performs necessary validation on the input given for
// the rollback operation.
func ValidateRollbackParams(couchInstance *couchdb.CouchInstance, ledgerID string, targetBlockNum uint64) error {
logger.Infof("Validating the rollback parameters: ledgerID [%s], block number [%d]",
ledgerID, targetBlockNum)
if err := validateLedgerID(couchInstance, ledgerID); err != nil {
return err
}
if err := validateTargetBlkNum(couchInstance, ledgerID, targetBlockNum); err != nil {
return err
}
return nil
}
func validateLedgerID(couchInstance *couchdb.CouchInstance, ledgerID string) error {
logger.Debugf("Validating the existence of ledgerID [%s]", ledgerID)
id := strings.ToLower(ledgerID)
blockStoreDBName := couchdb.ConstructBlockchainDBName(id, blockStoreName)
blockStoreDB := couchdb.CouchDatabase{CouchInstance: couchInstance, DBName: blockStoreDBName}
exists, err := blockStoreDB.Exists()
if err != nil {
return err
}
if !exists {
return errors.Errorf("ledgerID [%s] does not exist", ledgerID)
}
return nil
}
func validateTargetBlkNum(couchInstance *couchdb.CouchInstance, ledgerID string, targetBlockNum uint64) error {
logger.Debugf("Validating the given block number [%d] against the ledger block height", targetBlockNum)
id := strings.ToLower(ledgerID)
blockStoreDBName := couchdb.ConstructBlockchainDBName(id, blockStoreName)
blockStore := newCDBBlockStore(&couchdb.CouchDatabase{CouchInstance: couchInstance, DBName: blockStoreDBName}, nil, ledgerID)
if blockStore.cpInfo.lastBlockNumber <= targetBlockNum {
return errors.Errorf("target block number [%d] should be less than the biggest block number [%d]",
targetBlockNum, blockStore.cpInfo.lastBlockNumber)
}
return nil
}