Skip to content

Commit

Permalink
Merge pull request #88 from multiversx/spica-01
Browse files Browse the repository at this point in the history
Reference newer libraries. Minor refactoring.
  • Loading branch information
andreibancioiu authored Aug 12, 2024
2 parents 280364a + e4589b6 commit 0432f0c
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 116 deletions.
51 changes: 24 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,57 @@ go 1.18
require (
github.com/coinbase/rosetta-sdk-go v0.8.3
github.com/coinbase/rosetta-sdk-go/types v1.0.0
github.com/multiversx/mx-chain-core-go v1.1.37
github.com/multiversx/mx-chain-go v1.5.13
github.com/multiversx/mx-chain-logger-go v1.0.11
github.com/multiversx/mx-chain-proxy-go v1.1.39
github.com/multiversx/mx-chain-storage-go v1.0.7
github.com/gin-gonic/gin v1.9.1
github.com/multiversx/mx-chain-core-go v1.2.21
github.com/multiversx/mx-chain-go v1.7.17
github.com/multiversx/mx-chain-logger-go v1.0.15
github.com/multiversx/mx-chain-proxy-go v1.1.50
github.com/multiversx/mx-chain-storage-go v1.0.16
github.com/stretchr/testify v1.8.4
github.com/urfave/cli v1.22.10
)

require (
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
github.com/bytedance/sonic v1.8.0 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/denisbrodbeck/machineid v1.0.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.11.2 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/golang-lru v0.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiversx/concurrent-map v0.1.4 // indirect
github.com/multiversx/mx-chain-crypto-go v1.2.6 // indirect
github.com/multiversx/mx-chain-es-indexer-go v1.3.20 // indirect
github.com/multiversx/mx-chain-p2p-go v1.0.18 // indirect
github.com/multiversx/mx-chain-vm-common-go v1.3.42 // indirect
github.com/multiversx/mx-chain-communication-go v1.1.0 // indirect
github.com/multiversx/mx-chain-crypto-go v1.2.12 // indirect
github.com/multiversx/mx-chain-es-indexer-go v1.7.4 // indirect
github.com/multiversx/mx-chain-vm-common-go v1.5.13 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.9 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.10.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
120 changes: 59 additions & 61 deletions go.sum

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion server/factory/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
hasherType = "blake2b"
marshalizerForHashingType = "gogo protobuf"
pubKeyLength = 32
bech32Prefix = "erd"

notApplicableConfigurationFilePath = "not applicable"
notApplicableFullHistoryNodesMessage = "not applicable"
Expand Down Expand Up @@ -56,7 +57,7 @@ func CreateNetworkProvider(args ArgsCreateNetworkProvider) (NetworkProvider, err
return nil, err
}

pubKeyConverter, err := pubkeyConverter.NewBech32PubkeyConverter(pubKeyLength, log)
pubKeyConverter, err := pubkeyConverter.NewBech32PubkeyConverter(pubKeyLength, bech32Prefix)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion server/provider/currenciesProvider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package provider

import "github.com/multiversx/mx-chain-rosetta/server/resources"
import (
"github.com/multiversx/mx-chain-rosetta/server/resources"
)

type currenciesProvider struct {
nativeCurrency resources.Currency
Expand Down
8 changes: 7 additions & 1 deletion server/provider/networkProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,13 @@ func (provider *networkProvider) ComputeShardIdOfPubKey(pubKey []byte) uint32 {

// ConvertPubKeyToAddress converts a public key to an address
func (provider *networkProvider) ConvertPubKeyToAddress(pubkey []byte) string {
return provider.pubKeyConverter.Encode(pubkey)
address, err := provider.pubKeyConverter.Encode(pubkey)
if err != nil {
log.Warn("networkProvider.ConvertPubKeyToAddress()", "pubkey", pubkey, "err", err)
return ""
}

return address
}

// ConvertAddressToPubKey converts an address to a pubkey
Expand Down
2 changes: 1 addition & 1 deletion server/services/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var (
transactionVersion = 1
transactionProcessingTypeRelayed = "RelayedTx"
transactionProcessingTypeRelayedV1 = "RelayedTx"
transactionProcessingTypeBuiltInFunctionCall = "BuiltInFunctionCall"
transactionProcessingTypeMoveBalance = "MoveBalance"
amountZero = "0"
Expand Down
5 changes: 2 additions & 3 deletions server/services/relayedTransactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ import (

// innerTransactionOfRelayedV1 is used to parse the inner transaction of a relayed V1 transaction, and holds only the fields handled by Rosetta.
type innerTransactionOfRelayedV1 struct {
Nonce uint64 `json:"nonce"`
Value big.Int `json:"value"`
ReceiverPubKey []byte `json:"receiver"`
SenderPubKey []byte `json:"sender"`
}

func isRelayedV1Transaction(tx *transaction.ApiTransactionResult) bool {
return (tx.Type == string(transaction.TxTypeNormal)) &&
(tx.ProcessingTypeOnSource == transactionProcessingTypeRelayed) &&
(tx.ProcessingTypeOnDestination == transactionProcessingTypeRelayed)
(tx.ProcessingTypeOnSource == transactionProcessingTypeRelayedV1) &&
(tx.ProcessingTypeOnDestination == transactionProcessingTypeRelayedV1)
}

func parseInnerTxOfRelayedV1(tx *transaction.ApiTransactionResult) (*innerTransactionOfRelayedV1, error) {
Expand Down
5 changes: 2 additions & 3 deletions server/services/relayedTransactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func Test_IsRelayedV1Transaction(t *testing.T) {
t.Run("relayed v1 tx", func(t *testing.T) {
tx := &transaction.ApiTransactionResult{
Type: string(transaction.TxTypeNormal),
ProcessingTypeOnSource: transactionProcessingTypeRelayed,
ProcessingTypeOnDestination: transactionProcessingTypeRelayed,
ProcessingTypeOnSource: transactionProcessingTypeRelayedV1,
ProcessingTypeOnDestination: transactionProcessingTypeRelayedV1,
}

require.True(t, isRelayedV1Transaction(tx))
Expand All @@ -42,7 +42,6 @@ func Test_ParseInnerTxOfRelayedV1(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, innerTx)

require.Equal(t, uint64(7), innerTx.Nonce)
require.Equal(t, "1000000000000000000", innerTx.Value.String())
require.Equal(t, testscommon.TestPubKeyAlice, innerTx.SenderPubKey)
require.Equal(t, testscommon.TestPubKeyBob, innerTx.ReceiverPubKey)
Expand Down
2 changes: 1 addition & 1 deletion server/services/transactionsFeaturesDetector.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (detector *transactionsFeaturesDetector) isInvalidTransactionOfTypeMoveBala
return withSendingValueToNonPayableContract || withMetaTransactionIsInvalid
}

func (detector *transactionsFeaturesDetector) isRelayedTransactionCompletelyIntrashardWithSignalError(tx *transaction.ApiTransactionResult, innerTx *innerTransactionOfRelayedV1) bool {
func (detector *transactionsFeaturesDetector) isRelayedV1TransactionCompletelyIntrashardWithSignalError(tx *transaction.ApiTransactionResult, innerTx *innerTransactionOfRelayedV1) bool {
innerTxSenderShard := detector.networkProvider.ComputeShardIdOfPubKey(innerTx.SenderPubKey)
innerTxReceiverShard := detector.networkProvider.ComputeShardIdOfPubKey(innerTx.ReceiverPubKey)

Expand Down
8 changes: 4 additions & 4 deletions server/services/transactionsFeaturesDetector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestTransactionsFeaturesDetector_IsInvalidTransactionOfTypeMoveBalanceThatO
})
}

func TestTransactionsFeaturesDetector_IsRelayedTransactionCompletelyIntrashardWithSignalError(t *testing.T) {
func TestTransactionsFeaturesDetector_isRelayedV1TransactionCompletelyIntrashardWithSignalError(t *testing.T) {
networkProvider := testscommon.NewNetworkProviderMock()
detector := newTransactionsFeaturesDetector(networkProvider)

Expand All @@ -90,7 +90,7 @@ func TestTransactionsFeaturesDetector_IsRelayedTransactionCompletelyIntrashardWi
ReceiverPubKey: testscommon.TestUserShard2.PubKey,
}

featureDetected := detector.isRelayedTransactionCompletelyIntrashardWithSignalError(tx, innerTx)
featureDetected := detector.isRelayedV1TransactionCompletelyIntrashardWithSignalError(tx, innerTx)
require.False(t, featureDetected)
})

Expand All @@ -105,7 +105,7 @@ func TestTransactionsFeaturesDetector_IsRelayedTransactionCompletelyIntrashardWi
ReceiverPubKey: testscommon.TestUserBShard0.PubKey,
}

featureDetected := detector.isRelayedTransactionCompletelyIntrashardWithSignalError(tx, innerTx)
featureDetected := detector.isRelayedV1TransactionCompletelyIntrashardWithSignalError(tx, innerTx)
require.False(t, featureDetected)
})

Expand All @@ -127,7 +127,7 @@ func TestTransactionsFeaturesDetector_IsRelayedTransactionCompletelyIntrashardWi
ReceiverPubKey: testscommon.TestUserBShard0.PubKey,
}

featureDetected := detector.isRelayedTransactionCompletelyIntrashardWithSignalError(tx, innerTx)
featureDetected := detector.isRelayedV1TransactionCompletelyIntrashardWithSignalError(tx, innerTx)
require.True(t, featureDetected)
})
}
4 changes: 3 additions & 1 deletion server/services/transactionsTransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ func (transformer *transactionsTransformer) normalTxToRosetta(tx *transaction.Ap
}, nil
}

// This only handles operations for the native balance.
func (transformer *transactionsTransformer) extractInnerTxOperationsIfRelayedCompletelyIntrashardWithSignalError(tx *transaction.ApiTransactionResult) ([]*types.Operation, error) {
// Only relayed V1 is handled. Relayed V2 cannot bear native value in the inner transaction.
isRelayedTransaction := isRelayedV1Transaction(tx)
if !isRelayedTransaction {
return []*types.Operation{}, nil
Expand All @@ -224,7 +226,7 @@ func (transformer *transactionsTransformer) extractInnerTxOperationsIfRelayedCom
return []*types.Operation{}, nil
}

if !transformer.featuresDetector.isRelayedTransactionCompletelyIntrashardWithSignalError(tx, innerTx) {
if !transformer.featuresDetector.isRelayedV1TransactionCompletelyIntrashardWithSignalError(tx, innerTx) {
return []*types.Operation{}, nil
}

Expand Down
16 changes: 8 additions & 8 deletions server/services/transactionsTransformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func TestTransactionsTransformer_NormalTxToRosettaTx(t *testing.T) {
t.Run("relayed tx, completely intrashard, with signal error", func(t *testing.T) {
tx := &transaction.ApiTransactionResult{
Type: string(transaction.TxTypeNormal),
ProcessingTypeOnSource: transactionProcessingTypeRelayed,
ProcessingTypeOnDestination: transactionProcessingTypeRelayed,
ProcessingTypeOnSource: transactionProcessingTypeRelayedV1,
ProcessingTypeOnDestination: transactionProcessingTypeRelayedV1,
Hash: "aaaa",
Sender: testscommon.TestUserAShard0.Address,
Receiver: testscommon.TestUserBShard0.Address,
Expand Down Expand Up @@ -118,8 +118,8 @@ func TestTransactionsTransformer_ExtractInnerTxOperationsIfRelayedCompletelyIntr
t.Run("relayed tx (badly formatted)", func(t *testing.T) {
tx := &transaction.ApiTransactionResult{
Type: string(transaction.TxTypeNormal),
ProcessingTypeOnSource: transactionProcessingTypeRelayed,
ProcessingTypeOnDestination: transactionProcessingTypeRelayed,
ProcessingTypeOnSource: transactionProcessingTypeRelayedV1,
ProcessingTypeOnDestination: transactionProcessingTypeRelayedV1,
Data: []byte("bad"),
}

Expand All @@ -131,8 +131,8 @@ func TestTransactionsTransformer_ExtractInnerTxOperationsIfRelayedCompletelyIntr
t.Run("relayed tx, completely intrashard, with signal error, inner tx has non-zero value", func(t *testing.T) {
tx := &transaction.ApiTransactionResult{
Type: string(transaction.TxTypeNormal),
ProcessingTypeOnSource: transactionProcessingTypeRelayed,
ProcessingTypeOnDestination: transactionProcessingTypeRelayed,
ProcessingTypeOnSource: transactionProcessingTypeRelayedV1,
ProcessingTypeOnDestination: transactionProcessingTypeRelayedV1,
SourceShard: 0,
DestinationShard: 0,
Data: []byte("relayedTx@7b226e6f6e6365223a372c2273656e646572223a226e69424758747949504349644a78793373796c6c455a474c78506a503148734a45646e43732b6d726577413d222c227265636569766572223a224141414141414141414141464145356c3079623173734a3933504433672f4b396f48384579366d576958513d222c2276616c7565223a313030303030303030303030303030303030302c226761735072696365223a313030303030303030302c226761734c696d6974223a35303030302c2264617461223a22222c227369676e6174757265223a226e6830743338585a77614b6a725878446969716f6d364d6a5671724d612f6b70767474696a33692b5a6d43492f3778626830596762363548424151445744396f7036575567674541755430756e5253595736455341413d3d222c22636861696e4944223a224d513d3d222c2276657273696f6e223a327d"),
Expand All @@ -159,8 +159,8 @@ func TestTransactionsTransformer_ExtractInnerTxOperationsIfRelayedCompletelyIntr
t.Run("relayed tx, completely intrashard, with signal error, inner tx has zero value", func(t *testing.T) {
tx := &transaction.ApiTransactionResult{
Type: string(transaction.TxTypeNormal),
ProcessingTypeOnSource: transactionProcessingTypeRelayed,
ProcessingTypeOnDestination: transactionProcessingTypeRelayed,
ProcessingTypeOnSource: transactionProcessingTypeRelayedV1,
ProcessingTypeOnDestination: transactionProcessingTypeRelayedV1,
SourceShard: 0,
DestinationShard: 0,
Data: []byte("relayedTx@7b226e6f6e6365223a372c2273656e646572223a226e69424758747949504349644a78793373796c6c455a474c78506a503148734a45646e43732b6d726577413d222c227265636569766572223a224141414141414141414141464145356c3079623173734a3933504433672f4b396f48384579366d576958513d222c2276616c7565223a302c226761735072696365223a313030303030303030302c226761734c696d6974223a35303030302c2264617461223a22222c227369676e6174757265223a22336c644e7a32435734416143675069495863636c466b4654324149586a4a4757316a526a306c542b4f3161736b6241394a744e365a764173396e394f58716d343130574a49665332332b4168666e48793267446c41773d3d222c22636861696e4944223a224d513d3d222c2276657273696f6e223a327d"),
Expand Down
10 changes: 8 additions & 2 deletions testscommon/networkProviderMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type networkProviderMock struct {

// NewNetworkProviderMock -
func NewNetworkProviderMock() *networkProviderMock {
pubKeyConverter, _ := pubkeyConverter.NewBech32PubkeyConverter(32, log)
pubKeyConverter, _ := pubkeyConverter.NewBech32PubkeyConverter(32, "erd")

return &networkProviderMock{
pubKeyConverter: pubKeyConverter,
Expand Down Expand Up @@ -299,7 +299,13 @@ func (mock *networkProviderMock) ComputeShardIdOfPubKey(pubKey []byte) uint32 {

// ConvertPubKeyToAddress -
func (mock *networkProviderMock) ConvertPubKeyToAddress(pubkey []byte) string {
return mock.pubKeyConverter.Encode(pubkey)
address, err := mock.pubKeyConverter.Encode(pubkey)
if err != nil {
log.Error("networkProviderMock.ConvertPubKeyToAddress() failed", "pubkey", pubkey, "error", err)
return ""
}

return address
}

// ConvertAddressToPubKey -
Expand Down
3 changes: 1 addition & 2 deletions testscommon/realworld.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"github.com/multiversx/mx-chain-core-go/core/pubkeyConverter"
hasherFactory "github.com/multiversx/mx-chain-core-go/hashing/factory"
marshalFactory "github.com/multiversx/mx-chain-core-go/marshal/factory"
logger "github.com/multiversx/mx-chain-logger-go"
)

// RealWorldBech32PubkeyConverter is a bech32 converter, to be used in tests
var RealWorldBech32PubkeyConverter, _ = pubkeyConverter.NewBech32PubkeyConverter(32, logger.GetOrCreate("testscommon"))
var RealWorldBech32PubkeyConverter, _ = pubkeyConverter.NewBech32PubkeyConverter(32, "erd")

// RealWorldBlake2bHasher is a blake2b hasher, to be used in tests
var RealWorldBlake2bHasher, _ = hasherFactory.NewHasher("blake2b")
Expand Down

0 comments on commit 0432f0c

Please sign in to comment.