Skip to content

[B31] Mithril adopts code design decisions from Agave #133

@juliankuners

Description

@juliankuners

Agave makes use of index variables of type unsigned integer that refer to a specific element in a slice. This pattern is used throughout the code base for referring to accounts.

Mithril adopts the same pattern, utilizing uint64 typed variables to refer to elements in slices. This results in structs with uint64 variables that only implicitly refer to the types they point to:

type InstructionAccount struct {
	IndexInTransaction uint64
	IndexInCaller      uint64
	IndexInCallee      uint64
	IsSigner           bool
	IsWritable         bool
}
type BorrowedAccount struct {
	TxCtx              *TransactionCtx
	InstrCtx           *InstructionCtx
	IndexInTransaction uint64
	IndexInInstruction uint64
	Account            *accounts.Account
}

The actual type of the element these indices point to can be inferred by helper functions that utilize these indices:

func (instrCtx *InstructionCtx) IndexOfInstructionAccountInTransaction(instrAcctIdx uint64) (uint64, error) {
	if len(instrCtx.InstructionAccounts) == 0 || instrAcctIdx > uint64(len(instrCtx.InstructionAccounts)-1) {
		return 0, InstrErrNotEnoughAccountKeys
	}
	return instrCtx.InstructionAccounts[instrAcctIdx].IndexInTransaction, nil
}

This pattern reduces readability of Mithril's code.

However, since Go has native garbage collection support, Mithril could instead employ a different pattern with pointer references to accounts instead of the utilization of index variables and helper functions. This would improve readability.

Recommendation

We recommend to refactor the codebase and replace index variables with Go pointers to the underlying datatype. E.g., the struct BorrowedAccount implementation could be changed to:

type BorrowedAccount struct {
	TxCtx            *TransactionCtx
	InstrCtx        *InstructionCtx
	Key                *solana.PublicKey
	InstAccount *InstructionAccount
	Account        *accounts.Account
        // [...] all other references that were inferred using the indices
}

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