Skip to content

Commit

Permalink
start work on spring codec
Browse files Browse the repository at this point in the history
  • Loading branch information
fschoell committed Jun 6, 2024
1 parent 1845f06 commit 7513564
Show file tree
Hide file tree
Showing 9 changed files with 2,246 additions and 1,751 deletions.
2 changes: 2 additions & 0 deletions codec/antelope/hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ type Hydrator interface {
// DecodeTransactionTrace decodes the received Deep Mind AppliedTransaction data structure against the
// correct struct for this version of EOSIO supported by this hydrator.
DecodeTransactionTrace(input []byte, opts ...ConversionOption) (*pbantelope.TransactionTrace, error)

DecodeFinalityData(input []byte) (*pbantelope.FinalityData, error)
}
32 changes: 32 additions & 0 deletions codec/antelope/v3.1/eos_to_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,35 @@ 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
}
10 changes: 10 additions & 0 deletions codec/antelope/v3.1/hydrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,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) {

finalityData := &FinalityData{}
if err := unmarshalBinary(input, finalityData); err != nil {
return nil, fmt.Errorf("unmarshalling binary finality data: %w", err)
}

return FinalityDataToDEOS(finalityData), nil
}

func unmarshalBinary(data []byte, v interface{}) error {
decoder := eos.NewDecoder(data)
decoder.DecodeActions(false)
Expand Down
27 changes: 27 additions & 0 deletions codec/antelope/v3.1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,33 @@ type SignedBlock struct {
BlockExtensions []*eos.Extension `json:"block_extensions"`
}

// FinalityData
//
// File hierarchy:
//
// - 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" eos:"optional"`
}

type FinalizerPolicy struct {
Generation uint32 `json:"generation"`
Threshold uint64 `json:"threshold"`
Finalizers []*FinalizerAuthority `json:"finalizers"`
}

type FinalizerAuthority struct {
Description string `json:"description"`
Weight uint64 `json:"weight"`
PublicKey string `json:"public_key"`
}

// TransactionTrace
//
// File hierarchy:
Expand Down
69 changes: 67 additions & 2 deletions codec/consolereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ import (
"go.uber.org/zap"
)

var supportedVersions = []uint64{13}
var supportedVersionStrings = []string{"13"}
var supportedVersions = []uint64{1, 13}
var supportedVersionStrings = []string{"1", "13"}

// ConsoleReader is what reads the `nodeos` output directly. It builds
// up some LogEntry objects. See `LogReader to read those entries.
Expand Down Expand Up @@ -687,6 +687,71 @@ func (ctx *parseCtx) readAcceptedBlock(line string) (*pbantelope.Block, error) {
return block, nil
}

// Line format:
//
// ACCEPTED_BLOCK_V2 ${block_num} ${lib} ${block_state_hex} ${finality_data_hex}
func (ctx *parseCtx) readAcceptedBlockV2(line string) (*pbantelope.Block, error) {
chunks := strings.SplitN(line, " ", 5)
if len(chunks) != 3 {
return nil, fmt.Errorf("expected 5 fields, got %d", len(chunks))
}

blockNum, err := strconv.ParseInt(chunks[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("block_num not a valid number, got: %q", chunks[1])
}

if ctx.activeBlockNum != blockNum {
return nil, fmt.Errorf("block_num %d doesn't match the active block num (%d)", blockNum, ctx.activeBlockNum)
}

ctx.stats = newParsingStats(ctx.logger, uint64(blockNum))

blockStateHex, err := hex.DecodeString(chunks[3])
if err != nil {
return nil, fmt.Errorf("unable to decode block %d state hex: %w", blockNum, err)
}

if err := ctx.hydrator.HydrateBlock(ctx.currentBlock, blockStateHex); err != nil {
return nil, fmt.Errorf("hydrate block %d: %w", blockNum, err)
}
block := ctx.currentBlock

lib, err := strconv.ParseInt(chunks[2], 10, 32)
if err != nil {
return nil, fmt.Errorf("lib not a valid number, got: %q", chunks[2])
}
block.FinalityLib = uint32(lib)

finalityDataHex, err := hex.DecodeString(chunks[4])
if err != nil {
return nil, fmt.Errorf("unable to decode finality data hex: %w", err)
}

finalityData, err := ctx.hydrator.DecodeFinalityData(finalityDataHex)
if err != nil {
return nil, fmt.Errorf("unable to decode finality data: %w", err)
}
block.FinalityData = finalityData

zlog.Debug("blocking until abi decoder has decoded every transaction pushed to it")
err = ctx.abiDecoder.endBlock(ctx.currentBlock)
if err != nil {
return nil, fmt.Errorf("abi decoding post-process failed: %w", err)
}

ctx.globalStats.lastBlock = ctx.currentBlock.AsRef()
ctx.globalStats.blockRate.Inc()
ctx.globalStats.blockAverageParseTime.AddElapsedTime(ctx.stats.startAt)
ctx.globalStats.transactionRate.IncBy(int64(len(ctx.currentBlock.TransactionTraces())))
ctx.stats.log()

zlog.Debug("abi decoder terminated all decoding operations, resetting block")
ctx.resetBlock()

return block, nil
}

// Line format:
//
// APPLIED_TRANSACTION ${block_num} ${trace_hex}
Expand Down
26 changes: 26 additions & 0 deletions proto/sf/antelope/type/v1/type.proto
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ message Block {
PendingProducerSchedule pending_schedule = 16;
ActivatedProtocolFeatures activated_protocol_features = 17;

// from Spring v1 this field will contain the lib block number in favour of the dpos_proposed_irreversible_blocknum
uint32 finality_lib = 60;
FinalityData finality_data = 61;

// EOS 2.1.x feature, did not make it into leap
// bool validated = 18;
reserved 18;
Expand Down Expand Up @@ -207,6 +211,28 @@ message Block {
string filtering_system_actions_include_filter_expr = 50;
}

message FinalityData {
uint32 major_version = 1;
uint32 minor_version = 2;
uint32 active_finalizer_policy_generation = 3;
uint32 final_on_strong_qc_block_num = 4;
bytes action_mroot = 5;
bytes base_digest = 6;
FinalizerPolicy proposed_finalizer_policy = 7;
}

message FinalizerPolicy {
uint32 generation = 1;
uint64 threshold = 2;
repeated FinalizerAuthority finalizers = 3;
}

message FinalizerAuthority {
string description = 1;
uint64 weight = 2;
string public_key = 3;
}

// BlockWithRefs is a lightweight block, with traces and transactions
// purged from the `block` within, and only. It is used in transports
// to pass block data around.
Expand Down
4 changes: 2 additions & 2 deletions types/pb/last_generate.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
generate.sh - Tue Dec 12 16:34:49 CET 2023 - work
streamingfast/firehose-antelope/proto revision: 9565470
generate.sh - Thu Jun 6 13:43:55 CEST 2024 - work
streamingfast/firehose-antelope/proto revision: fc5f9bd
4 changes: 4 additions & 0 deletions types/pb/sf/antelope/type/v1/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (b *Block) GetFirehoseBlockLIBNum() uint64 {
}

func (b *Block) LIBNum() uint64 {
if b.FinalityLib > 0 {
return uint64(b.FinalityLib)
}

return uint64(b.DposIrreversibleBlocknum)
}

Expand Down
Loading

0 comments on commit 7513564

Please sign in to comment.