Skip to content
This repository was archived by the owner on Jul 20, 2021. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/rivined/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/threefoldtech/rivine/modules/wallet"
"github.com/threefoldtech/rivine/pkg/api"
"github.com/threefoldtech/rivine/pkg/daemon"
"github.com/threefoldtech/rivine/types"
)

func runDaemon(cfg daemon.Config, networkCfg daemon.NetworkConfig, moduleIdentifiers daemon.ModuleIdentifierSet) error {
Expand Down Expand Up @@ -85,6 +86,7 @@ func runDaemon(cfg daemon.Config, networkCfg daemon.NetworkConfig, moduleIdentif
fmt.Println("Error during consensus set shutdown:", err)
}
}()
types.RegisterTransactionVersion(types.TransactionVersionBlockCreation, types.NewBlockCreationTransactionController(cs))

}
var tpool modules.TransactionPool
Expand Down
42 changes: 31 additions & 11 deletions modules/blockcreator/proofofblockstake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package blockcreator

import (
"encoding/json"
"errors"
"math/big"
"time"

Expand Down Expand Up @@ -157,24 +158,43 @@ func (bc *BlockCreator) RespentBlockStake(ubso types.UnspentBlockStakeOutput) er
}
}

//otherwise the blockstake is not yet spent in this block, spent it now
t := bc.wallet.StartTransaction()
err := t.SpendBlockStake(ubso.BlockStakeOutputID) // link the input of this transaction
// to the used BlockStake output
// No valid blockstake have been spend yet in the block to be created

// get current height so we can store the block height of the block the tx is
// supposed to make in the block (currentHeight + 1)
currentHeight := bc.cs.Height()

bso, err := bc.cs.GetBlockStakeOutput(ubso.BlockStakeOutputID)
if err != nil {
return err
}

bso := types.BlockStakeOutput{
Value: ubso.Value, //use the same amount of BlockStake
Condition: ubso.Condition, //use the same condition.
if bso.Condition.ConditionType() != types.ConditionTypeUnlockHash {
return errors.New("unsupported output condition for pobs")
}
t.AddBlockStakeOutput(bso)
txnSet, err := t.Sign()

pk, _, err := bc.wallet.GetKey(bso.Condition.UnlockHash())
if err != nil {
return errors.New("Failed to retrieve public key for blockstake output")
}

// create the input for the used output
input := types.BlockStakeInput{ParentID: ubso.BlockStakeOutputID, Fulfillment: types.NewFulfillment(types.NewSingleSignatureFulfillment(pk))}

// create the extension for the block creation tx based on the used input
txExt := &types.BlockCreationTransactionExtension{Reference: input, Height: types.BlockHeight(currentHeight + 1)}

// Create transactionbuilder for the new transaction and add the extension
txb := bc.wallet.StartTransactionWithVersion(types.TransactionVersionBlockCreation)
txb.SetExtension(txExt)
if err = txb.SignAllPossible(); err != nil {
return err
}
//add this transaction in front of the list of unsolved block transactions
bc.unsolvedBlock.Transactions = append(txnSet, bc.unsolvedBlock.Transactions...)

tx, _ := txb.View()

// add this transaction in front of the list of unsolved block transactions
bc.unsolvedBlock.Transactions = append([]types.Transaction{tx}, bc.unsolvedBlock.Transactions...)

return nil
}
23 changes: 23 additions & 0 deletions modules/consensus/block_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ func (bv stdBlockValidator) ValidateBlock(b types.Block, minTimestamp types.Time
spent = true
}
}
// check block creation txes, as they don't technically respend
if tr.Version == types.TransactionVersionBlockCreation {
ext, ok := tr.Extension.(*types.BlockCreationTransactionExtension)
if !ok {
// should never happen
continue
}
if ext.Reference.ParentID == bsoid {
spent = true
}
}
}
}
}
Expand All @@ -123,6 +134,18 @@ func (bv stdBlockValidator) ValidateBlock(b types.Block, minTimestamp types.Time
spent = true
}
}
// check block creation txes, as they don't technically respend
if tr.Version == types.TransactionVersionBlockCreation {
ext, ok := tr.Extension.(*types.BlockCreationTransactionExtension)
if !ok {
// should never happen
continue
}
if ext.Reference.ParentID == blockatheight.Transactions[ubsu.TransactionIndex].BlockStakeOutputID(ubsu.OutputIndex) {
bv.cs.log.Debugf("[SBV] Confirmed blockstake respend from an inactive fork, ubsu in block %d, new block at height %d\n", ubsu.BlockHeight, height)
spent = true
}
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions modules/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ type (
// AddArbitraryData sets the arbitrary data of the transaction.
SetArbitraryData(arb []byte)

// SetExtension sets the extension of the transaction
SetExtension(interface{})

// Sign will sign any inputs added by 'FundCoins' or 'FundBlockStakes'
// and return a transaction set that contains all parents prepended to
// the transaction. If more fields need to be added, a new transaction
Expand Down Expand Up @@ -389,6 +392,10 @@ type (
// RegisterTransaction(types.Transaction{}, nil)
StartTransaction() TransactionBuilder

// StartTransactionWithVersion is a convenience function that calls
// RegisterTransaction(types.Transaction{Version: version}, nil).
StartTransactionWithVersion(types.TransactionVersion) TransactionBuilder

// SendCoins is a tool for sending coins from the wallet to anyone who can fulfill the
// given condition (can be nil). The transaction is automatically given to the transaction pool, and
// are also returned to the caller.
Expand Down
5 changes: 5 additions & 0 deletions modules/wallet/transactionbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ func (tb *transactionBuilder) SetArbitraryData(arb []byte) {
tb.transaction.ArbitraryData = arb
}

// SetExtension sets the extension of the transaction
func (tb *transactionBuilder) SetExtension(extension interface{}) {
tb.transaction.Extension = extension
}

// Drop discards all of the outputs in a transaction, returning them to the
// pool so that other transactions may use them. 'Drop' should only be called
// if a transaction is both unsigned and will not be used any further.
Expand Down
Loading