-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseAccounts.go
169 lines (159 loc) · 5.2 KB
/
parseAccounts.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
package scenjsonparse
import (
"errors"
"fmt"
oj "github.com/Dharitri-org/drtg-scenario/orderedjson"
scenmodel "github.com/Dharitri-org/drtg-scenario/scenario/model"
)
func (p *Parser) parseAccountAddress(addrRaw string) (scenmodel.JSONBytesFromString, error) {
if len(addrRaw) == 0 {
return scenmodel.JSONBytesFromString{}, errors.New("missing account address")
}
addrBytes, err := p.ExprInterpreter.InterpretString(addrRaw)
if err == nil && len(addrBytes) != 32 {
return scenmodel.JSONBytesFromString{}, errors.New("account address is not 32 bytes in length")
}
return scenmodel.NewJSONBytesFromString(addrBytes, addrRaw), err
}
func (p *Parser) processAccount(acctRaw oj.OJsonObject) (*scenmodel.Account, error) {
acctMap, isMap := acctRaw.(*oj.OJsonMap)
if !isMap {
return nil, errors.New("unmarshalled account object is not a map")
}
acct := scenmodel.Account{
Shard: scenmodel.JSONUint64Zero(),
IsSmartContract: false,
Comment: "",
Nonce: scenmodel.JSONUint64Zero(),
Balance: scenmodel.JSONBigIntZero(),
Username: scenmodel.JSONBytesEmpty(),
Storage: nil,
Code: scenmodel.JSONBytesEmpty(),
CodeMetadata: scenmodel.JSONBytesEmpty(),
Owner: scenmodel.JSONBytesEmpty(),
AsyncCallData: "",
DCTData: nil,
Update: false,
DeveloperReward: scenmodel.JSONBigIntZero(),
}
var err error
for _, kvp := range acctMap.OrderedKV {
switch kvp.Key {
case "comment":
acct.Comment, err = p.parseString(kvp.Value)
if err != nil {
return nil, fmt.Errorf("invalid account comment: %w", err)
}
case "update":
acct.Update, err = p.parseBool(kvp.Value)
if err != nil {
return nil, fmt.Errorf("invalid update flag bool: %w", err)
}
case "shard":
acct.Shard, err = p.processUint64(kvp.Value)
if err != nil {
return nil, fmt.Errorf("invalid shard number: %w", err)
}
case "nonce":
acct.Nonce, err = p.processUint64(kvp.Value)
if err != nil {
return nil, errors.New("invalid account nonce")
}
case "balance":
acct.Balance, err = p.processBigInt(kvp.Value, bigIntUnsignedBytes)
if err != nil {
return nil, errors.New("invalid account balance")
}
case "dct":
dctMap, dctOk := kvp.Value.(*oj.OJsonMap)
if !dctOk {
return nil, errors.New("invalid DCT map")
}
for _, dctKvp := range dctMap.OrderedKV {
tokenNameStr, err := p.ExprInterpreter.InterpretString(dctKvp.Key)
if err != nil {
return nil, fmt.Errorf("invalid dct token identifer: %w", err)
}
tokenName := scenmodel.NewJSONBytesFromString(tokenNameStr, dctKvp.Key)
dctItem, err := p.processDCTData(tokenName, dctKvp.Value)
if err != nil {
return nil, fmt.Errorf("invalid dct value: %w", err)
}
acct.DCTData = append(acct.DCTData, dctItem)
}
case "username":
acct.Username, err = p.processStringAsByteArray(kvp.Value)
if err != nil {
return nil, fmt.Errorf("invalid account username: %w", err)
}
case "storage":
storageMap, storageOk := kvp.Value.(*oj.OJsonMap)
if !storageOk {
return nil, errors.New("invalid account storage")
}
for _, storageKvp := range storageMap.OrderedKV {
byteKey, err := p.ExprInterpreter.InterpretString(storageKvp.Key)
if err != nil {
return nil, fmt.Errorf("invalid account storage key: %w", err)
}
byteVal, err := p.processSubTreeAsByteArray(storageKvp.Value)
if err != nil {
return nil, fmt.Errorf("invalid account storage value: %w", err)
}
stElem := scenmodel.StorageKeyValuePair{
Key: scenmodel.NewJSONBytesFromString(byteKey, storageKvp.Key),
Value: byteVal,
}
acct.Storage = append(acct.Storage, &stElem)
}
case "code":
acct.Code, err = p.processStringAsByteArray(kvp.Value)
if err != nil {
return nil, fmt.Errorf("invalid account code: %w", err)
}
case "codeMetadata":
acct.CodeMetadata, err = p.processStringAsByteArray(kvp.Value)
if err != nil {
return nil, fmt.Errorf("invalid account codeMetadata: %w", err)
}
case "owner":
acct.Owner, err = p.processStringAsByteArray(kvp.Value)
if err != nil {
return nil, fmt.Errorf("invalid account owner: %w", err)
}
case "asyncCallData":
acct.AsyncCallData, err = p.parseString(kvp.Value)
if err != nil {
return nil, fmt.Errorf("invalid asyncCallData string: %w", err)
}
case "developerRewards":
acct.DeveloperReward, err = p.processBigInt(kvp.Value, bigIntUnsignedBytes)
if err != nil {
return nil, errors.New("invalid developerRewards")
}
default:
return nil, fmt.Errorf("unknown account field: %s", kvp.Key)
}
}
return &acct, nil
}
func (p *Parser) processAccountMap(acctMapRaw oj.OJsonObject) ([]*scenmodel.Account, error) {
var accounts []*scenmodel.Account
preMap, isPreMap := acctMapRaw.(*oj.OJsonMap)
if !isPreMap {
return nil, errors.New("unmarshalled account map object is not a map")
}
for _, acctKVP := range preMap.OrderedKV {
acct, acctErr := p.processAccount(acctKVP.Value)
if acctErr != nil {
return nil, acctErr
}
acctAddr, hexErr := p.parseAccountAddress(acctKVP.Key)
if hexErr != nil {
return nil, hexErr
}
acct.Address = acctAddr
accounts = append(accounts, acct)
}
return accounts, nil
}