Skip to content

Commit

Permalink
resolve 2nd wave of comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Aug 1, 2024
1 parent e88593e commit e94fd92
Show file tree
Hide file tree
Showing 29 changed files with 157 additions and 162 deletions.
2 changes: 2 additions & 0 deletions cmd/zetaclientd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
)

// solanaTestKey is a local test private key for Solana
// TODO: use separate keys for each zetaclient in Solana E2E tests
// https://github.com/zeta-chain/node/issues/2614
var solanaTestKey = []uint8{
199, 16, 63, 28, 125, 103, 131, 13, 6, 94, 68, 109, 13, 68, 132, 17,
71, 33, 216, 51, 49, 103, 146, 241, 245, 162, 90, 228, 71, 177, 32, 199,
Expand Down
15 changes: 9 additions & 6 deletions e2e/e2etests/test_solana_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import (
func TestSolanaWithdraw(r *runner.E2ERunner, args []string) {
require.Len(r, args, 1)

// print balance of from address
solZRC20 := r.SOLZRC20
balance, err := solZRC20.BalanceOf(&bind.CallOpts{}, r.ZEVMAuth.From)
// print balanceAfter of from address
balanceBefore, err := r.SOLZRC20.BalanceOf(&bind.CallOpts{}, r.ZEVMAuth.From)
require.NoError(r, err)
r.Logger.Info("from address %s balance of SOL before: %d", r.ZEVMAuth.From, balance)
r.Logger.Info("from address %s balance of SOL before: %d", r.ZEVMAuth.From, balanceBefore)

// parse withdraw amount (in lamports), approve amount is 1 SOL
approvedAmount := new(big.Int).SetUint64(solana.LAMPORTS_PER_SOL)
Expand All @@ -38,7 +37,11 @@ func TestSolanaWithdraw(r *runner.E2ERunner, args []string) {
r.WithdrawSOLZRC20(privkey.PublicKey(), withdrawAmount, approvedAmount)

// print balance of from address after withdraw
balance, err = solZRC20.BalanceOf(&bind.CallOpts{}, r.ZEVMAuth.From)
balanceAfter, err := r.SOLZRC20.BalanceOf(&bind.CallOpts{}, r.ZEVMAuth.From)
require.NoError(r, err)
r.Logger.Info("from address %s balance of SOL after: %d", r.ZEVMAuth.From, balance)
r.Logger.Info("from address %s balance of SOL after: %d", r.ZEVMAuth.From, balanceAfter)

// check if the balance is reduced correctly
amountReduced := new(big.Int).Sub(balanceBefore, balanceAfter)
require.True(r, amountReduced.Cmp(withdrawAmount) >= 0, "balance is not reduced correctly")
}
10 changes: 5 additions & 5 deletions e2e/runner/setup_solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/pkg/chains"
solanacontract "github.com/zeta-chain/zetacore/pkg/contract/solana"
solanacontracts "github.com/zeta-chain/zetacore/pkg/contracts/solana"
)

// SetupSolanaAccount imports the deployer's private key
Expand All @@ -25,7 +25,7 @@ func (r *E2ERunner) SetSolanaContracts(deployerPrivateKey string) {
r.Logger.Print("⚙️ initializing gateway program on Solana")

// set Solana contracts
r.GatewayProgram = solana.MustPublicKeyFromBase58(solanacontract.SolanaGatewayProgramID)
r.GatewayProgram = solana.MustPublicKeyFromBase58(solanacontracts.SolanaGatewayProgramID)

// get deployer account balance
privkey, err := solana.PrivateKeyFromBase58(deployerPrivateKey)
Expand All @@ -47,8 +47,8 @@ func (r *E2ERunner) SetSolanaContracts(deployerPrivateKey string) {
inst.ProgID = r.GatewayProgram
inst.AccountValues = accountSlice

inst.DataBytes, err = borsh.Serialize(solanacontract.InitializeParams{
Discriminator: solanacontract.DiscriminatorInitialize(),
inst.DataBytes, err = borsh.Serialize(solanacontracts.InitializeParams{
Discriminator: solanacontracts.DiscriminatorInitialize(),
TssAddress: r.TSSAddress,
ChainID: uint64(chains.SolanaLocalnet.ChainId),
})
Expand All @@ -66,7 +66,7 @@ func (r *E2ERunner) SetSolanaContracts(deployerPrivateKey string) {
require.NoError(r, err)

// deserialize the PDA info
pda := solanacontract.PdaInfo{}
pda := solanacontracts.PdaInfo{}
err = borsh.Deserialize(&pda, pdaInfo.Bytes())
require.NoError(r, err)
tssAddress := ethcommon.BytesToAddress(pda.TssAddress[:])
Expand Down
2 changes: 1 addition & 1 deletion e2e/runner/solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/zeta-chain/zetacore/e2e/utils"
solanacontract "github.com/zeta-chain/zetacore/pkg/contract/solana"
solanacontract "github.com/zeta-chain/zetacore/pkg/contracts/solana"
crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package solana privides structures and constants that are used when interacting with the gateway program on Solana chain.
package solana

import (
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/gagliardetto/solana-go"
"github.com/zeta-chain/zetacore/pkg/chains"
contract "github.com/zeta-chain/zetacore/pkg/contract/solana"
contracts "github.com/zeta-chain/zetacore/pkg/contracts/solana"
)

func Test_MsgWithdrawHash(t *testing.T) {
Expand All @@ -25,7 +25,7 @@ func Test_MsgWithdrawHash(t *testing.T) {
require.NoError(t, err)

// create new withdraw message
hash := contract.NewMsgWithdraw(chainID, nonce, amount, to).Hash()
hash := contracts.NewMsgWithdraw(chainID, nonce, amount, to).Hash()
require.True(t, bytes.Equal(hash[:], wantHashBytes))
})
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
package solana // PdaInfo represents the PDA for the gateway program
package solana

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/gagliardetto/solana-go"
"github.com/near/borsh-go"
"github.com/pkg/errors"
)

type PdaInfo struct {
// Discriminator is the unique identifier for the PDA
Discriminator [8]byte

// Nonce is the current nonce for the PDA
Nonce uint64

// TssAddress is the TSS address for the PDA
TssAddress [20]byte

// Authority is the authority for the PDA
Authority [32]byte

// ChainId is the Solana chain id
ChainID uint64
}

// InitializeParams contains the parameters for a gateway initialize instruction
type InitializeParams struct {
// Discriminator is the unique identifier for the initialize instruction
Expand Down Expand Up @@ -99,6 +86,24 @@ func (inst *WithdrawInstructionParams) TokenAmount() uint64 {
return inst.Amount

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

View check run for this annotation

Codecov / codecov/patch

pkg/contracts/solana/instruction.go#L85-L86

Added lines #L85 - L86 were not covered by tests
}

// ParseInstructionWithdraw tries to parse the instruction as a 'withdraw'.
// It returns nil if the instruction can't be parsed as a 'withdraw'.
func ParseInstructionWithdraw(instruction solana.CompiledInstruction) (*WithdrawInstructionParams, error) {

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

View check run for this annotation

Codecov / codecov/patch

pkg/contracts/solana/instruction.go#L91

Added line #L91 was not covered by tests
// try deserializing instruction as a 'withdraw'
inst := &WithdrawInstructionParams{}
err := borsh.Deserialize(inst, instruction.Data)
if err != nil {
return nil, errors.Wrap(err, "error deserializing instruction")

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

View check run for this annotation

Codecov / codecov/patch

pkg/contracts/solana/instruction.go#L93-L96

Added lines #L93 - L96 were not covered by tests
}

// check the discriminator to ensure it's a 'withdraw' instruction
if inst.Discriminator != DiscriminatorWithdraw() {
return nil, errors.New("not a withdraw instruction")

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

View check run for this annotation

Codecov / codecov/patch

pkg/contracts/solana/instruction.go#L100-L101

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

return inst, nil

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

View check run for this annotation

Codecov / codecov/patch

pkg/contracts/solana/instruction.go#L104

Added line #L104 was not covered by tests
}

// RecoverSigner recover the ECDSA signer from given message hash and signature
func RecoverSigner(msgHash []byte, msgSig []byte) (signer common.Address, err error) {
// recover the public key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/require"

ethcommon "github.com/ethereum/go-ethereum/common"
contract "github.com/zeta-chain/zetacore/pkg/contract/solana"
contracts "github.com/zeta-chain/zetacore/pkg/contracts/solana"
)

const (
Expand Down Expand Up @@ -42,7 +42,7 @@ func Test_SignerWithdraw(t *testing.T) {
copy(sigRS[:], sigTest[:64])

// create a withdraw instruction
inst := contract.WithdrawInstructionParams{
inst := contracts.WithdrawInstructionParams{
Signature: sigRS,
RecoveryID: 0,
MessageHash: getTestmessageHash(),
Expand All @@ -59,21 +59,21 @@ func Test_RecoverSigner(t *testing.T) {
hashTest := getTestmessageHash()

// recover the signer from the test message hash and signature
signer, err := contract.RecoverSigner(hashTest[:], sigTest[:])
signer, err := contracts.RecoverSigner(hashTest[:], sigTest[:])
require.NoError(t, err)
require.EqualValues(t, testSigner, signer.String())

// slightly modify the signature and recover the signer
sigFake := sigTest
sigFake[0]++
signer, err = contract.RecoverSigner(hashTest[:], sigFake[:])
signer, err = contracts.RecoverSigner(hashTest[:], sigFake[:])
require.Error(t, err)
require.Equal(t, ethcommon.Address{}, signer)

// slightly modify the message hash and recover the signer
hashFake := hashTest
hashFake[0]++
signer, err = contract.RecoverSigner(hashFake[:], sigTest[:])
signer, err = contracts.RecoverSigner(hashFake[:], sigTest[:])
require.NoError(t, err)
require.NotEqual(t, ethcommon.Address{}, signer)
require.NotEqual(t, testSigner, signer.String())
Expand Down
19 changes: 19 additions & 0 deletions pkg/contracts/solana/pda.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package solana

// PdaInfo represents the PDA for the gateway program
type PdaInfo struct {
// Discriminator is the unique identifier for the PDA
Discriminator [8]byte

// Nonce is the current nonce for the PDA
Nonce uint64

// TssAddress is the TSS address for the PDA
TssAddress [20]byte

// Authority is the authority for the PDA
Authority [32]byte

// ChainId is the Solana chain id
ChainID uint64
}
4 changes: 2 additions & 2 deletions x/observer/types/chain_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/pkg/errors"

"github.com/zeta-chain/zetacore/pkg/chains"
solanacontract "github.com/zeta-chain/zetacore/pkg/contract/solana"
solanacontracts "github.com/zeta-chain/zetacore/pkg/contracts/solana"
)

const (
Expand Down Expand Up @@ -331,7 +331,7 @@ func GetDefaultSolanaLocalnetChainParams() *ChainParams {
BallotThreshold: DefaultBallotThreshold,
MinObserverDelegation: DefaultMinObserverDelegation,
IsSupported: false,
GatewayAddress: solanacontract.SolanaGatewayProgramID,
GatewayAddress: solanacontracts.SolanaGatewayProgramID,
}
}
func GetDefaultGoerliLocalnetChainParams() *ChainParams {
Expand Down
5 changes: 5 additions & 0 deletions zetaclient/chains/base/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ func (ob *Observer) WithHeaderCache(cache *lru.Cache) *Observer {
return ob
}

// OutboundID returns a unique identifier for the outbound transaction.
func (ob *Observer) OutboundID(nonce uint64) string {
return fmt.Sprintf("%d-%d", ob.chain.ChainId, nonce)

Check warning on line 272 in zetaclient/chains/base/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/base/observer.go#L271-L272

Added lines #L271 - L272 were not covered by tests
}

// DB returns the database for the observer.
func (ob *Observer) DB() *db.DB {
return ob.db
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/chains/bitcoin/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ func (ob *Observer) FetchUTXOs(ctx context.Context) error {
// SaveBroadcastedTx saves successfully broadcasted transaction
// TODO(revamp): move to db file
func (ob *Observer) SaveBroadcastedTx(txHash string, nonce uint64) {
outboundID := ob.GetTxID(nonce)
outboundID := ob.OutboundID(nonce)

Check warning on line 538 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L538

Added line #L538 was not covered by tests
ob.Mu().Lock()
ob.broadcastedTx[outboundID] = txHash
ob.Mu().Unlock()
Expand Down
20 changes: 7 additions & 13 deletions zetaclient/chains/bitcoin/observer/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ import (
"github.com/zeta-chain/zetacore/zetaclient/zetacore"
)

// GetTxID returns a unique id for outbound tx
func (ob *Observer) GetTxID(nonce uint64) string {
tssAddr := ob.TSS().BTCAddress()
return fmt.Sprintf("%d-%s-%d", ob.Chain().ChainId, tssAddr, nonce)
}

// WatchOutbound watches Bitcoin chain for outgoing txs status
// TODO(revamp): move ticker functions to a specific file
// TODO(revamp): move into a separate package
Expand Down Expand Up @@ -66,7 +60,7 @@ func (ob *Observer) WatchOutbound(ctx context.Context) error {
}
for _, tracker := range trackers {
// get original cctx parameters
outboundID := ob.GetTxID(tracker.Nonce)
outboundID := ob.OutboundID(tracker.Nonce)

Check warning on line 63 in zetaclient/chains/bitcoin/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/outbound.go#L63

Added line #L63 was not covered by tests
cctx, err := ob.ZetacoreClient().GetCctxByNonce(ctx, chainID, tracker.Nonce)
if err != nil {
ob.logger.Outbound.Info().
Expand Down Expand Up @@ -140,7 +134,7 @@ func (ob *Observer) IsOutboundProcessed(
nonce := cctx.GetCurrentOutboundParam().TssNonce

// get broadcasted outbound and tx result
outboundID := ob.GetTxID(nonce)
outboundID := ob.OutboundID(nonce)
ob.Logger().Outbound.Info().Msgf("IsOutboundProcessed %s", outboundID)

Check warning on line 138 in zetaclient/chains/bitcoin/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/outbound.go#L137-L138

Added lines #L137 - L138 were not covered by tests

ob.Mu().Lock()
Expand Down Expand Up @@ -443,7 +437,7 @@ func (ob *Observer) checkIncludedTx(
cctx *crosschaintypes.CrossChainTx,
txHash string,
) (*btcjson.GetTransactionResult, bool) {
outboundID := ob.GetTxID(cctx.GetCurrentOutboundParam().TssNonce)
outboundID := ob.OutboundID(cctx.GetCurrentOutboundParam().TssNonce)

Check warning on line 440 in zetaclient/chains/bitcoin/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/outbound.go#L440

Added line #L440 was not covered by tests
hash, getTxResult, err := rpc.GetTxResultByHash(ob.btcClient, txHash)
if err != nil {
ob.logger.Outbound.Error().Err(err).Msgf("checkIncludedTx: error GetTxResultByHash: %s", txHash)
Expand Down Expand Up @@ -472,7 +466,7 @@ func (ob *Observer) checkIncludedTx(
// setIncludedTx saves included tx result in memory
func (ob *Observer) setIncludedTx(nonce uint64, getTxResult *btcjson.GetTransactionResult) {
txHash := getTxResult.TxID
outboundID := ob.GetTxID(nonce)
outboundID := ob.OutboundID(nonce)

Check warning on line 469 in zetaclient/chains/bitcoin/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/outbound.go#L469

Added line #L469 was not covered by tests

ob.Mu().Lock()
defer ob.Mu().Unlock()
Expand Down Expand Up @@ -501,17 +495,17 @@ func (ob *Observer) setIncludedTx(nonce uint64, getTxResult *btcjson.GetTransact
func (ob *Observer) getIncludedTx(nonce uint64) *btcjson.GetTransactionResult {
ob.Mu().Lock()
defer ob.Mu().Unlock()
return ob.includedTxResults[ob.GetTxID(nonce)]
return ob.includedTxResults[ob.OutboundID(nonce)]
}

// removeIncludedTx removes included tx from memory
func (ob *Observer) removeIncludedTx(nonce uint64) {
ob.Mu().Lock()
defer ob.Mu().Unlock()
txResult, found := ob.includedTxResults[ob.GetTxID(nonce)]
txResult, found := ob.includedTxResults[ob.OutboundID(nonce)]

Check warning on line 505 in zetaclient/chains/bitcoin/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/outbound.go#L505

Added line #L505 was not covered by tests
if found {
delete(ob.includedTxHashes, txResult.TxID)
delete(ob.includedTxResults, ob.GetTxID(nonce))
delete(ob.includedTxResults, ob.OutboundID(nonce))

Check warning on line 508 in zetaclient/chains/bitcoin/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/outbound.go#L508

Added line #L508 was not covered by tests
}
}

Expand Down
2 changes: 1 addition & 1 deletion zetaclient/chains/bitcoin/observer/outbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func createObserverWithUTXOs(t *testing.T) *Observer {

func mineTxNSetNonceMark(ob *Observer, nonce uint64, txid string, preMarkIndex int) {
// Mine transaction
outboundID := ob.GetTxID(nonce)
outboundID := ob.OutboundID(nonce)
ob.includedTxResults[outboundID] = &btcjson.GetTransactionResult{TxID: txid}

// Set nonce mark
Expand Down
2 changes: 2 additions & 0 deletions zetaclient/chains/bitcoin/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ func (signer *Signer) GetERC20CustodyAddress() ethcommon.Address {
}

// SetGatewayAddress does nothing for BTC
// Note: TSS address will be used as gateway address for Bitcoin
func (signer *Signer) SetGatewayAddress(_ string) {

Check warning on line 112 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L112

Added line #L112 was not covered by tests
}

// GetGatewayAddress returns empty address
// Note: same as SetGatewayAddress
func (signer *Signer) GetGatewayAddress() string {
return ""

Check warning on line 118 in zetaclient/chains/bitcoin/signer/signer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/signer/signer.go#L117-L118

Added lines #L117 - L118 were not covered by tests
}
Expand Down
Loading

0 comments on commit e94fd92

Please sign in to comment.