Skip to content

Commit

Permalink
coin_processor fix: compile regex once
Browse files Browse the repository at this point in the history
  • Loading branch information
keyliaran authored and grao1991 committed Apr 24, 2024
1 parent b6a3035 commit 3f12844
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions rust/processor/src/models/coin_models/coin_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
use anyhow::{Context, Result};
use aptos_protos::transaction::v1::{move_type::Content, MoveType, WriteResource};
use bigdecimal::BigDecimal;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use tracing::error;
Expand Down Expand Up @@ -162,15 +163,24 @@ pub struct CoinInfoType {
creator_address: String,
}

static RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"(<(.*)>)").unwrap()
});

static COIN_RESOURCES: Lazy<[String; 2]> = Lazy::new(|| {
[
format!("{}::coin::CoinInfo", COIN_ADDR),
format!("{}::coin::CoinStore", COIN_ADDR),
]
});

impl CoinInfoType {
/// get creator address from move_type, and get coin type from move_type_str
/// Since move_type_str will contain things we don't need, e.g. 0x1::coin::CoinInfo<T>. We will use
/// regex to extract T.
pub fn from_move_type(move_type: &MoveType, move_type_str: &str, txn_version: i64) -> Self {
if let Content::Struct(struct_tag) = move_type.content.as_ref().unwrap() {
let re = Regex::new(r"(<(.*)>)").unwrap();

let matched = re.captures(move_type_str).unwrap_or_else(|| {
let matched = RE.captures(move_type_str).unwrap_or_else(|| {
error!(
txn_version = txn_version,
move_type_str = move_type_str,
Expand Down Expand Up @@ -225,11 +235,7 @@ pub enum CoinResource {

impl CoinResource {
pub fn is_resource_supported(data_type: &str) -> bool {
[
format!("{}::coin::CoinInfo", COIN_ADDR),
format!("{}::coin::CoinStore", COIN_ADDR),
]
.contains(&data_type.to_string())
COIN_RESOURCES.contains(&data_type.to_string())
}

pub fn from_resource(
Expand Down

0 comments on commit 3f12844

Please sign in to comment.