forked from zeta-chain/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestdata.go
More file actions
123 lines (101 loc) · 2.95 KB
/
testdata.go
File metadata and controls
123 lines (101 loc) · 2.95 KB
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
package testdata
import (
"embed"
"encoding/json"
"fmt"
"testing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"
)
const (
HeaderPath = "ethereum/header.json"
EthReceiptPrefixPath = "ethereum/receipt_"
ZetaReceiptPrefixPath = "zeta/receipt_"
TxPrefixPath = "ethereum/tx_"
TxsCount = 81
)
// TypesFiles contains the embedded files of different types in ZetaChain
//
//go:embed types/*
var TypesFiles embed.FS
//go:embed ethereum/*
var ethFiles embed.FS
//go:embed zeta/*
var zetaFiles embed.FS
//go:embed *
var testDataFiles embed.FS
// ReadEthHeader reads a header from a file.
// TODO: centralize test data
// https://github.com/zeta-chain/node/issues/1874
func ReadEthHeader() (header types.Header, err error) {
file, err := ethFiles.Open(HeaderPath)
if err != nil {
return header, err
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&header)
return header, err
}
// ReadEthReceipt reads a receipt from a file.
// TODO: centralize test data
// https://github.com/zeta-chain/node/issues/1874
func ReadEthReceipt(index int) (receipt types.Receipt, err error) {
filePath := fmt.Sprintf("%s%d.json", EthReceiptPrefixPath, index)
return readReceipt(filePath, ethFiles)
}
// ReadZetaReceipt reads a receipt from a file by filename.
// TODO: centralize test data
// https://github.com/zeta-chain/node/issues/1874
func ReadZetaReceipt(name string) (receipt types.Receipt, err error) {
filePath := fmt.Sprintf("%s%s.json", ZetaReceiptPrefixPath, name)
return readReceipt(filePath, zetaFiles)
}
func readReceipt(filePath string, e embed.FS) (receipt types.Receipt, err error) {
file, err := e.Open(filePath)
if err != nil {
return receipt, err
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&receipt)
return receipt, err
}
// ReadEthTx reads a tx from a file.
// TODO: centralize test data
// https://github.com/zeta-chain/node/issues/1874
func ReadEthTx(index int) (tx types.Transaction, err error) {
filePath := fmt.Sprintf("%s%d.json", TxPrefixPath, index)
file, err := ethFiles.Open(filePath)
if err != nil {
return tx, err
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&tx)
return tx, err
}
type Block struct {
TssAddress string `json:"tssAddress"`
Height int `json:"height"`
Nonce uint64 `json:"nonce"`
OutboundID string `json:"outboundID"`
HeaderBase64 string `json:"headerBase64"`
BlockBase64 string `json:"blockBase64"`
}
type Blocks struct {
Blocks []Block `json:"blocks"`
}
// LoadTestBlocks loads test blocks from a file.
// TODO: centralize test data
// https://github.com/zeta-chain/node/issues/1874
func LoadTestBlocks(t *testing.T) Blocks {
file, err := testDataFiles.Open("test_blocks.json")
require.NoError(t, err)
defer file.Close()
// Decode the JSON into the data struct
var blocks Blocks
err = json.NewDecoder(file).Decode(&blocks)
require.NoError(t, err)
return blocks
}