Skip to content
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

feat: add mutable BytesToSign to skyway module #1258

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions proto/palomachain/paloma/skyway/batch.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ message OutgoingTxBatch {
bytes bytes_to_sign = 7;
string assignee = 8;
uint64 gas_estimate = 9;
bytes assignee_remote_address = 10;
}

// OutgoingTransferTx represents an individual send from Paloma to remote chain
Expand Down
27 changes: 26 additions & 1 deletion x/skyway/keeper/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,19 @@ func (k Keeper) BuildOutgoingTXBatch(
return nil, err
}

batch, err := types.NewInternalOutgingTxBatch(nextID, k.getBatchTimeoutHeight(ctx), selectedTxs, contract, 0, chainReferenceID, turnstoneID, assignee, 0)
assigneeValAddr, err := sdk.ValAddressFromBech32(assignee)
if err != nil {
return nil, fmt.Errorf("invalid validator address: %w", err)
}
assigneeRemoteAddress, found, err := k.GetEthAddressByValidator(ctx, assigneeValAddr, chainReferenceID)
if err != nil {
return nil, fmt.Errorf("failed to get remote address by validator: %w", err)
}
if !found {
return nil, fmt.Errorf("no remote address found for validator %s", assignee)
}

batch, err := types.NewInternalOutgingTxBatch(nextID, k.getBatchTimeoutHeight(ctx), selectedTxs, contract, 0, chainReferenceID, turnstoneID, assignee, assigneeRemoteAddress, 0)
if err != nil {
return nil, sdkerrors.Wrap(err, "unable to create batch")
}
Expand Down Expand Up @@ -186,7 +198,20 @@ func (k Keeper) UpdateBatchGasEstimate(c context.Context, batch types.InternalOu
if entity.GasEstimate > 0 {
return fmt.Errorf("batch gas estimate already set")
}
// Update estimate
entity.GasEstimate = estimate

// Recalculate the checkpoint and store on batch
ci, err := k.EVMKeeper.GetChainInfo(ctx, batch.ChainReferenceID)
if err != nil {
return fmt.Errorf("failed to get chain info: %w", err)
}
bts, err := entity.GetCheckpoint(string(ci.SmartContractUniqueID))
if err != nil {
return fmt.Errorf("failed to get checkpoint: %w", err)
}
entity.BytesToSign = bts

store := k.GetStore(ctx, types.StoreModulePrefix)
key := types.GetOutgoingTxBatchKey(batch.TokenContract, batch.BatchNonce)
externalBatch := entity.ToExternal()
Expand Down
11 changes: 11 additions & 0 deletions x/skyway/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func TestLastPendingBatchRequest(t *testing.T) {
got.Batch[0].BatchTimeout = 0
got.Batch[0].BytesToSign = nil
got.Batch[0].Assignee = ""
got.Batch[0].AssigneeRemoteAddress = nil

assert.Equal(t, &spec.expResp, got, got)
})
Expand Down Expand Up @@ -231,6 +232,7 @@ func TestQueryBatch(t *testing.T) {
batch.Batch.BatchTimeout = 0
batch.Batch.BytesToSign = nil
batch.Batch.Assignee = ""
batch.Batch.AssigneeRemoteAddress = nil

assert.Equal(t, &expectedRes, batch, batch)
}
Expand Down Expand Up @@ -260,10 +262,17 @@ func TestLastBatchesRequest(t *testing.T) {
require.Len(t, lastBatches.Batches, 0)

// Make sure gas is set, otherwise we don't hand it out for relaying
oldCheckpoints := make(map[uint64][]byte)
k.IterateOutgoingTxBatches(ctx, func(key []byte, batch types.InternalOutgoingTxBatch) bool {
oldCheckpoints[batch.BatchNonce] = batch.BytesToSign
k.UpdateBatchGasEstimate(ctx, batch, 21_000)
return false
})
// Make sure the bytes to sign are changed after updating the gas estimate
k.IterateOutgoingTxBatches(ctx, func(key []byte, batch types.InternalOutgoingTxBatch) bool {
require.NotEqual(t, oldCheckpoints[batch.BatchNonce], batch.BytesToSign, "should have changed the bytes to sign")
return false
})

lastBatches, err = k.OutgoingTxBatches(ctx, &types.QueryOutgoingTxBatchesRequest{
ChainReferenceId: "test-chain",
Expand Down Expand Up @@ -352,9 +361,11 @@ func TestLastBatchesRequest(t *testing.T) {
lastBatches.Batches[0].BatchTimeout = 0
lastBatches.Batches[0].BytesToSign = nil
lastBatches.Batches[0].Assignee = ""
lastBatches.Batches[0].AssigneeRemoteAddress = nil
lastBatches.Batches[1].BatchTimeout = 0
lastBatches.Batches[1].BytesToSign = nil
lastBatches.Batches[1].Assignee = ""
lastBatches.Batches[1].AssigneeRemoteAddress = nil

assert.Equal(t, &expectedRes, lastBatches, "json is equal")
}
Expand Down
115 changes: 68 additions & 47 deletions x/skyway/types/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import (
"github.com/VolumeFi/whoops"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)

const (
cConservativeDummyGasEstimate = 300_000
)

func (o OutgoingTransferTx) ToInternal() (*InternalOutgoingTransferTx, error) {
return NewInternalOutgoingTransferTx(o.Id, o.Sender, o.DestAddress,
o.Erc20Token, o.BridgeTaxAmount)
Expand Down Expand Up @@ -83,15 +87,16 @@ type InternalOutgoingTxBatches []InternalOutgoingTxBatch

// InternalOutgoingTxBatch is an internal duplicate of OutgoingTxBatch with validation
type InternalOutgoingTxBatch struct {
BatchNonce uint64
BatchTimeout uint64
Transactions []*InternalOutgoingTransferTx
TokenContract EthAddress
PalomaBlockCreated uint64
ChainReferenceID string
BytesToSign []byte
Assignee string
GasEstimate uint64
BatchNonce uint64
BatchTimeout uint64
Transactions []*InternalOutgoingTransferTx
TokenContract EthAddress
PalomaBlockCreated uint64
ChainReferenceID string
BytesToSign []byte
Assignee string
GasEstimate uint64
AssigneeRemoteAddress common.Address
}

func NewInternalOutgingTxBatch(
Expand All @@ -103,17 +108,19 @@ func NewInternalOutgingTxBatch(
chainReferenceID string,
turnstoneID string,
assignee string,
assigneeRemoteAddress *EthAddress,
gasEstimate uint64,
) (*InternalOutgoingTxBatch, error) {
ret := &InternalOutgoingTxBatch{
BatchNonce: nonce,
BatchTimeout: timeout,
Transactions: transactions,
TokenContract: contract,
PalomaBlockCreated: blockCreated,
ChainReferenceID: chainReferenceID,
Assignee: assignee,
GasEstimate: gasEstimate,
BatchNonce: nonce,
BatchTimeout: timeout,
Transactions: transactions,
TokenContract: contract,
PalomaBlockCreated: blockCreated,
ChainReferenceID: chainReferenceID,
Assignee: assignee,
GasEstimate: gasEstimate,
AssigneeRemoteAddress: assigneeRemoteAddress.GetAddress(),
}
bytesToSign, err := ret.GetCheckpoint(turnstoneID)
if err != nil {
Expand Down Expand Up @@ -142,15 +149,16 @@ func NewInternalOutgingTxBatchFromExternalBatch(batch OutgoingTxBatch) (*Interna
}

intBatch := InternalOutgoingTxBatch{
BatchNonce: batch.BatchNonce,
BatchTimeout: batch.BatchTimeout,
Transactions: txs,
TokenContract: *contractAddr,
PalomaBlockCreated: batch.PalomaBlockCreated,
ChainReferenceID: batch.ChainReferenceId,
BytesToSign: batch.BytesToSign,
Assignee: batch.Assignee,
GasEstimate: batch.GasEstimate,
BatchNonce: batch.BatchNonce,
BatchTimeout: batch.BatchTimeout,
Transactions: txs,
TokenContract: *contractAddr,
PalomaBlockCreated: batch.PalomaBlockCreated,
ChainReferenceID: batch.ChainReferenceId,
BytesToSign: batch.BytesToSign,
Assignee: batch.Assignee,
AssigneeRemoteAddress: common.BytesToAddress(batch.AssigneeRemoteAddress),
GasEstimate: batch.GasEstimate,
}
return &intBatch, nil
}
Expand All @@ -165,15 +173,16 @@ func (i *InternalOutgoingTxBatch) ToExternal() OutgoingTxBatch {
txs[i] = tx.ToExternal()
}
return OutgoingTxBatch{
BatchNonce: i.BatchNonce,
BatchTimeout: i.BatchTimeout,
Transactions: txs,
TokenContract: i.TokenContract.GetAddress().Hex(),
PalomaBlockCreated: i.PalomaBlockCreated,
ChainReferenceId: i.ChainReferenceID,
BytesToSign: i.BytesToSign,
Assignee: i.Assignee,
GasEstimate: i.GasEstimate,
BatchNonce: i.BatchNonce,
BatchTimeout: i.BatchTimeout,
Transactions: txs,
TokenContract: i.TokenContract.GetAddress().Hex(),
PalomaBlockCreated: i.PalomaBlockCreated,
ChainReferenceId: i.ChainReferenceID,
BytesToSign: i.BytesToSign,
Assignee: i.Assignee,
GasEstimate: i.GasEstimate,
AssigneeRemoteAddress: i.AssigneeRemoteAddress.Bytes(),
}
}

Expand All @@ -187,15 +196,16 @@ func (i *InternalOutgoingTxBatches) ToExternalArray() []OutgoingTxBatch {
}

arr = append(arr, OutgoingTxBatch{
BatchNonce: val.BatchNonce,
BatchTimeout: val.BatchTimeout,
Transactions: txs,
TokenContract: val.TokenContract.GetAddress().Hex(),
PalomaBlockCreated: val.PalomaBlockCreated,
ChainReferenceId: val.ChainReferenceID,
BytesToSign: val.BytesToSign,
Assignee: val.Assignee,
GasEstimate: val.GasEstimate,
BatchNonce: val.BatchNonce,
BatchTimeout: val.BatchTimeout,
Transactions: txs,
TokenContract: val.TokenContract.GetAddress().Hex(),
PalomaBlockCreated: val.PalomaBlockCreated,
ChainReferenceId: val.ChainReferenceID,
BytesToSign: val.BytesToSign,
Assignee: val.Assignee,
GasEstimate: val.GasEstimate,
AssigneeRemoteAddress: val.AssigneeRemoteAddress.Bytes(),
})
}

Expand Down Expand Up @@ -249,6 +259,10 @@ func (i InternalOutgoingTxBatch) GetCheckpoint(turnstoneID string) ([]byte, erro
{Type: whoops.Must(abi.NewType("bytes32", "", nil))},
// deadline
{Type: whoops.Must(abi.NewType("uint256", "", nil))},
// relayer eth address
{Type: whoops.Must(abi.NewType("address", "", nil))},
// gas estimate
{Type: whoops.Must(abi.NewType("uint256", "", nil))},
}

var turnstoneBytes32 [32]byte
Expand All @@ -262,26 +276,33 @@ func (i InternalOutgoingTxBatch) GetCheckpoint(turnstoneID string) ([]byte, erro

// Run through the elements of the batch and serialize them
txAmounts := make([]*big.Int, len(i.Transactions))
txDestinations := make([]gethcommon.Address, len(i.Transactions))
txDestinations := make([]common.Address, len(i.Transactions))
for j, tx := range i.Transactions {
txAmounts[j] = tx.Erc20Token.Amount.BigInt()
txDestinations[j] = tx.DestAddress.GetAddress()
}

args := struct {
Receiver []gethcommon.Address
Receiver []common.Address
Amount []*big.Int
}{
Receiver: txDestinations,
Amount: txAmounts,
}

estimate := big.NewInt(cConservativeDummyGasEstimate)
if i.GasEstimate != 0 {
estimate.SetUint64(i.GasEstimate)
}

abiEncodedBatch, err := arguments.Pack(
i.TokenContract.GetAddress(),
args,
big.NewInt(int64(i.BatchNonce)),
turnstoneBytes32,
big.NewInt(int64(i.BatchTimeout)),
i.AssigneeRemoteAddress,
estimate,
)
// this should never happen outside of test since any case that could crash on encoding
// should be filtered above.
Expand Down
Loading
Loading