You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1176 lines
31 KiB

max-bytes PR follow-up (#2318) * ReapMaxTxs: return all txs if max is negative this mirrors ReapMaxBytes behavior See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950 * increase MaxAminoOverheadForBlock tested with: ``` func TestMaxAminoOverheadForBlock(t *testing.T) { maxChainID := "" for i := 0; i < MaxChainIDLen; i++ { maxChainID += "𠜎" } h := Header{ ChainID: maxChainID, Height: 10, Time: time.Now().UTC(), NumTxs: 100, TotalTxs: 200, LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)), LastCommitHash: tmhash.Sum([]byte("last_commit_hash")), DataHash: tmhash.Sum([]byte("data_hash")), ValidatorsHash: tmhash.Sum([]byte("validators_hash")), NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")), ConsensusHash: tmhash.Sum([]byte("consensus_hash")), AppHash: tmhash.Sum([]byte("app_hash")), LastResultsHash: tmhash.Sum([]byte("last_results_hash")), EvidenceHash: tmhash.Sum([]byte("evidence_hash")), ProposerAddress: tmhash.Sum([]byte("proposer_address")), } b := Block{ Header: h, Data: Data{Txs: makeTxs(10000, 100)}, Evidence: EvidenceData{}, LastCommit: &Commit{}, } bz, err := cdc.MarshalBinary(b) require.NoError(t, err) assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1) } ``` * fix MaxYYY constants calculation by using math.MaxInt64 See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244 * pass mempool filter as an option See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869 * fixes after Dev's comments
6 years ago
max-bytes PR follow-up (#2318) * ReapMaxTxs: return all txs if max is negative this mirrors ReapMaxBytes behavior See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950 * increase MaxAminoOverheadForBlock tested with: ``` func TestMaxAminoOverheadForBlock(t *testing.T) { maxChainID := "" for i := 0; i < MaxChainIDLen; i++ { maxChainID += "𠜎" } h := Header{ ChainID: maxChainID, Height: 10, Time: time.Now().UTC(), NumTxs: 100, TotalTxs: 200, LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)), LastCommitHash: tmhash.Sum([]byte("last_commit_hash")), DataHash: tmhash.Sum([]byte("data_hash")), ValidatorsHash: tmhash.Sum([]byte("validators_hash")), NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")), ConsensusHash: tmhash.Sum([]byte("consensus_hash")), AppHash: tmhash.Sum([]byte("app_hash")), LastResultsHash: tmhash.Sum([]byte("last_results_hash")), EvidenceHash: tmhash.Sum([]byte("evidence_hash")), ProposerAddress: tmhash.Sum([]byte("proposer_address")), } b := Block{ Header: h, Data: Data{Txs: makeTxs(10000, 100)}, Evidence: EvidenceData{}, LastCommit: &Commit{}, } bz, err := cdc.MarshalBinary(b) require.NoError(t, err) assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1) } ``` * fix MaxYYY constants calculation by using math.MaxInt64 See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244 * pass mempool filter as an option See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869 * fixes after Dev's comments
6 years ago
10 years ago
10 years ago
7 years ago
7 years ago
7 years ago
7 years ago
10 years ago
11 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "sync"
  8. "time"
  9. "github.com/gogo/protobuf/proto"
  10. gogotypes "github.com/gogo/protobuf/types"
  11. "github.com/tendermint/tendermint/crypto"
  12. "github.com/tendermint/tendermint/crypto/merkle"
  13. "github.com/tendermint/tendermint/crypto/tmhash"
  14. "github.com/tendermint/tendermint/libs/bits"
  15. tmbytes "github.com/tendermint/tendermint/libs/bytes"
  16. tmmath "github.com/tendermint/tendermint/libs/math"
  17. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  18. "github.com/tendermint/tendermint/version"
  19. )
  20. const (
  21. // MaxHeaderBytes is a maximum header size.
  22. // NOTE: Because app hash can be of arbitrary size, the header is therefore not
  23. // capped in size and thus this number should be seen as a soft max
  24. MaxHeaderBytes int64 = 626
  25. // MaxOverheadForBlock - maximum overhead to encode a block (up to
  26. // MaxBlockSizeBytes in size) not including it's parts except Data.
  27. // This means it also excludes the overhead for individual transactions.
  28. //
  29. // Uvarint length of MaxBlockSizeBytes: 4 bytes
  30. // 2 fields (2 embedded): 2 bytes
  31. // Uvarint length of Data.Txs: 4 bytes
  32. // Data.Txs field: 1 byte
  33. MaxOverheadForBlock int64 = 11
  34. )
  35. // Block defines the atomic unit of a Tendermint blockchain.
  36. type Block struct {
  37. mtx sync.Mutex
  38. Header `json:"header"`
  39. Data `json:"data"`
  40. Evidence EvidenceList `json:"evidence"`
  41. LastCommit *Commit `json:"last_commit"`
  42. }
  43. // ValidateBasic performs basic validation that doesn't involve state data.
  44. // It checks the internal consistency of the block.
  45. // Further validation is done using state#ValidateBlock.
  46. func (b *Block) ValidateBasic() error {
  47. if b == nil {
  48. return errors.New("nil block")
  49. }
  50. b.mtx.Lock()
  51. defer b.mtx.Unlock()
  52. if err := b.Header.ValidateBasic(); err != nil {
  53. return fmt.Errorf("invalid header: %w", err)
  54. }
  55. // Validate the last commit and its hash.
  56. if b.LastCommit == nil {
  57. return errors.New("nil LastCommit")
  58. }
  59. if err := b.LastCommit.ValidateBasic(); err != nil {
  60. return fmt.Errorf("wrong LastCommit: %w", err)
  61. }
  62. if w, g := b.LastCommit.Hash(), b.LastCommitHash; !bytes.Equal(w, g) {
  63. return fmt.Errorf("wrong Header.LastCommitHash. Expected %X, got %X", w, g)
  64. }
  65. // NOTE: b.Data.Txs may be nil, but b.Data.Hash() still works fine.
  66. if w, g := b.Data.Hash(), b.DataHash; !bytes.Equal(w, g) {
  67. return fmt.Errorf("wrong Header.DataHash. Expected %X, got %X", w, g)
  68. }
  69. // NOTE: b.Evidence may be nil, but we're just looping.
  70. for i, ev := range b.Evidence {
  71. if err := ev.ValidateBasic(); err != nil {
  72. return fmt.Errorf("invalid evidence (#%d): %v", i, err)
  73. }
  74. }
  75. if w, g := b.Evidence.Hash(), b.EvidenceHash; !bytes.Equal(w, g) {
  76. return fmt.Errorf("wrong Header.EvidenceHash. Expected %X, got %X", w, g)
  77. }
  78. return nil
  79. }
  80. // fillHeader fills in any remaining header fields that are a function of the block data
  81. func (b *Block) fillHeader() {
  82. if b.LastCommitHash == nil {
  83. b.LastCommitHash = b.LastCommit.Hash()
  84. }
  85. if b.DataHash == nil {
  86. b.DataHash = b.Data.Hash()
  87. }
  88. if b.EvidenceHash == nil {
  89. b.EvidenceHash = b.Evidence.Hash()
  90. }
  91. }
  92. // Hash computes and returns the block hash.
  93. // If the block is incomplete, block hash is nil for safety.
  94. func (b *Block) Hash() tmbytes.HexBytes {
  95. if b == nil {
  96. return nil
  97. }
  98. b.mtx.Lock()
  99. defer b.mtx.Unlock()
  100. if b.LastCommit == nil {
  101. return nil
  102. }
  103. b.fillHeader()
  104. return b.Header.Hash()
  105. }
  106. // MakePartSet returns a PartSet containing parts of a serialized block.
  107. // This is the form in which the block is gossipped to peers.
  108. // CONTRACT: partSize is greater than zero.
  109. func (b *Block) MakePartSet(partSize uint32) (*PartSet, error) {
  110. if b == nil {
  111. return nil, errors.New("nil block")
  112. }
  113. b.mtx.Lock()
  114. defer b.mtx.Unlock()
  115. pbb, err := b.ToProto()
  116. if err != nil {
  117. return nil, err
  118. }
  119. bz, err := proto.Marshal(pbb)
  120. if err != nil {
  121. return nil, err
  122. }
  123. return NewPartSetFromData(bz, partSize), nil
  124. }
  125. // HashesTo is a convenience function that checks if a block hashes to the given argument.
  126. // Returns false if the block is nil or the hash is empty.
  127. func (b *Block) HashesTo(hash []byte) bool {
  128. if len(hash) == 0 {
  129. return false
  130. }
  131. if b == nil {
  132. return false
  133. }
  134. return bytes.Equal(b.Hash(), hash)
  135. }
  136. // Size returns size of the block in bytes.
  137. func (b *Block) Size() int {
  138. pbb, err := b.ToProto()
  139. if err != nil {
  140. return 0
  141. }
  142. return pbb.Size()
  143. }
  144. // String returns a string representation of the block
  145. //
  146. // See StringIndented.
  147. func (b *Block) String() string {
  148. return b.StringIndented("")
  149. }
  150. // StringIndented returns an indented String.
  151. //
  152. // Header
  153. // Data
  154. // Evidence
  155. // LastCommit
  156. // Hash
  157. func (b *Block) StringIndented(indent string) string {
  158. if b == nil {
  159. return "nil-Block"
  160. }
  161. return fmt.Sprintf(`Block{
  162. %s %v
  163. %s %v
  164. %s %v
  165. %s %v
  166. %s}#%v`,
  167. indent, b.Header.StringIndented(indent+" "),
  168. indent, b.Data.StringIndented(indent+" "),
  169. indent, b.Evidence.StringIndented(indent+" "),
  170. indent, b.LastCommit.StringIndented(indent+" "),
  171. indent, b.Hash())
  172. }
  173. // StringShort returns a shortened string representation of the block.
  174. func (b *Block) StringShort() string {
  175. if b == nil {
  176. return "nil-Block"
  177. }
  178. return fmt.Sprintf("Block#%X", b.Hash())
  179. }
  180. // ToProto converts Block to protobuf
  181. func (b *Block) ToProto() (*tmproto.Block, error) {
  182. if b == nil {
  183. return nil, errors.New("nil Block")
  184. }
  185. pb := new(tmproto.Block)
  186. pb.Header = *b.Header.ToProto()
  187. pb.LastCommit = b.LastCommit.ToProto()
  188. pb.Data = b.Data.ToProto()
  189. protoEvidence, err := b.Evidence.ToProto()
  190. if err != nil {
  191. return nil, err
  192. }
  193. pb.Evidence = *protoEvidence
  194. return pb, nil
  195. }
  196. // FromProto sets a protobuf Block to the given pointer.
  197. // It returns an error if the block is invalid.
  198. func BlockFromProto(bp *tmproto.Block) (*Block, error) {
  199. if bp == nil {
  200. return nil, errors.New("nil block")
  201. }
  202. b := new(Block)
  203. h, err := HeaderFromProto(&bp.Header)
  204. if err != nil {
  205. return nil, err
  206. }
  207. b.Header = h
  208. data, err := DataFromProto(&bp.Data)
  209. if err != nil {
  210. return nil, err
  211. }
  212. b.Data = data
  213. if err := b.Evidence.FromProto(&bp.Evidence); err != nil {
  214. return nil, err
  215. }
  216. if bp.LastCommit != nil {
  217. lc, err := CommitFromProto(bp.LastCommit)
  218. if err != nil {
  219. return nil, err
  220. }
  221. b.LastCommit = lc
  222. }
  223. return b, b.ValidateBasic()
  224. }
  225. //-----------------------------------------------------------------------------
  226. // MaxDataBytes returns the maximum size of block's data.
  227. //
  228. // XXX: Panics on negative result.
  229. func MaxDataBytes(maxBytes, evidenceBytes int64, valsCount int) int64 {
  230. maxDataBytes := maxBytes -
  231. MaxOverheadForBlock -
  232. MaxHeaderBytes -
  233. MaxCommitBytes(valsCount) -
  234. evidenceBytes
  235. if maxDataBytes < 0 {
  236. panic(fmt.Sprintf(
  237. "Negative MaxDataBytes. Block.MaxBytes=%d is too small to accommodate header&lastCommit&evidence=%d",
  238. maxBytes,
  239. -(maxDataBytes - maxBytes),
  240. ))
  241. }
  242. return maxDataBytes
  243. }
  244. // MaxDataBytesNoEvidence returns the maximum size of block's data when
  245. // evidence count is unknown. MaxEvidencePerBlock will be used for the size
  246. // of evidence.
  247. //
  248. // XXX: Panics on negative result.
  249. func MaxDataBytesNoEvidence(maxBytes int64, valsCount int) int64 {
  250. maxDataBytes := maxBytes -
  251. MaxOverheadForBlock -
  252. MaxHeaderBytes -
  253. MaxCommitBytes(valsCount)
  254. if maxDataBytes < 0 {
  255. panic(fmt.Sprintf(
  256. "Negative MaxDataBytesUnknownEvidence. Block.MaxBytes=%d is too small to accommodate header&lastCommit&evidence=%d",
  257. maxBytes,
  258. -(maxDataBytes - maxBytes),
  259. ))
  260. }
  261. return maxDataBytes
  262. }
  263. // MakeBlock returns a new block with an empty header, except what can be
  264. // computed from itself.
  265. // It populates the same set of fields validated by ValidateBasic.
  266. func MakeBlock(height int64, txs []Tx, lastCommit *Commit, evidence []Evidence) *Block {
  267. block := &Block{
  268. Header: Header{
  269. Version: version.Consensus{Block: version.BlockProtocol, App: 0},
  270. Height: height,
  271. },
  272. Data: Data{
  273. Txs: txs,
  274. },
  275. Evidence: evidence,
  276. LastCommit: lastCommit,
  277. }
  278. block.fillHeader()
  279. return block
  280. }
  281. //-----------------------------------------------------------------------------
  282. // Header defines the structure of a Tendermint block header.
  283. // NOTE: changes to the Header should be duplicated in:
  284. // - header.Hash()
  285. // - abci.Header
  286. // - https://github.com/tendermint/tendermint/blob/master/spec/core/data_structures.md
  287. type Header struct {
  288. // basic block info
  289. Version version.Consensus `json:"version"`
  290. ChainID string `json:"chain_id"`
  291. Height int64 `json:"height,string"`
  292. Time time.Time `json:"time"`
  293. // prev block info
  294. LastBlockID BlockID `json:"last_block_id"`
  295. // hashes of block data
  296. LastCommitHash tmbytes.HexBytes `json:"last_commit_hash"` // commit from validators from the last block
  297. DataHash tmbytes.HexBytes `json:"data_hash"` // transactions
  298. // hashes from the app output from the prev block
  299. ValidatorsHash tmbytes.HexBytes `json:"validators_hash"` // validators for the current block
  300. NextValidatorsHash tmbytes.HexBytes `json:"next_validators_hash"` // validators for the next block
  301. ConsensusHash tmbytes.HexBytes `json:"consensus_hash"` // consensus params for current block
  302. AppHash tmbytes.HexBytes `json:"app_hash"` // state after txs from the previous block
  303. // root hash of all results from the txs from the previous block
  304. // see `deterministicResponseDeliverTx` to understand which parts of a tx is hashed into here
  305. LastResultsHash tmbytes.HexBytes `json:"last_results_hash"`
  306. // consensus info
  307. EvidenceHash tmbytes.HexBytes `json:"evidence_hash"` // evidence included in the block
  308. ProposerAddress Address `json:"proposer_address"` // original proposer of the block
  309. }
  310. // Populate the Header with state-derived data.
  311. // Call this after MakeBlock to complete the Header.
  312. func (h *Header) Populate(
  313. version version.Consensus, chainID string,
  314. timestamp time.Time, lastBlockID BlockID,
  315. valHash, nextValHash []byte,
  316. consensusHash, appHash, lastResultsHash []byte,
  317. proposerAddress Address,
  318. ) {
  319. h.Version = version
  320. h.ChainID = chainID
  321. h.Time = timestamp
  322. h.LastBlockID = lastBlockID
  323. h.ValidatorsHash = valHash
  324. h.NextValidatorsHash = nextValHash
  325. h.ConsensusHash = consensusHash
  326. h.AppHash = appHash
  327. h.LastResultsHash = lastResultsHash
  328. h.ProposerAddress = proposerAddress
  329. }
  330. // ValidateBasic performs stateless validation on a Header returning an error
  331. // if any validation fails.
  332. //
  333. // NOTE: Timestamp validation is subtle and handled elsewhere.
  334. func (h Header) ValidateBasic() error {
  335. if h.Version.Block != version.BlockProtocol {
  336. return fmt.Errorf("block protocol is incorrect: got: %d, want: %d ", h.Version.Block, version.BlockProtocol)
  337. }
  338. if len(h.ChainID) > MaxChainIDLen {
  339. return fmt.Errorf("chainID is too long; got: %d, max: %d", len(h.ChainID), MaxChainIDLen)
  340. }
  341. if h.Height < 0 {
  342. return errors.New("negative Height")
  343. } else if h.Height == 0 {
  344. return errors.New("zero Height")
  345. }
  346. if err := h.LastBlockID.ValidateBasic(); err != nil {
  347. return fmt.Errorf("wrong LastBlockID: %w", err)
  348. }
  349. if err := ValidateHash(h.LastCommitHash); err != nil {
  350. return fmt.Errorf("wrong LastCommitHash: %w", err)
  351. }
  352. if err := ValidateHash(h.DataHash); err != nil {
  353. return fmt.Errorf("wrong DataHash: %w", err)
  354. }
  355. if err := ValidateHash(h.EvidenceHash); err != nil {
  356. return fmt.Errorf("wrong EvidenceHash: %w", err)
  357. }
  358. if len(h.ProposerAddress) != crypto.AddressSize {
  359. return fmt.Errorf(
  360. "invalid ProposerAddress length; got: %d, expected: %d",
  361. len(h.ProposerAddress), crypto.AddressSize,
  362. )
  363. }
  364. // Basic validation of hashes related to application data.
  365. // Will validate fully against state in state#ValidateBlock.
  366. if err := ValidateHash(h.ValidatorsHash); err != nil {
  367. return fmt.Errorf("wrong ValidatorsHash: %w", err)
  368. }
  369. if err := ValidateHash(h.NextValidatorsHash); err != nil {
  370. return fmt.Errorf("wrong NextValidatorsHash: %w", err)
  371. }
  372. if err := ValidateHash(h.ConsensusHash); err != nil {
  373. return fmt.Errorf("wrong ConsensusHash: %w", err)
  374. }
  375. // NOTE: AppHash is arbitrary length
  376. if err := ValidateHash(h.LastResultsHash); err != nil {
  377. return fmt.Errorf("wrong LastResultsHash: %w", err)
  378. }
  379. return nil
  380. }
  381. // Hash returns the hash of the header.
  382. // It computes a Merkle tree from the header fields
  383. // ordered as they appear in the Header.
  384. // Returns nil if ValidatorHash is missing,
  385. // since a Header is not valid unless there is
  386. // a ValidatorsHash (corresponding to the validator set).
  387. func (h *Header) Hash() tmbytes.HexBytes {
  388. if h == nil || len(h.ValidatorsHash) == 0 {
  389. return nil
  390. }
  391. hpb := h.Version.ToProto()
  392. hbz, err := hpb.Marshal()
  393. if err != nil {
  394. return nil
  395. }
  396. pbt, err := gogotypes.StdTimeMarshal(h.Time)
  397. if err != nil {
  398. return nil
  399. }
  400. pbbi := h.LastBlockID.ToProto()
  401. bzbi, err := pbbi.Marshal()
  402. if err != nil {
  403. return nil
  404. }
  405. return merkle.HashFromByteSlices([][]byte{
  406. hbz,
  407. cdcEncode(h.ChainID),
  408. cdcEncode(h.Height),
  409. pbt,
  410. bzbi,
  411. cdcEncode(h.LastCommitHash),
  412. cdcEncode(h.DataHash),
  413. cdcEncode(h.ValidatorsHash),
  414. cdcEncode(h.NextValidatorsHash),
  415. cdcEncode(h.ConsensusHash),
  416. cdcEncode(h.AppHash),
  417. cdcEncode(h.LastResultsHash),
  418. cdcEncode(h.EvidenceHash),
  419. cdcEncode(h.ProposerAddress),
  420. })
  421. }
  422. // StringIndented returns an indented string representation of the header.
  423. func (h *Header) StringIndented(indent string) string {
  424. if h == nil {
  425. return "nil-Header"
  426. }
  427. return fmt.Sprintf(`Header{
  428. %s Version: %v
  429. %s ChainID: %v
  430. %s Height: %v
  431. %s Time: %v
  432. %s LastBlockID: %v
  433. %s LastCommit: %v
  434. %s Data: %v
  435. %s Validators: %v
  436. %s NextValidators: %v
  437. %s App: %v
  438. %s Consensus: %v
  439. %s Results: %v
  440. %s Evidence: %v
  441. %s Proposer: %v
  442. %s}#%v`,
  443. indent, h.Version,
  444. indent, h.ChainID,
  445. indent, h.Height,
  446. indent, h.Time,
  447. indent, h.LastBlockID,
  448. indent, h.LastCommitHash,
  449. indent, h.DataHash,
  450. indent, h.ValidatorsHash,
  451. indent, h.NextValidatorsHash,
  452. indent, h.AppHash,
  453. indent, h.ConsensusHash,
  454. indent, h.LastResultsHash,
  455. indent, h.EvidenceHash,
  456. indent, h.ProposerAddress,
  457. indent, h.Hash(),
  458. )
  459. }
  460. // ToProto converts Header to protobuf
  461. func (h *Header) ToProto() *tmproto.Header {
  462. if h == nil {
  463. return nil
  464. }
  465. return &tmproto.Header{
  466. Version: h.Version.ToProto(),
  467. ChainID: h.ChainID,
  468. Height: h.Height,
  469. Time: h.Time,
  470. LastBlockId: h.LastBlockID.ToProto(),
  471. ValidatorsHash: h.ValidatorsHash,
  472. NextValidatorsHash: h.NextValidatorsHash,
  473. ConsensusHash: h.ConsensusHash,
  474. AppHash: h.AppHash,
  475. DataHash: h.DataHash,
  476. EvidenceHash: h.EvidenceHash,
  477. LastResultsHash: h.LastResultsHash,
  478. LastCommitHash: h.LastCommitHash,
  479. ProposerAddress: h.ProposerAddress,
  480. }
  481. }
  482. // FromProto sets a protobuf Header to the given pointer.
  483. // It returns an error if the header is invalid.
  484. func HeaderFromProto(ph *tmproto.Header) (Header, error) {
  485. if ph == nil {
  486. return Header{}, errors.New("nil Header")
  487. }
  488. h := new(Header)
  489. bi, err := BlockIDFromProto(&ph.LastBlockId)
  490. if err != nil {
  491. return Header{}, err
  492. }
  493. h.Version = version.Consensus{Block: ph.Version.Block, App: ph.Version.App}
  494. h.ChainID = ph.ChainID
  495. h.Height = ph.Height
  496. h.Time = ph.Time
  497. h.Height = ph.Height
  498. h.LastBlockID = *bi
  499. h.ValidatorsHash = ph.ValidatorsHash
  500. h.NextValidatorsHash = ph.NextValidatorsHash
  501. h.ConsensusHash = ph.ConsensusHash
  502. h.AppHash = ph.AppHash
  503. h.DataHash = ph.DataHash
  504. h.EvidenceHash = ph.EvidenceHash
  505. h.LastResultsHash = ph.LastResultsHash
  506. h.LastCommitHash = ph.LastCommitHash
  507. h.ProposerAddress = ph.ProposerAddress
  508. return *h, h.ValidateBasic()
  509. }
  510. //-------------------------------------
  511. // BlockIDFlag indicates which BlockID the signature is for.
  512. type BlockIDFlag byte
  513. const (
  514. // BlockIDFlagAbsent - no vote was received from a validator.
  515. BlockIDFlagAbsent BlockIDFlag = iota + 1
  516. // BlockIDFlagCommit - voted for the Commit.BlockID.
  517. BlockIDFlagCommit
  518. // BlockIDFlagNil - voted for nil.
  519. BlockIDFlagNil
  520. )
  521. const (
  522. // Max size of commit without any commitSigs -> 82 for BlockID, 8 for Height, 4 for Round.
  523. MaxCommitOverheadBytes int64 = 94
  524. // Commit sig size is made up of 64 bytes for the signature, 20 bytes for the address,
  525. // 1 byte for the flag and 14 bytes for the timestamp
  526. MaxCommitSigBytes int64 = 109
  527. )
  528. // CommitSig is a part of the Vote included in a Commit.
  529. type CommitSig struct {
  530. BlockIDFlag BlockIDFlag `json:"block_id_flag"`
  531. ValidatorAddress Address `json:"validator_address"`
  532. Timestamp time.Time `json:"timestamp"`
  533. Signature []byte `json:"signature"`
  534. VoteExtension VoteExtensionToSign `json:"vote_extension"`
  535. }
  536. // NewCommitSigForBlock returns new CommitSig with BlockIDFlagCommit.
  537. func NewCommitSigForBlock(signature []byte, valAddr Address, ts time.Time, ext VoteExtensionToSign) CommitSig {
  538. return CommitSig{
  539. BlockIDFlag: BlockIDFlagCommit,
  540. ValidatorAddress: valAddr,
  541. Timestamp: ts,
  542. Signature: signature,
  543. VoteExtension: ext,
  544. }
  545. }
  546. func MaxCommitBytes(valCount int) int64 {
  547. // From the repeated commit sig field
  548. var protoEncodingOverhead int64 = 2
  549. return MaxCommitOverheadBytes + ((MaxCommitSigBytes + protoEncodingOverhead) * int64(valCount))
  550. }
  551. // NewCommitSigAbsent returns new CommitSig with BlockIDFlagAbsent. Other
  552. // fields are all empty.
  553. func NewCommitSigAbsent() CommitSig {
  554. return CommitSig{
  555. BlockIDFlag: BlockIDFlagAbsent,
  556. }
  557. }
  558. // ForBlock returns true if CommitSig is for the block.
  559. func (cs CommitSig) ForBlock() bool {
  560. return cs.BlockIDFlag == BlockIDFlagCommit
  561. }
  562. // Absent returns true if CommitSig is absent.
  563. func (cs CommitSig) Absent() bool {
  564. return cs.BlockIDFlag == BlockIDFlagAbsent
  565. }
  566. // CommitSig returns a string representation of CommitSig.
  567. //
  568. // 1. first 6 bytes of signature
  569. // 2. first 6 bytes of validator address
  570. // 3. block ID flag
  571. // 4. first 6 bytes of the vote extension
  572. // 5. timestamp
  573. func (cs CommitSig) String() string {
  574. return fmt.Sprintf("CommitSig{%X by %X on %v with %X @ %s}",
  575. tmbytes.Fingerprint(cs.Signature),
  576. tmbytes.Fingerprint(cs.ValidatorAddress),
  577. cs.BlockIDFlag,
  578. tmbytes.Fingerprint(cs.VoteExtension.BytesPacked()),
  579. CanonicalTime(cs.Timestamp))
  580. }
  581. // BlockID returns the Commit's BlockID if CommitSig indicates signing,
  582. // otherwise - empty BlockID.
  583. func (cs CommitSig) BlockID(commitBlockID BlockID) BlockID {
  584. var blockID BlockID
  585. switch cs.BlockIDFlag {
  586. case BlockIDFlagAbsent:
  587. blockID = BlockID{}
  588. case BlockIDFlagCommit:
  589. blockID = commitBlockID
  590. case BlockIDFlagNil:
  591. blockID = BlockID{}
  592. default:
  593. panic(fmt.Sprintf("Unknown BlockIDFlag: %v", cs.BlockIDFlag))
  594. }
  595. return blockID
  596. }
  597. // ValidateBasic performs basic validation.
  598. func (cs CommitSig) ValidateBasic() error {
  599. switch cs.BlockIDFlag {
  600. case BlockIDFlagAbsent:
  601. case BlockIDFlagCommit:
  602. case BlockIDFlagNil:
  603. default:
  604. return fmt.Errorf("unknown BlockIDFlag: %v", cs.BlockIDFlag)
  605. }
  606. switch cs.BlockIDFlag {
  607. case BlockIDFlagAbsent:
  608. if len(cs.ValidatorAddress) != 0 {
  609. return errors.New("validator address is present")
  610. }
  611. if !cs.Timestamp.IsZero() {
  612. return errors.New("time is present")
  613. }
  614. if len(cs.Signature) != 0 {
  615. return errors.New("signature is present")
  616. }
  617. default:
  618. if len(cs.ValidatorAddress) != crypto.AddressSize {
  619. return fmt.Errorf("expected ValidatorAddress size to be %d bytes, got %d bytes",
  620. crypto.AddressSize,
  621. len(cs.ValidatorAddress),
  622. )
  623. }
  624. // NOTE: Timestamp validation is subtle and handled elsewhere.
  625. if len(cs.Signature) == 0 {
  626. return errors.New("signature is missing")
  627. }
  628. if len(cs.Signature) > MaxSignatureSize {
  629. return fmt.Errorf("signature is too big (max: %d)", MaxSignatureSize)
  630. }
  631. }
  632. return nil
  633. }
  634. // ToProto converts CommitSig to protobuf
  635. func (cs *CommitSig) ToProto() *tmproto.CommitSig {
  636. if cs == nil {
  637. return nil
  638. }
  639. return &tmproto.CommitSig{
  640. BlockIdFlag: tmproto.BlockIDFlag(cs.BlockIDFlag),
  641. ValidatorAddress: cs.ValidatorAddress,
  642. Timestamp: cs.Timestamp,
  643. Signature: cs.Signature,
  644. VoteExtension: cs.VoteExtension.ToProto(),
  645. }
  646. }
  647. // FromProto sets a protobuf CommitSig to the given pointer.
  648. // It returns an error if the CommitSig is invalid.
  649. func (cs *CommitSig) FromProto(csp tmproto.CommitSig) error {
  650. cs.BlockIDFlag = BlockIDFlag(csp.BlockIdFlag)
  651. cs.ValidatorAddress = csp.ValidatorAddress
  652. cs.Timestamp = csp.Timestamp
  653. cs.Signature = csp.Signature
  654. cs.VoteExtension = VoteExtensionToSignFromProto(csp.VoteExtension)
  655. return cs.ValidateBasic()
  656. }
  657. //-------------------------------------
  658. // Commit contains the evidence that a block was committed by a set of validators.
  659. // NOTE: Commit is empty for height 1, but never nil.
  660. type Commit struct {
  661. // NOTE: The signatures are in order of address to preserve the bonded
  662. // ValidatorSet order.
  663. // Any peer with a block can gossip signatures by index with a peer without
  664. // recalculating the active ValidatorSet.
  665. Height int64 `json:"height,string"`
  666. Round int32 `json:"round"`
  667. BlockID BlockID `json:"block_id"`
  668. Signatures []CommitSig `json:"signatures"`
  669. // Memoized in first call to corresponding method.
  670. // NOTE: can't memoize in constructor because constructor isn't used for
  671. // unmarshaling.
  672. hash tmbytes.HexBytes
  673. bitArray *bits.BitArray
  674. }
  675. // NewCommit returns a new Commit.
  676. func NewCommit(height int64, round int32, blockID BlockID, commitSigs []CommitSig) *Commit {
  677. return &Commit{
  678. Height: height,
  679. Round: round,
  680. BlockID: blockID,
  681. Signatures: commitSigs,
  682. }
  683. }
  684. // CommitToVoteSet constructs a VoteSet from the Commit and validator set.
  685. // Panics if signatures from the commit can't be added to the voteset.
  686. // Inverse of VoteSet.MakeCommit().
  687. func CommitToVoteSet(chainID string, commit *Commit, vals *ValidatorSet) *VoteSet {
  688. voteSet := NewVoteSet(chainID, commit.Height, commit.Round, tmproto.PrecommitType, vals)
  689. for idx, commitSig := range commit.Signatures {
  690. if commitSig.Absent() {
  691. continue // OK, some precommits can be missing.
  692. }
  693. added, err := voteSet.AddVote(commit.GetVote(int32(idx)))
  694. if !added || err != nil {
  695. panic(fmt.Errorf("failed to reconstruct LastCommit: %w", err))
  696. }
  697. }
  698. return voteSet
  699. }
  700. // GetVote converts the CommitSig for the given valIdx to a Vote.
  701. // Returns nil if the precommit at valIdx is nil.
  702. // Panics if valIdx >= commit.Size().
  703. func (commit *Commit) GetVote(valIdx int32) *Vote {
  704. commitSig := commit.Signatures[valIdx]
  705. return &Vote{
  706. Type: tmproto.PrecommitType,
  707. Height: commit.Height,
  708. Round: commit.Round,
  709. BlockID: commitSig.BlockID(commit.BlockID),
  710. Timestamp: commitSig.Timestamp,
  711. ValidatorAddress: commitSig.ValidatorAddress,
  712. ValidatorIndex: valIdx,
  713. Signature: commitSig.Signature,
  714. VoteExtension: commitSig.VoteExtension.ToVoteExtension(),
  715. }
  716. }
  717. // VoteSignBytes returns the bytes of the Vote corresponding to valIdx for
  718. // signing.
  719. //
  720. // The only unique part is the Timestamp - all other fields signed over are
  721. // otherwise the same for all validators.
  722. //
  723. // Panics if valIdx >= commit.Size().
  724. //
  725. // See VoteSignBytes
  726. func (commit *Commit) VoteSignBytes(chainID string, valIdx int32) []byte {
  727. v := commit.GetVote(valIdx).ToProto()
  728. return VoteSignBytes(chainID, v)
  729. }
  730. // Type returns the vote type of the commit, which is always VoteTypePrecommit
  731. // Implements VoteSetReader.
  732. func (commit *Commit) Type() byte {
  733. return byte(tmproto.PrecommitType)
  734. }
  735. // GetHeight returns height of the commit.
  736. // Implements VoteSetReader.
  737. func (commit *Commit) GetHeight() int64 {
  738. return commit.Height
  739. }
  740. // GetRound returns height of the commit.
  741. // Implements VoteSetReader.
  742. func (commit *Commit) GetRound() int32 {
  743. return commit.Round
  744. }
  745. // Size returns the number of signatures in the commit.
  746. // Implements VoteSetReader.
  747. func (commit *Commit) Size() int {
  748. if commit == nil {
  749. return 0
  750. }
  751. return len(commit.Signatures)
  752. }
  753. // BitArray returns a BitArray of which validators voted for BlockID or nil in this commit.
  754. // Implements VoteSetReader.
  755. func (commit *Commit) BitArray() *bits.BitArray {
  756. if commit.bitArray == nil {
  757. commit.bitArray = bits.NewBitArray(len(commit.Signatures))
  758. for i, commitSig := range commit.Signatures {
  759. // TODO: need to check the BlockID otherwise we could be counting conflicts,
  760. // not just the one with +2/3 !
  761. commit.bitArray.SetIndex(i, !commitSig.Absent())
  762. }
  763. }
  764. return commit.bitArray
  765. }
  766. // GetByIndex returns the vote corresponding to a given validator index.
  767. // Panics if `index >= commit.Size()`.
  768. // Implements VoteSetReader.
  769. func (commit *Commit) GetByIndex(valIdx int32) *Vote {
  770. return commit.GetVote(valIdx)
  771. }
  772. // IsCommit returns true if there is at least one signature.
  773. // Implements VoteSetReader.
  774. func (commit *Commit) IsCommit() bool {
  775. return len(commit.Signatures) != 0
  776. }
  777. // ValidateBasic performs basic validation that doesn't involve state data.
  778. // Does not actually check the cryptographic signatures.
  779. func (commit *Commit) ValidateBasic() error {
  780. if commit.Height < 0 {
  781. return errors.New("negative Height")
  782. }
  783. if commit.Round < 0 {
  784. return errors.New("negative Round")
  785. }
  786. if commit.Height >= 1 {
  787. if commit.BlockID.IsNil() {
  788. return errors.New("commit cannot be for nil block")
  789. }
  790. if len(commit.Signatures) == 0 {
  791. return errors.New("no signatures in commit")
  792. }
  793. for i, commitSig := range commit.Signatures {
  794. if err := commitSig.ValidateBasic(); err != nil {
  795. return fmt.Errorf("wrong CommitSig #%d: %v", i, err)
  796. }
  797. }
  798. }
  799. return nil
  800. }
  801. // Hash returns the hash of the commit
  802. func (commit *Commit) Hash() tmbytes.HexBytes {
  803. if commit == nil {
  804. return nil
  805. }
  806. if commit.hash == nil {
  807. bs := make([][]byte, len(commit.Signatures))
  808. for i, commitSig := range commit.Signatures {
  809. pbcs := commitSig.ToProto()
  810. bz, err := pbcs.Marshal()
  811. if err != nil {
  812. panic(err)
  813. }
  814. bs[i] = bz
  815. }
  816. commit.hash = merkle.HashFromByteSlices(bs)
  817. }
  818. return commit.hash
  819. }
  820. // StringIndented returns a string representation of the commit.
  821. func (commit *Commit) StringIndented(indent string) string {
  822. if commit == nil {
  823. return "nil-Commit"
  824. }
  825. commitSigStrings := make([]string, len(commit.Signatures))
  826. for i, commitSig := range commit.Signatures {
  827. commitSigStrings[i] = commitSig.String()
  828. }
  829. return fmt.Sprintf(`Commit{
  830. %s Height: %d
  831. %s Round: %d
  832. %s BlockID: %v
  833. %s Signatures:
  834. %s %v
  835. %s}#%v`,
  836. indent, commit.Height,
  837. indent, commit.Round,
  838. indent, commit.BlockID,
  839. indent,
  840. indent, strings.Join(commitSigStrings, "\n"+indent+" "),
  841. indent, commit.hash)
  842. }
  843. // ToProto converts Commit to protobuf
  844. func (commit *Commit) ToProto() *tmproto.Commit {
  845. if commit == nil {
  846. return nil
  847. }
  848. c := new(tmproto.Commit)
  849. sigs := make([]tmproto.CommitSig, len(commit.Signatures))
  850. for i := range commit.Signatures {
  851. sigs[i] = *commit.Signatures[i].ToProto()
  852. }
  853. c.Signatures = sigs
  854. c.Height = commit.Height
  855. c.Round = commit.Round
  856. c.BlockID = commit.BlockID.ToProto()
  857. return c
  858. }
  859. // FromProto sets a protobuf Commit to the given pointer.
  860. // It returns an error if the commit is invalid.
  861. func CommitFromProto(cp *tmproto.Commit) (*Commit, error) {
  862. if cp == nil {
  863. return nil, errors.New("nil Commit")
  864. }
  865. var (
  866. commit = new(Commit)
  867. )
  868. bi, err := BlockIDFromProto(&cp.BlockID)
  869. if err != nil {
  870. return nil, err
  871. }
  872. sigs := make([]CommitSig, len(cp.Signatures))
  873. for i := range cp.Signatures {
  874. if err := sigs[i].FromProto(cp.Signatures[i]); err != nil {
  875. return nil, err
  876. }
  877. }
  878. commit.Signatures = sigs
  879. commit.Height = cp.Height
  880. commit.Round = cp.Round
  881. commit.BlockID = *bi
  882. return commit, commit.ValidateBasic()
  883. }
  884. //-----------------------------------------------------------------------------
  885. // Data contains the set of transactions included in the block
  886. type Data struct {
  887. // Txs that will be applied by state @ block.Height+1.
  888. // NOTE: not all txs here are valid. We're just agreeing on the order first.
  889. // This means that block.AppHash does not include these txs.
  890. Txs Txs `json:"txs"`
  891. // Volatile
  892. hash tmbytes.HexBytes
  893. }
  894. // Hash returns the hash of the data
  895. func (data *Data) Hash() tmbytes.HexBytes {
  896. if data == nil {
  897. return (Txs{}).Hash()
  898. }
  899. if data.hash == nil {
  900. data.hash = data.Txs.Hash() // NOTE: leaves of merkle tree are TxIDs
  901. }
  902. return data.hash
  903. }
  904. // StringIndented returns an indented string representation of the transactions.
  905. func (data *Data) StringIndented(indent string) string {
  906. if data == nil {
  907. return "nil-Data"
  908. }
  909. txStrings := make([]string, tmmath.MinInt(len(data.Txs), 21))
  910. for i, tx := range data.Txs {
  911. if i == 20 {
  912. txStrings[i] = fmt.Sprintf("... (%v total)", len(data.Txs))
  913. break
  914. }
  915. txStrings[i] = fmt.Sprintf("%X (%d bytes)", tx.Hash(), len(tx))
  916. }
  917. return fmt.Sprintf(`Data{
  918. %s %v
  919. %s}#%v`,
  920. indent, strings.Join(txStrings, "\n"+indent+" "),
  921. indent, data.hash)
  922. }
  923. // ToProto converts Data to protobuf
  924. func (data *Data) ToProto() tmproto.Data {
  925. tp := new(tmproto.Data)
  926. if len(data.Txs) > 0 {
  927. txBzs := make([][]byte, len(data.Txs))
  928. for i := range data.Txs {
  929. txBzs[i] = data.Txs[i]
  930. }
  931. tp.Txs = txBzs
  932. }
  933. return *tp
  934. }
  935. // DataFromProto takes a protobuf representation of Data &
  936. // returns the native type.
  937. func DataFromProto(dp *tmproto.Data) (Data, error) {
  938. if dp == nil {
  939. return Data{}, errors.New("nil data")
  940. }
  941. data := new(Data)
  942. if len(dp.Txs) > 0 {
  943. txBzs := make(Txs, len(dp.Txs))
  944. for i := range dp.Txs {
  945. txBzs[i] = Tx(dp.Txs[i])
  946. }
  947. data.Txs = txBzs
  948. } else {
  949. data.Txs = Txs{}
  950. }
  951. return *data, nil
  952. }
  953. //--------------------------------------------------------------------------------
  954. // BlockID
  955. type BlockID struct {
  956. Hash tmbytes.HexBytes `json:"hash"`
  957. PartSetHeader PartSetHeader `json:"parts"`
  958. }
  959. // Equals returns true if the BlockID matches the given BlockID
  960. func (blockID BlockID) Equals(other BlockID) bool {
  961. return bytes.Equal(blockID.Hash, other.Hash) &&
  962. blockID.PartSetHeader.Equals(other.PartSetHeader)
  963. }
  964. // Key returns a machine-readable string representation of the BlockID
  965. func (blockID BlockID) Key() string {
  966. pbph := blockID.PartSetHeader.ToProto()
  967. bz, err := pbph.Marshal()
  968. if err != nil {
  969. panic(err)
  970. }
  971. return fmt.Sprint(string(blockID.Hash), string(bz))
  972. }
  973. // ValidateBasic performs basic validation.
  974. func (blockID BlockID) ValidateBasic() error {
  975. // Hash can be empty in case of POLBlockID in Proposal.
  976. if err := ValidateHash(blockID.Hash); err != nil {
  977. return fmt.Errorf("wrong Hash: %w", err)
  978. }
  979. if err := blockID.PartSetHeader.ValidateBasic(); err != nil {
  980. return fmt.Errorf("wrong PartSetHeader: %w", err)
  981. }
  982. return nil
  983. }
  984. // IsNil returns true if this is the BlockID of a nil block.
  985. func (blockID BlockID) IsNil() bool {
  986. return len(blockID.Hash) == 0 &&
  987. blockID.PartSetHeader.IsZero()
  988. }
  989. // IsComplete returns true if this is a valid BlockID of a non-nil block.
  990. func (blockID BlockID) IsComplete() bool {
  991. return len(blockID.Hash) == tmhash.Size &&
  992. blockID.PartSetHeader.Total > 0 &&
  993. len(blockID.PartSetHeader.Hash) == tmhash.Size
  994. }
  995. // String returns a human readable string representation of the BlockID.
  996. //
  997. // 1. hash
  998. // 2. part set header
  999. //
  1000. // See PartSetHeader#String
  1001. func (blockID BlockID) String() string {
  1002. return fmt.Sprintf(`%v:%v`, blockID.Hash, blockID.PartSetHeader)
  1003. }
  1004. // ToProto converts BlockID to protobuf
  1005. func (blockID *BlockID) ToProto() tmproto.BlockID {
  1006. if blockID == nil {
  1007. return tmproto.BlockID{}
  1008. }
  1009. return tmproto.BlockID{
  1010. Hash: blockID.Hash,
  1011. PartSetHeader: blockID.PartSetHeader.ToProto(),
  1012. }
  1013. }
  1014. // FromProto sets a protobuf BlockID to the given pointer.
  1015. // It returns an error if the block id is invalid.
  1016. func BlockIDFromProto(bID *tmproto.BlockID) (*BlockID, error) {
  1017. if bID == nil {
  1018. return nil, errors.New("nil BlockID")
  1019. }
  1020. blockID := new(BlockID)
  1021. ph, err := PartSetHeaderFromProto(&bID.PartSetHeader)
  1022. if err != nil {
  1023. return nil, err
  1024. }
  1025. blockID.PartSetHeader = *ph
  1026. blockID.Hash = bID.Hash
  1027. return blockID, blockID.ValidateBasic()
  1028. }