Skip to content

Commit

Permalink
coreth atomic pkg dependency (#3588)
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyonur authored Jan 3, 2025
1 parent 8172d8b commit a8cc764
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 89 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/DataDog/zstd v1.5.2
github.com/NYTimes/gziphandler v1.1.1
github.com/antithesishq/antithesis-sdk-go v0.3.8
github.com/ava-labs/coreth v0.13.9-rc.2-encapsulate-signer
github.com/ava-labs/coreth v0.14.1-0.20241230191223-351149733d35
github.com/ava-labs/ledger-avalanche/go v0.0.0-20241009183145-e6f90a8a1a60
github.com/btcsuite/btcd/btcutil v1.1.3
github.com/cockroachdb/pebble v0.0.0-20230928194634-aa077af62593
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ github.com/antithesishq/antithesis-sdk-go v0.3.8/go.mod h1:IUpT2DPAKh6i/YhSbt6Gl
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/ava-labs/coreth v0.13.9-rc.2-encapsulate-signer h1:mRB03tLPUvgNko4nP4VwWQdiHeHaLHtdwsnqwxrsGec=
github.com/ava-labs/coreth v0.13.9-rc.2-encapsulate-signer/go.mod h1:tqRAe+7bGLo2Rq/Ph4iYMSch72ag/Jn0DiDMDz1Xa9E=
github.com/ava-labs/coreth v0.14.1-0.20241230191223-351149733d35 h1:qBNnMleaJ7yWjNiDdV7wIf/e/PxubB+Ww7Mfx4QN4p8=
github.com/ava-labs/coreth v0.14.1-0.20241230191223-351149733d35/go.mod h1:nvQqJem4MuE0pU93aqBPsaEZx9NnXT0lI8d6rrQS5uY=
github.com/ava-labs/ledger-avalanche/go v0.0.0-20241009183145-e6f90a8a1a60 h1:EL66gtXOAwR/4KYBjOV03LTWgkEXvLePribLlJNu4g0=
github.com/ava-labs/ledger-avalanche/go v0.0.0-20241009183145-e6f90a8a1a60/go.mod h1:/7qKobTfbzBu7eSTVaXMTr56yTYk4j2Px6/8G+idxHo=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
Expand Down
5 changes: 2 additions & 3 deletions tests/e2e/c/dynamic_fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/plugin/evm"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -48,7 +47,7 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {

tc.By("allocating a pre-funded key")
key := privateNetwork.PreFundedKeys[0]
ethAddress := evm.GetEthAddress(key)
ethAddress := key.EthAddress()

tc.By("initializing a coreth client")
node := privateNetwork.Nodes[0]
Expand Down Expand Up @@ -154,7 +153,7 @@ var _ = e2e.DescribeCChain("[Dynamic Fees]", func() {
// Create a recipient address
var (
recipientKey = e2e.NewPrivateKey(tc)
recipientEthAddress = evm.GetEthAddress(recipientKey)
recipientEthAddress = recipientKey.EthAddress()
)

// Create transaction
Expand Down
5 changes: 2 additions & 3 deletions tests/e2e/c/interchain_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"math/big"

"github.com/ava-labs/coreth/core/types"
"github.com/ava-labs/coreth/plugin/evm"
"github.com/onsi/ginkgo/v2"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -39,9 +38,9 @@ var _ = e2e.DescribeCChain("[Interchain Workflow]", func() {
tc.By("allocating a pre-funded key to send from and a recipient key to deliver to")
var (
senderKey = env.PreFundedKey
senderEthAddress = evm.GetEthAddress(senderKey)
senderEthAddress = senderKey.EthAddress()
recipientKey = e2e.NewPrivateKey(tc)
recipientEthAddress = evm.GetEthAddress(recipientKey)
recipientEthAddress = recipientKey.EthAddress()
)

tc.By("sending funds from one address to another on the C-Chain", func() {
Expand Down
3 changes: 1 addition & 2 deletions tests/e2e/p/interchain_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"math/big"
"time"

"github.com/ava-labs/coreth/plugin/evm"
"github.com/onsi/ginkgo/v2"
"github.com/spf13/cast"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -74,7 +73,7 @@ var _ = e2e.DescribePChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL
)

tc.By("defining common configuration")
recipientEthAddress := evm.GetEthAddress(recipientKey)
recipientEthAddress := recipientKey.EthAddress()
// Use the same owner for sending to X-Chain and importing funds to P-Chain
recipientOwner := secp256k1fx.OutputOwners{
Threshold: 1,
Expand Down
3 changes: 1 addition & 2 deletions tests/e2e/x/interchain_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package x
import (
"math/big"

"github.com/ava-labs/coreth/plugin/evm"
"github.com/onsi/ginkgo/v2"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -41,7 +40,7 @@ var _ = e2e.DescribeXChain("[Interchain Workflow]", ginkgo.Label(e2e.UsesCChainL
pWallet := baseWallet.P()

tc.By("defining common configuration")
recipientEthAddress := evm.GetEthAddress(recipientKey)
recipientEthAddress := recipientKey.EthAddress()
xBuilder := xWallet.Builder()
xContext := xBuilder.Context()
cBuilder := cWallet.Builder()
Expand Down
3 changes: 1 addition & 2 deletions tests/fixture/tmpnet/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/ava-labs/coreth/core"
"github.com/ava-labs/coreth/params"
"github.com/ava-labs/coreth/plugin/evm"

"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/ids"
Expand Down Expand Up @@ -117,7 +116,7 @@ func NewTestGenesis(
cChainBalances := make(core.GenesisAlloc, len(keysToFund))
for _, key := range keysToFund {
xChainBalances[key.Address()] = defaultFundedKeyXChainAmount
cChainBalances[evm.GetEthAddress(key)] = core.GenesisAccount{
cChainBalances[key.EthAddress()] = core.GenesisAccount{
Balance: defaultFundedKeyCChainAmount,
}
}
Expand Down
10 changes: 10 additions & 0 deletions utils/crypto/secp256k1/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"

"github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"

"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/ids"
Expand Down Expand Up @@ -166,6 +168,10 @@ func (k *PublicKey) Address() ids.ShortID {
return k.addr
}

func (k *PublicKey) EthAddress() common.Address {
return crypto.PubkeyToAddress(*(k.ToECDSA()))
}

func (k *PublicKey) Bytes() []byte {
if k.bytes == nil {
k.bytes = k.pk.SerializeCompressed()
Expand All @@ -190,6 +196,10 @@ func (k *PrivateKey) Address() ids.ShortID {
return k.PublicKey().Address()
}

func (k *PrivateKey) EthAddress() common.Address {
return crypto.PubkeyToAddress(*(k.PublicKey().ToECDSA()))
}

func (k *PrivateKey) Sign(msg []byte) ([]byte, error) {
return k.SignHash(hashing.ComputeHash256(msg))
}
Expand Down
7 changes: 1 addition & 6 deletions vms/secp256k1fx/keychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto/keychain"
Expand Down Expand Up @@ -55,7 +54,7 @@ func (kc *Keychain) Add(key *secp256k1.PrivateKey) {
avaxAddr := pk.Address()
if _, ok := kc.avaxAddrToKeyIndex[avaxAddr]; !ok {
kc.avaxAddrToKeyIndex[avaxAddr] = len(kc.Keys)
ethAddr := publicKeyToEthAddress(pk)
ethAddr := pk.EthAddress()
kc.ethAddrToKeyIndex[ethAddr] = len(kc.Keys)
kc.Keys = append(kc.Keys, key)
kc.Addrs.Add(avaxAddr)
Expand Down Expand Up @@ -169,7 +168,3 @@ func (kc Keychain) get(id ids.ShortID) (*secp256k1.PrivateKey, bool) {
}
return nil, false
}

func publicKeyToEthAddress(pk *secp256k1.PublicKey) common.Address {
return crypto.PubkeyToAddress(*(pk.ToECDSA()))
}
10 changes: 5 additions & 5 deletions wallet/chain/c/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"math/big"
"sync"

"github.com/ava-labs/coreth/plugin/evm"
"github.com/ava-labs/coreth/plugin/evm/atomic"

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/utils/math"
Expand All @@ -32,7 +32,7 @@ type Backend interface {
BuilderBackend
SignerBackend

AcceptAtomicTx(ctx context.Context, tx *evm.Tx) error
AcceptAtomicTx(ctx context.Context, tx *atomic.Tx) error
}

type backend struct {
Expand All @@ -57,9 +57,9 @@ func NewBackend(
}
}

func (b *backend) AcceptAtomicTx(ctx context.Context, tx *evm.Tx) error {
func (b *backend) AcceptAtomicTx(ctx context.Context, tx *atomic.Tx) error {
switch tx := tx.UnsignedAtomicTx.(type) {
case *evm.UnsignedImportTx:
case *atomic.UnsignedImportTx:
for _, input := range tx.ImportedInputs {
utxoID := input.InputID()
if err := b.RemoveUTXO(ctx, tx.SourceChain, utxoID); err != nil {
Expand All @@ -80,7 +80,7 @@ func (b *backend) AcceptAtomicTx(ctx context.Context, tx *evm.Tx) error {
balance.Mul(balance, avaxConversionRate)
account.Balance.Add(account.Balance, balance)
}
case *evm.UnsignedExportTx:
case *atomic.UnsignedExportTx:
txID := tx.ID()
for i, out := range tx.ExportedOutputs {
err := b.AddUTXO(
Expand Down
42 changes: 21 additions & 21 deletions wallet/chain/c/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"errors"
"math/big"

"github.com/ava-labs/coreth/plugin/evm"
"github.com/ava-labs/coreth/plugin/evm/atomic"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils"
Expand Down Expand Up @@ -71,7 +71,7 @@ type Builder interface {
to ethcommon.Address,
baseFee *big.Int,
options ...common.Option,
) (*evm.UnsignedImportTx, error)
) (*atomic.UnsignedImportTx, error)

// NewExportTx creates an export transaction that attempts to send all the
// provided [outputs] to the requested [chainID].
Expand All @@ -84,7 +84,7 @@ type Builder interface {
outputs []*secp256k1fx.TransferOutput,
baseFee *big.Int,
options ...common.Option,
) (*evm.UnsignedExportTx, error)
) (*atomic.UnsignedExportTx, error)
}

// BuilderBackend specifies the required information needed to build unsigned
Expand Down Expand Up @@ -185,7 +185,7 @@ func (b *builder) NewImportTx(
to ethcommon.Address,
baseFee *big.Int,
options ...common.Option,
) (*evm.UnsignedImportTx, error) {
) (*atomic.UnsignedImportTx, error) {
ops := common.NewOptions(options)
utxos, err := b.backend.UTXOs(ops.Context(), chainID)
if err != nil {
Expand Down Expand Up @@ -226,26 +226,26 @@ func (b *builder) NewImportTx(
}

utils.Sort(importedInputs)
tx := &evm.UnsignedImportTx{
tx := &atomic.UnsignedImportTx{
NetworkID: b.context.NetworkID,
BlockchainID: b.context.BlockchainID,
SourceChain: chainID,
ImportedInputs: importedInputs,
}

// We must initialize the bytes of the tx to calculate the initial cost
wrappedTx := &evm.Tx{UnsignedAtomicTx: tx}
if err := wrappedTx.Sign(evm.Codec, nil); err != nil {
wrappedTx := &atomic.Tx{UnsignedAtomicTx: tx}
if err := wrappedTx.Sign(atomic.Codec, nil); err != nil {
return nil, err
}

gasUsedWithoutOutput, err := tx.GasUsed(true /*=IsApricotPhase5*/)
if err != nil {
return nil, err
}
gasUsedWithOutput := gasUsedWithoutOutput + evm.EVMOutputGas
gasUsedWithOutput := gasUsedWithoutOutput + atomic.EVMOutputGas

txFee, err := evm.CalculateDynamicFee(gasUsedWithOutput, baseFee)
txFee, err := atomic.CalculateDynamicFee(gasUsedWithOutput, baseFee)
if err != nil {
return nil, err
}
Expand All @@ -254,7 +254,7 @@ func (b *builder) NewImportTx(
return nil, errInsufficientFunds
}

tx.Outs = []evm.EVMOutput{{
tx.Outs = []atomic.EVMOutput{{
Address: to,
Amount: importedAmount - txFee,
AssetID: avaxAssetID,
Expand All @@ -267,7 +267,7 @@ func (b *builder) NewExportTx(
outputs []*secp256k1fx.TransferOutput,
baseFee *big.Int,
options ...common.Option,
) (*evm.UnsignedExportTx, error) {
) (*atomic.UnsignedExportTx, error) {
var (
avaxAssetID = b.context.AVAXAssetID
exportedOutputs = make([]*avax.TransferableOutput, len(outputs))
Expand All @@ -287,17 +287,17 @@ func (b *builder) NewExportTx(
exportedAmount = newExportedAmount
}

avax.SortTransferableOutputs(exportedOutputs, evm.Codec)
tx := &evm.UnsignedExportTx{
avax.SortTransferableOutputs(exportedOutputs, atomic.Codec)
tx := &atomic.UnsignedExportTx{
NetworkID: b.context.NetworkID,
BlockchainID: b.context.BlockchainID,
DestinationChain: chainID,
ExportedOutputs: exportedOutputs,
}

// We must initialize the bytes of the tx to calculate the initial cost
wrappedTx := &evm.Tx{UnsignedAtomicTx: tx}
if err := wrappedTx.Sign(evm.Codec, nil); err != nil {
wrappedTx := &atomic.Tx{UnsignedAtomicTx: tx}
if err := wrappedTx.Sign(atomic.Codec, nil); err != nil {
return nil, err
}

Expand All @@ -306,7 +306,7 @@ func (b *builder) NewExportTx(
return nil, err
}

initialFee, err := evm.CalculateDynamicFee(cost, baseFee)
initialFee, err := atomic.CalculateDynamicFee(cost, baseFee)
if err != nil {
return nil, err
}
Expand All @@ -320,20 +320,20 @@ func (b *builder) NewExportTx(
ops = common.NewOptions(options)
ctx = ops.Context()
addrs = ops.EthAddresses(b.ethAddrs)
inputs = make([]evm.EVMInput, 0, addrs.Len())
inputs = make([]atomic.EVMInput, 0, addrs.Len())
)
for addr := range addrs {
if amountToConsume == 0 {
break
}

prevFee, err := evm.CalculateDynamicFee(cost, baseFee)
prevFee, err := atomic.CalculateDynamicFee(cost, baseFee)
if err != nil {
return nil, err
}

newCost := cost + evm.EVMInputGas
newFee, err := evm.CalculateDynamicFee(newCost, baseFee)
newCost := cost + atomic.EVMInputGas
newFee, err := atomic.CalculateDynamicFee(newCost, baseFee)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -371,7 +371,7 @@ func (b *builder) NewExportTx(
}

inputAmount := min(amountToConsume, avaxBalance)
inputs = append(inputs, evm.EVMInput{
inputs = append(inputs, atomic.EVMInput{
Address: addr,
Amount: inputAmount,
AssetID: avaxAssetID,
Expand Down
6 changes: 3 additions & 3 deletions wallet/chain/c/builder_with_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package c
import (
"math/big"

"github.com/ava-labs/coreth/plugin/evm"
"github.com/ava-labs/coreth/plugin/evm/atomic"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
Expand Down Expand Up @@ -59,7 +59,7 @@ func (b *builderWithOptions) NewImportTx(
to ethcommon.Address,
baseFee *big.Int,
options ...common.Option,
) (*evm.UnsignedImportTx, error) {
) (*atomic.UnsignedImportTx, error) {
return b.Builder.NewImportTx(
chainID,
to,
Expand All @@ -73,7 +73,7 @@ func (b *builderWithOptions) NewExportTx(
outputs []*secp256k1fx.TransferOutput,
baseFee *big.Int,
options ...common.Option,
) (*evm.UnsignedExportTx, error) {
) (*atomic.UnsignedExportTx, error) {
return b.Builder.NewExportTx(
chainID,
outputs,
Expand Down
Loading

0 comments on commit a8cc764

Please sign in to comment.