이 튜토리얼에서는 어떻게 blockchain간에 packet을 생성하고 보내는지 이해할 것이다.
배울 점
IBC를 사용해서 blockchain간에 packet을 생성하고 보낸다
Cosmos SDK와 Starport Relayer를 사용해서 blockchain 사이를 이동한다
다른 blockchain에 기초적인 블로그를 게시하고 저장한다
What is IBC?
IBC module은 두 blockchain 사이에서 기초적인 상호작용이다. IBC module은 packet과 message들이 blockchain에서 수신과 발신이 어떻게 구성되는지 정의한다.
IBC relayer는 IBC가 있는 체인 사이를 연결해준다. 이 튜토리얼에서는 두 blockchain에서 relayer를 어떻게 사용하는지 배운다.
이 튜토리얼은 module, IBC packets, relayer와 IBC에 라우팅된 packet들의 lifecycle을 다룬다.
Create a Blockchain
다른 blockchain에 포스트를 작성하는 blog module을 가진 blockchain app을 생성한다. Hello Mars, Hello Cosmos, Hello Earth message를 작성할 것이다.
체인들은 IBC를 이용해서 서로 post를 보낼 수 있다
Sending chain에서는 acknowledged와 timed out 포스트를 저장한다.
Transaction을 받는 체인에게 승인이 된 후에 양쪽 blockchain에서 post가 저장되었다는 것을 알 수 있다.
발신 chain은 추가적으로 postID 데이터를 가진다.
제목과 내용을 담은 post packet을 보낸다.
만약에 Mars에 제 시간에 도착하지 못하면 TimedoutPosts에 추가된다.
포스트를 받는다.
postID를 포함한 Ack를 보내준다.
Ack를 받으면 SentPosts에 추가한다. (제대로 보낸 것 확인 완료)
Build your Blockchain App
Blockchain 생성.
Scaffold the blog module inside your blockchain
blog module을 생성한다.
IBC module logic도 함께 생성된다.
Generate CRUD actions for types
blog post 생성
인증된 sent posts 생성
post timeout 관리
Starport Scaffold List Command Overview
첫 매개변수는 type의 이름을 말한다. blog 에서는 post, sentPost, timedoutPost 3개의 type을 생성했다.
다음 매개변수는 type 과 관련된 field들이다.
—module flag는 어떤 module에 새로운 transaction type이 추가될 지 지정한다. module이 여러개일 때 사용하고 지정하지 않으면 repo의 이름과 매칭되는 module에 생성된다.
새로운 type이 생성되면 기본적으로 user에 의해 생성될 수 있는 CRUD작업의 message들이 생성된다. —no-message flag를 설정하면 message가 생성되지 않는다.
Scaffold a sendable and interpretable IBC packet
제목과 내용을 담고 있는 packet을 생성해야 한다.
starport packet command가 다른 blockchain으로 보내질 수 있는 IBC packet logic을 생성한다.
제목과 내용이 타겟 체인에 저장되어 있다.
sending chain에 postID가 인증된다.
ibcPost의 필드가 post type 과 일치한다.
—ack flag는 어떤 identifier가 리턴될지 정의한다.
—module flag는 packet이 어떤 module에 생성될 지 지정한다.
Modify the Source Code
Add creator to the blog post packet
proto file에서 IBC packet 구조를 정의해보자.
포스트의 creator를 정의하기 위해 packet에 creator field를 추가한다.
수신 체인에 블로그 post creator에 대한 콘텐츠가 있는지 확인하려면 이 값을 IBC 패킷에 추가하라(packet.Creator). sender의 내용은 SendIbcPost 메시지에 자동으로 포함된다. sender가 메시지의 서명자로 확인되므로 IBC를 통해 보내기 전에 새 패킷의 작성자로 msg.Sender를 추가할 수 있다.
Receive the post
주요한 transaction logic은 planet/x/blog/keeper/ibcPost.go 파일에 들어있다.
TransmitIbcPostPacket
IBC로 packcet을 보낼 때 불린다.
이 메소드는 packet이 다른 블록체인 앱에 IBC로 보내지기 전 logic을 정의한다.
OnRecvIbcPostPacket
체인에서 packet을 받을 때 자동으로 불린다.
이 메소드는 packet 수신 logic을 정의한다.
OnAcknowledgementIbcPostPacket
보낸 packet이 승인됐을 때 불린다.
이 메소드는 packet이 수신 됐을 때 logic을 정의한다.
OnTimeoutIbcPostPacket
보낸 packet이 시간초과 됐을 때 불린다.
이 메소드는 packet이 목표 체인에 전달되지 못 했을 때 logic을 정의한다.
Post 메시지를 받으면 받은 제목과 내용으로 새로운 post를 생성해야 한다.
누가 메시지를 생성했는지 알기 위해 아래 포맷으로 identifier를 설정한다.
$<portID>-<channelID>-<creatorAddress>$
마지막으로 새로 추가된 post의 ID를 리턴하는 AppendPost 함수를 생성한다. Acknowledgment를 통해 이 값을 source chain으로 리턴할 수 있다.
---------------------------------------------
Setting up chains
---------------------------------------------
🔐 Account on "source" is "cosmos1xcxgzq75yrxzd0tu2kwmwajv7j550dkj7m00za"
|· received coins from a faucet
|· (balance: 100000stake,5token)
🔐 Account on "target" is "cosmos1nxg8e4mfp5v7sea6ez23a65rvy0j59kayqr8cx"
|· received coins from a faucet
|· (balance: 100000stake,5token)
⛓ Configured chains: earth-mars
starport relayer connect
🔌 Linked chains with 1 paths.
---------------------------------------------
Chains by paths
---------------------------------------------
earth-mars:
earth > (port: blog) (channel: channel-0)
mars > (port: blog) (channel: channel-0)
---------------------------------------------
Listening and relaying packets between chains...
---------------------------------------------
planetd tx blog send-ibc-post blog channel-0 "Hello" "Hello Mars, I'm Alice from Earth" --from alice --chain-id earth --home ~/.earth
planetd q blog list-post --node tcp://localhost:26659
planetd tx blog send-ibc-post blog channel-0 "Sorry" "Sorry Mars, you will never see this post" --from alice --chain-id earth --home ~/.earth --packet-timeout-timestamp 1
planetd tx blog send-ibc-post blog channel-0 "Hello" "Hello Earth, I'm Alice from Mars" --from alice --chain-id mars --home ~/.mars --node tcp://localhost:26659