-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgen_vectors_test.go
135 lines (118 loc) · 4.6 KB
/
gen_vectors_test.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
package main
/*
type TestCaseInput struct {
PrivateKey string
To string
Nonce int64
Value string
GasLimit int64
GasPrice string
PriorityGasPrice string
MaxFeePerBlobGas string
Data string
}
type TestCaseOutput struct {
testCase TestCaseInput
blobs types.Blobs
commitments []types.KZGCommitment
versionedHashes []common.Hash
proofs []types.KZGProof
txData types.SignedBlobTx
wrapData types.BlobTxWrapData
transaction *types.Transaction
}
type TestCaseResult struct {
Input TestCaseInput
RawEncodedTransaction string
}
func TestGenerateTestVectors(t *testing.T) {
fmt.Println("Generating test vectors...")
chainId := big.NewInt(1331)
signer := types.NewDankSigner(chainId)
data8Bytes := make([]byte, 8)
data128Bytes := make([]byte, 128)
data256Bytes := make([]byte, 256)
data1024Bytes := make([]byte, 1024)
rand.Read(data8Bytes)
rand.Read(data128Bytes)
rand.Read(data256Bytes)
rand.Read(data1024Bytes)
// Define test cases
testCaseInputs := []TestCaseInput{
{"aa3a09289747a62b7b8190af3a75544cbe8c3b4a58f7b11d8d3b12ad17300a59", "0x45Ae5777c9b35Eb16280e423b0d7c91C06C66B58", 1, "1", int64(100000), "1000", "50", "100", hexutil.Encode(data8Bytes)},
{"67f45650acc5dc426fc424348f8d9f07032c439f03797c4beba096a6e23e666b", "0x549A51956bd364D8bB2Efb1F1eA4436e8D7764Ff", 2, "1", int64(50000), "1234", "100", "200", hexutil.Encode(data128Bytes)},
{"2e2b5c749eab38b8eca6b788c70429580ffa8eb79ab9635b95af00c6f6cba661", "0xa39c4e1B259473fbcC5213a0613eB53a8C50bf76", 3, "1", int64(70000), "999", "10", "20", hexutil.Encode(data256Bytes)},
{"ee15ba623c2a495eefc9b8dc7447ff70bee325cc1f75f5170a71dc4dd3227f13", "0xd59399657A78bb69dEE83C416C13Be711e02fA23", 4, "1", int64(21000), "1001", "35", "70", hexutil.Encode(data1024Bytes)},
}
summary := []TestCaseResult{}
for _, testCaseInput := range testCaseInputs {
testCaseOutput, err := generateTestVector(chainId, signer, testCaseInput)
assert.Nil(t, err)
txAsRawData, _ := testCaseOutput.transaction.MarshalBinary()
txRawHex := hexutil.Encode(txAsRawData)
result := TestCaseResult{testCaseOutput.testCase, txRawHex}
summary = append(summary, result)
}
// Generate JSON summary
summaryAsJson, _ := json.MarshalIndent(summary, "", "\t")
// Write summary in output file
outputFilePath := "/tmp/eip4844_test_vectors.json"
outputFile, err := os.Create(outputFilePath)
assert.Nil(t, err)
outputFile.WriteString(string(summaryAsJson))
}
func generateTestVector(chainId *big.Int, signer types.Signer, testCaseInput TestCaseInput) (*TestCaseOutput, error) {
blobs := EncodeBlobs(hexutil.MustDecode(testCaseInput.Data))
commitments, versionedHashes, proofs, err := blobs.ComputeCommitmentsAndProofs()
if err != nil {
log.Fatalf("failed to compute commitments: %v", err)
}
// Encode fields
gasPrice256, _ := DecodeUint256String(testCaseInput.GasPrice)
priorityGasPrice256, _ := DecodeUint256String(testCaseInput.PriorityGasPrice)
maxFeePerBlobGas256, _ := DecodeUint256String(testCaseInput.MaxFeePerBlobGas)
value256, _ := DecodeUint256String(testCaseInput.Value)
to := common.HexToAddress(testCaseInput.To)
// Generate signed blob transaction
txData := types.SignedBlobTx{
Message: types.BlobTxMessage{
ChainID: view.Uint256View(*uint256.NewInt(chainId.Uint64())),
Nonce: view.Uint64View(testCaseInput.Nonce),
Gas: view.Uint64View(testCaseInput.GasLimit),
GasFeeCap: view.Uint256View(*gasPrice256),
GasTipCap: view.Uint256View(*priorityGasPrice256),
MaxFeePerBlobGas: view.Uint256View(*maxFeePerBlobGas256),
Value: view.Uint256View(*value256),
To: types.AddressOptionalSSZ{Address: (*types.AddressSSZ)(&to)},
BlobVersionedHashes: versionedHashes,
},
}
// Generate wrap transaction data
wrapData := types.BlobTxWrapData{
BlobKzgs: commitments,
Blobs: blobs,
Proofs: proofs,
}
// Generate key from private key string
key, err := crypto.HexToECDSA(testCaseInput.PrivateKey)
if err != nil {
return nil, fmt.Errorf("%w: invalid private key", err)
}
tx := types.NewTx(&txData, types.WithTxWrapData(&wrapData))
tx, err = types.SignTx(tx, signer, key)
if err != nil {
log.Fatalf("Error signing tx: %v", err)
}
testCaseOutput := &TestCaseOutput{
testCase: testCaseInput,
blobs: blobs,
commitments: commitments,
versionedHashes: versionedHashes,
proofs: proofs,
txData: txData,
wrapData: wrapData,
transaction: tx,
}
return testCaseOutput, nil
}
*/