Exemple #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(Fmt("ExecBlock got %v valid txs and %v invalid txs", validTxs, invalidTxs))

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

	return nil
}
Exemple #2
0
// Executes transactions on proxyAppCtx.
func (s *State) execBlockOnProxyApp(proxyAppCtx proxy.AppContext, block *types.Block) error {
	// Execute transactions and get hash
	var invalidTxErr error
	proxyCb := func(req tmsp.Request, res tmsp.Response) {
		switch res := res.(type) {
		case tmsp.ResponseAppendTx:
			reqAppendTx := req.(tmsp.RequestAppendTx)
			if res.RetCode != tmsp.RetCodeOK {
				if invalidTxErr == nil {
					invalidTxErr = InvalidTxError{reqAppendTx.TxBytes, res.RetCode}
				}
			}
		case tmsp.ResponseEvent:
			s.evc.FireEvent(types.EventStringApp(), types.EventDataApp{res.Key, res.Data})
		}
	}
	proxyAppCtx.SetResponseCallback(proxyCb)
	for _, tx := range block.Data.Txs {
		proxyAppCtx.AppendTxAsync(tx)
		if err := proxyAppCtx.Error(); err != nil {
			return err
		}
	}
	hash, err := proxyAppCtx.GetHashSync()
	if err != nil {
		log.Warn("Error computing proxyAppCtx hash", "error", err)
		return err
	}
	if invalidTxErr != nil {
		log.Warn("Invalid transaction in block")
		return invalidTxErr
	}

	// Check that appHash matches
	if !bytes.Equal(block.AppHash, hash) {
		log.Warn(Fmt("App hash in proposal was %X, computed %X instead", block.AppHash, hash))
		return InvalidAppHashError{block.AppHash, hash}
	}

	return nil
}