[beta] Skip OOG check for simple transfers #4558 (#4560)

* Skip OOG check for simple transfers #4558

* Fix failing test
This commit is contained in:
Jaco Greeff 2017-02-15 15:45:52 +01:00 committed by GitHub
parent 948a5387b3
commit 4d4821c577
2 changed files with 111 additions and 18 deletions

View File

@ -1,4 +1,4 @@
// Copyright 2015, 2016 Parity Technologies (UK) Ltd. // Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity. // This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify // Parity is free software: you can redistribute it and/or modify
@ -21,8 +21,10 @@ import { FormattedMessage } from 'react-intl';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { txLink } from '~/3rdparty/etherscan/links'; import { txLink } from '~/3rdparty/etherscan/links';
import ShortenedHash from '../ShortenedHash'; import Warning from '~/ui/Warning';
import { DEFAULT_GAS } from '~/util/constants';
import ShortenedHash from '../ShortenedHash';
import styles from './txHash.css'; import styles from './txHash.css';
class TxHash extends Component { class TxHash extends Component {
@ -43,8 +45,59 @@ class TxHash extends Component {
state = { state = {
blockNumber: new BigNumber(0), blockNumber: new BigNumber(0),
isRecipientContract: false,
subscriptionId: null, subscriptionId: null,
transaction: null transaction: null,
transactionReceipt: null
}
componentWillMount () {
this.fetchTransaction();
}
componentWillReceiveProps (nextProps) {
const prevHash = this.props.hash;
const nextHash = nextProps.hash;
if (prevHash !== nextHash) {
this.fetchTransaction(nextProps);
}
}
/**
* Get the sent transaction data
*/
fetchTransaction (props = this.props) {
const { hash } = props;
if (!hash) {
return;
}
this.context.api.eth
.getTransactionByHash(hash)
.then((transaction) => {
this.setState({ transaction });
return this.fetchRecipientCode(transaction);
});
}
fetchRecipientCode (transaction) {
if (!transaction || !transaction.to) {
return;
}
this.context.api.eth
.getCode(transaction.to)
.then((code) => {
const isRecipientContract = code && !/^(0x)?0*$/.test(code);
this.setState({ isRecipientContract });
})
.catch((error) => {
console.error('fetchRecipientCode', error);
});
} }
componentDidMount () { componentDidMount () {
@ -73,24 +126,58 @@ class TxHash extends Component {
return ( return (
<div> <div>
{ this.renderWarning() }
<p>{ <p>{
summary summary
? hashLink ? hashLink
: <FormattedMessage : <FormattedMessage
id='ui.txHash.posted' id='ui.txHash.posted'
defaultMessage='The transaction has been posted to the network with a hash of {hashLink}' defaultMessage='The transaction has been posted to the network with a hash of {hashLink}'
values={ { hashLink } } /> values={ { hashLink } }
/>
}</p> }</p>
{ this.renderConfirmations() } { this.renderConfirmations() }
</div> </div>
); );
} }
renderWarning () {
const { isRecipientContract, transaction, transactionReceipt } = this.state;
if (!(transactionReceipt && transactionReceipt.blockNumber && transactionReceipt.blockNumber.gt(0))) {
return null;
}
const { gas, input } = transaction;
const { gasUsed = new BigNumber(0) } = transactionReceipt;
const isOog = gasUsed.gte(gas);
// Skip OOG check if a simple transaction to a non-contract account
// @see: https://github.com/ethcore/parity/issues/4550
const skipOogCheck = gasUsed.eq(DEFAULT_GAS) && (!input || input === '0x') && !isRecipientContract;
if (!isOog || skipOogCheck) {
return null;
}
return (
<Warning
warning={
<FormattedMessage
id='ui.txHash.oog'
defaultMessage='The transaction might have gone out of gas. Try again with more gas.'
/>
}
/>
);
}
renderConfirmations () { renderConfirmations () {
const { maxConfirmations } = this.props; const { maxConfirmations } = this.props;
const { blockNumber, transaction } = this.state; const { blockNumber, transactionReceipt } = this.state;
if (!(transaction && transaction.blockNumber && transaction.blockNumber.gt(0))) { if (!(transactionReceipt && transactionReceipt.blockNumber && transactionReceipt.blockNumber.gt(0))) {
return ( return (
<div className={ styles.confirm }> <div className={ styles.confirm }>
<LinearProgress <LinearProgress
@ -101,16 +188,18 @@ class TxHash extends Component {
<div className={ styles.progressinfo }> <div className={ styles.progressinfo }>
<FormattedMessage <FormattedMessage
id='ui.txHash.waiting' id='ui.txHash.waiting'
defaultMessage='waiting for confirmations' /> defaultMessage='waiting for confirmations'
/>
</div> </div>
</div> </div>
); );
} }
const confirmations = blockNumber.minus(transaction.blockNumber).plus(1); const confirmations = blockNumber.minus(transactionReceipt.blockNumber).plus(1);
const value = Math.min(confirmations.toNumber(), maxConfirmations); const value = Math.min(confirmations.toNumber(), maxConfirmations);
let count = confirmations.toFormat(0); let count = confirmations.toFormat(0);
if (confirmations.lte(maxConfirmations)) { if (confirmations.lte(maxConfirmations)) {
count = `${count}/${maxConfirmations}`; count = `${count}/${maxConfirmations}`;
} }
@ -123,7 +212,8 @@ class TxHash extends Component {
max={ maxConfirmations } max={ maxConfirmations }
value={ value } value={ value }
color='white' color='white'
mode='determinate' /> mode='determinate'
/>
<div className={ styles.progressinfo }> <div className={ styles.progressinfo }>
<abbr title={ `block #${blockNumber.toFormat(0)}` }> <abbr title={ `block #${blockNumber.toFormat(0)}` }>
<FormattedMessage <FormattedMessage
@ -132,7 +222,8 @@ class TxHash extends Component {
values={ { values={ {
count, count,
value value
} } /> } }
/>
</abbr> </abbr>
</div> </div>
</div> </div>
@ -143,21 +234,22 @@ class TxHash extends Component {
const { api } = this.context; const { api } = this.context;
const { hash } = this.props; const { hash } = this.props;
const nextState = { blockNumber };
if (error || !hash || /^(0x)?0*$/.test(hash)) { if (error || !hash || /^(0x)?0*$/.test(hash)) {
return; return this.setState(nextState);
} }
return api.eth return api.eth
.getTransactionReceipt(hash) .getTransactionReceipt(hash)
.then((transaction) => { .then((transactionReceipt) => {
this.setState({ nextState.transactionReceipt = transactionReceipt;
blockNumber,
transaction
});
}) })
.catch((error) => { .catch((error) => {
console.warn('onBlockNumber', error); console.error('onBlockNumber', error);
this.setState({ blockNumber }); })
.then(() => {
this.setState(nextState);
}); });
} }
} }

View File

@ -33,6 +33,7 @@ function createApi () {
blockNumber = new BigNumber(100); blockNumber = new BigNumber(100);
api = { api = {
eth: { eth: {
getTransactionByHash: sinon.stub().resolves({ gas: 21000 }),
getTransactionReceipt: (hash) => { getTransactionReceipt: (hash) => {
return Promise.resolve({ return Promise.resolve({
blockNumber: new BigNumber(100), blockNumber: new BigNumber(100),