BlobTx un/marshal json (#7815)

This commit is contained in:
racytech 2023-06-29 09:07:07 +06:00 committed by GitHub
parent 0a1229bd76
commit affef546b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 145 additions and 130 deletions

View File

@ -415,131 +415,117 @@ func (tx *DynamicFeeTransaction) UnmarshalJSON(input []byte) error {
}
func UnmarshalBlobTxJSON(input []byte) (Transaction, error) {
// var dec txJSON
// if err := json.Unmarshal(input, &dec); err != nil {
// return nil, err
var dec txJSON
if err := json.Unmarshal(input, &dec); err != nil {
return nil, err
}
tx := BlobTx{}
if dec.AccessList != nil {
tx.AccessList = *dec.AccessList
} else {
tx.AccessList = []types2.AccessTuple{}
}
if dec.ChainID == nil {
return nil, errors.New("missing required field 'chainId' in transaction")
}
chainID, overflow := uint256.FromBig(dec.ChainID.ToInt())
if overflow {
return nil, errors.New("'chainId' in transaction does not fit in 256 bits")
}
tx.ChainID = chainID
if dec.To != nil {
tx.To = dec.To
}
if dec.Nonce == nil {
return nil, errors.New("missing required field 'nonce' in transaction")
}
tx.Nonce = uint64(*dec.Nonce)
// if dec.GasPrice == nil { // do we need gasPrice here?
// return nil, errors.New("missing required field 'gasPrice' in transaction")
// }
// tx := SignedBlobTx{}
// if dec.AccessList != nil {
// tx.Message.AccessList = AccessListView(*dec.AccessList)
// } else {
// tx.Message.AccessList = AccessListView([]types2.AccessTuple{})
// }
// if dec.ChainID == nil {
// return nil, errors.New("missing required field 'chainId' in transaction")
// }
// chainID, overflow := uint256.FromBig(dec.ChainID.ToInt())
// if overflow {
// return nil, errors.New("'chainId' in transaction does not fit in 256 bits")
// }
// tx.Message.ChainID = Uint256View(*chainID)
// if dec.To != nil {
// address := AddressSSZ(*dec.To)
// tx.Message.To = AddressOptionalSSZ{Address: &address}
// }
// if dec.Nonce == nil {
// return nil, errors.New("missing required field 'nonce' in transaction")
// }
// tx.Message.Nonce = Uint64View(uint64(*dec.Nonce))
// tip, overflow := uint256.FromBig(dec.Tip.ToInt())
// if overflow {
// return nil, errors.New("'tip' in transaction does not fit in 256 bits")
// }
// tx.Message.GasTipCap = Uint256View(*tip)
// feeCap, overflow := uint256.FromBig(dec.FeeCap.ToInt())
// if overflow {
// return nil, errors.New("'feeCap' in transaction does not fit in 256 bits")
// }
// tx.Message.GasFeeCap = Uint256View(*feeCap)
// if dec.Gas == nil {
// return nil, errors.New("missing required field 'gas' in transaction")
// }
// tx.Message.Gas = Uint64View(uint64(*dec.Gas))
// if dec.Value == nil {
// return nil, errors.New("missing required field 'value' in transaction")
// }
// value, overflow := uint256.FromBig(dec.Value.ToInt())
// if overflow {
// return nil, errors.New("'value' in transaction does not fit in 256 bits")
// }
// tx.Message.Value = Uint256View(*value)
// if dec.Data == nil {
// return nil, errors.New("missing required field 'input' in transaction")
// }
// tx.Message.Data = TxDataView(*dec.Data)
tx.Tip, overflow = uint256.FromBig(dec.Tip.ToInt())
if overflow {
return nil, errors.New("'tip' in transaction does not fit in 256 bits")
}
tx.FeeCap, overflow = uint256.FromBig(dec.FeeCap.ToInt())
if overflow {
return nil, errors.New("'feeCap' in transaction does not fit in 256 bits")
}
if dec.Gas == nil {
return nil, errors.New("missing required field 'gas' in transaction")
}
tx.Gas = uint64(*dec.Gas)
if dec.Value == nil {
return nil, errors.New("missing required field 'value' in transaction")
}
tx.Value, overflow = uint256.FromBig(dec.Value.ToInt())
if overflow {
return nil, errors.New("'value' in transaction does not fit in 256 bits")
}
if dec.Data == nil {
return nil, errors.New("missing required field 'input' in transaction")
}
tx.Data = *dec.Data
// if dec.MaxFeePerDataGas == nil {
// return nil, errors.New("missing required field 'maxFeePerDataGas' in transaction")
// }
// maxFeePerDataGas, overflow := uint256.FromBig(dec.MaxFeePerDataGas.ToInt())
// if overflow {
// return nil, errors.New("'maxFeePerDataGas' in transaction does not fit in 256 bits")
// }
// tx.Message.MaxFeePerDataGas = Uint256View(*maxFeePerDataGas)
if dec.MaxFeePerDataGas == nil {
return nil, errors.New("missing required field 'maxFeePerDataGas' in transaction")
}
// if dec.BlobVersionedHashes != nil {
// tx.Message.BlobVersionedHashes = VersionedHashesView(dec.BlobVersionedHashes)
// } else {
// tx.Message.BlobVersionedHashes = VersionedHashesView([]libcommon.Hash{})
// }
maxFeePerDataGas, overflow := uint256.FromBig(dec.MaxFeePerDataGas.ToInt())
if overflow {
return nil, errors.New("'maxFeePerDataGas' in transaction does not fit in 256 bits")
}
tx.MaxFeePerDataGas = maxFeePerDataGas
// if dec.V == nil {
// return nil, errors.New("missing required field 'v' in transaction")
// }
// var v uint256.Int
// overflow = v.SetFromBig(dec.V.ToInt())
// if overflow {
// return nil, fmt.Errorf("dec.V higher than 2^256-1")
// }
// if v.Uint64() > 255 {
// return nil, fmt.Errorf("dev.V higher than 2^8 - 1")
// }
if dec.BlobVersionedHashes != nil {
tx.BlobVersionedHashes = dec.BlobVersionedHashes
} else {
tx.BlobVersionedHashes = []libcommon.Hash{}
}
// tx.Signature.V = Uint8View(v.Uint64())
if dec.V == nil {
return nil, errors.New("missing required field 'v' in transaction")
}
overflow = tx.V.SetFromBig(dec.V.ToInt())
if overflow {
return nil, fmt.Errorf("dec.V higher than 2^256-1")
}
if dec.R == nil {
return nil, errors.New("missing required field 'r' in transaction")
}
overflow = tx.R.SetFromBig(dec.R.ToInt())
if overflow {
return nil, fmt.Errorf("dec.R higher than 2^256-1")
}
if dec.S == nil {
return nil, errors.New("missing required field 's' in transaction")
}
overflow = tx.S.SetFromBig(dec.S.ToInt())
if overflow {
return nil, fmt.Errorf("dec.S higher than 2^256-1")
}
// if dec.R == nil {
// return nil, errors.New("missing required field 'r' in transaction")
// }
// var r uint256.Int
// overflow = r.SetFromBig(dec.R.ToInt())
// if overflow {
// return nil, fmt.Errorf("dec.R higher than 2^256-1")
// }
// tx.Signature.R = Uint256View(r)
withSignature := !tx.V.IsZero() || !tx.R.IsZero() || !tx.S.IsZero()
if withSignature {
if err := sanityCheckSignature(&tx.V, &tx.R, &tx.S, false); err != nil {
return nil, err
}
}
// if dec.S == nil {
// return nil, errors.New("missing required field 's' in transaction")
// }
// var s uint256.Int
// overflow = s.SetFromBig(dec.S.ToInt())
// if overflow {
// return nil, errors.New("'s' in transaction does not fit in 256 bits")
// }
// tx.Signature.S = Uint256View(s)
if len(dec.Blobs) == 0 {
// if no blobs are specified in the json we assume it is an unwrapped blob tx
return &tx, nil
}
// withSignature := !v.IsZero() || !r.IsZero() || !s.IsZero()
// if withSignature {
// if err := sanityCheckSignature(&v, &r, &s, false); err != nil {
// return nil, err
// }
// }
// if len(dec.Blobs) == 0 {
// // if no blobs are specified in the json we assume it is an unwrapped blob tx
// return &tx, nil
// }
// btx := BlobTxWrapper{
// Tx: tx,
// Commitments: dec.Commitments,
// Blobs: dec.Blobs,
// Proofs: dec.Proofs,
// }
// err := btx.ValidateBlobTransactionWrapper()
// if err != nil {
// return nil, err
// }
// return &btx, nil
return nil, nil
btx := BlobTxWrapper{
Tx: tx,
Commitments: dec.Commitments,
Blobs: dec.Blobs,
Proofs: dec.Proofs,
}
err := btx.ValidateBlobTransactionWrapper()
if err != nil {
return nil, err
}
return &btx, nil
}

View File

@ -684,9 +684,12 @@ func newRandBlobTx() *BlobTx {
To: randAddr(),
Value: uint256.NewInt(rand.Uint64()),
Data: randData(),
V: *uint256.NewInt(rand.Uint64()),
R: *uint256.NewInt(rand.Uint64()),
S: *uint256.NewInt(rand.Uint64()),
// V: *uint256.NewInt(rand.Uint64()),
// R: *uint256.NewInt(rand.Uint64()),
// S: *uint256.NewInt(rand.Uint64()),
V: *uint256.NewInt(0),
R: *uint256.NewInt(rand.Uint64()),
S: *uint256.NewInt(rand.Uint64()),
},
ChainID: uint256.NewInt(rand.Uint64()),
Tip: uint256.NewInt(rand.Uint64()),
@ -694,7 +697,7 @@ func newRandBlobTx() *BlobTx {
AccessList: randAccessList(),
},
MaxFeePerDataGas: uint256.NewInt(rand.Uint64()),
BlobVersionedHashes: randHashes(randIntInRange(5, 10)),
BlobVersionedHashes: randHashes(randIntInRange(0, 6)),
}
return stx
}
@ -768,12 +771,14 @@ func newRandBlobs(size int) Blobs {
return result
}
func newRandBlobWrapper(size int) *BlobTxWrapper {
func newRandBlobWrapper() *BlobTxWrapper {
btxw := *newRandBlobTx()
l := len(btxw.BlobVersionedHashes)
return &BlobTxWrapper{
Tx: *newRandBlobTx(),
Commitments: newRandCommitments(size),
Blobs: newRandBlobs(size),
Proofs: newRandProofs(size),
Tx: btxw,
Commitments: newRandCommitments(l),
Blobs: newRandBlobs(l),
Proofs: newRandProofs(l),
}
}
@ -785,8 +790,7 @@ func populateBlobTxs() {
func populateBlobWrapperTxs() {
for i := 0; i < N-1; i++ {
n := randIntInRange(0, 10)
dummyBlobWrapperTxs[i] = newRandBlobWrapper(n)
dummyBlobWrapperTxs[i] = newRandBlobWrapper()
}
dummyBlobWrapperTxs[N-1] = &BlobTxWrapper{
@ -810,6 +814,16 @@ func TestBlobTxEncodeDecode(t *testing.T) {
if err := assertEqual(dummyBlobTxs[i], tx); err != nil {
t.Fatal(err)
}
// JSON
tx, err = encodeDecodeJSON(dummyBlobTxs[i])
if err != nil {
t.Fatal(err)
}
if err = assertEqual(dummyBlobTxs[i], tx); err != nil {
t.Fatal(err)
}
}
}
@ -827,5 +841,20 @@ func TestBlobTxWrappedEncodeDecode(t *testing.T) {
if err := assertEqualBlobWrapper(dummyBlobWrapperTxs[i], tx); err != nil {
t.Fatal(err)
}
// JSON
// fails in ValidateBlobTransactionWrapper()
// error during proof verification: invalid infinity point encoding
// jtx, err := encodeDecodeJSON(dummyBlobWrapperTxs[i])
// if err != nil {
// t.Fatal(err)
// }
// if err = assertEqual(dummyBlobWrapperTxs[i], jtx); err != nil {
// t.Fatal(err)
// }
// if err := assertEqualBlobWrapper(dummyBlobWrapperTxs[i], jtx.(*BlobTxWrapper)); err != nil {
// t.Fatal(err)
// }
}
}