github.com/edxfund/masterchain@v1.8.16-0.20190112084457-6ad8bdd0f74a/core/types/com_def.go (about) 1 package types 2 3 import ( 4 "encoding/binary" 5 "github.com/EDXFund/MasterChain/common" 6 "github.com/EDXFund/MasterChain/common/hexutil" 7 "github.com/EDXFund/MasterChain/crypto/sha3" 8 "github.com/EDXFund/MasterChain/rlp" 9 "io" 10 "math/big" 11 "time" 12 ) 13 14 var ( 15 EmptyRootHash = DeriveSha(Transactions{}) 16 EmptyUncleHash = CalcUncleHash(nil) 17 ) 18 19 // A BlockNonce is a 64-bit hash which proves (combined with the 20 // mix-hash) that a sufficient amount of computation has been carried 21 // out on a block. 22 type BlockNonce [8]byte 23 24 // EncodeNonce converts the given integer to a block nonce. 25 func EncodeNonce(i uint64) BlockNonce { 26 var n BlockNonce 27 binary.BigEndian.PutUint64(n[:], i) 28 return n 29 } 30 31 // Uint64 returns the integer value of a block nonce. 32 func (n BlockNonce) Uint64() uint64 { 33 return binary.BigEndian.Uint64(n[:]) 34 } 35 36 // MarshalText encodes n as a hex string with 0x prefix. 37 func (n BlockNonce) MarshalText() ([]byte, error) { 38 return hexutil.Bytes(n[:]).MarshalText() 39 40 } 41 42 // UnmarshalText implements encoding.TextUnmarshaler. 43 func (n *BlockNonce) UnmarshalText(input []byte) error { 44 return hexutil.UnmarshalFixedText("BlockNonce", input, n[:]) 45 } 46 47 func rlpHash(x interface{}) (h common.Hash) { 48 hw := sha3.NewKeccak256() 49 rlp.Encode(hw, x) 50 hw.Sum(h[:0]) 51 return h 52 } 53 54 type writeCounter common.StorageSize 55 56 func (c *writeCounter) Write(b []byte) (int, error) { 57 *c += writeCounter(len(b)) 58 return len(b), nil 59 } 60 61 type ShardStatus uint16 62 63 var ( 64 ShardMaster uint16 = 0xFFFF 65 ) 66 var ( 67 ShardEnableLen = 32 68 ) 69 70 type contractReception struct { 71 key uint64 `json:"key" gencodec:"required"` 72 value []byte `json:"value" gencodec:"required"` 73 } 74 75 // Minimized data to transfer 76 // contract data should be retrieved by other command 77 type ContractResult struct { 78 TxType byte `json:"TxType" gencodec:"required"` 79 TxHash common.Hash `json:"txHash" gencodec:"required"` 80 GasUsed uint64 `json:"gasPrice" gencodec:"required"` 81 PostState []byte `json:"root" gencodec:"required"` 82 Data []byte `json:"data"` 83 } 84 type ContractResults []*ContractResult 85 86 // Len returns the length of s. 87 func (s ContractResults) Len() int { return len(s) } 88 89 // Swap swaps the i'th and the j'th element in s. 90 func (s ContractResults) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 91 92 // GetRlp implements Rlpable and returns the i'th element of s in rlp. 93 func (s ContractResults) GetRlp(i int) []byte { 94 enc, _ := rlp.EncodeToBytes(s[i]) 95 return enc 96 } 97 98 type LastShardInfo struct { 99 ShardId uint16 100 BlockNumber uint64 101 Hash common.Hash 102 Td uint64 103 time time.Time 104 } 105 106 // TxDifference returns a new set which is the difference between a and b. 107 func ShardBlockDifference(a, b ShardBlockInfos) ShardBlockInfos { 108 keep := make(ShardBlockInfos, 0, len(a)) 109 110 remove := make(map[common.Hash]struct{}) 111 for _, shard := range b { 112 remove[shard.Hash] = struct{}{} 113 } 114 115 for _, shard := range a { 116 if _, ok := remove[shard.Hash]; !ok { 117 keep = append(keep, shard) 118 } 119 } 120 121 return keep 122 } 123 124 // TODO: copies 125 type BlockIntf interface { 126 DecodeRLP(s *rlp.Stream) error 127 EncodeRLP(w io.Writer) error 128 ShardBlock(hash common.Hash) *ShardBlockInfo 129 Uncles() []HeaderIntf 130 Header() HeaderIntf 131 Number() *big.Int 132 GasLimit() uint64 133 GasUsed() uint64 134 Difficulty() *big.Int 135 Time() *big.Int 136 137 NumberU64() uint64 138 MixDigest() common.Hash 139 Nonce() BlockNonce 140 Bloom() Bloom 141 BloomRejected() Bloom 142 Coinbase() common.Address 143 Root() common.Hash 144 ParentHash() common.Hash 145 TxHash() common.Hash 146 ReceiptHash() common.Hash 147 UncleHash() common.Hash 148 Extra() []byte 149 150 ShardId() uint16 151 ShardExp() uint16 152 ShardEnabled() [32]byte 153 // Body returns the non-header content of the block. 154 Body() *SuperBody 155 Size() common.StorageSize 156 WithSeal(header HeaderIntf) BlockIntf 157 WithBody(shardBlocksInfos []*ShardBlockInfo, receipts []*Receipt, transactions []*Transaction, results []*ContractResult) BlockIntf 158 //extract as block 159 ToBlock() *Block 160 //extract as shard block 161 ToSBlock() *SBlock 162 Hash() common.Hash 163 ClearHashCache() 164 165 //Uncles() []*Header 166 167 // 168 ReceivedAt() time.Time 169 SetReceivedAt(tm time.Time) 170 SetReceivedFrom(interface{}) 171 172 Transactions() []*Transaction 173 174 ShardBlocks() []*ShardBlockInfo 175 Receipts() []*Receipt 176 Results() []*ContractResult 177 } 178 179 type HeaderIntf interface { 180 Hash() common.Hash 181 Size() common.StorageSize 182 ShardId() uint16 183 Number() *big.Int 184 NumberU64() uint64 185 ToHeader() *Header 186 ToSHeader() *SHeader 187 ParentHash() common.Hash 188 UncleHash() common.Hash 189 ReceiptHash() common.Hash 190 ResultHash() common.Hash 191 TxHash() common.Hash 192 ShardTxsHash() common.Hash 193 Extra() []byte 194 Time() *big.Int 195 Coinbase() common.Address 196 Root() common.Hash 197 Bloom() Bloom 198 Difficulty() *big.Int 199 GasLimit() uint64 200 GasUsed() uint64 201 MixDigest() common.Hash 202 Nonce() BlockNonce 203 //func (b *Header) ExtraPtr() *[]byte { return &b.extra } 204 GasUsedPtr() *uint64 205 CoinbasePtr() *common.Address 206 SetShardId(uint16) 207 SetNumber(*big.Int) 208 SetParentHash(common.Hash) 209 SetUncleHash(common.Hash) 210 SetReceiptHash(common.Hash) 211 SetTxHash(common.Hash) 212 SetExtra([]byte) 213 SetTime(*big.Int) 214 SetCoinbase(common.Address) 215 SetRoot(common.Hash) 216 SetBloom(Bloom) 217 SetDifficulty(*big.Int) 218 SetGasLimit(uint64) 219 SetGasUsed(uint64) 220 SetMixDigest(common.Hash) 221 SetNonce(BlockNonce) 222 223 setHashDirty(bool) 224 } 225 226 type SuperBody struct { 227 ShardBlocks []*ShardBlockInfo 228 229 Uncles []*Header 230 231 Transactions []*Transaction 232 Receipts []*Receipt 233 234 //receipts 235 Results []*ContractResult 236 } 237 238 func (sb *SuperBody) ToBody() *Body { 239 return &Body{Transactions: sb.ShardBlocks, Uncles: sb.Uncles} 240 } 241 242 func (sb *SuperBody) ToSBody() *SBody { 243 return &SBody{Receipts: sb.Results} 244 } 245 246 type HeadEncode struct { 247 ShardId uint16 248 Header []byte 249 } 250 251 type BodyEncode struct { 252 ShardId uint16 253 Body []byte 254 } 255 256 // TxDifference returns a new set which is the difference between a and b. 257 func ShardInfoDifference(a, b ShardBlockInfos) ShardBlockInfos { 258 keep := make(ShardBlockInfos, 0, len(a)) 259 260 remove := make(map[common.Hash]struct{}) 261 for _, tx := range b { 262 remove[tx.Hash] = struct{}{} 263 } 264 265 for _, tx := range a { 266 if _, ok := remove[tx.Hash]; !ok { 267 keep = append(keep, tx) 268 } 269 } 270 271 return keep 272 }