Skip to content

Commit

Permalink
accounts/abi/bind: better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusVanDerWijden committed Dec 4, 2020
1 parent fc7fbb4 commit e61c432
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 32 deletions.
8 changes: 4 additions & 4 deletions accounts/abi/bind/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.I
if err != nil {
return nil, err
}
return NewKeyedTransactorWithChainID(key.PrivateKey, chainID), nil
return NewKeyedTransactorWithChainID(key.PrivateKey, chainID)
}

// NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from
Expand All @@ -138,10 +138,10 @@ func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accou

// 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 {
func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) {
keyAddr := crypto.PubkeyToAddress(key.PublicKey)
if chainID == nil {
return nol, NoChainID
return nil, NoChainID
}
signer := types.NewEIP155Signer(chainID)
return &TransactOpts{
Expand All @@ -156,7 +156,7 @@ func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) *Tra
}
return tx.WithSignature(signer, signature)
},
}
}, nil
}

// NewClefTransactor is a utility method to easily create a transaction signer
Expand Down
12 changes: 6 additions & 6 deletions accounts/abi/bind/backends/simulated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
func TestSimulatedBackend(t *testing.T) {
var gasLimit uint64 = 8000029
key, _ := crypto.GenerateKey() // nolint: gosec
auth := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
genAlloc := make(core.GenesisAlloc)
genAlloc[auth.From] = core.GenesisAccount{Balance: big.NewInt(9223372036854775807)}

Expand Down Expand Up @@ -411,7 +411,7 @@ func TestSimulatedBackend_EstimateGas(t *testing.T) {

key, _ := crypto.GenerateKey()
addr := crypto.PubkeyToAddress(key.PublicKey)
opts := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
opts, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))

