@@ -20,6 +20,7 @@ import (
20
20
"bytes"
21
21
"context"
22
22
"crypto/ecdsa"
23
+ "crypto/sha256"
23
24
"encoding/json"
24
25
"errors"
25
26
"fmt"
@@ -45,6 +46,7 @@ import (
45
46
"github.com/ethereum/go-ethereum/core/types"
46
47
"github.com/ethereum/go-ethereum/core/vm"
47
48
"github.com/ethereum/go-ethereum/crypto"
49
+ "github.com/ethereum/go-ethereum/crypto/kzg4844"
48
50
"github.com/ethereum/go-ethereum/ethdb"
49
51
"github.com/ethereum/go-ethereum/event"
50
52
"github.com/ethereum/go-ethereum/internal/blocktest"
@@ -1079,6 +1081,195 @@ func TestSendBlobTransaction(t *testing.T) {
1079
1081
}
1080
1082
}
1081
1083
1084
+ func TestFillBlobTransaction (t * testing.T ) {
1085
+ t .Parallel ()
1086
+ // Initialize test accounts
1087
+ var (
1088
+ key , _ = crypto .HexToECDSA ("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a" )
1089
+ to = crypto .PubkeyToAddress (key .PublicKey )
1090
+ genesis = & core.Genesis {
1091
+ Config : params .MergedTestChainConfig ,
1092
+ Alloc : core.GenesisAlloc {},
1093
+ }
1094
+ emptyBlob = kzg4844.Blob {}
1095
+ emptyBlobCommit , _ = kzg4844 .BlobToCommitment (emptyBlob )
1096
+ emptyBlobProof , _ = kzg4844 .ComputeBlobProof (emptyBlob , emptyBlobCommit )
1097
+ emptyBlobHash common.Hash = kzg4844 .CalcBlobHashV1 (sha256 .New (), & emptyBlobCommit )
1098
+ )
1099
+ b := newTestBackend (t , 1 , genesis , beacon .New (ethash .NewFaker ()), func (i int , b * core.BlockGen ) {
1100
+ b .SetPoS ()
1101
+ })
1102
+ api := NewTransactionAPI (b , nil )
1103
+ type result struct {
1104
+ Hashes []common.Hash
1105
+ Sidecar * types.BlobTxSidecar
1106
+ }
1107
+ suite := []struct {
1108
+ name string
1109
+ args TransactionArgs
1110
+ err string
1111
+ want * result
1112
+ }{
1113
+ {
1114
+ name : "TestInvalidParamsCombination1" ,
1115
+ args : TransactionArgs {
1116
+ From : & b .acc .Address ,
1117
+ To : & to ,
1118
+ Value : (* hexutil .Big )(big .NewInt (1 )),
1119
+ Blobs : []kzg4844.Blob {{}},
1120
+ Proofs : []kzg4844.Proof {{}},
1121
+ },
1122
+ err : `blob proofs provided while commitments were not` ,
1123
+ },
1124
+ {
1125
+ name : "TestInvalidParamsCombination2" ,
1126
+ args : TransactionArgs {
1127
+ From : & b .acc .Address ,
1128
+ To : & to ,
1129
+ Value : (* hexutil .Big )(big .NewInt (1 )),
1130
+ Blobs : []kzg4844.Blob {{}},
1131
+ Commitments : []kzg4844.Commitment {{}},
1132
+ },
1133
+ err : `blob commitments provided while proofs were not` ,
1134
+ },
1135
+ {
1136
+ name : "TestInvalidParamsCount1" ,
1137
+ args : TransactionArgs {
1138
+ From : & b .acc .Address ,
1139
+ To : & to ,
1140
+ Value : (* hexutil .Big )(big .NewInt (1 )),
1141
+ Blobs : []kzg4844.Blob {{}},
1142
+ Commitments : []kzg4844.Commitment {{}, {}},
1143
+ Proofs : []kzg4844.Proof {{}, {}},
1144
+ },
1145
+ err : `number of blobs and commitments mismatch (have=2, want=1)` ,
1146
+ },
1147
+ {
1148
+ name : "TestInvalidParamsCount2" ,
1149
+ args : TransactionArgs {
1150
+ From : & b .acc .Address ,
1151
+ To : & to ,
1152
+ Value : (* hexutil .Big )(big .NewInt (1 )),
1153
+ Blobs : []kzg4844.Blob {{}, {}},
1154
+ Commitments : []kzg4844.Commitment {{}, {}},
1155
+ Proofs : []kzg4844.Proof {{}},
1156
+ },
1157
+ err : `number of blobs and proofs mismatch (have=1, want=2)` ,
1158
+ },
1159
+ {
1160
+ name : "TestInvalidProofVerification" ,
1161
+ args : TransactionArgs {
1162
+ From : & b .acc .Address ,
1163
+ To : & to ,
1164
+ Value : (* hexutil .Big )(big .NewInt (1 )),
1165
+ Blobs : []kzg4844.Blob {{}, {}},
1166
+ Commitments : []kzg4844.Commitment {{}, {}},
1167
+ Proofs : []kzg4844.Proof {{}, {}},
1168
+ },
1169
+ err : `failed to verify blob proof: short buffer` ,
1170
+ },
1171
+ {
1172
+ name : "TestGenerateBlobHashes" ,
1173
+ args : TransactionArgs {
1174
+ From : & b .acc .Address ,
1175
+ To : & to ,
1176
+ Value : (* hexutil .Big )(big .NewInt (1 )),
1177
+ Blobs : []kzg4844.Blob {emptyBlob },
1178
+ Commitments : []kzg4844.Commitment {emptyBlobCommit },
1179
+ Proofs : []kzg4844.Proof {emptyBlobProof },
1180
+ },
1181
+ want : & result {
1182
+ Hashes : []common.Hash {emptyBlobHash },
1183
+ Sidecar : & types.BlobTxSidecar {
1184
+ Blobs : []kzg4844.Blob {emptyBlob },
1185
+ Commitments : []kzg4844.Commitment {emptyBlobCommit },
1186
+ Proofs : []kzg4844.Proof {emptyBlobProof },
1187
+ },
1188
+ },
1189
+ },
1190
+ {
1191
+ name : "TestValidBlobHashes" ,
1192
+ args : TransactionArgs {
1193
+ From : & b .acc .Address ,
1194
+ To : & to ,
1195
+ Value : (* hexutil .Big )(big .NewInt (1 )),
1196
+ BlobHashes : []common.Hash {emptyBlobHash },
1197
+ Blobs : []kzg4844.Blob {emptyBlob },
1198
+ Commitments : []kzg4844.Commitment {emptyBlobCommit },
1199
+ Proofs : []kzg4844.Proof {emptyBlobProof },
1200
+ },
1201
+ want : & result {
1202
+ Hashes : []common.Hash {emptyBlobHash },
1203
+ Sidecar : & types.BlobTxSidecar {
1204
+ Blobs : []kzg4844.Blob {emptyBlob },
1205
+ Commitments : []kzg4844.Commitment {emptyBlobCommit },
1206
+ Proofs : []kzg4844.Proof {emptyBlobProof },
1207
+ },
1208
+ },
1209
+ },
1210
+ {
1211
+ name : "TestInvalidBlobHashes" ,
1212
+ args : TransactionArgs {
1213
+ From : & b .acc .Address ,
1214
+ To : & to ,
1215
+ Value : (* hexutil .Big )(big .NewInt (1 )),
1216
+ BlobHashes : []common.Hash {{0x01 , 0x22 }},
1217
+ Blobs : []kzg4844.Blob {emptyBlob },
1218
+ Commitments : []kzg4844.Commitment {emptyBlobCommit },
1219
+ Proofs : []kzg4844.Proof {emptyBlobProof },
1220
+ },
1221
+ err : fmt .Sprintf ("blob hash verification failed (have=%s, want=%s)" , common.Hash {0x01 , 0x22 }, emptyBlobHash ),
1222
+ },
1223
+ {
1224
+ name : "TestGenerateBlobProofs" ,
1225
+ args : TransactionArgs {
1226
+ From : & b .acc .Address ,
1227
+ To : & to ,
1228
+ Value : (* hexutil .Big )(big .NewInt (1 )),
1229
+ Blobs : []kzg4844.Blob {emptyBlob },
1230
+ },
1231
+ want : & result {
1232
+ Hashes : []common.Hash {emptyBlobHash },
1233
+ Sidecar : & types.BlobTxSidecar {
1234
+ Blobs : []kzg4844.Blob {emptyBlob },
1235
+ Commitments : []kzg4844.Commitment {emptyBlobCommit },
1236
+ Proofs : []kzg4844.Proof {emptyBlobProof },
1237
+ },
1238
+ },
1239
+ },
1240
+ }
1241
+ for _ , tc := range suite {
1242
+ t .Run (tc .name , func (t * testing.T ) {
1243
+ res , err := api .FillTransaction (context .Background (), tc .args )
1244
+ if len (tc .err ) > 0 {
1245
+ if err == nil {
1246
+ t .Fatalf ("missing error. want: %s" , tc .err )
1247
+ } else if err != nil && err .Error () != tc .err {
1248
+ t .Fatalf ("error mismatch. want: %s, have: %s" , tc .err , err .Error ())
1249
+ }
1250
+ return
1251
+ }
1252
+ if err != nil && len (tc .err ) == 0 {
1253
+ t .Fatalf ("expected no error. have: %s" , err )
1254
+ }
1255
+ if res == nil {
1256
+ t .Fatal ("result missing" )
1257
+ }
1258
+ want , err := json .Marshal (tc .want )
1259
+ if err != nil {
1260
+ t .Fatalf ("failed to encode expected: %v" , err )
1261
+ }
1262
+ have , err := json .Marshal (result {Hashes : res .Tx .BlobHashes (), Sidecar : res .Tx .BlobTxSidecar ()})
1263
+ if err != nil {
1264
+ t .Fatalf ("failed to encode computed sidecar: %v" , err )
1265
+ }
1266
+ if ! bytes .Equal (have , want ) {
1267
+ t .Errorf ("blob sidecar mismatch. Have: %s, want: %s" , have , want )
1268
+ }
1269
+ })
1270
+ }
1271
+ }
1272
+
1082
1273
func argsFromTransaction (tx * types.Transaction , from common.Address ) TransactionArgs {
1083
1274
var (
1084
1275
gas = tx .Gas ()
0 commit comments