Skip to content

Commit

Permalink
fix handling accept_block_v2
Browse files Browse the repository at this point in the history
  • Loading branch information
fschoell committed Jun 10, 2024
1 parent b88fea0 commit 042914c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 66 deletions.
2 changes: 1 addition & 1 deletion codec/antelope/hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions codec/antelope/leap_v5/hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
144 changes: 91 additions & 53 deletions codec/antelope/spring_v1/hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions codec/antelope/spring_v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down
4 changes: 2 additions & 2 deletions codec/consolereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 042914c

Please sign in to comment.