From 2f4d80892cb928114bb7ef78a89d70fb4af30436 Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Fri, 3 May 2024 12:57:38 +0200 Subject: [PATCH 1/4] 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 ) From acaf83cebf67214eb4e49ec10e5933881fdd2807 Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Fri, 3 May 2024 13:08:08 +0200 Subject: [PATCH 2/4] fix --- client_online.go | 4 ++-- converter.go | 4 ++-- converter_test.go | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/client_online.go b/client_online.go index 5ab237d..11c1b08 100644 --- a/client_online.go +++ b/client_online.go @@ -496,7 +496,7 @@ 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 - FinalizeBlockTx := &rosettatypes.Transaction{ + finalizeBlockTx := &rosettatypes.Transaction{ TransactionIdentifier: &rosettatypes.TransactionIdentifier{Hash: c.converter.ToRosetta().FinalizeBlockTxHash(blockInfo.BlockID.Hash)}, Operations: AddOperationIndexes( nil, @@ -516,7 +516,7 @@ func (c *Client) blockTxs(ctx context.Context, height *int64) (crgtypes.BlockTra finalTxs := make([]*rosettatypes.Transaction, 0, 1+len(deliverTx)) finalTxs = append(finalTxs, deliverTx...) - finalTxs = append(finalTxs, FinalizeBlockTx) + finalTxs = append(finalTxs, finalizeBlockTx) return crgtypes.BlockTransactionsResponse{ BlockResponse: c.converter.ToRosetta().BlockResponse(blockInfo), diff --git a/converter.go b/converter.go index 4456c66..a10628b 100644 --- a/converter.go +++ b/converter.go @@ -473,8 +473,8 @@ func AddOperationIndexes(msgOps, balanceOps []*rosettatypes.Operation) (finalOps } // 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) +// for finalizeBlock operations, it also serves the purpose of representing +// part of the state changes happening at finalizeblock level (balance ones) func (c converter) FinalizeBlockTxHash(hash []byte) string { final := append([]byte{FinalizeBlockHashStart}, hash...) return fmt.Sprintf("%X", final) diff --git a/converter_test.go b/converter_test.go index 8ef1655..2594b8c 100644 --- a/converter_test.go +++ b/converter_test.go @@ -179,23 +179,23 @@ func (s *ConverterTestSuite) TestOpsAndSigners() { }) } -func (s *ConverterTestSuite) TestBeginEndBlockAndHashToTxType() { +func (s *ConverterTestSuite) TestFinalizeBlockHashToTxType() { const deliverTxHex = "5229A67AA008B5C5F1A0AEA77D4DEBE146297A30AAEF01777AF10FAD62DD36AB" deliverTxBytes, err := hex.DecodeString(deliverTxHex) s.Require().NoError(err) - endBlockTxHex := s.c.ToRosetta().FinalizeBlockTxHash(deliverTxBytes) + finalizeBlockTxHex := s.c.ToRosetta().FinalizeBlockTxHash(deliverTxBytes) txType, hash := s.c.ToSDK().HashToTxType(deliverTxBytes) s.Require().Equal(rosetta.DeliverTxTx, txType) s.Require().Equal(deliverTxBytes, hash, "deliver tx hash should not change") - endBlockTxBytes, err := hex.DecodeString(endBlockTxHex) + finalizeBlockTxBytes, err := hex.DecodeString(finalizeBlockTxHex) s.Require().NoError(err) - txType, hash = s.c.ToSDK().HashToTxType(endBlockTxBytes) + txType, hash = s.c.ToSDK().HashToTxType(finalizeBlockTxBytes) s.Require().Equal(rosetta.FinalizeBlockTx, txType) s.Require().Equal(deliverTxBytes, hash, "end block tx hash should be equal to a block hash") From a0b323828edb5c04c8e6305a64e9c4b76c615eae Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Fri, 3 May 2024 13:11:13 +0200 Subject: [PATCH 3/4] CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97f5d77..8960243 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +### Bug Fixes + +* [123](https://github.com/cosmos/rosetta/pull/123) Correctly parse cometBFT finalize block. + ## [v0.50.6](https://github.com/cosmos/rosetta/releases/tag/v0.50.6) 2024-04-23 ### Improvements From 2a7bdbe41a0b59639e23f180263451a310715e0f Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Fri, 3 May 2024 13:46:16 +0200 Subject: [PATCH 4/4] fix --- client_online.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client_online.go b/client_online.go index 11c1b08..e1a82c3 100644 --- a/client_online.go +++ b/client_online.go @@ -352,7 +352,7 @@ func (c *Client) GetUnconfirmedTx(ctx context.Context, hash string) (*rosettatyp default: return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, fmt.Sprintf("unrecognized tx size: %d", len(hashAsBytes))) case FinalizeBlockTxSize: - return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, "endblock and begin block txs cannot be unconfirmed") + return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, "finalize block txs cannot be unconfirmed") case DeliverTxSize: break }