This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
/
mapper.go
145 lines (133 loc) · 4.65 KB
/
mapper.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
// Copyright (C) 2018 MediBloc
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package rpc
import (
"github.com/gogo/protobuf/proto"
dposState "github.com/medibloc/go-medibloc/consensus/dpos/state"
"github.com/medibloc/go-medibloc/core"
corepb "github.com/medibloc/go-medibloc/core/pb"
coreState "github.com/medibloc/go-medibloc/core/state"
rpcpb "github.com/medibloc/go-medibloc/rpc/pb"
"github.com/medibloc/go-medibloc/util/byteutils"
)
func coreAccount2rpcAccount(account *coreState.Account, curTs int64, address string) (*rpcpb.Account, error) {
aliasBytes, err := account.GetData("", []byte(coreState.AliasKey))
if err != nil && err != core.ErrNotFound {
return nil, err
}
pbAlias := new(corepb.Alias)
err = proto.Unmarshal(aliasBytes, pbAlias)
if err != nil {
return nil, err
}
votedSlice, err := account.VotedSlice()
if err != nil {
return nil, err
}
voted := byteutils.BytesSlice2HexSlice(votedSlice)
return &rpcpb.Account{
Address: address,
Balance: account.Balance.String(),
Nonce: account.Nonce,
Staking: account.Staking.String(),
Voted: voted,
Points: account.Points.String(),
Unstaking: account.Unstaking.String(),
Alias: pbAlias.AliasName,
CandidateId: byteutils.Bytes2Hex(account.CandidateID),
}, nil
}
func coreBlock2rpcBlock(block *core.Block, light bool) *rpcpb.Block {
var txs []*rpcpb.Transaction
var txHashes []string
if light {
for _, tx := range block.Transactions() {
txHashes = append(txHashes, byteutils.Bytes2Hex(tx.Hash()))
}
} else {
txs = transactions2rpcTxs(block.Transactions(), true)
}
return &rpcpb.Block{
Height: block.Height(),
Hash: byteutils.Bytes2Hex(block.Hash()),
ParentHash: byteutils.Bytes2Hex(block.ParentHash()),
Coinbase: block.Coinbase().Hex(),
Reward: block.Reward().String(),
Supply: block.Supply().String(),
Timestamp: block.Timestamp(),
ChainId: block.ChainID(),
Sign: byteutils.Bytes2Hex(block.Sign()),
AccsRoot: byteutils.Bytes2Hex(block.AccStateRoot()),
TxsRoot: byteutils.Bytes2Hex(block.TxStateRoot()),
DposRoot: byteutils.Bytes2Hex(block.DposRoot()),
Transactions: txs,
TxHashes: txHashes,
CpuPrice: block.BlockData.BlockHeader.CPUPrice().String(),
CpuUsage: block.BlockData.BlockHeader.CPUUsage(),
NetPrice: block.BlockData.BlockHeader.NetPrice().String(),
NetUsage: block.BlockData.BlockHeader.NetUsage(),
}
}
func dposCandidate2rpcCandidate(candidate *dposState.Candidate) *rpcpb.Candidate {
return &rpcpb.Candidate{
CandidateId: byteutils.Bytes2Hex(candidate.ID),
Address: candidate.Addr.Hex(),
Url: candidate.URL,
Collateral: candidate.Collateral.String(),
VotePower: candidate.VotePower.String(),
}
}
// CoreTx2rpcTx converts core transaction type to rpcpb response type
func CoreTx2rpcTx(tx *core.Transaction, onChain bool) *rpcpb.Transaction {
var rpcReceipt *rpcpb.TransactionReceipt
if onChain {
rpcReceipt = coreReceipt2rpcReceipt(tx)
}
return &rpcpb.Transaction{
Hash: byteutils.Bytes2Hex(tx.Hash()),
From: tx.From().Hex(),
To: tx.To().Hex(),
Value: tx.Value().String(),
TxType: tx.TxType(),
Nonce: tx.Nonce(),
ChainId: tx.ChainID(),
Payload: byteutils.Bytes2Hex(tx.Payload()),
Sign: byteutils.Bytes2Hex(tx.Sign()),
PayerSign: byteutils.Bytes2Hex(tx.PayerSign()),
OnChain: onChain,
Receipt: rpcReceipt,
}
}
func transactions2rpcTxs(txs []*core.Transaction, onChain bool) []*rpcpb.Transaction {
var rpcTxs []*rpcpb.Transaction
for _, tx := range txs {
rpcTx := CoreTx2rpcTx(tx, onChain)
rpcTxs = append(rpcTxs, rpcTx)
}
return rpcTxs
}
func coreReceipt2rpcReceipt(tx *core.Transaction) *rpcpb.TransactionReceipt {
err := string(tx.Receipt().Error())
points := tx.Receipt().Points().String()
return &rpcpb.TransactionReceipt{
Executed: tx.Receipt().Executed(),
Timestamp: tx.Receipt().Timestamp(),
Height: tx.Receipt().Height(),
CpuUsage: tx.Receipt().CPUUsage(),
NetUsage: tx.Receipt().NetUsage(),
Points: points,
Error: err,
}
}