-
Notifications
You must be signed in to change notification settings - Fork 289
/
types.go
246 lines (217 loc) · 6.87 KB
/
types.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package rpc
import (
"bytes"
"encoding/hex"
"encoding/json"
"math/big"
"strconv"
"strings"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/harmony-one/harmony/block"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/shard"
)
// CallArgs represents the arguments for a call.
type CallArgs struct {
From *common.Address `json:"from"`
To *common.Address `json:"to"`
Gas *hexutil.Uint64 `json:"gas"`
GasPrice *hexutil.Big `json:"gasPrice"`
Value *hexutil.Big `json:"value"`
Data *hexutil.Bytes `json:"data"`
}
// ToMessage converts CallArgs to the Message type used by the core evm
// Adapted from go-ethereum/internal/ethapi/api.go
func (args *CallArgs) ToMessage(globalGasCap *big.Int) types.Message {
// Set sender address or use zero address if none specified.
var addr common.Address
if args.From != nil {
addr = *args.From
}
// Set default gas & gas price if none were set
gas := uint64(math.MaxUint64 / 2)
if args.Gas != nil {
gas = uint64(*args.Gas)
}
if globalGasCap != nil && globalGasCap.Uint64() < gas {
utils.Logger().Warn().
Uint64("requested", gas).
Uint64("cap", globalGasCap.Uint64()).
Msg("Caller gas above allowance, capping")
gas = globalGasCap.Uint64()
}
gasPrice := new(big.Int)
if args.GasPrice != nil {
gasPrice = args.GasPrice.ToInt()
}
value := new(big.Int)
if args.Value != nil {
value = args.Value.ToInt()
}
var data []byte
if args.Data != nil {
data = []byte(*args.Data)
}
msg := types.NewMessage(addr, args.To, 0, value, gas, gasPrice, data, false)
return msg
}
// StakingNetworkInfo returns global staking info.
type StakingNetworkInfo struct {
TotalSupply numeric.Dec `json:"total-supply"`
CirculatingSupply numeric.Dec `json:"circulating-supply"`
EpochLastBlock uint64 `json:"epoch-last-block"`
TotalStaking *big.Int `json:"total-staking"`
MedianRawStake numeric.Dec `json:"median-raw-stake"`
}
// Delegation represents a particular delegation to a validator
type Delegation struct {
ValidatorAddress string `json:"validator_address"`
DelegatorAddress string `json:"delegator_address"`
Amount *big.Int `json:"amount"`
Reward *big.Int `json:"reward"`
Undelegations []Undelegation `json:"Undelegations"`
}
// Undelegation represents one undelegation entry
type Undelegation struct {
Amount *big.Int
Epoch *big.Int
}
// StructuredResponse type of RPCs
type StructuredResponse = map[string]interface{}
// NewStructuredResponse creates a structured response from the given input
func NewStructuredResponse(input interface{}) (StructuredResponse, error) {
var objMap StructuredResponse
dat, err := json.Marshal(input)
if err != nil {
return nil, err
}
d := json.NewDecoder(bytes.NewReader(dat))
d.UseNumber()
err = d.Decode(&objMap)
if err != nil {
return nil, err
}
return objMap, nil
}
// BlockNumber ..
type BlockNumber rpc.BlockNumber
// UnmarshalJSON converts a hex string or integer to a block number
func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
baseBn := rpc.BlockNumber(0)
baseErr := baseBn.UnmarshalJSON(data)
if baseErr != nil {
input := strings.TrimSpace(string(data))
if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {
input = input[1 : len(input)-1]
}
input = strings.TrimPrefix(input, "0x")
num, err := strconv.ParseInt(input, 10, 64)
if err != nil {
return err
}
*bn = BlockNumber(num)
return nil
}
*bn = BlockNumber(baseBn)
return nil
}
// Int64 ..
func (bn BlockNumber) Int64() int64 {
return (int64)(bn)
}
// EthBlockNumber ..
func (bn BlockNumber) EthBlockNumber() rpc.BlockNumber {
return (rpc.BlockNumber)(bn)
}
// TransactionIndex ..
type TransactionIndex uint64
// UnmarshalJSON converts a hex string or integer to a Transaction index
func (i *TransactionIndex) UnmarshalJSON(data []byte) (err error) {
input := strings.TrimSpace(string(data))
if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {
input = input[1 : len(input)-1]
}
var num int64
if strings.HasPrefix(input, "0x") {
num, err = strconv.ParseInt(strings.TrimPrefix(input, "0x"), 16, 64)
} else {
num, err = strconv.ParseInt(input, 10, 64)
}
if err != nil {
return err
}
*i = TransactionIndex(num)
return nil
}
// TxHistoryArgs is struct to include optional transaction formatting params.
type TxHistoryArgs struct {
Address string `json:"address"`
PageIndex uint32 `json:"pageIndex"`
PageSize uint32 `json:"pageSize"`
FullTx bool `json:"fullTx"`
TxType string `json:"txType"`
Order string `json:"order"`
}
// UnmarshalFromInterface ..
func (ta *TxHistoryArgs) UnmarshalFromInterface(blockArgs interface{}) error {
var args TxHistoryArgs
dat, err := json.Marshal(blockArgs)
if err != nil {
return err
}
if err := json.Unmarshal(dat, &args); err != nil {
return err
}
*ta = args
return nil
}
// HeaderInformation represents the latest consensus information
type HeaderInformation struct {
BlockHash common.Hash `json:"blockHash"`
BlockNumber uint64 `json:"blockNumber"`
ShardID uint32 `json:"shardID"`
Leader string `json:"leader"`
ViewID uint64 `json:"viewID"`
Epoch uint64 `json:"epoch"`
Timestamp string `json:"timestamp"`
UnixTime uint64 `json:"unixtime"`
LastCommitSig string `json:"lastCommitSig"`
LastCommitBitmap string `json:"lastCommitBitmap"`
CrossLinks *types.CrossLinks `json:"crossLinks,omitempty"`
}
// NewHeaderInformation returns the header information that will serialize to the RPC representation.
func NewHeaderInformation(header *block.Header, leader string) *HeaderInformation {
if header == nil {
return nil
}
result := &HeaderInformation{
BlockHash: header.Hash(),
BlockNumber: header.Number().Uint64(),
ShardID: header.ShardID(),
Leader: leader,
ViewID: header.ViewID().Uint64(),
Epoch: header.Epoch().Uint64(),
UnixTime: header.Time().Uint64(),
Timestamp: time.Unix(header.Time().Int64(), 0).UTC().String(),
LastCommitBitmap: hex.EncodeToString(header.LastCommitBitmap()),
}
sig := header.LastCommitSignature()
result.LastCommitSig = hex.EncodeToString(sig[:])
if header.ShardID() == shard.BeaconChainShardID {
decodedCrossLinks := &types.CrossLinks{}
err := rlp.DecodeBytes(header.CrossLinks(), decodedCrossLinks)
if err != nil {
result.CrossLinks = &types.CrossLinks{}
} else {
result.CrossLinks = decodedCrossLinks
}
}
return result
}