Skip to content

Commit 892cdb5

Browse files
committed
all: rework trc21
1 parent 73061af commit 892cdb5

File tree

11 files changed

+56
-48
lines changed

11 files changed

+56
-48
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM
754754
SkipNonceChecks: true,
755755
SkipFromEOACheck: true,
756756
}
757-
feeCapacity := state.GetTRC21FeeCapacityFromState(stateDB)
757+
feeCapacity := stateDB.GetTRC21FeeCapacityFromState()
758758
if msg.To != nil {
759759
if value, ok := feeCapacity[*msg.To]; ok {
760760
msg.BalanceTokenFee = value

core/blockchain.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,7 @@ func (bc *BlockChain) processBlock(block *types.Block, parent *types.Header, sta
19101910
bc.reportBlock(block, nil, err)
19111911
return nil, err
19121912
}
1913-
feeCapacity := state.GetTRC21FeeCapacityFromStateWithCache(parent.Root, statedb)
1913+
feeCapacity := statedb.GetTRC21FeeCapacityFromStateWithCache(parent.Root)
19141914
receipts, logs, usedGas, err := bc.processor.Process(block, statedb, tradingState, bc.vmConfig, feeCapacity)
19151915
if err != nil {
19161916
bc.reportBlock(block, receipts, err)
@@ -2195,7 +2195,7 @@ func (bc *BlockChain) getResultBlock(block *types.Block, verifiedM2 bool) (*Resu
21952195
bc.reportBlock(block, nil, err)
21962196
return nil, err
21972197
}
2198-
feeCapacity := state.GetTRC21FeeCapacityFromStateWithCache(parent.Root, statedb)
2198+
feeCapacity := statedb.GetTRC21FeeCapacityFromStateWithCache(parent.Root)
21992199
receipts, logs, usedGas, err := bc.processor.ProcessBlockNoValidator(calculatedBlock, statedb, tradingState, bc.vmConfig, feeCapacity)
22002200
process := time.Since(bstart)
22012201
if err != nil {

core/chain_makers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (b *BlockGen) addTx(bc *BlockChain, vmConfig vm.Config, tx *types.Transacti
8585
if b.gasPool == nil {
8686
b.SetCoinbase(common.Address{})
8787
}
88-
feeCapacity := state.GetTRC21FeeCapacityFromState(b.statedb)
88+
feeCapacity := b.statedb.GetTRC21FeeCapacityFromState()
8989
b.statedb.SetTxContext(tx.Hash(), len(b.txs))
9090
receipt, gas, tokenFeeUsed, err := ApplyTransaction(b.config, feeCapacity, bc, &b.header.Coinbase, b.gasPool, b.statedb, nil, b.header, tx, &b.header.GasUsed, vmConfig)
9191
if err != nil {
@@ -95,7 +95,7 @@ func (b *BlockGen) addTx(bc *BlockChain, vmConfig vm.Config, tx *types.Transacti
9595
b.receipts = append(b.receipts, receipt)
9696
if tokenFeeUsed {
9797
fee := common.GetGasFee(b.header.Number.Uint64(), gas)
98-
state.UpdateTRC21Fee(b.statedb, map[common.Address]*big.Int{*tx.To(): new(big.Int).Sub(feeCapacity[*tx.To()], new(big.Int).SetUint64(gas))}, fee)
98+
b.statedb.UpdateTRC21Fee(map[common.Address]*big.Int{*tx.To(): new(big.Int).Sub(feeCapacity[*tx.To()], new(big.Int).SetUint64(gas))}, fee)
9999
}
100100
}
101101

core/state/trc21_reader.go

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

core/state_processor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, tra
144144
totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee)
145145
}
146146
}
147-
state.UpdateTRC21Fee(statedb, balanceUpdated, totalFeeUsed)
147+
statedb.UpdateTRC21Fee(balanceUpdated, totalFeeUsed)
148148
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
149149
p.engine.Finalize(p.bc, header, statedb, parentState, block.Transactions(), block.Uncles(), receipts)
150150
return receipts, allLogs, *usedGas, nil
@@ -238,7 +238,7 @@ func (p *StateProcessor) ProcessBlockNoValidator(cBlock *CalculatedBlock, stated
238238
totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee)
239239
}
240240
}
241-
state.UpdateTRC21Fee(statedb, balanceUpdated, totalFeeUsed)
241+
statedb.UpdateTRC21Fee(balanceUpdated, totalFeeUsed)
242242
// Finalize the block, applying any consensus engine specific extras (e.g. block rewards)
243243
p.engine.Finalize(p.bc, header, statedb, parentState, block.Transactions(), block.Uncles(), receipts)
244244
return receipts, allLogs, *usedGas, nil
@@ -441,7 +441,7 @@ func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPo
441441
*usedGas += result.UsedGas
442442

443443
if balanceFee != nil && result.Failed() {
444-
state.PayFeeWithTRC21TxFail(statedb, msg.From, *to)
444+
statedb.PayFeeWithTRC21TxFail(msg.From, *to)
445445
}
446446

447447
return MakeReceipt(evm, result, statedb, blockNumber, blockHash, tx, *usedGas, root), result.UsedGas, balanceFee != nil, nil

core/token_validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func CallContractWithState(call ethereum.CallMsg, chain consensus.ChainContext,
9999
SkipNonceChecks: true,
100100
SkipFromEOACheck: true,
101101
}
102-
feeCapacity := state.GetTRC21FeeCapacityFromState(statedb)
102+
feeCapacity := statedb.GetTRC21FeeCapacityFromState()
103103
if msg.To != nil {
104104
if value, ok := feeCapacity[*msg.To]; ok {
105105
msg.BalanceTokenFee = value

core/txpool/txpool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
701701
if tx.To() != nil {
702702
if value, ok := pool.trc21FeeCapacity[*tx.To()]; ok {
703703
feeCapacity = value
704-
if !state.ValidateTRC21Tx(pool.currentState, from, *tx.To(), tx.Data()) {
704+
if !pool.currentState.ValidateTRC21Tx(from, *tx.To(), tx.Data()) {
705705
return core.ErrInsufficientFunds
706706
}
707707
cost = tx.TxCost(number)
@@ -1442,7 +1442,7 @@ func (pool *TxPool) reset(oldHead, newHead *types.Header) {
14421442
return
14431443
}
14441444
pool.currentState = statedb
1445-
pool.trc21FeeCapacity = state.GetTRC21FeeCapacityFromStateWithCache(newHead.Root, statedb)
1445+
pool.trc21FeeCapacity = statedb.GetTRC21FeeCapacityFromStateWithCache(newHead.Root)
14461446
pool.pendingNonces = newNoncer(statedb)
14471447
pool.currentMaxGas = newHead.GasLimit
14481448

eth/state_accessor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block,
211211
}
212212
// Recompute transactions up to the target index.
213213
signer := types.MakeSigner(eth.blockchain.Config(), block.Number())
214-
feeCapacity := state.GetTRC21FeeCapacityFromState(statedb)
214+
feeCapacity := statedb.GetTRC21FeeCapacityFromState()
215215
for idx, tx := range block.Transactions() {
216216
var balance *big.Int
217217
if tx.To() != nil {

eth/tracers/api.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func (api *API) traceChain(start, end *types.Block, config *TraceConfig, closed
264264
signer = types.MakeSigner(api.backend.ChainConfig(), task.block.Number())
265265
blockCtx = core.NewEVMBlockContext(task.block.Header(), api.chainContext(ctx), nil)
266266
)
267-
feeCapacity := state.GetTRC21FeeCapacityFromState(task.statedb)
267+
feeCapacity := task.statedb.GetTRC21FeeCapacityFromState()
268268
// Trace all the transactions contained within
269269
for i, tx := range task.block.Transactions() {
270270
var balance *big.Int
@@ -513,7 +513,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config
513513
vmctx = core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil)
514514
deleteEmptyObjects = chainConfig.IsEIP158(block.Number())
515515
)
516-
feeCapacity := state.GetTRC21FeeCapacityFromState(statedb)
516+
feeCapacity := statedb.GetTRC21FeeCapacityFromState()
517517
for i, tx := range block.Transactions() {
518518
if err := ctx.Err(); err != nil {
519519
return nil, err
@@ -593,7 +593,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac
593593
signer = types.MakeSigner(api.backend.ChainConfig(), block.Number())
594594
results = make([]*txTraceResult, len(txs))
595595
)
596-
feeCapacity := state.GetTRC21FeeCapacityFromState(statedb)
596+
feeCapacity := statedb.GetTRC21FeeCapacityFromState()
597597
for i, tx := range txs {
598598
var balance *big.Int
599599
if tx.To() != nil {
@@ -644,7 +644,7 @@ func (api *API) traceBlockParallel(ctx context.Context, block *types.Block, stat
644644
defer pend.Done()
645645
// Fetch and execute the next transaction trace tasks
646646
for task := range jobs {
647-
feeCapacity := state.GetTRC21FeeCapacityFromState(task.statedb)
647+
feeCapacity := task.statedb.GetTRC21FeeCapacityFromState()
648648
var balance *big.Int
649649
if txs[task.index].To() != nil {
650650
if value, ok := feeCapacity[*txs[task.index].To()]; ok {
@@ -675,7 +675,7 @@ func (api *API) traceBlockParallel(ctx context.Context, block *types.Block, stat
675675
}
676676

677677
// Feed the transactions into the tracers and return
678-
feeCapacity := state.GetTRC21FeeCapacityFromState(statedb)
678+
feeCapacity := statedb.GetTRC21FeeCapacityFromState()
679679
var failed error
680680
blockCtx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil)
681681
txloop:
@@ -751,7 +751,7 @@ func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config *
751751
return nil, err
752752
}
753753
defer release()
754-
feeCapacity := state.GetTRC21FeeCapacityFromState(statedb)
754+
feeCapacity := statedb.GetTRC21FeeCapacityFromState()
755755
var balance *big.Int
756756
if tx.To() != nil {
757757
if value, ok := feeCapacity[*tx.To()]; ok {
@@ -900,7 +900,7 @@ func (api *API) traceTx(ctx context.Context, tx *types.Transaction, message *cor
900900
}()
901901
defer cancel()
902902

903-
feeCapacity := state.GetTRC21FeeCapacityFromState(statedb)
903+
feeCapacity := statedb.GetTRC21FeeCapacityFromState()
904904
var balance *big.Int
905905
if tx.To() != nil {
906906
if value, ok := feeCapacity[*tx.To()]; ok {

internal/ethapi/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1842,7 +1842,7 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH
18421842
args.AccessList = &accessList
18431843
msg := args.ToMessage(b, header.BaseFee, true, true)
18441844

1845-
feeCapacity := state.GetTRC21FeeCapacityFromState(statedb)
1845+
feeCapacity := statedb.GetTRC21FeeCapacityFromState()
18461846
var balanceTokenFee *big.Int
18471847
if value, ok := feeCapacity[to]; ok {
18481848
balanceTokenFee = value

0 commit comments

Comments
 (0)