/
block.go
149 lines (137 loc) · 4.73 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package abci
import (
"time"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/gridfoundation/furyint/types"
)
// ToABCIHeaderPB converts Furyint header to Header format defined in ABCI.
// Caller should fill all the fields that are not available in Furyint header (like ChainID).
func ToABCIHeaderPB(header *types.Header) tmproto.Header {
return tmproto.Header{
Version: tmversion.Consensus{
Block: header.Version.Block,
App: header.Version.App,
},
Height: int64(header.Height),
Time: time.Unix(0, int64(header.Time)),
LastBlockId: tmproto.BlockID{
Hash: header.LastHeaderHash[:],
PartSetHeader: tmproto.PartSetHeader{
Total: 1,
Hash: header.LastHeaderHash[:],
},
},
LastCommitHash: header.LastCommitHash[:],
DataHash: header.DataHash[:],
ValidatorsHash: header.AggregatorsHash[:],
NextValidatorsHash: header.AggregatorsHash[:],
ConsensusHash: header.ConsensusHash[:],
AppHash: header.AppHash[:],
LastResultsHash: header.LastResultsHash[:],
EvidenceHash: new(tmtypes.EvidenceData).Hash(),
ProposerAddress: header.ProposerAddress,
ChainID: header.ChainID,
}
}
// ToABCIHeader converts Furyint header to Header format defined in ABCI.
// Caller should fill all the fields that are not available in Furyint header (like ChainID).
func ToABCIHeader(header *types.Header) tmtypes.Header {
return tmtypes.Header{
Version: tmversion.Consensus{
Block: header.Version.Block,
App: header.Version.App,
},
Height: int64(header.Height),
Time: time.Unix(0, int64(header.Time)),
LastBlockID: tmtypes.BlockID{
Hash: header.LastHeaderHash[:],
PartSetHeader: tmtypes.PartSetHeader{
Total: 1,
Hash: header.LastHeaderHash[:],
},
},
LastCommitHash: header.LastCommitHash[:],
DataHash: header.DataHash[:],
ValidatorsHash: header.AggregatorsHash[:],
NextValidatorsHash: header.AggregatorsHash[:],
ConsensusHash: header.ConsensusHash[:],
AppHash: header.AppHash[:],
LastResultsHash: header.LastResultsHash[:],
EvidenceHash: new(tmtypes.EvidenceData).Hash(),
ProposerAddress: header.ProposerAddress,
ChainID: header.ChainID,
}
}
// ToABCIBlock converts Furyint block into block format defined by ABCI.
// Returned block should pass `ValidateBasic`.
func ToABCIBlock(block *types.Block) (*tmtypes.Block, error) {
abciHeader := ToABCIHeader(&block.Header)
abciCommit := ToABCICommit(&block.LastCommit, &block.Header)
// This assumes that we have only one signature
if len(abciCommit.Signatures) == 1 {
abciCommit.Signatures[0].ValidatorAddress = block.Header.ProposerAddress
}
abciBlock := tmtypes.Block{
Header: abciHeader,
Evidence: tmtypes.EvidenceData{
Evidence: nil,
},
LastCommit: abciCommit,
}
abciBlock.Data.Txs = ToABCIBlockDataTxs(&block.Data)
abciBlock.Header.DataHash = block.Header.DataHash[:]
return &abciBlock, nil
}
// ToABCIBlockDataTxs converts Furyint block-data into block-data format defined by ABCI.
func ToABCIBlockDataTxs(data *types.Data) []tmtypes.Tx {
txs := make([]tmtypes.Tx, len(data.Txs))
for i := range data.Txs {
txs[i] = tmtypes.Tx(data.Txs[i])
}
return txs
}
// ToABCIBlockMeta converts Furyint block into BlockMeta format defined by ABCI
func ToABCIBlockMeta(block *types.Block) (*tmtypes.BlockMeta, error) {
tmblock, err := ToABCIBlock(block)
if err != nil {
return nil, err
}
blockID := tmtypes.BlockID{Hash: tmblock.Hash()}
return &tmtypes.BlockMeta{
BlockID: blockID,
BlockSize: tmblock.Size(),
Header: tmblock.Header,
NumTxs: len(tmblock.Txs),
}, nil
}
// ToABCICommit converts Furyint commit into commit format defined by ABCI.
// This function only converts fields that are available in Furyint commit.
// Other fields (especially ValidatorAddress and Timestamp of Signature) has to be filled by caller.
func ToABCICommit(commit *types.Commit, header *types.Header) *tmtypes.Commit {
tmCommit := tmtypes.Commit{
Height: int64(commit.Height),
Round: 0,
BlockID: tmtypes.BlockID{
Hash: commit.HeaderHash[:],
PartSetHeader: tmtypes.PartSetHeader{
Total: 1,
Hash: commit.HeaderHash[:],
},
},
}
for _, sig := range commit.Signatures {
commitSig := tmtypes.CommitSig{
BlockIDFlag: tmtypes.BlockIDFlagCommit,
Signature: sig,
}
tmCommit.Signatures = append(tmCommit.Signatures, commitSig)
}
// This assumes that we have only one signature
if len(commit.Signatures) == 1 {
tmCommit.Signatures[0].ValidatorAddress = header.ProposerAddress
tmCommit.Signatures[0].Timestamp = time.Unix(0, int64(header.Time))
}
return &tmCommit
}