Skip to content

Commit 259e632

Browse files
committed
use global registration for coreth
1 parent e9b4c54 commit 259e632

File tree

8 files changed

+87
-95
lines changed

8 files changed

+87
-95
lines changed

cmd/server/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/ava-labs/avalanchego/ids"
14+
"github.com/ava-labs/coreth/plugin/evm"
1415
"github.com/coinbase/rosetta-sdk-go/asserter"
1516
"github.com/coinbase/rosetta-sdk-go/server"
1617
"github.com/coinbase/rosetta-sdk-go/types"
@@ -47,6 +48,9 @@ func init() {
4748
}
4849

4950
func main() {
51+
// Registers coreth to work with libevm
52+
evm.RegisterAllLibEVMExtras()
53+
5054
if opts.version {
5155
log.Printf("%s %s\n", cmdName, cmdVersion)
5256
return

mapper/block.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
)
88

99
// BlockMetadata returns meta data for a block
10-
// Must be called within emulate.CChain, otherwise the functions like
11-
// customtypes.BlockExtDataGasUsed will panic.
1210
func BlockMetadata(block *types.Block) map[string]interface{} {
1311
meta := map[string]interface{}{
1412
"gas_limit": hexutil.EncodeUint64(block.GasLimit()),

mapper/test_main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package mapper
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/ava-labs/coreth/plugin/evm"
8+
)
9+
10+
func TestMain(m *testing.M) {
11+
evm.RegisterAllLibEVMExtras()
12+
os.Exit(m.Run())
13+
}

mapper/transaction.go

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/ava-labs/avalanchego/ids"
1111
"github.com/ava-labs/avalanchego/utils/formatting/address"
1212
"github.com/ava-labs/avalanchego/vms/components/avax"
13-
"github.com/ava-labs/avalanchego/vms/evm/emulate"
1413
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
1514
"github.com/ava-labs/coreth/core"
1615
"github.com/ava-labs/coreth/plugin/evm/atomic"
@@ -335,37 +334,31 @@ func CrossChainTransactions(
335334
) ([]*types.Transaction, error) {
336335
transactions := []*types.Transaction{}
337336

338-
err := emulate.CChain(func() error {
339-
extra := customtypes.BlockExtData(block)
340-
if len(extra) == 0 {
341-
return nil
342-
}
337+
extra := customtypes.BlockExtData(block)
338+
if len(extra) == 0 {
339+
return transactions, nil
340+
}
341+
342+
atomicTxs, err := atomic.ExtractAtomicTxs(extra, block.Time() >= ap5Activation, atomic.Codec)
343+
if err != nil {
344+
return nil, err
345+
}
343346

344-
atomicTxs, err := atomic.ExtractAtomicTxs(extra, block.Time() >= ap5Activation, atomic.Codec)
347+
for _, tx := range atomicTxs {
348+
txOps, metadata, err := crossChainTransaction(networkIdentifier, chainIDToAliasMapping, 0, avaxAssetID, tx)
345349
if err != nil {
346-
return err
350+
return nil, err
347351
}
348352

349-
for _, tx := range atomicTxs {
350-
txOps, metadata, err := crossChainTransaction(networkIdentifier, chainIDToAliasMapping, 0, avaxAssetID, tx)
351-
if err != nil {
352-
return err
353-
}
354-
355-
transaction := &types.Transaction{
356-
TransactionIdentifier: &types.TransactionIdentifier{
357-
Hash: tx.ID().String(),
358-
},
359-
Operations: txOps,
360-
Metadata: metadata,
361-
}
362-
363-
transactions = append(transactions, transaction)
353+
transaction := &types.Transaction{
354+
TransactionIdentifier: &types.TransactionIdentifier{
355+
Hash: tx.ID().String(),
356+
},
357+
Operations: txOps,
358+
Metadata: metadata,
364359
}
365-
return nil
366-
})
367-
if err != nil {
368-
return nil, err
360+
361+
transactions = append(transactions, transaction)
369362
}
370363

371364
return transactions, nil

service/config.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package service
33
import (
44
"math/big"
55

6-
"github.com/ava-labs/avalanchego/vms/evm/emulate"
76
"github.com/ava-labs/coreth/params"
87
"github.com/coinbase/rosetta-sdk-go/types"
98

@@ -89,12 +88,6 @@ func GetChainConfig(chainID *big.Int) *params.ChainConfig {
8988
IstanbulBlock: big.NewInt(0),
9089
MuirGlacierBlock: big.NewInt(0),
9190
}
92-
err := emulate.CChain(func() error {
93-
params.SetEthUpgrades(c)
94-
return nil
95-
})
96-
if err != nil {
97-
return nil
98-
}
91+
params.SetEthUpgrades(c)
9992
return c
10093
}

service/config_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"testing"
55

66
"github.com/ava-labs/coreth/params"
7+
"github.com/ava-labs/coreth/plugin/evm"
78
"github.com/coinbase/rosetta-sdk-go/types"
89
"github.com/stretchr/testify/require"
910

1011
ethtypes "github.com/ava-labs/libevm/core/types"
1112
)
1213

1314
func TestConfig(t *testing.T) {
15+
evm.RegisterAllLibEVMExtras()
1416
t.Run("online", func(t *testing.T) {
1517
cfg := Config{
1618
Mode: "online",

service/service_block.go

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package service
22

33
import (
44
"context"
5-
"errors"
65
"math/big"
76
"strings"
87

98
"github.com/ava-labs/avalanchego/ids"
10-
"github.com/ava-labs/avalanchego/vms/evm/emulate"
119
"github.com/ava-labs/coreth/core"
1210
"github.com/ava-labs/libevm/common"
1311
"github.com/coinbase/rosetta-sdk-go/server"
@@ -86,8 +84,10 @@ func (s *BlockService) Block(
8684
}
8785

8886
var (
89-
block *ethtypes.Block
90-
err error
87+
blockIdentifier *types.BlockIdentifier
88+
parentBlockIdentifier *types.BlockIdentifier
89+
block *ethtypes.Block
90+
err error
9191
)
9292

9393
if hash := request.BlockIdentifier.Hash; hash != nil {
@@ -101,69 +101,45 @@ func (s *BlockService) Block(
101101
}
102102
return nil, WrapError(ErrClientError, err)
103103
}
104-
return s.parseBlock(ctx, request, block)
105-
}
106104

107-
func (s *BlockService) parseBlock(ctx context.Context, request *types.BlockRequest, block *ethtypes.Block) (*types.BlockResponse, *types.Error) {
108-
var (
109-
blockIdentifier *types.BlockIdentifier
110-
parentBlockIdentifier *types.BlockIdentifier
111-
blockResponse *types.BlockResponse
112-
rosettaErr *types.Error
113-
)
114-
115-
// emulate.CChain is used to simulate "block" function calls in a Coreth node.
116-
// Otherwise, methods like block.Hash() will not include Avalanche-specific
117-
// headers while calculating the block hash, and give incorrect data.
118-
err := emulate.CChain(func() error {
119-
blockIdentifier = &types.BlockIdentifier{
120-
Index: block.Number().Int64(),
121-
Hash: block.Hash().String(), // block.Hash() is cached, so it must be used within emulate.CChain
122-
}
105+
blockIdentifier = &types.BlockIdentifier{
106+
Index: block.Number().Int64(),
107+
Hash: block.Hash().String(),
108+
}
123109

124-
if block.ParentHash().String() != s.config.GenesisBlockHash {
125-
parentBlock, err := s.client.HeaderByHash(ctx, block.ParentHash())
126-
if err != nil {
127-
return err
128-
}
129-
130-
parentBlockIdentifier = &types.BlockIdentifier{
131-
Index: parentBlock.Number.Int64(),
132-
Hash: parentBlock.Hash().String(),
133-
}
134-
} else {
135-
parentBlockIdentifier = s.genesisBlock.BlockIdentifier
110+
if block.ParentHash().String() != s.config.GenesisBlockHash {
111+
parentBlock, err := s.client.HeaderByHash(ctx, block.ParentHash())
112+
if err != nil {
113+
return nil, WrapError(ErrClientError, err)
136114
}
137115

138-
transactions, rosettaErr := s.fetchTransactions(ctx, block)
139-
if rosettaErr != nil {
140-
return errors.New("failed to fetch transactions")
116+
parentBlockIdentifier = &types.BlockIdentifier{
117+
Index: parentBlock.Number.Int64(),
118+
Hash: parentBlock.Hash().String(),
141119
}
120+
} else {
121+
parentBlockIdentifier = s.genesisBlock.BlockIdentifier
122+
}
142123

143-
crosstx, rosettaErr := s.parseCrossChainTransactions(request.NetworkIdentifier, block)
144-
if rosettaErr != nil {
145-
return errors.New("failed to parse cross chain transactions")
146-
}
124+
transactions, terr := s.fetchTransactions(ctx, block)
125+
if terr != nil {
126+
return nil, terr
127+
}
147128

148-
blockResponse = &types.BlockResponse{
149-
Block: &types.Block{
150-
BlockIdentifier: blockIdentifier,
151-
ParentBlockIdentifier: parentBlockIdentifier,
152-
Timestamp: int64(block.Time() * utils.MillisecondsInSecond),
153-
Transactions: append(transactions, crosstx...),
154-
Metadata: mapper.BlockMetadata(block),
155-
},
156-
}
157-
return nil
158-
})
159-
if err != nil {
160-
if rosettaErr != nil {
161-
return nil, rosettaErr
162-
}
163-
return nil, WrapError(ErrClientError, err)
129+
crosstx, terr := s.parseCrossChainTransactions(request.NetworkIdentifier, block)
130+
if terr != nil {
131+
return nil, terr
164132
}
165133

166-
return blockResponse, nil
134+
return &types.BlockResponse{
135+
Block: &types.Block{
136+
BlockIdentifier: blockIdentifier,
137+
ParentBlockIdentifier: parentBlockIdentifier,
138+
Timestamp: int64(block.Time() * utils.MillisecondsInSecond),
139+
Transactions: append(transactions, crosstx...),
140+
Metadata: mapper.BlockMetadata(block),
141+
},
142+
}, nil
167143
}
168144

169145
// BlockTransaction implements the /block/transaction endpoint.

service/test_main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package service
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/ava-labs/coreth/plugin/evm"
8+
)
9+
10+
func TestMain(m *testing.M) {
11+
evm.RegisterAllLibEVMExtras()
12+
os.Exit(m.Run())
13+
}

0 commit comments

Comments
 (0)