Skip to content

Commit 6f04951

Browse files
committed
API GetReceiptProof test
1 parent ab8f6cb commit 6f04951

File tree

3 files changed

+112
-34
lines changed

3 files changed

+112
-34
lines changed

internal/ethapi/api.go

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828

2929
"github.com/XinFinOrg/XDC-Subnet/XDCxlending/lendingstate"
3030
"github.com/XinFinOrg/XDC-Subnet/consensus"
31-
"github.com/XinFinOrg/XDC-Subnet/trie"
3231

3332
"github.com/XinFinOrg/XDC-Subnet/XDCx/tradingstate"
3433

@@ -535,35 +534,6 @@ func (s *PublicBlockChainAPI) GetBalance(ctx context.Context, address common.Add
535534
return b, state.Error()
536535
}
537536

538-
// proofPairList implements ethdb.KeyValueWriter and collects the proofs as
539-
// hex-strings of key and value for delivery to rpc-caller.
540-
type proofPairList struct {
541-
keys []string
542-
values []string
543-
}
544-
545-
func (n *proofPairList) Put(key []byte, value []byte) error {
546-
n.keys = append(n.keys, hexutil.Encode(key))
547-
n.values = append(n.values, hexutil.Encode(value))
548-
return nil
549-
}
550-
551-
func (n *proofPairList) Delete(key []byte) error {
552-
panic("not supported")
553-
}
554-
555-
// modified from core/types/derive_sha.go
556-
func deriveTrie(list types.DerivableList) *trie.Trie {
557-
keybuf := new(bytes.Buffer)
558-
trie := new(trie.Trie)
559-
for i := 0; i < list.Len(); i++ {
560-
keybuf.Reset()
561-
rlp.Encode(keybuf, uint(i))
562-
trie.Update(keybuf.Bytes(), list.GetRlp(i))
563-
}
564-
return trie
565-
}
566-
567537
// GetReceiptProof returns the Trie proof of the receipt for a given transaction.
568538
func (s *PublicBlockChainAPI) GetReceiptProof(ctx context.Context, hash common.Hash) (map[string]interface{}, error) {
569539
tx, blockHash, _, index := core.GetTransaction(s.b.ChainDb(), hash)
@@ -585,10 +555,8 @@ func (s *PublicBlockChainAPI) GetReceiptProof(ctx context.Context, hash common.H
585555
return nil, err
586556
}
587557
fields := map[string]interface{}{
588-
"keys": proof.keys,
589-
"values": proof.values,
590-
"index": index,
591-
"leafValue": receipts.GetRlp(int(index)),
558+
"keys": proof.keys,
559+
"values": proof.values,
592560
}
593561
return fields, nil
594562
}

internal/ethapi/receipt_proof.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package ethapi
2+
3+
import (
4+
"bytes"
5+
6+
"github.com/XinFinOrg/XDC-Subnet/common/hexutil"
7+
"github.com/XinFinOrg/XDC-Subnet/core/types"
8+
"github.com/XinFinOrg/XDC-Subnet/rlp"
9+
"github.com/XinFinOrg/XDC-Subnet/trie"
10+
)
11+
12+
// proofPairList implements ethdb.KeyValueWriter and collects the proofs as
13+
// hex-strings of key and value for delivery to rpc-caller.
14+
type proofPairList struct {
15+
keys []string
16+
values []string
17+
}
18+
19+
func (n *proofPairList) Put(key []byte, value []byte) error {
20+
n.keys = append(n.keys, hexutil.Encode(key))
21+
n.values = append(n.values, hexutil.Encode(value))
22+
return nil
23+
}
24+
25+
func (n *proofPairList) Delete(key []byte) error {
26+
panic("not supported")
27+
}
28+
29+
// modified from core/types/derive_sha.go
30+
func deriveTrie(list types.DerivableList) *trie.Trie {
31+
keybuf := new(bytes.Buffer)
32+
trie := new(trie.Trie)
33+
for i := 0; i < list.Len(); i++ {
34+
keybuf.Reset()
35+
rlp.Encode(keybuf, uint(i))
36+
trie.Update(keybuf.Bytes(), list.GetRlp(i))
37+
}
38+
return trie
39+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package ethapi
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"reflect"
7+
"testing"
8+
9+
"github.com/XinFinOrg/XDC-Subnet/common/hexutil"
10+
"github.com/XinFinOrg/XDC-Subnet/core/types"
11+
"github.com/XinFinOrg/XDC-Subnet/rlp"
12+
"github.com/XinFinOrg/XDC-Subnet/trie"
13+
)
14+
15+
// implement interface only for testing verifyProof
16+
func (n *proofPairList) Has(key []byte) (bool, error) {
17+
key_hex := hexutil.Encode(key)
18+
for _, k := range n.keys {
19+
if k == key_hex {
20+
return true, nil
21+
}
22+
}
23+
return false, nil
24+
}
25+
26+
func (n *proofPairList) Get(key []byte) ([]byte, error) {
27+
key_hex := hexutil.Encode(key)
28+
for i, k := range n.keys {
29+
if k == key_hex {
30+
b, err := hexutil.Decode(n.values[i])
31+
if err != nil {
32+
return nil, err
33+
}
34+
return b, nil
35+
}
36+
}
37+
return nil, fmt.Errorf("key not found")
38+
}
39+
40+
func TestReceiptProof(t *testing.T) {
41+
root1 := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
42+
root2 := []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
43+
r1 := types.NewReceipt(root1, false, 1)
44+
r2 := types.NewReceipt(root2, true, 2)
45+
r3 := types.NewReceipt(root2, false, 3)
46+
r4 := types.NewReceipt(root1, true, 4)
47+
receipts := types.Receipts([]*types.Receipt{r1, r2, r3, r4})
48+
tr := deriveTrie(receipts)
49+
// for verifying the proof
50+
root := types.DeriveSha(receipts)
51+
for i := 0; i < receipts.Len(); i++ {
52+
var proof proofPairList
53+
keybuf := new(bytes.Buffer)
54+
rlp.Encode(keybuf, uint(i))
55+
if err := tr.Prove(keybuf.Bytes(), 0, &proof); err != nil {
56+
t.Fatal("Prove err:", err)
57+
}
58+
// verify the proof
59+
value, err := trie.VerifyProof(root, keybuf.Bytes(), &proof)
60+
if err != nil {
61+
t.Fatal("verify proof error")
62+
}
63+
encodedReceipt, err := rlp.EncodeToBytes(receipts[i])
64+
if err != nil {
65+
t.Fatal("encode receipt error")
66+
}
67+
if !reflect.DeepEqual(encodedReceipt, value) {
68+
t.Fatal("verify does not return the receipt we want")
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)