|
| 1 | +package apphash |
| 2 | + |
| 3 | +import "crypto/sha256" |
| 4 | + |
| 5 | +// appHashVersion is the version of the app hash schema produced by this package. |
| 6 | +const appHashVersion uint8 = 1 |
| 7 | + |
| 8 | +// appHashDomain is the domain separation tag that prefixes the canonical byte format in the app hash preimage. |
| 9 | +const appHashDomain = "sei-apphash" |
| 10 | + |
| 11 | +// Contains the data used to produce an app hash. |
| 12 | +type AppHashData struct { |
| 13 | + |
| 14 | + // The version of the app hash schema. |
| 15 | + version uint8 |
| 16 | + |
| 17 | + // The EVM chain ID of the chain the block belongs to. |
| 18 | + chainID uint64 |
| 19 | + |
| 20 | + // The block height corresponding to this |
| 21 | + // app hash. |
| 22 | + blockHeight uint64 |
| 23 | + |
| 24 | + // The hash of the block. |
| 25 | + blockHash [32]byte |
| 26 | + |
| 27 | + // The hash of the state lattice hash. |
| 28 | + stateHash [32]byte |
| 29 | + |
| 30 | + // The "Block Update Digest", aka the hash of |
| 31 | + // the key-value pairs that changed as a result |
| 32 | + // of executing this block. |
| 33 | + bud [32]byte |
| 34 | + |
| 35 | + // The hash of the transaction receipts |
| 36 | + // produced by executing this block. |
| 37 | + receiptHash [32]byte |
| 38 | + |
| 39 | + // The app hash of block (blockHeight-1). |
| 40 | + // All 0s if this is the first block. |
| 41 | + previousAppHash [32]byte |
| 42 | + |
| 43 | + // The app hash: SHA-256 of appHashDomain followed by the canonical byte format. |
| 44 | + // It is not itself serialized. |
| 45 | + appHash [32]byte |
| 46 | +} |
| 47 | + |
| 48 | +// Construct a new AppHashData object at the current schema version. |
| 49 | +func NewAppHashData( |
| 50 | + // The EVM chain ID of the chain the block belongs to. |
| 51 | + chainID uint64, |
| 52 | + // The block height corresponding to this app hash. |
| 53 | + blockHeight uint64, |
| 54 | + // The hash of the block. |
| 55 | + blockHash [32]byte, |
| 56 | + // The hash of the state lattice hash. |
| 57 | + stateHash [32]byte, |
| 58 | + // The "Block Update Digest", aka the hash of |
| 59 | + // the key-value pairs that changed as a result |
| 60 | + // of executing this block. |
| 61 | + bud [32]byte, |
| 62 | + // The hash of the transaction receipts produced by executing this block. |
| 63 | + receiptHash [32]byte, |
| 64 | + // The app hash of block (blockHeight-1), or all 0s if this is the first block. |
| 65 | + previousAppHash [32]byte, |
| 66 | +) *AppHashData { |
| 67 | + ahd := &AppHashData{ |
| 68 | + version: appHashVersion, |
| 69 | + chainID: chainID, |
| 70 | + blockHeight: blockHeight, |
| 71 | + blockHash: blockHash, |
| 72 | + stateHash: stateHash, |
| 73 | + bud: bud, |
| 74 | + receiptHash: receiptHash, |
| 75 | + previousAppHash: previousAppHash, |
| 76 | + } |
| 77 | + ahd.appHash = sha256.Sum256(append([]byte(appHashDomain), ahd.Serialize()...)) |
| 78 | + return ahd |
| 79 | +} |
| 80 | + |
| 81 | +// Version returns the version of the app hash schema. |
| 82 | +func (ahd *AppHashData) Version() uint8 { |
| 83 | + return ahd.version |
| 84 | +} |
| 85 | + |
| 86 | +// ChainID returns the EVM chain ID of the chain the block belongs to. |
| 87 | +func (ahd *AppHashData) ChainID() uint64 { |
| 88 | + return ahd.chainID |
| 89 | +} |
| 90 | + |
| 91 | +// BlockHeight returns the block height corresponding to this app hash. |
| 92 | +func (ahd *AppHashData) BlockHeight() uint64 { |
| 93 | + return ahd.blockHeight |
| 94 | +} |
| 95 | + |
| 96 | +// BlockHash returns the hash of the block. |
| 97 | +func (ahd *AppHashData) BlockHash() [32]byte { |
| 98 | + return ahd.blockHash |
| 99 | +} |
| 100 | + |
| 101 | +// StateHash returns the hash of the state lattice hash. |
| 102 | +func (ahd *AppHashData) StateHash() [32]byte { |
| 103 | + return ahd.stateHash |
| 104 | +} |
| 105 | + |
| 106 | +// BUD returns the "Block Update Digest", aka the hash of the key-value pairs that changed as a result of executing |
| 107 | +// this block. |
| 108 | +func (ahd *AppHashData) BUD() [32]byte { |
| 109 | + return ahd.bud |
| 110 | +} |
| 111 | + |
| 112 | +// ReceiptHash returns the hash of the transaction receipts produced by executing this block. |
| 113 | +func (ahd *AppHashData) ReceiptHash() [32]byte { |
| 114 | + return ahd.receiptHash |
| 115 | +} |
| 116 | + |
| 117 | +// PreviousAppHash returns the app hash of block (blockHeight-1), or all 0s if this is the first block. |
| 118 | +func (ahd *AppHashData) PreviousAppHash() [32]byte { |
| 119 | + return ahd.previousAppHash |
| 120 | +} |
| 121 | + |
| 122 | +// AppHash returns the app hash: the SHA-256 of appHashDomain followed by the canonical byte format. |
| 123 | +func (ahd *AppHashData) AppHash() [32]byte { |
| 124 | + return ahd.appHash |
| 125 | +} |
0 commit comments