Skip to content
Snippets Groups Projects
  • Jonas Theis's avatar
    7db8e8e1
    TipManager & ValueObject factory (#417) · 7db8e8e1
    Jonas Theis authored
    
    * Add TipManager with naive locking and some basic tests
    
    * Add ValueObjectFactory
    
    * Bones
    
    * Refactor TipManager to be able to handle tips in the context of branches
    
    * Add unit tests
    
    * Add more tests
    
    * Feat: initial commit
    
    * Feat: added setPreferred to TransactionMetadata
    
    * Feat: added a Conflicting() method to the transactionMetadata
    
    * Fix: fixed logic bug
    
    * Feat: refactored fcob
    
    * Refactor: refactored additional code
    
    * Fix: fixed a bug in ForeachConsumers
    
    * Refactor: cleaned up code
    
    * Feat: implemented FCOB consensus into the valuetransfer dapp
    
    * Refactor: refactored FCOB
    
    * Docs: added some additional comments
    
    * Docs: fixed comments
    
    * Refactor: commit before branch change
    
    * Feat: added PayloadLiked Event
    
    * Refactor: fixed some missing comments + added liked to marshal
    
    * Feat: reworked the preferred and liked propagation
    
    * Refactor: cleaned up some logic
    
    * Refactor: simplified code
    
    * Refactor: cleaned up more stuff :P
    
    * Refactor: refactor
    
    * Feat: moved test + refactored fcob
    
    * Fix: fixed a few bugs in liked propagation
    
    * Rewrite TipManager to maintain a simple flat map of tips. The value transfers plugin needs to call the corresponding functions AddTip and RemoveTip when a value object is liked or disliked, respectively.
    
    * adapt to new hive.go version
    
    * upgrade hive.go
    
    * Feat: started implementing a wallet
    
    * Feat: extended wallet files
    
    * Integrate tipmanager on tangle events
    
    * Rename TipCount to Size
    
    Co-authored-by: default avatarWolfgang Welz <welzwo@gmail.com>
    Co-authored-by: default avatarHans Moog <hm@mkjc.net>
    TipManager & ValueObject factory (#417)
    Jonas Theis authored
    
    * Add TipManager with naive locking and some basic tests
    
    * Add ValueObjectFactory
    
    * Bones
    
    * Refactor TipManager to be able to handle tips in the context of branches
    
    * Add unit tests
    
    * Add more tests
    
    * Feat: initial commit
    
    * Feat: added setPreferred to TransactionMetadata
    
    * Feat: added a Conflicting() method to the transactionMetadata
    
    * Fix: fixed logic bug
    
    * Feat: refactored fcob
    
    * Refactor: refactored additional code
    
    * Fix: fixed a bug in ForeachConsumers
    
    * Refactor: cleaned up code
    
    * Feat: implemented FCOB consensus into the valuetransfer dapp
    
    * Refactor: refactored FCOB
    
    * Docs: added some additional comments
    
    * Docs: fixed comments
    
    * Refactor: commit before branch change
    
    * Feat: added PayloadLiked Event
    
    * Refactor: fixed some missing comments + added liked to marshal
    
    * Feat: reworked the preferred and liked propagation
    
    * Refactor: cleaned up some logic
    
    * Refactor: simplified code
    
    * Refactor: cleaned up more stuff :P
    
    * Refactor: refactor
    
    * Feat: moved test + refactored fcob
    
    * Fix: fixed a few bugs in liked propagation
    
    * Rewrite TipManager to maintain a simple flat map of tips. The value transfers plugin needs to call the corresponding functions AddTip and RemoveTip when a value object is liked or disliked, respectively.
    
    * adapt to new hive.go version
    
    * upgrade hive.go
    
    * Feat: started implementing a wallet
    
    * Feat: extended wallet files
    
    * Integrate tipmanager on tangle events
    
    * Rename TipCount to Size
    
    Co-authored-by: default avatarWolfgang Welz <welzwo@gmail.com>
    Co-authored-by: default avatarHans Moog <hm@mkjc.net>
factory.go 1.55 KiB
package tangle

import (
	"github.com/iotaledger/goshimmer/dapps/valuetransfers/packages/payload"
	"github.com/iotaledger/goshimmer/dapps/valuetransfers/packages/tipmanager"
	"github.com/iotaledger/goshimmer/dapps/valuetransfers/packages/transaction"
	"github.com/iotaledger/hive.go/events"
)

// ValueObjectFactory acts as a factory to create new value objects.
type ValueObjectFactory struct {
	tipManager *tipmanager.TipManager
	Events     *ValueObjectFactoryEvents
}

// NewValueObjectFactory creates a new ValueObjectFactory.
func NewValueObjectFactory(tipManager *tipmanager.TipManager) *ValueObjectFactory {
	return &ValueObjectFactory{
		tipManager: tipManager,
		Events: &ValueObjectFactoryEvents{
			ValueObjectConstructed: events.NewEvent(valueObjectConstructedEvent),
		},
	}
}

// IssueTransaction creates a new value object including tip selection and returns it.
// It also triggers the ValueObjectConstructed event once it's done.
func (v *ValueObjectFactory) IssueTransaction(tx *transaction.Transaction) *payload.Payload {
	parent1, parent2 := v.tipManager.Tips()

	valueObject := payload.New(parent1, parent2, tx)
	v.Events.ValueObjectConstructed.Trigger(valueObject)

	return valueObject
}

// ValueObjectFactoryEvents represent events happening on a ValueObjectFactory.
type ValueObjectFactoryEvents struct {
	// Fired when a value object is built including tips.
	ValueObjectConstructed *events.Event
}

func valueObjectConstructedEvent(handler interface{}, params ...interface{}) {
	handler.(func(*transaction.Transaction))(params[0].(*transaction.Transaction))
}