my-cloud / ruthenium

Golang implementation of the Ruthenium protocol
The Unlicense
11 stars 1 forks source link

The same transaction can be added twice #121

Closed JeremyPansier closed 1 year ago

JeremyPansier commented 1 year ago

To Reproduce Steps to reproduce the behavior:

  1. The last block contains a transaction.
  2. The transactions pool contains the same transaction.
  3. A new block is created.

Expected behavior The new block doesn't contain the transaction.

Actual behavior The new block contains the transaction.

JeremyPansier commented 1 year ago

The verification done when adding a transaction should be done for each transaction just before creating the new block:

    if len(blocks) > 2 {
        if transaction.Timestamp() < blocks[len(blocks)-2].Timestamp {
            err = errors.New("the transaction timestamp is invalid")
            return
        }
        for i := len(blocks) - 2; i < len(blocks); i++ {
            for _, validatedTransaction := range blocks[i].Transactions {
                if transaction.Equals(validatedTransaction) {
                    err = errors.New("the transaction is already in the blockchain")
                    return
                }
            }
        }
    }