Skip to content

Commit

Permalink
Handle contracts (initial implementation).
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Nov 9, 2023
1 parent 610dff1 commit 5db6aa4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
1 change: 1 addition & 0 deletions server/services/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
transactionProcessingTypeRelayed = "RelayedTx"
transactionProcessingTypeBuiltInFunctionCall = "BuiltInFunctionCall"
transactionProcessingTypeMoveBalance = "MoveBalance"
transactionProcessingTypeContractInvoking = "SCInvoking"
amountZero = "0"
builtInFunctionClaimDeveloperRewards = core.BuiltInFunctionClaimDeveloperRewards
builtInFunctionESDTTransfer = core.BuiltInFunctionESDTTransfer
Expand Down
3 changes: 1 addition & 2 deletions server/services/networkProviderExtension.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ func (extension *networkProviderExtension) isAddressObserved(address string) (bo
return false, err
}

isUserAddress := extension.isUserAddress(address)
return belongsToObservedShard && isUserAddress, nil
return belongsToObservedShard, nil
}

func (extension *networkProviderExtension) isUserAddress(address string) bool {
Expand Down
36 changes: 36 additions & 0 deletions server/services/transactionsFeaturesDetector.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,39 @@ func (detector *transactionsFeaturesDetector) isRelayedTransactionCompletelyIntr
isWithSignalError := detector.eventsController.hasAnySignalError(tx)
return isWithSignalError
}

func (detector *transactionsFeaturesDetector) isIntrashardContractCallWithSignalErrorButWithoutContractResultBearingRefundValue(
txInQuestion *transaction.ApiTransactionResult,
allTransactionsInBlock []*transaction.ApiTransactionResult,
) bool {
if !detector.isContractCallWithSignalError(txInQuestion) {
return false
}

if !detector.isIntrashard(txInQuestion) {
return false
}

for _, tx := range allTransactionsInBlock {
matchesTypeOnSource := tx.ProcessingTypeOnSource == transactionProcessingTypeMoveBalance
matchesTypeOnDestination := tx.ProcessingTypeOnDestination == transactionProcessingTypeMoveBalance
matchesOriginalTransactionHash := tx.OriginalTransactionHash == txInQuestion.Hash
matchesRefundValue := tx.Value == txInQuestion.Value

if matchesTypeOnSource && matchesTypeOnDestination && matchesOriginalTransactionHash && matchesRefundValue {
return false
}
}

return true
}

func (detector *transactionsFeaturesDetector) isContractCallWithSignalError(tx *transaction.ApiTransactionResult) bool {
return tx.ProcessingTypeOnSource == transactionProcessingTypeContractInvoking &&
tx.ProcessingTypeOnDestination == transactionProcessingTypeContractInvoking &&
detector.eventsController.hasAnySignalError(tx)
}

func (detector *transactionsFeaturesDetector) isIntrashard(tx *transaction.ApiTransactionResult) bool {
return tx.SourceShard == tx.DestinationShard
}
44 changes: 42 additions & 2 deletions server/services/transactionsTransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (transformer *transactionsTransformer) txToRosettaTx(tx *transaction.ApiTra

switch tx.Type {
case string(transaction.TxTypeNormal):
rosettaTx, err = transformer.normalTxToRosetta(tx)
rosettaTx, err = transformer.normalTxToRosetta(tx, txsInBlock)
if err != nil {
return nil, err
}
Expand All @@ -115,6 +115,15 @@ func (transformer *transactionsTransformer) unsignedTxToRosettaTx(
txsInBlock []*transaction.ApiTransactionResult,
) *types.Transaction {
if scr.IsRefund {
if scr.Sender == scr.Receiver && !transformer.extension.isUserAddress(scr.Sender) {
log.Info("unsignedTxToRosettaTx: dismissed refund", "hash", scr.Hash, "originalTxHash", scr.OriginalTransactionHash)

return &types.Transaction{
TransactionIdentifier: hashToTransactionIdentifier(scr.Hash),
Operations: []*types.Operation{},
}
}

return &types.Transaction{
TransactionIdentifier: hashToTransactionIdentifier(scr.Hash),
Operations: []*types.Operation{
Expand Down Expand Up @@ -171,7 +180,10 @@ func (transformer *transactionsTransformer) rewardTxToRosettaTx(tx *transaction.
}
}

func (transformer *transactionsTransformer) normalTxToRosetta(tx *transaction.ApiTransactionResult) (*types.Transaction, error) {
func (transformer *transactionsTransformer) normalTxToRosetta(
tx *transaction.ApiTransactionResult,
allTransactionsInBlock []*transaction.ApiTransactionResult,
) (*types.Transaction, error) {
hasValue := !isZeroAmount(tx.Value)
operations := make([]*types.Operation, 0)

Expand Down Expand Up @@ -200,7 +212,13 @@ func (transformer *transactionsTransformer) normalTxToRosetta(tx *transaction.Ap
return nil, err
}

valueRefundOperationIfContractCallWithSignalError, err := transformer.createValueReturnOperationsIfIntrashardContractCallWithSignalError(tx, allTransactionsInBlock)
if err != nil {
return nil, err
}

operations = append(operations, innerTxOperationsIfRelayedCompletelyIntrashardWithSignalError...)
operations = append(operations, valueRefundOperationIfContractCallWithSignalError...)

return &types.Transaction{
TransactionIdentifier: hashToTransactionIdentifier(tx.Hash),
Expand Down Expand Up @@ -239,6 +257,28 @@ func (transformer *transactionsTransformer) extractInnerTxOperationsIfRelayedCom
}, nil
}

func (transformer *transactionsTransformer) createValueReturnOperationsIfIntrashardContractCallWithSignalError(
tx *transaction.ApiTransactionResult,
allTransactionsInBlock []*transaction.ApiTransactionResult,
) ([]*types.Operation, error) {
if !transformer.featuresDetector.isIntrashardContractCallWithSignalErrorButWithoutContractResultBearingRefundValue(tx, allTransactionsInBlock) {
return []*types.Operation{}, nil
}

return []*types.Operation{
{
Type: opTransfer,
Account: addressToAccountIdentifier(tx.Sender),
Amount: transformer.extension.valueToNativeAmount(tx.Value),
},
{
Type: opTransfer,
Account: addressToAccountIdentifier(tx.Receiver),
Amount: transformer.extension.valueToNativeAmount("-" + tx.Value),
},
}, nil
}

func (transformer *transactionsTransformer) refundReceiptToRosettaTx(receipt *transaction.ApiReceipt) (*types.Transaction, error) {
receiptHash, err := transformer.provider.ComputeReceiptHash(receipt)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions server/services/transactionsTransformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestTransactionsTransformer_NormalTxToRosettaTx(t *testing.T) {
Metadata: extractTransactionMetadata(tx),
}

rosettaTx, err := transformer.normalTxToRosetta(tx)
rosettaTx, err := transformer.normalTxToRosetta(tx, nil)
require.NoError(t, err)
require.Equal(t, expectedRosettaTx, rosettaTx)
})
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestTransactionsTransformer_NormalTxToRosettaTx(t *testing.T) {
Metadata: extractTransactionMetadata(tx),
}

rosettaTx, err := transformer.normalTxToRosetta(tx)
rosettaTx, err := transformer.normalTxToRosetta(tx, nil)
require.NoError(t, err)
require.Equal(t, expectedRosettaTx, rosettaTx)
})
Expand Down

0 comments on commit 5db6aa4

Please sign in to comment.