github.com/phillinzzz/newBsc@v1.1.6/core/state/snapshot/account.go (about) 1 // Copyright 2019 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package snapshot 18 19 import ( 20 "bytes" 21 "math/big" 22 23 "github.com/phillinzzz/newBsc/common" 24 "github.com/phillinzzz/newBsc/rlp" 25 ) 26 27 // Account is a modified version of a state.Account, where the root is replaced 28 // with a byte slice. This format can be used to represent full-consensus format 29 // or slim-snapshot format which replaces the empty root and code hash as nil 30 // byte slice. 31 type Account struct { 32 Nonce uint64 33 Balance *big.Int 34 Root []byte 35 CodeHash []byte 36 } 37 38 // SlimAccount converts a state.Account content into a slim snapshot account 39 func SlimAccount(nonce uint64, balance *big.Int, root common.Hash, codehash []byte) Account { 40 slim := Account{ 41 Nonce: nonce, 42 Balance: balance, 43 } 44 if root != emptyRoot { 45 slim.Root = root[:] 46 } 47 if !bytes.Equal(codehash, emptyCode[:]) { 48 slim.CodeHash = codehash 49 } 50 return slim 51 } 52 53 // SlimAccountRLP converts a state.Account content into a slim snapshot 54 // version RLP encoded. 55 func SlimAccountRLP(nonce uint64, balance *big.Int, root common.Hash, codehash []byte) []byte { 56 data, err := rlp.EncodeToBytes(SlimAccount(nonce, balance, root, codehash)) 57 if err != nil { 58 panic(err) 59 } 60 return data 61 } 62 63 // FullAccount decodes the data on the 'slim RLP' format and return 64 // the consensus format account. 65 func FullAccount(data []byte) (Account, error) { 66 var account Account 67 if err := rlp.DecodeBytes(data, &account); err != nil { 68 return Account{}, err 69 } 70 if len(account.Root) == 0 { 71 account.Root = emptyRoot[:] 72 } 73 if len(account.CodeHash) == 0 { 74 account.CodeHash = emptyCode[:] 75 } 76 return account, nil 77 } 78 79 // FullAccountRLP converts data on the 'slim RLP' format into the full RLP-format. 80 func FullAccountRLP(data []byte) ([]byte, error) { 81 account, err := FullAccount(data) 82 if err != nil { 83 return nil, err 84 } 85 return rlp.EncodeToBytes(account) 86 }