-
Notifications
You must be signed in to change notification settings - Fork 2
/
shardchains.go
207 lines (188 loc) · 5.95 KB
/
shardchains.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package shardchain
import (
"math/big"
"sync"
"github.com/PositionExchange/posichain/core/state"
harmonyconfig "github.com/PositionExchange/posichain/internal/configs/harmony"
"github.com/PositionExchange/posichain/internal/shardchain/tikv_manage"
"github.com/PositionExchange/posichain/shard"
"github.com/PositionExchange/posichain/consensus/engine"
"github.com/PositionExchange/posichain/core"
"github.com/PositionExchange/posichain/core/rawdb"
"github.com/PositionExchange/posichain/core/vm"
"github.com/PositionExchange/posichain/internal/params"
"github.com/PositionExchange/posichain/internal/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/pkg/errors"
)
// Collection is a collection of per-shard blockchains.
type Collection interface {
// ShardChain returns the blockchain for the given shard,
// opening one as necessary.
ShardChain(shardID uint32, options ...core.Options) (core.BlockChain, error)
// CloseShardChain closes the given shard chain.
CloseShardChain(shardID uint32) error
// Close closes all shard chains.
Close() error
}
// CollectionImpl is the main implementation of the shard chain collection.
// See the Collection interface for details.
type CollectionImpl struct {
dbFactory DBFactory
dbInit DBInitializer
engine engine.Engine
mtx sync.Mutex
pool map[uint32]core.BlockChain
disableCache map[uint32]bool
chainConfig *params.ChainConfig
harmonyconfig *harmonyconfig.HarmonyConfig
}
// NewCollection creates and returns a new shard chain collection.
//
// dbFactory is the shard chain database factory to use.
//
// dbInit is the shard chain initializer to use when the database returned by
// the factory is brand new (empty).
func NewCollection(
harmonyconfig *harmonyconfig.HarmonyConfig,
dbFactory DBFactory, dbInit DBInitializer, engine engine.Engine,
chainConfig *params.ChainConfig,
) *CollectionImpl {
return &CollectionImpl{
harmonyconfig: harmonyconfig,
dbFactory: dbFactory,
dbInit: dbInit,
engine: engine,
pool: make(map[uint32]core.BlockChain),
disableCache: make(map[uint32]bool),
chainConfig: chainConfig,
}
}
// ShardChain returns the blockchain for the given shard,
// opening one as necessary.
func (sc *CollectionImpl) ShardChain(shardID uint32, options ...core.Options) (core.BlockChain, error) {
sc.mtx.Lock()
defer sc.mtx.Unlock()
if bc, ok := sc.pool[shardID]; ok {
return bc, nil
}
var db ethdb.Database
defer func() {
if db != nil {
db.Close()
}
}()
var err error
if db, err = sc.dbFactory.NewChainDB(shardID); err != nil {
// NewChainDB may return incompletely initialized DB;
// avoid closing it.
db = nil
return nil, errors.Wrap(err, "cannot open chain database")
}
if rawdb.ReadCanonicalHash(db, 0) == (common.Hash{}) {
utils.Logger().Info().
Uint32("shardID", shardID).
Msg("initializing a new chain database")
if err := sc.dbInit.InitChainDB(db, shardID); err != nil {
return nil, errors.Wrapf(err, "cannot initialize a new chain database")
}
}
var cacheConfig *core.CacheConfig
if sc.disableCache[shardID] {
cacheConfig = &core.CacheConfig{Disabled: true}
utils.Logger().Info().
Uint32("shardID", shardID).
Msg("disable cache, running in archival mode")
}
chainConfig := *sc.chainConfig
if shardID == shard.BeaconChainShardID {
// For beacon chain inside a shard chain, need to reset the eth chainID to shard 0's eth chainID in the config
chainConfig.EthCompatibleChainID = big.NewInt(chainConfig.EthCompatibleShard0ChainID.Int64())
}
opts := core.Options{}
if len(options) == 1 {
opts = options[0]
}
var bc core.BlockChain
if opts.EpochChain {
bc, err = core.NewEpochChain(db, &chainConfig, sc.engine, vm.Config{})
} else {
stateCache, err := initStateCache(db, sc, shardID)
if err != nil {
return nil, err
}
bc, err = core.NewBlockChainWithOptions(
db, stateCache, cacheConfig, &chainConfig, sc.engine, vm.Config{}, opts,
)
}
if err != nil {
return nil, errors.Wrapf(err, "cannot create blockchain")
}
db = nil // don't close
sc.pool[shardID] = bc
if sc.harmonyconfig != nil && sc.harmonyconfig.General.RunElasticMode {
// init the tikv mode
bc.InitTiKV(sc.harmonyconfig.TiKV)
}
return bc, nil
}
func initStateCache(db ethdb.Database, sc *CollectionImpl, shardID uint32) (state.Database, error) {
if sc.harmonyconfig != nil && sc.harmonyconfig.General.RunElasticMode {
// used for tikv mode, init state db using tikv storage
stateDB, err := tikv_manage.GetDefaultTiKVFactory().NewStateDB(shardID)
if err != nil {
return nil, err
}
return state.NewDatabaseWithCache(stateDB, 64), nil
} else {
return state.NewDatabase(db), nil
}
}
// DisableCache disables caching mode for newly opened chains.
// It does not affect already open chains. For best effect,
// use this immediately after creating collection.
func (sc *CollectionImpl) DisableCache(shardID uint32) {
if sc.disableCache == nil {
sc.disableCache = make(map[uint32]bool)
}
sc.disableCache[shardID] = true
}
// CloseShardChain closes the given shard chain.
func (sc *CollectionImpl) CloseShardChain(shardID uint32) error {
sc.mtx.Lock()
defer sc.mtx.Unlock()
bc, ok := sc.pool[shardID]
if !ok {
return errors.Errorf("shard chain not found %d", shardID)
}
utils.Logger().Info().
Uint32("shardID", shardID).
Msg("closing shard chain")
delete(sc.pool, shardID)
bc.Stop()
bc.ChainDb().Close()
utils.Logger().Info().
Uint32("shardID", shardID).
Msg("closed shard chain")
return nil
}
// Close closes all shard chains.
func (sc *CollectionImpl) Close() error {
newPool := make(map[uint32]core.BlockChain)
sc.mtx.Lock()
oldPool := sc.pool
sc.pool = newPool
sc.mtx.Unlock()
for shardID, bc := range oldPool {
utils.Logger().Info().
Uint32("shardID", shardID).
Msg("closing shard chain")
bc.Stop()
bc.ChainDb().Close()
utils.Logger().Info().
Uint32("shardID", shardID).
Msg("closed shard chain")
}
return nil
}