출처: https://www.inflearn.com/course-status-2/
블록체인 강좌
1. 머클트리
해시가 이용된 데이터 검증을 위한 트리 구조. 블록체인에서 라이트노드(모든 거래정보를 가지고 있지 않고, 머클노드의 정보만을 가지고 있는 것을 뜻함)들과 거래검증의 핵심요소임. 머클트리는 2진 트리로 2개씩 결합하여 창의 노드가 됨? 머클루트만을 가지고 있는 것을 라이트노드라고함.
거래검증
머클트리는 머클경로를 알려주면 몇번째 블록인지 검증 가능.
2. 블록체인의 활용
가치를 전달하는 것이 가능. 지금은 은행은 정보전달만을 했음.
3. 이더리움과 스마트 컨트랙트
4. 블록체인과 이더리움의 문제
단점:
4-1. 프라이버시: 데이터 조작불가능. 기록남음.
4-2. 트랜잭션 코스트: 예를 들어, 가스비 납부시 블록체인 기술 활용. 가스비 높아지고, 암호화폐의 가치가 높아지면 수수료 커짐. 사용자가 난감해짐.
4-3. 트랜잭션 스피드: 1초에 20 밖에... 이거 엄청난 결함임. 1초에 200,000개는 되야.
4-4, 블록사이즈: 블록이 매달 2기가 정도씩 쌓인다면... 급격히 하드웨어 비용이 늘어남. 때론 중앙화가 정답일 때가 있다.
5, 솔리디티
스마트 컨트랙트를 만들기 위한 언어. C++, python, javascript 영향을 받은 고수준의 언어. 이더리움 버추얼 머신(evm)을 타겟으로 디자인 되어 있음.
remix.ethereum.org 하는 웹기반 솔리디티를 사용할 계획.
솔리디티 특징
튜링완전: 반복문과 제어문 사용가능.
msg.sender: 메시지 보낸 주소
msg.value: 메시지 보낸 값
6. 스마트 컨트랙트 구조
컨트랙트는 클래스와 비슷.
// 1. 컨트랙트 선언 contract Sample { // 2. 상태 변수 선언 uint256 data; address owner; // 3. 이벤트 정의 event logData(uint256 dataToLog); // 4. 함수 변경자 정의 modifier onlyOwner() { if(msg.sender != owner) revert(); _; } // 5. 생성자 function Sample(uint256 initData, address initOwner) { data = initData; onwer = initOwner; } // 6. 함수(메소드) 정의 function getData() returns (uint256 returned) { return data; } function setData(uint256 newData) onlyOwner { logData(newData); data = newData; } } ```
개인적 소견으로 봤을때, 자바와 거의 비슷
storage - 전역변수(블록체인에 저장)
memory - 로컬변수(사용 후 휘발됨)
솔리디티 문법: if, else, while, for, break, continue, return, ? :(3항연산 지원)
가시성
누가 접근할 수 있는지 정의.
external: 다른 컨트랙트나 트랜잭션을 통해서 호출될 수 있음.
public: 모든 방식으로 가능.
internal: 접근을 위해 this 사용불가. 내부에서만 접근가능.
private: 인터널과 비슷하지만 상속된 컨트랙트에선 접근불가능.
가스
evm에서 뭔가를 실행하면 가스라는 것이 발생함.
가스는 수수료라고 생각하면 됨.
2가지 요소.
가스 리밋: 수수료의 한계치.
가스프라이스: 가스당 가격.
가스 프라이스 * 가스 사용량 = 수수료.
투표앱 만들기
먼저 어떤 버전의 솔리디티를 사용할 것인지를 설정하고 시작해야 함.
그리고 컴파일러 설정해야 함.
pragma solidity 0.4.25;
---------------------------------------------------------------------------------------------
pragma solidity 0.4.25;
contract Vote{
//structure
struct candidator {
string name;
uint upVote;
}
//variable
bool live;
address owner;
candidator[] public candidatorList;
//mapping
mapping(address => bool) Voted;
//event
event AddCandidator(string name);
event UpVote(string candidator, uint upVote);
event FinishVote(bool live);
event Voting(address owner);
//modifier(must be shutting down this contract by admin dont touch anybody)
modifier onlyOwner{
require(msg.sender == owner);
_;
}
//constructor
constructor() public{
owner = msg.sender;
live = true;
emit Voting(owner);
}
// candidator
function addCandidator(string _name) public onlyOwner{
require(live == true);
//to save the gas
require(candidatorList.length < 5);
candidatorList.push(candidator(_name, 0));
//emit event(must call with emit when you want to call the event.)
emit AddCandidator(_name);
}
// get candidator
// voting
function upVote(uint _indexOfCandidator) public{
require(live == true);
require( _indexOfCandidator < candidatorList.length );
require(Voted[msg.sender] == false);
candidatorList[_indexOfCandidator].upVote++;
Voted[msg.sender] = true;
emit UpVote(candidatorList[_indexOfCandidator].name, candidatorList[_indexOfCandidator].upVote);
}
// finish vote
function finishVote() public onlyOwner{
require(live == true);
live = false;
emit FinishVote(live);
}
}
-----------------------------------------------------------------------------------------