Skip to content

Commit 459257d

Browse files
authored
Merge pull request #435 from keillera/ALIS-5936
ALIS-5936: Fixed a bug that unnecessary data existed.
2 parents d7935cb + 05e08d5 commit 459257d

File tree

2 files changed

+141
-49
lines changed

2 files changed

+141
-49
lines changed

src/handlers/me/wallet/token/allhistories/create/me_wallet_token_allhistories_create.py

+25-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import hashlib
66
import boto3
77
import io
8+
import csv
89
import pytz
910
from decimal import Decimal, ROUND_FLOOR
1011
from datetime import datetime
@@ -39,15 +40,16 @@ def exec_main_proc(self):
3940
# csvファイルを生成するためのdataの格納先の生成
4041
data_for_csv = io.StringIO()
4142

42-
# private chainからトークンのTransferとMintのデータを取得し、csvに書き込むためのデータを準備する
43-
self.setTransferHistoryToData(address, eoa, data_for_csv)
44-
self.setMintHistoryToData(address, eoa, data_for_csv)
45-
4643
# カストディ対応前にアドレスを保持していた場合は追記
4744
user_configuration = self.__get_user_configuration(user_id)
4845
if user_configuration is not None and user_configuration.get('old_private_eth_address') is not None:
4946
self.setTransferHistoryToData(address, user_configuration['old_private_eth_address'], data_for_csv)
5047
self.setMintHistoryToData(address, user_configuration['old_private_eth_address'], data_for_csv)
48+
tmp_sum_token = self.__get_sum_token(data_for_csv)
49+
50+
# private chainからトークンのTransferとMintのデータを取得し、csvに書き込むためのデータを準備する
51+
self.setTransferHistoryToData(address, eoa, data_for_csv)
52+
self.setMintHistoryToData(address, eoa, data_for_csv, tmp_sum_token)
5153

5254
# If the file is empty, then error will be raised
5355
if len(data_for_csv.getvalue()) == 0:
@@ -137,8 +139,9 @@ def setTransferHistoryToData(self, address, eoa, data_for_csv):
137139
transfer_result_to = tofilter.get_all_entries()
138140
self.filter_transfer_data(transfer_result_to, eoa, data_for_csv)
139141

140-
def filter_mint_data(self, mint_result, eoa, data_for_csv):
142+
def filter_mint_data(self, mint_result, eoa, data_for_csv, remove_token):
141143
# 取得したデータのうち、csvファイルに書き込むデータのみを抽出し、data_for_csvに成型して書き込む
144+
is_removed = False
142145
for i in range(len(mint_result)):
143146
time = datetime.fromtimestamp(
144147
self.web3.eth.getBlock(mint_result[i]['blockNumber'])['timestamp']).astimezone(self.jst)
@@ -151,9 +154,13 @@ def filter_mint_data(self, mint_result, eoa, data_for_csv):
151154
amountWei = int(mint_result[i]['data'], 16)
152155
content_text = strtime + ',' + transactionHash + ',' + type + ',' + str(amountEth) + ','\
153156
+ str(amountWei) + '\n'
154-
data_for_csv.write(content_text)
157+
# 移行で発生したデータは書き込み対象外とする
158+
if amountWei == remove_token and type == 'get by like' and not is_removed:
159+
is_removed = True
160+
else:
161+
data_for_csv.write(content_text)
155162

