Example #1
0
// Executes block's transactions on proxyAppConn.
// TODO: Generate a bitmap or otherwise store tx validity in state.
func (s *State) execBlockOnProxyApp(evsw *events.EventSwitch, proxyAppConn proxy.AppConn, block *types.Block) error {

	var validTxs, invalidTxs = 0, 0

	// Execute transactions and get hash
	proxyCb := func(req tmsp.Request, res tmsp.Response) {
		switch res := res.(type) {
		case tmsp.ResponseAppendTx:
			// TODO: make use of this info
			// Blocks may include invalid txs.
			// reqAppendTx := req.(tmsp.RequestAppendTx)
			if res.RetCode == tmsp.RetCodeOK {
				validTxs += 1
			} else {
				invalidTxs += 1
			}
		case tmsp.ResponseEvent:
			// TODO: some events should get stored in the blockchain.
			evsw.FireEvent(types.EventStringApp(), types.EventDataApp{res.Key, res.Data})
		}
	}
	proxyAppConn.SetResponseCallback(proxyCb)

	// Run next txs in the block and get new AppHash
	for _, tx := range block.Txs {
		proxyAppConn.AppendTxAsync(tx)
		if err := proxyAppConn.Error(); err != nil {
			return err
		}
	}
	hash, err := proxyAppConn.GetHashSync()
	if err != nil {
		log.Warn("Error computing proxyAppConn hash", "error", err)
		return err
	}
	log.Info("ExecBlock got %v valid txs and %v invalid txs", validTxs, invalidTxs)

	// Set the state's new AppHash
	s.AppHash = hash

	return nil
}