Skip to content

Commit

Permalink
include new accepted_block_v2 fields (#148)
Browse files Browse the repository at this point in the history
* add proposer_policy, cleanup

* add finalizer policy

* parse fields in the console reader

* include changes to finality_data_t

* remove decoded header extensions from tests
  • Loading branch information
fschoell authored Aug 1, 2024
1 parent 7a43eb2 commit 534ca5b
Show file tree
Hide file tree
Showing 13 changed files with 2,178 additions and 2,397 deletions.
61 changes: 0 additions & 61 deletions codec/antelope/eos_to_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"
)

type BlockExtensionId uint16

const (
AdditionalBlockSignatureExtensionsId BlockExtensionId = 2
QuorumCertificateExtensionId BlockExtensionId = 3
)

func ActivatedProtocolFeaturesToDEOS(in *eos.ProtocolFeatureActivationSet) *pbantelope.ActivatedProtocolFeatures {
out := &pbantelope.ActivatedProtocolFeatures{}
out.ProtocolFeatures = checksumsToBytesSlices(in.ProtocolFeatures)
Expand Down Expand Up @@ -163,60 +156,6 @@ func ExtensionsToDEOS(in []*eos.Extension) (out []*pbantelope.Extension) {
return
}

func BlockHeaderExtensionsToDEOS(in []*eos.Extension) ([]*pbantelope.BlockHeaderExtension, error) {

res := make([]*pbantelope.BlockHeaderExtension, 0, len(in))
for _, extension := range in {

ext, err := extension.AsBlockHeaderExtension("EOS")
if err != nil {
return nil, fmt.Errorf("unable to convert to block header extension: %w", err)
}

switch ext.TypeID() {

case eos.EOS_ProtocolFeatureActivation:
pfaExtension := ext.(*eos.ProtocolFeatureActivationExtension)
res = append(res, &pbantelope.BlockHeaderExtension{
Extension: &pbantelope.BlockHeaderExtension_ProtocolFeatureActivationExtension{
ProtocolFeatureActivationExtension: &pbantelope.ProtocolFeatureActivationExtension{
ProtocolFeatures: checksumsToBytesSlices(pfaExtension.FeatureDigests),
},
},
})

case eos.EOS_ProducerScheduleChangeExtension:
pscExtension := ext.(*eos.ProducerScheduleChangeExtension)
res = append(res, &pbantelope.BlockHeaderExtension{
Extension: &pbantelope.BlockHeaderExtension_ProducerScheduleChangeExtension{
ProducerScheduleChangeExtension: &pbantelope.ProducerScheduleChangeExtension{
ProducerSchedule: ProducerAuthorityScheduleToDEOS(&eos.ProducerAuthoritySchedule{
Version: pscExtension.Version,
Producers: pscExtension.Producers,
}),
},
},
})

default:
return nil, fmt.Errorf("unknown extension type: %v", extension.Type)
}
}

return res, nil
}

//func QuorumCertificateToDEOS(qc eos.QuorumCertificate) *pbantelope.QuorumCertificate {
// return &pbantelope.QuorumCertificate{
// BlockNum: qc.BlockNum,
// Data: &pbantelope.ValidQuorumCertificate{
// StrongVotes: qc.ValidQuorumCertificate.StrongVotes,
// WeakVotes: qc.ValidQuorumCertificate.WeakVotes,
// BlsAggregateSignature: qc.ValidQuorumCertificate.BlsAggregateSignature.String(),
// },
// }
//}

func ProducerAuthoritiesToDEOS(producerAuthorities []*eos.ProducerAuthority) (out []*pbantelope.ProducerAuthority) {
if len(producerAuthorities) <= 0 {
return nil
Expand Down
4 changes: 4 additions & 0 deletions codec/antelope/hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ type Hydrator interface {
DecodeTransactionTrace(input []byte, opts ...ConversionOption) (*pbantelope.TransactionTrace, error)

DecodeFinalityData(input []byte) (*pbantelope.FinalityData, error)

DecodeProposerPolicy(input []byte) (*pbantelope.ProposerPolicy, error)

DecodeFinalizerPolicy(input []byte) (*pbantelope.FinalizerPolicy, error)
}
32 changes: 0 additions & 32 deletions codec/antelope/leap_v5/eos_to_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,35 +222,3 @@ func SignaturesToDEOS(in []ecc.Signature) (out []string) {
}
return
}

func FinalityDataToDEOS(in *FinalityData) *pbantelope.FinalityData {

res := &pbantelope.FinalityData{
MajorVersion: in.MajorVersion,
MinorVersion: in.MinorVersion,
ActiveFinalizerPolicyGeneration: in.ActiveFinalizerPolicyGeneration,
FinalOnStrongQcBlockNum: in.FinalOnStrongQCBlockNum,
ActionMroot: in.ActionMroot,
BaseDigest: in.BaseDigest,
}

if in.ProposedFinalizerPolicy != nil {
finalizerPolicy := &pbantelope.FinalizerPolicy{
Generation: in.ProposedFinalizerPolicy.Generation,
Threshold: in.ProposedFinalizerPolicy.Threshold,
Finalizers: nil,
}

finalizers := make([]*pbantelope.FinalizerAuthority, 0, len(finalizerPolicy.Finalizers))
for _, finalizer := range finalizerPolicy.Finalizers {
finalizers = append(finalizers, &pbantelope.FinalizerAuthority{
Description: finalizer.Description,
Weight: finalizer.Weight,
PublicKey: finalizer.PublicKey,
})
}
finalizerPolicy.Finalizers = finalizers
}

return res
}
12 changes: 10 additions & 2 deletions codec/antelope/leap_v5/hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,16 @@ func (h *Hydrator) DecodeTransactionTrace(input []byte, opts ...antelope.Convers
return TransactionTraceToDEOS(h.logger, trxTrace, opts...), nil
}

func (h *Hydrator) DecodeFinalityData(input []byte) (*pbantelope.FinalityData, error) {
return nil, errors.New("finality not supported in pre spring versions")
func (h *Hydrator) DecodeFinalityData(_ []byte) (*pbantelope.FinalityData, error) {
return nil, errors.New("finality not supported in pre-spring versions")
}

func (h *Hydrator) DecodeProposerPolicy(_ []byte) (*pbantelope.ProposerPolicy, error) {
return nil, errors.New("proposer policy not supported in pre-spring versions")
}

func (h *Hydrator) DecodeFinalizerPolicy(_ []byte) (*pbantelope.FinalizerPolicy, error) {
return nil, errors.New("finalizer policy not supported in pre-spring versions")
}

func unmarshalBinary(data []byte, v interface{}) error {
Expand Down
59 changes: 38 additions & 21 deletions codec/antelope/spring_v1/eos_to_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,31 +226,48 @@ func SignaturesToDEOS(in []ecc.Signature) (out []string) {
func FinalityDataToDEOS(in *FinalityData) *pbantelope.FinalityData {

res := &pbantelope.FinalityData{
MajorVersion: in.MajorVersion,
MinorVersion: in.MinorVersion,
ActiveFinalizerPolicyGeneration: in.ActiveFinalizerPolicyGeneration,
FinalOnStrongQcBlockNum: in.FinalOnStrongQCBlockNum,
ActionMroot: in.ActionMroot,
BaseDigest: in.BaseDigest,
MajorVersion: in.MajorVersion,
MinorVersion: in.MinorVersion,
ActiveFinalizerPolicyGeneration: in.ActiveFinalizerPolicyGeneration,
ActionMroot: in.ActionMroot,
ReversibleBlocksMroot: in.ReversibleBlocksMroot,
LatestQcClaimBlockNum: in.LatestQCClaimBlockNum,
LatestQcClaimFinalityDigest: in.LatestQCClaimFinalityDigest,
LatestQcClaimTimestamp: timestamppb.New(in.LatestQCClaimTimestamp.Time),
BaseDigest: in.BaseDigest,
LastPendingFinalizerPolicyGeneration: in.LastPendingFinalizerPolicyGeneration,
}

if in.ProposedFinalizerPolicy != nil {
finalizerPolicy := &pbantelope.FinalizerPolicy{
Generation: in.ProposedFinalizerPolicy.Generation,
Threshold: in.ProposedFinalizerPolicy.Threshold,
Finalizers: nil,
}
if in.PendingFinalizerPolicy != nil {
res.PendingFinalizerPolicy = FinalizerPolicyToDEOS(in.PendingFinalizerPolicy)
}

finalizers := make([]*pbantelope.FinalizerAuthority, 0, len(finalizerPolicy.Finalizers))
for _, finalizer := range finalizerPolicy.Finalizers {
finalizers = append(finalizers, &pbantelope.FinalizerAuthority{
Description: finalizer.Description,
Weight: finalizer.Weight,
PublicKey: finalizer.PublicKey,
})
}
finalizerPolicy.Finalizers = finalizers
return res
}

func FinalizerPolicyToDEOS(in *FinalizerPolicy) *pbantelope.FinalizerPolicy {
res := &pbantelope.FinalizerPolicy{
Generation: in.Generation,
Threshold: in.Threshold,
Finalizers: nil,
}

finalizers := make([]*pbantelope.FinalizerAuthority, 0, len(res.Finalizers))
for _, finalizer := range res.Finalizers {
finalizers = append(finalizers, &pbantelope.FinalizerAuthority{
Description: finalizer.Description,
Weight: finalizer.Weight,
PublicKey: finalizer.PublicKey,
})
}
res.Finalizers = finalizers

return res
}

func ProposerPolicyToDEOS(in *ProposerPolicy) *pbantelope.ProposerPolicy {
return &pbantelope.ProposerPolicy{
ActiveTime: timestamppb.New(in.ActiveTime.Time),
ProposerSchedule: antelope.ProducerAuthorityScheduleToDEOS(in.ProducerSchedule),
}
}
33 changes: 19 additions & 14 deletions codec/antelope/spring_v1/hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ func (h *Hydrator) HydrateBlock(block *pbantelope.Block, input []byte, version s
block.Header = antelope.BlockHeaderToDEOS(&signedBlock.BlockHeader)
block.BlockExtensions = antelope.ExtensionsToDEOS(signedBlock.BlockExtensions)

decodedBlockHeaderExtensions, err := antelope.BlockHeaderExtensionsToDEOS(signedBlock.BlockHeader.HeaderExtensions)
if err != nil {
h.logger.Debug("failed to decode block header extensions", zap.Error(err))
} else {
block.Header.DecodedHeaderExtensions = decodedBlockHeaderExtensions
}

block.DposIrreversibleBlocknum = blockState.DPoSIrreversibleBlockNum
block.DposProposedIrreversibleBlocknum = blockState.DPoSProposedIrreversibleBlockNum
block.BlockrootMerkle = antelope.BlockrootMerkleToDEOS(blockState.BlockrootMerkle)
Expand Down Expand Up @@ -102,13 +95,6 @@ func (h *Hydrator) HydrateBlock(block *pbantelope.Block, input []byte, version s
block.Header = antelope.BlockHeaderToDEOS(&signedBlock.BlockHeader)
block.BlockExtensions = antelope.ExtensionsToDEOS(signedBlock.BlockExtensions)

decodedBlockHeaderExtensions, err := antelope.BlockHeaderExtensionsToDEOS(signedBlock.BlockHeader.HeaderExtensions)
if err != nil {
h.logger.Debug("failed to decode block header extensions", zap.Error(err))
} else {
block.Header.DecodedHeaderExtensions = decodedBlockHeaderExtensions
}

block.UnfilteredTransactionCount = uint32(len(signedBlock.Transactions))
for idx, transaction := range signedBlock.Transactions {
deosTransaction := TransactionReceiptToDEOS(transaction)
Expand Down Expand Up @@ -157,6 +143,25 @@ func (h *Hydrator) DecodeFinalityData(input []byte) (*pbantelope.FinalityData, e
return FinalityDataToDEOS(finalityData), nil
}

func (h *Hydrator) DecodeProposerPolicy(input []byte) (*pbantelope.ProposerPolicy, error) {

proposerPolicy := &ProposerPolicy{}
if err := unmarshalBinary(input, proposerPolicy); err != nil {
return nil, fmt.Errorf("unmarshalling binary proposer policy: %w", err)
}

return ProposerPolicyToDEOS(proposerPolicy), nil
}

func (h *Hydrator) DecodeFinalizerPolicy(input []byte) (*pbantelope.FinalizerPolicy, error) {
finalizerPolicy := &FinalizerPolicy{}
if err := unmarshalBinary(input, finalizerPolicy); err != nil {
return nil, fmt.Errorf("unmarshalling binary finalizer policy: %w", err)
}

return FinalizerPolicyToDEOS(finalizerPolicy), nil
}

func unmarshalBinary(data []byte, v interface{}) error {
decoder := eos.NewDecoder(data)
decoder.DecodeActions(false)
Expand Down
23 changes: 16 additions & 7 deletions codec/antelope/spring_v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ type SignedBlock struct {
//
// - https://github.com/AntelopeIO/spring/blob/main/libraries/chain/include/eosio/chain/block_state.hpp#L61
type FinalityData struct {
MajorVersion uint32 `json:"major_version"`
MinorVersion uint32 `json:"minor_version"`
ActiveFinalizerPolicyGeneration uint32 `json:"active_finalizer_policy_generation"`
FinalOnStrongQCBlockNum uint32 `json:"final_on_strong_qc_block_num"`
ActionMroot eos.Checksum256 `json:"action_mroot"`
BaseDigest eos.Checksum256 `json:"base_digest"`
ProposedFinalizerPolicy *FinalizerPolicy `json:"proposed_finalizer_policy,omitempty" eos:"optional"`
MajorVersion uint32 `json:"major_version"`
MinorVersion uint32 `json:"minor_version"`
ActiveFinalizerPolicyGeneration uint32 `json:"active_finalizer_policy_generation"`
ActionMroot eos.Checksum256 `json:"action_mroot"`
ReversibleBlocksMroot eos.Checksum256 `json:"reversible_blocks_mroot"`
LatestQCClaimBlockNum uint32 `json:"latest_qc_claim_block_num"`
LatestQCClaimFinalityDigest eos.Checksum256 `json:"latest_qc_claim_finality_digest"`
LatestQCClaimTimestamp eos.BlockTimestamp `json:"latest_qc_claim_timestamp"`
BaseDigest eos.Checksum256 `json:"base_digest"`
PendingFinalizerPolicy *FinalizerPolicy `json:"pending_finalizer_policy,omitempty" eos:"optional"`
LastPendingFinalizerPolicyGeneration uint32 `json:"last_pending_finalizer_policy_generation"`
}

type FinalizerPolicy struct {
Expand All @@ -68,6 +72,11 @@ type FinalizerAuthority struct {
PublicKey string `json:"public_key"`
}

type ProposerPolicy struct {
ActiveTime eos.BlockTimestamp `json:"active_time"`
ProducerSchedule *eos.ProducerAuthoritySchedule `json:"proposer_schedule"`
}

// TransactionTrace
//
// File hierarchy:
Expand Down
Loading

0 comments on commit 534ca5b

Please sign in to comment.