Primrose Docs
  • Home
    • ⛓️Blockchain
      • Avalanche
        • What is AVAX?
      • Ethereum
        • Ethereum Cancun Upgrade Explained(draft)
        • go-ethereum: gas estimate
        • Blockchain Transaction Lifecycle
        • Mempool
        • Gas optimization in Solidity, Ethereum
      • Solidity DeepDive
        • Meta transaction
        • solidity: patterns
        • UUPS vs Transparent
        • Solidity Interface
        • Smart contract storage
        • ERC-2981 Contract
        • Solidity modifier
        • Solidity delete keyword
        • How To Make NFTs with On-Chain Metadata - Hardhat and JavaScript
        • How to Build "Buy Me a Coffee" DeFi dapp
        • How to Develop an NFT Smart Contract (ERC 721) with Alchemy
        • Upgradeable Contract
        • Smart Contract Verification
      • Common
        • Eigenlayer
        • MultiSig(draft)
        • Chain-Based Proof-of- Stake, BFT-Style Proof-of-Stake
        • Byzantine Fault Tolerance
        • Zero-knowledge
        • Hierarchical Deterministic Wallet
        • Maker DAO
        • Defi
        • Uniswap
        • IBC
        • Cosmos
        • Gossip Protocol
        • Tendermint
        • UTXO vs Account
        • Blockchain Layer
        • Consensus Algorithm
        • How does mining work?
        • Immutable Ledger
        • SHA256 Hash
        • Filecoin
        • IPFS - InterPlanetary File System
        • IPFS와 파일코인
        • Livepeer
        • Layer 0
      • Bitcoin
        • BIP for HD Wallet
        • P2WPKH
        • Segwit vs Native Segwit
    • 📖Languages
      • Javascript/Typescript
        • Hoisting
        • This value in Javascript
        • Execution Context
        • About Javscript
        • tsconfig.json
        • Nest js Provider
        • 'return await promise' vs 'return promise'
      • Python
        • Pythonic
        • Python: Iterable, Iterator
        • Uvicorn & Gunicorn
        • WSGI, ASGI
        • Python docstring
        • Decorator in Python
        • Namespace in Python
        • Python Method
      • Go
        • GORM+MySQL Connection Pool
        • Context in golang
        • How to sign Ethereum EIP-1559 transactions using AWS KMS
        • Mongo DB in golang(draft)
        • Golang HTTP Package
        • Panic
        • Golang new/make
        • golang container package
        • errgroup in golang
        • Generic Programming in Golang
        • Goroutine(draft)
    • 📝Database
      • MongoDB in golang
      • Nested loop join, Hash join
      • DB Query plan
      • Index
      • Optimistic Lock Pessimistic Lock
    • 💻Computer Science
      • N+1 query in go
      • Web server 를 구성할 때 Thread, Process 개수를 어떻게 정할 것인가?
      • CAP
      • Socket programming
      • DNS, IP
      • URL, URI
      • TLS과 SSL
      • Caching(draft)
      • Building Microservices: Micro Service 5 Deploy Principle
      • Red Black Tree
      • AOP
      • Distributed Lock
      • VPC
      • Docker
      • All about Session and JWT
      • Closure
      • Singleton Pattern
      • TCP 3 way handshake & 4 way handshake
      • Race Condition
      • Process Address Space 
      • Call by value, Call by reference, Call by assignment
      • Zookeeper, ETCD
      • URL Shortening
      • Raft consensus
      • Sharding, Partitioning
    • 📒ETC
      • K8S SIGTERM
      • SQS
      • Git Branch Strategy: Ship / Show / Ask
      • Kafka
      • Redis Data Types
      • CI/CD
      • How does Google design APIs?
      • Minishell (42 cursus)
      • Coroutine & Subroutine
      • Redis
Powered by GitBook
On this page
  1. Home
  2. Blockchain
  3. Solidity DeepDive

Solidity modifier

Solidity modifier

Solidity에는 modifier라는 것이 있다.

modifier는 우리가 컨트랙트를 개발할 때 함수의 동작을 제어할 수 있도록 해준다.

간단한 modifier의 예시를 보자.

modifier modifierName {
    // ...modifier code...

    _;  // this represents the function body that the modifier is applied to
}

_; 키워드는 함수의 나머지 body 부분을 실행하라는 명령을 나타낸다.

다음과 같이 함수에 적용할 수 있다.

function functionName() modifierName public {
    // ...function code...
}

보통 modifier를 통해서 달성하고자 하는 목표는 다음과 같다.

  1. Access control : onlyOwner와 같이 Role에 따라 함수 Access를 제한하기 위함

  2. Precondition : 함수의 동작을 위한 전제조건, (e.g. decorator를 이용한 제어와 비슷하다)

보통 modifier 를 작성할 때 error 및 assestion을 처리하기 위해 revert(), require() 및 assert()와 같은 몇 가지 기능을 사용한다.

revert

revert() 함수는 블록체인의 상태를 현재 함수가 호출되기 전의 상태로 되돌린다.

이는 현재 기능에서 블록체인에 대한 변경 사항이 취소되었음을 의미한다.

일반적으로 처리 방법을 모르거나 처리하지 않으려는 오류가 발생할 때 사용된다.

revert()는 오류에 대한 자세한 정보를 제공할 수 있는 선택적 문자열 메시지도 허용한다.

required

require() 함수는 항상 참이어야 하는 항목의 유효성을 검사하는 데 사용된다.

일반적으로 입력의 유효성을 검사하기 위해 함수를 시작할 때 또는 작업의 결과를 검사하기 위해 함수를 실행할 때 사용된다.

require() 내부 조건이 false이면 revert()와 유사하게 트랜잭션을 반환한다.

require()는 또한 요구 사항이 충족되지 않은 이유를 설명하는 선택적 문자열 메시지를 허용한다.

assert

assert() 함수는 require()와 비슷하지만 내부 오류 또는 절대 발생해서는 안 되는 조건을 확인하는 데 사용된다.

assert() 내부의 조건이 거짓이면 심각한 문제가 발생하여 트랜잭션이 반환됩니다.

assert()는 고장이 났을 때 남은 가스를 모두 소비하므로, revert()보다 사용 비용이 더 많이 필요하다.

나라면 실제 프로덕션에서는 require와 revert를 사용하고, assert는 테스트넷 혹은 디버깅을 위해 사용할 것 같다.

PreviousERC-2981 ContractNextSolidity delete keyword

Last updated 1 year ago

⛓️