Create _setCurrentContextAddressIfRequired function in MixinTransactions

This commit is contained in:
Amir Bandeali 2019-09-01 18:31:46 -07:00
parent 8231e7703e
commit d131c39e46
3 changed files with 35 additions and 7 deletions

View File

@ -99,11 +99,9 @@ contract MixinTransactions is
transactionHash transactionHash
); );
address signerAddress = transaction.signerAddress;
if (signerAddress != msg.sender) {
// Set the current transaction signer // Set the current transaction signer
currentContextAddress = signerAddress; address signerAddress = transaction.signerAddress;
} _setCurrentContextAddressIfRequired(signerAddress);
// Execute transaction // Execute transaction
transactionsExecuted[transactionHash] = true; transactionsExecuted[transactionHash] = true;
@ -116,9 +114,7 @@ contract MixinTransactions is
} }
// Reset current transaction signer if it was previously updated // Reset current transaction signer if it was previously updated
if (signerAddress != msg.sender) { _setCurrentContextAddressIfRequired(address(0));
currentContextAddress = address(0);
}
emit TransactionExecution(transactionHash); emit TransactionExecution(transactionHash);
@ -190,6 +186,16 @@ contract MixinTransactions is
} }
} }
/// @dev Sets the currentContextAddress if the current context is not msg.sender.
/// @param contextAddress The current context address.
function _setCurrentContextAddressIfRequired(address contextAddress)
internal
{
if (contextAddress != msg.sender) {
currentContextAddress = contextAddress;
}
}
/// @dev The current function will be called in the context of this address (either 0x transaction signer or `msg.sender`). /// @dev The current function will be called in the context of this address (either 0x transaction signer or `msg.sender`).
/// If calling a fill function, this address will represent the taker. /// If calling a fill function, this address will represent the taker.
/// If calling a cancel function, this address will represent the maker. /// If calling a cancel function, this address will represent the maker.

View File

@ -49,6 +49,12 @@ contract TestTransactions is
transactionsExecuted[hash] = true; transactionsExecuted[hash] = true;
} }
function setCurrentContextAddressIfRequired(address context)
external
{
_setCurrentContextAddressIfRequired(context);
}
function getCurrentContextAddress() function getCurrentContextAddress()
external external
view view

View File

@ -692,6 +692,22 @@ blockchainTests.resets('Transaction Unit Tests', ({ provider, web3Wrapper, txDef
}); });
}); });
describe('setCurrentContextAddressIfRequired', () => {
it('should set the currentContextAddress if not equal to sender', async () => {
const randomAddress = hexRandom(20);
await transactionsContract.setCurrentContextAddressIfRequired.awaitTransactionSuccessAsync(randomAddress);
const currentContextAddress = await transactionsContract.currentContextAddress.callAsync();
expect(currentContextAddress).to.eq(randomAddress);
});
it('should not set the currentContextAddress if equal to sender', async () => {
await transactionsContract.setCurrentContextAddressIfRequired.awaitTransactionSuccessAsync(accounts[0], {
from: accounts[0],
});
const currentContextAddress = await transactionsContract.currentContextAddress.callAsync();
expect(currentContextAddress).to.eq(constants.NULL_ADDRESS);
});
});
describe('getCurrentContext', () => { describe('getCurrentContext', () => {
it('should return the sender address when there is not a saved context address', async () => { it('should return the sender address when there is not a saved context address', async () => {
const currentContextAddress = await transactionsContract.getCurrentContextAddress.callAsync({ const currentContextAddress = await transactionsContract.getCurrentContextAddress.callAsync({