dukDukz

[블록체인] 트랜젝션 transaction [실습] 본문

웹 개발/블록체인

[블록체인] 트랜젝션 transaction [실습]

헤일리_HJ 2021. 9. 24. 15:19

데몬을 실행시킨다는 것은 root 폴더 안에 bin 으로 실행시켜야 함

 

# 데몬1 & 클라1 연결하고 코인 생성



데몬 1

데몬 실행

.\ingcoind.exe -printtoconsole -datadir="C:\Users\USER\ingcoin2\data"

or (배포 다운 ver)

.\ingcoind.exe -printtoconsole -rpcport=3000 -rpcuser=ingoo -rpcpassword=1234 -conf="C:\Users\USER\ingcoin2\bin\incoind2.conf" -datadir="C:\Users\USER\tmp"

이렇게 옵션으로 써주거나 conf 파일을 작성해서 옵션에 conf 를 줘도 된다.

나는 conf 파일이 안먹어서 실습은 이렇게 진행했다.

 


클라 1

conf 파일 작성해보기

 

하나 생성해볼거임
메모장 하나 열어서

server=1
rpcport=3000
rpcuser=ingoo 
rpcpassword=1234

 

으로 작성하고 root 폴더 안에 bin 폴더 안에 넣어주기 [incoind2.conf] 로 저장

데몬을 잠시 꺼보자
그런 다음에 몇번 포트로 실행할건지 기본 설정값을 넣어주자



데몬 1

데몬 실행

.\ingcoind.exe -printtoconsole -conf="C:\Users\USER\ingcoin2\bin\incoind2.conf" -datadir="C:\Users\USER\ingcoin2\data"

 

[항상 datadir를 맨 뒤에 적어야함]

클라 1

.\ingcoin-cli.exe -conf="C:\Users\USER\ingcoin2\bin\incoind2.conf" getbalance

 

이렇게 하면 rpcuser or rpcpassword 가 맞지 않는다는 오류가 난다.

수시로 파일 바뀔때마다 비동기적으로 읽어오지 않는다. 
그렇기 때문에 뭔가 내용이 바뀔 때 마다 데몬을 껐다 켜야 함.

그래서 데몬을 껐다가 다시 켜주고

다시 저 명령어를 입력하면 0.00000000 으로 뜬다.


# 코인 만들기
클라 1

.\ingcoin-cli.exe -conf="C:\Users\USER\ingcoin2\bin\incoind2.conf" generate 100

 



교수님이 준걸로 실행한 경우...
위치는 데몬과 클라 모두 C:\Program Files\INGcoin\daemon>
여기서 실행할것


데몬 1

.\ingcoind.exe -printtoconsole -rpcport=3000 -rpcuser=ingoo -rpcpassword=1234 -conf="C:\Users\USER\ingcoin2\bin\incoind2.conf" -datadir="C:\Users\USER\tmp"


클라 1

 .\ingcoin-cli.exe -rpcport=3000 -rpcuser=ingoo -rpcpassword=1234 -datadir="C:\Program Files\INGcoin\daemon" getbalance

0.00000000 이 나오고

 

코인 만들기

.\ingcoin-cli.exe -rpcport=3000 -rpcuser=ingoo -rpcpassword=1234 generate 1

 


# 데몬2 & 클라2 연결하기

 


데몬 2 클라 2 만들고

conf 파일 생성

conf 나 data 2 는 어느 경로든 상관없고 잘 적어주기만 하면 됨
그래서 C:\Users\USER\tmp\data2 여기에
ingcoind.conf 파일을 만들어줌 클라 2 전용임

 

server=1
rpcuser=ingoo2
rpcpassword=1234
rpctimeout=600
rpcport=3001
rpcallowip=127.0.0.1
addnode=127.0.0.1:9233

 

이렇게 작성했음

 

rpcallowip
: 내가 요청을 보낼때 
addnode
:peer 가 가능하게함




데몬 2 

데몬 켜주고


클라 2

[getbalance]

 .\ingcoin-cli.exe -conf="C:\Users\USER\tmp\data2\ingcoind.conf" getbalance

 

0.00000000

 


[getnewaddress]

nodeB(임의의 이름) 라는 주소를 생성해줌

.\ingcoin-cli.exe -conf="C:\Users\USER\tmp\data2\ingcoind.conf" getnewaddress nodeB

주소

iEAneAhAD53JDC14bGPCrjkUpA5ncuaJeS


getbalance 

: 데몬이 전체적으로 갖고있는 코인


getbalance nodeB

: nodeB의 코인만 보여준다.



클라 1에는 generate 5 로 계속 코인 채굴하기

(클라에서 블럭 생성할때 빈 배열로 나오면 안된거임)

만약 주소값을 까먹었다면?
주소를 찾는 API 가 있다.

.\ingcoin-cli.exe -conf="C:\Users\USER\tmp\data2\ingcoind.conf" getaddressesbyaccount nodeB
혹은
.\ingcoin-cli.exe -rpcport=3002 -rpcuser=ingoo -rpcpassword=1234 getaddressesbyaccount nodeB

 


블럭이 생성 되었다면

# 1 → 2 로 코인 보내기


클라 1

.\ingcoin-cli.exe -rpcport=3000 -rpcuser=ingoo -rpcpassword=1234 sendtoaddress iEAneAhAD53JDC14bGPCrjkUpA5ncuaJeS 10

 

10개를 nodeB 에 보낸다는 뜻
어떤 값이 나오는데 그걸 복사해서 저장해두고.

bf46375970c62a2d16c47acae8ce5cb2b64e0eb54df516e85c87fef3cde279e2

 

  
getbalance 로 확인해보자. 

조금 빠져나감
근데 정확히 10개가 빠진게 아니라 10.594 어쩌구가 빠짐... 왜??
해당 금액에 트랜젝션 (거래)를 발생시킨 수수료까지 빠져나간거임



클라 2
getbalance nodeB 로 확인해보자
0.00000000 이 나온다.

 

왜지? 10개 보냈는뎅??
서버가 블럭에 대한 정보를 받지 못한 상태임

그래서 클라 2 에도 
generate 1 을 해야함
어딘가에 히스토리가 생길때 그 전에 있던 히스토리를 다 가져오기 때문에 이렇게 한번 해줘야함

완료되고 다시 getbalance nodeB 하면 정상적으로 10개가 들어온것을 확인할 수 있다.



클라 1 
gettransaction 을 해주자

보내고 나온 값
= txid 값
거래 하나에 대한 정보를 담고 있는 키 값이다.
그렇게 나온거중에 confirmations 이 1 이면 거래 완료 0이면 거래 전이라는 뜻

그런 뒤에 다시 클라 2로 가서
완료가 되었으면 getbalace 로 다시 조회해보면 코인이 들어온걸 확인 할 수 있다.




litecoin API
https://litecoin.info/index.php/Litecoin_API

 

Litecoin API - Litecoin Wiki

Most of the Litecoin API calls are the same as they are in Bitcoin. Required arguments are denoted inside < and > Optional arguments are inside [ and ]. Command Parameters Description Requires unlocked wallet addmultisigaddress <'["key","key"]'> [account]

litecoin.info

 

 

[getnewaddress]

nodeB(임의의 이름) 라는 주소를 생성해줌

.\ingcoin-cli.exe -conf="C:\Users\USER\tmp\data2\ingcoind.conf" getnewaddress nodeB

결과 :

iEAneAhAD53JDC14bGPCrjkUpA5ncuaJeS


[getbalance] 

: 데몬이 전체적으로 갖고있는 코인


getbalance nodeB

: nodeB의 코인만 보여준다.




[getaddressbyaccount]

주소를 찾는 API

.\ingcoin-cli.exe -rpcport=3002 -rpcuser=ingoo -rpcpassword=1234 getaddressesbyaccount nodeB

[getnewaddress]

하나의 계정에 여러 주소 생성하기

.\ingcoin-cli.exe -rpcport=3002 -rpcuser=ingoo -rpcpassword=1234 getnewaddress nodeB

 

getaddressesbyaccount 로 확인할 수 있다.



[getaccount]

주소를 갖고 계정명을 알아내는 방법도 있다.

.\ingcoin-cli.exe -rpcport=3002 -rpcuser=ingoo -rpcpassword=1234 getaccount iFpcPBGH3VkvtLeDU8rMNHBGHAbDr4M73d


하면 nodeB 가 나온다.



[getblockchaininfo]

.\ingcoin-cli.exe -rpcport=3002 -rpcuser=ingoo -rpcpassword=1234 getblockchaininfo


[getpeerinfo]

.\ingcoin-cli.exe -rpcport=3002 -rpcuser=ingoo -rpcpassword=1234 getpeerinfo

연결된 ip 주소를 보여주는..


 

[getblockhash 숫자]

블럭의 해쉬값 반환

.\ingcoin-cli.exe -rpcport=3002 -rpcuser=ingoo -rpcpassword=1234 getblockhash 0

1 에는 생성한적이 없으므로 안나옴



[getconnectioncount]

연결된 peer 를 반환함

.\ingcoin-cli.exe -rpcport=3002 -rpcuser=ingoo -rpcpassword=1234 getconnectioncount