-
Notifications
You must be signed in to change notification settings - Fork 44
/
statedb_mdl.go
106 lines (93 loc) · 3.31 KB
/
statedb_mdl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2023 The CortexTheseus Authors
// This file is part of the CortexFoundation library.
//
// The CortexFoundation library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The CortexFoundation library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the CortexFoundation library. If not, see <http://www.gnu.org/licenses/>.
package state
import (
"bytes"
"encoding/binary"
"math/big"
"github.com/CortexFoundation/CortexTheseus/common"
"github.com/CortexFoundation/CortexTheseus/crypto"
)
func (s *StateDB) GetUpload(addr common.Address) *big.Int {
stateObject := s.getStateObject(addr)
if stateObject != nil {
return stateObject.Upload()
}
return common.Big0
}
func (s *StateDB) GetNum(addr common.Address) *big.Int {
stateObject := s.getStateObject(addr)
if stateObject != nil {
return stateObject.Num()
}
return common.Big0
}
// GetState returns a value in account storage.
func (s *StateDB) GetSolidityBytes(addr common.Address, slot common.Hash) ([]byte, error) {
length := s.GetState(addr, slot).Big().Uint64()
if length == uint64(0) {
return nil, nil
}
hashBig := new(big.Int).SetBytes(crypto.Keccak256(slot.Bytes()))
//log.Warn(fmt.Sprintf("Pos %v, %v => %v, %v", addr, slot, length, hash))
//log.Trace("solid", "addr", addr, "slot", slot, "length", length, "hash", hash, "x", s.GetState(addr, slot), "y", common.Hash{})
// fmt.Println(fmt.Sprintf("Pos %v, %v => %v, %v", addr, slot, length, hash))
//buffSize := length * 32
buff := new(bytes.Buffer) //make([]byte, length * 32)
for i := int64(0); i < int64(length); i++ {
slotAddr := common.BigToHash(big.NewInt(0).Add(hashBig, big.NewInt(i)))
payload := s.GetState(addr, slotAddr).Bytes()
//copy(buff[idx*32:], payload[:])
binary.Write(buff, binary.LittleEndian, payload[:])
//binary.LittleEndian.Put(buff[idx*32:], payload[:])
// fmt.Println(fmt.Sprintf("load[%v]: %x, %x => %x, %x", idx, addr, slotAddr, payload, hash))
}
// fmt.Println(fmt.Sprintf("data: %v", buff))
return buff.Bytes(), nil
}
func (s *StateDB) Upload(addr common.Address) *big.Int {
stateObject := s.getStateObject(addr)
if stateObject != nil {
return stateObject.Upload()
}
return nil
}
func (s *StateDB) SetUpload(addr common.Address, amount *big.Int) {
stateObject := s.getOrNewStateObject(addr)
if stateObject != nil {
stateObject.SetUpload(amount)
}
}
func (s *StateDB) SetNum(addr common.Address, num *big.Int) {
stateObject := s.getOrNewStateObject(addr)
if stateObject != nil {
stateObject.SetNum(num)
}
}
func (s *StateDB) Uploading(addr common.Address) bool {
stateObject := s.getStateObject(addr)
if stateObject != nil {
return stateObject.Upload().Sign() > 0
}
return false
}
func (s *StateDB) SubUpload(addr common.Address, amount *big.Int) *big.Int {
stateObject := s.getOrNewStateObject(addr)
if stateObject != nil {
return stateObject.SubUpload(amount)
}
return big0
}