Skip to content

Commit c431f10

Browse files
committed
fix(): modify block decode
1 parent 80e5749 commit c431f10

File tree

5 files changed

+86
-85
lines changed

5 files changed

+86
-85
lines changed

client.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,7 @@ func (c *Client) GetBlockByID(id string) (block *types.Block, err error) {
371371
if code != 200 {
372372
return nil, fmt.Errorf("get block by id error: %s", string(body))
373373
}
374-
block = &types.Block{}
375-
err = block.Unmarshal(string(body))
374+
block, err = utils.DecodeBlock(string(body))
376375
return
377376
}
378377

@@ -385,8 +384,7 @@ func (c *Client) GetBlockByHeight(height int64) (block *types.Block, err error)
385384
if code != 200 {
386385
return nil, fmt.Errorf("get block by height error: %s", string(body))
387386
}
388-
block = &types.Block{}
389-
err = block.Unmarshal(string(body))
387+
block, err = utils.DecodeBlock(string(body))
390388
return
391389
}
392390

types/block.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package types
2+
3+
type Block struct {
4+
Nonce string `json:"nonce"`
5+
PreviousBlock string `json:"previous_block"`
6+
Timestamp int64 `json:"timestamp"`
7+
LastRetarget int64 `json:"last_retarget"`
8+
Diff interface{} `json:"diff"`
9+
Height int64 `json:"height"`
10+
Hash string `json:"hash"`
11+
IndepHash string `json:"indep_hash"`
12+
Txs []string `json:"txs"`
13+
TxRoot string `json:"tx_root"`
14+
TxTree interface{} `json:"tx_tree"`
15+
HashList interface{} `json:"hash_list"`
16+
HashListMerkle string `json:"hash_list_merkle"`
17+
WalletList string `json:"wallet_list"`
18+
RewardAddr string `json:"reward_addr"`
19+
Tags []interface{} `json:"tags"`
20+
RewardPool interface{} `json:"reward_pool"` // always string
21+
WeaveSize interface{} `json:"weave_size"` // always string
22+
BlockSize interface{} `json:"block_size"` // always string
23+
CumulativeDiff interface{} `json:"cumulative_diff"`
24+
SizeTaggedTxs interface{} `json:"size_tagged_txs"`
25+
Poa POA `json:"poa"`
26+
UsdToArRate []string `json:"usd_to_ar_rate"`
27+
ScheduledUsdToArRate []string `json:"scheduled_usd_to_ar_rate"`
28+
Packing25Threshold string `json:"packing_2_5_threshold"`
29+
StrictDataSplitThreshold string `json:"strict_data_split_threshold"`
30+
}
31+
32+
type POA struct {
33+
Option string `json:"option"`
34+
TxPath string `json:"tx_path"`
35+
DataPath string `json:"data_path"`
36+
Chunk string `json:"chunk"`
37+
}

types/types.go

-65
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package types
22

3-
import (
4-
"encoding/json"
5-
"strings"
6-
)
7-
83
type NetworkInfo struct {
94
Network string `json:"network"`
105
Version int64 `json:"version"`
@@ -17,66 +12,6 @@ type NetworkInfo struct {
1712
NodeStateLatency int64 `json:"node_state_latency"`
1813
}
1914

20-
type Block struct {
21-
Nonce string `json:"nonce"`
22-
PreviousBlock string `json:"previous_block"`
23-
Timestamp int64 `json:"timestamp"`
24-
LastRetarget int64 `json:"last_retarget"`
25-
Diff interface{} `json:"diff"`
26-
Height int64 `json:"height"`
27-
Hash string `json:"hash"`
28-
IndepHash string `json:"indep_hash"`
29-
Txs []string `json:"txs"`
30-
TxRoot string `json:"tx_root"`
31-
TxTree interface{} `json:"tx_tree"`
32-
HashList interface{} `json:"hash_list"`
33-
HashListMerkle string `json:"hash_list_merkle"`
34-
WalletList string `json:"wallet_list"`
35-
RewardAddr string `json:"reward_addr"`
36-
Tags []interface{} `json:"tags"`
37-
RewardPool interface{} `json:"reward_pool"`
38-
WeaveSize interface{} `json:"weave_size"`
39-
BlockSize interface{} `json:"block_size"`
40-
CumulativeDiff interface{} `json:"cumulative_diff"`
41-
SizeTaggedTxs interface{} `json:"size_tagged_txs"`
42-
Poa POA `json:"poa"`
43-
UsdToArRate []string `json:"usd_to_ar_rate"`
44-
ScheduledUsdToArRate []string `json:"scheduled_usd_to_ar_rate"`
45-
Packing25Threshold string `json:"packing_2_5_threshold"`
46-
StrictDataSplitThreshold string `json:"strict_data_split_threshold"`
47-
}
48-
49-
func (b *Block) Format() {
50-
if _, ok := b.RewardPool.(string); !ok {
51-
by, _ := json.Marshal(b.RewardPool)
52-
b.RewardPool = string(by)
53-
}
54-
55-
if _, ok := b.WeaveSize.(string); !ok {
56-
by, _ := json.Marshal(b.WeaveSize)
57-
b.WeaveSize = string(by)
58-
}
59-
60-
if _, ok := b.BlockSize.(string); !ok {
61-
by, _ := json.Marshal(b.BlockSize)
62-
b.BlockSize = string(by)
63-
}
64-
}
65-
66-
func (b *Block) Unmarshal(body string) error {
67-
// json unmarshal exist number precision problem
68-
decoder := json.NewDecoder(strings.NewReader(body))
69-
decoder.UseNumber()
70-
return decoder.Decode(b)
71-
}
72-
73-
type POA struct {
74-
Option string `json:"option"`
75-
TxPath string `json:"tx_path"`
76-
DataPath string `json:"data_path"`
77-
Chunk string `json:"chunk"`
78-
}
79-
8015
type TransactionChunk struct {
8116
Chunk string `json:"chunk"`
8217
DataPath string `json:"data_path"`

utils/block.go

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package utils
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"github.com/everFinance/goar/types"
7+
"strings"
68
)
79

810
const (
@@ -16,7 +18,6 @@ func GenerateIndepHash(b types.Block) string {
1618
return b.IndepHash
1719
}
1820

19-
b.Format()
2021
bds := generateBlockDataSegment(b)
2122
list := make([]interface{}, 0)
2223
list = append(list, Base64Encode(bds))
@@ -97,3 +98,33 @@ func poaToList(poa types.POA) []string {
9798
poa.Chunk,
9899
}
99100
}
101+
102+
func DecodeBlock(body string) (*types.Block, error) {
103+
b := &types.Block{}
104+
// json unmarshal exist number precision problem
105+
decoder := json.NewDecoder(strings.NewReader(body))
106+
decoder.UseNumber()
107+
err := decoder.Decode(b)
108+
if err != nil {
109+
return nil, err
110+
}
111+
formatBlockFields(b)
112+
return b, err
113+
}
114+
115+
func formatBlockFields(b *types.Block) {
116+
if _, ok := b.RewardPool.(string); !ok {
117+
by, _ := json.Marshal(b.RewardPool)
118+
b.RewardPool = string(by)
119+
}
120+
121+
if _, ok := b.WeaveSize.(string); !ok {
122+
by, _ := json.Marshal(b.WeaveSize)
123+
b.WeaveSize = string(by)
124+
}
125+
126+
if _, ok := b.BlockSize.(string); !ok {
127+
by, _ := json.Marshal(b.BlockSize)
128+
b.BlockSize = string(by)
129+
}
130+
}

utils/block_test.go

+15-15
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)