Skip to content

Instantly share code, notes, and snippets.

@goodGid
Last active January 22, 2019 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save goodGid/9c608c96ad0dea8534fee578b1ce8306 to your computer and use it in GitHub Desktop.
Save goodGid/9c608c96ad0dea8534fee578b1ce8306 to your computer and use it in GitHub Desktop.
## 01 블록체인 네트워크 설치
1. 루트 권한 접속
A. sudo su
2. 소스코드의 빌드를 위해 Go언어와 C컴파일러를 설치한다.
A. apt-get install -y build-essential libgmp3-dev golang git tree
3. Git 저장소에서 소스를 받아 설치한다.
A. git clone https://github.com/ethereum/go-ethereum.git
4. 1.5.5의 버전을 사용한다.
A. cd go-ethereum/
B. git checkout refs/tags/v1.5.5
5. 소스를 빌드한다.
A. make geth
6. 버전을 확인하여 정상적으로 설치된 것을 확인한다.
A. ./build/bin/get version
------------------------------------------------------------------------
Geth
Version: 1.5.5-stable
Git Commit: ff07d54843ea7ed9997c420d216b4c007f9c80c3
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.6.2
OS: linux
GOPATH=
GOROOT=/usr/lib/go-1.6
------------------------------------------------------------------------
7. Get를 복사한 후 경로를 확인한다.
A. cp build/bin/geth /usr/local/bin/
B. which geth
8. 블록체인 데이터를 위한 디렉토리를 생성하고 이동한다.
A. mkdir /data_test
B. pwd
9. Json형태로 제네시스 블록을 생성한다.
genesis.json
-----------------------------------------------------------------------------------------------------------------------
{
"config": {
"chainId": 63,
"homesteadBlock": 0,
"eip150Block": 0,
"eip150Hash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 0,
"eip158Block": 0
},
"nonce": "0x0000000000000042",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x00",
"gasLimit": "0x80000000",
"difficulty": "0x4000",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {
}
}
-----------------------------------------------------------------------------------------------------------------------
10. Geth를 초기화 한다.
A. geth --datadir /data_test init /data_test/genesis.json
11. 블록체인 데이터의 디렉토리를 확인한다.
A. tree /data_test
-----------------------------------------------------------------------------------------------------------------------
/data_test
|-- genesis.json
|-- geth
| `-- chaindata
| |-- 000002.log
| |-- CURRENT
| |-- LOCK
| |-- LOG
| `-- MANIFEST-000003
`-- keystore
-----------------------------------------------------------------------------------------------------------------------
12. 초기화를 정상적으로 한 다음 geth를 실행한다.
 A. geth --networkid 4649 --nodiscover --maxpeers 0 console
## 02 블록체인 네트워크 가동
1. 지갑주소를 생성한다.
A. personal.netAccount("pass0")
      이 지갑에대한 PW는 pass0이다.
2. 지갑 리스트를 검색한다.
A. eth.accounts
3. 인덱스 형태로도 확인할 수 있다.
A. eth.accounts[0]
4. Keystore 밑에 지갑이 생성된 것을 확인할 수 있다.
A. tree /data_test
-----------------------------------------------------------------------------------------------------------------------
/data_test
|-- genesis.json
|-- geth
| |-- chaindata
| | |-- 000012.log
| | |-- 000014.ldb
| | |-- CURRENT
| | |-- LOCK
| | |-- LOG
| | `-- MANIFEST-000013
| |-- LOCK
| `-- nodekey
|-- geth.ipc
|-- geth.log
`-- keystore
|-- UTC--2018-02-26T01-55-20.209489275Z--c07096a9afe4012aea52c4c6bdb9e40d18250514
`-- UTC--2018-02-26T02-42-33.166631271Z--b20039102023fc3887a8e2c4d46fd51401bb1cc5
-----------------------------------------------------------------------------------------------------------------------
5. 채굴하기전 보상받는 지갑주소 확인
A. eth.coinbase
6. 보상받는 지갑을 변경
A. miner.setEtherbase(eth.accounts[1])
7. 잔고 확인
A. eth.getBalance(eth.accounts[1])
8. 블록 갯수 확인
A. eth.blockNumber
9. 채굴 시작
A. miner.start(1)
// 첫 번째 채굴에서는 DAG가 생성되기 때문에 시간이 약간 걸린다.
// 채굴의 독재를 막기 위함이라고 보면 된다.
// 크기는 약 1GB이며, 3000개의 블록마다 하나씩 생성된다.
10. 채굴 유무 확인
A. eth.mining
11. 해시 속도(연산력) 확인
A. eth.hashrate
12. 블록 갯수 확인
A. eth.blockNumber
13. 채굴 중지
A. miner.stop
14. 잔고 확인
A. eth.getBalance(eth.accounts[1])
15. 송금
A. eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(5, "ether")})
16. 지갑의 잠금해제
https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_unlockaccount
A. personal.unlockAccount(eth.accoutns[0])
B. personal.unlockAccount(eth.accoutns[0],"password")
C. personal.unlockAccount(eth.accoutns[0],"password",0)
17. 트랜잭션 내용을 확인
A. eth.getTransaction( 확인하려는 Tx address )
18. 블록화 되지 않은 거래들을 확인
A. eth.pendginTransactions
19. 채굴을 통해 블록을 생성
A. miner.start(1)
20. 블록정보 확인
A. eth.getBlock(블록번호)
## 03 스마트 계약 개발 (1)
1. 블록체인에 배포를 위해 컴파일러를 설치
A. add-apt-repository ppa:ethereum/ethereum
B. apt-get update
C. apt-get install solc
D. solc --version
2. 설치 경로를 확인
A. which solc
3. 콘솔에 접속
A. nohup geth --networkid 4649 --nodiscover --maxpeers 0 --datadir /data_test/
--mine --minerthreads 1 --rpc --rpcaddr "0.0.0.0" --rpcport 8545
--rpccorsdomain "*" --rpcapi "admin,db,debug,miner,net,shh,txpool,personal,web3"
2>> /data_test/geth.log &
// 여기서 '&'의 의미는 Demon으로 돌린다는 뜻이다.
// console이라는 명령어가 없기 때문에
// 백그라운드로 돌린다는 뜻이다.
4. 원격 접속을 위한 작업
A. geth attach rpc:http://localhost:8545
5. 4번에서 접속한 Console안에서 Solc의 경로를 설정
A. admin.setSolc("설치 경로") // 2번에서 solc를 설치한 경로를 입력
ex) admin.setSolc("/usr/bin/solc")
6. 정상적으로 설정되었는지 확인
A. eth.getCompilers()
ex) ["Solidity"]
7. 컴파일을 위해 줄 바꿈을 모두 제거하고, *.sol 형태로 저장한다.
A. cat HelloWorld.sol | tr -d '\n' > HelloWorld2.sol
B. cat HelloWorld2.sol // 원하는 형태로 되었는지 확인
8. 소스 컴파일
A. source = 'pragma solidity ^0.4.8; contract HelloWorld { string public greeting; function HelloWorld(string _greeting){ greeting = _greeting; } function setGreeting(string _greeting){ greeting = _greeting; } function say() constant returns(string){ return greeting; }}'
B. sourceCompiled = eth.compile.solidity(source)
9. 계약을 배포
A. contractAbiDefinition = sourceCompiled['/tmp/geth-compile-solidity911883863:HelloWorld'].info.abiDefinition
10. 계약 객체를 만든다.
A. sourceCompiledContract = eth.contract(contractAbiDefinition)
// 9,10번은 그냥 만들기만 한거고
// 11 ~ 14는 생성자를 이용해
// contract값을 초기화한 후 배포 & 객체 생성을 하는것이다.
11. 계약 객체를 이더리움 블록체인에 배포 준비를 한다.
A. _greeting = "Hello, BlockChain "
12. 배포 작업
A. contract = sourceCompiledContract.new(_greeting, {from : eth.accounts[0], data: sourceCompiled['/tmp/geth-compile-solidity911883863:HelloWorld'].code, gas: '4700000'})
B. contract // 값 확인
{
abi: [{
constant: true,
inputs: [],
name: "say",
outputs: [{...}],
payable: false,
stateMutability: "view",
type: "function"
}, {
constant: false,
inputs: [{...}],
name: "setGreeting",
outputs: [],
payable: false,
stateMutability: "nonpayable",
type: "function"
}, {
constant: true,
inputs: [],
name: "greeting",
outputs: [{...}],
payable: false,
stateMutability: "view",
type: "function"
}, {
inputs: [{...}],
payable: false,
stateMutability: "nonpayable",
type: "constructor"
}],
address: undefined,
transactionHash: "0xb4aee08c1517e67585ac0de0656184db74ccc5efe620ca83d7cd24649f10b5dc"
}
13. 계약을 배포한다.
A. contractAbiDefinition = sourceCompiled['/tmp/geth-compile-solidity911883863:HelloWorld'].info.abiDefinition
14. 계약 객체를 만든다.
A. dsourceCompiledContract = eth.contract(contractAbiDefinition)
15. Say메서드 실행
A. contract.say.call()
16. 상태 변수 확인
A. contract.greeting.call()
17. 상태 변수 변경
A. contract.setGreeting.sendTransaction("Hello, Gid ", {from: eth.accounts[0], gas:15})
B. 변경된 값을 확인하려면 miner.start()를 해줘야한다.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment