Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround (quick fix) to support construction of cross-shard transactions #52

Merged
merged 2 commits into from
Oct 5, 2022
Merged
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
38 changes: 27 additions & 11 deletions server/services/constructionService.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"

"github.com/ElrondNetwork/elrond-proxy-go/data"
"github.com/coinbase/rosetta-sdk-go/server"
Expand Down Expand Up @@ -38,9 +39,9 @@ func (service *constructionService) ConstructionPreprocess(
return nil, err
}

options, err := service.getOptionsFromOperations(request.Operations)
if err != nil {
return nil, err
options, errOptions := service.prepareConstructionOptions(request.Operations, request.Metadata)
if errOptions != nil {
return nil, service.errFactory.newErrWithOriginal(ErrConstructionCheck, errOptions)
}

if len(request.MaxFee) > 0 {
Expand Down Expand Up @@ -118,15 +119,30 @@ func checkOperationsType(op *types.Operation) bool {
return false
}

func (service *constructionService) getOptionsFromOperations(ops []*types.Operation) (objectsMap, *types.Error) {
if len(ops) < 2 {
return nil, service.errFactory.newErrWithOriginal(ErrConstructionCheck, errors.New("invalid number of operations"))
}
func (service *constructionService) prepareConstructionOptions(operations []*types.Operation, metadata objectsMap) (objectsMap, error) {
options := make(objectsMap)
options["sender"] = ops[0].Account.Address
options["receiver"] = ops[1].Account.Address
options["type"] = ops[0].Type
options["value"] = ops[1].Amount.Value
options["type"] = operations[0].Type
options["sender"] = operations[0].Account.Address

if metadata["receiver"] != nil {
options["receiver"] = metadata["receiver"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The receiver from the metadata takes priority.

} else {
if len(operations) > 1 {
options["receiver"] = operations[1].Account.Address
} else {
return nil, errors.New("cannot prepare transaction receiver")
}
}

if metadata["value"] != nil {
options["value"] = metadata["value"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value from the metadata takes priority.

} else {
if len(operations) > 1 {
options["value"] = operations[1].Amount.Value
} else {
options["value"] = strings.Trim(operations[0].Amount.Value, "-")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a temporary workaround - will be refactored in #51.

}
}

return options, nil
}
Expand Down
102 changes: 102 additions & 0 deletions server/services/constructionService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,105 @@ func TestConstructionService_CreateOperationsFromPreparedTx(t *testing.T) {
operations := service.createOperationsFromPreparedTx(preparedTx)
require.Equal(t, expectedOperations, operations)
}

func TestConstructionService_PrepareConstructionOptions(t *testing.T) {
t.Parallel()

networkProvider := testscommon.NewNetworkProviderMock()
extension := newNetworkProviderExtension(networkProvider)
service := NewConstructionService(networkProvider).(*constructionService)

t.Run("two operations, no metadata", func(t *testing.T) {
t.Parallel()

operations := []*types.Operation{
{
OperationIdentifier: indexToOperationIdentifier(0),
Type: opTransfer,
Account: addressToAccountIdentifier(testscommon.TestAddressAlice),
Amount: extension.valueToNativeAmount("-12345"),
},
{
OperationIdentifier: indexToOperationIdentifier(1),
Type: opTransfer,
Account: addressToAccountIdentifier(testscommon.TestAddressBob),
Amount: extension.valueToNativeAmount("12345"),
},
}

metadata := make(objectsMap)

options, err := service.prepareConstructionOptions(operations, metadata)
require.Nil(t, err)
require.Equal(t, opTransfer, options["type"])
require.Equal(t, testscommon.TestAddressAlice, options["sender"])
require.Equal(t, testscommon.TestAddressBob, options["receiver"])
require.Equal(t, "12345", options["value"])
})

t.Run("one operation, with metadata having: receiver", func(t *testing.T) {
t.Parallel()

operations := []*types.Operation{
{
OperationIdentifier: indexToOperationIdentifier(0),
Type: opTransfer,
Account: addressToAccountIdentifier(testscommon.TestAddressAlice),
Amount: extension.valueToNativeAmount("-12345"),
},
}

metadata := make(objectsMap)
metadata["receiver"] = testscommon.TestAddressBob

options, err := service.prepareConstructionOptions(operations, metadata)
require.Nil(t, err)
require.Equal(t, opTransfer, options["type"])
require.Equal(t, testscommon.TestAddressAlice, options["sender"])
require.Equal(t, testscommon.TestAddressBob, options["receiver"])
require.Equal(t, "12345", options["value"])
})

t.Run("one operation, with metadata having: receiver, value", func(t *testing.T) {
t.Parallel()

operations := []*types.Operation{
{
OperationIdentifier: indexToOperationIdentifier(0),
Type: opTransfer,
Account: addressToAccountIdentifier(testscommon.TestAddressAlice),
Amount: extension.valueToNativeAmount("ignored"),
},
}

metadata := make(objectsMap)
metadata["receiver"] = testscommon.TestAddressBob
metadata["value"] = "12345"

options, err := service.prepareConstructionOptions(operations, metadata)
require.Nil(t, err)
require.Equal(t, opTransfer, options["type"])
require.Equal(t, testscommon.TestAddressAlice, options["sender"])
require.Equal(t, testscommon.TestAddressBob, options["receiver"])
require.Equal(t, "12345", options["value"])
})

t.Run("one operation, with missing metadata: receiver", func(t *testing.T) {
t.Parallel()

operations := []*types.Operation{
{
OperationIdentifier: indexToOperationIdentifier(0),
Type: opTransfer,
Account: addressToAccountIdentifier(testscommon.TestAddressAlice),
Amount: extension.valueToNativeAmount("ignored"),
},
}

metadata := make(objectsMap)

options, err := service.prepareConstructionOptions(operations, metadata)
require.ErrorContains(t, err, "cannot prepare transaction receiver")
require.Nil(t, options)
})
}