ข้ามไปยังเนื้อหาหลัก
iCute Financeสินทรัพย์ดิจิทัล · ภาพรวมตลาด
ไทย
สารบัญงานวิจัย
iCute Finance

การนำทางหลัก

การนำทางหลัก

สินทรัพย์ดิจิทัล · ภาพรวมตลาด

INIT_CODE_HASH คืออะไร และคำนวณ / ใช้งานอย่างไรในสัญญาอัจฉริยะ DeFi

Init code hash โดยพื้นฐานแล้วคือแฮช keccak256 ของ bytecode ของสัญญา การใช้งาน Init Code

Init code hash มีความจำเป็น "เนื่องจาก การคำนวณที่อยู่ขึ้นอยู่กับการแฮช init code. Init code คือโค้ดที่เมื่อถูกเรียกใช้งาน จะสร้าง runtime bytecode ที่จะถูกวางไว้ใน state และซึ่ง โดยทั่วไปถูกใช้โดยภาษา level สูงเพื่อ implement 'constructor' "EIP-1014: Skinny CREATE2," Ethereum Improvement Proposals, no. 1014, April 2018. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-1014.

การใช้งาน Init Code การใช้งานหลักของ init code คือสำหรับการขุดที่อยู่ (mining addresses) คุณสามารถดูได้ที่ https://github.com/0age/create2crunch สำหรับตัวอย่างว่าเราสามารถใช้ประโยชน์จาก CREATE2 และ init code ของสัญญาเพื่อ 'ขุด' ที่อยู่ที่มีประสิทธิภาพมากขึ้นสำหรับการ deploy สัญญาได้อย่างไร นี่คือ bash script ง่ายๆ ที่ใช้ forge เพื่อสร้าง code init hash ให้คุณ:

https://github.com/sambacha/foundry-scripts/tree/master/scripts/deploy

Script #!/usr/bin/env bash

exit_on_error() { echo "Error: $1" exit 1 }

usage() { echo "Usage: $0 <contract_path> <contract_name> [-v]" echo " <contract_path> : Path to the Solidity contract file" echo " <contract_name> : Name of the Solidity contract" echo " -v : Enable verbose output (optional)" exit 1 }

if ! command -v forge &> /dev/null; then exit_on_error "Foundry is not installed. Please install it first." fi

if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then usage fi

CONTRACT_PATH=$1 CONTRACT_NAME=$2 VERBOSE=false

if [ "$#" -eq 3 ] && [ "$3" == "-v" ]; then VERBOSE=true fi

if [ ! -f "$CONTRACT_PATH" ]; then exit_on_error "Contract path '$CONTRACT_PATH' does not exist or is not a file." fi

CONTRACT_DIR=$(dirname "$CONTRACT_PATH")

CONTRACT_FILE=$(basename "$CONTRACT_PATH")

cd "$CONTRACT_DIR" || exit_on_error "Failed to change directory to '$CONTRACT_DIR'."

echo "Compiling the contract..." if $VERBOSE; then forge build --contracts . || exit_on_error "Failed to compile the contract." else forge build --contracts . > /dev/null 2>&1 || exit_on_error "Failed to compile the contract." fi

echo "Generating init code..." INIT_CODE=$(forge inspect "$CONTRACT_NAME" bytecode) || exit_on_error "Failed to generate init code."

echo "Calculating init code hash..." INIT_CODE_HASH=$(echo -n "$INIT_CODE" | keccak-256sum | cut -d' ' -f1) || exit_on_error "Failed to calculate init code hash."# ขั้นตอนที่ 4: เขียน init code hash ลงไฟล์ OUTPUT_FILE="${CONTRACT_NAME}_INIT_CODE_HASH.txt" echo "$INIT_CODE_HASH" > "$OUTPUT_FILE" || exit_on_error "ไม่สามารถเขียน init code hash ลงไฟล์ได้"

echo "Init code hash ถูกเขียนไปยัง $OUTPUT_FILE แล้ว" if $VERBOSE; then echo "Init code: $INIT_CODE" echo "Init code hash: $INIT_CODE_HASH" fi

cd - > /dev/null || exit

ดูแหล่งที่มา CC BY-SA 4.0