-
Notifications
You must be signed in to change notification settings - Fork 45
/
block.go
94 lines (82 loc) · 3.25 KB
/
block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mdl
import (
"fmt"
"github.com/dfuse-io/dfuse-eosio/codec"
pbcodec "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/codec/v1"
eos "github.com/eoscanada/eos-go"
)
// BlockSummary is the dfuse summary information for a given block
// Candidate for a move in eosws-go directly once the related REST API are made
// public.
type BlockSummary struct {
ID string `json:"id"`
BlockNum uint32 `json:"block_num"`
Irreversible bool `json:"irreversible"`
Header *eos.BlockHeader `json:"header"`
ActiveSchedule *eos.ProducerSchedule `json:"active_schedule"`
TransactionCount int `json:"transaction_count"`
SiblingBlocks []*BlockSummary `json:"sibling_blocks"`
DPoSLIBNum uint32 `json:"dpos_lib_num"`
}
func ToV1BlockSummary(in *pbcodec.BlockWithRefs) (*BlockSummary, error) {
summary := &BlockSummary{
ID: in.Id,
Irreversible: in.Irreversible,
Header: codec.BlockHeaderToEOS(in.Block.Header),
TransactionCount: int(in.Block.TransactionCount),
BlockNum: in.Block.Number,
DPoSLIBNum: in.Block.DposIrreversibleBlocknum,
}
// FIXME: At some point, we will like to maybe change the output format so that it fits EOSIO 2.0
// format. Will need to think about how all this should be.
if in.Block.ActiveScheduleV2 != nil {
downgradedSchedule, err := downgradeActiveScheduleV2ToV1(in.Block.ActiveScheduleV2)
if err != nil {
return nil, err
}
summary.ActiveSchedule = codec.ProducerScheduleToEOS(downgradedSchedule)
} else if in.Block.ActiveScheduleV1 != nil {
summary.ActiveSchedule = codec.ProducerScheduleToEOS(in.Block.ActiveScheduleV1)
}
return summary, nil
}
func downgradeActiveScheduleV2ToV1(in *pbcodec.ProducerAuthoritySchedule) (*pbcodec.ProducerSchedule, error) {
newProducers := make([]*pbcodec.ProducerKey, len(in.Producers))
for i, producer := range in.Producers {
pubKey, err := extractFirstPublicKeyFromAuthority(producer.BlockSigningAuthority)
if err != nil {
return nil, fmt.Errorf("failed to downgrade schedule: %w", err)
}
newProducers[i] = &pbcodec.ProducerKey{
AccountName: producer.AccountName,
BlockSigningKey: pubKey,
}
}
return &pbcodec.ProducerSchedule{
Version: in.Version,
Producers: newProducers,
}, nil
}
func extractFirstPublicKeyFromAuthority(in *pbcodec.BlockSigningAuthority) (string, error) {
if in.GetV0() == nil {
return "", fmt.Errorf("only knowns how to deal with BlockSigningAuthority_V0 type, got %t", in.Variant)
}
keys := in.GetV0().GetKeys()
if len(keys) <= 0 {
return "", nil
}
return keys[0].PublicKey, nil
}