156-
def setMintHistoryToData(self, address, eoa, data_for_csv):
163+
def setMintHistoryToData(self, address, eoa, data_for_csv, remove_token=0):
157164
# topics[0]の値がMintに一致し、topics[1]の値が今回データを生成するEoAにマッチするものを取得
158165
to_filter = self.web3.eth.filter({
159166
"address": address,
@@ -166,7 +173,7 @@ def setMintHistoryToData(self, address, eoa, data_for_csv):
166173

167174
# filterしたデータをすべて取得するメソッドを実行後、必要なデータだけをcsvに書き込むためのfilterを実行する
168175
mint_result = to_filter.get_all_entries()
169-
self.filter_mint_data(mint_result, eoa, data_for_csv)
176+
self.filter_mint_data(mint_result, eoa, data_for_csv, remove_token)
170177

171178
def extract_file_to_s3(self, user_id, data_for_csv):
172179
bucket = os.environ['ALL_TOKEN_HISTORY_CSV_DOWNLOAD_S3_BUCKET']
@@ -244,3 +251,13 @@ def __get_user_configuration(self, user_id):
244251
return user_configurations_table.get_item(Key={
245252
'user_id': user_id
246253
}).get('Item')
254+
255+
def __get_sum_token(self, data_for_csv):
256+
csv_list = list(csv.reader(data_for_csv.getvalue().splitlines()))
257+
tmp_token = 0
258+
for l in csv_list:
259+
if l[2] in ['withdraw', 'pool', 'give', 'burn']:
260+
tmp_token -= int(l[4])
261+
elif l[2] in ['deposit', 'get by like', 'get from user']:
262+
tmp_token += int(l[4])
263+
return tmp_token

tests/handlers/me/wallet/token/allhistories/create/test_me_wallet_token_allhistories_create.py

+116-41
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import json
33
import boto3
4+
import re
45
from unittest import TestCase
56
from me_wallet_token_allhistories_create import MeWalletTokenAllhistoriesCreate
67
from unittest.mock import patch, MagicMock
@@ -57,7 +58,7 @@ def setUp(self):
5758
{
5859
'user_id': 'user_03',
5960
'private_eth_address': '0x1234567890123456789012345678901234567890',
60-
'old_private_eth_address': '0x1234567890123456789012345678901234567891'
61+
'old_private_eth_address': '0x1111111111111111111111111111111111111111'
6162
},
6263
]
6364
TestsUtil.create_table(self.dynamodb, os.environ['USER_CONFIGURATIONS_TABLE_NAME'], user_configurations_items)
@@ -148,10 +149,15 @@ def test_main_ok_with_old_address(self):
148149
user_util_mock.get_cognito_user_info.return_value = {
149150
'UserAttributes': [{
150151
'Name': 'custom:private_eth_address',
151-
'Value': '0x1111111111111111111111111111111111111111'
152+
'Value': '0x1234567890123456789012345678901234567890'
152153
}]
153154
}
154-
web3_eth_filter_mock.return_value = PrivateChainEthFilterFakeResponse()
155+
156+
response = PrivateChainEthFilterFakeResponse()
157+
response_mint_old = PrivateChainEthFilterFakeResponseMintOld()
158+
response_mint_new = PrivateChainEthFilterFakeResponseMintNew()
159+
web3_eth_filter_mock.side_effect = [response, response, response_mint_old,
160+
response, response, response_mint_new]
155161

156162
event = {
157163
'headers': {
@@ -185,13 +191,22 @@ def test_main_ok_with_old_address(self):
185191
Key={'user_id': 'user_03'}
186192
).get('Item')
187193

188-
notification_type = notification_table.get_item(
194+
notification_item = notification_table.get_item(
189195
Key={'notification_id': 'notification_id_randomhash'}
190-
).get('Item').get('type')
196+
).get('Item')
191197

192198
self.assertEqual(len(notification_after), len(notification_before) + 1)
193199
self.assertEqual(unread_notification_manager_after['unread'], True)
194-
self.assertEqual(notification_type, 'csvdownload')
200+
self.assertEqual(notification_item.get('type'), 'csvdownload')
201+
202+
# S3 上のデータ件数を確認
203+
s3 = boto3.client('s3', endpoint_url='http://localhost:4572/')
204+
s3_param = re.match(r'https://(.*).s3-ap-northeast-1.amazonaws.com/(.*)',
205+
notification_item.get('announce_url'))
206+
obj = s3.get_object(Bucket=s3_param.group(1), Key=s3_param.group(2))['Body'].read()
207+
# 対象となるテストデータは 16 件だが、移行時のデータ分が除かれ 15 件になっていること
208+
# ※ response_mint_new の 2件の内2件が除かれている
209+
self.assertEqual(len(obj.decode().split('\n')) - 1, 15)
195210

196211
@patch('web3.eth.Eth.getBlock',
197212
MagicMock(return_value={'timestamp': 1546268400}))
@@ -347,46 +362,106 @@ def test_ng_migration_checking(self):
347362
class PrivateChainEthFilterFakeResponse:
348363
def get_all_entries(self):
349364
eth_filter_mock_value = MagicMock()
350-
eth_filter_mock_value.side_effect = [
351-
[{'address': '0x1383B25f9ba231e3a1a1E45c0b5689d778D44AD5',
352-
'blockHash': HexBytes('0xf32e073349e4ca49c5e193b161ea1b9ba7dc9c2a9bf1271725c99bb5c690bba7'),
353-
'blockNumber': 836877, 'data': '0x0000000000000000000000000000000000000000000000000de0b6b3a7640000',
354-
'logIndex': 0,
355-
'topics': [HexBytes('0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'),
356-
HexBytes('0x0000000000000000000000001111111111111111111111111111111111111111'),
357-
HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000')],
358-
'transactionHash': HexBytes('0xf45f335a0bb17d112e870f98218ebd5159e5d7ab9f1739677d7c0b3df4879456'),
359-
'transactionIndex': 0,
360-
'transactionLogIndex': '0x0',
361-
'type': 'mined'}],
362-
[{'address': '0x1383B25f9ba231e3a1a1E45c0b5689d778D44AD5',
363-
'blockHash': HexBytes('0xf32e073349e4ca49c5e193b161ea1b9ba7dc9c2a9bf1271725c99bb5c690bba8'),
364-
'blockNumber': 836877, 'data': '0x0000000000000000000000000000000000000000000000000de0b6b3a7640000',
365-
'logIndex': 0,
366-
'topics': [HexBytes('0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'),
367-
HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000'),
368-
HexBytes('0x0000000000000000000000001111111111111111111111111111111111111111')],
369-
'transactionHash': HexBytes('0xf45f335a0bb17d112e870f98218ebd5159e5d7ab9f1739677d7c0b3df4879457'),
370-
'transactionIndex': 0,
371-
'transactionLogIndex': '0x0',
372-
'type': 'mined'}],
373-
[{'address': '0x1383B25f9ba231e3a1a1E45c0b5689d778D44AD5',
374-
'blockHash': HexBytes('0xf32e073349e4ca49c5e193b161ea1b9ba7dc9c2a9bf1271725c99bb5c690bba9'),
375-
'blockNumber': 836877, 'data': '0x0000000000000000000000000000000000000000000000000de0b6b3a7640000',
376-
'logIndex': 0,
377-
'topics': [HexBytes('0x0f6798a560793a54c3bcfe86a93cde1e73087d944c0ea20544137d4121396885'),
378-
HexBytes('0x0000000000000000000000001111111111111111111111111111111111111111'),
379-
HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000')],
380-
'transactionHash': HexBytes('0xf45f335a0bb17d112e870f98218ebd5159e5d7ab9f1739677d7c0b3df4879458'),
381-
'transactionIndex': 0,
382-
'transactionLogIndex': '0x0',
383-
'type': 'mined'}],
365+
eth_filter_mock_value.return_value = [
366+
{'address': '0x1383B25f9ba231e3a1a1E45c0b5689d778D44AD5',
367+
'blockHash': HexBytes('0xf32e073349e4ca49c5e193b161ea1b9ba7dc9c2a9bf1271725c99bb5c690bba7'),
368+
'blockNumber': 836877, 'data': '0x0000000000000000000000000000000000000000000000000de0b6b3a7640000',
369+
'logIndex': 0,
370+
'topics': [HexBytes('0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'),
371+
HexBytes('0x0000000000000000000000001111111111111111111111111111111111111111'),
372+
HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000')],
373+
'transactionHash': HexBytes('0xf45f335a0bb17d112e870f98218ebd5159e5d7ab9f1739677d7c0b3df4879456'),
374+
'transactionIndex': 0,
375+
'transactionLogIndex': '0x0',
376+
'type': 'mined'},
377+
{'address': '0x1383B25f9ba231e3a1a1E45c0b5689d778D44AD5',
378+
'blockHash': HexBytes('0xf32e073349e4ca49c5e193b161ea1b9ba7dc9c2a9bf1271725c99bb5c690bba8'),
379+
'blockNumber': 836877, 'data': '0x0000000000000000000000000000000000000000000000000de0b6b3a7640000',
380+
'logIndex': 0,
381+
'topics': [HexBytes('0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'),
382+
HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000'),
383+
HexBytes('0x0000000000000000000000001111111111111111111111111111111111111111')],
384+
'transactionHash': HexBytes('0xf45f335a0bb17d112e870f98218ebd5159e5d7ab9f1739677d7c0b3df4879457'),
385+
'transactionIndex': 0,
386+
'transactionLogIndex': '0x0',
387+
'type': 'mined'},
388+
{'address': '0x1383B25f9ba231e3a1a1E45c0b5689d778D44AD5',
389+
'blockHash': HexBytes('0xf32e073349e4ca49c5e193b161ea1b9ba7dc9c2a9bf1271725c99bb5c690bba9'),
390+
'blockNumber': 836877, 'data': '0x0000000000000000000000000000000000000000000000000de0b6b3a7640000',
391+
'logIndex': 0,
392+
'topics': [HexBytes('0x0f6798a560793a54c3bcfe86a93cde1e73087d944c0ea20544137d4121396885'),
393+
HexBytes('0x0000000000000000000000001111111111111111111111111111111111111111'),
394+
HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000')],
395+
'transactionHash': HexBytes('0xf45f335a0bb17d112e870f98218ebd5159e5d7ab9f1739677d7c0b3df4879458'),
396+
'transactionIndex': 0,
397+
'transactionLogIndex': '0x0',
398+
'type': 'mined'},
384399
]
385400
return eth_filter_mock_value()
386401

387402

388-
class PrivateChainEthFilterFakeResponseWithSeveralData:
403+
class PrivateChainEthFilterFakeResponseMintOld:
404+
def get_all_entries(self):
405+
eth_filter_mock_value = MagicMock()
406+
eth_filter_mock_value.return_value = [
407+
{'address': '0x1383B25f9ba231e3a1a1E45c0b5689d778D44AD5',
408+
'blockHash': HexBytes('0xf32e073349e4ca49c5e193b161ea1b9ba7dc9c2a9bf1271725c99bb5c690bba7'),
409+
'blockNumber': 836877, 'data': '0x00000000000000000000000000000000000000000000000029A2241AF62C0000',
410+
'logIndex': 0,
411+
'topics': [HexBytes('0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'),
412+
HexBytes('0x0000000000000000000000001111111111111111111111111111111111111111'),
413+
HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000')],
414+
'transactionHash': HexBytes('0xf45f335a0bb17d112e870f98218ebd5159e5d7ab9f1739677d7c0b3df4879456'),
415+
'transactionIndex': 0,
416+
'transactionLogIndex': '0x0',
417+
'type': 'mined'},
418+
{'address': '0x1383B25f9ba231e3a1a1E45c0b5689d778D44AD5',
419+
'blockHash': HexBytes('0xf32e073349e4ca49c5e193b161ea1b9ba7dc9c2a9bf1271725c99bb5c690bba9'),
420+
'blockNumber': 836877, 'data': '0x00000000000000000000000000000000000000000000000029A2241AF62C0000',
421+
'logIndex': 0,
422+
'topics': [HexBytes('0x0f6798a560793a54c3bcfe86a93cde1e73087d944c0ea20544137d4121396885'),
423+
HexBytes('0x0000000000000000000000001111111111111111111111111111111111111111'),
424+
HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000')],
425+
'transactionHash': HexBytes('0xf45f335a0bb17d112e870f98218ebd5159e5d7ab9f1739677d7c0b3df4879458'),
426+
'transactionIndex': 0,
427+
'transactionLogIndex': '0x0',
428+
'type': 'mined'}
429+
]
430+
return eth_filter_mock_value()
431+
389432

433+
class PrivateChainEthFilterFakeResponseMintNew:
434+
def get_all_entries(self):
435+
eth_filter_mock_value = MagicMock()
436+
# 移行対象のデータが存在する返り値を設定
437+
eth_filter_mock_value.return_value = [
438+
{'address': '0x1383B25f9ba231e3a1a1E45c0b5689d778D44AD5',
439+
'blockHash': HexBytes('0xf32e073349e4ca49c5e193b161ea1b9ba7dc9c2a9bf1271725c99bb5c690bba7'),
440+
'blockNumber': 836877, 'data': '0x0000000000000000000000000000000000000000000000003782DACE9D900000',
441+
'logIndex': 0,
442+
'topics': [HexBytes('0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'),
443+
HexBytes('0x0000000000000000000000001234567890123456789012345678901234567890'),
444+
HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000')],
445+
'transactionHash': HexBytes('0xf45f335a0bb17d112e870f98218ebd5159e5d7ab9f1739677d7c0b3df4879456'),
446+
'transactionIndex': 0,
447+
'transactionLogIndex': '0x0',
448+
'type': 'mined'},
449+
{'address': '0x1383B25f9ba231e3a1a1E45c0b5689d778D44AD5',
450+
'blockHash': HexBytes('0xf32e073349e4ca49c5e193b161ea1b9ba7dc9c2a9bf1271725c99bb5c690bba9'),
451+
'blockNumber': 836877, 'data': '0x0000000000000000000000000000000000000000000000003782DACE9D900000',
452+
'logIndex': 0,
453+
'topics': [HexBytes('0x0f6798a560793a54c3bcfe86a93cde1e73087d944c0ea20544137d4121396885'),
454+
HexBytes('0x0000000000000000000000001234567890123456789012345678901234567890'),
455+
HexBytes('0x0000000000000000000000000000000000000000000000000000000000000000')],
456+
'transactionHash': HexBytes('0xf45f335a0bb17d112e870f98218ebd5159e5d7ab9f1739677d7c0b3df4879458'),
457+
'transactionIndex': 0,
458+
'transactionLogIndex': '0x0',
459+
'type': 'mined'},
460+
]
461+
return eth_filter_mock_value()
462+
463+
464+
class PrivateChainEthFilterFakeResponseWithSeveralData:
390465
def get_all_entries(self):
391466
eth_filter_mock_value = MagicMock()
392467
eth_filter_mock_value.side_effect = [

0 commit comments

Comments
 (0)