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

Function NewChainWithContext

sqlchain/chain.go:141–352  ·  view source on GitHub ↗

NewChainWithContext creates a new sql-chain struct with context.

(ctx context.Context, c *Config)

Source from the content-addressed store, hash-verified

139
140// NewChainWithContext creates a new sql-chain struct with context.
141func NewChainWithContext(ctx context.Context, c *Config) (chain *Chain, err error) {
142 le := log.WithField("db", c.DatabaseID)
143
144 leveldbInit.Do(func() {
145 // Open LevelDB for block and state
146 bdbFile := c.ChainFilePrefix + "-block-state.ldb"
147 blkDB, err = leveldb.OpenFile(bdbFile, &leveldbConf)
148 if err != nil {
149 err = errors.Wrapf(err, "open leveldb %s", bdbFile)
150 return
151 }
152 le.Debugf("opened chain bdb %s", bdbFile)
153
154 // Open LevelDB for ack/request/response
155 tdbFile := c.ChainFilePrefix + "-ack-req-resp.ldb"
156 txDB, err = leveldb.OpenFile(tdbFile, &leveldbConf)
157 if err != nil {
158 err = errors.Wrapf(err, "open leveldb %s", tdbFile)
159 return
160 }
161 le.Debugf("opened chain tdb %s", tdbFile)
162 })
163 if err != nil {
164 return
165 }
166
167 // Open storage
168 var strg xi.Storage
169 if strg, err = xs.NewSqlite(c.DataFile); err != nil {
170 err = errors.Wrapf(err, "open data file %s", c.DataFile)
171 return
172 }
173
174 // Cache local private key
175 var (
176 pk *asymmetric.PrivateKey
177 addr proto.AccountAddress
178 )
179 if pk, err = kms.GetLocalPrivateKey(); err != nil {
180 err = errors.Wrap(err, "failed to cache private key")
181 return
182 }
183 addr, err = crypto.PubKeyHash(pk.PubKey())
184 if err != nil {
185 err = errors.Wrap(err, "failed to generate address")
186 return
187 }
188
189 metaKeyPrefix, err := c.DatabaseID.AccountAddress()
190 if err != nil {
191 err = errors.Wrap(err, "failed to generate database meta prefix")
192 return
193 }
194
195 // Create chain state
196 chain = &Chain{
197 bi: newBlockIndex(),
198 ai: newAckIndex(),

Callers 1

NewChainFunction · 0.70

Calls 15

WithFieldFunction · 0.92
GetLocalPrivateKeyFunction · 0.92
PubKeyHashFunction · 0.92
ConcatAllFunction · 0.92
DecodeMsgPackFunction · 0.92
WithFieldsFunction · 0.92
newBlockIndexFunction · 0.85
newAckIndexFunction · 0.85
newRunTimeFunction · 0.85
keyWithSymbolToHeightFunction · 0.85
newBlockNodeExFunction · 0.85
DoMethod · 0.80

Tested by

no test coverage detected