Skip to content

Commit 9552500

Browse files
authored
xdpos API getV2Block (ethereum#227)
1 parent 105f387 commit 9552500

File tree

7 files changed

+115
-14
lines changed

7 files changed

+115
-14
lines changed

consensus/XDPoS/api.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
package XDPoS
1717

1818
import (
19+
"encoding/base64"
1920
"math/big"
2021

2122
"github.com/XinFinOrg/XDPoSChain/common"
2223
"github.com/XinFinOrg/XDPoSChain/consensus"
2324
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
2425
"github.com/XinFinOrg/XDPoSChain/core/types"
26+
"github.com/XinFinOrg/XDPoSChain/rlp"
2527
"github.com/XinFinOrg/XDPoSChain/rpc"
2628
)
2729

@@ -31,6 +33,17 @@ type API struct {
3133
chain consensus.ChainReader
3234
XDPoS *XDPoS
3335
}
36+
37+
type V2BlockInfo struct {
38+
Hash common.Hash
39+
Round types.Round
40+
Number *big.Int
41+
ParentHash common.Hash
42+
Committed bool
43+
EncodedRLP string
44+
Error string
45+
}
46+
3447
type NetworkInformation struct {
3548
NetworkId *big.Int
3649
XDCValidatorAddress common.Address
@@ -96,6 +109,89 @@ func (api *API) GetLatestCommittedBlockHeader() *types.BlockInfo {
96109
return api.XDPoS.EngineV2.GetLatestCommittedBlockInfo()
97110
}
98111

112+
func (api *API) GetV2BlockByHeader(header *types.Header, uncle bool) *V2BlockInfo {
113+
committed := false
114+
latestCommittedBlock := api.XDPoS.EngineV2.GetLatestCommittedBlockInfo()
115+
if latestCommittedBlock == nil {
116+
return &V2BlockInfo{
117+
Hash: header.Hash(),
118+
Error: "can not find latest committed block from consensus",
119+
}
120+
}
121+
if header.Number.Uint64() <= latestCommittedBlock.Number.Uint64() {
122+
committed = true && !uncle
123+
}
124+
125+
round, err := api.XDPoS.EngineV2.GetRoundNumber(header)
126+
127+
if err != nil {
128+
return &V2BlockInfo{
129+
Hash: header.Hash(),
130+
Error: err.Error(),
131+
}
132+
}
133+
134+
encodeBytes, err := rlp.EncodeToBytes(header)
135+
if err != nil {
136+
return &V2BlockInfo{
137+
Hash: header.Hash(),
138+
Error: err.Error(),
139+
}
140+
}
141+
142+
block := &V2BlockInfo{
143+
Hash: header.Hash(),
144+
ParentHash: header.ParentHash,
145+
Number: header.Number,
146+
Round: round,
147+
Committed: committed,
148+
EncodedRLP: base64.StdEncoding.EncodeToString(encodeBytes),
149+
}
150+
return block
151+
}
152+
153+
func (api *API) GetV2BlockByNumber(number *rpc.BlockNumber) *V2BlockInfo {
154+
var header *types.Header
155+
if number == nil || *number == rpc.LatestBlockNumber {
156+
header = api.chain.CurrentHeader()
157+
} else if *number == rpc.CommittedBlockNumber {
158+
hash := api.XDPoS.EngineV2.GetLatestCommittedBlockInfo().Hash
159+
header = api.chain.GetHeaderByHash(hash)
160+
} else {
161+
header = api.chain.GetHeaderByNumber(uint64(number.Int64()))
162+
}
163+
164+
if header == nil {
165+
return &V2BlockInfo{
166+
Number: big.NewInt(number.Int64()),
167+
Error: "can not find block from this number",
168+
}
169+
}
170+
171+
uncle := false
172+
return api.GetV2BlockByHeader(header, uncle)
173+
}
174+
175+
// Confirm V2 Block Committed Status
176+
func (api *API) GetV2BlockByHash(blockHash common.Hash) *V2BlockInfo {
177+
header := api.chain.GetHeaderByHash(blockHash)
178+
if header == nil {
179+
return &V2BlockInfo{
180+
Hash: blockHash,
181+
Error: "can not find block from this hash",
182+
}
183+
}
184+
185+
// confirm this is on the main chain
186+
chainHeader := api.chain.GetHeaderByNumber(header.Number.Uint64())
187+
uncle := false
188+
if header.Hash() != chainHeader.Hash() {
189+
uncle = true
190+
}
191+
192+
return api.GetV2BlockByHeader(header, uncle)
193+
}
194+
99195
func (api *API) NetworkInformation() NetworkInformation {
100196
info := NetworkInformation{}
101197
info.NetworkId = api.chain.Config().ChainId

eth/api_backend.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (b *EthApiBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNum
8282
// Otherwise resolve and return the block
8383
if blockNr == rpc.LatestBlockNumber {
8484
return b.eth.blockchain.CurrentBlock().Header(), nil
85-
} else if blockNr == rpc.ConfirmedBlockNumber {
85+
} else if blockNr == rpc.CommittedBlockNumber {
8686
if b.eth.chainConfig.XDPoS == nil {
8787
return nil, errors.New("PoW does not support confirmed block lookup")
8888
}
@@ -110,7 +110,7 @@ func (b *EthApiBackend) BlockByNumber(ctx context.Context, blockNr rpc.BlockNumb
110110
// Otherwise resolve and return the block
111111
if blockNr == rpc.LatestBlockNumber {
112112
return b.eth.blockchain.CurrentBlock(), nil
113-
} else if blockNr == rpc.ConfirmedBlockNumber {
113+
} else if blockNr == rpc.CommittedBlockNumber {
114114
if b.eth.chainConfig.XDPoS == nil {
115115
return nil, errors.New("PoW does not support confirmed block lookup")
116116
}

internal/jsre/deps/bindata.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/jsre/deps/web3.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/web3ext/web3ext.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,12 @@ web3._extend({
139139
params: 1
140140
}),
141141
new web3._extend.Method({
142-
name: 'getLatestCommittedBlockInfo',
143-
call: 'XDPoS_getLatestCommittedBlockHeader'
142+
name: 'getV2Block',
143+
call: function (args) {
144+
return (web3._extend.utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? "XDPoS_getV2BlockByHash" : "XDPoS_getV2BlockByNumber";
145+
},
146+
params: 1,
147+
inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter]
144148
}),
145149
],
146150
properties: [

rpc/types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ type BlockNumber int64
120120
type EpochNumber int64
121121

122122
const (
123-
ConfirmedBlockNumber = BlockNumber(-3)
123+
CommittedBlockNumber = BlockNumber(-3)
124124
PendingBlockNumber = BlockNumber(-2)
125125
LatestBlockNumber = BlockNumber(-1)
126126
EarliestBlockNumber = BlockNumber(0)
127127
LatestEpochNumber = EpochNumber(-1)
128128
)
129129

130130
// UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports:
131-
// - "latest", "earliest", "pending" and "confirmed" as string arguments
131+
// - "latest", "earliest", "pending" and "committed" as string arguments
132132
// - the block number
133133
// Returned errors:
134134
// - an invalid block number error when the given argument isn't a known strings
@@ -145,8 +145,8 @@ func (bn *BlockNumber) UnmarshalJSON(data []byte) error {
145145
case "pending":
146146
*bn = PendingBlockNumber
147147
return nil
148-
case "confirmed":
149-
*bn = ConfirmedBlockNumber
148+
case "committed":
149+
*bn = CommittedBlockNumber
150150
return nil
151151
}
152152

rpc/types_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ func TestBlockNumberJSONUnmarshal(t *testing.T) {
4343
11: {`"pending"`, false, PendingBlockNumber},
4444
12: {`"latest"`, false, LatestBlockNumber},
4545
13: {`"earliest"`, false, EarliestBlockNumber},
46-
14: {`someString`, true, BlockNumber(0)},
47-
15: {`""`, true, BlockNumber(0)},
48-
16: {``, true, BlockNumber(0)},
46+
14: {`"committed"`, false, CommittedBlockNumber},
47+
15: {`someString`, true, BlockNumber(0)},
48+
16: {`""`, true, BlockNumber(0)},
49+
17: {``, true, BlockNumber(0)},
4950
}
5051

5152
for i, test := range tests {

0 commit comments

Comments
 (0)