Skip to content

Commit 7072db7

Browse files
authored
Change all amount-related fields from bytes to decimal string in protobuf (iotexproject#628)
1 parent 844f380 commit 7072db7

25 files changed

+539
-550
lines changed

action/action.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (elp *Envelope) Proto() *iotextypes.ActionCore {
113113
GasLimit: elp.gasLimit,
114114
}
115115
if elp.gasPrice != nil {
116-
actCore.GasPrice = elp.gasPrice.Bytes()
116+
actCore.GasPrice = elp.gasPrice.String()
117117
}
118118

119119
// TODO assert each action
@@ -164,7 +164,7 @@ func (elp *Envelope) LoadProto(pbAct *iotextypes.ActionCore) error {
164164
elp.nonce = pbAct.GetNonce()
165165
elp.gasLimit = pbAct.GetGasLimit()
166166
elp.gasPrice = &big.Int{}
167-
elp.gasPrice.SetBytes(pbAct.GetGasPrice())
167+
elp.gasPrice.SetString(pbAct.GetGasPrice(), 10)
168168

169169
switch {
170170
case pbAct.GetTransfer() != nil:

action/claimreward.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,19 @@ func (c *ClaimFromRewardingFund) ByteStream() []byte {
4444
// Proto converts a claim action struct to a claim action protobuf
4545
func (c *ClaimFromRewardingFund) Proto() *iotextypes.ClaimFromRewardingFund {
4646
return &iotextypes.ClaimFromRewardingFund{
47-
Amount: c.amount.Bytes(),
47+
Amount: c.amount.String(),
4848
Data: c.data,
4949
}
5050
}
5151

5252
// LoadProto converts a claim action protobuf to a claim action struct
5353
func (c *ClaimFromRewardingFund) LoadProto(claim *iotextypes.ClaimFromRewardingFund) error {
5454
*c = ClaimFromRewardingFund{}
55-
c.amount = big.NewInt(0).SetBytes(claim.Amount)
55+
amount, ok := big.NewInt(0).SetString(claim.Amount, 10)
56+
if !ok {
57+
errors.New("failed to set claim amount")
58+
}
59+
c.amount = amount
5660
c.data = claim.Data
5761
return nil
5862
}

action/createdeposit.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ func (d *CreateDeposit) Proto() *iotextypes.CreateDeposit {
8383
ChainID: d.chainID,
8484
Recipient: d.recipient,
8585
}
86-
if d.amount != nil && len(d.amount.Bytes()) > 0 {
87-
act.Amount = d.amount.Bytes()
86+
if d.amount != nil && len(d.amount.String()) > 0 {
87+
act.Amount = d.amount.String()
8888
}
8989
return act
9090
}
@@ -102,7 +102,7 @@ func (d *CreateDeposit) LoadProto(pbDpst *iotextypes.CreateDeposit) error {
102102

103103
d.chainID = pbDpst.GetChainID()
104104
d.amount = big.NewInt(0)
105-
d.amount.SetBytes(pbDpst.GetAmount())
105+
d.amount.SetString(pbDpst.GetAmount(), 10)
106106
d.recipient = pbDpst.GetRecipient()
107107
return nil
108108
}

action/depositreward.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,19 @@ func (d *DepositToRewardingFund) ByteStream() []byte {
4444
// Proto converts a deposit action struct to a deposit action protobuf
4545
func (d *DepositToRewardingFund) Proto() *iotextypes.DepositToRewardingFund {
4646
return &iotextypes.DepositToRewardingFund{
47-
Amount: d.amount.Bytes(),
47+
Amount: d.amount.String(),
4848
Data: d.data,
4949
}
5050
}
5151

5252
// LoadProto converts a deposit action protobuf to a deposit action struct
5353
func (d *DepositToRewardingFund) LoadProto(deposit *iotextypes.DepositToRewardingFund) error {
5454
*d = DepositToRewardingFund{}
55-
d.amount = big.NewInt(0).SetBytes(deposit.Amount)
55+
amount, ok := big.NewInt(0).SetString(deposit.Amount, 10)
56+
if !ok {
57+
return errors.New("failed to set deposit amount")
58+
}
59+
d.amount = amount
5660
d.data = deposit.Data
5761
return nil
5862
}

action/execution.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ func (ex *Execution) Proto() *iotextypes.Execution {
9999
Contract: ex.contract,
100100
Data: ex.data,
101101
}
102-
if ex.amount != nil && len(ex.amount.Bytes()) > 0 {
103-
act.Amount = ex.amount.Bytes()
102+
if ex.amount != nil && len(ex.amount.String()) > 0 {
103+
act.Amount = ex.amount.String()
104104
}
105105
return act
106106
}
@@ -117,7 +117,7 @@ func (ex *Execution) LoadProto(pbAct *iotextypes.Execution) error {
117117

118118
ex.contract = pbAct.GetContract()
119119
ex.amount = &big.Int{}
120-
ex.amount.SetBytes(pbAct.GetAmount())
120+
ex.amount.SetString(pbAct.GetAmount(), 10)
121121
ex.data = pbAct.GetData()
122122
return nil
123123
}

action/protocol/account/accountpb/account.pb.go

+23-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

action/protocol/account/accountpb/account.proto

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ package accountpb;
1212
message Account {
1313
// used by state-based model
1414
uint64 nonce = 1;
15-
bytes balance = 2;
15+
string balance = 2;
1616
bytes root = 3;
1717
bytes codeHash = 4;
1818
bool isCandidate = 5;

action/protocol/multichain/mainchain/datamodel.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ func (bs SubChain) Serialize() ([]byte, error) {
4343
DepositCount: bs.DepositCount,
4444
}
4545
if bs.SecurityDeposit != nil {
46-
gen.SecurityDeposit = bs.SecurityDeposit.Bytes()
46+
gen.SecurityDeposit = bs.SecurityDeposit.String()
4747
}
4848
if bs.OperationDeposit != nil {
49-
gen.OperationDeposit = bs.OperationDeposit.Bytes()
49+
gen.OperationDeposit = bs.OperationDeposit.String()
5050
}
5151
return proto.Marshal(gen)
5252
}
@@ -73,8 +73,8 @@ func (bs *SubChain) Deserialize(data []byte) error {
7373
CurrentHeight: gen.CurrentHeight,
7474
DepositCount: gen.DepositCount,
7575
}
76-
bs.SecurityDeposit.SetBytes(gen.SecurityDeposit)
77-
bs.OperationDeposit.SetBytes(gen.OperationDeposit)
76+
bs.SecurityDeposit.SetString(gen.SecurityDeposit, 10)
77+
bs.OperationDeposit.SetString(gen.OperationDeposit, 10)
7878
return nil
7979
}
8080

@@ -237,7 +237,7 @@ func (bs Deposit) Serialize() ([]byte, error) {
237237
Confirmed: bs.Confirmed,
238238
}
239239
if bs.Amount != nil {
240-
gen.Amount = bs.Amount.Bytes()
240+
gen.Amount = bs.Amount.String()
241241
}
242242
return proto.Marshal(gen)
243243
}
@@ -254,6 +254,6 @@ func (bs *Deposit) Deserialize(data []byte) error {
254254
Addr: gen.Address,
255255
Confirmed: gen.Confirmed,
256256
}
257-
bs.Amount.SetBytes(gen.Amount)
257+
bs.Amount.SetString(gen.Amount, 10)
258258
return nil
259259
}

0 commit comments

Comments
 (0)