Skip to content

Commit

Permalink
don't scale-encode witness data
Browse files Browse the repository at this point in the history
Align raw TX format to the genvm: `raw = <tx bytes> | <witness data>`.
  • Loading branch information
poszu committed Jan 10, 2025
1 parent 148208e commit 2a1d300
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 25 deletions.
6 changes: 1 addition & 5 deletions vm/core/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ func SignedTx(tx *Tx, genesisID types.Hash20, pk signing.PrivateKey) ([]byte, er
return nil, fmt.Errorf("encoding deploy TX: %w", err)
}

sig := SignRawTx(encodedTx.Bytes(), genesisID, pk)
if _, err := scale.EncodeByteSlice(enc, sig); err != nil {
return nil, fmt.Errorf("encoding TX signature: %w", err)
}

encodedTx.Write(SignRawTx(encodedTx.Bytes(), genesisID, pk))
return encodedTx.Bytes(), nil
}
11 changes: 3 additions & 8 deletions vm/sdk/multisig/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,15 @@ func (a *SignatureAggregator) Add(ref uint8, sig core.Signature) {

// Raw returns full raw transaction including payload and signatures.
func (a *SignatureAggregator) Raw() []byte {
var buf bytes.Buffer
enc := scale.NewEncoder(&buf)
buf := bytes.NewBuffer(a.unsigned)
enc := scale.NewEncoder(buf)
keys := slices.Sorted(maps.Keys(a.parts))
for _, ref := range keys {
if err := enc.Encode(a.parts[ref]); err != nil {
panic(err)
}
}
rawTxBuf := bytes.NewBuffer(a.unsigned)
enc = scale.NewEncoder(rawTxBuf)
if err := enc.Encode(buf.Bytes()); err != nil {
panic(err)
}
return rawTxBuf.Bytes()
return buf.Bytes()
}

func EncodeSpawnArgs(required uint8, pubkeys []core.PublicKey) []byte {
Expand Down
15 changes: 3 additions & 12 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
"io"
"time"

gossamerScale "github.com/ChainSafe/gossamer/pkg/scale"
Expand Down Expand Up @@ -537,14 +536,6 @@ func parse(
if tx.Version != 1 {
return nil, nil, fmt.Errorf("%w: %d", errWrongVersion, tx.Version)
}
// Decode witness data
witnessData, _, err := scale.DecodeByteSlice(decoder)
switch {
case errors.Is(err, io.EOF):
logger.Debug("witness data is empty")
case err != nil:
return nil, nil, fmt.Errorf("decoding witness data: %w", err)
}

ctx, err := core.New(cfg.GenesisID, lid, tx.Principal, loader, logger)
if err != nil {
Expand Down Expand Up @@ -602,7 +593,7 @@ func parse(
}
ctx.TxPayload = tx.Payload
ctx.TxData = raw[:n]
ctx.WitnessData = witnessData
ctx.WitnessData = raw[n:]

// At this point we've established that the transaction is correctly formed, but we haven't
// yet attempted to validate the signature. That happens later in Verify().
Expand All @@ -616,10 +607,10 @@ func parse(
logger.Debug(
"calculating max gas",
zap.Int("payload len", len(tx.Payload)),
zap.Int("witness data len", len(witnessData)),
zap.Int("witness data len", len(ctx.WitnessData)),
)
ctx.Header.MaxGas = core.MaxGas(
estimatedStateSize + max(len(tx.Payload), 6) - 6 + len(witnessData),
estimatedStateSize + max(len(tx.Payload), 6) - 6 + len(ctx.WitnessData),
) // skip bytes for method selector
ctx.Header.Principal = tx.Principal
ctx.Header.GasPrice = tx.Metadata.GasPrice
Expand Down

0 comments on commit 2a1d300

Please sign in to comment.