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.

955 lines
27 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
7 years ago
10 years ago
10 years ago
7 years ago
7 years ago
10 years ago
11 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strings"
  6. "sync"
  7. "time"
  8. "github.com/pkg/errors"
  9. "github.com/tendermint/tendermint/crypto"
  10. "github.com/tendermint/tendermint/crypto/merkle"
  11. "github.com/tendermint/tendermint/crypto/tmhash"
  12. "github.com/tendermint/tendermint/libs/bits"
  13. tmbytes "github.com/tendermint/tendermint/libs/bytes"
  14. tmmath "github.com/tendermint/tendermint/libs/math"
  15. "github.com/tendermint/tendermint/version"
  16. )
  17. const (
  18. // MaxHeaderBytes is a maximum header size (including amino overhead).
  19. MaxHeaderBytes int64 = 632
  20. // MaxAminoOverheadForBlock - maximum amino overhead to encode a block (up to
  21. // MaxBlockSizeBytes in size) not including it's parts except Data.
  22. // This means it also excludes the overhead for individual transactions.
  23. // To compute individual transactions' overhead use types.ComputeAminoOverhead(tx types.Tx, fieldNum int).
  24. //
  25. // Uvarint length of MaxBlockSizeBytes: 4 bytes
  26. // 2 fields (2 embedded): 2 bytes
  27. // Uvarint length of Data.Txs: 4 bytes
  28. // Data.Txs field: 1 byte
  29. MaxAminoOverheadForBlock int64 = 11
  30. )
  31. // Block defines the atomic unit of a Tendermint blockchain.
  32. type Block struct {
  33. mtx sync.Mutex
  34. Header `json:"header"`
  35. Data `json:"data"`
  36. Evidence EvidenceData `json:"evidence"`
  37. LastCommit *Commit `json:"last_commit"`
  38. }
  39. // ValidateBasic performs basic validation that doesn't involve state data.
  40. // It checks the internal consistency of the block.
  41. // Further validation is done using state#ValidateBlock.
  42. func (b *Block) ValidateBasic() error {
  43. if b == nil {
  44. return errors.New("nil block")
  45. }
  46. b.mtx.Lock()
  47. defer b.mtx.Unlock()
  48. if err := b.Header.ValidateBasic(); err != nil {
  49. return fmt.Errorf("invalid header: %w", err)
  50. }
  51. // Validate the last commit and its hash.
  52. if b.Header.Height > 1 {
  53. if b.LastCommit == nil {
  54. return errors.New("nil LastCommit")
  55. }
  56. if err := b.LastCommit.ValidateBasic(); err != nil {
  57. return fmt.Errorf("wrong LastCommit: %v", err)
  58. }
  59. }
  60. if !bytes.Equal(b.LastCommitHash, b.LastCommit.Hash()) {
  61. return fmt.Errorf("wrong Header.LastCommitHash. Expected %v, got %v",
  62. b.LastCommit.Hash(),
  63. b.LastCommitHash,
  64. )
  65. }
  66. // NOTE: b.Data.Txs may be nil, but b.Data.Hash() still works fine.
  67. if !bytes.Equal(b.DataHash, b.Data.Hash()) {
  68. return fmt.Errorf(
  69. "wrong Header.DataHash. Expected %v, got %v",
  70. b.Data.Hash(),
  71. b.DataHash,
  72. )
  73. }
  74. // NOTE: b.Evidence.Evidence may be nil, but we're just looping.
  75. for i, ev := range b.Evidence.Evidence {
  76. if err := ev.ValidateBasic(); err != nil {
  77. return fmt.Errorf("invalid evidence (#%d): %v", i, err)
  78. }
  79. }
  80. if !bytes.Equal(b.EvidenceHash, b.Evidence.Hash()) {
  81. return fmt.Errorf("wrong Header.EvidenceHash. Expected %v, got %v",
  82. b.EvidenceHash,
  83. b.Evidence.Hash(),
  84. )
  85. }
  86. return nil
  87. }
  88. // fillHeader fills in any remaining header fields that are a function of the block data
  89. func (b *Block) fillHeader() {
  90. if b.LastCommitHash == nil {
  91. b.LastCommitHash = b.LastCommit.Hash()
  92. }
  93. if b.DataHash == nil {
  94. b.DataHash = b.Data.Hash()
  95. }
  96. if b.EvidenceHash == nil {
  97. b.EvidenceHash = b.Evidence.Hash()
  98. }
  99. }
  100. // Hash computes and returns the block hash.
  101. // If the block is incomplete, block hash is nil for safety.
  102. func (b *Block) Hash() tmbytes.HexBytes {
  103. if b == nil {
  104. return nil
  105. }
  106. b.mtx.Lock()
  107. defer b.mtx.Unlock()
  108. if b.LastCommit == nil {
  109. return nil
  110. }
  111. b.fillHeader()
  112. return b.Header.Hash()
  113. }
  114. // MakePartSet returns a PartSet containing parts of a serialized block.
  115. // This is the form in which the block is gossipped to peers.
  116. // CONTRACT: partSize is greater than zero.
  117. func (b *Block) MakePartSet(partSize int) *PartSet {
  118. if b == nil {
  119. return nil
  120. }
  121. b.mtx.Lock()
  122. defer b.mtx.Unlock()
  123. // We prefix the byte length, so that unmarshaling
  124. // can easily happen via a reader.
  125. bz, err := cdc.MarshalBinaryLengthPrefixed(b)
  126. if err != nil {
  127. panic(err)
  128. }
  129. return NewPartSetFromData(bz, partSize)
  130. }
  131. // HashesTo is a convenience function that checks if a block hashes to the given argument.
  132. // Returns false if the block is nil or the hash is empty.
  133. func (b *Block) HashesTo(hash []byte) bool {
  134. if len(hash) == 0 {
  135. return false
  136. }
  137. if b == nil {
  138. return false
  139. }
  140. return bytes.Equal(b.Hash(), hash)
  141. }
  142. // Size returns size of the block in bytes.
  143. func (b *Block) Size() int {
  144. bz, err := cdc.MarshalBinaryBare(b)
  145. if err != nil {
  146. return 0
  147. }
  148. return len(bz)
  149. }
  150. // String returns a string representation of the block
  151. func (b *Block) String() string {
  152. return b.StringIndented("")
  153. }
  154. // StringIndented returns a string representation of the block
  155. func (b *Block) StringIndented(indent string) string {
  156. if b == nil {
  157. return "nil-Block"
  158. }
  159. return fmt.Sprintf(`Block{
  160. %s %v
  161. %s %v
  162. %s %v
  163. %s %v
  164. %s}#%v`,
  165. indent, b.Header.StringIndented(indent+" "),
  166. indent, b.Data.StringIndented(indent+" "),
  167. indent, b.Evidence.StringIndented(indent+" "),
  168. indent, b.LastCommit.StringIndented(indent+" "),
  169. indent, b.Hash())
  170. }
  171. // StringShort returns a shortened string representation of the block
  172. func (b *Block) StringShort() string {
  173. if b == nil {
  174. return "nil-Block"
  175. }
  176. return fmt.Sprintf("Block#%v", b.Hash())
  177. }
  178. //-----------------------------------------------------------
  179. // These methods are for Protobuf Compatibility
  180. // Marshal returns the amino encoding.
  181. func (b *Block) Marshal() ([]byte, error) {
  182. return cdc.MarshalBinaryBare(b)
  183. }
  184. // MarshalTo calls Marshal and copies to the given buffer.
  185. func (b *Block) MarshalTo(data []byte) (int, error) {
  186. bs, err := b.Marshal()
  187. if err != nil {
  188. return -1, err
  189. }
  190. return copy(data, bs), nil
  191. }
  192. // Unmarshal deserializes from amino encoded form.
  193. func (b *Block) Unmarshal(bs []byte) error {
  194. return cdc.UnmarshalBinaryBare(bs, b)
  195. }
  196. //-----------------------------------------------------------------------------
  197. // MaxDataBytes returns the maximum size of block's data.
  198. //
  199. // XXX: Panics on negative result.
  200. func MaxDataBytes(maxBytes int64, valsCount, evidenceCount int) int64 {
  201. maxDataBytes := maxBytes -
  202. MaxAminoOverheadForBlock -
  203. MaxHeaderBytes -
  204. int64(valsCount)*MaxVoteBytes -
  205. int64(evidenceCount)*MaxEvidenceBytes
  206. if maxDataBytes < 0 {
  207. panic(fmt.Sprintf(
  208. "Negative MaxDataBytes. Block.MaxBytes=%d is too small to accommodate header&lastCommit&evidence=%d",
  209. maxBytes,
  210. -(maxDataBytes - maxBytes),
  211. ))
  212. }
  213. return maxDataBytes
  214. }
  215. // MaxDataBytesUnknownEvidence returns the maximum size of block's data when
  216. // evidence count is unknown. MaxEvidencePerBlock will be used for the size
  217. // of evidence.
  218. //
  219. // XXX: Panics on negative result.
  220. func MaxDataBytesUnknownEvidence(maxBytes int64, valsCount int) int64 {
  221. _, maxEvidenceBytes := MaxEvidencePerBlock(maxBytes)
  222. maxDataBytes := maxBytes -
  223. MaxAminoOverheadForBlock -
  224. MaxHeaderBytes -
  225. int64(valsCount)*MaxVoteBytes -
  226. maxEvidenceBytes
  227. if maxDataBytes < 0 {
  228. panic(fmt.Sprintf(
  229. "Negative MaxDataBytesUnknownEvidence. Block.MaxBytes=%d is too small to accommodate header&lastCommit&evidence=%d",
  230. maxBytes,
  231. -(maxDataBytes - maxBytes),
  232. ))
  233. }
  234. return maxDataBytes
  235. }
  236. //-----------------------------------------------------------------------------
  237. // Header defines the structure of a Tendermint block header.
  238. // NOTE: changes to the Header should be duplicated in:
  239. // - header.Hash()
  240. // - abci.Header
  241. // - https://github.com/tendermint/spec/blob/master/spec/blockchain/blockchain.md
  242. type Header struct {
  243. // basic block info
  244. Version version.Consensus `json:"version"`
  245. ChainID string `json:"chain_id"`
  246. Height int64 `json:"height"`
  247. Time time.Time `json:"time"`
  248. // prev block info
  249. LastBlockID BlockID `json:"last_block_id"`
  250. // hashes of block data
  251. LastCommitHash tmbytes.HexBytes `json:"last_commit_hash"` // commit from validators from the last block
  252. DataHash tmbytes.HexBytes `json:"data_hash"` // transactions
  253. // hashes from the app output from the prev block
  254. ValidatorsHash tmbytes.HexBytes `json:"validators_hash"` // validators for the current block
  255. NextValidatorsHash tmbytes.HexBytes `json:"next_validators_hash"` // validators for the next block
  256. ConsensusHash tmbytes.HexBytes `json:"consensus_hash"` // consensus params for current block
  257. AppHash tmbytes.HexBytes `json:"app_hash"` // state after txs from the previous block
  258. // root hash of all results from the txs from the previous block
  259. LastResultsHash tmbytes.HexBytes `json:"last_results_hash"`
  260. // consensus info
  261. EvidenceHash tmbytes.HexBytes `json:"evidence_hash"` // evidence included in the block
  262. ProposerAddress Address `json:"proposer_address"` // original proposer of the block
  263. }
  264. // Populate the Header with state-derived data.
  265. // Call this after MakeBlock to complete the Header.
  266. func (h *Header) Populate(
  267. version version.Consensus, chainID string,
  268. timestamp time.Time, lastBlockID BlockID,
  269. valHash, nextValHash []byte,
  270. consensusHash, appHash, lastResultsHash []byte,
  271. proposerAddress Address,
  272. ) {
  273. h.Version = version
  274. h.ChainID = chainID
  275. h.Time = timestamp
  276. h.LastBlockID = lastBlockID
  277. h.ValidatorsHash = valHash
  278. h.NextValidatorsHash = nextValHash
  279. h.ConsensusHash = consensusHash
  280. h.AppHash = appHash
  281. h.LastResultsHash = lastResultsHash
  282. h.ProposerAddress = proposerAddress
  283. }
  284. // ValidateBasic performs stateless validation on a Header returning an error
  285. // if any validation fails.
  286. //
  287. // NOTE: Timestamp validation is subtle and handled elsewhere.
  288. func (h Header) ValidateBasic() error {
  289. if len(h.ChainID) > MaxChainIDLen {
  290. return fmt.Errorf("chainID is too long; got: %d, max: %d", len(h.ChainID), MaxChainIDLen)
  291. }
  292. if h.Height < 0 {
  293. return errors.New("negative Height")
  294. } else if h.Height == 0 {
  295. return errors.New("zero Height")
  296. }
  297. if err := h.LastBlockID.ValidateBasic(); err != nil {
  298. return fmt.Errorf("wrong LastBlockID: %w", err)
  299. }
  300. if err := ValidateHash(h.LastCommitHash); err != nil {
  301. return fmt.Errorf("wrong LastCommitHash: %v", err)
  302. }
  303. if err := ValidateHash(h.DataHash); err != nil {
  304. return fmt.Errorf("wrong DataHash: %v", err)
  305. }
  306. if err := ValidateHash(h.EvidenceHash); err != nil {
  307. return fmt.Errorf("wrong EvidenceHash: %v", err)
  308. }
  309. if len(h.ProposerAddress) != crypto.AddressSize {
  310. return fmt.Errorf(
  311. "invalid ProposerAddress length; got: %d, expected: %d",
  312. len(h.ProposerAddress), crypto.AddressSize,
  313. )
  314. }
  315. // Basic validation of hashes related to application data.
  316. // Will validate fully against state in state#ValidateBlock.
  317. if err := ValidateHash(h.ValidatorsHash); err != nil {
  318. return fmt.Errorf("wrong ValidatorsHash: %v", err)
  319. }
  320. if err := ValidateHash(h.NextValidatorsHash); err != nil {
  321. return fmt.Errorf("wrong NextValidatorsHash: %v", err)
  322. }
  323. if err := ValidateHash(h.ConsensusHash); err != nil {
  324. return fmt.Errorf("wrong ConsensusHash: %v", err)
  325. }
  326. // NOTE: AppHash is arbitrary length
  327. if err := ValidateHash(h.LastResultsHash); err != nil {
  328. return fmt.Errorf("wrong LastResultsHash: %v", err)
  329. }
  330. return nil
  331. }
  332. // Hash returns the hash of the header.
  333. // It computes a Merkle tree from the header fields
  334. // ordered as they appear in the Header.
  335. // Returns nil if ValidatorHash is missing,
  336. // since a Header is not valid unless there is
  337. // a ValidatorsHash (corresponding to the validator set).
  338. func (h *Header) Hash() tmbytes.HexBytes {
  339. if h == nil || len(h.ValidatorsHash) == 0 {
  340. return nil
  341. }
  342. return merkle.SimpleHashFromByteSlices([][]byte{
  343. cdcEncode(h.Version),
  344. cdcEncode(h.ChainID),
  345. cdcEncode(h.Height),
  346. cdcEncode(h.Time),
  347. cdcEncode(h.LastBlockID),
  348. cdcEncode(h.LastCommitHash),
  349. cdcEncode(h.DataHash),
  350. cdcEncode(h.ValidatorsHash),
  351. cdcEncode(h.NextValidatorsHash),
  352. cdcEncode(h.ConsensusHash),
  353. cdcEncode(h.AppHash),
  354. cdcEncode(h.LastResultsHash),
  355. cdcEncode(h.EvidenceHash),
  356. cdcEncode(h.ProposerAddress),
  357. })
  358. }
  359. // StringIndented returns a string representation of the header
  360. func (h *Header) StringIndented(indent string) string {
  361. if h == nil {
  362. return "nil-Header"
  363. }
  364. return fmt.Sprintf(`Header{
  365. %s Version: %v
  366. %s ChainID: %v
  367. %s Height: %v
  368. %s Time: %v
  369. %s LastBlockID: %v
  370. %s LastCommit: %v
  371. %s Data: %v
  372. %s Validators: %v
  373. %s NextValidators: %v
  374. %s App: %v
  375. %s Consensus: %v
  376. %s Results: %v
  377. %s Evidence: %v
  378. %s Proposer: %v
  379. %s}#%v`,
  380. indent, h.Version,
  381. indent, h.ChainID,
  382. indent, h.Height,
  383. indent, h.Time,
  384. indent, h.LastBlockID,
  385. indent, h.LastCommitHash,
  386. indent, h.DataHash,
  387. indent, h.ValidatorsHash,
  388. indent, h.NextValidatorsHash,
  389. indent, h.AppHash,
  390. indent, h.ConsensusHash,
  391. indent, h.LastResultsHash,
  392. indent, h.EvidenceHash,
  393. indent, h.ProposerAddress,
  394. indent, h.Hash())
  395. }
  396. //-------------------------------------
  397. // BlockIDFlag indicates which BlockID the signature is for.
  398. type BlockIDFlag byte
  399. const (
  400. // BlockIDFlagAbsent - no vote was received from a validator.
  401. BlockIDFlagAbsent BlockIDFlag = iota + 1
  402. // BlockIDFlagCommit - voted for the Commit.BlockID.
  403. BlockIDFlagCommit
  404. // BlockIDFlagNil - voted for nil.
  405. BlockIDFlagNil
  406. )
  407. // CommitSig is a part of the Vote included in a Commit.
  408. type CommitSig struct {
  409. BlockIDFlag BlockIDFlag `json:"block_id_flag"`
  410. ValidatorAddress Address `json:"validator_address"`
  411. Timestamp time.Time `json:"timestamp"`
  412. Signature []byte `json:"signature"`
  413. }
  414. // NewCommitSigForBlock returns new CommitSig with BlockIDFlagCommit.
  415. func NewCommitSigForBlock(signature []byte, valAddr Address, ts time.Time) CommitSig {
  416. return CommitSig{
  417. BlockIDFlag: BlockIDFlagCommit,
  418. ValidatorAddress: valAddr,
  419. Timestamp: ts,
  420. Signature: signature,
  421. }
  422. }
  423. // ForBlock returns true if CommitSig is for the block.
  424. func (cs CommitSig) ForBlock() bool {
  425. return cs.BlockIDFlag == BlockIDFlagCommit
  426. }
  427. // NewCommitSigAbsent returns new CommitSig with BlockIDFlagAbsent. Other
  428. // fields are all empty.
  429. func NewCommitSigAbsent() CommitSig {
  430. return CommitSig{
  431. BlockIDFlag: BlockIDFlagAbsent,
  432. }
  433. }
  434. // Absent returns true if CommitSig is absent.
  435. func (cs CommitSig) Absent() bool {
  436. return cs.BlockIDFlag == BlockIDFlagAbsent
  437. }
  438. func (cs CommitSig) String() string {
  439. return fmt.Sprintf("CommitSig{%X by %X on %v @ %s}",
  440. tmbytes.Fingerprint(cs.Signature),
  441. tmbytes.Fingerprint(cs.ValidatorAddress),
  442. cs.BlockIDFlag,
  443. CanonicalTime(cs.Timestamp))
  444. }
  445. // BlockID returns the Commit's BlockID if CommitSig indicates signing,
  446. // otherwise - empty BlockID.
  447. func (cs CommitSig) BlockID(commitBlockID BlockID) BlockID {
  448. var blockID BlockID
  449. switch cs.BlockIDFlag {
  450. case BlockIDFlagAbsent:
  451. blockID = BlockID{}
  452. case BlockIDFlagCommit:
  453. blockID = commitBlockID
  454. case BlockIDFlagNil:
  455. blockID = BlockID{}
  456. default:
  457. panic(fmt.Sprintf("Unknown BlockIDFlag: %v", cs.BlockIDFlag))
  458. }
  459. return blockID
  460. }
  461. // ValidateBasic performs basic validation.
  462. func (cs CommitSig) ValidateBasic() error {
  463. switch cs.BlockIDFlag {
  464. case BlockIDFlagAbsent:
  465. case BlockIDFlagCommit:
  466. case BlockIDFlagNil:
  467. default:
  468. return fmt.Errorf("unknown BlockIDFlag: %v", cs.BlockIDFlag)
  469. }
  470. switch cs.BlockIDFlag {
  471. case BlockIDFlagAbsent:
  472. if len(cs.ValidatorAddress) != 0 {
  473. return errors.New("validator address is present")
  474. }
  475. if !cs.Timestamp.IsZero() {
  476. return errors.New("time is present")
  477. }
  478. if len(cs.Signature) != 0 {
  479. return errors.New("signature is present")
  480. }
  481. default:
  482. if len(cs.ValidatorAddress) != crypto.AddressSize {
  483. return fmt.Errorf("expected ValidatorAddress size to be %d bytes, got %d bytes",
  484. crypto.AddressSize,
  485. len(cs.ValidatorAddress),
  486. )
  487. }
  488. // NOTE: Timestamp validation is subtle and handled elsewhere.
  489. if len(cs.Signature) == 0 {
  490. return errors.New("signature is missing")
  491. }
  492. if len(cs.Signature) > MaxSignatureSize {
  493. return fmt.Errorf("signature is too big (max: %d)", MaxSignatureSize)
  494. }
  495. }
  496. return nil
  497. }
  498. //-------------------------------------
  499. // Commit contains the evidence that a block was committed by a set of validators.
  500. // NOTE: Commit is empty for height 1, but never nil.
  501. type Commit struct {
  502. // NOTE: The signatures are in order of address to preserve the bonded
  503. // ValidatorSet order.
  504. // Any peer with a block can gossip signatures by index with a peer without
  505. // recalculating the active ValidatorSet.
  506. Height int64 `json:"height"`
  507. Round int `json:"round"`
  508. BlockID BlockID `json:"block_id"`
  509. Signatures []CommitSig `json:"signatures"`
  510. // Memoized in first call to corresponding method.
  511. // NOTE: can't memoize in constructor because constructor isn't used for
  512. // unmarshaling.
  513. hash tmbytes.HexBytes
  514. bitArray *bits.BitArray
  515. }
  516. // NewCommit returns a new Commit.
  517. func NewCommit(height int64, round int, blockID BlockID, commitSigs []CommitSig) *Commit {
  518. return &Commit{
  519. Height: height,
  520. Round: round,
  521. BlockID: blockID,
  522. Signatures: commitSigs,
  523. }
  524. }
  525. // CommitToVoteSet constructs a VoteSet from the Commit and validator set.
  526. // Panics if signatures from the commit can't be added to the voteset.
  527. // Inverse of VoteSet.MakeCommit().
  528. func CommitToVoteSet(chainID string, commit *Commit, vals *ValidatorSet) *VoteSet {
  529. voteSet := NewVoteSet(chainID, commit.Height, commit.Round, PrecommitType, vals)
  530. for idx, commitSig := range commit.Signatures {
  531. if commitSig.Absent() {
  532. continue // OK, some precommits can be missing.
  533. }
  534. added, err := voteSet.AddVote(commit.GetVote(idx))
  535. if !added || err != nil {
  536. panic(fmt.Sprintf("Failed to reconstruct LastCommit: %v", err))
  537. }
  538. }
  539. return voteSet
  540. }
  541. // GetVote converts the CommitSig for the given valIdx to a Vote.
  542. // Returns nil if the precommit at valIdx is nil.
  543. // Panics if valIdx >= commit.Size().
  544. func (commit *Commit) GetVote(valIdx int) *Vote {
  545. commitSig := commit.Signatures[valIdx]
  546. return &Vote{
  547. Type: PrecommitType,
  548. Height: commit.Height,
  549. Round: commit.Round,
  550. BlockID: commitSig.BlockID(commit.BlockID),
  551. Timestamp: commitSig.Timestamp,
  552. ValidatorAddress: commitSig.ValidatorAddress,
  553. ValidatorIndex: valIdx,
  554. Signature: commitSig.Signature,
  555. }
  556. }
  557. // VoteSignBytes constructs the SignBytes for the given CommitSig.
  558. // The only unique part of the SignBytes is the Timestamp - all other fields
  559. // signed over are otherwise the same for all validators.
  560. // Panics if valIdx >= commit.Size().
  561. func (commit *Commit) VoteSignBytes(chainID string, valIdx int) []byte {
  562. return commit.GetVote(valIdx).SignBytes(chainID)
  563. }
  564. // Type returns the vote type of the commit, which is always VoteTypePrecommit
  565. // Implements VoteSetReader.
  566. func (commit *Commit) Type() byte {
  567. return byte(PrecommitType)
  568. }
  569. // GetHeight returns height of the commit.
  570. // Implements VoteSetReader.
  571. func (commit *Commit) GetHeight() int64 {
  572. return commit.Height
  573. }
  574. // GetRound returns height of the commit.
  575. // Implements VoteSetReader.
  576. func (commit *Commit) GetRound() int {
  577. return commit.Round
  578. }
  579. // Size returns the number of signatures in the commit.
  580. // Implements VoteSetReader.
  581. func (commit *Commit) Size() int {
  582. if commit == nil {
  583. return 0
  584. }
  585. return len(commit.Signatures)
  586. }
  587. // BitArray returns a BitArray of which validators voted for BlockID or nil in this commit.
  588. // Implements VoteSetReader.
  589. func (commit *Commit) BitArray() *bits.BitArray {
  590. if commit.bitArray == nil {
  591. commit.bitArray = bits.NewBitArray(len(commit.Signatures))
  592. for i, commitSig := range commit.Signatures {
  593. // TODO: need to check the BlockID otherwise we could be counting conflicts,
  594. // not just the one with +2/3 !
  595. commit.bitArray.SetIndex(i, !commitSig.Absent())
  596. }
  597. }
  598. return commit.bitArray
  599. }
  600. // GetByIndex returns the vote corresponding to a given validator index.
  601. // Panics if `index >= commit.Size()`.
  602. // Implements VoteSetReader.
  603. func (commit *Commit) GetByIndex(valIdx int) *Vote {
  604. return commit.GetVote(valIdx)
  605. }
  606. // IsCommit returns true if there is at least one signature.
  607. // Implements VoteSetReader.
  608. func (commit *Commit) IsCommit() bool {
  609. return len(commit.Signatures) != 0
  610. }
  611. // ValidateBasic performs basic validation that doesn't involve state data.
  612. // Does not actually check the cryptographic signatures.
  613. func (commit *Commit) ValidateBasic() error {
  614. if commit.Height < 0 {
  615. return errors.New("negative Height")
  616. }
  617. if commit.Round < 0 {
  618. return errors.New("negative Round")
  619. }
  620. if commit.BlockID.IsZero() {
  621. return errors.New("commit cannot be for nil block")
  622. }
  623. if len(commit.Signatures) == 0 {
  624. return errors.New("no signatures in commit")
  625. }
  626. for i, commitSig := range commit.Signatures {
  627. if err := commitSig.ValidateBasic(); err != nil {
  628. return fmt.Errorf("wrong CommitSig #%d: %v", i, err)
  629. }
  630. }
  631. return nil
  632. }
  633. // Hash returns the hash of the commit
  634. func (commit *Commit) Hash() tmbytes.HexBytes {
  635. if commit == nil {
  636. return nil
  637. }
  638. if commit.hash == nil {
  639. bs := make([][]byte, len(commit.Signatures))
  640. for i, commitSig := range commit.Signatures {
  641. bs[i] = cdcEncode(commitSig)
  642. }
  643. commit.hash = merkle.SimpleHashFromByteSlices(bs)
  644. }
  645. return commit.hash
  646. }
  647. // StringIndented returns a string representation of the commit
  648. func (commit *Commit) StringIndented(indent string) string {
  649. if commit == nil {
  650. return "nil-Commit"
  651. }
  652. commitSigStrings := make([]string, len(commit.Signatures))
  653. for i, commitSig := range commit.Signatures {
  654. commitSigStrings[i] = commitSig.String()
  655. }
  656. return fmt.Sprintf(`Commit{
  657. %s Height: %d
  658. %s Round: %d
  659. %s BlockID: %v
  660. %s Signatures:
  661. %s %v
  662. %s}#%v`,
  663. indent, commit.Height,
  664. indent, commit.Round,
  665. indent, commit.BlockID,
  666. indent,
  667. indent, strings.Join(commitSigStrings, "\n"+indent+" "),
  668. indent, commit.hash)
  669. }
  670. //-----------------------------------------------------------------------------
  671. // SignedHeader is a header along with the commits that prove it.
  672. // It is the basis of the lite client.
  673. type SignedHeader struct {
  674. *Header `json:"header"`
  675. Commit *Commit `json:"commit"`
  676. }
  677. // ValidateBasic does basic consistency checks and makes sure the header
  678. // and commit are consistent.
  679. //
  680. // NOTE: This does not actually check the cryptographic signatures. Make sure
  681. // to use a Verifier to validate the signatures actually provide a
  682. // significantly strong proof for this header's validity.
  683. func (sh SignedHeader) ValidateBasic(chainID string) error {
  684. if sh.Header == nil {
  685. return errors.New("missing header")
  686. }
  687. if sh.Commit == nil {
  688. return errors.New("missing commit")
  689. }
  690. if err := sh.Header.ValidateBasic(); err != nil {
  691. return fmt.Errorf("invalid header: %w", err)
  692. }
  693. if err := sh.Commit.ValidateBasic(); err != nil {
  694. return fmt.Errorf("invalid commit: %w", err)
  695. }
  696. if sh.ChainID != chainID {
  697. return fmt.Errorf("header belongs to another chain %q, not %q", sh.ChainID, chainID)
  698. }
  699. // Make sure the header is consistent with the commit.
  700. if sh.Commit.Height != sh.Height {
  701. return fmt.Errorf("header and commit height mismatch: %d vs %d", sh.Height, sh.Commit.Height)
  702. }
  703. if hhash, chash := sh.Hash(), sh.Commit.BlockID.Hash; !bytes.Equal(hhash, chash) {
  704. return fmt.Errorf("commit signs block %X, header is block %X", chash, hhash)
  705. }
  706. return nil
  707. }
  708. func (sh SignedHeader) String() string {
  709. return sh.StringIndented("")
  710. }
  711. // StringIndented returns a string representation of the SignedHeader.
  712. func (sh SignedHeader) StringIndented(indent string) string {
  713. return fmt.Sprintf(`SignedHeader{
  714. %s %v
  715. %s %v
  716. %s}`,
  717. indent, sh.Header.StringIndented(indent+" "),
  718. indent, sh.Commit.StringIndented(indent+" "),
  719. indent)
  720. }
  721. //-----------------------------------------------------------------------------
  722. // Data contains the set of transactions included in the block
  723. type Data struct {
  724. // Txs that will be applied by state @ block.Height+1.
  725. // NOTE: not all txs here are valid. We're just agreeing on the order first.
  726. // This means that block.AppHash does not include these txs.
  727. Txs Txs `json:"txs"`
  728. // Volatile
  729. hash tmbytes.HexBytes
  730. }
  731. // Hash returns the hash of the data
  732. func (data *Data) Hash() tmbytes.HexBytes {
  733. if data == nil {
  734. return (Txs{}).Hash()
  735. }
  736. if data.hash == nil {
  737. data.hash = data.Txs.Hash() // NOTE: leaves of merkle tree are TxIDs
  738. }
  739. return data.hash
  740. }
  741. // StringIndented returns a string representation of the transactions
  742. func (data *Data) StringIndented(indent string) string {
  743. if data == nil {
  744. return "nil-Data"
  745. }
  746. txStrings := make([]string, tmmath.MinInt(len(data.Txs), 21))
  747. for i, tx := range data.Txs {
  748. if i == 20 {
  749. txStrings[i] = fmt.Sprintf("... (%v total)", len(data.Txs))
  750. break
  751. }
  752. txStrings[i] = fmt.Sprintf("%X (%d bytes)", tx.Hash(), len(tx))
  753. }
  754. return fmt.Sprintf(`Data{
  755. %s %v
  756. %s}#%v`,
  757. indent, strings.Join(txStrings, "\n"+indent+" "),
  758. indent, data.hash)
  759. }
  760. //-----------------------------------------------------------------------------
  761. // EvidenceData contains any evidence of malicious wrong-doing by validators
  762. type EvidenceData struct {
  763. Evidence EvidenceList `json:"evidence"`
  764. // Volatile
  765. hash tmbytes.HexBytes
  766. }
  767. // Hash returns the hash of the data.
  768. func (data *EvidenceData) Hash() tmbytes.HexBytes {
  769. if data.hash == nil {
  770. data.hash = data.Evidence.Hash()
  771. }
  772. return data.hash
  773. }
  774. // StringIndented returns a string representation of the evidence.
  775. func (data *EvidenceData) StringIndented(indent string) string {
  776. if data == nil {
  777. return "nil-Evidence"
  778. }
  779. evStrings := make([]string, tmmath.MinInt(len(data.Evidence), 21))
  780. for i, ev := range data.Evidence {
  781. if i == 20 {
  782. evStrings[i] = fmt.Sprintf("... (%v total)", len(data.Evidence))
  783. break
  784. }
  785. evStrings[i] = fmt.Sprintf("Evidence:%v", ev)
  786. }
  787. return fmt.Sprintf(`EvidenceData{
  788. %s %v
  789. %s}#%v`,
  790. indent, strings.Join(evStrings, "\n"+indent+" "),
  791. indent, data.hash)
  792. }
  793. //--------------------------------------------------------------------------------
  794. // BlockID defines the unique ID of a block as its Hash and its PartSetHeader
  795. type BlockID struct {
  796. Hash tmbytes.HexBytes `json:"hash"`
  797. PartsHeader PartSetHeader `json:"parts"`
  798. }
  799. // Equals returns true if the BlockID matches the given BlockID
  800. func (blockID BlockID) Equals(other BlockID) bool {
  801. return bytes.Equal(blockID.Hash, other.Hash) &&
  802. blockID.PartsHeader.Equals(other.PartsHeader)
  803. }
  804. // Key returns a machine-readable string representation of the BlockID
  805. func (blockID BlockID) Key() string {
  806. bz, err := cdc.MarshalBinaryBare(blockID.PartsHeader)
  807. if err != nil {
  808. panic(err)
  809. }
  810. return string(blockID.Hash) + string(bz)
  811. }
  812. // ValidateBasic performs basic validation.
  813. func (blockID BlockID) ValidateBasic() error {
  814. // Hash can be empty in case of POLBlockID in Proposal.
  815. if err := ValidateHash(blockID.Hash); err != nil {
  816. return fmt.Errorf("wrong Hash")
  817. }
  818. if err := blockID.PartsHeader.ValidateBasic(); err != nil {
  819. return fmt.Errorf("wrong PartsHeader: %v", err)
  820. }
  821. return nil
  822. }
  823. // IsZero returns true if this is the BlockID of a nil block.
  824. func (blockID BlockID) IsZero() bool {
  825. return len(blockID.Hash) == 0 &&
  826. blockID.PartsHeader.IsZero()
  827. }
  828. // IsComplete returns true if this is a valid BlockID of a non-nil block.
  829. func (blockID BlockID) IsComplete() bool {
  830. return len(blockID.Hash) == tmhash.Size &&
  831. blockID.PartsHeader.Total > 0 &&
  832. len(blockID.PartsHeader.Hash) == tmhash.Size
  833. }
  834. // String returns a human readable string representation of the BlockID
  835. func (blockID BlockID) String() string {
  836. return fmt.Sprintf(`%v:%v`, blockID.Hash, blockID.PartsHeader)
  837. }