Skip to content

Commit

Permalink
Merge pull request #313 from tonkeeper/normalized-external-hash
Browse files Browse the repository at this point in the history
Normalized external hash
  • Loading branch information
mr-tron authored Nov 8, 2024
2 parents becd5c2 + db923ea commit d64e889
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
16 changes: 15 additions & 1 deletion tlb/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
62 changes: 61 additions & 1 deletion tlb/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"math/big"
"os"
"reflect"
"testing"
Expand All @@ -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)
Expand Down Expand Up @@ -244,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",
Expand Down

0 comments on commit d64e889

Please sign in to comment.