Skip to content

Commit

Permalink
Fix message encoding and printing
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Nov 25, 2022
1 parent 82cd6ff commit 064828b
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,13 @@ func (mx *Message) EncodeToTree(txTree treeout.Branches) {
}
txTree.Child(text.Sf("RecentBlockhash: %s", mx.RecentBlockhash))

txTree.Child(fmt.Sprintf("AccountKeys[len=%v]", len(mx.AccountKeys)+mx.addressTableLookups.NumLookups())).ParentFunc(func(accountKeysBranch treeout.Branches) {
txTree.Child(fmt.Sprintf("AccountKeys[len=%v]", mx.numStaticAccounts()+mx.addressTableLookups.NumLookups())).ParentFunc(func(accountKeysBranch treeout.Branches) {
accountKeys, err := mx.AccountMetaList()
if err != nil {
accountKeysBranch.Child(text.RedBG(fmt.Sprintf("AccountMetaList: %s", err)))
} else {
for keyIndex, key := range accountKeys {
isFromTable := mx.IsVersioned() && keyIndex >= len(mx.AccountKeys)
isFromTable := mx.IsVersioned() && keyIndex >= mx.numStaticAccounts()
if isFromTable {
accountKeysBranch.Child(text.Sf("%s (from Address Table Lookup)", text.ColorizeBG(key.PublicKey.String())))
} else {
Expand Down Expand Up @@ -269,14 +269,14 @@ func (mx *Message) MarshalV0() ([]byte, error) {
mx.Header.NumReadonlyUnsignedAccounts,
}
{
accountKeys, err := mx.GetAllKeys()
staticAccountKeys, err := mx.getStaticKeys()
if err != nil {
return nil, err
}
// Encode only the keys that are not in the address table lookups.
accountKeys = accountKeys[:mx.numStaticAccounts()]
bin.EncodeCompactU16Length(&buf, len(accountKeys))
for _, key := range accountKeys {
staticAccountKeys = staticAccountKeys[:mx.numStaticAccounts()]
bin.EncodeCompactU16Length(&buf, len(staticAccountKeys))
for _, key := range staticAccountKeys {
buf = append(buf, key[:]...)
}

Expand Down Expand Up @@ -430,6 +430,15 @@ func (mx Message) GetAllKeys() (keys PublicKeySlice, err error) {
return append(mx.AccountKeys, atlAccounts...), nil
}

func (mx Message) getStaticKeys() (keys PublicKeySlice, err error) {
if mx.resolved {
// If the message has been resolved, then the account keys have already
// been appended to the `AccountKeys` field of the message.
return mx.AccountKeys[:mx.numStaticAccounts()], nil
}
return mx.AccountKeys, nil
}

func (mx *Message) UnmarshalV0(decoder *bin.Decoder) (err error) {
version, err := decoder.ReadByte()
if err != nil {
Expand Down

0 comments on commit 064828b

Please sign in to comment.