sim := NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(params.Ether)}}, 10000000)
defer sim.Close()
Expand Down Expand Up @@ -888,7 +888,7 @@ func TestSimulatedBackend_PendingCodeAt(t *testing.T) {
if err != nil {
t.Errorf("could not get code at test addr: %v", err)
}
auth := bind.NewKeyedTransactorWithChainID(testKey, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(testKey, big.NewInt(1337))
contractAddr, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex(abiBin), sim)
if err != nil {
t.Errorf("could not deploy contract: %v tx: %v contract: %v", err, tx, contract)
Expand Down Expand Up @@ -924,7 +924,7 @@ func TestSimulatedBackend_CodeAt(t *testing.T) {
if err != nil {
t.Errorf("could not get code at test addr: %v", err)
}
auth := bind.NewKeyedTransactorWithChainID(testKey, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(testKey, big.NewInt(1337))
contractAddr, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex(abiBin), sim)
if err != nil {
t.Errorf("could not deploy contract: %v tx: %v contract: %v", err, tx, contract)
Expand Down Expand Up @@ -956,7 +956,7 @@ func TestSimulatedBackend_PendingAndCallContract(t *testing.T) {
if err != nil {
t.Errorf("could not get code at test addr: %v", err)
}
contractAuth := bind.NewKeyedTransactorWithChainID(testKey, big.NewInt(1337))
contractAuth, _ := bind.NewKeyedTransactorWithChainID(testKey, big.NewInt(1337))
addr, _, _, err := bind.DeployContract(contractAuth, parsed, common.FromHex(abiBin), sim)
if err != nil {
t.Errorf("could not deploy contract: %v", err)
Expand Down Expand Up @@ -1043,7 +1043,7 @@ func TestSimulatedBackend_CallContractRevert(t *testing.T) {
if err != nil {
t.Errorf("could not get code at test addr: %v", err)
}
contractAuth := bind.NewKeyedTransactorWithChainID(testKey, big.NewInt(1337))
contractAuth, _ := bind.NewKeyedTransactorWithChainID(testKey, big.NewInt(1337))
addr, _, _, err := bind.DeployContract(contractAuth, parsed, common.FromHex(reverterBin), sim)
if err != nil {
t.Errorf("could not deploy contract: %v", err)
Expand Down
34 changes: 17 additions & 17 deletions accounts/abi/bind/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ var bindTests = []struct {
`
// Generate a new random account and a funded simulator
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
defer sim.Close()
Expand Down Expand Up @@ -351,7 +351,7 @@ var bindTests = []struct {
`
// Generate a new random account and a funded simulator
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
defer sim.Close()
Expand Down Expand Up @@ -397,7 +397,7 @@ var bindTests = []struct {
`
// Generate a new random account and a funded simulator
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
defer sim.Close()
Expand Down Expand Up @@ -455,7 +455,7 @@ var bindTests = []struct {
`
// Generate a new random account and a funded simulator
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
defer sim.Close()
Expand Down Expand Up @@ -503,7 +503,7 @@ var bindTests = []struct {
`
// Generate a new random account and a funded simulator
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
defer sim.Close()
Expand Down Expand Up @@ -598,7 +598,7 @@ var bindTests = []struct {
`
// Generate a new random account and a funded simulator
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
defer sim.Close()
Expand Down Expand Up @@ -648,7 +648,7 @@ var bindTests = []struct {
`
// Generate a new random account and a funded simulator
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
defer sim.Close()
Expand Down Expand Up @@ -723,7 +723,7 @@ var bindTests = []struct {
`
// Generate a new random account and a funded simulator
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
defer sim.Close()
Expand Down Expand Up @@ -817,7 +817,7 @@ var bindTests = []struct {
`
// Generate a new random account and a funded simulator
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
defer sim.Close()
Expand Down Expand Up @@ -1007,7 +1007,7 @@ var bindTests = []struct {
`
// Generate a new random account and a funded simulator
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
defer sim.Close()
Expand Down Expand Up @@ -1142,7 +1142,7 @@ var bindTests = []struct {

`
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
defer sim.Close()
Expand Down Expand Up @@ -1284,7 +1284,7 @@ var bindTests = []struct {
`
// Generate a new random account and a funded simulator
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
defer sim.Close()
Expand Down Expand Up @@ -1350,7 +1350,7 @@ var bindTests = []struct {
`
// Initialize test accounts
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
defer sim.Close()
Expand Down Expand Up @@ -1444,7 +1444,7 @@ var bindTests = []struct {
sim := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}, 10000000)
defer sim.Close()
transactOpts := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
transactOpts, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
_, _, _, err := DeployIdentifierCollision(transactOpts, sim)
if err != nil {
t.Fatalf("failed to deploy contract: %v", err)
Expand Down Expand Up @@ -1506,7 +1506,7 @@ var bindTests = []struct {
sim := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}, 10000000)
defer sim.Close()
transactOpts := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
transactOpts, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
_, _, c1, err := DeployContractOne(transactOpts, sim)
if err != nil {
t.Fatal("Failed to deploy contract")
Expand Down Expand Up @@ -1563,7 +1563,7 @@ var bindTests = []struct {
`
// Generate a new random account and a funded simulator
key, _ := crypto.GenerateKey()
auth := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
auth, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
sim := backends.NewSimulatedBackend(core.GenesisAlloc{auth.From: {Balance: big.NewInt(10000000000)}}, 10000000)
defer sim.Close()
Expand Down Expand Up @@ -1632,7 +1632,7 @@ var bindTests = []struct {
sim := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(1000000000)}}, 1000000)
defer sim.Close()
opts := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
opts, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))
_, _, c, err := DeployNewFallbacks(opts, sim)
if err != nil {
t.Fatalf("Failed to deploy contract: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion contracts/checkpointoracle/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestCheckpointRegister(t *testing.T) {
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{accounts[0].addr: {Balance: big.NewInt(1000000000)}, accounts[1].addr: {Balance: big.NewInt(1000000000)}, accounts[2].addr: {Balance: big.NewInt(1000000000)}}, 10000000)
defer contractBackend.Close()

transactOpts := bind.NewKeyedTransactorWithChainID(accounts[0].key, big.NewInt(1337))
transactOpts, _ := bind.NewKeyedTransactorWithChainID(accounts[0].key, big.NewInt(1337))

// 3 trusted signers, threshold 2
contractAddr, _, c, err := contract.DeployCheckpointOracle(transactOpts, contractBackend, []common.Address{accounts[0].addr, accounts[1].addr, accounts[2].addr}, sectionSize, processConfirms, big.NewInt(2))
Expand Down
6 changes: 4 additions & 2 deletions les/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ func testCheckpointSyncing(t *testing.T, protocol int, syncMode int) {
data := append([]byte{0x19, 0x00}, append(registrarAddr.Bytes(), append([]byte{0, 0, 0, 0, 0, 0, 0, 0}, cp.Hash().Bytes()...)...)...)
sig, _ := crypto.Sign(crypto.Keccak256(data), signerKey)
sig[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
if _, err := server.handler.server.oracle.Contract().RegisterCheckpoint(bind.NewKeyedTransactorWithChainID(signerKey, big.NewInt(1337)), cp.SectionIndex, cp.Hash().Bytes(), new(big.Int).Sub(header.Number, big.NewInt(1)), header.ParentHash, [][]byte{sig}); err != nil {
auth, _ := bind.NewKeyedTransactorWithChainID(signerKey, big.NewInt(1337))
if _, err := server.handler.server.oracle.Contract().RegisterCheckpoint(auth, cp.SectionIndex, cp.Hash().Bytes(), new(big.Int).Sub(header.Number, big.NewInt(1)), header.ParentHash, [][]byte{sig}); err != nil {
t.Error("register checkpoint failed", err)
}
server.backend.Commit()
Expand Down Expand Up @@ -162,7 +163,8 @@ func testMissOracleBackend(t *testing.T, hasCheckpoint bool) {
data := append([]byte{0x19, 0x00}, append(registrarAddr.Bytes(), append([]byte{0, 0, 0, 0, 0, 0, 0, 0}, cp.Hash().Bytes()...)...)...)
sig, _ := crypto.Sign(crypto.Keccak256(data), signerKey)
sig[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
if _, err := server.handler.server.oracle.Contract().RegisterCheckpoint(bind.NewKeyedTransactorWithChainID(signerKey, big.NewInt(1337)), cp.SectionIndex, cp.Hash().Bytes(), new(big.Int).Sub(header.Number, big.NewInt(1)), header.ParentHash, [][]byte{sig}); err != nil {
auth, _ := bind.NewKeyedTransactorWithChainID(signerKey, big.NewInt(1337))
if _, err := server.handler.server.oracle.Contract().RegisterCheckpoint(auth, cp.SectionIndex, cp.Hash().Bytes(), new(big.Int).Sub(header.Number, big.NewInt(1)), header.ParentHash, [][]byte{sig}); err != nil {
t.Error("register checkpoint failed", err)
}
server.backend.Commit()
Expand Down
3 changes: 2 additions & 1 deletion les/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ func prepare(n int, backend *backends.SimulatedBackend) {
switch i {
case 0:
// deploy checkpoint contract
registrarAddr, _, _, _ = contract.DeployCheckpointOracle(bind.NewKeyedTransactorWithChainID(bankKey, big.NewInt(1337)), backend, []common.Address{signerAddr}, sectionSize, processConfirms, big.NewInt(1))
auth, _ := bind.NewKeyedTransactorWithChainID(bankKey, big.NewInt(1337))
registrarAddr, _, _, _ = contract.DeployCheckpointOracle(auth, backend, []common.Address{signerAddr}, sectionSize, processConfirms, big.NewInt(1))
// bankUser transfers some ether to user1
nonce, _ := backend.PendingNonceAt(ctx, bankAddr)
tx, _ := types.SignTx(types.NewTransaction(nonce, userAddr1, big.NewInt(10000), params.TxGas, nil, nil), signer, bankKey)
Expand Down
6 changes: 5 additions & 1 deletion mobile/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ func NewKeyedTransactOpts(keyJson []byte, passphrase string, chainID *big.Int) (
if err != nil {
return nil, err
}
return &TransactOpts{*bind.NewKeyedTransactorWithChainID(key.PrivateKey, chainID)}, nil
auth, err := bind.NewKeyedTransactorWithChainID(key.PrivateKey, chainID)
if err != nil {
return nil, err
}
return &TransactOpts{*auth}, nil
}

func (opts *TransactOpts) GetFrom() *Address { return &Address{opts.opts.From} }
Expand Down

0 comments on commit e61c432

Please sign in to comment.