-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DPA-1413: fix(solana): IsOperation in timelock inspector #231
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@smartcontractkit/mcms": minor | ||
--- | ||
|
||
feat(solana): timelock inspection - operation statuses check |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package solana | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/gagliardetto/solana-go/rpc" | ||
"github.com/smartcontractkit/chainlink-ccip/chains/solana/gobindings/mcm" | ||
"github.com/smartcontractkit/chainlink-ccip/chains/solana/gobindings/timelock" | ||
solanaCommon "github.com/smartcontractkit/chainlink-ccip/chains/solana/utils/common" | ||
|
||
"github.com/smartcontractkit/mcms/sdk" | ||
) | ||
|
||
var _ sdk.TimelockInspector = (*TimelockInspector)(nil) | ||
|
||
var TimelockOpDoneTimestamp = uint64(1) | ||
|
||
// TimelockInspector is an Inspector implementation for Solana chains for accessing the RBACTimelock contract | ||
type TimelockInspector struct { | ||
client *rpc.Client | ||
} | ||
|
||
// NewTimelockInspector creates a new TimelockInspector | ||
func NewTimelockInspector(client *rpc.Client) *TimelockInspector { | ||
return &TimelockInspector{client: client} | ||
} | ||
|
||
func (t TimelockInspector) GetProposers(ctx context.Context, address string) ([]common.Address, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (t TimelockInspector) GetExecutors(ctx context.Context, address string) ([]common.Address, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (t TimelockInspector) GetBypassers(ctx context.Context, address string) ([]common.Address, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (t TimelockInspector) GetCancellers(ctx context.Context, address string) ([]common.Address, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (t TimelockInspector) IsOperation(ctx context.Context, address string, opID [32]byte) (bool, error) { | ||
_, err := t.getOpData(ctx, address, opID) | ||
if err != nil { | ||
if errors.Is(err, rpc.ErrNotFound) { | ||
return false, nil | ||
} | ||
|
||
return false, err | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (t TimelockInspector) IsOperationPending(ctx context.Context, address string, opID [32]byte) (bool, error) { | ||
op, err := t.getOpData(ctx, address, opID) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return op.Timestamp > TimelockOpDoneTimestamp, nil | ||
} | ||
|
||
func (t TimelockInspector) IsOperationReady(ctx context.Context, address string, opID [32]byte) (bool, error) { | ||
op, err := t.getOpData(ctx, address, opID) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
blockTime, err := solanaCommon.GetBlockTime(ctx, t.client, rpc.CommitmentConfirmed) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
ts, err := safeConvertUint64ToInt64(op.Timestamp) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
// a safety catch to prevent nil blocktime - ideally should not happen | ||
if blockTime == nil { | ||
return false, errors.New("failed to get block time: nil value") | ||
} | ||
|
||
return op.Timestamp > TimelockOpDoneTimestamp && ts <= int64(*blockTime), nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have to perform these check manually because anchor-go does not generate bindings for these. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we be 100% sure that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i can add a guard to be safe. |
||
} | ||
|
||
func (t TimelockInspector) IsOperationDone(ctx context.Context, address string, opID [32]byte) (bool, error) { | ||
op, err := t.getOpData(ctx, address, opID) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return op.Timestamp == TimelockOpDoneTimestamp, nil | ||
} | ||
|
||
func (t TimelockInspector) getOpData(ctx context.Context, address string, opID [32]byte) (timelock.Operation, error) { | ||
programID, seed, err := ParseContractAddress(address) | ||
if err != nil { | ||
return timelock.Operation{}, err | ||
} | ||
|
||
mcm.SetProgramID(programID) | ||
|
||
pda, err := FindTimelockOperationPDA(programID, seed, opID) | ||
if err != nil { | ||
return timelock.Operation{}, err | ||
} | ||
|
||
var opAccount timelock.Operation | ||
err = solanaCommon.GetAccountDataBorshInto(ctx, t.client, pda, rpc.CommitmentConfirmed, &opAccount) | ||
if err != nil { | ||
return timelock.Operation{}, err | ||
} | ||
|
||
return opAccount, nil | ||
} | ||
|
||
func safeConvertUint64ToInt64(value uint64) (int64, error) { | ||
const maxInt64 = int64(^uint64(0) >> 1) | ||
|
||
if value > uint64(maxInt64) { | ||
return 0, errors.New("cannot convert uint64 to int64: value out of int64 range") | ||
} | ||
|
||
return int64(value), nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rather than converting, could you make
TimelockOpDoneTimestamp
into a uint64?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you mean something like
uint64(op.Timestamp)
, then lint will complain