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.

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