loadDriver 加载执行器 对单笔交易执行期间的执行器做了缓存,避免多次加载 只有参数的tx,index,和记录的当前交易,当前索引,均相等时,才返回缓存的当前交易执行器
(tx *types.Transaction, index int)
| 312 | // 对单笔交易执行期间的执行器做了缓存,避免多次加载 |
| 313 | // 只有参数的tx,index,和记录的当前交易,当前索引,均相等时,才返回缓存的当前交易执行器 |
| 314 | func (e *executor) loadDriver(tx *types.Transaction, index int) (c drivers.Driver) { |
| 315 | |
| 316 | // 交易和index都相等时,返回已缓存的当前交易执行器 |
| 317 | if e.currExecTx == tx && e.currTxIdx == index { |
| 318 | return e.currDriver |
| 319 | } |
| 320 | var err error |
| 321 | name := types.Bytes2Str(tx.Execer) |
| 322 | driver, ok := e.driverCache[name] |
| 323 | isFork := e.cfg.IsFork(e.height, "ForkCacheDriver") |
| 324 | |
| 325 | if !ok { |
| 326 | driver, err = drivers.LoadDriverWithClient(e.api, name, e.height) |
| 327 | if err != nil { |
| 328 | driver = e.loadNoneDriver() |
| 329 | } |
| 330 | e.driverCache[name] = driver |
| 331 | } |
| 332 | |
| 333 | //fork之前,多笔相同执行器的交易只有第一笔会进行Allow判定,从缓存中获取的执行器不需要进行allow判定 |
| 334 | //fork之后,所有的交易均需要单独执行Allow判定 |
| 335 | //Allow判定主要目的是在主链中, 对平行链的交易只做存证, 而不进行实际的执行逻辑 |
| 336 | //Allow判定失败, 加载none执行器, 即平行链交易被认为是存证交易类型 |
| 337 | if !ok || isFork { |
| 338 | driver.SetEnv(e.height, 0, 0) |
| 339 | err = driver.Allow(tx, index) |
| 340 | } |
| 341 | |
| 342 | // allow不通过时,统一加载none执行器 |
| 343 | if err != nil { |
| 344 | driver = e.loadNoneDriver() |
| 345 | //fork之前,cache中存放的是经过allow判定后,实际用于执行的执行器,比如主链执行平行链交易的执行器对应的是none对象 |
| 346 | //fork之后,cache中存放的是和Execer名称对应的driver对象, 如user.p.para.coins => coins |
| 347 | //fork之前的问题在于cache缓存错乱,不应该缓存实际用于执行的,即缓存包含了allow的逻辑,导致错乱 |
| 348 | //正确逻辑是,cache中的执行器对象和名称是一一对应的,保证了driver对象复用,但同时不同交易的allow需要重新判定 |
| 349 | if !isFork { |
| 350 | e.driverCache[name] = driver |
| 351 | } |
| 352 | } else { |
| 353 | driver.SetName(types.Bytes2Str(types.GetRealExecName(tx.Execer))) |
| 354 | driver.SetCurrentExecName(name) |
| 355 | } |
| 356 | e.setEnv(driver) |
| 357 | |
| 358 | //均不相等时,表明当前交易已更新,需要同步更新缓存,并记录当前交易及其index |
| 359 | if e.currExecTx != tx && e.currTxIdx != index { |
| 360 | e.currExecTx = tx |
| 361 | e.currTxIdx = index |
| 362 | e.currDriver = driver |
| 363 | } |
| 364 | return driver |
| 365 | } |
| 366 | |
| 367 | func (e *executor) execTxGroup(txs []*types.Transaction, index int) ([]*types.Receipt, error) { |
| 368 | txgroup := &types.Transactions{Txs: txs} |