github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/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/tacshi/go-ethereum/common" 24 "github.com/tacshi/go-ethereum/core/types" 25 "github.com/tacshi/go-ethereum/rlp" 26 ) 27 28 // Account is a modified version of a state.Account, where the root is replaced 29 // with a byte slice. This format can be used to represent full-consensus format 30 // or slim-snapshot format which replaces the empty root and code hash as nil 31 // byte slice. 32 type Account struct { 33 Nonce uint64 34 Balance *big.Int 35 Root []byte 36 CodeHash []byte 37 } 38 39 // SlimAccount converts a state.Account content into a slim snapshot account 40 func SlimAccount(nonce uint64, balance *big.Int, root common.Hash, codehash []byte) Account { 41 slim := Account{ 42 Nonce: nonce, 43 Balance: balance, 44 } 45 if root != types.EmptyRootHash { 46 slim.Root = root[:] 47 } 48 if !bytes.Equal(codehash, types.EmptyCodeHash[:]) { 49 slim.CodeHash = codehash 50 } 51 return slim 52 } 53 54 // SlimAccountRLP converts a state.Account content into a slim snapshot 55 // version RLP encoded. 56 func SlimAccountRLP(nonce uint64, balance *big.Int, root common.Hash, codehash []byte) []byte { 57 data, err := rlp.EncodeToBytes(SlimAccount(nonce, balance, root, codehash)) 58 if err != nil { 59 panic(err) 60 } 61 return data 62 } 63 64 // FullAccount decodes the data on the 'slim RLP' format and return 65 // the consensus format account. 66 func FullAccount(data []byte) (Account, error) { 67 var account Account 68 if err := rlp.DecodeBytes(data, &account); err != nil { 69 return Account{}, err 70 } 71 if len(account.Root) == 0 { 72 account.Root = types.EmptyRootHash[:] 73 } 74 if len(account.CodeHash) == 0 { 75 account.CodeHash = types.EmptyCodeHash[:] 76 } 77 return account, nil 78 } 79 80 // FullAccountRLP converts data on the 'slim RLP' format into the full RLP-format. 81 func FullAccountRLP(data []byte) ([]byte, error) { 82 account, err := FullAccount(data) 83 if err != nil { 84 return nil, err 85 } 86 return rlp.EncodeToBytes(account) 87 }