Skip to content

Commit ca650cf

Browse files
authoredFeb 11, 2025
Add utils to get RequestBeginBlock (#258)
1 parent 3e14eb1 commit ca650cf

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
 

‎types/block.go

+26
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import (
1111
"github.com/gogo/protobuf/proto"
1212
gogotypes "github.com/gogo/protobuf/types"
1313

14+
abci "github.com/tendermint/tendermint/abci/types"
1415
"github.com/tendermint/tendermint/crypto"
1516
"github.com/tendermint/tendermint/crypto/merkle"
1617
"github.com/tendermint/tendermint/libs/bits"
1718
tmbytes "github.com/tendermint/tendermint/libs/bytes"
1819
tmmath "github.com/tendermint/tendermint/libs/math"
1920
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
21+
"github.com/tendermint/tendermint/utils"
2022
"github.com/tendermint/tendermint/version"
2123
)
2224

@@ -233,6 +235,30 @@ func (b *Block) ToProto() (*tmproto.Block, error) {
233235
return pb, nil
234236
}
235237

238+
func (b *Block) ToReqBeginBlock(vals []*Validator) abci.RequestBeginBlock {
239+
tmHeader := b.Header.ToProto()
240+
votes := make([]abci.VoteInfo, b.LastCommit.Size())
241+
for i, val := range vals {
242+
commitSig := b.LastCommit.Signatures[i]
243+
votes[i] = abci.VoteInfo{
244+
Validator: TM2PB.Validator(val),
245+
SignedLastBlock: commitSig.BlockIDFlag != BlockIDFlagAbsent,
246+
}
247+
}
248+
abciEvidence := b.Evidence.ToABCI()
249+
return abci.RequestBeginBlock{
250+
Hash: b.hash,
251+
Header: *tmHeader,
252+
LastCommitInfo: abci.LastCommitInfo{
253+
Round: b.LastCommit.Round,
254+
Votes: votes,
255+
},
256+
ByzantineValidators: utils.Map(abciEvidence, func(e abci.Misbehavior) abci.Evidence {
257+
return abci.Evidence(e)
258+
}),
259+
}
260+
}
261+
236262
// FromProto sets a protobuf Block to the given pointer.
237263
// It returns an error if the block is invalid.
238264
func BlockFromProto(bp *tmproto.Block) (*Block, error) {

‎utils/slice.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package utils
2+
3+
func Map[I any, O any](input []I, lambda func(i I) O) []O {
4+
res := []O{}
5+
for _, i := range input {
6+
res = append(res, lambda(i))
7+
}
8+
return res
9+
}

0 commit comments

Comments
 (0)