github.com/aergoio/aergo@v1.3.1/types/state.go (about) 1 package types 2 3 import ( 4 "bytes" 5 "fmt" 6 "math/big" 7 "reflect" 8 9 "github.com/aergoio/aergo/internal/common" 10 "github.com/aergoio/aergo/internal/enc" 11 ) 12 13 const ( 14 HashIDLength = 32 15 ) 16 17 // HashID is a fixed size bytes 18 type HashID [HashIDLength]byte 19 20 // BlockID is a HashID to identify a block 21 type BlockID HashID 22 23 // AccountID is a HashID to identify an account 24 type AccountID HashID 25 26 // TxID is a HashID to identify a transaction 27 type TxID HashID 28 29 // ImplHashID is a object has HashID 30 type ImplHashID interface { 31 HashID() HashID 32 } 33 34 // ImplHashBytes is a object supports Hash 35 type ImplHashBytes interface { 36 Hash() []byte 37 } 38 39 // ImplMarshal is a object has marshal interface 40 type ImplMarshal interface { 41 Marshal() ([]byte, error) 42 } 43 44 var ( 45 emptyHashID = HashID{} 46 ) 47 48 // GetHashID make a HashID from hash of bytes 49 func GetHashID(bytes ...[]byte) HashID { 50 hash := common.Hasher(bytes...) 51 return ToHashID(hash) 52 } 53 54 // ToHashID make a HashID from bytes 55 func ToHashID(hash []byte) HashID { 56 buf := HashID{} 57 copy(buf[:], hash) 58 return HashID(buf) 59 } 60 func (id HashID) String() string { 61 return enc.ToString(id[:]) 62 } 63 64 // Bytes make a byte slice from id 65 func (id HashID) Bytes() []byte { 66 if id == emptyHashID { 67 return nil 68 } 69 return id[:] 70 } 71 72 // Compare returns an integer comparing two HashIDs as byte slices. 73 func (id HashID) Compare(alt HashID) int { 74 return bytes.Compare(id.Bytes(), alt.Bytes()) 75 } 76 77 // Equal returns a boolean comparing two HashIDs as byte slices. 78 func (id HashID) Equal(alt HashID) bool { 79 return bytes.Equal(id.Bytes(), alt.Bytes()) 80 } 81 82 // ParseToTxID parse BlockID from bytes. it return error if length of parameter is not fit. 83 func ParseToBlockID(blockHash []byte) (BlockID, error) { 84 var hash BlockID 85 if len(blockHash) != HashIDLength { 86 return hash, fmt.Errorf("parse error: invalid length") 87 } 88 copy(hash[:], blockHash) 89 return hash, nil 90 } 91 92 // MustParseBlockID parse, it panics if parsing is failed. 93 func MustParseBlockID(blockHash []byte) BlockID { 94 hash, err := ParseToBlockID(blockHash) 95 if err != nil { 96 panic(err) 97 } 98 return hash 99 } 100 101 // ToBlockID make a BlockID from bytes 102 func ToBlockID(blockHash []byte) BlockID { 103 return BlockID(ToHashID(blockHash)) 104 } 105 func (id BlockID) String() string { 106 return HashID(id).String() 107 } 108 109 // ParseToTxID parse TxID from bytes. it return error if length of parameter is not fit. 110 func ParseToTxID(txHash []byte) (TxID, error) { 111 var hash TxID 112 if len(txHash) != HashIDLength { 113 return hash, fmt.Errorf("parse error: invalid length") 114 } 115 return ToTxID(txHash), nil 116 } 117 118 // ToTxID make a TxID from bytes 119 func ToTxID(txHash []byte) TxID { 120 return TxID(ToHashID(txHash)) 121 } 122 func (id TxID) String() string { 123 return HashID(id).String() 124 } 125 126 // ToAccountID make a AccountHash from bytes 127 func ToAccountID(account []byte) AccountID { 128 return AccountID(GetHashID(account)) 129 } 130 func (id AccountID) String() string { 131 return HashID(id).String() 132 } 133 134 // NewState returns an instance of account state 135 func NewState() *State { 136 return &State{ 137 Nonce: 0, 138 Balance: []byte{0}, 139 SqlRecoveryPoint: uint64(1), 140 } 141 } 142 143 // func (st *State) IsEmpty() bool { 144 // return st.Nonce == 0 && st.Balance == 0 145 // } 146 147 // func (st *State) GetHash() []byte { 148 // digest := sha256.New() 149 // binary.Write(digest, binary.LittleEndian, st.Nonce) 150 // binary.Write(digest, binary.LittleEndian, st.Balance) 151 // return digest.Sum(nil) 152 // } 153 154 // func (st *State) Clone() *State { 155 // if st == nil { 156 // return nil 157 // } 158 // return &State{ 159 // Nonce: st.Nonce, 160 // Balance: st.Balance, 161 // CodeHash: st.CodeHash, 162 // StorageRoot: st.StorageRoot, 163 // } 164 // } 165 166 func Clone(i interface{}) interface{} { 167 if i == nil { 168 return nil 169 } 170 return reflect.Indirect(reflect.ValueOf(i)).Interface() 171 } 172 173 func (st *State) GetBalanceBigInt() *big.Int { 174 if st == nil { 175 return new(big.Int).SetUint64(0) 176 } 177 return new(big.Int).SetBytes(st.Balance) 178 }