// PKGPATH: gno.land/r/aib/main package main import ( "chain" "encoding/base64" "encoding/hex" "testing" "time" tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing" "gno.land/p/aib/ibc/types" "gno.land/p/aib/ics23" "gno.land/p/nt/testutils/v0" "gno.land/r/aib/ibc/apps/transfer" "gno.land/r/aib/ibc/core" ) // OnRecvPacket: success w/ IBC token func main(cur realm) { var ( chainID = "chain-id-2" trustedHeight = types.NewHeight(2, 2) clientState = tmtesting.NewClientState(chainID, trustedHeight) // NOTE this apphash was provided by the gen-proof command below. apphash, _ = hex.DecodeString("efb8069a70149ec2985f044957bcb3a9aefa81122db1c3dbc286f6f4c6709821") trustedValset = tmtesting.GenValset() consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash()) counterpartyID = "07-tendermint-42" ) clientID := core.CreateClient(cross(cur), clientState, consensusState) core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, counterpartyID) // The transfer app must have 100ugnot escrowed for this test to work // NOTE: The setup has only send 100ugnot to the transfer app, but these // tokens are not stored as escrowed like they should be after a real // transfer. As a result the escrowed ugnot will be negative at the end of // the test. transferAppAddr := chain.PackageAddress("gno.land/r/aib/ibc/apps/transfer") testing.IssueCoins(transferAppAddr, chain.NewCoins(chain.NewCoin("ugnot", 100))) receiver := testutils.TestAddress("receiver") payload := transfer.NewFungibleTokenPacketData( "transfer/"+counterpartyID+"/ugnot", // expect transfer/07-tendermint-42/ugnot "100", "atone1user", receiver.String(), "", ) payloadBz := payload.ProtoMarshal() // NOTE this base64 value is used in payload.value in the gen-proof command below. println("Payload proto:", base64.StdEncoding.EncodeToString(payloadBz)) // Generate the proof of existence of the packet commitment for the // counterparty chain. specs := ics23.IavlSpec() // NOTE code generated by: // go run -C ./cmd/gen-proof . 'prefix2' '07-tendermint-42' 'packet' '{"sequence":1,"source_client":"07-tendermint-42","destination_client":"07-tendermint-1","timeout_timestamp":1234571490,"payloads":[{"source_port":"transfer","destination_port":"transfer","encoding":"application/x-protobuf", "value":"Ch90cmFuc2Zlci8wNy10ZW5kZXJtaW50LTQyL3Vnbm90EgMxMDAaCmF0b25lMXVzZXIiKGcxd2Zqa3hldGZ3ZWpoeWg2bHRhMDQ3aDZsdGEwNDdoNmxsbTgyeWw=","version":"ics20-1"}]}' proof := []ics23.CommitmentProof{ // iavl proof ics23.CommitmentProof_Exist{ Exist: &ics23.ExistenceProof{ Key: []byte("\x70\x72\x65\x66\x69\x78\x32\x30\x37\x2d\x74\x65\x6e\x64\x65\x72\x6d\x69\x6e\x74\x2d\x34\x32\x01\x00\x00\x00\x00\x00\x00\x00\x01"), Value: []byte("\x0d\x99\x21\xf7\xdb\xea\xfe\x29\x2d\x78\x15\xd3\x20\x61\x12\xbf\x5f\xce\x28\x0d\x26\x23\x62\xb6\xfb\xe0\x4f\x9b\x28\x24\x92\xf7"), Leaf: &ics23.LeafOp{ Hash: specs.LeafSpec.Hash, PrehashKey: specs.LeafSpec.PrehashKey, PrehashValue: specs.LeafSpec.PrehashValue, Length: specs.LeafSpec.Length, Prefix: []byte("\x00\x02\x02"), }, Path: []*ics23.InnerOp{ { Hash: specs.InnerSpec.Hash, Prefix: []byte("\x02\x04\x02\x20\x35\xf8\xea\x80\x53\x90\xe0\x84\x85\x4f\x39\x9b\x42\xcc\xde\xae\xa3\x3a\x1d\xed\xc1\x15\x63\x8a\xc4\x8d\x06\x00\x63\x7d\xba\x1f\x20"), Suffix: []byte(""), }, { Hash: specs.InnerSpec.Hash, Prefix: []byte("\x04\x08\x02\x20"), Suffix: []byte("\x20\x79\x8e\x2c\xaa\x96\xfd\xfb\xa3\x76\xdd\xeb\x47\x99\x99\x54\xd2\xf4\x7e\x65\x16\x22\x64\xb0\x53\x6a\xb5\xdf\xf7\xfc\x0a\x2e\x07"), }, { Hash: specs.InnerSpec.Hash, Prefix: []byte("\x06\x0c\x02\x20\x9a\xf3\x7d\xd5\x95\xa0\x19\x08\x03\xb5\xe0\x5a\xae\xf4\x2a\xe3\xfa\xd4\x99\xe4\xfb\xe3\x7f\x7c\xd3\x1c\xad\xff\x22\xa9\xee\x74\x20"), Suffix: []byte(""), }, }, }, }, // rootmulti proof ics23.CommitmentProof_Exist{ Exist: &ics23.ExistenceProof{ Key: []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"), Value: []byte("\xdc\x5b\xb5\x1b\x61\xfe\xee\x3e\x93\x07\x16\x1a\xf3\xa3\x89\xd6\x6a\x3c\xf1\x44\x1f\xef\x41\x85\xd1\xb0\x47\x55\xa5\x61\x19\xa6"), Leaf: &ics23.LeafOp{ Hash: specs.LeafSpec.Hash, PrehashKey: specs.LeafSpec.PrehashKey, PrehashValue: specs.LeafSpec.PrehashValue, Length: specs.LeafSpec.Length, Prefix: []byte("\x00"), }, Path: []*ics23.InnerOp{}, }, }, } recvPacket := types.MsgRecvPacket{ Packet: types.Packet{ Sequence: 1, SourceClient: counterpartyID, DestinationClient: clientID, TimeoutTimestamp: uint64(time.Now().Add(time.Hour).Unix()), Payloads: []types.Payload{{ SourcePort: transfer.PortID, DestinationPort: transfer.PortID, Encoding: transfer.EncodingProtobuf, Value: payloadBz, Version: transfer.V1, }}, }, ProofCommitment: proof, ProofHeight: trustedHeight, } // NOTE the following will show 0ugnot since there was no previous escrowed // ugnot before that test. println("----------- assert render total_escrow/ugnot before RecvPacket") println(transfer.Render("total_escrow/ugnot")) res := core.RecvPacket(cross(cur), recvPacket) println("\nack res:", res) // NOTE the following will show -100ugnot since there was no previous // escrowed ugnot before that test. println("----------- assert render total_escrow/ugnot after RecvPacket") println(transfer.Render("total_escrow/ugnot")) } // Output: // Payload proto: Ch90cmFuc2Zlci8wNy10ZW5kZXJtaW50LTQyL3Vnbm90EgMxMDAaCmF0b25lMXVzZXIiKGcxd2Zqa3hldGZ3ZWpoeWg2bHRhMDQ3aDZsdGEwNDdoNmxsbTgyeWw= // ----------- assert render total_escrow/ugnot before RecvPacket // {"denom":"ugnot","amount":0} // // ack res: (2 gno.land/p/aib/ibc/types.ResponseResultType) // ----------- assert render total_escrow/ugnot after RecvPacket // {"denom":"ugnot","amount":-100} // Events: // [ // { // "type": "create_client", // "attrs": [ // { // "key": "client_id", // "value": "07-tendermint-1" // }, // { // "key": "client_type", // "value": "07-tendermint" // }, // { // "key": "consensus_heights", // "value": "2/2" // } // ], // "pkg_path": "gno.land/r/aib/ibc/core" // }, // { // "type": "recv_packet", // "attrs": [ // { // "key": "packet_source_client", // "value": "07-tendermint-42" // }, // { // "key": "packet_dest_client", // "value": "07-tendermint-1" // }, // { // "key": "packet_sequence", // "value": "1" // }, // { // "key": "packet_timeout_timestamp", // "value": "1234571490" // }, // { // "key": "encoded_packet_hex", // "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a93010a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a5c0a1f7472616e736665722f30372d74656e6465726d696e742d34322f75676e6f7412033130301a0a61746f6e6531757365722228673177666a6b7865746677656a687968366c746130343768366c746130343768366c6c6d3832796c" // } // ], // "pkg_path": "gno.land/r/aib/ibc/core" // }, // { // "type": "fungible_token_packet", // "attrs": [ // { // "key": "sender", // "value": "atone1user" // }, // { // "key": "receiver", // "value": "g1wfjkxetfwejhyh6lta047h6lta047h6llm82yl" // }, // { // "key": "denom", // "value": "ugnot" // }, // { // "key": "amount", // "value": "100" // }, // { // "key": "memo", // "value": "" // }, // { // "key": "success", // "value": "true" // } // ], // "pkg_path": "gno.land/r/aib/ibc/apps/transfer" // }, // { // "type": "write_acknowledgement", // "attrs": [ // { // "key": "packet_source_client", // "value": "07-tendermint-42" // }, // { // "key": "packet_dest_client", // "value": "07-tendermint-1" // }, // { // "key": "packet_sequence", // "value": "1" // }, // { // "key": "packet_timeout_timestamp", // "value": "1234571490" // }, // { // "key": "encoded_packet_hex", // "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a93010a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a5c0a1f7472616e736665722f30372d74656e6465726d696e742d34322f75676e6f7412033130301a0a61746f6e6531757365722228673177666a6b7865746677656a687968366c746130343768366c746130343768366c6c6d3832796c" // }, // { // "key": "encoded_acknowledgement_hex", // "value": "0a117b22726573756c74223a2241513d3d227d" // } // ], // "pkg_path": "gno.land/r/aib/ibc/core" // } // ]