|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +type Block struct { |
| 10 | + Header Header `bson:"header" json:"header"` |
| 11 | + Data Data `bson:"data" json:"data"` |
| 12 | + Evidence EvidenceData `bson:"evidence" json:"evidence"` |
| 13 | + LastCommit *Commit `bson:"lastCommit" json:"lastCommit"` |
| 14 | +} |
| 15 | + |
| 16 | +type Header struct { |
| 17 | + Version Consensus `bson:"version" json:"version"` |
| 18 | + ChainID string `bson:"chainId" json:"chainId"` |
| 19 | + Height int64 `bson:"height" json:"height"` |
| 20 | + Time time.Time `bson:"time" json:"time"` |
| 21 | + LastBlockID BlockID `bson:"lastBlockId" json:"lastBlockId"` |
| 22 | + LastCommitHash string `bson:"lastCommitHash" json:"lastCommitHash"` |
| 23 | + DataHash string `bson:"dataHash" json:"dataHash"` |
| 24 | + ValidatorsHash string `bson:"validatorsHash" json:"validatorsHash"` |
| 25 | + NextValidatorsHash string `bson:"nextValidatorsHash" json:"nextValidatorsHash"` |
| 26 | + ConsensusHash string `bson:"consensusHash" json:"consensusHash"` |
| 27 | + AppHash string `bson:"appHash" json:"appHash"` |
| 28 | + LastResultsHash string `bson:"lastResultsHash" json:"lastResultsHash"` |
| 29 | + EvidenceHash string `bson:"evidenceHash" json:"evidenceHash"` |
| 30 | + ProposerAddress string `bson:"proposerAddress" json:"proposerAddress"` |
| 31 | +} |
| 32 | + |
| 33 | +type Consensus struct { |
| 34 | + Block uint64 `bson:"block" json:"block"` |
| 35 | + App uint64 `bson:"app" json:"app"` |
| 36 | +} |
| 37 | + |
| 38 | +type Data struct { |
| 39 | + Txs []Tx `bson:"txs" json:"txs"` |
| 40 | +} |
| 41 | + |
| 42 | +type Tx []byte |
| 43 | + |
| 44 | +type EvidenceData struct { |
| 45 | + Evidence []Evidence `bson:"evidence" json:"evidence"` |
| 46 | +} |
| 47 | + |
| 48 | +type Evidence interface{} |
| 49 | + |
| 50 | +type Commit struct { |
| 51 | + Height int64 `bson:"height" json:"height"` |
| 52 | + Round int32 `bson:"round" json:"round"` |
| 53 | + BlockID BlockID `bson:"blockId" json:"blockId"` |
| 54 | + Signatures []CommitSig `bson:"signatures" json:"signatures"` |
| 55 | +} |
| 56 | + |
| 57 | +type CommitSig struct { |
| 58 | + BlockIDFlag BlockIDFlag `bson:"blockIdFlag" json:"blockIdFlag"` |
| 59 | + ValidatorAddress string `bson:"validatorAddress" json:"validatorAddress"` |
| 60 | + Timestamp time.Time `bson:"timestamp" json:"timestamp"` |
| 61 | + Signature string `bson:"signature" json:"signature"` |
| 62 | +} |
| 63 | + |
| 64 | +type BlockIDFlag int |
| 65 | + |
| 66 | +const ( |
| 67 | + BlockIDFlagAbsent BlockIDFlag = 1 |
| 68 | + BlockIDFlagCommit BlockIDFlag = 2 |
| 69 | + BlockIDFlagNil BlockIDFlag = 3 |
| 70 | +) |
| 71 | + |
| 72 | +func NewCommitSigAbsent() CommitSig { |
| 73 | + return CommitSig{ |
| 74 | + BlockIDFlag: BlockIDFlagAbsent, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func (b *Block) String() string { |
| 79 | + if b == nil { |
| 80 | + return "nil-Block" |
| 81 | + } |
| 82 | + return "Block{" + |
| 83 | + "\n " + b.Header.String() + |
| 84 | + "\n " + b.Data.String() + |
| 85 | + "\n " + b.Evidence.String() + |
| 86 | + "\n " + b.LastCommit.String() + |
| 87 | + "\n}#" + b.Hash() |
| 88 | +} |
| 89 | + |
| 90 | +func (h Header) String() string { |
| 91 | + return "Header{" + |
| 92 | + "\n Version: " + h.Version.String() + |
| 93 | + "\n ChainID: " + h.ChainID + |
| 94 | + "\n Height: " + fmt.Sprintf("%d", h.Height) + |
| 95 | + "\n Time: " + h.Time.Format(time.RFC3339Nano) + |
| 96 | + "\n LastBlockID: " + h.LastBlockID.String() + |
| 97 | + "\n LastCommit: " + h.LastCommitHash + |
| 98 | + "\n Data: " + h.DataHash + |
| 99 | + "\n Validators: " + h.ValidatorsHash + |
| 100 | + "\n NextValidators: " + h.NextValidatorsHash + |
| 101 | + "\n App: " + h.AppHash + |
| 102 | + "\n Consensus: " + h.ConsensusHash + |
| 103 | + "\n Results: " + h.LastResultsHash + |
| 104 | + "\n Evidence: " + h.EvidenceHash + |
| 105 | + "\n Proposer: " + h.ProposerAddress + |
| 106 | + "\n }#" + h.Hash() |
| 107 | +} |
| 108 | + |
| 109 | +func (c Consensus) String() string { |
| 110 | + return fmt.Sprintf("{%d %d}", c.Block, c.App) |
| 111 | +} |
| 112 | + |
| 113 | +func (d Data) String() string { |
| 114 | + result := "Data{" |
| 115 | + for _, tx := range d.Txs { |
| 116 | + result += fmt.Sprintf("\n %s (%d bytes)", hex.EncodeToString(tx), len(tx)) |
| 117 | + } |
| 118 | + return result + "\n }#" + d.Hash() |
| 119 | +} |
| 120 | + |
| 121 | +func (e EvidenceData) String() string { |
| 122 | + return "EvidenceData{" + |
| 123 | + "\n }#" + e.Hash() |
| 124 | +} |
| 125 | + |
| 126 | +func (c *Commit) String() string { |
| 127 | + if c == nil { |
| 128 | + return "nil-Commit" |
| 129 | + } |
| 130 | + result := "Commit{" + |
| 131 | + fmt.Sprintf("\n Height: %d", c.Height) + |
| 132 | + fmt.Sprintf("\n Round: %d", c.Round) + |
| 133 | + "\n BlockID: " + c.BlockID.String() + |
| 134 | + "\n Signatures:" |
| 135 | + for _, sig := range c.Signatures { |
| 136 | + result += "\n " + sig.String() |
| 137 | + } |
| 138 | + return result + "\n }#" + c.Hash() |
| 139 | +} |
| 140 | + |
| 141 | +func (cs CommitSig) String() string { |
| 142 | + return fmt.Sprintf("CommitSig{%s by %s on %d @ %s}", cs.Signature, cs.ValidatorAddress, cs.BlockIDFlag, cs.Timestamp.Format(time.RFC3339)) |
| 143 | +} |
| 144 | + |
| 145 | +func (bid BlockID) String() string { |
| 146 | + return fmt.Sprintf("%s:%d:%s", bid.Hash, bid.PartSetHeader.Total, bid.PartSetHeader.Hash) |
| 147 | +} |
| 148 | + |
| 149 | +func (b *Block) Hash() string { |
| 150 | + return "" |
| 151 | +} |
| 152 | + |
| 153 | +func (h Header) Hash() string { |
| 154 | + return "" |
| 155 | +} |
| 156 | + |
| 157 | +func (d Data) Hash() string { |
| 158 | + return "" |
| 159 | +} |
| 160 | + |
| 161 | +func (e EvidenceData) Hash() string { |
| 162 | + return "" |
| 163 | +} |
| 164 | + |
| 165 | +func (c *Commit) Hash() string { |
| 166 | + return "" |
| 167 | +} |
| 168 | + |
| 169 | +func (b *Block) ValidateBasic() error { |
| 170 | + return nil |
| 171 | +} |
0 commit comments