forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 58
/
trc21_reader.go
154 lines (143 loc) · 4.94 KB
/
trc21_reader.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package state
import (
"bytes"
"math/big"
"github.com/XinFinOrg/XDPoSChain/common"
lru "github.com/hashicorp/golang-lru"
)
var (
SlotTRC21Issuer = map[string]uint64{
"minCap": 0,
"tokens": 1,
"tokensState": 2,
}
SlotTRC21Token = map[string]uint64{
"balances": 0,
"minFee": 1,
"issuer": 2,
}
transferFuncHex = common.Hex2Bytes("0xa9059cbb")
transferFromFuncHex = common.Hex2Bytes("0x23b872dd")
cache, _ = lru.NewARC(128)
)
func GetTRC21FeeCapacityFromStateWithCache(trieRoot common.Hash, statedb *StateDB) map[common.Address]*big.Int {
if statedb == nil {
return map[common.Address]*big.Int{}
}
data, _ := cache.Get(trieRoot)
var info map[common.Address]*big.Int
if data != nil {
info = data.(map[common.Address]*big.Int)
} else {
info = GetTRC21FeeCapacityFromState(statedb)
}
cache.Add(trieRoot, info)
tokensFee := map[common.Address]*big.Int{}
for key, value := range info {
tokensFee[key] = big.NewInt(0).SetBytes(value.Bytes())
}
return tokensFee
}
func GetTRC21FeeCapacityFromState(statedb *StateDB) map[common.Address]*big.Int {
if statedb == nil {
return map[common.Address]*big.Int{}
}
tokensCapacity := map[common.Address]*big.Int{}
slotTokens := SlotTRC21Issuer["tokens"]
slotTokensHash := common.BigToHash(new(big.Int).SetUint64(slotTokens))
slotTokensState := SlotTRC21Issuer["tokensState"]
tokenCount := statedb.GetState(common.TRC21IssuerSMC, slotTokensHash).Big().Uint64()
for i := uint64(0); i < tokenCount; i++ {
key := GetLocDynamicArrAtElement(slotTokensHash, i, 1)
value := statedb.GetState(common.TRC21IssuerSMC, key)
if !common.EmptyHash(value) {
token := common.BytesToAddress(value.Bytes())
balanceKey := GetLocMappingAtKey(token.Hash(), slotTokensState)
balanceHash := statedb.GetState(common.TRC21IssuerSMC, common.BigToHash(balanceKey))
tokensCapacity[common.BytesToAddress(token.Bytes())] = balanceHash.Big()
}
}
return tokensCapacity
}
func PayFeeWithTRC21TxFail(statedb *StateDB, from common.Address, token common.Address) {
if statedb == nil {
return
}
slotBalanceTrc21 := SlotTRC21Token["balances"]
balanceKey := GetLocMappingAtKey(from.Hash(), slotBalanceTrc21)
balanceHash := statedb.GetState(token, common.BigToHash(balanceKey))
if !common.EmptyHash(balanceHash) {
balance := balanceHash.Big()
feeUsed := big.NewInt(0)
if balance.Cmp(feeUsed) <= 0 {
return
}
issuerTokenKey := GetLocSimpleVariable(SlotTRC21Token["issuer"])
if common.EmptyHash(issuerTokenKey) {
return
}
issuerAddr := common.BytesToAddress(statedb.GetState(token, issuerTokenKey).Bytes())
feeTokenKey := GetLocSimpleVariable(SlotTRC21Token["minFee"])
feeHash := statedb.GetState(token, feeTokenKey)
fee := feeHash.Big()
if balance.Cmp(fee) < 0 {
feeUsed = balance
} else {
feeUsed = fee
}
balance = balance.Sub(balance, feeUsed)
statedb.SetState(token, common.BigToHash(balanceKey), common.BigToHash(balance))
issuerBalanceKey := GetLocMappingAtKey(issuerAddr.Hash(), slotBalanceTrc21)
issuerBalanceHash := statedb.GetState(token, common.BigToHash(issuerBalanceKey))
issuerBalance := issuerBalanceHash.Big()
issuerBalance = issuerBalance.Add(issuerBalance, feeUsed)
statedb.SetState(token, common.BigToHash(issuerBalanceKey), common.BigToHash(issuerBalance))
}
}
func ValidateTRC21Tx(statedb *StateDB, from common.Address, token common.Address, data []byte) bool {
if data == nil || statedb == nil {
return false
}
slotBalanceTrc21 := SlotTRC21Token["balances"]
balanceKey := GetLocMappingAtKey(from.Hash(), slotBalanceTrc21)
balanceHash := statedb.GetState(token, common.BigToHash(balanceKey))
if !common.EmptyHash(balanceHash) {
balance := balanceHash.Big()
minFeeTokenKey := GetLocSimpleVariable(SlotTRC21Token["minFee"])
minFeeHash := statedb.GetState(token, minFeeTokenKey)
requiredMinBalance := minFeeHash.Big()
funcHex := data[:4]
value := big.NewInt(0)
if bytes.Equal(funcHex, transferFuncHex) && len(data) == 68 {
value = common.BytesToHash(data[36:]).Big()
} else {
if bytes.Equal(funcHex, transferFromFuncHex) && len(data) == 80 {
value = common.BytesToHash(data[68:]).Big()
}
}
requiredMinBalance = requiredMinBalance.Add(requiredMinBalance, value)
if balance.Cmp(requiredMinBalance) < 0 {
return false
} else {
return true
}
} else {
// we both accept tx with balance = 0 and fee = 0
minFeeTokenKey := GetLocSimpleVariable(SlotTRC21Token["minFee"])
if !common.EmptyHash(minFeeTokenKey) {
return true
}
}
return false
}
func UpdateTRC21Fee(statedb *StateDB, newBalance map[common.Address]*big.Int, totalFeeUsed *big.Int) {
if statedb == nil || len(newBalance) == 0 {
return
}
slotTokensState := SlotTRC21Issuer["tokensState"]
for token, value := range newBalance {
balanceKey := GetLocMappingAtKey(token.Hash(), slotTokensState)
statedb.SetState(common.TRC21IssuerSMC, common.BigToHash(balanceKey), common.BigToHash(value))
}
statedb.SubBalance(common.TRC21IssuerSMC, totalFeeUsed)
}