diff --git a/codec/antelope/hydrator.go b/codec/antelope/hydrator.go index e65b316..4ec7c10 100644 --- a/codec/antelope/hydrator.go +++ b/codec/antelope/hydrator.go @@ -5,7 +5,7 @@ import "github.com/pinax-network/firehose-antelope/types/pb/sf/antelope/type/v1" type Hydrator interface { // HydrateBlock decodes the received Deep Mind AcceptedBlock data structure against the // correct struct for this version of EOSIO supported by this hydrator. - HydrateBlock(block *pbantelope.Block, input []byte) error + HydrateBlock(block *pbantelope.Block, input []byte, version string) error // DecodeTransactionTrace decodes the received Deep Mind AppliedTransaction data structure against the // correct struct for this version of EOSIO supported by this hydrator. diff --git a/codec/antelope/leap_v5/hydrator.go b/codec/antelope/leap_v5/hydrator.go index 57741bd..b529cf8 100644 --- a/codec/antelope/leap_v5/hydrator.go +++ b/codec/antelope/leap_v5/hydrator.go @@ -20,8 +20,12 @@ type Hydrator struct { logger *zap.Logger } -func (h *Hydrator) HydrateBlock(block *pbantelope.Block, input []byte) error { - h.logger.Debug("hydrating block from bytes") +func (h *Hydrator) HydrateBlock(block *pbantelope.Block, input []byte, version string) error { + h.logger.Debug("hydrating block from bytes", zap.String("version", version)) + + if version != "v1" { + return fmt.Errorf("unsupported version: %s", version) + } blockState := &BlockState{} err := unmarshalBinary(input, blockState) diff --git a/codec/antelope/spring_v1/hydrator.go b/codec/antelope/spring_v1/hydrator.go index 8ea33c4..6c1332e 100644 --- a/codec/antelope/spring_v1/hydrator.go +++ b/codec/antelope/spring_v1/hydrator.go @@ -19,68 +19,106 @@ type Hydrator struct { logger *zap.Logger } -func (h *Hydrator) HydrateBlock(block *pbantelope.Block, input []byte) error { - h.logger.Debug("hydrating block from bytes") +func (h *Hydrator) HydrateBlock(block *pbantelope.Block, input []byte, version string) error { + h.logger.Debug("hydrating block from bytes", zap.String("version", version)) + + if version == "v1" { + blockState := &LegacyBlockState{} + err := unmarshalBinary(input, blockState) + if err != nil { + return fmt.Errorf("unmarshalling binary block state (spring v1): %w", err) + } - blockState := &BlockState{} - err := unmarshalBinary(input, blockState) - if err != nil { - return fmt.Errorf("unmarshalling binary block state (spring v1): %w", err) - } + signedBlock := blockState.SignedBlock + + block.Id = blockState.BlockID.String() + block.Number = blockState.BlockNum + // Version 1: Added the total counts (ExecutedInputActionCount, ExecutedTotalActionCount, + // TransactionCount, TransactionTraceCount) + block.Version = 1 + block.Header = antelope.BlockHeaderToDEOS(&signedBlock.BlockHeader) + block.BlockExtensions = antelope.ExtensionsToDEOS(signedBlock.BlockExtensions) + block.DposIrreversibleBlocknum = blockState.DPoSIrreversibleBlockNum + block.DposProposedIrreversibleBlocknum = blockState.DPoSProposedIrreversibleBlockNum + block.BlockrootMerkle = antelope.BlockrootMerkleToDEOS(blockState.BlockrootMerkle) + block.ProducerToLastProduced = antelope.ProducerToLastProducedToDEOS(blockState.ProducerToLastProduced) + block.ProducerToLastImpliedIrb = antelope.ProducerToLastImpliedIrbToDEOS(blockState.ProducerToLastImpliedIRB) + block.ActivatedProtocolFeatures = antelope.ActivatedProtocolFeaturesToDEOS(blockState.ActivatedProtocolFeatures) + block.ProducerSignature = signedBlock.ProducerSignature.String() + block.Validated = blockState.Validated + block.ActionMrootSavanna = blockState.ActionMrootSavanna + + block.ConfirmCount = make([]uint32, len(blockState.ConfirmCount)) + for i, count := range blockState.ConfirmCount { + block.ConfirmCount[i] = uint32(count) + } - signedBlock := blockState.SignedBlock - - block.Id = blockState.BlockID.String() - block.Number = blockState.BlockNum - // Version 1: Added the total counts (ExecutedInputActionCount, ExecutedTotalActionCount, - // TransactionCount, TransactionTraceCount) - block.Version = 1 - block.Header = antelope.BlockHeaderToDEOS(&signedBlock.BlockHeader) - block.BlockExtensions = antelope.ExtensionsToDEOS(signedBlock.BlockExtensions) - block.DposIrreversibleBlocknum = blockState.DPoSIrreversibleBlockNum - block.DposProposedIrreversibleBlocknum = blockState.DPoSProposedIrreversibleBlockNum - // block.Validated = blockState.Validated - block.BlockrootMerkle = antelope.BlockrootMerkleToDEOS(blockState.BlockrootMerkle) - block.ProducerToLastProduced = antelope.ProducerToLastProducedToDEOS(blockState.ProducerToLastProduced) - block.ProducerToLastImpliedIrb = antelope.ProducerToLastImpliedIrbToDEOS(blockState.ProducerToLastImpliedIRB) - block.ActivatedProtocolFeatures = antelope.ActivatedProtocolFeaturesToDEOS(blockState.ActivatedProtocolFeatures) - block.ProducerSignature = signedBlock.ProducerSignature.String() - block.Validated = blockState.Validated - block.ActionMrootSavanna = blockState.ActionMrootSavanna - - block.ConfirmCount = make([]uint32, len(blockState.ConfirmCount)) - for i, count := range blockState.ConfirmCount { - block.ConfirmCount[i] = uint32(count) - } + if blockState.PendingSchedule != nil { + block.PendingSchedule = antelope.PendingScheduleToDEOS(blockState.PendingSchedule) + } - if blockState.PendingSchedule != nil { - block.PendingSchedule = antelope.PendingScheduleToDEOS(blockState.PendingSchedule) - } + block.ValidBlockSigningAuthorityV2 = antelope.BlockSigningAuthorityToDEOS(blockState.ValidBlockSigningAuthorityV2) + block.ActiveScheduleV2 = antelope.ProducerAuthorityScheduleToDEOS(blockState.ActiveSchedule) - block.ValidBlockSigningAuthorityV2 = antelope.BlockSigningAuthorityToDEOS(blockState.ValidBlockSigningAuthorityV2) - block.ActiveScheduleV2 = antelope.ProducerAuthorityScheduleToDEOS(blockState.ActiveSchedule) + block.UnfilteredTransactionCount = uint32(len(signedBlock.Transactions)) + for idx, transaction := range signedBlock.Transactions { + deosTransaction := TransactionReceiptToDEOS(transaction) + deosTransaction.Index = uint64(idx) - block.UnfilteredTransactionCount = uint32(len(signedBlock.Transactions)) - for idx, transaction := range signedBlock.Transactions { - deosTransaction := TransactionReceiptToDEOS(transaction) - deosTransaction.Index = uint64(idx) + block.UnfilteredTransactions = append(block.UnfilteredTransactions, deosTransaction) + } - block.UnfilteredTransactions = append(block.UnfilteredTransactions, deosTransaction) - } + block.UnfilteredTransactionTraceCount = uint32(len(block.UnfilteredTransactionTraces)) + for idx, t := range block.UnfilteredTransactionTraces { + t.Index = uint64(idx) + t.BlockTime = block.Header.Timestamp + t.ProducerBlockId = block.Id + t.BlockNum = uint64(block.Number) + + for _, actionTrace := range t.ActionTraces { + block.UnfilteredExecutedTotalActionCount++ + if actionTrace.IsInput() { + block.UnfilteredExecutedInputActionCount++ + } + } + } + } else if version == "v2" { + signedBlock := &SignedBlock{} + + err := unmarshalBinary(input, signedBlock) + if err != nil { + return fmt.Errorf("unmarshalling signed block (spring v2): %w", err) + } + + block.Version = 2 + block.Header = antelope.BlockHeaderToDEOS(&signedBlock.BlockHeader) + block.BlockExtensions = antelope.ExtensionsToDEOS(signedBlock.BlockExtensions) + block.ProducerSignature = signedBlock.ProducerSignature.String() + + block.UnfilteredTransactionCount = uint32(len(signedBlock.Transactions)) + for idx, transaction := range signedBlock.Transactions { + deosTransaction := TransactionReceiptToDEOS(transaction) + deosTransaction.Index = uint64(idx) + + block.UnfilteredTransactions = append(block.UnfilteredTransactions, deosTransaction) + } - block.UnfilteredTransactionTraceCount = uint32(len(block.UnfilteredTransactionTraces)) - for idx, t := range block.UnfilteredTransactionTraces { - t.Index = uint64(idx) - t.BlockTime = block.Header.Timestamp - t.ProducerBlockId = block.Id - t.BlockNum = uint64(block.Number) - - for _, actionTrace := range t.ActionTraces { - block.UnfilteredExecutedTotalActionCount++ - if actionTrace.IsInput() { - block.UnfilteredExecutedInputActionCount++ + block.UnfilteredTransactionTraceCount = uint32(len(block.UnfilteredTransactionTraces)) + for idx, t := range block.UnfilteredTransactionTraces { + t.Index = uint64(idx) + t.BlockTime = block.Header.Timestamp + t.ProducerBlockId = block.Id + t.BlockNum = uint64(block.Number) + + for _, actionTrace := range t.ActionTraces { + block.UnfilteredExecutedTotalActionCount++ + if actionTrace.IsInput() { + block.UnfilteredExecutedInputActionCount++ + } } } + } else { + return fmt.Errorf("unsupported version: %s", version) } return nil diff --git a/codec/antelope/spring_v1/types.go b/codec/antelope/spring_v1/types.go index 0c5c280..fa4cec9 100644 --- a/codec/antelope/spring_v1/types.go +++ b/codec/antelope/spring_v1/types.go @@ -5,13 +5,12 @@ import ( "github.com/eoscanada/eos-go/ecc" ) -// BlockState +// LegacyBlockState // // File hierarchy: -// - https://github.com/EOSIO/eos/blob/v2.1.0/libraries/chain/include/eosio/chain/block_header_state.hpp#L57 -// - https://github.com/EOSIO/eos/blob/v2.1.0/libraries/chain/include/eosio/chain/block_header_state.hpp#L126 -// - https://github.com/EOSIO/eos/blob/v2.1.0/libraries/chain/include/eosio/chain/block_state.hpp#L10 -type BlockState struct { +// - https://github.com/AntelopeIO/spring/blob/main/libraries/chain/include/eosio/chain/block_header_state_legacy.hpp +// - https://github.com/AntelopeIO/spring/blob/main/libraries/chain/include/eosio/chain/block_state_legacy.hpp +type LegacyBlockState struct { // From 'struct block_header_state_legacy_common' BlockNum uint32 `json:"block_num"` @@ -72,7 +71,7 @@ type FinalizerAuthority struct { // TransactionTrace // // File hierarchy: -// - https://github.com/EOSIO/eos/blob/v2.1.0/libraries/chain/include/eosio/chain/trace.hpp#L51 +// - https://github.com/AntelopeIO/spring/blob/main/libraries/chain/include/eosio/chain/trace.hpp type TransactionTrace struct { ID eos.Checksum256 `json:"id"` BlockNum uint32 `json:"block_num"` @@ -89,10 +88,10 @@ type TransactionTrace struct { ErrorCode *eos.Uint64 `json:"error_code,omitempty" eos:"optional"` } -// TransactionTrace +// ActionTrace // // File hierarchy: -// - https://github.com/EOSIO/eos/blob/v2.1.0/libraries/chain/include/eosio/chain/trace.hpp#L22 +// - https://github.com/AntelopeIO/spring/blob/main/libraries/chain/include/eosio/chain/trace.hpp type ActionTrace struct { ActionOrdinal eos.Varuint32 `json:"action_ordinal"` CreatorActionOrdinal eos.Varuint32 `json:"creator_action_ordinal"` diff --git a/codec/consolereader.go b/codec/consolereader.go index 86ca449..c13dec5 100644 --- a/codec/consolereader.go +++ b/codec/consolereader.go @@ -673,7 +673,7 @@ func (ctx *parseCtx) readAcceptedBlock(line string) (*pbantelope.Block, error) { return nil, fmt.Errorf("unable to decode block %d state hex: %w", blockNum, err) } - if err := ctx.hydrator.HydrateBlock(ctx.currentBlock, blockStateHex); err != nil { + if err := ctx.hydrator.HydrateBlock(ctx.currentBlock, blockStateHex, "v1"); err != nil { return nil, fmt.Errorf("hydrate block %d: %w", blockNum, err) } @@ -722,7 +722,7 @@ func (ctx *parseCtx) readAcceptedBlockV2(line string) (*pbantelope.Block, error) return nil, fmt.Errorf("unable to decode block %d state hex: %w", blockNum, err) } - if err := ctx.hydrator.HydrateBlock(ctx.currentBlock, blockStateHex); err != nil { + if err := ctx.hydrator.HydrateBlock(ctx.currentBlock, blockStateHex, "v2"); err != nil { return nil, fmt.Errorf("hydrate block %d: %w", blockNum, err) } block := ctx.currentBlock