From 6d28fcb6d085dca7f19b7d295b111bbacd43e3bb Mon Sep 17 00:00:00 2001 From: wangdong Date: Mon, 23 Apr 2018 00:21:45 -0400 Subject: [PATCH] batch update fill history --- contracts/LoopringProtocol.sol | 3 +- contracts/LoopringProtocolImpl.sol | 44 ++++++++++--------------- contracts/TokenTransferDelegate.sol | 5 +-- contracts/TokenTransferDelegateImpl.sol | 14 ++++++-- 4 files changed, 32 insertions(+), 34 deletions(-) diff --git a/contracts/LoopringProtocol.sol b/contracts/LoopringProtocol.sol index 8bc1d4c1..8d6323f9 100644 --- a/contracts/LoopringProtocol.sol +++ b/contracts/LoopringProtocol.sol @@ -38,8 +38,7 @@ contract LoopringProtocol { event RingMined( uint _ringIndex, - bytes32 indexed _ringHash, - address _miner, + address indexed _miner, Fill[] _fills ); diff --git a/contracts/LoopringProtocolImpl.sol b/contracts/LoopringProtocolImpl.sol index a2a86578..6f70757c 100644 --- a/contracts/LoopringProtocolImpl.sol +++ b/contracts/LoopringProtocolImpl.sol @@ -748,40 +748,32 @@ contract LoopringProtocolImpl is LoopringProtocol { private { bytes32[] memory batch = new bytes32[](ctx.ringSize * 7); + bytes32[] memory historyBatch = new bytes32[](ctx.ringSize * 2); Fill[] memory fills = new Fill[](ctx.ringSize); uint p = 0; + uint q = 0; uint prevSplitB = ctx.orders[ctx.ringSize - 1].splitB; for (uint i = 0; i < ctx.ringSize; i++) { Order memory order = ctx.orders[i]; uint nextFillAmountS = ctx.orders[(i + 1) % ctx.ringSize].fillAmountS; // Store owner and tokenS of every order - batch[p] = bytes32(order.owner); - batch[p + 1] = bytes32(order.signer); - batch[p + 2] = bytes32(order.trackerAddr); - batch[p + 3] = bytes32(order.tokenS); + batch[p++] = bytes32(order.owner); + batch[p++] = bytes32(order.signer); + batch[p++] = bytes32(order.trackerAddr); + batch[p++] = bytes32(order.tokenS); // Store all amounts - batch[p + 4] = bytes32(order.fillAmountS - prevSplitB); - batch[p + 5] = bytes32(prevSplitB + order.splitS); - batch[p + 6] = bytes32(order.lrcReward); - batch[p + 7] = bytes32(order.lrcFeeState); - batch[p + 8] = bytes32(order.wallet); - p += 9; - - // Update fill records - if (order.capByAmountB) { - ctx.delegate.addCancelledOrFilled( - order.orderHash, - nextFillAmountS - ); - } else { - ctx.delegate.addCancelledOrFilled( - order.orderHash, - order.fillAmountS - ); - } + batch[p++] = bytes32(order.fillAmountS - prevSplitB); + batch[p++] = bytes32(prevSplitB + order.splitS); + batch[p++] = bytes32(order.lrcReward); + batch[p++] = bytes32(order.lrcFeeState); + batch[p++] = bytes32(order.wallet); + + historyBatch[q++] = order.orderHash; + historyBatch[q++] = + bytes32(order.capByAmountB ? nextFillAmountS : order.fillAmountS); fills[i] = Fill( order.orderHash, @@ -795,17 +787,15 @@ contract LoopringProtocolImpl is LoopringProtocol { prevSplitB = order.splitB; } - - // Do all transactions - ctx.delegate.batchTransferToken( + ctx.delegate.batchUpdateHistoryAndTransferTokens( lrcTokenAddress, ctx.miner, + historyBatch, batch ); emit RingMined( ctx.ringIndex, - ctx.ringHash, ctx.miner, fills ); diff --git a/contracts/TokenTransferDelegate.sol b/contracts/TokenTransferDelegate.sol index b5655dbc..52d67d4d 100644 --- a/contracts/TokenTransferDelegate.sol +++ b/contracts/TokenTransferDelegate.sol @@ -81,10 +81,11 @@ contract TokenTransferDelegate { ) external; - function batchTransferToken( + function batchUpdateHistoryAndTransferTokens( address lrcTokenAddress, address minerFeeRecipient, - bytes32[] batch + bytes32[] historyBatch, + bytes32[] transferBatch ) external; diff --git a/contracts/TokenTransferDelegateImpl.sol b/contracts/TokenTransferDelegateImpl.sol index c3e73d6b..4ff8e6a4 100644 --- a/contracts/TokenTransferDelegateImpl.sol +++ b/contracts/TokenTransferDelegateImpl.sol @@ -144,19 +144,27 @@ contract TokenTransferDelegateImpl is TokenTransferDelegate, Claimable { } } - function batchTransferToken( + function batchUpdateHistoryAndTransferTokens( address lrcAddr, address miner, + bytes32[] historyBatch, bytes32[] batch ) onlyAuthorized external { - require(batch.length % 9 == 0, "invalid batch"); + // require(batch.length % 9 == 0); + // require(historyBatch.length % 2 == 0); + // require(batch.length / 9 == historyBatch.length / 2); + uint i; + for (i = 0; i < historyBatch.length / 2; i += 2) { + cancelledOrFilled[historyBatch[i]] = + cancelledOrFilled[historyBatch[i]].add(uint(historyBatch[i + 1])); + } address prevOwner = address(batch[batch.length - 9]); - for (uint i = 0; i < batch.length; i += 9) { + for (i = 0; i < batch.length; i += 9) { address owner = address(batch[i]); address signer = address(batch[i + 1]); address tracker = address(batch[i + 2]);