-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_dct_read.go
173 lines (144 loc) · 4.94 KB
/
account_dct_read.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package dctconvert
import (
"bytes"
"fmt"
"math/big"
"github.com/bhagyaraj1208117/andes-core-go/data/dct"
)
// MockDCTData groups together all instances of a token (same token name, different nonces).
type MockDCTData struct {
TokenIdentifier []byte
Instances []*dct.DCToken
LastNonce uint64
Roles [][]byte
}
const (
dctIdentifierSeparator = "-"
dctRandomSequenceLength = 6
)
// GetTokenBalance returns the DCT balance of the account, specified by the
// token key.
func GetTokenBalance(tokenIdentifier []byte, nonce uint64, source map[string][]byte) (*big.Int, error) {
tokenData, err := GetTokenData(tokenIdentifier, nonce, source, make(map[string][]byte))
if err != nil {
return nil, err
}
return tokenData.Value, nil
}
// GetTokenData gets the DCT information related to a token from the storage of the account.
func GetTokenData(tokenIdentifier []byte, nonce uint64, source map[string][]byte, systemAccStorage map[string][]byte) (*dct.DCToken, error) {
tokenKey := makeTokenKey(tokenIdentifier, nonce)
return getTokenDataByKey(tokenKey, source, systemAccStorage)
}
func getTokenDataByKey(tokenKey []byte, source map[string][]byte, systemAccStorage map[string][]byte) (*dct.DCToken, error) {
// default value copied from the protocol
dctData := &dct.DCToken{
Value: big.NewInt(0),
}
marshaledData := source[string(tokenKey)]
if len(marshaledData) == 0 {
return dctData, nil
}
err := dctDataMarshalizer.Unmarshal(dctData, marshaledData)
if err != nil {
return nil, err
}
marshaledData = systemAccStorage[string(tokenKey)]
if len(marshaledData) == 0 {
return dctData, nil
}
dctDataFromSystemAcc := &dct.DCToken{}
err = dctDataMarshalizer.Unmarshal(dctDataFromSystemAcc, marshaledData)
if err != nil {
return nil, err
}
dctData.TokenMetaData = dctDataFromSystemAcc.TokenMetaData
return dctData, nil
}
// GetTokenRoles returns the roles of the account for the specified tokenName.
func GetTokenRoles(tokenName []byte, source map[string][]byte) ([][]byte, error) {
tokenRolesKey := makeTokenRolesKey(tokenName)
tokenRolesData := &dct.DCTRoles{
Roles: make([][]byte, 0),
}
marshaledData := source[string(tokenRolesKey)]
if len(marshaledData) == 0 {
return tokenRolesData.Roles, nil
}
err := dctDataMarshalizer.Unmarshal(tokenRolesData, marshaledData)
if err != nil {
return nil, err
}
return tokenRolesData.Roles, nil
}
// GetFullMockDCTData returns the information about all the DCT tokens held by the account.
func GetFullMockDCTData(source map[string][]byte, systemAccStorage map[string][]byte) (map[string]*MockDCTData, error) {
resultMap := make(map[string]*MockDCTData)
for key := range source {
storageKeyBytes := []byte(key)
if isTokenKey(storageKeyBytes) {
tokenName, tokenInstance, err := loadMockDCTDataInstance(storageKeyBytes, source, systemAccStorage)
if err != nil {
return nil, err
}
if tokenInstance.Value.Sign() > 0 {
resultObj := getOrCreateMockDCTData(tokenName, resultMap)
resultObj.Instances = append(resultObj.Instances, tokenInstance)
}
} else if isNonceKey(storageKeyBytes) {
tokenName := key[len(dctNonceKeyPrefix):]
resultObj := getOrCreateMockDCTData(tokenName, resultMap)
resultObj.LastNonce = big.NewInt(0).SetBytes(source[key]).Uint64()
} else if isRoleKey(storageKeyBytes) {
tokenName := key[len(dctRoleKeyPrefix):]
roles, err := GetTokenRoles([]byte(tokenName), source)
if err != nil {
return nil, err
}
resultObj := getOrCreateMockDCTData(tokenName, resultMap)
resultObj.Roles = roles
}
}
return resultMap, nil
}
func extractTokenIdentifierAndNonceDCTWipe(args []byte) ([]byte, uint64) {
argsSplit := bytes.Split(args, []byte(dctIdentifierSeparator))
if len(argsSplit) < 2 {
return args, 0
}
if len(argsSplit[1]) <= dctRandomSequenceLength {
return args, 0
}
identifier := []byte(fmt.Sprintf("%s-%s", argsSplit[0], argsSplit[1][:dctRandomSequenceLength]))
nonce := big.NewInt(0).SetBytes(argsSplit[1][dctRandomSequenceLength:])
return identifier, nonce.Uint64()
}
// loads and prepared the DCT instance
func loadMockDCTDataInstance(tokenKey []byte, source map[string][]byte, systemAccStorage map[string][]byte) (string, *dct.DCToken, error) {
tokenInstance, err := getTokenDataByKey(tokenKey, source, systemAccStorage)
if err != nil {
return "", nil, err
}
tokenNameFromKey := getTokenNameFromKey(tokenKey)
tokenName, nonce := extractTokenIdentifierAndNonceDCTWipe(tokenNameFromKey)
if tokenInstance.TokenMetaData == nil {
tokenInstance.TokenMetaData = &dct.MetaData{
Name: tokenName,
Nonce: nonce,
}
}
return string(tokenName), tokenInstance, nil
}
func getOrCreateMockDCTData(tokenName string, resultMap map[string]*MockDCTData) *MockDCTData {
resultObj := resultMap[tokenName]
if resultObj == nil {
resultObj = &MockDCTData{
TokenIdentifier: []byte(tokenName),
Instances: nil,
LastNonce: 0,
Roles: nil,
}
resultMap[tokenName] = resultObj
}
return resultObj
}