💡118.12x Faster Event Fetching (2023.04.30)

출금에 걸리는 시간이 더 빨라졌습니다. 출금 시, 프론트엔드 앱에서 머클 증명을 생성하기 위해 Deposit 이벤트를 가져오는 속도가 118.12배 향상되었으니까요.

📌 It's already been a week since the launch.

Hey! We launched on the 22nd this month and only a week has passed. Tonic is a protocol that implements anonymity on the Klaytn and WEMIX mainnets by allowing tokens to be sent to a new wallet with mixed information from the sending wallet.

📌 Introduction to existing implementation

  1. If you've already used Tonic, you may have had to wait quite a long time, up to 40 seconds, to withdraw tokens on the Klaytn network.

  2. This is because the withdrawal proof process uses contract.getPastEvents() to fetch all Deposit events that have occurred since the contract was deployed and create a Merkle tree proof.

  3. After the contract was deployed, there were few blocks to query and events to fetch, so the queries would have been fast. However, due to the nature of the Klaytn network, which produces a new block almost every second, a query timeout issue occurs in just a few days.

  4. Tonic has been using binary search until now for improved stability. You've got a wide search range (split into safe zones), and if the node crashes, you shrink the search area by half and launch a bunch of tiny parallel search parties. It's kind of like playing a guessing game where your friend thinks of a number between 0 and 100, and you get UP/DOWN hints. You start by shouting '50!' and then maybe '75!' or '25!'. Fun, right?

  5. But this also slowed down as blocks continued to accumulate...

📌 Introduction to the new implementation

  1. Periodically (every 1-3 days), the events occurring in each Tonic (instance) contract are archived on GitHub.

  2. The browser first fetches the event list uploaded to GitHub, and then searches only for blocks that have not been archived (binary search is still used as nodes can still go down).

  3. That's it!

🥂 Results

Based on a straightforward benchmark, as of tonight, the process that previously took 25.35-43 seconds now only requires 0.214 seconds! This is an impressive improvement of approximately 118.12 times faster. 👏

📌 런칭 후 벌써 일주일이 지났어요.

안녕하세요! 22일에 토닉을 출시하고 벌써 한 주가 지났어요. 토닉은 보내는 지갑의 정보가 섞인 채로 새로운 지갑으로 토큰을 보낼 수 있게 해서, 클레이튼과 위믹스 메인넷에서 익명성을 구현하는 프로토콜입니다.

📌 기존 구현 소개

  1. 헉, 그런데 혹시 토닉을 벌써 사용해 보신 분이 있다면, 클레이튼 네트워크에서는 토큰을 출금하실 때 많게는 40초까지, 꽤 오랜 시간을 기다려야 했을 거예요.

  2. 이것은 출금의 증명 과정에서 contract.getPastEvents() 를 사용해, 해당 컨트랙트가 배포되고 난 이래로 발생한 모든 Deposit 이벤트를 가져와, 머클 트리 증명을 만들기 때문입니다.

  3. 컨트랙트가 배포되고 난 뒤에는 조회할 블록도, 가져올 이벤트도 몇 개 없기 때문에 쿼리가 금방 금방 이루어졌을 텐데요. 거의 1초에 한 꼴로 새로운 블록이 생기는 클레이튼 네트워크의 특성상, 며칠만 지나도 노드가 금방 뻗는(query timeout) 문제가 발생하는 것이죠.

  4. 노드가 개같이 뒤졌다고, 출금이 안되면 속상하겠죠? 그래서 아직까지 토닉은 이진 탐색(binary search)을 사용하고 있었어요. 간단하게 설명하자면, (안전한 값으로 나뉜) 꽤 넓은 검색 범위로 쿼리한 뒤에, 노드가 뒤지면 검색하는 블록의 범위를 절반씩 줄여가며 작은 병렬 요청을 여러개 보내는 방식입니다. 친구가 0~100 중 하나의 숫자를 생각하고 UP/DOWN으로 대답할 테니 맞춰보라고 하면, 우리가 보통 먼저 50을 불러 범위롤 좁힌 뒤에, 75나 25 정도를 다음으로 부르는 거랑 동일한 탐색법이죠.

  5. 근데 이것 역시 블록이 계속 쌓이면서 느려졌어요.

  6. 게다가 요청을 보내서 노드를 괴롭히고, 노드가 뒤져서 괴롭힘 당하는 것은 여러분의 브라우저입니다.

📌 새로운 구현 소개

  1. 주기적으로(1~3일) 토닉의 각 (인스턴스) 컨트랙트에서 발생한 이벤트들이 GitHub에 아카이빙 됩니다.

  2. 브라우저는 먼저 GitHub에 올라온 이벤트 목록을 가져온 뒤, 박제되지 않은 블록에 한해서만 검색해요(그래도 노드가 뒤질 수 있으니 이진 탐색은 계속 씁니다).

🥂 결과

긴단한 벤치마크 결과, 오늘 저녁 기준으로 25.35~43초씩 걸리던 게 0.214초 만에 이루어집니다! 118.12배쯤 빨라졌네요. 👏

Last updated