Skip to content

Commit

Permalink
guardedArrayReplace fix & simple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
protinam committed Jan 22, 2018
1 parent 873e840 commit e20e8ed
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contracts/common/ArrayUtils.sol
Expand Up @@ -29,7 +29,7 @@ library ArrayUtils {
{
byte[8] memory bitmasks = [byte(2 ** 7), byte(2 ** 6), byte(2 ** 5), byte(2 ** 4), byte(2 ** 3), byte(2 ** 2), byte(2 ** 1), byte(2 ** 0)];
require(array.length == desired.length);
require(mask.length == array.length / 8);
require(mask.length >= array.length / 8);
for (uint i = 0; i < array.length; i++ ) {
/* 1-bit means value can be changed. */
bool masked = (mask[i / 8] & bitmasks[i % 8]) == 0;
Expand Down
8 changes: 8 additions & 0 deletions contracts/exchange/Exchange.sol
Expand Up @@ -105,6 +105,14 @@ contract Exchange is LazyBank, ReentrancyGuarded {
event OrderBidOn (bytes32 hash, address indexed bidder, uint amount, uint timestamp);
event OrdersMatched (Order buy, Order sell);

function guardedArrayReplace(bytes array, bytes desired, bytes mask)
public
pure
returns (bytes)
{
return ArrayUtils.guardedArrayReplace(array, desired, mask);
}

function chargeFee(address from, address to, uint amount)
internal
{
Expand Down
20 changes: 20 additions & 0 deletions test/wyvern-exchange.js
Expand Up @@ -63,6 +63,26 @@ const hashOrder = (order) => {
*/

contract('WyvernExchange', (accounts) => {
it('should allow simple array replacement', () => {
return WyvernExchange
.deployed()
.then(exchangeInstance => {
return exchangeInstance.guardedArrayReplace.call('0xff', '0x00', '0xff').then(res => {
assert.equal(res, '0x00', 'Array was not properly replaced!')
})
})
})

it('should disallow array replacement', () => {
return WyvernExchange
.deployed()
.then(exchangeInstance => {
return exchangeInstance.guardedArrayReplace.call('0xff', '0x00', '0x00').then(res => {
assert.equal(res, '0xff', 'Array replacement was not disallowed!')
})
})
})

const makeOrder = (exchange) => ({
exchange: exchange,
maker: accounts[0],
Expand Down

0 comments on commit e20e8ed

Please sign in to comment.