Skip to content

Commit

Permalink
v2alpha1: support ineffectual transaction state (#6468)
Browse files Browse the repository at this point in the history
## Motivation

#6443 Introduced new transaction state `TRANSACTION_STATE_INEFFECTUAL` which was not supported by v2alpha1.

Closes #6466
  • Loading branch information
kacpersaw committed Nov 19, 2024
1 parent 86ac1f4 commit d203f45
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 10 deletions.
34 changes: 27 additions & 7 deletions api/grpcserver/v2alpha1/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
// transactionConState is an API to validate transaction.
type transactionConState interface {
Validation(raw types.RawTx) system.ValidationRequest
HasEvicted(tid types.TransactionID) (bool, error)
}

// transactionSyncer is an API to get sync status.
Expand Down Expand Up @@ -130,7 +131,7 @@ func (s *TransactionService) List(
if err := transactions.IterateTransactionsOps(s.db, ops, func(tx *types.MeshTransaction,
result *types.TransactionResult,
) bool {
rst = append(rst, toTx(tx, result, request.IncludeResult, request.IncludeState))
rst = append(rst, s.toTx(ctx, tx, result, request.IncludeResult, request.IncludeState))
return true
}); err != nil {
return nil, status.Error(codes.Internal, err.Error())
Expand Down Expand Up @@ -303,7 +304,10 @@ func toTransactionOperations(filter *spacemeshv2alpha1.TransactionRequest) (buil
return ops, nil
}

func toTx(tx *types.MeshTransaction, result *types.TransactionResult,
func (s *TransactionService) toTx(
ctx context.Context,
tx *types.MeshTransaction,
result *types.TransactionResult,
includeResult, includeState bool,
) *spacemeshv2alpha1.TransactionResponse {
rst := &spacemeshv2alpha1.TransactionResponse{}
Expand Down Expand Up @@ -332,7 +336,7 @@ func toTx(tx *types.MeshTransaction, result *types.TransactionResult,

if includeResult && result != nil {
rst.TxResult = &spacemeshv2alpha1.TransactionResult{
Status: convertTxResult(result),
Status: s.convertTxResult(result),
Message: result.Message,
GasConsumed: result.Gas,
Fee: result.Fee,
Expand All @@ -348,15 +352,17 @@ func toTx(tx *types.MeshTransaction, result *types.TransactionResult,
}

if includeState {
rst.TxState = convertTxState(tx)
rst.TxState = s.convertTxState(ctx, tx)
}

rst.Tx = t

return rst
}

func convertTxResult(result *types.TransactionResult) spacemeshv2alpha1.TransactionResult_Status {
func (s *TransactionService) convertTxResult(
result *types.TransactionResult,
) spacemeshv2alpha1.TransactionResult_Status {
switch result.Status {
case types.TransactionSuccess:
return spacemeshv2alpha1.TransactionResult_TRANSACTION_STATUS_SUCCESS
Expand All @@ -367,8 +373,9 @@ func convertTxResult(result *types.TransactionResult) spacemeshv2alpha1.Transact
}
}

// TODO: REJECTED, INSUFFICIENT_FUNDS, CONFLICTING, MESH.
func convertTxState(tx *types.MeshTransaction) *spacemeshv2alpha1.TransactionState {
func (s *TransactionService) convertTxState(
ctx context.Context, tx *types.MeshTransaction,
) *spacemeshv2alpha1.TransactionState {
switch tx.State {
case types.MEMPOOL:
state := spacemeshv2alpha1.TransactionState_TRANSACTION_STATE_MEMPOOL
Expand All @@ -377,6 +384,19 @@ func convertTxState(tx *types.MeshTransaction) *spacemeshv2alpha1.TransactionSta
state := spacemeshv2alpha1.TransactionState_TRANSACTION_STATE_PROCESSED
return &state
default:
evicted, err := s.conState.HasEvicted(tx.ID)
if err != nil {
ctxzap.Debug(ctx, "failed to check if tx is evicted",
zap.String("tx_id", tx.ID.String()),
zap.Error(err),
)
state := spacemeshv2alpha1.TransactionState_TRANSACTION_STATE_UNSPECIFIED
return &state
}
if evicted {
state := spacemeshv2alpha1.TransactionState_TRANSACTION_STATE_INEFFECTUAL
return &state
}
state := spacemeshv2alpha1.TransactionState_TRANSACTION_STATE_UNSPECIFIED
return &state
}
Expand Down
42 changes: 42 additions & 0 deletions api/grpcserver/v2alpha1/transaction_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions api/grpcserver/v2alpha1/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,3 +757,37 @@ func TestToTxContents(t *testing.T) {
require.Equal(t, spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_VESTING_SPAWN, txType)
})
}

func TestEvictedTransaction(t *testing.T) {
req := require.New(t)

db := statesql.InMemoryTest(t)
ctrl := gomock.NewController(t)
syncer := NewMocktransactionSyncer(ctrl)
publisher := pubsubmocks.NewMockPublisher(ctrl)
txHandler := NewMocktransactionValidator(ctrl)
conState := NewMocktransactionConState(ctrl)

signer, err := signing.NewEdSigner()
require.NoError(t, err)
tx := newTx(0, types.Address{}, signer)
require.NoError(t, transactions.Add(db, &types.Transaction{
RawTx: tx.RawTx,
TxHeader: nil,
}, time.Now()))

svc := NewTransactionService(db, conState, syncer, txHandler, publisher)
cfg, cleanup := launchServer(t, svc)
t.Cleanup(cleanup)

conn := dialGrpc(t, cfg)
client := spacemeshv2alpha1.NewTransactionServiceClient(conn)

conState.EXPECT().HasEvicted(gomock.Any()).Return(true, nil)
list, err := client.List(context.Background(), &spacemeshv2alpha1.TransactionRequest{Limit: 1, IncludeState: true})
req.NoError(err)
req.Len(list.Transactions, 1)
req.Equal(tx.ID.Bytes(), list.Transactions[0].Tx.Id)
req.Equal(list.Transactions[0].TxState.String(),
spacemeshv2alpha1.TransactionState_TRANSACTION_STATE_INEFFECTUAL.String())
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ require (
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/seehuhn/mt19937 v1.0.0
github.com/slok/go-http-metrics v0.13.0
github.com/spacemeshos/api/release/go v1.56.0
github.com/spacemeshos/api/release/go v1.57.0
github.com/spacemeshos/economics v0.1.4
github.com/spacemeshos/fixed v0.1.2
github.com/spacemeshos/go-scale v1.2.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,8 @@ github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:Udh
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA=
github.com/spacemeshos/api/release/go v1.56.0 h1:llBVijoO4I3mhHk0OtGJdTT/11I7ajo0CZp3x8h1EjA=
github.com/spacemeshos/api/release/go v1.56.0/go.mod h1:6o17nhNyXpbVeijAQqkZfL8Pe/IkMGAWMLSLZni0DOU=
github.com/spacemeshos/api/release/go v1.57.0 h1:KE4zNmuI12q7MDcy2cDwQLSVWKpY3gXoDgX02URFzBk=
github.com/spacemeshos/api/release/go v1.57.0/go.mod h1:w/WRxR8JVmaeKqEEyL6DCoQxt3oyZOTv4uQSdMXlucM=
github.com/spacemeshos/economics v0.1.4 h1:twlawrcQhYNqPgyDv08+24EL/OgUKz3d7q+PvJIAND0=
github.com/spacemeshos/economics v0.1.4/go.mod h1:6HKWKiKdxjVQcGa2z/wA0LR4M/DzKib856bP16yqNmQ=
github.com/spacemeshos/fixed v0.1.2 h1:pENQ8pXFAqin3f15ZLoOVVeSgcmcFJ0IFdFm4+9u4SM=
Expand Down

0 comments on commit d203f45

Please sign in to comment.