Skip to content
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
5 changes: 0 additions & 5 deletions backend/ethereum/channel/contractbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,6 @@ func (c *ContractBackend) NewTransactor(ctx context.Context, gasLimit uint64,
return nil, errors.WithMessage(err, "creating transactor")
}

auth.GasPrice, err = c.SuggestGasPrice(ctx)
if err != nil {
return nil, errors.Wrapf(err, "estimating gas price")
}

auth.GasLimit = gasLimit
auth.Context = ctx

Expand Down
16 changes: 8 additions & 8 deletions backend/ethereum/channel/contractbackend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -126,15 +127,14 @@ func Test_ConfirmTransaction(t *testing.T) {
defer cancel()

// Create the Transaction.
rawTx := types.NewTx(&types.LegacyTx{
Nonce: 0,
To: &common.Address{},
Value: big.NewInt(1),
Gas: test.GasLimit,
GasPrice: big.NewInt(test.GasPrice),
Data: nil,
rawTx := types.NewTx(&types.DynamicFeeTx{
Nonce: 0,
GasFeeCap: big.NewInt(test.InitialGasBaseFee),
Gas: params.TxGas,
To: &common.Address{},
Value: big.NewInt(1),
})
opts, err := s.CB.NewTransactor(ctx, 1, s.TxSender.Account)
opts, err := s.CB.NewTransactor(ctx, params.TxGas, s.TxSender.Account)
require.NoError(t, err)
signed, err := opts.Signer(s.TxSender.Account.Address, rawTx)
require.NoError(t, err)
Expand Down
5 changes: 2 additions & 3 deletions backend/ethereum/channel/funder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -76,7 +75,7 @@ func newFunderSetup(rng *rand.Rand) (
ksWallet := wallettest.RandomWallet().(*keystore.Wallet)
cb := ethchannel.NewContractBackend(
simBackend,
keystore.NewTransactor(*ksWallet, types.NewEIP155Signer(big.NewInt(1337))),
keystore.NewTransactor(*ksWallet, test.SimSigner),
TxFinalityDepth,
)
funder := ethchannel.NewFunder(cb)
Expand Down Expand Up @@ -396,7 +395,7 @@ func newNFunders(
simBackend.FundAddress(ctx, tokenAcc.Address)
cb := ethchannel.NewContractBackend(
simBackend,
keystore.NewTransactor(*ksWallet, types.NewEIP155Signer(big.NewInt(1337))),
keystore.NewTransactor(*ksWallet, test.SimSigner),
TxFinalityDepth,
)

Expand Down
8 changes: 2 additions & 6 deletions backend/ethereum/channel/test/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/require"

ethchannel "perun.network/go-perun/backend/ethereum/channel"
Expand Down Expand Up @@ -73,10 +71,9 @@ func NewSimSetup(t *testing.T, rng *rand.Rand, txFinalityDepth uint64, blockInte
t.Cleanup(simBackend.StopMining)
}

signer := types.NewEIP155Signer(params.AllEthashProtocolChanges.ChainID)
contractBackend := ethchannel.NewContractBackend(
simBackend,
keystore.NewTransactor(*ksWallet, signer),
keystore.NewTransactor(*ksWallet, SimSigner),
txFinalityDepth,
)

Expand Down Expand Up @@ -119,10 +116,9 @@ func NewSetup(t *testing.T, rng *rand.Rand, n int, blockInterval time.Duration,
s.Parts[i] = s.Accs[i].Address()
s.SimBackend.FundAddress(ctx, s.Accs[i].Account.Address)
s.Recvs[i] = ksWallet.NewRandomAccount(rng).Address().(*ethwallet.Address)
signer := types.NewEIP155Signer(params.AllEthashProtocolChanges.ChainID)
cb := ethchannel.NewContractBackend(
s.SimBackend,
keystore.NewTransactor(*ksWallet, signer),
keystore.NewTransactor(*ksWallet, SimSigner),
txFinalityDepth,
)
s.Funders[i] = ethchannel.NewFunder(cb)
Expand Down
39 changes: 22 additions & 17 deletions backend/ethereum/channel/test/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,21 @@ import (
)

const (
// GasLimit is the max amount of gas we want to send per transaction.
GasLimit = 500000
// GasPrice is the gas price that is used for simulated transactions.
// This value is set to `maxFeePerGas` from go-ethereum to prevent
// "max fee per gas less than block base fee" errors.
GasPrice = 875000000
// InitialGasBaseFee is the simulated backend's initial base fee.
// It should only decrease from the first block onwards, as no gas auctions
// are taking place.
//
// When constructing a transaction manually, GasFeeCap can be set to this
// value to avoid the error 'max fee per gas less than block base fee'.
InitialGasBaseFee = 875_000_000
// internal gas limit of the simulated backend.
simBackendGasLimit = 8000000
simBackendGasLimit = 8_000_000
)

// SimSigner is the latest types.Signer that can be used with the simulated
// backend.
var SimSigner = types.LatestSigner(params.AllEthashProtocolChanges)

// SimulatedBackend provides a simulated ethereum blockchain for tests.
type SimulatedBackend struct {
backends.SimulatedBackend
Expand Down Expand Up @@ -101,11 +106,6 @@ func NewSimulatedBackend(opts ...SimBackendOpt) *SimulatedBackend {
return sb
}

// SuggestGasPrice always returns `GasPrice`.
func (*SimulatedBackend) SuggestGasPrice(context.Context) (*big.Int, error) {
return big.NewInt(GasPrice), nil
}

// SendTransaction executes a transaction.
func (s *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error {
if err := s.SimulatedBackend.SendTransaction(ctx, tx); err != nil {
Expand All @@ -123,17 +123,22 @@ func (s *SimulatedBackend) FundAddress(ctx context.Context, addr common.Address)
if err != nil {
panic(err)
}
tx := types.NewTransaction(nonce, addr, test.MaxBalance, GasLimit, big.NewInt(GasPrice), nil)
signer := types.NewEIP155Signer(params.AllEthashProtocolChanges.ChainID)
signedTX, err := types.SignTx(tx, signer, s.faucetKey)
txdata := &types.DynamicFeeTx{
Comment thread
matthiasgeihs marked this conversation as resolved.
Nonce: nonce,
GasFeeCap: big.NewInt(InitialGasBaseFee),
Gas: params.TxGas,
To: &addr,
Value: test.MaxBalance,
}
tx, err := types.SignNewTx(s.faucetKey, SimSigner, txdata)
if err != nil {
panic(err)
}
if err := s.SimulatedBackend.SendTransaction(ctx, signedTX); err != nil {
if err := s.SimulatedBackend.SendTransaction(ctx, tx); err != nil {
panic(err)
}
s.Commit()
if _, err := bind.WaitMined(ctx, s, signedTX); err != nil {
if _, err := bind.WaitMined(ctx, s, tx); err != nil {
panic(err)
}
}
Expand Down
4 changes: 1 addition & 3 deletions backend/ethereum/channel/test/tokensetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/require"

"perun.network/go-perun/backend/ethereum/bindings"
Expand Down Expand Up @@ -69,10 +68,9 @@ func NewTokenSetup(ctx context.Context, t *testing.T, rng *rand.Rand, txFinality
sb.FundAddress(ctx, acc1.Address)
acc2 := &ksWallet.NewRandomAccount(rng).(*keystore.Account).Account
sb.FundAddress(ctx, acc2.Address)
signer := types.NewEIP155Signer(params.AllEthashProtocolChanges.ChainID)
cb := ethchannel.NewContractBackend(
sb,
keystore.NewTransactor(*ksWallet, signer),
keystore.NewTransactor(*ksWallet, SimSigner),
txFinalityDepth,
)

Expand Down
89 changes: 63 additions & 26 deletions backend/ethereum/channel/test/transactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"testing"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
Expand All @@ -30,10 +29,24 @@ import (
"perun.network/go-perun/backend/ethereum/wallet"
)

// TxType is a transaction type, specifying how it is hashed for signing and how
// the v value of the signature is coded.
type TxType int

const (
// LegacyTx - legacy transaction with v = {0,1} + 27.
LegacyTx TxType = iota
// EIP155Tx - EIP155 transaction with v = {0,1} + CHAIN_ID * 2 + 35.
EIP155Tx
// EIP1559Tx - EIP1559 transaction with v = {0,1}.
EIP1559Tx
)

// TransactorSetup holds the setup for running generic tests on a transactor implementation.
type TransactorSetup struct {
Signer types.Signer
ChainID int64
TxType TxType // Transaction type to generate and check against this signer
Tr channel.Transactor
ValidAcc accounts.Account // wallet should contain key corresponding to this account.
MissingAcc accounts.Account // wallet should not contain key corresponding to this account.
Expand All @@ -45,22 +58,37 @@ const signerTestDataMaxLength = 100
// for the passed signer.
func GenericSignerTest(t *testing.T, rng *rand.Rand, setup TransactorSetup) {
t.Helper()
signer := setup.Signer
chainID := setup.ChainID
data := make([]byte, rng.Int31n(signerTestDataMaxLength)+1)
rng.Read(data)

newTx := func() *types.Transaction {
data := make([]byte, rng.Int31n(signerTestDataMaxLength)+1)
rng.Read(data)
switch setup.TxType {
case LegacyTx, EIP155Tx:
return types.NewTx(&types.LegacyTx{
Value: big.NewInt(rng.Int63()),
Data: data,
})
case EIP1559Tx:
return types.NewTx(&types.DynamicFeeTx{
ChainID: big.NewInt(setup.ChainID),
Value: big.NewInt(rng.Int63()),
Data: data,
})
}
panic("unsupported tx type")
}

t.Run("happy", func(t *testing.T) {
transactOpts, err := setup.Tr.NewTransactor(setup.ValidAcc)
require.NoError(t, err)
rawTx := types.NewTransaction(uint64(1), common.Address{}, big.NewInt(1), uint64(1), big.NewInt(1), data)
signedTx, err := transactOpts.Signer(setup.ValidAcc.Address, rawTx)
tx := newTx()
signedTx, err := transactOpts.Signer(setup.ValidAcc.Address, tx)
assert.NoError(t, err)
require.NotNil(t, signedTx)

txHash := signer.Hash(rawTx).Bytes()
txHash := setup.Signer.Hash(tx).Bytes()
v, r, s := signedTx.RawSignatureValues()
sig := sigFromRSV(t, r, s, v, chainID)
sig := sigFromRSV(t, r, s, v, &setup)
pk, err := crypto.SigToPub(txHash, sig)
require.NoError(t, err)
addr := crypto.PubkeyToAddress(*pk)
Expand All @@ -76,30 +104,39 @@ func GenericSignerTest(t *testing.T, rng *rand.Rand, setup TransactorSetup) {
transactOpts, err := setup.Tr.NewTransactor(setup.ValidAcc)
require.NoError(t, err)

rawTx := types.NewTransaction(uint64(1), common.Address{}, big.NewInt(1), uint64(1), big.NewInt(1), data)
_, err = transactOpts.Signer(setup.MissingAcc.Address, rawTx)
_, err = transactOpts.Signer(setup.MissingAcc.Address, newTx())
assert.Error(t, err)
})
}

func sigFromRSV(t *testing.T, r, s, _v *big.Int, chainID int64) []byte {
func sigFromRSV(t *testing.T, r, s, _v *big.Int, setup *TransactorSetup) []byte {
t.Helper()
const (
elemLen = 32
sigLen = elemLen*2 + 1
sigVAdd = 35
sigVSubtract = 27
elemLen = 32
sigVEIP155Shift = 35
sigVLegacyShift = 27
)
var (
sig = make([]byte, wallet.SigLen)
rb = r.Bytes()
sb = s.Bytes()
v = byte(_v.Uint64()) // truncation anticipated for large ChainIDs
)
sig := make([]byte, wallet.SigLen)
copy(sig[elemLen-len(r.Bytes()):elemLen], r.Bytes())
copy(sig[elemLen*2-len(s.Bytes()):elemLen*2], s.Bytes())
v := byte(_v.Uint64()) // Needed for chain ids > 110.

if chainID == 0 {
sig[sigLen-1] = v - sigVSubtract
} else {
sig[sigLen-1] = v - byte(chainID*2+sigVAdd) // Underflow is ok here.
copy(sig[elemLen-len(rb):elemLen], rb)
copy(sig[elemLen*2-len(sb):elemLen*2], sb)

switch setup.TxType {
case LegacyTx:
v -= sigVLegacyShift
case EIP155Tx:
v -= byte(setup.ChainID*2 + sigVEIP155Shift) // underflow anticipated
case EIP1559Tx:
// EIP1559 transactions simply code the y-parity into v, so no correction
// necessary.
}
require.Contains(t, []byte{0, 1}, sig[sigLen-1], "Invalid v")
require.Containsf(t, []byte{0, 1}, v,
"Invalid v (txType: %v; chainID: %d)", setup.TxType, setup.ChainID)

sig[wallet.SigLen-1] = v
return sig
}
5 changes: 2 additions & 3 deletions backend/ethereum/subscription/eventsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"

"perun.network/go-perun/backend/ethereum/bindings"
Expand Down Expand Up @@ -62,7 +61,7 @@ func TestEventSub(t *testing.T) {
sb.FundAddress(ctx, account.Address)
cb := ethchannel.NewContractBackend(
sb,
keystore.NewTransactor(*ksWallet, types.NewEIP155Signer(big.NewInt(1337))),
keystore.NewTransactor(*ksWallet, test.SimSigner),
txFinalityDepth,
)

Expand Down Expand Up @@ -150,7 +149,7 @@ func TestEventSub_Filter(t *testing.T) {
sb.FundAddress(ctx, account.Address)
cb := ethchannel.NewContractBackend(
sb,
keystore.NewTransactor(*ksWallet, types.NewEIP155Signer(big.NewInt(1337))),
keystore.NewTransactor(*ksWallet, test.SimSigner),
txFinalityDepth,
)

Expand Down
Loading