Skip to content

Marshalling Transaction from RPC response with EncodingJSON sets incorrect versioning #339

@allen-moonshot

Description

@allen-moonshot

Issue
When calling an RPC for a method such as GetTransactions with encoding EncodingJSON, the versioning is always being set to legacy regardless of if it actually is

But if one sets EncodingBase64 then the call to GetTransaction() has the version set correctly

The resolution is to either use EncodingBase64 or to manually set version with resJson.Message.SetVersion(solana.MessageVersion(rpc.MaxSupportedTransactionVersion0))

This is particularly problematic when one wants to resolve ALTs

possible causes

  1. in message.go we define legacy as 0 while in rpc/transacton_version.go it in defined differently, thus when the RPC response contains 0 it is being decoded to legacy as eluded to in serious bug: message version Legacy and 0 is not mapped to 0 and 1 #180
  2. struct Message field version is set to private with no JSON tag

message.go

const (
	MessageVersionLegacy MessageVersion = 0 // default
	MessageVersionV0     MessageVersion = 1 // v0
)

rpc/transaction_version.go

const (
	LegacyTransactionVersion TransactionVersion = -1
	legacyVersion                               = `"legacy"`
)

Reproduction code

sig := "5tfxN6bZLz91P5YSKnu6XyEEbdTUz7emF2jQBbPxiND8Z7Zu5u5s2A4XTGU9xt13SugSwL8667gHVpzeDX33d7Wd"

resJson, _ := c.rpc.GetTransaction(context.Background(), sig, &rpc.GetTransactionOpts{
	Commitment:                     rpc.CommitmentConfirmed,
	MaxSupportedTransactionVersion: &rpc.MaxSupportedTransactionVersion0,
	// Encoding: solana.EncodingJSON, // this is default behaviour
})

// this just returns the already parsed txn under the hood
// so presumably the json parsing of solana.Transaction is wrong
txnJson, err := resJson.Transaction.GetTransaction()


resBase64, _ := c.rpc.GetTransaction(context.Background(), sig, &rpc.GetTransactionOpts{
	Commitment:                     rpc.CommitmentConfirmed,
	MaxSupportedTransactionVersion: &rpc.MaxSupportedTransactionVersion0,
	Encoding: solana.EncodingBase64,
})
txnBase64, err := resBase64.Transaction.GetTransaction()

fmt.Println(resJson.Message.version) // 0
fmt.Println(resBase64.Message.version) // 1
fmt.Println(txnJson.Message.IsVersioned()) // false
fmt.Println(txnBase64.Message.IsVersioned()) // true

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions