github.com/amazechain/amc@v0.1.3/common/account/state_account.go (about) 1 // Copyright 2023 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The AmazeChain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package account 18 19 import ( 20 "fmt" 21 "github.com/amazechain/amc/api/protocol/state" 22 "github.com/amazechain/amc/common/crypto" 23 "github.com/amazechain/amc/common/types" 24 "github.com/amazechain/amc/utils" 25 "github.com/holiman/uint256" 26 "google.golang.org/protobuf/proto" 27 ) 28 29 // Account is the Ethereum consensus representation of accounts. 30 // These objects are stored in the main account trie. 31 // DESCRIBED: docs/programmers_guide/guide.md#ethereum-state 32 type StateAccount struct { 33 Initialised bool 34 Nonce uint64 35 Balance uint256.Int 36 Root types.Hash 37 CodeHash types.Hash // hash of the bytecode 38 Incarnation uint16 39 } 40 41 const ( 42 MimetypeDataWithValidator = "data/validator" 43 MimetypeTypedData = "data/typed" 44 MimetypeClique = "application/x-clique-header" 45 MimetypeParlia = "application/x-parlia-header" 46 MimetypeBor = "application/x-bor-header" 47 MimetypeTextPlain = "text/plain" 48 ) 49 50 var emptyCodeHash = crypto.Keccak256Hash(nil) 51 52 // var emptyRoot = types.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") 53 var emptyRoot = types.BytesHash(crypto.Keccak256(nil)) 54 55 // NewAccount creates a new account w/o code nor storage. 56 func NewAccount() StateAccount { 57 return StateAccount{ 58 Root: emptyRoot, 59 CodeHash: emptyCodeHash, 60 } 61 } 62 63 func (a *StateAccount) EncodingLengthForStorage() uint { 64 pb := a.ToProtoMessage() 65 return uint(proto.Size(pb)) 66 } 67 68 // var structLength uint = 1 // 1 byte for fieldset 69 // 70 // if !a.Balance.IsZero() { 71 // structLength += uint(a.Balance.ByteLen()) + 1 72 // } 73 // 74 // if a.Nonce > 0 { 75 // structLength += uint((bits.Len64(a.Nonce)+7)/8) + 1 76 // } 77 // 78 // if !a.IsEmptyCodeHash() { 79 // structLength += 33 // 32-byte array + 1 bytes for length 80 // } 81 // 82 // if a.Incarnation > 0 { 83 // structLength += uint((bits.Len64(a.Incarnation)+7)/8) + 1 84 // } 85 // 86 // return structLength 87 //} 88 89 //func (a *Account) EncodingLengthForHashing() uint { 90 // var structLength uint 91 // 92 // balanceBytes := 0 93 // if !a.Balance.LtUint64(128) { 94 // balanceBytes = a.Balance.ByteLen() 95 // } 96 // 97 // nonceBytes := rlp.IntLenExcludingHead(a.Nonce) 98 // 99 // structLength += uint(balanceBytes + nonceBytes + 2) 100 // 101 // structLength += 66 // Two 32-byte arrays + 2 prefixes 102 // 103 // if structLength < 56 { 104 // return 1 + structLength 105 // } 106 // 107 // lengthBytes := (bits.Len(structLength) + 7) / 8 108 // 109 // return uint(1+lengthBytes) + structLength 110 //} 111 112 func (a *StateAccount) EncodeForStorage(buffer []byte) { 113 pb := a.ToProtoMessage() 114 data, _ := proto.Marshal(pb) 115 copy(buffer, data) 116 //var fieldSet = 0 // start with first bit set to 0 117 //var pos = 1 118 //if a.Nonce > 0 { 119 // fieldSet = 1 120 // nonceBytes := (bits.Len64(a.Nonce) + 7) / 8 121 // buffer[pos] = byte(nonceBytes) 122 // var nonce = a.Nonce 123 // for i := nonceBytes; i > 0; i-- { 124 // buffer[pos+i] = byte(nonce) 125 // nonce >>= 8 126 // } 127 // pos += nonceBytes + 1 128 //} 129 // 130 //// Encoding balance 131 //if !a.Balance.IsZero() { 132 // fieldSet |= 2 133 // balanceBytes := a.Balance.ByteLen() 134 // buffer[pos] = byte(balanceBytes) 135 // pos++ 136 // a.Balance.WriteToSlice(buffer[pos : pos+balanceBytes]) 137 // pos += balanceBytes 138 //} 139 // 140 //if a.Incarnation > 0 { 141 // fieldSet |= 4 142 // incarnationBytes := (bits.Len64(uint64(a.Incarnation)) + 7) / 8 143 // buffer[pos] = byte(incarnationBytes) 144 // var incarnation = a.Incarnation 145 // for i := incarnationBytes; i > 0; i-- { 146 // buffer[pos+i] = byte(incarnation) 147 // incarnation >>= 8 148 // } 149 // pos += incarnationBytes + 1 150 //} 151 // 152 //// Encoding CodeHash 153 //if !a.IsEmptyCodeHash() { 154 // fieldSet |= 8 155 // buffer[pos] = 32 156 // copy(buffer[pos+1:], a.CodeHash.Bytes()) 157 // //pos += 33 158 //} 159 // 160 //buffer[0] = byte(fieldSet) 161 } 162 163 // Decodes length and determines whether it corresponds to a structure of a byte array 164 //func decodeLengthForHashing(buffer []byte, pos int) (length int, structure bool, newPos int) { 165 // switch firstByte := int(buffer[pos]); { 166 // case firstByte < 128: 167 // return 0, false, pos 168 // case firstByte < 184: 169 // return firstByte - 128, false, pos + 1 170 // case firstByte < 192: 171 // // Next byte is the length of the length + 183 172 // lenEnd := pos + 1 + firstByte - 183 173 // len := 0 174 // for i := pos + 1; i < lenEnd; i++ { 175 // len = (len << 8) + int(buffer[i]) 176 // } 177 // return len, false, lenEnd 178 // case firstByte < 248: 179 // return firstByte - 192, true, pos + 1 180 // default: 181 // // Next byte is the length of the length + 247 182 // lenEnd := pos + 1 + firstByte - 247 183 // len := 0 184 // for i := pos + 1; i < lenEnd; i++ { 185 // len = (len << 8) + int(buffer[i]) 186 // } 187 // return len, true, lenEnd 188 // } 189 //} 190 191 //var rlpEncodingBufPool = sync.Pool{ 192 // New: func() interface{} { 193 // buf := make([]byte, 0, 128) 194 // return &buf 195 // }, 196 //} 197 198 //func (a *Account) EncodeRLP(w io.Writer) error { 199 // var buf []byte 200 // l := a.EncodingLengthForHashing() 201 // if l > 128 { 202 // buf = make([]byte, l) 203 // } else { 204 // bp := rlpEncodingBufPool.Get().(*[]byte) 205 // defer rlpEncodingBufPool.Put(bp) 206 // buf = *bp 207 // buf = buf[:l] 208 // } 209 // 210 // a.EncodeForHashing(buf) 211 // _, err := w.Write(buf) 212 // return err 213 //} 214 215 //func (a *Account) EncodeForHashing(buffer []byte) { 216 // balanceBytes := 0 217 // if !a.Balance.LtUint64(128) { 218 // balanceBytes = a.Balance.ByteLen() 219 // } 220 // 221 // nonceBytes := rlp.IntLenExcludingHead(a.Nonce) 222 // 223 // var structLength = uint(balanceBytes + nonceBytes + 2) 224 // structLength += 66 // Two 32-byte arrays + 2 prefixes 225 // 226 // var pos int 227 // if structLength < 56 { 228 // buffer[0] = byte(192 + structLength) 229 // pos = 1 230 // } else { 231 // lengthBytes := (bits.Len(structLength) + 7) / 8 232 // buffer[0] = byte(247 + lengthBytes) 233 // 234 // for i := lengthBytes; i > 0; i-- { 235 // buffer[i] = byte(structLength) 236 // structLength >>= 8 237 // } 238 // 239 // pos = lengthBytes + 1 240 // } 241 // 242 // // Encoding nonce 243 // if a.Nonce < 128 && a.Nonce != 0 { 244 // buffer[pos] = byte(a.Nonce) 245 // } else { 246 // buffer[pos] = byte(128 + nonceBytes) 247 // var nonce = a.Nonce 248 // for i := nonceBytes; i > 0; i-- { 249 // buffer[pos+i] = byte(nonce) 250 // nonce >>= 8 251 // } 252 // } 253 // pos += 1 + nonceBytes 254 // 255 // // Encoding balance 256 // if a.Balance.LtUint64(128) && !a.Balance.IsZero() { 257 // buffer[pos] = byte(a.Balance.Uint64()) 258 // pos++ 259 // } else { 260 // buffer[pos] = byte(128 + balanceBytes) 261 // pos++ 262 // a.Balance.WriteToSlice(buffer[pos : pos+balanceBytes]) 263 // pos += balanceBytes 264 // } 265 // 266 // // Encoding Root and CodeHash 267 // buffer[pos] = 128 + 32 268 // pos++ 269 // copy(buffer[pos:], a.Root[:]) 270 // pos += 32 271 // buffer[pos] = 128 + 32 272 // pos++ 273 // copy(buffer[pos:], a.CodeHash[:]) 274 // //pos += 32 275 //} 276 277 // Copy makes `a` a full, independent (meaning that if the `image` changes in any way, it does not affect `a`) copy of the account `image`. 278 func (a *StateAccount) Copy(image *StateAccount) { 279 a.Initialised = image.Initialised 280 a.Nonce = image.Nonce 281 a.Balance.Set(&image.Balance) 282 copy(a.Root[:], image.Root[:]) 283 copy(a.CodeHash[:], image.CodeHash[:]) 284 a.Incarnation = image.Incarnation 285 } 286 287 //func (a *Account) DecodeForHashing(enc []byte) error { 288 // length, structure, pos := decodeLengthForHashing(enc, 0) 289 // if pos+length != len(enc) { 290 // return fmt.Errorf( 291 // "malformed RLP for Account(%x): prefixLength(%d) + dataLength(%d) != sliceLength(%d)", 292 // enc, pos, length, len(enc)) 293 // } 294 // if !structure { 295 // return fmt.Errorf( 296 // "encoding of Account should be RLP struct, got byte array: %x", 297 // enc, 298 // ) 299 // } 300 // 301 // a.Initialised = true 302 // a.Nonce = 0 303 // a.Balance.Clear() 304 // a.Root = emptyRoot 305 // a.CodeHash = emptyCodeHash 306 // if length == 0 && structure { 307 // return nil 308 // } 309 // 310 // if pos < len(enc) { 311 // nonceBytes, s, newPos := decodeLengthForHashing(enc, pos) 312 // if s { 313 // return fmt.Errorf( 314 // "encoding of Account.Nonce should be byte array, got RLP struct: %x", 315 // enc[pos:newPos+nonceBytes], 316 // ) 317 // } 318 // 319 // if newPos+nonceBytes > len(enc) { 320 // return fmt.Errorf( 321 // "malformed RLP for Account.Nonce(%x): prefixLength(%d) + dataLength(%d) >= sliceLength(%d)", 322 // enc[pos:newPos+nonceBytes], 323 // newPos-pos, nonceBytes, len(enc)-pos, 324 // ) 325 // } 326 // 327 // var nonce uint64 328 // if nonceBytes == 0 && newPos == pos { 329 // nonce = uint64(enc[newPos]) 330 // pos = newPos + 1 331 // } else { 332 // for _, b := range enc[newPos : newPos+nonceBytes] { 333 // nonce = (nonce << 8) + uint64(b) 334 // } 335 // pos = newPos + nonceBytes 336 // } 337 // 338 // a.Nonce = nonce 339 // } 340 // if pos < len(enc) { 341 // balanceBytes, s, newPos := decodeLengthForHashing(enc, pos) 342 // if s { 343 // return fmt.Errorf( 344 // "encoding of Account.Balance should be byte array, got RLP struct: %x", 345 // enc[pos:newPos+balanceBytes], 346 // ) 347 // } 348 // 349 // if newPos+balanceBytes > len(enc) { 350 // return fmt.Errorf("malformed RLP for Account.Balance(%x): prefixLength(%d) + dataLength(%d) >= sliceLength(%d)", 351 // enc[pos], 352 // newPos-pos, balanceBytes, len(enc)-pos, 353 // ) 354 // } 355 // 356 // if balanceBytes == 0 && newPos == pos { 357 // a.Balance.SetUint64(uint64(enc[newPos])) 358 // pos = newPos + 1 359 // } else { 360 // a.Balance.SetBytes(enc[newPos : newPos+balanceBytes]) 361 // pos = newPos + balanceBytes 362 // } 363 // } 364 // if pos < len(enc) { 365 // rootBytes, s, newPos := decodeLengthForHashing(enc, pos) 366 // if s { 367 // return fmt.Errorf( 368 // "encoding of Account.Root should be byte array, got RLP struct: %x", 369 // enc[pos:newPos+rootBytes], 370 // ) 371 // } 372 // 373 // if rootBytes != 32 { 374 // return fmt.Errorf( 375 // "encoding of Account.Root should have size 32, got %d", 376 // rootBytes, 377 // ) 378 // } 379 // 380 // if newPos+rootBytes > len(enc) { 381 // return fmt.Errorf("malformed RLP for Account.Root(%x): prefixLength(%d) + dataLength(%d) >= sliceLength(%d)", 382 // enc[pos], 383 // newPos-pos, rootBytes, len(enc)-pos, 384 // ) 385 // } 386 // 387 // copy(a.Root[:], enc[newPos:newPos+rootBytes]) 388 // pos = newPos + rootBytes 389 // } 390 // 391 // if pos < len(enc) { 392 // codeHashBytes, s, newPos := decodeLengthForHashing(enc, pos) 393 // if s { 394 // return fmt.Errorf( 395 // "encoding of Account.CodeHash should be byte array, got RLP struct: %x", 396 // enc[pos:newPos+codeHashBytes], 397 // ) 398 // } 399 // 400 // if codeHashBytes != 32 { 401 // return fmt.Errorf( 402 // "encoding of Account.CodeHash should have size 32, got %d", 403 // codeHashBytes, 404 // ) 405 // } 406 // 407 // if newPos+codeHashBytes > len(enc) { 408 // return fmt.Errorf("malformed RLP for Account.CodeHash(%x): prefixLength(%d) + dataLength(%d) >= sliceLength(%d)", 409 // enc[pos:newPos+codeHashBytes], 410 // newPos-pos, codeHashBytes, len(enc)-pos, 411 // ) 412 // } 413 // 414 // copy(a.CodeHash[:], enc[newPos:newPos+codeHashBytes]) 415 // pos = newPos + codeHashBytes 416 // } 417 // 418 // if pos < len(enc) { 419 // storageSizeBytes, s, newPos := decodeLengthForHashing(enc, pos) 420 // if s { 421 // return fmt.Errorf( 422 // "encoding of Account.StorageSize should be byte array, got RLP struct: %x", 423 // enc[pos:newPos+storageSizeBytes], 424 // ) 425 // } 426 // 427 // if newPos+storageSizeBytes > len(enc) { 428 // return fmt.Errorf( 429 // "malformed RLP for Account.StorageSize(%x): prefixLength(%d) + dataLength(%d) >= sliceLength(%d)", 430 // enc[pos:newPos+storageSizeBytes], 431 // newPos-pos, storageSizeBytes, len(enc)-pos, 432 // ) 433 // } 434 // 435 // // Commented out because of the ineffectual assignment - uncomment if adding more fields 436 // var storageSize uint64 437 // if storageSizeBytes == 0 && newPos == pos { 438 // storageSize = uint64(enc[newPos]) 439 // pos = newPos + 1 440 // } else { 441 // for _, b := range enc[newPos : newPos+storageSizeBytes] { 442 // storageSize = (storageSize << 8) + uint64(b) 443 // } 444 // pos = newPos + storageSizeBytes 445 // } 446 // _ = storageSize 447 // } 448 // _ = pos 449 // 450 // return nil 451 //} 452 453 func (a *StateAccount) Reset() { 454 a.Initialised = true 455 a.Nonce = 0 456 a.Incarnation = 0 457 a.Balance.Clear() 458 copy(a.Root[:], emptyRoot[:]) 459 copy(a.CodeHash[:], emptyCodeHash[:]) 460 } 461 462 func (a *StateAccount) DecodeForStorage(enc []byte) error { 463 a.Reset() 464 if len(enc) == 0 { 465 return nil 466 } 467 return a.Unmarshal(enc) 468 //pbAccount := new(state.Account) 469 //if err := proto.Unmarshal(enc, pbAccount); nil != err { 470 // return err 471 //} 472 //if err := a.FromProtoMessage(pbAccount); nil != err { 473 // return err 474 //} 475 476 //a.Reset() 477 // 478 //if len(enc) == 0 { 479 // return nil 480 //} 481 // 482 //var fieldSet = enc[0] 483 //var pos = 1 484 // 485 //if fieldSet&1 > 0 { 486 // decodeLength := int(enc[pos]) 487 // 488 // if len(enc) < pos+decodeLength+1 { 489 // return fmt.Errorf( 490 // "malformed CBOR for Account.Nonce: %s, Length %d", 491 // enc[pos+1:], decodeLength) 492 // } 493 // 494 // a.Nonce = bytesToUint64(enc[pos+1 : pos+decodeLength+1]) 495 // pos += decodeLength + 1 496 //} 497 // 498 //if fieldSet&2 > 0 { 499 // decodeLength := int(enc[pos]) 500 // 501 // if len(enc) < pos+decodeLength+1 { 502 // return fmt.Errorf( 503 // "malformed CBOR for Account.Nonce: %s, Length %d", 504 // enc[pos+1:], decodeLength) 505 // } 506 // 507 // a.Balance.SetBytes(enc[pos+1 : pos+decodeLength+1]) 508 // pos += decodeLength + 1 509 //} 510 // 511 //if fieldSet&4 > 0 { 512 // decodeLength := int(enc[pos]) 513 // 514 // if len(enc) < pos+decodeLength+1 { 515 // return fmt.Errorf( 516 // "malformed CBOR for Account.Incarnation: %s, Length %d", 517 // enc[pos+1:], decodeLength) 518 // } 519 // 520 // a.Incarnation = uint16(bytesToUint64(enc[pos+1 : pos+decodeLength+1])) 521 // pos += decodeLength + 1 522 //} 523 // 524 //if fieldSet&8 > 0 { 525 // 526 // decodeLength := int(enc[pos]) 527 // 528 // if decodeLength != 32 { 529 // return fmt.Errorf("codehash should be 32 bytes long, got %d instead", 530 // decodeLength) 531 // } 532 // 533 // if len(enc) < pos+decodeLength+1 { 534 // return fmt.Errorf( 535 // "malformed CBOR for Account.CodeHash: %s, Length %d", 536 // enc[pos+1:], decodeLength) 537 // } 538 // 539 // a.CodeHash.SetBytes(enc[pos+1 : pos+decodeLength+1]) 540 // pos += decodeLength + 1 541 //} 542 // 543 //_ = pos 544 } 545 func bytesToUint64(buf []byte) (x uint64) { 546 for i, b := range buf { 547 x = x<<8 + uint64(b) 548 if i == 7 { 549 return 550 } 551 } 552 return 553 } 554 555 //func DecodeIncarnationFromStorage(enc []byte) (uint64, error) { 556 // if len(enc) == 0 { 557 // return 0, nil 558 // } 559 // 560 // var fieldSet = enc[0] 561 // var pos = 1 562 // 563 // //looks for the position incarnation is at 564 // if fieldSet&1 > 0 { 565 // decodeLength := int(enc[pos]) 566 // if len(enc) < pos+decodeLength+1 { 567 // return 0, fmt.Errorf( 568 // "malformed CBOR for Account.Nonce: %s, Length %d", 569 // enc[pos+1:], decodeLength) 570 // } 571 // pos += decodeLength + 1 572 // } 573 // 574 // if fieldSet&2 > 0 { 575 // decodeLength := int(enc[pos]) 576 // if len(enc) < pos+decodeLength+1 { 577 // return 0, fmt.Errorf( 578 // "malformed CBOR for Account.Nonce: %s, Length %d", 579 // enc[pos+1:], decodeLength) 580 // } 581 // pos += decodeLength + 1 582 // } 583 // 584 // if fieldSet&4 > 0 { 585 // decodeLength := int(enc[pos]) 586 // 587 // //checks if the ending position is correct if not returns 0 588 // if len(enc) < pos+decodeLength+1 { 589 // return 0, fmt.Errorf( 590 // "malformed CBOR for Account.Incarnation: %s, Length %d", 591 // enc[pos+1:], decodeLength) 592 // } 593 // 594 // incarnation := bytesToUint64(enc[pos+1 : pos+decodeLength+1]) 595 // return incarnation, nil 596 // } 597 // 598 // return 0, nil 599 // 600 //} 601 602 func (a *StateAccount) SelfCopy() *StateAccount { 603 newAcc := NewAccount() 604 newAcc.Copy(a) 605 return &newAcc 606 } 607 608 func (a *StateAccount) IsEmptyCodeHash() bool { 609 return IsEmptyCodeHash(a.CodeHash) 610 } 611 612 func IsEmptyCodeHash(codeHash types.Hash) bool { 613 return codeHash == emptyCodeHash || codeHash == (types.Hash{}) 614 } 615 616 func (a *StateAccount) IsEmptyRoot() bool { 617 return a.Root == emptyRoot || a.Root == types.Hash{} 618 } 619 620 func (a *StateAccount) GetIncarnation() uint16 { 621 return a.Incarnation 622 } 623 624 func (a *StateAccount) SetIncarnation(v uint16) { 625 a.Incarnation = v 626 } 627 628 func (a *StateAccount) Equals(acc *StateAccount) bool { 629 return a.Nonce == acc.Nonce && 630 a.CodeHash == acc.CodeHash && 631 a.Balance.Cmp(&acc.Balance) == 0 && 632 a.Incarnation == acc.Incarnation 633 } 634 635 func (a *StateAccount) Marshal() ([]byte, error) { 636 protoMsg := a.ToProtoMessage() 637 v, err := proto.Marshal(protoMsg) 638 if nil != err { 639 return nil, err 640 } 641 return v, nil 642 } 643 644 func (a *StateAccount) Unmarshal(v []byte) error { 645 var pAccount state.Account 646 if err := proto.Unmarshal(v, &pAccount); nil != err { 647 return err 648 } 649 a.Initialised = pAccount.Initialised 650 a.Nonce = pAccount.Nonce 651 a.Balance = *utils.ConvertH256ToUint256Int(pAccount.Balance) 652 a.Root = utils.ConvertH256ToHash(pAccount.Root) 653 a.CodeHash = utils.ConvertH256ToHash(pAccount.CodeHash) 654 a.Incarnation = uint16(pAccount.Incarnation) 655 return nil 656 } 657 658 func (a *StateAccount) ToProtoMessage() proto.Message { 659 return &state.Account{ 660 Initialised: a.Initialised, 661 Nonce: a.Nonce, 662 Balance: utils.ConvertUint256IntToH256(&a.Balance), 663 Root: utils.ConvertHashToH256(a.Root), 664 CodeHash: utils.ConvertHashToH256(a.CodeHash), 665 Incarnation: uint64(a.Incarnation), 666 } 667 } 668 669 func (a *StateAccount) FromProtoMessage(msg proto.Message) error { 670 pAccount, ok := msg.(*state.Account) 671 if !ok { 672 return fmt.Errorf("impossible type assert ") 673 } 674 675 a.Initialised = pAccount.Initialised 676 a.Nonce = pAccount.Nonce 677 a.Balance = *utils.ConvertH256ToUint256Int(pAccount.Balance) 678 a.Root = utils.ConvertH256ToHash(pAccount.Root) 679 a.CodeHash = utils.ConvertH256ToHash(pAccount.CodeHash) 680 a.Incarnation = uint16(pAccount.Incarnation) 681 return nil 682 }