Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
Remove NewKeyedTransactor function (ledgerwatch#4472)
Browse files Browse the repository at this point in the history
* accounts/abi: remove NewKeyedTransactor function

* Remove "imported and not used" package

* Update state.go

* Update statedb_insert_chain_transaction_test.go

* Update statedb_chain_test.go

* Update database_test.go

* lint fixes

Co-authored-by: awskii <artem.tsskiy@gmail.com>
  • Loading branch information
Zachinquarantine and awskii committed Jul 13, 2022
1 parent 36586e5 commit 8c27879
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 54 deletions.
24 changes: 0 additions & 24 deletions accounts/abi/bind/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/log/v3"
)

// ErrNoChainID is returned whenever the user failed to specify a chain id.
Expand All @@ -33,29 +32,6 @@ var ErrNoChainID = errors.New("no chain id specified")
// ErrNotAuthorized is returned when an account is not properly unlocked.
var ErrNotAuthorized = errors.New("not authorized to sign this account")

// NewKeyedTransactor is a utility method to easily create a transaction signer
// from a single private key.
//
// Deprecated: Use NewKeyedTransactorWithChainID instead.
func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts {
log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithChainID")
keyAddr := crypto.PubkeyToAddress(key.PublicKey)
signer := types.LatestSignerForChainID(nil)
return &TransactOpts{
From: keyAddr,
Signer: func(address common.Address, tx types.Transaction) (types.Transaction, error) {
if address != keyAddr {
return nil, ErrNotAuthorized
}
signature, err := crypto.Sign(tx.SigningHash(nil).Bytes(), key)
if err != nil {
return nil, err
}
return tx.WithSignature(*signer, signature)
},
}
}

// NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer
// from a single private key.
func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) {
Expand Down
15 changes: 12 additions & 3 deletions cmd/pics/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,18 @@ func initialState1() error {

contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackend.Close()
transactOpts := bind.NewKeyedTransactor(key)
transactOpts1 := bind.NewKeyedTransactor(key1)
transactOpts2 := bind.NewKeyedTransactor(key2)
transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
if err != nil {
panic(err)
}
transactOpts1, err := bind.NewKeyedTransactorWithChainID(key1, m.ChainConfig.ChainID)
if err != nil {
panic(err)
}
transactOpts2, err := bind.NewKeyedTransactorWithChainID(key2, m.ChainConfig.ChainID)
if err != nil {
panic(err)
}

var tokenContract *contracts.Token
// We generate the blocks without plainstant because it's not supported in core.GenerateChain
Expand Down
51 changes: 27 additions & 24 deletions core/state/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ func TestCreate2Revive(t *testing.T) {

contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackend.Close()
transactOpts := bind.NewKeyedTransactor(key)
transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
require.NoError(t, err)
transactOpts.GasLimit = 1000000

var contractAddress common.Address
var revive *contracts.Revive
var err error
// Change this address whenever you make any changes in the code of the revive contract in
// contracts/revive.sol
var create2address = common.HexToAddress("e70fd65144383e1189bd710b1e23b61e26315ff4")
Expand Down Expand Up @@ -263,12 +263,13 @@ func TestCreate2Polymorth(t *testing.T) {

contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackend.Close()
transactOpts := bind.NewKeyedTransactor(key)
transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
require.NoError(t, err)
transactOpts.GasLimit = 1000000

var contractAddress common.Address
var poly *contracts.Poly
var err error

// Change this address whenever you make any changes in the code of the poly contract in
// contracts/poly.sol
var create2address = common.HexToAddress("c66aa74c220476f244b7f45897a124d1a01ca8a8")
Expand Down Expand Up @@ -509,12 +510,12 @@ func TestReorgOverSelfDestruct(t *testing.T) {

contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackend.Close()
transactOpts := bind.NewKeyedTransactor(key)
transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
require.NoError(t, err)
transactOpts.GasLimit = 1000000

var contractAddress common.Address
var selfDestruct *contracts.Selfdestruct
var err error

// Here we generate 3 blocks, two of which (the one with "Change" invocation and "Destruct" invocation will be reverted during the reorg)
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 3, func(i int, block *core.BlockGen) {
Expand Down Expand Up @@ -549,7 +550,8 @@ func TestReorgOverSelfDestruct(t *testing.T) {
// Create a longer chain, with 4 blocks (with higher total difficulty) that reverts the change of stroage self-destruction of the contract
contractBackendLonger := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackendLonger.Close()
transactOptsLonger := bind.NewKeyedTransactor(key)
transactOptsLonger, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
require.NoError(t, err)
transactOptsLonger.GasLimit = 1000000

longerChain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 4, func(i int, block *core.BlockGen) {
Expand Down Expand Up @@ -658,12 +660,12 @@ func TestReorgOverStateChange(t *testing.T) {

contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackend.Close()
transactOpts := bind.NewKeyedTransactor(key)
transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
require.NoError(t, err)
transactOpts.GasLimit = 1000000

var contractAddress common.Address
var selfDestruct *contracts.Selfdestruct
var err error

// Here we generate 3 blocks, two of which (the one with "Change" invocation and "Destruct" invocation will be reverted during the reorg)
chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 2, func(i int, block *core.BlockGen) {
Expand Down Expand Up @@ -692,7 +694,8 @@ func TestReorgOverStateChange(t *testing.T) {
// Create a longer chain, with 4 blocks (with higher total difficulty) that reverts the change of stroage self-destruction of the contract
contractBackendLonger := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackendLonger.Close()
transactOptsLonger := bind.NewKeyedTransactor(key)
transactOptsLonger, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
require.NoError(t, err)
transactOptsLonger.GasLimit = 1000000
longerChain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 3, func(i int, block *core.BlockGen) {
var tx types.Transaction
Expand Down Expand Up @@ -813,10 +816,8 @@ func TestCreateOnExistingStorage(t *testing.T) {
contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackend.Close()

transactOpts, err := bind.NewKeyedTransactorWithChainID(key, gspec.Config.ChainID)
if err != nil {
t.Fatal(err)
}
transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
require.NoError(t, err)
transactOpts.GasLimit = 1000000

var contractAddress common.Address
Expand Down Expand Up @@ -943,12 +944,12 @@ func TestEip2200Gas(t *testing.T) {

contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackend.Close()
transactOpts := bind.NewKeyedTransactor(key)
transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
require.NoError(t, err)
transactOpts.GasLimit = 1000000

var contractAddress common.Address
var selfDestruct *contracts.Selfdestruct
var err error

// Here we generate 1 block with 2 transactions, first creates a contract with some initial values in the
// It activates the SSTORE pricing rules specific to EIP-2200 (istanbul)
Expand Down Expand Up @@ -1035,12 +1036,12 @@ func TestWrongIncarnation(t *testing.T) {

contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackend.Close()
transactOpts := bind.NewKeyedTransactor(key)
transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
require.NoError(t, err)
transactOpts.GasLimit = 1000000

var contractAddress common.Address
var changer *contracts.Changer
var err error

chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 2, func(i int, block *core.BlockGen) {
var tx types.Transaction
Expand Down Expand Up @@ -1151,9 +1152,9 @@ func TestWrongIncarnation2(t *testing.T) {

contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackend.Close()
transactOpts := bind.NewKeyedTransactor(key)
transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
require.NoError(t, err)
transactOpts.GasLimit = 1000000
var err error

var contractAddress common.Address

Expand Down Expand Up @@ -1190,7 +1191,8 @@ func TestWrongIncarnation2(t *testing.T) {

// Create a longer chain, with 4 blocks (with higher total difficulty) that reverts the change of stroage self-destruction of the contract
contractBackendLonger := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
transactOptsLonger := bind.NewKeyedTransactor(key)
transactOptsLonger, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
require.NoError(t, err)
transactOptsLonger.GasLimit = 1000000
longerChain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 3, func(i int, block *core.BlockGen) {
var tx types.Transaction
Expand Down Expand Up @@ -1402,13 +1404,13 @@ func TestRecreateAndRewind(t *testing.T) {
m := stages.MockWithGenesis(t, gspec, key)
contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackend.Close()
transactOpts := bind.NewKeyedTransactor(key)
transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
require.NoError(t, err)
transactOpts.GasLimit = 1000000
var revive *contracts.Revive2
var phoenix *contracts.Phoenix
var reviveAddress common.Address
var phoenixAddress common.Address
var err error

chain, err1 := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 4, func(i int, block *core.BlockGen) {
var tx types.Transaction
Expand Down Expand Up @@ -1470,7 +1472,8 @@ func TestRecreateAndRewind(t *testing.T) {

contractBackendLonger := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackendLonger.Close()
transactOptsLonger := bind.NewKeyedTransactor(key)
transactOptsLonger, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
require.NoError(t, err)
transactOptsLonger.GasLimit = 1000000
longerChain, err1 := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 5, func(i int, block *core.BlockGen) {
var tx types.Transaction
Expand Down
5 changes: 3 additions & 2 deletions tests/statedb_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon/ethdb/olddb"
"github.com/stretchr/testify/require"

"github.com/ledgerwatch/erigon/accounts/abi/bind"
"github.com/ledgerwatch/erigon/accounts/abi/bind/backends"
Expand Down Expand Up @@ -64,11 +65,11 @@ func TestSelfDestructReceive(t *testing.T) {

contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit)
defer contractBackend.Close()
transactOpts := bind.NewKeyedTransactor(key)
transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID)
require.NoError(t, err)

var contractAddress common.Address
var selfDestructorContract *contracts.SelfDestructor
var err error

// There are two blocks
// First block deploys a contract, then makes it self-destruct, and then sends 1 wei to the address of the contract,
Expand Down
6 changes: 5 additions & 1 deletion tests/statedb_insert_chain_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,11 @@ func getGenesis(funds ...*big.Int) initialData {
for _, key := range keys {
addr := crypto.PubkeyToAddress(key.PublicKey)
addresses = append(addresses, addr)
transactOpts = append(transactOpts, bind.NewKeyedTransactor(key))
to, err := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1))
if err != nil {
panic(err)
}
transactOpts = append(transactOpts, to)

allocs[addr] = core.GenesisAccount{Balance: accountFunds}
}
Expand Down

0 comments on commit 8c27879

Please sign in to comment.