forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledger_mgmt.go
166 lines (143 loc) · 4.53 KB
/
ledger_mgmt.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ledgermgmt
import (
"errors"
"sync"
"github.com/hyperledger/fabric/core/ledger/cceventmgmt"
"fmt"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/customtx"
"github.com/hyperledger/fabric/core/ledger/kvledger"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
)
var logger = flogging.MustGetLogger("ledgermgmt")
// ErrLedgerAlreadyOpened is thrown by a CreateLedger call if a ledger with the given id is already opened
var ErrLedgerAlreadyOpened = errors.New("Ledger already opened")
// ErrLedgerMgmtNotInitialized is thrown when ledger mgmt is used before initializing this
var ErrLedgerMgmtNotInitialized = errors.New("ledger mgmt should be initialized before using")
var openedLedgers map[string]ledger.PeerLedger
var ledgerProvider ledger.PeerLedgerProvider
var lock sync.Mutex
var initialized bool
var once sync.Once
// Initialize initializes ledgermgmt
func Initialize(customTxProcessors customtx.Processors) {
once.Do(func() {
initialize(customTxProcessors)
})
}
func initialize(customTxProcessors customtx.Processors) {
logger.Info("Initializing ledger mgmt")
lock.Lock()
defer lock.Unlock()
initialized = true
openedLedgers = make(map[string]ledger.PeerLedger)
customtx.Initialize(customTxProcessors)
cceventmgmt.Initialize()
provider, err := kvledger.NewProvider()
if err != nil {
panic(fmt.Errorf("Error in instantiating ledger provider: %s", err))
}
provider.Initialize(kvLedgerStateListeners)
ledgerProvider = provider
logger.Info("ledger mgmt initialized")
}
// CreateLedger creates a new ledger with the given genesis block.
// This function guarantees that the creation of ledger and committing the genesis block would an atomic action
// The chain id retrieved from the genesis block is treated as a ledger id
func CreateLedger(genesisBlock *common.Block) (ledger.PeerLedger, error) {
lock.Lock()
defer lock.Unlock()
if !initialized {
return nil, ErrLedgerMgmtNotInitialized
}
id, err := utils.GetChainIDFromBlock(genesisBlock)
if err != nil {
return nil, err
}
logger.Infof("Creating ledger [%s] with genesis block", id)
l, err := ledgerProvider.Create(genesisBlock)
if err != nil {
return nil, err
}
l = wrapLedger(id, l)
openedLedgers[id] = l
logger.Infof("Created ledger [%s] with genesis block", id)
return l, nil
}
// OpenLedger returns a ledger for the given id
func OpenLedger(id string) (ledger.PeerLedger, error) {
logger.Infof("Opening ledger with id = %s", id)
lock.Lock()
defer lock.Unlock()
if !initialized {
return nil, ErrLedgerMgmtNotInitialized
}
l, ok := openedLedgers[id]
if ok {
return nil, ErrLedgerAlreadyOpened
}
l, err := ledgerProvider.Open(id)
if err != nil {
return nil, err
}
l = wrapLedger(id, l)
openedLedgers[id] = l
logger.Infof("Opened ledger with id = %s", id)
return l, nil
}
// GetLedgerIDs returns the ids of the ledgers created
func GetLedgerIDs() ([]string, error) {
lock.Lock()
defer lock.Unlock()
if !initialized {
return nil, ErrLedgerMgmtNotInitialized
}
return ledgerProvider.List()
}
// Close closes all the opened ledgers and any resources held for ledger management
func Close() {
logger.Infof("Closing ledger mgmt")
lock.Lock()
defer lock.Unlock()
if !initialized {
return
}
for _, l := range openedLedgers {
l.(*closableLedger).closeWithoutLock()
}
ledgerProvider.Close()
openedLedgers = nil
logger.Infof("ledger mgmt closed")
}
func wrapLedger(id string, l ledger.PeerLedger) ledger.PeerLedger {
return &closableLedger{id, l}
}
// closableLedger extends from actual validated ledger and overwrites the Close method
type closableLedger struct {
id string
ledger.PeerLedger
}
// Close closes the actual ledger and removes the entries from opened ledgers map
func (l *closableLedger) Close() {
lock.Lock()
defer lock.Unlock()
l.closeWithoutLock()
}
func (l *closableLedger) closeWithoutLock() {
l.PeerLedger.Close()
delete(openedLedgers, l.id)
}