Skip to content
Open
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
34 changes: 18 additions & 16 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ type Subnet struct {
// Address of the owner of the Validator Manager Contract
ValidatorManagerOwnerAddress *common.Address

// Private key of the owner of the Validator Manager Contract
ValidatorManagerOwnerPrivateKey string
// Signer of the owner of the Validator Manager Contract
ValidatorManagerOwnerSigner *evm.Signer

// BootstrapValidators are bootstrap validators that are included in the ConvertSubnetToL1Tx call
// that made Subnet a sovereign L1
Expand Down Expand Up @@ -364,7 +364,7 @@ func (c *Subnet) Commit(ms multisig.Multisig, wallet wallet.Wallet, waitForTxAcc
// to set as the owner of the PoA manager
func (c *Subnet) InitializeProofOfAuthority(
log logging.Logger,
privateKey string,
signer *evm.Signer,
aggregatorLogger logging.Logger,
useACP99 bool,
signatureAggregatorEndpoint string,
Expand Down Expand Up @@ -395,7 +395,7 @@ func (c *Subnet) InitializeProofOfAuthority(
if client, err := evm.GetClient(c.ValidatorManagerRPC); err != nil {
log.Error("failure connecting to Validator Manager RPC to setup proposer VM", zap.Error(err))
} else {
if err := client.SetupProposerVM(privateKey); err != nil {
if err := client.SetupProposerVM(signer); err != nil {
log.Error("failure setting proposer VM on Validator Manager's Blockchain", zap.Error(err))
}
client.Close()
Expand All @@ -405,7 +405,7 @@ func (c *Subnet) InitializeProofOfAuthority(
log,
c.ValidatorManagerRPC,
*c.ValidatorManagerAddress,
privateKey,
signer,
c.SubnetID,
*c.ValidatorManagerOwnerAddress,
useACP99,
Expand Down Expand Up @@ -446,7 +446,7 @@ func (c *Subnet) InitializeProofOfAuthority(
log,
c.ValidatorManagerRPC,
*c.ValidatorManagerAddress,
privateKey,
signer,
c.SubnetID,
c.ValidatorManagerBlockchainID,
c.BootstrapValidators,
Expand All @@ -461,12 +461,12 @@ func (c *Subnet) InitializeProofOfAuthority(

func (c *Subnet) InitializeProofOfStake(
log logging.Logger,
privateKey string,
signer *evm.Signer,
aggregatorLogger logging.Logger,
posParams validatormanager.PoSParams,
useACP99 bool,
signatureAggregatorEndpoint string,
nativeMinterPrecompileAdminPrivateKey string,
nativeMinterPrecompileAdminSigner *evm.Signer,
) error {
if c.Network == network.UndefinedNetwork {
return fmt.Errorf("unable to initialize Proof of Stake: %w", errMissingNetwork)
Expand All @@ -491,13 +491,15 @@ func (c *Subnet) InitializeProofOfStake(
if c.ValidatorManagerOwnerAddress == nil {
return fmt.Errorf("unable to initialize Proof of Stake: %w", errMissingValidatorManagerOwnerAddress)
}
if useACP99 && c.ValidatorManagerOwnerPrivateKey == "" {
return fmt.Errorf("unable to initialize Proof of Stake: %w", errMissingValidatorManagerOwnerPrivateKey)
if useACP99 {
if c.ValidatorManagerOwnerSigner == nil {
return fmt.Errorf("unable to initialize Proof of Stake: %w", errMissingValidatorManagerOwnerPrivateKey)
}
}
if client, err := evm.GetClient(c.ValidatorManagerRPC); err != nil {
log.Error("failure connecting to Validator Manager RPC to setup proposer VM", zap.Error(err))
} else {
if err := client.SetupProposerVM(privateKey); err != nil {
if err := client.SetupProposerVM(signer); err != nil {
log.Error("failure setting proposer VM on Validator Manager's Blockchain", zap.Error(err))
}
client.Close()
Expand All @@ -507,7 +509,7 @@ func (c *Subnet) InitializeProofOfStake(
log,
c.ValidatorManagerRPC,
*c.ValidatorManagerAddress,
privateKey,
signer,
c.SubnetID,
*c.ValidatorManagerOwnerAddress,
useACP99,
Expand All @@ -524,12 +526,12 @@ func (c *Subnet) InitializeProofOfStake(
c.ValidatorManagerRPC,
*c.ValidatorManagerAddress,
*c.SpecializedValidatorManagerAddress,
c.ValidatorManagerOwnerPrivateKey,
privateKey,
c.ValidatorManagerOwnerSigner,
signer,
c.SubnetID,
posParams,
useACP99,
nativeMinterPrecompileAdminPrivateKey,
nativeMinterPrecompileAdminSigner,
)
if err != nil {
if !errors.Is(err, validatormanager.ErrAlreadyInitialized) {
Expand Down Expand Up @@ -567,7 +569,7 @@ func (c *Subnet) InitializeProofOfStake(
log,
c.ValidatorManagerRPC,
*c.ValidatorManagerAddress,
privateKey,
signer,
c.SubnetID,
c.ValidatorManagerBlockchainID,
c.BootstrapValidators,
Expand Down
70 changes: 13 additions & 57 deletions evm/contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/common/hexutil"
"github.com/ava-labs/libevm/core/types"
"github.com/ava-labs/libevm/crypto"
"github.com/ava-labs/subnet-evm/accounts/abi/bind"

"github.com/ava-labs/avalanche-tooling-sdk-go/evm"
Expand Down Expand Up @@ -291,35 +290,20 @@ func ParseSpec(
return name, string(abiBytes), nil
}

func idempotentSigner(
_ common.Address,
tx *types.Transaction,
) (*types.Transaction, error) {
return tx, nil
}

// get method name and types from [methodsSpec], then call it
// at the smart contract [contractAddress] with the given [params].
// also send [payment] tokens to it
func TxToMethod(
logger logging.Logger,
rpcURL string,
generateRawTxOnly bool,
from common.Address,
privateKey string,
signer *evm.Signer,
contractAddress common.Address,
payment *big.Int,
description string,
errorSignatureToError map[string]error,
methodSpec string,
params ...interface{},
) (*types.Transaction, *types.Receipt, error) {
if privateKey == "" && from == (common.Address{}) {
return nil, nil, fmt.Errorf("from address and private key can't be both empty at TxToMethod")
}
if !generateRawTxOnly && privateKey == "" {
return nil, nil, fmt.Errorf("from private key must be defined to be able to sign the tx at TxToMethod")
}
methodName, methodABI, err := ParseSpec(methodSpec, nil, false, false, payment != nil, false, nil, params...)
if err != nil {
return nil, nil, err
Expand All @@ -337,26 +321,16 @@ func TxToMethod(
}
defer client.Close()
contract := bind.NewBoundContract(contractAddress, *abi, client.EthClient, client.EthClient, client.EthClient)
var txOpts *bind.TransactOpts
if generateRawTxOnly {
txOpts = &bind.TransactOpts{
From: from,
Signer: idempotentSigner,
NoSend: true,
}
} else {
txOpts, err = client.GetTxOptsWithSigner(privateKey)
if err != nil {
return nil, nil, err
}
txOpts, err := client.GetTxOptsWithSigner(signer)
if err != nil {
return nil, nil, err
}
txOpts.Value = payment
tx, err := contract.Transact(txOpts, methodName, params...)
if err != nil {
trace, traceCallErr := DebugTraceCall(
rpcURL,
from,
privateKey,
signer,
contractAddress,
payment,
methodSpec,
Expand All @@ -376,7 +350,7 @@ func TxToMethod(
}
return tx, nil, err
}
if generateRawTxOnly {
if txOpts.NoSend {
return tx, nil, nil
}
receipt, success, err := client.WaitForTransaction(tx)
Expand All @@ -403,9 +377,7 @@ func TxToMethod(
func TxToMethodWithWarpMessage(
logger logging.Logger,
rpcURL string,
generateRawTxOnly bool,
from common.Address,
privateKey string,
signer *evm.Signer,
contractAddress common.Address,
warpMessage *warp.Message,
payment *big.Int,
Expand All @@ -414,12 +386,6 @@ func TxToMethodWithWarpMessage(
methodSpec string,
params ...interface{},
) (*types.Transaction, *types.Receipt, error) {
if privateKey == "" && from == (common.Address{}) {
return nil, nil, fmt.Errorf("from address and private key can't be both empty at TxToMethodWithWarpMessage")
}
if !generateRawTxOnly && privateKey == "" {
return nil, nil, fmt.Errorf("from private key must be defined to be able to sign the tx at TxToMethodWithWarpMessage")
}
methodName, methodABI, err := ParseSpec(methodSpec, nil, false, false, false, false, nil, params...)
if err != nil {
return nil, nil, err
Expand All @@ -441,18 +407,16 @@ func TxToMethodWithWarpMessage(
}
defer client.Close()
tx, err := client.TransactWithWarpMessage(
from,
privateKey,
signer,
warpMessage,
contractAddress,
callData,
payment,
generateRawTxOnly,
)
if err != nil {
return nil, nil, err
}
if generateRawTxOnly {
if signer.IsNoOp() {
return tx, nil, nil
}
if err := client.SendTransaction(tx); err != nil {
Expand Down Expand Up @@ -518,8 +482,7 @@ func handleFailedReceiptStatus(

func DebugTraceCall(
rpcURL string,
from common.Address,
privateKey string,
signer *evm.Signer,
contractAddress common.Address,
payment *big.Int,
methodSpec string,
Expand All @@ -545,15 +508,8 @@ func DebugTraceCall(
return nil, err
}
defer client.Close()
if from == (common.Address{}) {
pk, err := crypto.HexToECDSA(privateKey)
if err != nil {
return nil, err
}
from = crypto.PubkeyToAddress(pk.PublicKey)
}
data := map[string]string{
"from": from.Hex(),
"from": signer.Address().Hex(),
"to": contractAddress.Hex(),
"input": "0x" + hex.EncodeToString(callData),
}
Expand Down Expand Up @@ -613,7 +569,7 @@ func GetSmartContractCallResult[T any](methodName string, out []interface{}) (T,

func DeployContract(
rpcURL string,
privateKey string,
signer *evm.Signer,
binBytes []byte,
methodSpec string,
params ...interface{},
Expand All @@ -639,7 +595,7 @@ func DeployContract(
return common.Address{}, nil, nil, err
}
defer client.Close()
txOpts, err := client.GetTxOptsWithSigner(privateKey)
txOpts, err := client.GetTxOptsWithSigner(signer)
if err != nil {
return common.Address{}, nil, nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions evm/contract/ownable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package contract
import (
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/libevm/common"

"github.com/ava-labs/avalanche-tooling-sdk-go/evm"
)

// GetContractOwner gets owner for https://docs.openzeppelin.com/contracts/2.x/api/ownership#Ownable-owner contracts
Expand All @@ -28,15 +30,13 @@ func TransferOwnership(
logger logging.Logger,
rpcURL string,
contractAddress common.Address,
ownerPrivateKey string,
signer *evm.Signer,
newOwner common.Address,
) error {
_, _, err := TxToMethod(
logger,
rpcURL,
false,
common.Address{},
ownerPrivateKey,
signer,
contractAddress,
nil,
"transfer ownership",
Expand Down
Loading
Loading