From 2f4d80892cb928114bb7ef78a89d70fb4af30436 Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Fri, 3 May 2024 12:57:38 +0200 Subject: [PATCH] fix: FinalizeBlock --- client_online.go | 35 ++++++----------------------------- converter.go | 28 ++++++++-------------------- converter_test.go | 14 ++------------ types.go | 10 ++++------ 4 files changed, 20 insertions(+), 67 deletions(-) diff --git a/client_online.go b/client_online.go index 4028b0f..5ab237d 100644 --- a/client_online.go +++ b/client_online.go @@ -306,20 +306,6 @@ func (c *Client) GetTx(ctx context.Context, hash string) (*rosettatypes.Transact // construct rosetta tx switch txType { // handle begin block hash - case BeginBlockTx: - // get block height by hash - block, err := c.tmRPC.BlockByHash(ctx, hashBytes) - if err != nil { - return nil, crgerrs.WrapError(crgerrs.ErrOnlineClient, fmt.Sprintf("getting block by hash %s", err.Error())) - } - - // get block txs - fullBlock, err := c.blockTxs(ctx, &block.Block.Height) - if err != nil { - return nil, crgerrs.WrapError(crgerrs.ErrOnlineClient, fmt.Sprintf("getting block tx %s", err.Error())) - } - - return fullBlock.Transactions[0], nil // handle deliver tx hash case DeliverTxTx: rawTx, err := c.tmRPC.Tx(ctx, hashBytes, true) @@ -328,7 +314,7 @@ func (c *Client) GetTx(ctx context.Context, hash string) (*rosettatypes.Transact } return c.converter.ToRosetta().Tx(rawTx.Tx, &rawTx.TxResult) // handle end block hash - case EndBlockTx: + case FinalizeBlockTx: // get block height by hash block, err := c.tmRPC.BlockByHash(ctx, hashBytes) if err != nil { @@ -365,7 +351,7 @@ func (c *Client) GetUnconfirmedTx(ctx context.Context, hash string) (*rosettatyp switch len(hashAsBytes) { default: return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, fmt.Sprintf("unrecognized tx size: %d", len(hashAsBytes))) - case BeginEndBlockTxSize: + case FinalizeBlockTxSize: return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, "endblock and begin block txs cannot be unconfirmed") case DeliverTxSize: break @@ -510,16 +496,8 @@ func (c *Client) blockTxs(ctx context.Context, height *int64) (crgtypes.BlockTra return crgtypes.BlockTransactionsResponse{}, crgerrs.WrapError(crgerrs.ErrOnlineClient, "block results transactions do now match block transactions") } // process begin and end block txs - beginBlockTx := &rosettatypes.Transaction{ - TransactionIdentifier: &rosettatypes.TransactionIdentifier{Hash: c.converter.ToRosetta().BeginBlockTxHash(blockInfo.BlockID.Hash)}, - Operations: AddOperationIndexes( - nil, - c.converter.ToRosetta().BalanceOps(StatusTxSuccess, blockResults.FinalizeBlockEvents), - ), - } - - endBlockTx := &rosettatypes.Transaction{ - TransactionIdentifier: &rosettatypes.TransactionIdentifier{Hash: c.converter.ToRosetta().EndBlockTxHash(blockInfo.BlockID.Hash)}, + FinalizeBlockTx := &rosettatypes.Transaction{ + TransactionIdentifier: &rosettatypes.TransactionIdentifier{Hash: c.converter.ToRosetta().FinalizeBlockTxHash(blockInfo.BlockID.Hash)}, Operations: AddOperationIndexes( nil, c.converter.ToRosetta().BalanceOps(StatusTxSuccess, blockResults.FinalizeBlockEvents), @@ -536,10 +514,9 @@ func (c *Client) blockTxs(ctx context.Context, height *int64) (crgtypes.BlockTra deliverTx[i] = rosTx } - finalTxs := make([]*rosettatypes.Transaction, 0, 2+len(deliverTx)) - finalTxs = append(finalTxs, beginBlockTx) + finalTxs := make([]*rosettatypes.Transaction, 0, 1+len(deliverTx)) finalTxs = append(finalTxs, deliverTx...) - finalTxs = append(finalTxs, endBlockTx) + finalTxs = append(finalTxs, FinalizeBlockTx) return crgtypes.BlockTransactionsResponse{ BlockResponse: c.converter.ToRosetta().BlockResponse(blockInfo), diff --git a/converter.go b/converter.go index b35dd67..4456c66 100644 --- a/converter.go +++ b/converter.go @@ -54,9 +54,7 @@ type ToRosettaConverter interface { // BlockResponse returns a block response given a result block BlockResponse(block *tmcoretypes.ResultBlock) crgtypes.BlockResponse // BeginBlockToTx converts the given begin block hash to rosetta transaction hash - BeginBlockTxHash(blockHash []byte) string - // EndBlockTxHash converts the given endblock hash to rosetta transaction hash - EndBlockTxHash(blockHash []byte) string + FinalizeBlockTxHash(blockHash []byte) string // Amounts converts sdk.Coins to rosetta.Amounts Amounts(ownedCoins []sdk.Coin, availableCoins sdk.Coins) []*rosettatypes.Amount // Ops converts an sdk.Msg to rosetta operations @@ -474,35 +472,25 @@ func AddOperationIndexes(msgOps, balanceOps []*rosettatypes.Operation) (finalOps return finalOps } -// EndBlockTxHash produces a mock endblock hash that rosetta can query -// for endblock operations, it also serves the purpose of representing -// part of the state changes happening at endblock level (balance ones) -func (c converter) EndBlockTxHash(hash []byte) string { - final := append([]byte{EndBlockHashStart}, hash...) - return fmt.Sprintf("%X", final) -} - -// BeginBlockTxHash produces a mock beginblock hash that rosetta can query +// FinalizeBlockTxHash produces a mock beginblock hash that rosetta can query // for beginblock operations, it also serves the purpose of representing // part of the state changes happening at beginblock level (balance ones) -func (c converter) BeginBlockTxHash(hash []byte) string { - final := append([]byte{BeginBlockHashStart}, hash...) +func (c converter) FinalizeBlockTxHash(hash []byte) string { + final := append([]byte{FinalizeBlockHashStart}, hash...) return fmt.Sprintf("%X", final) } // HashToTxType takes the provided hash bytes from rosetta and discerns if they are -// a deliver tx type or endblock/begin block hash, returning the real hash afterwards +// a deliver tx type or finalize block hash, returning the real hash afterward func (c converter) HashToTxType(hashBytes []byte) (txType TransactionType, realHash []byte) { switch len(hashBytes) { case DeliverTxSize: return DeliverTxTx, hashBytes - case BeginEndBlockTxSize: + case FinalizeBlockTxSize: switch hashBytes[0] { - case BeginBlockHashStart: - return BeginBlockTx, hashBytes[1:] - case EndBlockHashStart: - return EndBlockTx, hashBytes[1:] + case FinalizeBlockHashStart: + return FinalizeBlockHashStart, hashBytes[1:] default: return UnrecognizedTx, nil } diff --git a/converter_test.go b/converter_test.go index 057192c..8ef1655 100644 --- a/converter_test.go +++ b/converter_test.go @@ -185,8 +185,7 @@ func (s *ConverterTestSuite) TestBeginEndBlockAndHashToTxType() { deliverTxBytes, err := hex.DecodeString(deliverTxHex) s.Require().NoError(err) - endBlockTxHex := s.c.ToRosetta().EndBlockTxHash(deliverTxBytes) - beginBlockTxHex := s.c.ToRosetta().BeginBlockTxHash(deliverTxBytes) + endBlockTxHex := s.c.ToRosetta().FinalizeBlockTxHash(deliverTxBytes) txType, hash := s.c.ToSDK().HashToTxType(deliverTxBytes) @@ -197,18 +196,9 @@ func (s *ConverterTestSuite) TestBeginEndBlockAndHashToTxType() { s.Require().NoError(err) txType, hash = s.c.ToSDK().HashToTxType(endBlockTxBytes) - - s.Require().Equal(rosetta.EndBlockTx, txType) + s.Require().Equal(rosetta.FinalizeBlockTx, txType) s.Require().Equal(deliverTxBytes, hash, "end block tx hash should be equal to a block hash") - beginBlockTxBytes, err := hex.DecodeString(beginBlockTxHex) - s.Require().NoError(err) - - txType, hash = s.c.ToSDK().HashToTxType(beginBlockTxBytes) - - s.Require().Equal(rosetta.BeginBlockTx, txType) - s.Require().Equal(deliverTxBytes, hash, "begin block tx hash should be equal to a block hash") - txType, hash = s.c.ToSDK().HashToTxType([]byte("invalid")) s.Require().Equal(rosetta.UnrecognizedTx, txType) diff --git a/types.go b/types.go index fbc7f13..dbff44a 100644 --- a/types.go +++ b/types.go @@ -17,10 +17,9 @@ const ( // which are not represented as transactions we mock only the balance changes // happening at those levels as transactions. (check BeginBlockTxHash for more info) const ( - DeliverTxSize = sha256.Size - BeginEndBlockTxSize = DeliverTxSize + 1 - EndBlockHashStart = 0x0 - BeginBlockHashStart = 0x1 + DeliverTxSize = sha256.Size + FinalizeBlockTxSize = DeliverTxSize + 1 + FinalizeBlockHashStart = 0x1 ) const ( @@ -37,8 +36,7 @@ type TransactionType int const ( UnrecognizedTx TransactionType = iota - BeginBlockTx - EndBlockTx + FinalizeBlockTx DeliverTxTx )