Skip to content

Commit

Permalink
Unit tests and rename encoding method
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrit committed Sep 19, 2023
1 parent d213dcf commit 88d230a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
37 changes: 37 additions & 0 deletions x/axelarcork/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,40 @@ func (suite *KeeperTestSuite) TestAxelarCorkContractNonces() {
{chain2, address2, 2},
}, data)
}

func (suite *KeeperTestSuite) TestAxelarProxyArgsEncoding() {
require := suite.Require()

address := "0x0000000000000000000000000000000000000000"
cellars := []string{sampleCellarHex, "0x1111111111111111111111111111111111111111"}

_, err := types.EncodeLogicCallArgs(address, 1, 100000000, []byte("testcall"))
require.NoError(err)
_, err = types.EncodeUpgradeArgs(address, cellars)
require.NoError(err)
}

// TestAxelarProxyUpgradeData tests the upgrade data set, get, and delete functions
func (suite *KeeperTestSuite) TestAxelarProxyUpgradeData() {
ctx, axelarcorkKeeper := suite.ctx, suite.axelarcorkKeeper
require := suite.Require()

chainID := uint64(TestEVMChainID)
address := "0x0000000000000000000000000000000000000000"
cellars := []string{sampleCellarHex, "0x1111111111111111111111111111111111111111"}
payload, err := types.EncodeUpgradeArgs(address, cellars)
require.NoError(err)
upgradeData := types.AxelarUpgradeData{
Payload: payload,
ExecutableHeightThreshold: 10000000,
}

axelarcorkKeeper.SetAxelarProxyUpgradeData(ctx, chainID, upgradeData)
actualUpgradeData, found := axelarcorkKeeper.GetAxelarProxyUpgradeData(ctx, chainID)
require.True(found)
require.Equal(upgradeData, actualUpgradeData)

axelarcorkKeeper.DeleteAxelarProxyUpgradeData(ctx, chainID)
_, found = axelarcorkKeeper.GetAxelarProxyUpgradeData(ctx, chainID)
require.False(found)
}
2 changes: 1 addition & 1 deletion x/axelarcork/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (k Keeper) RelayCork(c context.Context, msg *types.MsgRelayAxelarCorkReques
}

nonce := k.IncrementAxelarContractCallNonce(ctx, msg.ChainId, cork.TargetContractAddress)
payload, err := types.EncodeExecuteArgs(msg.TargetContractAddress, nonce, cork.Deadline, cork.EncodedContractCall)
payload, err := types.EncodeLogicCallArgs(msg.TargetContractAddress, nonce, cork.Deadline, cork.EncodedContractCall)
if err != nil {
return nil, err
}
Expand Down
20 changes: 12 additions & 8 deletions x/axelarcork/types/axelar_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)

var (
Expand All @@ -14,27 +15,30 @@ var (

HourInBlocks = uint64((60 * 60) / 12)
DefaultExecutableHeightThreshold = 72 * HourInBlocks
)

type UpgradeData struct {
Payload []byte
ExecutionThresholdHeight uint64
}
LogicCallMsgID = big.NewInt(0)
UpgradeMsgID = big.NewInt(1)
)

func EncodeExecuteArgs(targetContract string, nonce uint64, deadline uint64, callData []byte) ([]byte, error) {
func EncodeLogicCallArgs(targetContract string, nonce uint64, deadline uint64, callData []byte) ([]byte, error) {
return abi.Arguments{
{Type: Uint256},
{Type: Address},
{Type: Uint256},
{Type: Uint256},
{Type: Bytes},
}.Pack(big.NewInt(0), targetContract, big.NewInt(int64(nonce)), big.NewInt(int64(deadline)), callData)
}.Pack(LogicCallMsgID, common.HexToAddress(targetContract), big.NewInt(int64(nonce)), big.NewInt(int64(deadline)), callData)
}

func EncodeUpgradeArgs(newAxelarProxy string, targets []string) ([]byte, error) {
targetAddrs := []common.Address{}
for _, target := range targets {
targetAddrs = append(targetAddrs, common.HexToAddress(target))
}

return abi.Arguments{
{Type: Uint256},
{Type: Address},
{Type: AddressArr},
}.Pack(big.NewInt(1), newAxelarProxy, targets)
}.Pack(UpgradeMsgID, common.HexToAddress(newAxelarProxy), targetAddrs)
}

0 comments on commit 88d230a

Please sign in to comment.