From c3bd94cc3d4c0e7e6eae84f796fcceb1e36f0ed9 Mon Sep 17 00:00:00 2001 From: wangdong Date: Sun, 22 Apr 2018 23:48:25 -0400 Subject: [PATCH 1/2] batch updating history --- contracts/LoopringProtocolImpl.sol | 47 ++++++++++++++----------- contracts/TokenTransferDelegate.sol | 5 ++- contracts/TokenTransferDelegateImpl.sol | 13 +++++-- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/contracts/LoopringProtocolImpl.sol b/contracts/LoopringProtocolImpl.sol index a8147f8d..bc52b2ad 100644 --- a/contracts/LoopringProtocolImpl.sol +++ b/contracts/LoopringProtocolImpl.sol @@ -425,44 +425,51 @@ contract LoopringProtocolImpl is LoopringProtocol { returns (uint[] memory orderInfoList) { bytes32[] memory batch = new bytes32[](ringSize * 7); // ringSize * (owner + tokenS + 4 amounts + wallet) + bytes32[] memory historyBatch = new bytes32[](ringSize * 2); // ringSize * (orderhash, fillAmount) orderInfoList = new uint[](ringSize * 6); uint p = 0; + uint q = 0; + uint r = 0; uint prevSplitB = orders[ringSize - 1].splitB; for (uint i = 0; i < ringSize; i++) { OrderState memory state = orders[i]; uint nextFillAmountS = orders[(i + 1) % ringSize].fillAmountS; // Store owner and tokenS of every order - batch[p] = bytes32(state.owner); - batch[p + 1] = bytes32(state.tokenS); + batch[p++] = bytes32(state.owner); + batch[p++] = bytes32(state.tokenS); // Store all amounts - batch[p + 2] = bytes32(state.fillAmountS - prevSplitB); - batch[p + 3] = bytes32(prevSplitB + state.splitS); - batch[p + 4] = bytes32(state.lrcReward); - batch[p + 5] = bytes32(state.lrcFeeState); - batch[p + 6] = bytes32(state.wallet); - p += 7; + batch[p++] = bytes32(state.fillAmountS - prevSplitB); + batch[p++] = bytes32(prevSplitB + state.splitS); + batch[p++] = bytes32(state.lrcReward); + batch[p++] = bytes32(state.lrcFeeState); + batch[p++] = bytes32(state.wallet); // Update fill records - if (state.buyNoMoreThanAmountB) { - delegate.addCancelledOrFilled(state.orderHash, nextFillAmountS); - } else { - delegate.addCancelledOrFilled(state.orderHash, state.fillAmountS); - } - - orderInfoList[i * 6 + 0] = uint(state.orderHash); - orderInfoList[i * 6 + 1] = state.fillAmountS; - orderInfoList[i * 6 + 2] = state.lrcReward; - orderInfoList[i * 6 + 3] = state.lrcFeeState; - orderInfoList[i * 6 + 4] = state.splitS; - orderInfoList[i * 6 + 5] = state.splitB; + // if (state.buyNoMoreThanAmountB) { + // delegate.addCancelledOrFilled(state.orderHash, nextFillAmountS); + // } else { + // delegate.addCancelledOrFilled(state.orderHash, state.fillAmountS); + // } + historyBatch[r++] = bytes32(state.orderHash); + historyBatch[r++] = + bytes32(state.buyNoMoreThanAmountB ? nextFillAmountS : state.fillAmountS); + + orderInfoList[q++] = uint(state.orderHash); + orderInfoList[q++] = state.fillAmountS; + orderInfoList[q++] = state.lrcReward; + orderInfoList[q++] = state.lrcFeeState; + orderInfoList[q++] = state.splitS; + orderInfoList[q++] = state.splitB; prevSplitB = state.splitB; } // Do all transactions + delegate.batchAddCancelledOrFilled(historyBatch); + delegate.batchTransferToken( _lrcTokenAddress, miner, diff --git a/contracts/TokenTransferDelegate.sol b/contracts/TokenTransferDelegate.sol index e62b42aa..ea6996ab 100644 --- a/contracts/TokenTransferDelegate.sol +++ b/contracts/TokenTransferDelegate.sol @@ -98,7 +98,10 @@ contract TokenTransferDelegate { external; function addCancelledOrFilled(bytes32 orderHash, uint cancelOrFillAmount) - external; + public; + + function batchAddCancelledOrFilled(bytes32[] batch) + public; function setCutoffs(uint t) external; diff --git a/contracts/TokenTransferDelegateImpl.sol b/contracts/TokenTransferDelegateImpl.sol index 23a1e5e1..8dd4fae9 100644 --- a/contracts/TokenTransferDelegateImpl.sol +++ b/contracts/TokenTransferDelegateImpl.sol @@ -151,7 +151,6 @@ contract TokenTransferDelegateImpl is TokenTransferDelegate, Claimable { // Pay token to previous order, or to miner as previous order's // margin split or/and this order's margin split. - ERC20 token = ERC20(address(batch[i + 1])); // Here batch[i + 2] has been checked not to be 0. @@ -258,11 +257,21 @@ contract TokenTransferDelegateImpl is TokenTransferDelegate, Claimable { function addCancelledOrFilled(bytes32 orderHash, uint cancelOrFillAmount) onlyAuthorized - external + public { cancelledOrFilled[orderHash] = cancelledOrFilled[orderHash].add(cancelOrFillAmount); } + function batchAddCancelledOrFilled(bytes32[] batch) + onlyAuthorized + public + { + uint j = 0; + for (uint i = 0; i < batch.length/2; i++) { + cancelledOrFilled[batch[j]] = cancelledOrFilled[batch[j++]].add(uint(batch[j++])); + } + } + function setCutoffs(uint t) onlyAuthorized external From 9c3cd19c3dd8a79e3a549943657dc1e5f11d09e4 Mon Sep 17 00:00:00 2001 From: wangdong Date: Mon, 23 Apr 2018 00:02:49 -0400 Subject: [PATCH 2/2] fix bug --- contracts/LoopringProtocolImpl.sol | 12 +++--------- contracts/TokenTransferDelegateImpl.sol | 7 ++++--- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/contracts/LoopringProtocolImpl.sol b/contracts/LoopringProtocolImpl.sol index bc52b2ad..811c33ff 100644 --- a/contracts/LoopringProtocolImpl.sol +++ b/contracts/LoopringProtocolImpl.sol @@ -447,13 +447,7 @@ contract LoopringProtocolImpl is LoopringProtocol { batch[p++] = bytes32(state.lrcFeeState); batch[p++] = bytes32(state.wallet); - // Update fill records - // if (state.buyNoMoreThanAmountB) { - // delegate.addCancelledOrFilled(state.orderHash, nextFillAmountS); - // } else { - // delegate.addCancelledOrFilled(state.orderHash, state.fillAmountS); - // } - historyBatch[r++] = bytes32(state.orderHash); + historyBatch[r++] = state.orderHash; historyBatch[r++] = bytes32(state.buyNoMoreThanAmountB ? nextFillAmountS : state.fillAmountS); @@ -466,10 +460,10 @@ contract LoopringProtocolImpl is LoopringProtocol { prevSplitB = state.splitB; } - - // Do all transactions + // Update fill records delegate.batchAddCancelledOrFilled(historyBatch); + // Do all transactions delegate.batchTransferToken( _lrcTokenAddress, miner, diff --git a/contracts/TokenTransferDelegateImpl.sol b/contracts/TokenTransferDelegateImpl.sol index 8dd4fae9..a3192e25 100644 --- a/contracts/TokenTransferDelegateImpl.sol +++ b/contracts/TokenTransferDelegateImpl.sol @@ -266,9 +266,10 @@ contract TokenTransferDelegateImpl is TokenTransferDelegate, Claimable { onlyAuthorized public { - uint j = 0; - for (uint i = 0; i < batch.length/2; i++) { - cancelledOrFilled[batch[j]] = cancelledOrFilled[batch[j++]].add(uint(batch[j++])); + require(batch.length % 2 == 0); + for (uint i = 0; i < batch.length / 2; i++) { + cancelledOrFilled[batch[i * 2]] = + cancelledOrFilled[batch[i * 2]].add(uint(batch[i * 2 + 1])); } }