From 770c39aecf6f7824981291c503eab245b84edffd Mon Sep 17 00:00:00 2001 From: Alexey Kostenko Date: Mon, 21 Oct 2024 15:36:51 +0300 Subject: [PATCH 1/2] ExtIn message normalization --- tlb/messages.go | 16 +++++++++++- tlb/messages_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/tlb/messages.go b/tlb/messages.go index d68f8fe..7b51285 100644 --- a/tlb/messages.go +++ b/tlb/messages.go @@ -24,7 +24,21 @@ type Message struct { // Hash returns a hash of this Message. func (m *Message) Hash() Bits256 { - return m.hash + if m.Info.SumType != "ExtInMsgInfo" { + return m.hash + } + // normalize ExtIn message + c := boc.NewCell() + _ = c.WriteUint(2, 2) // message$_ -> info:CommonMsgInfo -> ext_in_msg_info$10 + _ = c.WriteUint(0, 2) // message$_ -> info:CommonMsgInfo -> src:MsgAddressExt -> addr_none$00 + _ = Marshal(c, m.Info.ExtInMsgInfo.Dest) // message$_ -> info:CommonMsgInfo -> dest:MsgAddressInt + _ = c.WriteUint(0, 4) // message$_ -> info:CommonMsgInfo -> import_fee:Grams -> 0 + _ = c.WriteBit(false) // message$_ -> init:(Maybe (Either StateInit ^StateInit)) -> nothing$0 + _ = c.WriteBit(true) // message$_ -> body:(Either X ^X) -> right$1 + body := boc.Cell(m.Body.Value) + _ = c.AddRef(&body) + hash, _ := c.Hash256() + return hash } func (m *Message) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error { diff --git a/tlb/messages_test.go b/tlb/messages_test.go index 4eb1270..b89c785 100644 --- a/tlb/messages_test.go +++ b/tlb/messages_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "math/big" "os" "reflect" "testing" @@ -19,6 +20,65 @@ func byte32FromHex(x string) [32]byte { return result } +func TestMessage_normalized_hash(t *testing.T) { + info := struct { + Src MsgAddress + Dest MsgAddress + ImportFee VarUInteger16 + }{ + Src: MsgAddress{ + SumType: "AddrStd", + AddrStd: struct { + Anycast Maybe[Anycast] + WorkchainId int8 + Address Bits256 + }{ + WorkchainId: -1, + Address: byte32FromHex("adfd5f1d28db13e50591d5c76a976c15d8ab6cad90554748ab254871390d9334"), + }, + }, + Dest: MsgAddress{ + SumType: "AddrStd", + AddrStd: struct { + Anycast Maybe[Anycast] + WorkchainId int8 + Address Bits256 + }{ + Anycast: Maybe[Anycast]{ + Exists: true, + Value: Anycast{ + Depth: 16, + RewritePfx: 9, + }, + }, + WorkchainId: -1, + Address: byte32FromHex("adfd5f1d28db13e50591d5c76a976c15d8ab6cad90554748ab254871390d9334"), + }, + }, + ImportFee: VarUInteger16(*big.NewInt(12364)), + } + msg := Message{ + Info: CommonMsgInfo{ + SumType: "ExtInMsgInfo", + ExtInMsgInfo: &info, + }, + } + msg.Init.Exists = true + msg.Init.Value.IsRight = true + msg.Init.Value.Value.Code.Exists = true + code := boc.NewCell() + _ = code.WriteUint(102, 32) + msg.Init.Value.Value.Code.Value.Value = *code + + body := boc.NewCell() + _ = body.WriteUint(200, 32) + msg.Body.Value = Any(*body) + + if msg.Hash().Hex() != "4fa6ab2c6fa87a22eeb458acee34cefababeb328e5a9fb37846be730ba8305e2" { + t.Fatalf("invalid mesg hash") + } +} + func TestMsgAddress_JSON(t *testing.T) { bitstr := boc.NewBitString(16) From db923ea93cceddce2f3b9660650745e66d5ae0d1 Mon Sep 17 00:00:00 2001 From: Alexey Kostenko Date: Mon, 21 Oct 2024 15:38:48 +0300 Subject: [PATCH 2/2] fix test --- tlb/messages_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tlb/messages_test.go b/tlb/messages_test.go index b89c785..1bd5de5 100644 --- a/tlb/messages_test.go +++ b/tlb/messages_test.go @@ -304,7 +304,7 @@ func TestMessage_Marshal_and_Unmarshal(t *testing.T) { name: "ExtInMsg with body", boc: "te6ccgEBAgEAqgAB4YgA2ZpktQsYby0n9cV5VWOFINBjScIU2HdondFsK3lDpEAFG8W4Jpf7AeOqfzL9vZ79mX3eM6UEBxZvN6+QmpYwXBq32QOBIrP4lF5ijGgQmZbC6KDeiiptxmTNwl5f59OAGU1NGLsixYlYAAAA2AAcAQBoYgBZQOG7qXmeA/2Tw1pLX2IkcQ5h5fxWzzcBskMJbVVRsKNaTpAAAAAAAAAAAAAAAAAAAA==", filename: "testdata/message-1", - wantHash: "d5376cf6e9de8813d0640016545000e17bcc399bd654826f4fd7a3000b2fad68", + wantHash: "23ff6f150d573f64d5599a57813f991882b7b4d5ae0550ebd08ea658431e62f6", }, { name: "IntMsg with body",