| 1844 | } |
| 1845 | |
| 1846 | void TransactionExecutor::commit( |
| 1847 | const TwoPCParams& params, std::function<void(bcos::Error::Ptr)> callback) |
| 1848 | { |
| 1849 | EXECUTOR_NAME_LOG(TRACE) << BLOCK_NUMBER(params.number) << "Commit request"; |
| 1850 | |
| 1851 | if (!m_isRunning) |
| 1852 | { |
| 1853 | EXECUTOR_NAME_LOG(WARNING) << "TransactionExecutor is not running"; |
| 1854 | callback( |
| 1855 | BCOS_ERROR_UNIQUE_PTR(ExecuteError::STOPPED, "TransactionExecutor is not running")); |
| 1856 | return; |
| 1857 | } |
| 1858 | |
| 1859 | auto first = m_stateStorages.begin(); |
| 1860 | if (first == m_stateStorages.end()) |
| 1861 | { |
| 1862 | auto errorMessage = "Commit failed: empty stateStorages"; |
| 1863 | EXECUTOR_NAME_LOG(ERROR) << errorMessage; |
| 1864 | callback(BCOS_ERROR_PTR(INVALID_BLOCKNUMBER, errorMessage)); |
| 1865 | |
| 1866 | return; |
| 1867 | } |
| 1868 | |
| 1869 | if (first->number != params.number) |
| 1870 | { |
| 1871 | auto errorMessage = |
| 1872 | "Commit failed: Request blockNumber: " + |
| 1873 | boost::lexical_cast<std::string>(params.number) + |
| 1874 | " not equal to last blockNumber: " + boost::lexical_cast<std::string>(first->number); |
| 1875 | |
| 1876 | EXECUTOR_NAME_LOG(ERROR) << errorMessage; |
| 1877 | callback(BCOS_ERROR_PTR(INVALID_BLOCKNUMBER, errorMessage)); |
| 1878 | |
| 1879 | return; |
| 1880 | } |
| 1881 | |
| 1882 | bcos::protocol::TwoPCParams storageParams{params.number, params.primaryKey, params.timestamp}; |
| 1883 | m_backendStorage->asyncCommit(storageParams, [this, callback = std::move(callback), |
| 1884 | blockNumber = params.number]( |
| 1885 | Error::Ptr&& error, uint64_t) { |
| 1886 | if (!m_isRunning) |
| 1887 | { |
| 1888 | callback( |
| 1889 | BCOS_ERROR_UNIQUE_PTR(ExecuteError::STOPPED, "TransactionExecutor is not running")); |
| 1890 | return; |
| 1891 | } |
| 1892 | |
| 1893 | if (error) |
| 1894 | { |
| 1895 | auto errorMessage = "Commit failed: " + error->errorMessage(); |
| 1896 | |
| 1897 | EXECUTOR_NAME_LOG(ERROR) << BLOCK_NUMBER(blockNumber) << errorMessage; |
| 1898 | callback(BCOS_ERROR_WITH_PREV_PTR(ExecuteError::COMMIT_ERROR, errorMessage, *error)); |
| 1899 | return; |
| 1900 | } |
| 1901 | |
| 1902 | EXECUTOR_NAME_LOG(DEBUG) << BLOCK_NUMBER(blockNumber) << "Commit success"; |
| 1903 | |