Skip to content

Commit

Permalink
Fix transaction commitment
Browse files Browse the repository at this point in the history
  • Loading branch information
IronGauntlets committed Jul 30, 2024
1 parent 476baa7 commit d36765c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ func (b *Blockchain) Store(block *core.Block, blockCommitments *core.BlockCommit
if err := verifyBlock(txn, block); err != nil {
return err
}
return errors.New("temp error")
if err := core.NewState(txn).Update(block.Number, stateUpdate, newClasses); err != nil { //nolint:govet

if err := core.NewState(txn).Update(block.Number, stateUpdate, newClasses); err != nil {
return err
}
if err := StoreBlockHeader(txn, block.Header); err != nil {
Expand Down
19 changes: 13 additions & 6 deletions core/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func VerifyBlockHash(b *Block, network *utils.Network, stateDiff *StateDiff) (*B
if blockVer.LessThan(v0_13_2) {
hash, commitments, err = blockHash(b, network, overrideSeq)
} else {
hash, commitments, err = Post0132Hash(b, stateDiff.Length(), stateDiff.Commitment())
hash, commitments, err = Post0132Hash(b, stateDiff)

Check warning on line 128 in core/block.go

View check run for this annotation

Codecov / codecov/patch

core/block.go#L128

Added line #L128 was not covered by tests
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -188,12 +188,13 @@ func pre07Hash(b *Block, chain *felt.Felt) (*felt.Felt, *BlockCommitments, error
), &BlockCommitments{TransactionCommitment: txCommitment}, nil
}

func Post0132Hash(b *Block, stateDiffLen uint64, stateDiffHash *felt.Felt) (*felt.Felt, *BlockCommitments, error) {
func Post0132Hash(b *Block, stateDiff *StateDiff) (*felt.Felt, *BlockCommitments, error) {

Check warning on line 191 in core/block.go

View check run for this annotation

Codecov / codecov/patch

core/block.go#L191

Added line #L191 was not covered by tests
seqAddr := b.SequencerAddress
// todo override support?

wg := conc.NewWaitGroup()
var txCommitment, eCommitment, rCommitment *felt.Felt
var txCommitment, eCommitment, rCommitment, sdCommitment *felt.Felt
var sdLength uint64

Check warning on line 197 in core/block.go

View check run for this annotation

Codecov / codecov/patch

core/block.go#L196-L197

Added lines #L196 - L197 were not covered by tests
var tErr, eErr, rErr error

wg.Go(func() {
Expand All @@ -205,9 +206,15 @@ func Post0132Hash(b *Block, stateDiffLen uint64, stateDiffHash *felt.Felt) (*fel
wg.Go(func() {
rCommitment, rErr = receiptCommitment(b.Receipts)
})

wg.Go(func() {
sdLength = stateDiff.Length()
sdCommitment = stateDiff.Hash()
})

Check warning on line 213 in core/block.go

View check run for this annotation

Codecov / codecov/patch

core/block.go#L210-L213

Added lines #L210 - L213 were not covered by tests

wg.Wait()

fmt.Println("Commitments", txCommitment, eCommitment, rCommitment, stateDiffLen, stateDiffHash)
fmt.Println("Commitments", txCommitment, eCommitment, rCommitment, sdLength, sdCommitment)

Check warning on line 217 in core/block.go

View check run for this annotation

Codecov / codecov/patch

core/block.go#L217

Added line #L217 was not covered by tests

if tErr != nil {
return nil, nil, tErr
Expand All @@ -219,7 +226,7 @@ func Post0132Hash(b *Block, stateDiffLen uint64, stateDiffHash *felt.Felt) (*fel
return nil, nil, rErr
}

concatCounts := ConcatCounts(b.TransactionCount, b.EventCount, stateDiffLen, b.L1DAMode)
concatCounts := ConcatCounts(b.TransactionCount, b.EventCount, sdLength, b.L1DAMode)

Check warning on line 229 in core/block.go

View check run for this annotation

Codecov / codecov/patch

core/block.go#L229

Added line #L229 was not covered by tests

return crypto.PoseidonArray(
new(felt.Felt).SetBytes([]byte("STARKNET_BLOCK_HASH0")),
Expand All @@ -228,7 +235,7 @@ func Post0132Hash(b *Block, stateDiffLen uint64, stateDiffHash *felt.Felt) (*fel
seqAddr, // sequencer address
new(felt.Felt).SetUint64(b.Timestamp), // block timestamp
concatCounts,
stateDiffHash,
sdCommitment,

Check warning on line 238 in core/block.go

View check run for this annotation

Codecov / codecov/patch

core/block.go#L238

Added line #L238 was not covered by tests
txCommitment, // transaction commitment
eCommitment, // event commitment
rCommitment, // receipt commitment
Expand Down
14 changes: 9 additions & 5 deletions core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,12 +669,16 @@ func TransactionCommitmentPoseidon(transactions []Transaction) (*felt.Felt, erro
var commitment *felt.Felt
return commitment, trie.RunOnTempTriePoseidon(commitmentTrieHeight, func(trie *trie.Trie) error {
for i, transaction := range transactions {
hashElems := []*felt.Felt{transaction.Hash()}
// todo handle empty signature
hashElems = append(hashElems, transaction.Signature()...)
hash := crypto.PoseidonArray(hashElems...)
var digest crypto.PoseidonDigest
digest.Update(transaction.Hash())

Check warning on line 673 in core/transaction.go

View check run for this annotation

Codecov / codecov/patch

core/transaction.go#L672-L673

Added lines #L672 - L673 were not covered by tests

if _, err := trie.Put(new(felt.Felt).SetUint64(uint64(i)), hash); err != nil {
if txSignature := transaction.Signature(); len(txSignature) > 0 {
digest.Update(txSignature...)
} else {
digest.Update(&felt.Zero)

Check warning on line 678 in core/transaction.go

View check run for this annotation

Codecov / codecov/patch

core/transaction.go#L675-L678

Added lines #L675 - L678 were not covered by tests
}

if _, err := trie.Put(new(felt.Felt).SetUint64(uint64(i)), digest.Finish()); err != nil {

Check warning on line 681 in core/transaction.go

View check run for this annotation

Codecov / codecov/patch

core/transaction.go#L681

Added line #L681 was not covered by tests
return err
}
}
Expand Down

0 comments on commit d36765c

Please sign in to comment.