aboutsummaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
authoraaronbuchwald <aaron.buchwald56@gmail.com>2020-11-30 12:22:29 -0500
committerGitHub <noreply@github.com>2020-11-30 12:22:29 -0500
commitcfc11729d0f24b8e42a3f9cbcf7534e2db09d720 (patch)
tree9478961d310c5b69ca65fc9551ef9976b1d01da5 /plugin
parentae4541f42a666fb5ae1c36d6f7423c3d9eb2c875 (diff)
parent415b1eb564efc70cf7e673358286e19de8551fbf (diff)
Merge pull request #62 from ava-labs/devv0.3.15
Dev
Diffstat (limited to 'plugin')
-rw-r--r--plugin/evm/client.go174
-rw-r--r--plugin/evm/export_tx.go2
-rw-r--r--plugin/evm/import_tx.go2
-rw-r--r--plugin/evm/import_tx_test.go4
-rw-r--r--plugin/evm/service.go105
-rw-r--r--plugin/evm/static_service.go22
-rw-r--r--plugin/evm/tx.go6
-rw-r--r--plugin/evm/user.go4
-rw-r--r--plugin/evm/vm.go57
-rw-r--r--plugin/evm/vm_test.go19
10 files changed, 283 insertions, 112 deletions
diff --git a/plugin/evm/client.go b/plugin/evm/client.go
new file mode 100644
index 0000000..0817545
--- /dev/null
+++ b/plugin/evm/client.go
@@ -0,0 +1,174 @@
+// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
+// See the file LICENSE for licensing terms.
+
+package evm
+
+import (
+ "fmt"
+ "time"
+
+ "github.com/ava-labs/avalanchego/api"
+ "github.com/ava-labs/avalanchego/ids"
+ "github.com/ava-labs/avalanchego/utils/formatting"
+ cjson "github.com/ava-labs/avalanchego/utils/json"
+ "github.com/ava-labs/avalanchego/utils/rpc"
+)
+
+// Client ...
+type Client struct {
+ requester rpc.EndpointRequester
+}
+
+// NewClient returns a Client for interacting with EVM [chain]
+func NewClient(uri, chain string, requestTimeout time.Duration) *Client {
+ return &Client{
+ requester: rpc.NewEndpointRequester(uri, fmt.Sprintf("/ext/bc/%s/avax", chain), "avax", requestTimeout),
+ }
+}
+
+// NewCChainClient returns a Client for interacting with the C Chain
+func NewCChainClient(uri string, requestTimeout time.Duration) *Client {
+ return NewClient(uri, "C", requestTimeout)
+}
+
+// IssueTx issues a transaction to a node and returns the TxID
+func (c *Client) IssueTx(txBytes []byte) (ids.ID, error) {
+ res := &api.JSONTxID{}
+ txStr, err := formatting.Encode(formatting.Hex, txBytes)
+ if err != nil {
+ return res.TxID, fmt.Errorf("problem hex encoding bytes: %w", err)
+ }
+ err = c.requester.SendRequest("issueTx", &api.FormattedTx{
+ Tx: txStr,
+ Encoding: formatting.Hex,
+ }, res)
+ return res.TxID, err
+}
+
+// GetTxStatus returns the status of [txID]
+// func (c *Client) GetTxStatus(txID ids.ID) (choices.Status, error) {
+// res := &GetTxStatusReply{}
+// err := c.requester.SendRequest("getTxStatus", &api.JSONTxID{
+// TxID: txID,
+// }, res)
+// return res.Status, err
+// }
+
+// GetTx returns the byte representation of [txID]
+func (c *Client) GetTx(txID ids.ID) ([]byte, error) {
+ res := &api.FormattedTx{}
+ err := c.requester.SendRequest("getTx", &api.GetTxArgs{
+ TxID: txID,
+ Encoding: formatting.Hex,
+ }, res)
+ if err != nil {
+ return nil, err
+ }
+
+ return formatting.Decode(formatting.Hex, res.Tx)
+}
+
+// GetUTXOs returns the byte representation of the UTXOs controlled by [addrs]
+func (c *Client) GetUTXOs(addrs []string, limit uint32, startAddress, startUTXOID string) ([][]byte, api.Index, error) {
+ res := &api.GetUTXOsReply{}
+ err := c.requester.SendRequest("getUTXOs", &api.GetUTXOsArgs{
+ Addresses: addrs,
+ Limit: cjson.Uint32(limit),
+ StartIndex: api.Index{
+ Address: startAddress,
+ UTXO: startUTXOID,
+ },
+ Encoding: formatting.Hex,
+ }, res)
+ if err != nil {
+ return nil, api.Index{}, err
+ }
+
+ utxos := make([][]byte, len(res.UTXOs))
+ for i, utxo := range res.UTXOs {
+ b, err := formatting.Decode(formatting.Hex, utxo)
+ if err != nil {
+ return nil, api.Index{}, err
+ }
+ utxos[i] = b
+ }
+ return utxos, res.EndIndex, nil
+}
+
+// CreateAddress creates a new address controlled by [user]
+func (c *Client) CreateAddress(user api.UserPass) (string, error) {
+ res := &api.JSONAddress{}
+ err := c.requester.SendRequest("createAddress", &user, res)
+ return res.Address, err
+}
+
+// ListAddresses returns all addresses on this chain controlled by [user]
+func (c *Client) ListAddresses(user api.UserPass) ([]string, error) {
+ res := &api.JSONAddresses{}
+ err := c.requester.SendRequest("listAddresses", &user, res)
+ return res.Addresses, err
+}
+
+// ExportKey returns the private key corresponding to [addr] controlled by [user]
+// in both Avalanche standard format and hex format
+func (c *Client) ExportKey(user api.UserPass, addr string) (string, string, error) {
+ res := &ExportKeyReply{}
+ err := c.requester.SendRequest("exportKey", &ExportKeyArgs{
+ UserPass: user,
+ Address: addr,
+ }, res)
+ return res.PrivateKey, res.PrivateKeyHex, err
+}
+
+// ImportKey imports [privateKey] to [user]
+func (c *Client) ImportKey(user api.UserPass, privateKey string) (string, error) {
+ res := &api.JSONAddress{}
+ err := c.requester.SendRequest("importKey", &ImportKeyArgs{
+ UserPass: user,
+ PrivateKey: privateKey,
+ }, res)
+ return res.Address, err
+}
+
+// Import sends an import transaction to import funds from [sourceChain] and
+// returns the ID of the newly created transaction
+func (c *Client) Import(user api.UserPass, to, sourceChain string) (ids.ID, error) {
+ res := &api.JSONTxID{}
+ err := c.requester.SendRequest("import", &ImportArgs{
+ UserPass: user,
+ To: to,
+ SourceChain: sourceChain,
+ }, res)
+ return res.TxID, err
+}
+
+// ExportAVAX sends AVAX from this chain to the address specified by [to].
+// Returns the ID of the newly created atomic transaction
+func (c *Client) ExportAVAX(
+ user api.UserPass,
+ amount uint64,
+ to string,
+) (ids.ID, error) {
+ return c.Export(user, amount, to, "AVAX")
+}
+
+// Export sends an asset from this chain to the P/C-Chain.
+// After this tx is accepted, the AVAX must be imported to the P/C-chain with an importTx.
+// Returns the ID of the newly created atomic transaction
+func (c *Client) Export(
+ user api.UserPass,
+ amount uint64,
+ to string,
+ assetID string,
+) (ids.ID, error) {
+ res := &api.JSONTxID{}
+ err := c.requester.SendRequest("export", &ExportArgs{
+ ExportAVAXArgs: ExportAVAXArgs{
+ UserPass: user,
+ Amount: cjson.Uint64(amount),
+ To: to,
+ },
+ AssetID: assetID,
+ }, res)
+ return res.TxID, err
+}
diff --git a/plugin/evm/export_tx.go b/plugin/evm/export_tx.go
index ed069d4..2735573 100644
--- a/plugin/evm/export_tx.go
+++ b/plugin/evm/export_tx.go
@@ -149,7 +149,7 @@ func (tx *UnsignedExportTx) Accept(ctx *snow.Context, _ database.Batch) error {
Out: out.Out,
}
- utxoBytes, err := Codec.Marshal(utxo)
+ utxoBytes, err := Codec.Marshal(codecVersion, utxo)
if err != nil {
return err
}
diff --git a/plugin/evm/import_tx.go b/plugin/evm/import_tx.go
index 23dbc5f..1ec394c 100644
--- a/plugin/evm/import_tx.go
+++ b/plugin/evm/import_tx.go
@@ -135,7 +135,7 @@ func (tx *UnsignedImportTx) SemanticVerify(
utxoBytes := allUTXOBytes[i]
utxo := &avax.UTXO{}
- if err := vm.codec.Unmarshal(utxoBytes, utxo); err != nil {
+ if _, err := vm.codec.Unmarshal(utxoBytes, utxo); err != nil {
return tempError{err}
}
diff --git a/plugin/evm/import_tx_test.go b/plugin/evm/import_tx_test.go
index 53b9494..973802a 100644
--- a/plugin/evm/import_tx_test.go
+++ b/plugin/evm/import_tx_test.go
@@ -153,7 +153,7 @@ func TestImportTxSemanticVerify(t *testing.T) {
},
},
}
- utxoBytes, err := vm.codec.Marshal(utxo)
+ utxoBytes, err := vm.codec.Marshal(codecVersion, utxo)
if err != nil {
t.Fatal(err)
}
@@ -312,7 +312,7 @@ func TestNewImportTx(t *testing.T) {
},
},
}
- utxoBytes, err := vm.codec.Marshal(utxo)
+ utxoBytes, err := vm.codec.Marshal(codecVersion, utxo)
if err != nil {
t.Fatal(err)
}
diff --git a/plugin/evm/service.go b/plugin/evm/service.go
index 65ef3a2..2bb06df 100644
--- a/plugin/evm/service.go
+++ b/plugin/evm/service.go
@@ -24,7 +24,7 @@ import (
)
const (
- version = "coreth-v0.3.14"
+ version = "coreth-v0.3.15"
)
// test constants
@@ -91,6 +91,17 @@ func (api *SnowmanAPI) IssueBlock(ctx context.Context) error {
return api.vm.tryBlockGen()
}
+// parseAssetID parses an assetID string into an ID
+func (service *AvaxAPI) parseAssetID(assetID string) (ids.ID, error) {
+ if assetID == "" {
+ return ids.ID{}, fmt.Errorf("assetID is required")
+ } else if assetID == "AVAX" {
+ return service.vm.ctx.AVAXAssetID, nil
+ } else {
+ return ids.FromString(assetID)
+ }
+}
+
// ClientVersion returns the version of the vm running
func (service *AvaxAPI) ClientVersion() string { return version }
@@ -127,7 +138,11 @@ func (service *AvaxAPI) ExportKey(r *http.Request, args *ExportKeyArgs, reply *E
if err != nil {
return fmt.Errorf("problem retrieving private key: %w", err)
}
- reply.PrivateKey = constants.SecretKeyPrefix + formatting.CB58{Bytes: sk.Bytes()}.String()
+ encodedKey, err := formatting.Encode(formatting.CB58, sk.Bytes())
+ if err != nil {
+ return fmt.Errorf("problem encoding bytes as cb58: %w", err)
+ }
+ reply.PrivateKey = constants.SecretKeyPrefix + encodedKey
reply.PrivateKeyHex = hexutil.Encode(sk.Bytes())
return nil
}
@@ -147,13 +162,13 @@ func (service *AvaxAPI) ImportKey(r *http.Request, args *ImportKeyArgs, reply *a
}
trimmedPrivateKey := strings.TrimPrefix(args.PrivateKey, constants.SecretKeyPrefix)
- formattedPrivateKey := formatting.CB58{}
- if err := formattedPrivateKey.FromString(trimmedPrivateKey); err != nil {
+ pkBytes, err := formatting.Decode(formatting.CB58, trimmedPrivateKey)
+ if err != nil {
return fmt.Errorf("problem parsing private key: %w", err)
}
factory := crypto.FactorySECP256K1R{}
- skIntf, err := factory.ToPrivateKey(formattedPrivateKey.Bytes)
+ skIntf, err := factory.ToPrivateKey(pkBytes)
if err != nil {
return fmt.Errorf("problem parsing private key: %w", err)
}
@@ -232,8 +247,6 @@ func (service *AvaxAPI) Import(_ *http.Request, args *ImportArgs, response *api.
type ExportAVAXArgs struct {
api.UserPass
- // AssetID of the tokens
- AssetID ids.ID `json:"assetID"`
// Amount of asset to send
Amount json.Uint64 `json:"amount"`
@@ -247,7 +260,7 @@ type ExportAVAXArgs struct {
func (service *AvaxAPI) ExportAVAX(_ *http.Request, args *ExportAVAXArgs, response *api.JSONTxID) error {
return service.Export(nil, &ExportArgs{
ExportAVAXArgs: *args,
- AssetID: service.vm.ctx.AVAXAssetID,
+ AssetID: service.vm.ctx.AVAXAssetID.String(),
}, response)
}
@@ -255,15 +268,17 @@ func (service *AvaxAPI) ExportAVAX(_ *http.Request, args *ExportAVAXArgs, respon
type ExportArgs struct {
ExportAVAXArgs
// AssetID of the tokens
- AssetID ids.ID `json:"assetID"`
+ AssetID string `json:"assetID"`
}
// Export exports an asset from the C-Chain to the X-Chain
// It must be imported on the X-Chain to complete the transfer
func (service *AvaxAPI) Export(_ *http.Request, args *ExportArgs, response *api.JSONTxID) error {
log.Info("EVM: Export called")
- if args.AssetID == ids.Empty {
- return fmt.Errorf("assetID is required")
+
+ assetID, err := service.parseAssetID(args.AssetID)
+ if err != nil {
+ return err
}
if args.Amount == 0 {
@@ -290,7 +305,7 @@ func (service *AvaxAPI) Export(_ *http.Request, args *ExportArgs, response *api.
// Create the transaction
tx, err := service.vm.newExportTx(
- args.AssetID, // AssetID
+ assetID, // AssetID
uint64(args.Amount), // Amount
chainID, // ID of the chain to send the funds to
to, // Address
@@ -304,49 +319,8 @@ func (service *AvaxAPI) Export(_ *http.Request, args *ExportArgs, response *api.
return service.vm.issueTx(tx)
}
-// Index is an address and an associated UTXO.
-// Marks a starting or stopping point when fetching UTXOs. Used for pagination.
-type Index struct {
- Address string `json:"address"` // The address as a string
- UTXO string `json:"utxo"` // The UTXO ID as a string
-}
-
-// GetUTXOsArgs are arguments for passing into GetUTXOs.
-// Gets the UTXOs that reference at least one address in [Addresses].
-// Returns at most [limit] addresses.
-// If specified, [SourceChain] is the chain where the atomic UTXOs were exported from. If not specified,
-// then GetUTXOs returns an error since the C Chain only has atomic UTXOs.
-// If [limit] == 0 or > [maxUTXOsToFetch], fetches up to [maxUTXOsToFetch].
-// [StartIndex] defines where to start fetching UTXOs (for pagination.)
-// UTXOs fetched are from addresses equal to or greater than [StartIndex.Address]
-// For address [StartIndex.Address], only UTXOs with IDs greater than [StartIndex.UTXO] will be returned.
-// If [StartIndex] is omitted, gets all UTXOs.
-// If GetUTXOs is called multiple times, with our without [StartIndex], it is not guaranteed
-// that returned UTXOs are unique. That is, the same UTXO may appear in the response of multiple calls.
-type GetUTXOsArgs struct {
- Addresses []string `json:"addresses"`
- SourceChain string `json:"sourceChain"`
- Limit json.Uint32 `json:"limit"`
- StartIndex Index `json:"startIndex"`
- Encoding string `json:"encoding"`
-}
-
-// GetUTXOsReply defines the GetUTXOs replies returned from the API
-type GetUTXOsReply struct {
- // Number of UTXOs returned
- NumFetched json.Uint64 `json:"numFetched"`
- // The UTXOs
- UTXOs []string `json:"utxos"`
- // The last UTXO that was returned, and the address it corresponds to.
- // Used for pagination. To get the rest of the UTXOs, call GetUTXOs
- // again and set [StartIndex] to this value.
- EndIndex Index `json:"endIndex"`
- // Encoding specifies the encoding format the UTXOs are returned in
- Encoding string `json:"encoding"`
-}
-
// GetUTXOs gets all utxos for passed in addresses
-func (service *AvaxAPI) GetUTXOs(r *http.Request, args *GetUTXOsArgs, reply *GetUTXOsReply) error {
+func (service *AvaxAPI) GetUTXOs(r *http.Request, args *api.GetUTXOsArgs, reply *api.GetUTXOsReply) error {
service.vm.ctx.Log.Info("EVM: GetUTXOs called for with %s", args.Addresses)
if len(args.Addresses) == 0 {
@@ -356,11 +330,6 @@ func (service *AvaxAPI) GetUTXOs(r *http.Request, args *GetUTXOsArgs, reply *Get
return fmt.Errorf("number of addresses given, %d, exceeds maximum, %d", len(args.Addresses), maxGetUTXOsAddrs)
}
- encoding, err := service.vm.encodingManager.GetEncoding(args.Encoding)
- if err != nil {
- return fmt.Errorf("problem getting encoding formatter for '%s': %w", args.Encoding, err)
- }
-
sourceChain := ids.ID{}
if args.SourceChain == "" {
return errNoSourceChain
@@ -407,11 +376,15 @@ func (service *AvaxAPI) GetUTXOs(r *http.Request, args *GetUTXOsArgs, reply *Get
reply.UTXOs = make([]string, len(utxos))
for i, utxo := range utxos {
- b, err := service.vm.codec.Marshal(utxo)
+ b, err := service.vm.codec.Marshal(codecVersion, utxo)
if err != nil {
return fmt.Errorf("problem marshalling UTXO: %w", err)
}
- reply.UTXOs[i] = encoding.ConvertBytes(b)
+ str, err := formatting.Encode(args.Encoding, b)
+ if err != nil {
+ return fmt.Errorf("problem encoding utxo: %w", err)
+ }
+ reply.UTXOs[i] = str
}
endAddress, err := service.vm.FormatLocalAddress(endAddr)
@@ -422,7 +395,7 @@ func (service *AvaxAPI) GetUTXOs(r *http.Request, args *GetUTXOsArgs, reply *Get
reply.EndIndex.Address = endAddress
reply.EndIndex.UTXO = endUTXOID.String()
reply.NumFetched = json.Uint64(len(utxos))
- reply.Encoding = encoding.Encoding()
+ reply.Encoding = args.Encoding
return nil
}
@@ -430,17 +403,13 @@ func (service *AvaxAPI) GetUTXOs(r *http.Request, args *GetUTXOsArgs, reply *Get
func (service *AvaxAPI) IssueTx(r *http.Request, args *api.FormattedTx, response *api.JSONTxID) error {
log.Info("EVM: IssueTx called")
- encoding, err := service.vm.encodingManager.GetEncoding(args.Encoding)
- if err != nil {
- return fmt.Errorf("problem getting encoding formatter for '%s': %w", args.Encoding, err)
- }
- txBytes, err := encoding.ConvertString(args.Tx)
+ txBytes, err := formatting.Decode(args.Encoding, args.Tx)
if err != nil {
return fmt.Errorf("problem decoding transaction: %w", err)
}
tx := &Tx{}
- if err := service.vm.codec.Unmarshal(txBytes, tx); err != nil {
+ if _, err := service.vm.codec.Unmarshal(txBytes, tx); err != nil {
return fmt.Errorf("problem parsing transaction: %w", err)
}
if err := tx.Sign(service.vm.codec, nil); err != nil {
diff --git a/plugin/evm/static_service.go b/plugin/evm/static_service.go
index 1e48734..7b0fa8b 100644
--- a/plugin/evm/static_service.go
+++ b/plugin/evm/static_service.go
@@ -7,16 +7,32 @@ import (
"context"
"encoding/json"
- "github.com/ava-labs/coreth/core"
"github.com/ava-labs/avalanchego/utils/formatting"
+ "github.com/ava-labs/coreth/core"
)
// StaticService defines the static API services exposed by the evm
type StaticService struct{}
+// BuildGenesisReply is the reply from BuildGenesis
+type BuildGenesisReply struct {
+ Bytes string `json:"bytes"`
+ Encoding formatting.Encoding `json:"encoding"`
+}
+
// BuildGenesis returns the UTXOs such that at least one address in [args.Addresses] is
// referenced in the UTXO.
-func (*StaticService) BuildGenesis(_ context.Context, args *core.Genesis) (formatting.CB58, error) {
+func (*StaticService) BuildGenesis(_ context.Context, args *core.Genesis) (*BuildGenesisReply, error) {
bytes, err := json.Marshal(args)
- return formatting.CB58{Bytes: bytes}, err
+ if err != nil {
+ return nil, err
+ }
+ bytesStr, err := formatting.Encode(formatting.Hex, bytes)
+ if err != nil {
+ return nil, err
+ }
+ return &BuildGenesisReply{
+ Bytes: bytesStr,
+ Encoding: formatting.Hex,
+ }, nil
}
diff --git a/plugin/evm/tx.go b/plugin/evm/tx.go
index 82d4c10..1c0ce48 100644
--- a/plugin/evm/tx.go
+++ b/plugin/evm/tx.go
@@ -89,8 +89,8 @@ type Tx struct {
// (*secp256k1fx.Credential)
// Sign this transaction with the provided signers
-func (tx *Tx) Sign(c codec.Codec, signers [][]*crypto.PrivateKeySECP256K1R) error {
- unsignedBytes, err := c.Marshal(&tx.UnsignedTx)
+func (tx *Tx) Sign(c codec.Manager, signers [][]*crypto.PrivateKeySECP256K1R) error {
+ unsignedBytes, err := c.Marshal(codecVersion, &tx.UnsignedTx)
if err != nil {
return fmt.Errorf("couldn't marshal UnsignedTx: %w", err)
}
@@ -111,7 +111,7 @@ func (tx *Tx) Sign(c codec.Codec, signers [][]*crypto.PrivateKeySECP256K1R) erro
tx.Creds = append(tx.Creds, cred) // Attach credential
}
- signedBytes, err := c.Marshal(tx)
+ signedBytes, err := c.Marshal(codecVersion, tx)
if err != nil {
return fmt.Errorf("couldn't marshal Tx: %w", err)
}
diff --git a/plugin/evm/user.go b/plugin/evm/user.go
index 0ab1863..fc4587e 100644
--- a/plugin/evm/user.go
+++ b/plugin/evm/user.go
@@ -48,7 +48,7 @@ func (u *user) getAddresses() ([]common.Address, error) {
return nil, err
}
addresses := []common.Address{}
- if err := Codec.Unmarshal(bytes, &addresses); err != nil {
+ if _, err := Codec.Unmarshal(bytes, &addresses); err != nil {
return nil, err
}
return addresses, nil
@@ -94,7 +94,7 @@ func (u *user) putAddress(privKey *crypto.PrivateKeySECP256K1R) error {
}
}
addresses = append(addresses, address)
- bytes, err := Codec.Marshal(addresses)
+ bytes, err := Codec.Marshal(codecVersion, addresses)
if err != nil {
return err
}
diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go
index 284a772..58ab600 100644
--- a/plugin/evm/vm.go
+++ b/plugin/evm/vm.go
@@ -70,6 +70,7 @@ const (
batchSize = 250
maxUTXOsToFetch = 1024
blockCacheSize = 1 << 10 // 1024
+ codecVersion = uint16(0)
)
const (
@@ -110,26 +111,29 @@ func maxDuration(x, y time.Duration) time.Duration {
}
// Codec does serialization and deserialization
-var Codec codec.Codec
+var Codec codec.Manager
func init() {
- Codec = codec.NewDefault()
+ Codec = codec.NewDefaultManager()
+ c := codec.NewDefault()
errs := wrappers.Errs{}
errs.Add(
- Codec.RegisterType(&UnsignedImportTx{}),
- Codec.RegisterType(&UnsignedExportTx{}),
+ c.RegisterType(&UnsignedImportTx{}),
+ c.RegisterType(&UnsignedExportTx{}),
)
- Codec.Skip(3)
+ c.Skip(3)
errs.Add(
- Codec.RegisterType(&secp256k1fx.TransferInput{}),
- Codec.RegisterType(&secp256k1fx.MintOutput{}),
- Codec.RegisterType(&secp256k1fx.TransferOutput{}),
- Codec.RegisterType(&secp256k1fx.MintOperation{}),
- Codec.RegisterType(&secp256k1fx.Credential{}),
- Codec.RegisterType(&secp256k1fx.Input{}),
- Codec.RegisterType(&secp256k1fx.OutputOwners{}),
+ c.RegisterType(&secp256k1fx.TransferInput{}),
+ c.RegisterType(&secp256k1fx.MintOutput{}),
+ c.RegisterType(&secp256k1fx.TransferOutput{}),
+ c.RegisterType(&secp256k1fx.MintOperation{}),
+ c.RegisterType(&secp256k1fx.Credential{}),
+ c.RegisterType(&secp256k1fx.Input{}),
+ c.RegisterType(&secp256k1fx.OutputOwners{}),
+ Codec.RegisterCodec(codecVersion, c),
)
+
if errs.Errored() {
panic(errs.Err)
}
@@ -139,8 +143,7 @@ func init() {
type VM struct {
ctx *snow.Context
- CLIConfig CommandLineConfig
- encodingManager formatting.EncodingManager
+ CLIConfig CommandLineConfig
chainID *big.Int
networkID uint64
@@ -173,7 +176,8 @@ type VM struct {
txSubmitChan <-chan struct{}
atomicTxSubmitChan chan struct{}
shutdownSubmitChan chan struct{}
- codec codec.Codec
+ baseCodec codec.Codec
+ codec codec.Manager
clock timer.Clock
txFee uint64
pendingAtomicTxs chan *Tx
@@ -187,7 +191,7 @@ type VM struct {
func (vm *VM) getAtomicTx(block *types.Block) *Tx {
extdata := block.ExtraData()
atx := new(Tx)
- if err := vm.codec.Unmarshal(extdata, atx); err != nil {
+ if _, err := vm.codec.Unmarshal(extdata, atx); err != nil {
return nil
}
atx.Sign(vm.codec, nil)
@@ -195,7 +199,10 @@ func (vm *VM) getAtomicTx(block *types.Block) *Tx {
}
// Codec implements the secp256k1fx interface
-func (vm *VM) Codec() codec.Codec { return codec.NewDefault() }
+func (vm *VM) Codec() codec.Manager { return vm.codec }
+
+// CodecRegistry implements the secp256k1fx interface
+func (vm *VM) CodecRegistry() codec.Registry { return vm.baseCodec }
// Clock implements the secp256k1fx interface
func (vm *VM) Clock() *timer.Clock { return &vm.clock }
@@ -221,12 +228,6 @@ func (vm *VM) Initialize(
return vm.CLIConfig.ParsingError
}
- encodingManager, err := formatting.NewEncodingManager(formatting.CB58Encoding)
- if err != nil {
- return fmt.Errorf("problem creating encoding manager: %w", err)
- }
- vm.encodingManager = encodingManager
-
if len(fxs) > 0 {
return errUnsupportedFXs
}
@@ -284,7 +285,7 @@ func (vm *VM) Initialize(
vm.newBlockChan <- nil
return nil, err
}
- raw, _ := vm.codec.Marshal(atx)
+ raw, _ := vm.codec.Marshal(codecVersion, atx)
return raw, nil
default:
if len(txs) == 0 {
@@ -382,10 +383,14 @@ func (vm *VM) Initialize(
vm.genesisHash = chain.GetGenesisBlock().Hash()
log.Info(fmt.Sprintf("lastAccepted = %s", vm.lastAccepted.ethBlock.Hash().Hex()))
- // TODO: shutdown this go routine
vm.shutdownWg.Add(1)
go vm.ctx.Log.RecoverAndPanic(vm.awaitSubmittedTxs)
vm.codec = Codec
+ // The Codec explicitly registers the types it requires from the secp256k1fx
+ // so [vm.baseCodec] is a dummy codec use to fulfill the secp256k1fx VM
+ // interface. The fx will register all of its types, which can be safely
+ // ignored by the VM's codec.
+ vm.baseCodec = codec.NewDefault()
return vm.fx.Initialize(vm)
}
@@ -857,7 +862,7 @@ func (vm *VM) GetAtomicUTXOs(
utxos := make([]*avax.UTXO, len(allUTXOBytes))
for i, utxoBytes := range allUTXOBytes {
utxo := &avax.UTXO{}
- if err := vm.codec.Unmarshal(utxoBytes, utxo); err != nil {
+ if _, err := vm.codec.Unmarshal(utxoBytes, utxo); err != nil {
return nil, ids.ShortID{}, ids.ID{}, fmt.Errorf("error parsing UTXO: %w", err)
}
utxos[i] = utxo
diff --git a/plugin/evm/vm_test.go b/plugin/evm/vm_test.go
index 020b663..0e9c102 100644
--- a/plugin/evm/vm_test.go
+++ b/plugin/evm/vm_test.go
@@ -36,7 +36,7 @@ var (
)
func init() {
- cb58 := formatting.CB58{}
+ var b []byte
factory := crypto.FactorySECP256K1R{}
for _, key := range []string{
@@ -44,8 +44,8 @@ func init() {
"2MMvUMsxx6zsHSNXJdFD8yc5XkancvwyKPwpw4xUK3TCGDuNBY",
"cxb7KpGWhDMALTjNNSJ7UQkkomPesyWAPUaWRGdyeBNzR6f35",
} {
- _ = cb58.FromString(key)
- pk, _ := factory.ToPrivateKey(cb58.Bytes)
+ b, _ = formatting.Decode(formatting.CB58, key)
+ pk, _ := factory.ToPrivateKey(b)
secpKey := pk.(*crypto.PrivateKeySECP256K1R)
testKeys = append(testKeys, secpKey)
testEthAddrs = append(testEthAddrs, GetEthAddress(secpKey))
@@ -67,7 +67,11 @@ func BuildGenesisTest(t *testing.T) []byte {
if err != nil {
t.Fatalf("Failed to create test genesis")
}
- return genesisReply.Bytes
+ genesisBytes, err := formatting.Decode(genesisReply.Encoding, genesisReply.Bytes)
+ if err != nil {
+ t.Fatalf("Failed to decode genesis bytes: %s", err)
+ }
+ return genesisBytes
}
func NewContext() *snow.Context {
@@ -102,7 +106,10 @@ func GenesisVM(t *testing.T, finishBootstrapping bool) (chan engCommon.Message,
// The caller of this function is responsible for unlocking.
ctx.Lock.Lock()
- userKeystore := keystore.CreateTestKeystore()
+ userKeystore, err := keystore.CreateTestKeystore()
+ if err != nil {
+ t.Fatal(err)
+ }
if err := userKeystore.AddUser(username, password); err != nil {
t.Fatal(err)
}
@@ -112,7 +119,7 @@ func GenesisVM(t *testing.T, finishBootstrapping bool) (chan engCommon.Message,
vm := &VM{
txFee: testTxFee,
}
- err := vm.Initialize(
+ err = vm.Initialize(
ctx,
prefixdb.New([]byte{1}, baseDB),
genesisBytes,
n1369' href='#n1369'>1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235