gagliardetto / binary

Encoding/decoding in Borsh and other formats.
Apache License 2.0
19 stars 8 forks source link

Unable to use unsafe.Sizeof() if struct contains a Uint128 #8

Open lucasoares opened 5 months ago

lucasoares commented 5 months ago

If a struct contains bin.Uint128 fields, the result of unsafe.Sizeof for the struct will be 16 bytes higher for each bin.Uint128 field it contains... The result of unsafe.Offsetof will also differ based on the location of the struct's fields.

How can we use unsafe helpers when using the Solana SDK to filter requests (subscriptions, etc) when using the Uint128?

It seems this library is not a good option to use when working with Solana programs for now, but how can we make it be? For now I'm using a slice of uint64 with 2 positions to represent uint128 :(

gagliardetto commented 5 months ago

when using the Solana SDK to filter requests (subscriptions, etc)

Can you tell me more?

lucasoares commented 5 months ago

when using the Solana SDK to filter requests (subscriptions, etc)

Can you tell me more?

Example of something I can't do with the current implementation if I use Uint128:

    info := AmmInfo{}
    subscription, err = wsConnection.ProgramSubscribeWithOpts(
        <other parameters>,
        []rpc.RPCFilter{
            {
                DataSize: uint64(unsafe.Sizeof(info)),
            },
            {
                Memcmp: &rpc.RPCFilterMemcmp{
                    Offset: uint64(unsafe.Offsetof(info.PcVaultMint)),
                    Bytes:  SOL[:],
                },
            },
            {
                Memcmp: &rpc.RPCFilterMemcmp{
                    Offset: uint64(unsafe.Offsetof(info.MarketProgram)),
                    Bytes:  open_book.MAINNET_OPENBOOK_MARKET[:],
                },
            },
        },
    )

If the AmmInfo struct contains any UInt128, the result of Sizeof and Offsetof will be incorrect.