본문 바로가기
  • 개발 / 공부 / 일상
BlockChain

(BlockChain) RPC

by JJeongHyun 2023. 2. 9.
반응형

RPC

  • Remote Procedure Cell의 약자, 원격 프로시저 호출
  • 별도의 코딩 없이 다른 공간에 서 함수 등을 호출할 수 있는 통신 기술
  • IPC처럼 admin, eth, miner 등등 이 존재한다

 

geth를 IPC 파일이 아니라 HTTP 통신으로 조작, 실행하기

  • HTTP 통신을 사용하기 때문에 port가 열려있으면 외부에서 조작이 가능하다
geth --datadir ~/myGeth --http --http.addr "0.0.0.0" --http.port 8080 --http.corsdomain "*" --http.api "admin,miner,txpool,web3,personal,eth,net" --allow-insecure-unlock --syncmode full --networkid 50
  • datadir : 개인 이더리움 네트워크 데이터 저장 폴더
  • http : HTTP 서버를 배포, IPC로 조작하던 개인 이더리움 네트워크를 HTTP 통신으로 조작
  • http.addr : 요청 가능한 IP 주소 설정, 기본값은 127.0.0.1 // 0.0.0.0으로 설정하면 모든 IP 주소 허용 하는 설정
  • http.port : 요청 가능한 port 설정, 기본값 8545
  • http.corsdomain : CORS에 대한 설정, 와일드카드(*) 사용 가능
  • networkid : 개인 이더리움 네트워크 아이디
  • allow-insecure-unlock : http 통신으로 계정을 열 수 있게 한다(unlock)
  • syncmode : peer연결 시 동기화 방법 설정
    • fast : 블록헤더를 동기화, 최신화 1024개의 트랜잭션 동기화, 기본값 << 현재는 없어졌다
    • full : 모든 데이터 동기화
    • light : 블록 헤더와 잔액 관련만 동기화
    • snap : 최근 128개 블록만 동기화, 기본값

 

geth에 HTTP 통신으로 연결

geth attach http://localhost:8080

 

attach로 연결한 곳에서 연결

계정 생성 및 잠금 풀기

personal.newAccount()
personal.unlockAccount(eth.accounts[0])

 

반대로 attach 없이 geth HTTP 통신으로 요청

curl -X POST -H "content-type:application/json" --data '{"id":50, "jsonrpc":"2.0","method":"eth_accounts", "params":[]}' http://localhost:8080
  • -X : 통신에 사용하는 method
  • -H : header
  • —data : 보내는 요청 body
    • id : 체인의 id
    • jsonrpc : json 사용하는 rpc의 버전
    • method : 이더리움의 호출 메세드명
    • params : 메서드의 인자값(매개변수)
     

curl accounts 결과 화면

 

새로운 계정 생성

curl -X POST -H "content-type:application/json" --data '{"id":50, "jsonrpc":"2.0", "method":"personal_newAccount", "params":["password"]}' http://localhost:8080

위 코드(새로운 계정 생성)에 대한 결과

 

계정 잠금 풀기

curl -X POST -H "content-type:application/json" --data '{"id":50, "jsonrpc":"2.0", "method":"personal_unlockAccount", "params":["0xf6f0c0c7c2afdc74c119707b29711ea91ec59b53", "password"]}' http://localhost:8080

위 코드(특정 계정 잠금 풀기)에 대한 결과

 

채굴 전 etherbase 설정

curl -X POST -H "content-type:application/json" --data '{"id":50, "jsonrpc":"2.0", "method":"miner_setEtherbase", "params":["0xf6f0c0c7c2afdc74c119707b29711ea91ec59b53"]}' http://localhost:8080

위 코드(Etherbase 설정)에 대한 결과

 

채굴 시작

  • "params" : [1] < 여기 숫자는 사용할 thread의 수를 뜻
    • thread란 간단하게 CPU의 작업 최소 단위
curl -X POST -H "content-type:application/json" --data '{"id":50, "jsonrpc":"2.0", "method":"miner_start", "params": []}' http://localhost:8080

위 코드(채굴 시작)에 대한 결과

 

채굴 중지

curl -X POST -H "content-type:application/json" --data '{"id":50, "jsonrpc":"2.0", "method":"miner_stop", "params": []}' http://localhost:8080

위 코드(채굴 중지)에 대한 결과

 

잔액 조회

curl -X POST -H "content-type:application/json" --data '{"id":50, "jsonrpc":"2.0", "method":"eth_getBalance", "params":["0xf6f0c0c7c2afdc74c119707b29711ea91ec59b53", "latest"]}' http://localhost:8080

위 코드(잔액 조회)에 대한 결과

 

트랜잭션 풀 확인하기

curl -X POST -H "content-type:application/json" --data '{"id":50, "jsonrpc":"2.0", "method": "txpool_content"}' http://localhost:8080

위 코드( 트랜잭션 전송 전 트랜잭션 pool 확인 )에 대한 결과

 

트랜잭션 전송하기

  • 전송 전 보내는 계정 잠금 풀기
curl -X POST -H "content-type:application/json" --data '{"id":50, "jsonrpc":"2.0", "method":"personal_unlockAccount", "params":["0xf6f0c0c7c2afdc74c119707b29711ea91ec59b53", "password"]}' http://localhost:8080

 

  • 전송
curl -X POST -H "content-type:application/json" --data '{"id":50, "jsonrpc":"2.0", "method": "eth_sendTransaction", "params":[{"from": "0xf6f0c0c7c2afdc74c119707b29711ea91ec59b53", "to": "0x867d194a3de500118f5cdf606f55dbde3d79fe93", "value" : "0x3B9ACA00", "gae": "0x15f903", "gasPrice":"0x4340e4500"}]}' http://localhost:80

위 코드(2번의 트랜잭션 전송)에 대한 결과

  • 채굴 하기 전 트랜잭션 pool 확인

위 코드 (트랜 잭션 전송 후 채굴로 인한 동기화 전 트랜잭션 pool)에 대한 결과

  • 채굴 후 받은 계정 잔액 확인
curl -X POST -H "content-type:application/json" --data '{"id":50, "jsonrpc":"2.0", "method":"eth_getBalance", "params":["0x867d194a3de500118f5cdf606f55dbde3d79fe93", "latest"]}' http://localhost:8080

위 코드 (받은 계정에 대한 잔액 확인)에 대한 결과

'BlockChain' 카테고리의 다른 글

(BlockChain) MetaMask axios 통신  (0) 2023.02.09
(BlockChain) MetaMask  (0) 2023.02.09
(BlockChain) IPC  (0) 2023.02.09
(BlockChain) Geth & Go 설정  (0) 2023.02.09
(BlockChain) WSL  (0) 2023.02.09