Skip to content

Commit

Permalink
Decide fee payer (on wrapped transaction).
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Dec 11, 2024
1 parent eadbce8 commit 2759489
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions txcache/wrappedTransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type WrappedTransaction struct {
Fee *big.Int
PricePerUnit uint64
TransferredValue *big.Int
FeePayer []byte
}

// precomputeFields computes (and caches) the (average) price per gas unit.
Expand All @@ -36,6 +37,16 @@ func (wrappedTx *WrappedTransaction) precomputeFields(host MempoolHost) {
}

wrappedTx.TransferredValue = host.GetTransferredValue(wrappedTx.Tx)
wrappedTx.FeePayer = wrappedTx.decideFeePayer()
}

func (wrappedTx *WrappedTransaction) decideFeePayer() []byte {
asRelayed, ok := wrappedTx.Tx.(data.RelayedTransactionHandler)
if ok && len(asRelayed.GetRelayerAddr()) > 0 {
return asRelayed.GetRelayerAddr()
}

return wrappedTx.Tx.GetSndAddr()
}

// Equality is out of scope (not possible in our case).
Expand Down
5 changes: 5 additions & 0 deletions txcache/wrappedTransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestWrappedTransaction_precomputeFields(t *testing.T) {
require.Equal(t, "51500000000000", tx.Fee.String())
require.Equal(t, oneBillion, int(tx.PricePerUnit))
require.Equal(t, "1000000000000000000", tx.TransferredValue.String())
require.Equal(t, []byte("a"), tx.FeePayer)
})

t.Run("move balance gas limit and execution gas limit (a)", func(t *testing.T) {
Expand All @@ -29,6 +30,7 @@ func TestWrappedTransaction_precomputeFields(t *testing.T) {

require.Equal(t, "51500010000000", tx.Fee.String())
require.Equal(t, 999_980_777, int(tx.PricePerUnit))
require.Equal(t, []byte("b"), tx.FeePayer)
})

t.Run("move balance gas limit and execution gas limit (b)", func(t *testing.T) {
Expand All @@ -41,6 +43,7 @@ func TestWrappedTransaction_precomputeFields(t *testing.T) {
require.Equal(t, "60985000000000", tx.Fee.String())
require.Equal(t, 60_985_000_000_000, actualFee)
require.Equal(t, actualFee/oneMilion, int(tx.PricePerUnit))
require.Equal(t, []byte("c"), tx.FeePayer)
})

t.Run("with guardian", func(t *testing.T) {
Expand All @@ -52,6 +55,7 @@ func TestWrappedTransaction_precomputeFields(t *testing.T) {
require.Equal(t, "50000000000000", tx.Fee.String())
require.Equal(t, oneBillion, int(tx.PricePerUnit))
require.Equal(t, "1000000000000000000", tx.TransferredValue.String())
require.Equal(t, []byte("a"), tx.FeePayer)
})

t.Run("with nil transferred value", func(t *testing.T) {
Expand All @@ -61,6 +65,7 @@ func TestWrappedTransaction_precomputeFields(t *testing.T) {
tx.precomputeFields(host)

require.Nil(t, tx.TransferredValue)
require.Equal(t, []byte("a"), tx.FeePayer)
})

t.Run("queries host", func(t *testing.T) {
Expand Down

0 comments on commit 2759489

Please sign in to comment.