@@ -25,50 +25,56 @@ var (
2525 cache = lru.NewCache [common.Hash , map [common.Address ]* big.Int ](128 )
2626)
2727
28- func GetTRC21FeeCapacityFromStateWithCache (trieRoot common.Hash , statedb * StateDB ) map [common.Address ]* big.Int {
29- if statedb == nil {
28+ func ( s * StateDB ) GetTRC21FeeCapacityFromStateWithCache (trieRoot common.Hash ) map [common.Address ]* big.Int {
29+ if s == nil {
3030 return map [common.Address ]* big.Int {}
3131 }
32+
3233 info , ok := cache .Get (trieRoot )
3334 if ! ok || info == nil {
34- info = GetTRC21FeeCapacityFromState (statedb )
35+ info = s . GetTRC21FeeCapacityFromState ()
3536 cache .Add (trieRoot , info )
3637 }
3738 tokensFee := map [common.Address ]* big.Int {}
3839 for key , value := range info {
3940 tokensFee [key ] = big .NewInt (0 ).SetBytes (value .Bytes ())
4041 }
42+
4143 return tokensFee
4244}
43- func GetTRC21FeeCapacityFromState (statedb * StateDB ) map [common.Address ]* big.Int {
44- if statedb == nil {
45+
46+ func (s * StateDB ) GetTRC21FeeCapacityFromState () map [common.Address ]* big.Int {
47+ if s == nil {
4548 return map [common.Address ]* big.Int {}
4649 }
50+
4751 tokensCapacity := map [common.Address ]* big.Int {}
4852 slotTokens := SlotTRC21Issuer ["tokens" ]
4953 slotTokensHash := common .BigToHash (new (big.Int ).SetUint64 (slotTokens ))
5054 slotTokensState := SlotTRC21Issuer ["tokensState" ]
51- tokenCount := statedb .GetState (common .TRC21IssuerSMC , slotTokensHash ).Big ().Uint64 ()
52- for i := uint64 ( 0 ); i < tokenCount ; i ++ {
55+ tokenCount := s .GetState (common .TRC21IssuerSMC , slotTokensHash ).Big ().Uint64 ()
56+ for i := range tokenCount {
5357 key := GetLocDynamicArrAtElement (slotTokensHash , i , 1 )
54- value := statedb .GetState (common .TRC21IssuerSMC , key )
58+ value := s .GetState (common .TRC21IssuerSMC , key )
5559 if ! value .IsZero () {
5660 token := common .BytesToAddress (value .Bytes ())
5761 balanceKey := GetLocMappingAtKey (token .Hash (), slotTokensState )
58- balanceHash := statedb .GetState (common .TRC21IssuerSMC , common .BigToHash (balanceKey ))
62+ balanceHash := s .GetState (common .TRC21IssuerSMC , common .BigToHash (balanceKey ))
5963 tokensCapacity [common .BytesToAddress (token .Bytes ())] = balanceHash .Big ()
6064 }
6165 }
66+
6267 return tokensCapacity
6368}
6469
65- func PayFeeWithTRC21TxFail ( statedb * StateDB , from common.Address , token common.Address ) {
66- if statedb == nil {
70+ func ( s * StateDB ) PayFeeWithTRC21TxFail ( from common.Address , token common.Address ) {
71+ if s == nil {
6772 return
6873 }
74+
6975 slotBalanceTrc21 := SlotTRC21Token ["balances" ]
7076 balanceKey := GetLocMappingAtKey (from .Hash (), slotBalanceTrc21 )
71- balanceHash := statedb .GetState (token , common .BigToHash (balanceKey ))
77+ balanceHash := s .GetState (token , common .BigToHash (balanceKey ))
7278 if ! balanceHash .IsZero () {
7379 balance := balanceHash .Big ()
7480 feeUsed := big .NewInt (0 )
@@ -79,38 +85,39 @@ func PayFeeWithTRC21TxFail(statedb *StateDB, from common.Address, token common.A
7985 if issuerTokenKey .IsZero () {
8086 return
8187 }
82- issuerAddr := common .BytesToAddress (statedb .GetState (token , issuerTokenKey ).Bytes ())
88+ issuerAddr := common .BytesToAddress (s .GetState (token , issuerTokenKey ).Bytes ())
8389 feeTokenKey := GetLocSimpleVariable (SlotTRC21Token ["minFee" ])
84- feeHash := statedb .GetState (token , feeTokenKey )
90+ feeHash := s .GetState (token , feeTokenKey )
8591 fee := feeHash .Big ()
8692 if balance .Cmp (fee ) < 0 {
8793 feeUsed = balance
8894 } else {
8995 feeUsed = fee
9096 }
9197 balance = balance .Sub (balance , feeUsed )
92- statedb .SetState (token , common .BigToHash (balanceKey ), common .BigToHash (balance ))
98+ s .SetState (token , common .BigToHash (balanceKey ), common .BigToHash (balance ))
9399
94100 issuerBalanceKey := GetLocMappingAtKey (issuerAddr .Hash (), slotBalanceTrc21 )
95- issuerBalanceHash := statedb .GetState (token , common .BigToHash (issuerBalanceKey ))
101+ issuerBalanceHash := s .GetState (token , common .BigToHash (issuerBalanceKey ))
96102 issuerBalance := issuerBalanceHash .Big ()
97103 issuerBalance = issuerBalance .Add (issuerBalance , feeUsed )
98- statedb .SetState (token , common .BigToHash (issuerBalanceKey ), common .BigToHash (issuerBalance ))
104+ s .SetState (token , common .BigToHash (issuerBalanceKey ), common .BigToHash (issuerBalance ))
99105 }
100106}
101107
102- func ValidateTRC21Tx ( statedb * StateDB , from common.Address , token common.Address , data []byte ) bool {
103- if data == nil || statedb == nil {
108+ func ( s * StateDB ) ValidateTRC21Tx ( from common.Address , token common.Address , data []byte ) bool {
109+ if s == nil || data == nil {
104110 return false
105111 }
112+
106113 slotBalanceTrc21 := SlotTRC21Token ["balances" ]
107114 balanceKey := GetLocMappingAtKey (from .Hash (), slotBalanceTrc21 )
108- balanceHash := statedb .GetState (token , common .BigToHash (balanceKey ))
115+ balanceHash := s .GetState (token , common .BigToHash (balanceKey ))
109116
110117 if ! balanceHash .IsZero () {
111118 balance := balanceHash .Big ()
112119 minFeeTokenKey := GetLocSimpleVariable (SlotTRC21Token ["minFee" ])
113- minFeeHash := statedb .GetState (token , minFeeTokenKey )
120+ minFeeHash := s .GetState (token , minFeeTokenKey )
114121 requiredMinBalance := minFeeHash .Big ()
115122 funcHex := data [:4 ]
116123 value := big .NewInt (0 )
@@ -138,14 +145,15 @@ func ValidateTRC21Tx(statedb *StateDB, from common.Address, token common.Address
138145 return false
139146}
140147
141- func UpdateTRC21Fee ( statedb * StateDB , newBalance map [common.Address ]* big.Int , totalFeeUsed * big.Int ) {
142- if statedb == nil || len (newBalance ) == 0 {
148+ func ( s * StateDB ) UpdateTRC21Fee ( newBalance map [common.Address ]* big.Int , totalFeeUsed * big.Int ) {
149+ if s == nil || len (newBalance ) == 0 {
143150 return
144151 }
152+
145153 slotTokensState := SlotTRC21Issuer ["tokensState" ]
146154 for token , value := range newBalance {
147155 balanceKey := GetLocMappingAtKey (token .Hash (), slotTokensState )
148- statedb .SetState (common .TRC21IssuerSMC , common .BigToHash (balanceKey ), common .BigToHash (value ))
156+ s .SetState (common .TRC21IssuerSMC , common .BigToHash (balanceKey ), common .BigToHash (value ))
149157 }
150- statedb .SubBalance (common .TRC21IssuerSMC , totalFeeUsed , tracing .BalanceChangeUnspecified )
158+ s .SubBalance (common .TRC21IssuerSMC , totalFeeUsed , tracing .BalanceChangeUnspecified )
151159}
0 commit comments