Skip to content

Commit

Permalink
Unify ContextInput and Input
Browse files Browse the repository at this point in the history
  • Loading branch information
karimodm committed Aug 2, 2023
1 parent 70beca2 commit 5e56919
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 44 deletions.
6 changes: 3 additions & 3 deletions api_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,13 @@ func V3API(protoParams ProtocolParameters) API {
must(api.RegisterTypeSettings(TransactionEssence{}, serix.TypeSettings{}.WithObjectType(TransactionEssenceNormal)))

must(api.RegisterTypeSettings(CommitmentInput{},
serix.TypeSettings{}.WithObjectType(uint8(ContextInputCommitment))),
serix.TypeSettings{}.WithObjectType(uint8(InputCommitment))),
)
must(api.RegisterTypeSettings(BlockIssuanceCreditInput{},
serix.TypeSettings{}.WithObjectType(uint8(ContextInputBlockIssuanceCredit))),
serix.TypeSettings{}.WithObjectType(uint8(InputBlockIssuanceCredit))),
)
must(api.RegisterTypeSettings(RewardInput{},
serix.TypeSettings{}.WithObjectType(uint8(ContextInputReward))),
serix.TypeSettings{}.WithObjectType(uint8(InputReward))),
)

must(api.RegisterTypeSettings(TxEssenceContextInputs{},
Expand Down
2 changes: 1 addition & 1 deletion builder/transaction_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (b *TransactionBuilder) AddInput(input *TxInput) *TransactionBuilder {
type TransactionBuilderInputFilter func(outputID iotago.OutputID, input iotago.Output) bool

// AddContextInput adds the given context input to the builder.
func (b *TransactionBuilder) AddContextInput(contextInput iotago.ContextInput) *TransactionBuilder {
func (b *TransactionBuilder) AddContextInput(contextInput iotago.Input) *TransactionBuilder {
b.essence.ContextInputs = append(b.essence.ContextInputs, contextInput)

return b
Expand Down
10 changes: 7 additions & 3 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ type InputType byte
const (
// InputUTXO is a type of input which references an unspent transaction output.
InputUTXO InputType = iota
// InputCommitment is a type of input which references a commitment.
InputCommitment
// InputBlockIssuanceCredit is a type of input which references the block issuance credit from a specific account and commitment, the latter being provided by a commitment input.
InputBlockIssuanceCredit
// InputReward is a type of input which references an Account or Delegation Input for which to claim rewards.
InputReward
)

func (inputType InputType) String() string {
Expand All @@ -23,9 +29,7 @@ func (inputType InputType) String() string {
return inputNames[inputType]
}

var (
inputNames = [InputUTXO + 1]string{"UTXOInput"}
)
var inputNames = [InputUTXO + 1]string{"UTXOInput"}

var (
// ErrRefUTXOIndexInvalid gets returned on invalid UTXO indices.
Expand Down
4 changes: 2 additions & 2 deletions input_block_issuance_credit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ type BlockIssuanceCreditInput struct {
AccountID AccountID `serix:"0,mapKey=accountId"`
}

func (b *BlockIssuanceCreditInput) Type() ContextInputType {
return ContextInputBlockIssuanceCredit
func (b *BlockIssuanceCreditInput) Type() InputType {
return InputBlockIssuanceCredit
}

func (b *BlockIssuanceCreditInput) Size() int {
Expand Down
4 changes: 2 additions & 2 deletions input_commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ type CommitmentInput struct {
CommitmentID CommitmentID `serix:"0,mapKey=commitmentId"`
}

func (c *CommitmentInput) Type() ContextInputType {
return ContextInputCommitment
func (c *CommitmentInput) Type() InputType {
return InputCommitment
}

func (c *CommitmentInput) Size() int {
Expand Down
30 changes: 6 additions & 24 deletions input_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ import (
// ContextInputType defines the type of context inputs.
type ContextInputType byte

const (
// ContextInputCommitment is a type of input which references a commitment.
ContextInputCommitment ContextInputType = iota
// ContextInputBlockIssuanceCredit is a type of input which references the block issuance credit from a specific account and commitment, the latter being provided by a commitment input.
ContextInputBlockIssuanceCredit
// ContextInputReward is a type of input which references an Account or Delegation Input for which to claim rewards.
ContextInputReward
)

func (inputType ContextInputType) String() string {
if int(inputType) >= len(contextInputNames) {
return fmt.Sprintf("unknown input type: %d", inputType)
Expand All @@ -27,12 +18,10 @@ func (inputType ContextInputType) String() string {
return contextInputNames[inputType]
}

var (
contextInputNames = [ContextInputReward + 1]string{"CommitmentInput", "BlockIssuanceCreditInput", "RewardInput"}
)
var contextInputNames = [InputReward + 1]string{"CommitmentInput", "BlockIssuanceCreditInput", "RewardInput"}

// ContextInputs is a slice of ContextInput.
type ContextInputs[T ContextInput] []T
type ContextInputs[T Input] []T

func (in ContextInputs[T]) WorkScore(workScoreStructure *WorkScoreStructure) (WorkScore, error) {
// LengthPrefixType
Expand Down Expand Up @@ -65,18 +54,9 @@ func (in ContextInputs[T]) Size() int {
return sum
}

// ContextInput provides an additional contextual input for transaction validation.
type ContextInput interface {
Sizer
ProcessableObject

// Type returns the type of ContextInput.
Type() ContextInputType
}

// ContextInputsSyntacticalValidationFunc which given the index of an input and the input itself,
// runs syntactical validations and returns an error if any should fail.
type ContextInputsSyntacticalValidationFunc func(index int, input ContextInput) error
type ContextInputsSyntacticalValidationFunc func(index int, input Input) error

// ContextInputsSyntacticalUnique returns a ContextInputsSyntacticalValidationFunc
// which checks that
Expand All @@ -88,7 +68,7 @@ func ContextInputsSyntacticalUnique() ContextInputsSyntacticalValidationFunc {
bicSet := map[string]int{}
rewardSet := map[uint16]int{}

return func(index int, input ContextInput) error {
return func(index int, input Input) error {
switch castInput := input.(type) {
case *BlockIssuanceCreditInput:
accountID := castInput.AccountID
Expand All @@ -111,6 +91,8 @@ func ContextInputsSyntacticalUnique() ContextInputsSyntacticalValidationFunc {
return ierrors.Wrapf(ErrMultipleInputCommitments, "input %d is the second commitment input", index)
}
hasCommitment = true
case *UTXOInput:
// ignore as we are evaluating context inputs only
default:
return ierrors.Wrapf(ErrUnknownContextInputType, "context input %d, tx can only contain CommitmentInputs, BlockIssuanceCreditInputs or RewardInputs", index)
}
Expand Down
4 changes: 2 additions & 2 deletions input_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type RewardInput struct {
Index uint16 `serix:"0,mapKey=index"`
}

func (r *RewardInput) Type() ContextInputType {
return ContextInputReward
func (r *RewardInput) Type() InputType {
return InputReward
}

func (r *RewardInput) Size() int {
Expand Down
12 changes: 6 additions & 6 deletions input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ func TestInputsSyntacticalUnique(t *testing.T) {
func TestContextInputsSyntacticalUnique(t *testing.T) {
tests := []struct {
name string
inputs iotago.ContextInputs[iotago.ContextInput]
inputs iotago.ContextInputs[iotago.Input]
wantErr error
}{
{
name: "ok",
inputs: iotago.ContextInputs[iotago.ContextInput]{
inputs: iotago.ContextInputs[iotago.Input]{
&iotago.CommitmentInput{
CommitmentID: tpkg.Rand40ByteArray(),
},
Expand All @@ -88,7 +88,7 @@ func TestContextInputsSyntacticalUnique(t *testing.T) {
},
{
name: "fail - multiple commitment inputs",
inputs: iotago.ContextInputs[iotago.ContextInput]{
inputs: iotago.ContextInputs[iotago.Input]{
&iotago.CommitmentInput{
CommitmentID: tpkg.Rand40ByteArray(),
},
Expand All @@ -100,7 +100,7 @@ func TestContextInputsSyntacticalUnique(t *testing.T) {
},
{
name: "fail - block issuance credit inputs not unique",
inputs: iotago.ContextInputs[iotago.ContextInput]{
inputs: iotago.ContextInputs[iotago.Input]{
&iotago.BlockIssuanceCreditInput{
AccountID: [32]byte{},
},
Expand All @@ -112,7 +112,7 @@ func TestContextInputsSyntacticalUnique(t *testing.T) {
},
{
name: "fail - reward input not unique",
inputs: iotago.ContextInputs[iotago.ContextInput]{
inputs: iotago.ContextInputs[iotago.Input]{
&iotago.RewardInput{
Index: 1,
},
Expand All @@ -124,7 +124,7 @@ func TestContextInputsSyntacticalUnique(t *testing.T) {
},
{
name: "fail - reward input references index greater than max inputs count",
inputs: iotago.ContextInputs[iotago.ContextInput]{
inputs: iotago.ContextInputs[iotago.Input]{
&iotago.RewardInput{
Index: 1,
},
Expand Down
2 changes: 1 addition & 1 deletion transaction_essence.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TransactionEssenceSelector(txType uint32) (*TransactionEssence, error) {
type InputsCommitment = [InputsCommitmentLength]byte

type (
txEssenceContextInput interface{ ContextInput }
txEssenceContextInput interface{ Input }
txEssenceInput interface{ Input }
TxEssenceOutput interface{ Output }
TxEssencePayload interface{ Payload }
Expand Down

0 comments on commit 5e56919

Please sign in to comment.