forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chains.go
157 lines (136 loc) · 4.27 KB
/
chains.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chainmgmt
import (
"fmt"
"os"
"path/filepath"
"sync"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
"github.com/hyperledger/fabric/core/ledger/ledgermgmt/ledgermgmttest"
"github.com/hyperledger/fabric/core/ledger/util/couchdb"
)
// ChainID is a type used for the ids for the chains for experiments
type ChainID int
func (chainID ChainID) String() string {
return fmt.Sprintf("chain-%04d", chainID)
}
// SimulationResult is a type used for simulation results
type SimulationResult []byte
// chainsMgr manages chains for experiments
type chainsMgr struct {
ledgerMgr *ledgermgmt.LedgerMgr
mgrConf *ChainMgrConf
batchConf *BatchConf
initOp chainInitOp
chainsMap map[ChainID]*Chain
wg *sync.WaitGroup
}
func newChainsMgr(mgrConf *ChainMgrConf, batchConf *BatchConf, initOp chainInitOp) *chainsMgr {
dataDir := filepath.Join(mgrConf.DataDir, "ledgersData")
ledgermgmtInitializer := ledgermgmttest.NewInitializer(dataDir)
ledgermgmtInitializer.Config.HistoryDBConfig.Enabled = true
if os.Getenv("useCouchDB") == "true" {
couchdbAddr, set := os.LookupEnv("COUCHDB_ADDR")
if !set {
panic("environment variable 'useCouchDB' is set to true but 'COUCHDB_ADDR' is not set")
}
ledgermgmtInitializer.Config.StateDBConfig.StateDatabase = "CouchDB"
ledgermgmtInitializer.Config.StateDBConfig.CouchDB = &couchdb.Config{
Address: couchdbAddr,
RedoLogPath: filepath.Join(dataDir, "couchdbRedologs"),
UserCacheSizeMBs: 500,
MaxBatchUpdateSize: 500,
}
}
ledgerMgr := ledgermgmt.NewLedgerMgr(ledgermgmtInitializer)
return &chainsMgr{ledgerMgr, mgrConf, batchConf, initOp, make(map[ChainID]*Chain), &sync.WaitGroup{}}
}
func (m *chainsMgr) createOrOpenChains() []*Chain {
numChains := m.mgrConf.NumChains
switch m.initOp {
case ChainInitOpCreate:
for i := 0; i < numChains; i++ {
chainID := ChainID(i)
ledgerID := chainID.String()
gb, err := test.MakeGenesisBlock(ledgerID)
panicOnError(err)
peerLedger, err := m.ledgerMgr.CreateLedger(ledgerID, gb)
panicOnError(err)
c := newChain(chainID, peerLedger, m)
m.chainsMap[chainID] = c
}
case ChainInitOpOpen:
for i := 0; i < numChains; i++ {
chainID := ChainID(i)
peerLedger, err := m.ledgerMgr.OpenLedger(chainID.String())
panicOnError(err)
c := newChain(chainID, peerLedger, m)
m.chainsMap[chainID] = c
}
default:
panic(fmt.Errorf("unknown chain init opeartion"))
}
return m.chains()
}
func (m *chainsMgr) chains() []*Chain {
chains := []*Chain{}
for _, chain := range m.chainsMap {
chains = append(chains, chain)
}
return chains
}
func (m *chainsMgr) waitForChainsToExhaustAllBlocks() {
m.wg.Wait()
m.ledgerMgr.Close()
}
// Chain embeds ledger.PeerLedger and the experiments invoke ledger functions via a chain type
type Chain struct {
ledger.PeerLedger
ID ChainID
blkGenerator *blkGenerator
m *chainsMgr
}
func newChain(id ChainID, peerLedger ledger.PeerLedger, m *chainsMgr) *Chain {
bcInfo, err := peerLedger.GetBlockchainInfo()
panicOnError(err)
return &Chain{peerLedger, id, newBlkGenerator(m.batchConf, bcInfo.Height, bcInfo.CurrentBlockHash), m}
}
func (c *Chain) startBlockPollingAndCommit() {
c.m.wg.Add(1)
go func() {
defer c.close()
for {
block := c.blkGenerator.nextBlock()
if block == nil {
break
}
panicOnError(c.PeerLedger.CommitLegacy(
&ledger.BlockAndPvtData{Block: block},
&ledger.CommitOptions{},
))
}
}()
}
// SubmitTx is expected to be called by an experiment for submitting the transactions
func (c *Chain) SubmitTx(sr SimulationResult) {
c.blkGenerator.addTx(sr)
}
// Done is expected to be called by an experiment when the experiment does not have any more transactions to submit
func (c *Chain) Done() {
c.blkGenerator.close()
}
// Commit overrides the Commit function in ledger.PeerLedger because,
// experiments are not expected to call Commit directly to the ledger
func (c *Chain) Commit(block *common.Block) {
panic(fmt.Errorf("Commit should not be invoked directly"))
}
func (c *Chain) close() {
c.PeerLedger.Close()
c.m.wg.Done()
}