MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / NewChainWithContext

Function NewChainWithContext

blockproducer/chain.go:107–322  ·  view source on GitHub ↗

NewChainWithContext creates a new blockchain with context.

(ctx context.Context, cfg *Config)

Source from the content-addressed store, hash-verified

105
106// NewChainWithContext creates a new blockchain with context.
107func NewChainWithContext(ctx context.Context, cfg *Config) (c *Chain, err error) {
108 var (
109 ierr error
110
111 st xi.Storage
112 cache *lru.Cache
113 lastIrre *blockNode
114 heads []*blockNode
115 immutable *metaState
116 txPool map[hash.Hash]pi.Transaction
117
118 branches []*branch
119 headBranch *branch
120 headIndex int
121
122 addr proto.AccountAddress
123 bpInfos []*blockProducerInfo
124 localBPInfo *blockProducerInfo
125 )
126
127 // Verify genesis block in config
128 if cfg.Genesis == nil {
129 err = ErrNilGenesis
130 return
131 }
132 if ierr = cfg.Genesis.VerifyHash(); ierr != nil {
133 err = errors.Wrap(ierr, "failed to verify genesis block hash")
134 return
135 }
136
137 // Open storage
138 var existed bool
139 if fi, err := os.Stat(cfg.DataFile); err == nil && fi.Mode().IsRegular() {
140 existed = true
141 }
142 if st, ierr = openStorage(fmt.Sprintf("file:%s", cfg.DataFile)); ierr != nil {
143 err = errors.Wrap(ierr, "failed to open storage")
144 return
145 }
146 defer func() {
147 if err != nil {
148 st.Close()
149 }
150 }()
151
152 // Create block cache
153 if cfg.BlockCacheSize > conf.MaxCachedBlock {
154 cfg.BlockCacheSize = conf.MaxCachedBlock
155 }
156 if cfg.BlockCacheSize <= 0 {
157 cfg.BlockCacheSize = 1 // Must provide a positive size
158 }
159 if cache, err = lru.NewWithEvict(cfg.BlockCacheSize, func(key interface{}, value interface{}) {
160 if node, ok := value.(*blockNode); ok && node != nil {
161 node.clear()
162 }
163 }); err != nil {
164 return

Callers 1

NewChainFunction · 0.70

Calls 15

WithFieldsFunction · 0.92
GetLocalPublicKeyFunction · 0.92
PubKeyHashFunction · 0.92
openStorageFunction · 0.85
newMetaStateFunction · 0.85
addBlockFunction · 0.85
updateIrreversibleFunction · 0.85
storeFunction · 0.85
loadDatabaseFunction · 0.85
loadBlockFunction · 0.85
newBranchFunction · 0.85
buildBlockProducerInfosFunction · 0.85

Tested by

no test coverage detected