-
Notifications
You must be signed in to change notification settings - Fork 328
/
blob_data.go
200 lines (182 loc) · 5.88 KB
/
blob_data.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) 2024 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package action
import (
"crypto/sha256"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
)
// BlobTxData represents an EIP-4844 transaction
type BlobTxData struct {
blobFeeCap *uint256.Int // a.k.a. maxFeePerBlobGas
blobHashes []common.Hash
// A blob transaction can optionally contain blobs. This field must be set when BlobTx
// is used to create a transaction for signing
sidecar *types.BlobTxSidecar
}
func NewBlobTxData(blobFeeCap *uint256.Int, blobHashes []common.Hash, sidecar *types.BlobTxSidecar) *BlobTxData {
return &BlobTxData{
blobFeeCap: blobFeeCap,
blobHashes: blobHashes,
sidecar: sidecar,
}
}
func (tx *BlobTxData) gasFeeCap() *uint256.Int {
p := uint256.Int{}
if tx.blobFeeCap != nil {
p.Set(tx.blobFeeCap)
}
return &p
}
func (tx *BlobTxData) hashes() []common.Hash {
return tx.blobHashes
}
func (tx *BlobTxData) gas() uint64 {
return params.BlobTxBlobGasPerBlob * uint64(len(tx.blobHashes))
}
func (tx *BlobTxData) blobHashesProto() [][]byte {
size := len(tx.blobHashes)
if tx.blobHashes == nil || size == 0 {
return nil
}
hashes := make([][]byte, size)
for i := 0; i < size; i++ {
hashes[i] = tx.blobHashes[i][:]
}
return hashes
}
func ToProtoSideCar(sidecar *types.BlobTxSidecar) *iotextypes.BlobTxSidecar {
if sidecar == nil || len(sidecar.Blobs) == 0 {
return nil
}
pbSidecar := iotextypes.BlobTxSidecar{}
if size := len(sidecar.Blobs); size > 0 {
pbSidecar.Blobs = make([][]byte, size)
for i := 0; i < size; i++ {
pbSidecar.Blobs[i] = sidecar.Blobs[i][:]
}
}
if size := len(sidecar.Commitments); size > 0 {
pbSidecar.Commitments = make([][]byte, size)
for i := 0; i < size; i++ {
pbSidecar.Commitments[i] = sidecar.Commitments[i][:]
}
}
if size := len(sidecar.Proofs); size > 0 {
pbSidecar.Proofs = make([][]byte, size)
for i := 0; i < size; i++ {
pbSidecar.Proofs[i] = sidecar.Proofs[i][:]
}
}
return &pbSidecar
}
func (tx *BlobTxData) toProto() *iotextypes.BlobTxData {
blob := iotextypes.BlobTxData{}
if tx.blobFeeCap != nil {
blob.BlobFeeCap = tx.blobFeeCap.String()
}
blob.BlobHashes = tx.blobHashesProto()
blob.BlobTxSidecar = ToProtoSideCar(tx.sidecar)
return &blob
}
func fromProtoBlobTxData(pb *iotextypes.BlobTxData) (*BlobTxData, error) {
if pb == nil {
return nil, ErrNilProto
}
blob := BlobTxData{
blobFeeCap: &uint256.Int{},
}
if fee := pb.GetBlobFeeCap(); len(fee) > 0 {
if err := blob.blobFeeCap.SetFromDecimal(fee); err != nil {
return nil, errors.Wrap(err, "invalid blob fee")
}
}
if bh := pb.GetBlobHashes(); bh != nil {
blob.blobHashes = make([]common.Hash, len(bh))
for i := 0; i < len(bh); i++ {
blob.blobHashes[i] = common.BytesToHash(bh[i])
}
}
if sc := pb.GetBlobTxSidecar(); sc != nil {
var err error
if blob.sidecar, err = FromProtoBlobTxSideCar(sc); err != nil {
return nil, err
}
}
return &blob, nil
}
func FromProtoBlobTxSideCar(pb *iotextypes.BlobTxSidecar) (*types.BlobTxSidecar, error) {
if pb == nil || len(pb.Blobs) == 0 {
return nil, ErrNilProto
}
sidecar := types.BlobTxSidecar{
Blobs: make([]kzg4844.Blob, len(pb.Blobs)),
Commitments: make([]kzg4844.Commitment, len(pb.Commitments)),
Proofs: make([]kzg4844.Proof, len(pb.Proofs)),
}
for i := range pb.Blobs {
sidecar.Blobs[i] = *(*kzg4844.Blob)(pb.Blobs[i])
}
for i := range pb.Commitments {
sidecar.Commitments[i] = *(*kzg4844.Commitment)(pb.Commitments[i])
}
for i := range pb.Proofs {
sidecar.Proofs[i] = *(*kzg4844.Proof)(pb.Proofs[i])
}
return &sidecar, nil
}
func (tx *BlobTxData) SanityCheck() error {
if price := tx.blobFeeCap; price != nil && price.Sign() < 0 {
return errors.Wrap(ErrNegativeValue, "negative blob fee cap")
}
if len(tx.blobHashes) == 0 {
return errors.New("blobless blob transaction")
}
if permitted := params.MaxBlobGasPerBlock / params.BlobTxBlobGasPerBlob; len(tx.blobHashes) > permitted {
return errors.Errorf("too many blobs in transaction: have %d, permitted %d", len(tx.blobHashes), params.MaxBlobGasPerBlock/params.BlobTxBlobGasPerBlob)
}
return nil
}
func (tx *BlobTxData) ValidateSidecar() error {
if tx.sidecar == nil {
return errors.New("sidecar is missing")
}
return verifySidecar(tx.sidecar, tx.blobHashes)
}
func verifySidecar(sidecar *types.BlobTxSidecar, hashes []common.Hash) error {
size := len(hashes)
// Verify the size of hashes, commitments and proofs
if len(sidecar.Blobs) != size {
return errors.New("number of blobs and hashes mismatch")
}
if len(sidecar.Commitments) != size {
return errors.New("number of blobs and commitments mismatch")
}
if len(sidecar.Proofs) != size {
return errors.New("number of blobs and proofs mismatch")
}
// Blob quantities match up, validate that the provers match with the
// transaction hash before getting to the cryptography
hasher := sha256.New()
for i, vhash := range hashes {
computed := kzg4844.CalcBlobHashV1(hasher, &sidecar.Commitments[i])
if vhash != computed {
return errors.Errorf("blob %d: computed hash %x mismatches transaction one %x", i, computed, vhash)
}
}
// Blob commitments match with the hashes in the transaction, verify the
// blobs themselves via KZG
for i := range sidecar.Blobs {
if err := kzg4844.VerifyBlobProof(sidecar.Blobs[i], sidecar.Commitments[i], sidecar.Proofs[i]); err != nil {
return errors.Errorf("invalid blob %d: %v", i, err)
}
}
return nil
}