Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Nov 4, 2024
1 parent b4ab0cd commit 051b30f
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 57 deletions.
2 changes: 1 addition & 1 deletion e2e/runner/setup_solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (r *E2ERunner) SetupSolana(deployerPrivateKey string) {
inst.AccountValues = accountSlice

inst.DataBytes, err = borsh.Serialize(solanacontracts.InitializeParams{
Discriminator: solanacontracts.DiscriminatorInitialize(),
Discriminator: solanacontracts.DiscriminatorInitialize,
TssAddress: r.TSSAddress,
// #nosec G115 chain id always positive
ChainID: uint64(chains.SolanaLocalnet.ChainId),
Expand Down
2 changes: 1 addition & 1 deletion e2e/runner/solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (r *E2ERunner) CreateDepositInstruction(

var err error
inst.DataBytes, err = borsh.Serialize(solanacontract.DepositInstructionParams{
Discriminator: solanacontract.DiscriminatorDeposit(),
Discriminator: solanacontract.DiscriminatorDeposit,
Amount: amount,
Memo: append(receiver.Bytes(), data...),
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/chains/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func (chain Chain) IsEVMChain() bool {
return chain.Vm == Vm_evm
}

func (chain Chain) IsSVMChain() bool {
return chain.Vm == Vm_svm
func (chain Chain) IsSolanaChain() bool {
return chain.Consensus == Consensus_solana_consensus

Check warning on line 100 in pkg/chains/chain.go

View check run for this annotation

Codecov / codecov/patch

pkg/chains/chain.go#L99-L100

Added lines #L99 - L100 were not covered by tests
}

func (chain Chain) IsBitcoinChain() bool {
Expand Down
43 changes: 14 additions & 29 deletions pkg/contracts/solana/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,20 @@ const (
AccountsNumDeposit = 3
)

// DiscriminatorInitialize returns the discriminator for Solana gateway 'initialize' instruction
func DiscriminatorInitialize() [8]byte {
return idlgateway.IDLGateway.GetDiscriminator("initialize")
}

// DiscriminatorDeposit returns the discriminator for Solana gateway 'deposit' instruction
func DiscriminatorDeposit() [8]byte {
return idlgateway.IDLGateway.GetDiscriminator("deposit")
}

// DiscriminatorDepositSPL returns the discriminator for Solana gateway 'deposit_spl_token' instruction
func DiscriminatorDepositSPL() [8]byte {
return idlgateway.IDLGateway.GetDiscriminator("deposit_spl_token")
}

// DiscriminatorWithdraw returns the discriminator for Solana gateway 'withdraw' instruction
func DiscriminatorWithdraw() [8]byte {
return idlgateway.IDLGateway.GetDiscriminator("withdraw")
}

// DiscriminatorWithdrawSPL returns the discriminator for Solana gateway 'withdraw_spl_token' instruction
func DiscriminatorWithdrawSPL() [8]byte {
return idlgateway.IDLGateway.GetDiscriminator("withdraw_spl_token")
}

// DiscriminatorWhitelist returns the discriminator for Solana gateway 'whitelist_spl_mint' instruction
func DiscriminatorWhitelistSplMint() [8]byte {
return idlgateway.IDLGateway.GetDiscriminator("whitelist_spl_mint")
}
var (
// DiscriminatorInitialize returns the discriminator for Solana gateway 'initialize' instruction
DiscriminatorInitialize = idlgateway.IDLGateway.GetDiscriminator("initialize")
// DiscriminatorDeposit returns the discriminator for Solana gateway 'deposit' instruction
DiscriminatorDeposit = idlgateway.IDLGateway.GetDiscriminator("deposit")
// DiscriminatorDepositSPL returns the discriminator for Solana gateway 'deposit_spl_token' instruction
DiscriminatorDepositSPL = idlgateway.IDLGateway.GetDiscriminator("deposit_spl_token")
// DiscriminatorWithdraw returns the discriminator for Solana gateway 'withdraw' instruction
DiscriminatorWithdraw = idlgateway.IDLGateway.GetDiscriminator("withdraw")
// DiscriminatorWithdrawSPL returns the discriminator for Solana gateway 'withdraw_spl_token' instruction
DiscriminatorWithdrawSPL = idlgateway.IDLGateway.GetDiscriminator("withdraw_spl_token")
// DiscriminatorWhitelist returns the discriminator for Solana gateway 'whitelist_spl_mint' instruction
DiscriminatorWhitelistSplMint = idlgateway.IDLGateway.GetDiscriminator("whitelist_spl_mint")
)

// ParseGatewayAddressAndPda parses the gateway id and program derived address from the given string
func ParseGatewayIDAndPda(address string) (solana.PublicKey, solana.PublicKey, error) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/contracts/solana/gateway_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ func (msg *MsgWithdraw) Signer() (common.Address, error) {

// MsgWhitelist is the message for the Solana gateway whitelist_spl_mint instruction
type MsgWhitelist struct {
// whitelistCandidate is the whitelist candidate
// whitelistCandidate is the SPL token to be whitelisted in gateway program
whitelistCandidate solana.PublicKey
whitelistEntry solana.PublicKey

// whitelistEntry is the entry in gateway program representing whitelisted SPL token
whitelistEntry solana.PublicKey

// chainID is the chain ID of Solana chain
chainID uint64
Expand Down
18 changes: 18 additions & 0 deletions pkg/contracts/solana/gateway_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,21 @@ func Test_MsgWithdrawHash(t *testing.T) {
require.True(t, bytes.Equal(hash[:], wantHashBytes))
})
}

func Test_MsgWhitelistHash(t *testing.T) {
t.Run("should pass for archived inbound, receipt and cctx", func(t *testing.T) {
// #nosec G115 always positive
chainID := uint64(chains.SolanaLocalnet.ChainId)
nonce := uint64(0)
whitelistCandidate := solana.MustPublicKeyFromBase58("37yGiHAnLvWZUNVwu9esp74YQFqxU1qHCbABkDvRddUQ")
whitelistEntry := solana.MustPublicKeyFromBase58("2kJndCL9NBR36ySiQ4bmArs4YgWQu67LmCDfLzk5Gb7s")

wantHash := "cde8fa3ab24b50320db1c47f30492e789177d28e76208176f0a52b8ed54ce2dd"
wantHashBytes, err := hex.DecodeString(wantHash)
require.NoError(t, err)

// create new withdraw message
hash := contracts.NewMsgWhitelist(whitelistCandidate, whitelistEntry, chainID, nonce).Hash()
require.True(t, bytes.Equal(hash[:], wantHashBytes))
})
}
4 changes: 2 additions & 2 deletions pkg/contracts/solana/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func ParseInstructionWithdraw(instruction solana.CompiledInstruction) (*Withdraw
}

// check the discriminator to ensure it's a 'withdraw' instruction
if inst.Discriminator != DiscriminatorWithdraw() {
if inst.Discriminator != DiscriminatorWithdraw {

Check warning on line 102 in pkg/contracts/solana/instruction.go

View check run for this annotation

Codecov / codecov/patch

pkg/contracts/solana/instruction.go#L102

Added line #L102 was not covered by tests
return nil, fmt.Errorf("not a withdraw instruction: %v", inst.Discriminator)
}

Expand Down Expand Up @@ -167,7 +167,7 @@ func ParseInstructionWhitelist(instruction solana.CompiledInstruction) (*Whiteli
}

Check warning on line 167 in pkg/contracts/solana/instruction.go

View check run for this annotation

Codecov / codecov/patch

pkg/contracts/solana/instruction.go#L161-L167

Added lines #L161 - L167 were not covered by tests

// check the discriminator to ensure it's a 'whitelist_spl_mint' instruction
if inst.Discriminator != DiscriminatorWhitelistSplMint() {
if inst.Discriminator != DiscriminatorWhitelistSplMint {
return nil, fmt.Errorf("not a whitelist_spl_mint instruction: %v", inst.Discriminator)
}

Check warning on line 172 in pkg/contracts/solana/instruction.go

View check run for this annotation

Codecov / codecov/patch

pkg/contracts/solana/instruction.go#L170-L172

Added lines #L170 - L172 were not covered by tests

Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/keeper/msg_server_whitelist_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (k msgServer) WhitelistERC20(
)
}

case chain.IsSVMChain():
case chain.IsSolanaChain():
_, err := solana.PublicKeyFromBase58(msg.Erc20Address)
if err != nil {
return nil, errorsmod.Wrapf(
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/chains/solana/observer/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (ob *Observer) ParseInboundAsDeposit(
}

// check if the instruction is a deposit or not
if inst.Discriminator != solanacontracts.DiscriminatorDeposit() {
if inst.Discriminator != solanacontracts.DiscriminatorDeposit {
return nil, nil
}

Expand Down
63 changes: 63 additions & 0 deletions zetaclient/chains/solana/observer/outbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const (

// tssAddressTest is the TSS address for testing
tssAddressTest = "0x05C7dBdd1954D59c9afaB848dA7d8DD3F35e69Cd"

// whitelistTxTest is local devnet tx result for testing
whitelistTxTest = "phM9bESbiqojmpkkUxgjed8EABkxvPGNau9q31B8Yk1sXUtsxJvd6G9VbZZQPsEyn6RiTH4YBtqJ89omqfbbNNY"
)

// createTestObserver creates a test observer for testing
Expand Down Expand Up @@ -294,3 +297,63 @@ func Test_ParseInstructionWithdraw(t *testing.T) {
require.Nil(t, inst)
})
}

func Test_ParseInstructionWhitelist(t *testing.T) {
// the test chain and transaction hash
chain := chains.SolanaDevnet
txHash := whitelistTxTest
txAmount := uint64(0)

t.Run("should parse instruction whitelist", func(t *testing.T) {
// tss address used in local devnet
tssAddress := "0x7E8c7bAcd3c6220DDC35A4EA1141BE14F2e1dFEB"
// load and unmarshal archived transaction
txResult := testutils.LoadSolanaOutboundTxResult(t, TestDataDir, chain.ChainId, txHash)
tx, err := txResult.Transaction.GetTransaction()
require.NoError(t, err)

instruction := tx.Message.Instructions[0]
inst, err := contracts.ParseInstructionWhitelist(instruction)
require.NoError(t, err)

// check sender, nonce and amount
sender, err := inst.Signer()
require.NoError(t, err)
require.Equal(t, tssAddress, sender.String())
require.EqualValues(t, inst.GatewayNonce(), 3)
require.EqualValues(t, inst.TokenAmount(), txAmount)
})

t.Run("should return error on invalid instruction data", func(t *testing.T) {
// load and unmarshal archived transaction
txResult := testutils.LoadSolanaOutboundTxResult(t, TestDataDir, chain.ChainId, txHash)
txFake, err := txResult.Transaction.GetTransaction()
require.NoError(t, err)

// set invalid instruction data
instruction := txFake.Message.Instructions[0]
instruction.Data = []byte("invalid instruction data")

inst, err := contracts.ParseInstructionWhitelist(instruction)
require.ErrorContains(t, err, "error deserializing instruction")
require.Nil(t, inst)
})

t.Run("should return error on discriminator mismatch", func(t *testing.T) {
// load and unmarshal archived transaction
txResult := testutils.LoadSolanaOutboundTxResult(t, TestDataDir, chain.ChainId, txHash)
txFake, err := txResult.Transaction.GetTransaction()
require.NoError(t, err)

// overwrite discriminator (first 8 bytes)
instruction := txFake.Message.Instructions[0]
fakeDiscriminator := "b712469c946da12100980d0000000000"
fakeDiscriminatorBytes, err := hex.DecodeString(fakeDiscriminator)
require.NoError(t, err)
copy(instruction.Data, fakeDiscriminatorBytes)

inst, err := contracts.ParseInstructionWhitelist(instruction)
require.ErrorContains(t, err, "not a whitelist_spl_mint instruction")
require.Nil(t, inst)
})
}
16 changes: 8 additions & 8 deletions zetaclient/chains/solana/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (signer *Signer) TryProcessOutbound(

// skip relaying the transaction if this signer hasn't set the relayer key
if !signer.HasRelayerKey() {
logger.Warn().Msgf("TryProcessOutbound: no relayer key configured")
return
}

Check warning on line 132 in zetaclient/chains/solana/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/solana/signer/signer.go#L127-L132

Added lines #L127 - L132 were not covered by tests

Expand All @@ -152,7 +153,7 @@ func (signer *Signer) TryProcessOutbound(
tx = withdrawTx
default:
logger.Error().
Msgf("TryProcessOutbound: can only send SOL to the Solana network for chain %d nonce %d", chainID, nonce)
Msgf("TryProcessOutbound: can only send SOL to the Solana network")
return

Check warning on line 157 in zetaclient/chains/solana/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/solana/signer/signer.go#L153-L157

Added lines #L153 - L157 were not covered by tests
}

Expand All @@ -171,10 +172,9 @@ func (signer *Signer) TryProcessOutbound(
rpc.TransactionOpts{PreflightCommitment: rpc.CommitmentProcessed},
)
if err != nil {

Check warning on line 174 in zetaclient/chains/solana/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/solana/signer/signer.go#L161-L174

Added lines #L161 - L174 were not covered by tests
signer.Logger().
Std.Warn().
logger.Error().
Err(err).
Msgf("TryProcessOutbound: broadcast error for chain %d nonce %d", chainID, nonce)
Msgf("TryProcessOutbound: broadcast error")

Check warning on line 177 in zetaclient/chains/solana/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/solana/signer/signer.go#L176-L177

Added lines #L176 - L177 were not covered by tests
return
}

Expand Down Expand Up @@ -205,13 +205,13 @@ func (signer *Signer) prepareWithdrawTx(
}

// sign gateway withdraw message by TSS
msg, err := signer.SignMsgWithdraw(ctx, params, height, cancelTx)
msg, err := signer.createAndSignMsgWithdraw(ctx, params, height, cancelTx)

Check warning on line 208 in zetaclient/chains/solana/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/solana/signer/signer.go#L208

Added line #L208 was not covered by tests
if err != nil {
return nil, err

Check warning on line 210 in zetaclient/chains/solana/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/solana/signer/signer.go#L210

Added line #L210 was not covered by tests
}

// sign the withdraw transaction by relayer key
tx, err := signer.SignWithdrawTx(ctx, *msg)
tx, err := signer.signWithdrawTx(ctx, *msg)
if err != nil {
return nil, err

Check warning on line 216 in zetaclient/chains/solana/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/solana/signer/signer.go#L214-L216

Added lines #L214 - L216 were not covered by tests
}
Expand Down Expand Up @@ -242,13 +242,13 @@ func (signer *Signer) prepareWhitelistTx(
}

// sign gateway whitelist message by TSS
msg, err := signer.SignMsgWhitelist(ctx, params, height, pk, whitelistEntryPDA)
msg, err := signer.createAndSignMsgWhitelist(ctx, params, height, pk, whitelistEntryPDA)
if err != nil {
return nil, err
}

Check warning on line 248 in zetaclient/chains/solana/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/solana/signer/signer.go#L245-L248

Added lines #L245 - L248 were not covered by tests

// sign the whitelist transaction by relayer key
tx, err := signer.SignWhitelistTx(ctx, msg)
tx, err := signer.signWhitelistTx(ctx, msg)
if err != nil {
return nil, err
}

Check warning on line 254 in zetaclient/chains/solana/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/solana/signer/signer.go#L251-L254

Added lines #L251 - L254 were not covered by tests
Expand Down
10 changes: 5 additions & 5 deletions zetaclient/chains/solana/signer/whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/zeta-chain/node/x/crosschain/types"
)

// SignMsgWhitelist signs a whitelist message (for gateway whitelist_spl_mint instruction) with TSS.
func (signer *Signer) SignMsgWhitelist(
// createAndSignMsgWhitelist creates and signs a whitelist message (for gateway whitelist_spl_mint instruction) with TSS.
func (signer *Signer) createAndSignMsgWhitelist(
ctx context.Context,
params *types.OutboundParams,
height uint64,
Expand Down Expand Up @@ -41,13 +41,13 @@ func (signer *Signer) SignMsgWhitelist(
return msg.SetSignature(signature), nil

Check warning on line 41 in zetaclient/chains/solana/signer/whitelist.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/solana/signer/whitelist.go#L22-L41

Added lines #L22 - L41 were not covered by tests
}

// SignWhitelistTx wraps the whitelist 'msg' into a Solana transaction and signs it with the relayer key.
func (signer *Signer) SignWhitelistTx(ctx context.Context, msg *contracts.MsgWhitelist) (*solana.Transaction, error) {
// signWhitelistTx wraps the whitelist 'msg' into a Solana transaction and signs it with the relayer key.
func (signer *Signer) signWhitelistTx(ctx context.Context, msg *contracts.MsgWhitelist) (*solana.Transaction, error) {
// create whitelist_spl_mint instruction with program call data
var err error
var inst solana.GenericInstruction
inst.DataBytes, err = borsh.Serialize(contracts.WhitelistInstructionParams{
Discriminator: contracts.DiscriminatorWhitelistSplMint(),
Discriminator: contracts.DiscriminatorWhitelistSplMint,
Signature: msg.SigRS(),
RecoveryID: msg.SigV(),
MessageHash: msg.Hash(),
Expand Down
10 changes: 5 additions & 5 deletions zetaclient/chains/solana/signer/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/zeta-chain/node/x/crosschain/types"
)

// SignMsgWithdraw signs a withdraw message (for gateway withdraw/withdraw_spl instruction) with TSS.
func (signer *Signer) SignMsgWithdraw(
// createAndSignMsgWithdraw creates and signs a withdraw message (for gateway withdraw/withdraw_spl instruction) with TSS.
func (signer *Signer) createAndSignMsgWithdraw(
ctx context.Context,
params *types.OutboundParams,
height uint64,
Expand Down Expand Up @@ -53,13 +53,13 @@ func (signer *Signer) SignMsgWithdraw(
return msg.SetSignature(signature), nil
}

// SignWithdrawTx wraps the withdraw 'msg' into a Solana transaction and signs it with the relayer key.
func (signer *Signer) SignWithdrawTx(ctx context.Context, msg contracts.MsgWithdraw) (*solana.Transaction, error) {
// signWithdrawTx wraps the withdraw 'msg' into a Solana transaction and signs it with the relayer key.
func (signer *Signer) signWithdrawTx(ctx context.Context, msg contracts.MsgWithdraw) (*solana.Transaction, error) {

Check warning on line 57 in zetaclient/chains/solana/signer/withdraw.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/solana/signer/withdraw.go#L57

Added line #L57 was not covered by tests
// create withdraw instruction with program call data
var err error
var inst solana.GenericInstruction
inst.DataBytes, err = borsh.Serialize(contracts.WithdrawInstructionParams{
Discriminator: contracts.DiscriminatorWithdraw(),
Discriminator: contracts.DiscriminatorWithdraw,

Check warning on line 62 in zetaclient/chains/solana/signer/withdraw.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/solana/signer/withdraw.go#L62

Added line #L62 was not covered by tests
Amount: msg.Amount(),
Signature: msg.SigRS(),
RecoveryID: msg.SigV(),
Expand Down
Loading

0 comments on commit 051b30f

Please sign in to comment.