Skip to content

Commit

Permalink
Merge pull request #380 from iotaledger/feat/segwit-renames
Browse files Browse the repository at this point in the history
Restructure Transaction to prepare for SegWit
  • Loading branch information
piotrm50 authored Sep 28, 2023
2 parents 5a0850d + 2208fd8 commit 6376c87
Show file tree
Hide file tree
Showing 32 changed files with 184 additions and 187 deletions.
12 changes: 6 additions & 6 deletions components/dashboard/explorer_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func createExplorerBlock(block *model.Block, cachedBlock *blocks.Block, metadata
return iotago.PayloadType(0)
}(),
Payload: func() json.RawMessage {
if isBasic && basicBlock.Payload != nil && basicBlock.Payload.PayloadType() == iotago.PayloadTransaction {
tx, _ := basicBlock.Payload.(*iotago.Transaction)
if isBasic && basicBlock.Payload != nil && basicBlock.Payload.PayloadType() == iotago.PayloadSignedTransaction {
tx, _ := basicBlock.Payload.(*iotago.SignedTransaction)
txResponse := NewTransaction(tx)
bytes, _ := json.Marshal(txResponse)

Expand All @@ -143,8 +143,8 @@ func createExplorerBlock(block *model.Block, cachedBlock *blocks.Block, metadata
return payloadJSON
}(),
TransactionID: func() string {
if isBasic && basicBlock.Payload != nil && basicBlock.Payload.PayloadType() == iotago.PayloadTransaction {
tx, _ := basicBlock.Payload.(*iotago.Transaction)
if isBasic && basicBlock.Payload != nil && basicBlock.Payload.PayloadType() == iotago.PayloadSignedTransaction {
tx, _ := basicBlock.Payload.(*iotago.SignedTransaction)
id, _ := tx.ID()

return id.ToHex()
Expand Down Expand Up @@ -211,9 +211,9 @@ func getTransaction(c echo.Context) error {
return ierrors.Errorf("block not found: %s", output.BlockID().ToHex())
}

iotaTX, isTX := block.Transaction()
iotaTX, isTX := block.SignedTransaction()
if !isTX {
return ierrors.Errorf("payload is not a transaction: %s", output.BlockID().ToHex())
return ierrors.Errorf("payload is not a signed transaction: %s", output.BlockID().ToHex())
}

return httpserver.JSONResponse(c, http.StatusOK, NewTransaction(iotaTX))
Expand Down
36 changes: 18 additions & 18 deletions components/dashboard/jsonresponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func NewOutputID(outputID iotago.OutputID) *OutputID {

// region Transaction //////////////////////////////////////////////////////////////////////////////////////////////////

// Transaction represents the JSON model of a ledgerstate.Transaction.
// Transaction represents the JSON model of a iotago.SignedTransaction.
type Transaction struct {
TransactionID string `json:"txId"`
NetworkID iotago.NetworkID `json:"networkId"`
Expand All @@ -128,20 +128,20 @@ type Transaction struct {
Payload []byte `json:"payload"`
}

// NewTransaction returns a Transaction from the given ledgerstate.Transaction.
func NewTransaction(iotaTx *iotago.Transaction) *Transaction {
txID, err := iotaTx.ID()
// NewTransaction returns a Transaction from the given iotago.SignedTransaction.
func NewTransaction(signedTx *iotago.SignedTransaction) *Transaction {
txID, err := signedTx.ID()
if err != nil {
return nil
}

inputs := make([]*Input, len(iotaTx.Essence.Inputs))
for i, input := range iotaTx.Essence.Inputs {
inputs := make([]*Input, len(signedTx.Transaction.Inputs))
for i, input := range signedTx.Transaction.Inputs {
inputs[i] = NewInput(input)
}

outputs := make([]*Output, len(iotaTx.Essence.Outputs))
for i, output := range iotaTx.Essence.Outputs {
outputs := make([]*Output, len(signedTx.Transaction.Outputs))
for i, output := range signedTx.Transaction.Outputs {
outputs[i] = NewOutput(output)
outputs[i].OutputID = &OutputID{
Hex: iotago.OutputIDFromTransactionIDAndIndex(txID, uint16(i)).ToHex(),
Expand All @@ -150,19 +150,19 @@ func NewTransaction(iotaTx *iotago.Transaction) *Transaction {
}
}

unlockBlocks := make([]*UnlockBlock, len(iotaTx.Unlocks))
for i, unlockBlock := range iotaTx.Unlocks {
unlockBlocks := make([]*UnlockBlock, len(signedTx.Unlocks))
for i, unlockBlock := range signedTx.Unlocks {
unlockBlocks[i] = NewUnlockBlock(unlockBlock)
}

dataPayload := make([]byte, 0)
if iotaTx.Essence.Payload != nil {
dataPayload, _ = deps.Protocol.CurrentAPI().Encode(iotaTx.Essence.Payload)
if signedTx.Transaction.Payload != nil {
dataPayload, _ = deps.Protocol.CurrentAPI().Encode(signedTx.Transaction.Payload)
}

return &Transaction{
NetworkID: iotaTx.Essence.NetworkID,
CreationSlot: iotaTx.Essence.CreationSlot,
NetworkID: signedTx.Transaction.NetworkID,
CreationSlot: signedTx.Transaction.CreationSlot,
Inputs: inputs,
Outputs: outputs,
Unlocks: unlockBlocks,
Expand All @@ -174,15 +174,15 @@ func NewTransaction(iotaTx *iotago.Transaction) *Transaction {

// region Input ////////////////////////////////////////////////////////////////////////////////////////////////////////

// Input represents the JSON model of a ledgerstate.Input.
// Input represents the JSON model of a iotago.Input.
type Input struct {
Type string `json:"type"`
ReferencedOutputID *OutputID `json:"referencedOutputID,omitempty"`
// the referenced output object, omit if not specified
Output *Output `json:"output,omitempty"`
}

// NewInput returns an Input from the given ledgerstate.Input.
// NewInput returns an Input from the given iotago.Input.
func NewInput(input iotago.Input) *Input {
utxoInput, isUtxoInput := input.(*iotago.UTXOInput)
if !isUtxoInput {
Expand All @@ -199,14 +199,14 @@ func NewInput(input iotago.Input) *Input {

// region UnlockBlock //////////////////////////////////////////////////////////////////////////////////////////////////

// UnlockBlock represents the JSON model of a ledgerstate.UnlockBlock.
// UnlockBlock represents the JSON model of a iotago.UnlockBlock.
type UnlockBlock struct {
Type string `json:"type"`
SignatureType iotago.SignatureType `json:"signatureType,omitempty"`
Signature json.RawMessage `json:"signature,omitempty"`
}

// NewUnlockBlock returns an UnlockBlock from the given ledgerstate.UnlockBlock.
// NewUnlockBlock returns an UnlockBlock from the given UnlockBlock.
func NewUnlockBlock(unlockBlock iotago.Unlock) *UnlockBlock {
result := &UnlockBlock{
Type: unlockBlock.Type().String(),
Expand Down
4 changes: 2 additions & 2 deletions components/dashboard/visualizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type tipinfo struct {

func sendVertex(blk *blocks.Block, confirmed bool) {
modelBlk, _ := model.BlockFromBlock(blk.ProtocolBlock())
tx, isTx := modelBlk.Transaction()
tx, isTx := modelBlk.SignedTransaction()

broadcastWsBlock(&wsblk{MsgTypeVertex, &vertex{
ID: blk.ID().ToHex(),
Expand Down Expand Up @@ -85,7 +85,7 @@ func runVisualizer(component *app.Component) {
deps.Protocol.Events.Engine.BlockDAG.BlockAttached.Hook(func(block *blocks.Block) {
sendVertex(block, false)

tx, hasTx := block.Transaction()
tx, hasTx := block.SignedTransaction()
if hasTx {
txMetadata, exists := deps.Protocol.MainEngineInstance().Ledger.MemPool().TransactionMetadata(lo.PanicOnErr(tx.ID()))
if exists {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/iotaledger/hive.go/stringify v0.0.0-20230926122307-d671b36a4a65
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230927140518-622f63be6182
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230927140257-bfa0bb0af2bd
github.com/iotaledger/iota.go/v4 v4.0.0-20230928103002-093e56bf1b19
github.com/iotaledger/iota.go/v4 v4.0.0-20230928135522-335156827793
github.com/labstack/echo/v4 v4.11.1
github.com/labstack/gommon v0.4.0
github.com/libp2p/go-libp2p v0.30.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230927140518-622f63be6182 h1:lQikt
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230927140518-622f63be6182/go.mod h1:q24QEsS887ZWJVX76w2kwSgC84KS7wIKOy1otuqZ2ZM=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230927140257-bfa0bb0af2bd h1:nFG3Zq/zFA4KhBYFX2IezX1C74zfE0DqCt0LrgTa9Ig=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230927140257-bfa0bb0af2bd/go.mod h1:c5778OnWpLq108YE+Eb2m8Ri/t/4ydV0TvI/Sy5YivQ=
github.com/iotaledger/iota.go/v4 v4.0.0-20230928103002-093e56bf1b19 h1:4vN+jZJECvlfP0Bam8tonH9ndFBJoi8G0nMb4m1kpKk=
github.com/iotaledger/iota.go/v4 v4.0.0-20230928103002-093e56bf1b19/go.mod h1:wR9xBbsofns9hFyRHFZ2bDYIb861qsfmQPVMBKcPvDo=
github.com/iotaledger/iota.go/v4 v4.0.0-20230928135522-335156827793 h1:YyhI+h6r2PkFZE7PorDhlT4EwAOvVuW2NdNF80FshIQ=
github.com/iotaledger/iota.go/v4 v4.0.0-20230928135522-335156827793/go.mod h1:wR9xBbsofns9hFyRHFZ2bDYIb861qsfmQPVMBKcPvDo=
github.com/ipfs/boxo v0.10.0 h1:tdDAxq8jrsbRkYoF+5Rcqyeb91hgWe2hp7iLu7ORZLY=
github.com/ipfs/boxo v0.10.0/go.mod h1:Fg+BnfxZ0RPzR0nOodzdIq3A7KgoWAOWsEIImrIQdBM=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
Expand Down
6 changes: 3 additions & 3 deletions pkg/blockfactory/blockissuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ func (i *BlockIssuer) AttachBlock(ctx context.Context, iotaBlock *iotago.Protoco
switch innerBlock := iotaBlock.Block.(type) {
case *iotago.BasicBlock:
switch payload := innerBlock.Payload.(type) {
case *iotago.Transaction:
if payload.Essence.NetworkID != protoParams.NetworkID() {
return iotago.EmptyBlockID(), ierrors.Wrapf(ErrBlockAttacherInvalidBlock, "invalid payload, error: wrong networkID: %d", payload.Essence.NetworkID)
case *iotago.SignedTransaction:
if payload.Transaction.NetworkID != protoParams.NetworkID() {
return iotago.EmptyBlockID(), ierrors.Wrapf(ErrBlockAttacherInvalidBlock, "invalid payload, error: wrong networkID: %d", payload.Transaction.NetworkID)
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/model/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ func (blk *Block) Payload() iotago.Payload {
return basicBlock.Payload
}

func (blk *Block) Transaction() (tx *iotago.Transaction, isTransaction bool) {
func (blk *Block) SignedTransaction() (tx *iotago.SignedTransaction, isTransaction bool) {
payload := blk.Payload()
if payload == nil {
return nil, false
}

tx, isTransaction = payload.(*iotago.Transaction)
tx, isTransaction = payload.(*iotago.SignedTransaction)

return tx, isTransaction
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocol/engine/blocks/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ func (b *Block) Payload() iotago.Payload {
return b.modelBlock.Payload()
}

func (b *Block) Transaction() (tx *iotago.Transaction, hasTransaction bool) {
func (b *Block) SignedTransaction() (tx *iotago.SignedTransaction, hasTransaction bool) {
if b.modelBlock == nil {
return nil, false
}

return b.modelBlock.Transaction()
return b.modelBlock.SignedTransaction()
}

func (b *Block) BasicBlock() (basicBlock *iotago.BasicBlock, isBasicBlock bool) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol/engine/booker/inmemorybooker/booker.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (b *Booker) inheritConflicts(block *blocks.Block) (conflictIDs ds.Set[iotag
case iotago.ShallowLikeParentType:
// Check whether the parent contains a conflicting TX,
// otherwise reference is invalid and the block should be marked as invalid as well.
if tx, hasTx := parentBlock.Transaction(); !hasTx || !parentBlock.PayloadConflictIDs().Has(lo.PanicOnErr(tx.ID())) {
if tx, hasTx := parentBlock.SignedTransaction(); !hasTx || !parentBlock.PayloadConflictIDs().Has(lo.PanicOnErr(tx.ID())) {
return nil, ierrors.Wrapf(err, "shallow like parent %s does not contain a conflicting transaction", parent.ID.String())
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/protocol/engine/ledger/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (l *Ledger) OnTransactionAttached(handler func(transaction mempool.Transact
}

func (l *Ledger) AttachTransaction(block *blocks.Block) (transactionMetadata mempool.TransactionMetadata, containsTransaction bool) {
if transaction, hasTransaction := block.Transaction(); hasTransaction {
if transaction, hasTransaction := block.SignedTransaction(); hasTransaction {
transactionMetadata, err := l.memPool.AttachTransaction(transaction, block.ID())
if err != nil {
l.retainTransactionFailure(block.ID(), err)
Expand Down Expand Up @@ -212,7 +212,7 @@ func (l *Ledger) AddGenesisUnspentOutput(unspentOutput *utxoledger.Output) error
func (l *Ledger) TrackBlock(block *blocks.Block) {
l.accountsLedger.TrackBlock(block)

if _, hasTransaction := block.Transaction(); hasTransaction {
if _, hasTransaction := block.SignedTransaction(); hasTransaction {
l.memPool.MarkAttachmentIncluded(block.ID())
}

Expand Down Expand Up @@ -584,7 +584,7 @@ func (l *Ledger) processStateDiffTransactions(stateDiff mempool.StateDiff) (spen
accountDiffs = make(map[iotago.AccountID]*model.AccountDiff)

stateDiff.ExecutedTransactions().ForEach(func(txID iotago.TransactionID, txWithMeta mempool.TransactionMetadata) bool {
tx, ok := txWithMeta.Transaction().(*iotago.Transaction)
tx, ok := txWithMeta.Transaction().(*iotago.SignedTransaction)
if !ok {
err = iotago.ErrTxTypeInvalid
return false
Expand Down Expand Up @@ -619,7 +619,7 @@ func (l *Ledger) processStateDiffTransactions(stateDiff mempool.StateDiff) (spen

// process allotments
{
for _, allotment := range tx.Essence.Allotments {
for _, allotment := range tx.Transaction.Allotments {
// in case it didn't exist, allotments won't change the outputID of the Account,
// so the diff defaults to empty new and previous outputIDs
accountDiff := getAccountDiff(accountDiffs, allotment.AccountID)
Expand Down
11 changes: 4 additions & 7 deletions pkg/protocol/engine/ledger/ledger/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ import (
)

func (l *Ledger) executeStardustVM(_ context.Context, stateTransition mempool.Transaction, inputStates []mempool.OutputState, timeReference mempool.ContextState) ([]mempool.OutputState, error) {
tx, ok := stateTransition.(*iotago.Transaction)
tx, ok := stateTransition.(*iotago.SignedTransaction)
if !ok {
return nil, iotago.ErrTxTypeInvalid
}

inputSet := iotagovm.InputSet{}
for _, inputState := range inputStates {
inputSet[inputState.OutputID()] = iotagovm.OutputWithCreationSlot{
Output: inputState.Output(),
CreationSlot: inputState.SlotCreated(),
}
for _, input := range inputStates {
inputSet[input.OutputID()] = input.Output()
}
resolvedInputs := iotagovm.ResolvedInputs{
InputSet: inputSet,
Expand Down Expand Up @@ -122,7 +119,7 @@ func (l *Ledger) executeStardustVM(_ context.Context, stateTransition mempool.Tr
created = append(created, &ExecutionOutput{
outputID: outputID,
output: output,
creationSlot: tx.Essence.CreationSlot,
creationSlot: tx.Transaction.CreationSlot,
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol/engine/utxoledger/database_prefixes.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const (
1 byte + 34 bytes
Value:
TargetTransactionID (iotago.TransactionID) + TransactionAcceptedSlotIndex (iotago.SlotIndex) + TransactionCreationSlot (time.Time)
TargetTransactionID (iotago.SignedTransactionID) + TransactionAcceptedSlotIndex (iotago.SlotIndex) + TransactionCreationSlot (time.Time)
32 bytes + 8 bytes + 8 bytes
Unspent Output:
Expand Down
6 changes: 3 additions & 3 deletions pkg/protocol/engine/utxoledger/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (o *Output) SlotBooked() iotago.SlotIndex {
}

func (o *Output) SlotCreated() iotago.SlotIndex {
return o.outputID.CreationSlotIndex()
return o.outputID.CreationSlot()
}

func (o *Output) OutputType() iotago.OutputType {
Expand Down Expand Up @@ -142,10 +142,10 @@ func NewOutput(apiProvider iotago.APIProvider, blockID iotago.BlockID, slotBooke
}

var output iotago.Output
if len(transaction.Essence.Outputs) <= int(index) {
if len(transaction.Outputs) <= int(index) {
return nil, ierrors.New("output not found")
}
output = transaction.Essence.Outputs[int(index)]
output = transaction.Outputs[int(index)]
outputID := iotago.OutputIDFromTransactionIDAndIndex(txID, index)

return CreateOutput(apiProvider, outputID, blockID, slotBooked, output), nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocol/engine/utxoledger/tpkg/equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func EqualOutput(t *testing.T, expected *utxoledger.Output, actual *utxoledger.O
case iotago.TransIndepIdentOutput:
expectedIdent = output.Ident()
case iotago.TransDepIdentOutput:
expectedIdent = output.Chain().ToAddress()
expectedIdent = output.ChainID().ToAddress()
default:
require.Fail(t, "unsupported output type")
}
Expand All @@ -34,7 +34,7 @@ func EqualOutput(t *testing.T, expected *utxoledger.Output, actual *utxoledger.O
case iotago.TransIndepIdentOutput:
actualIdent = output.Ident()
case iotago.TransDepIdentOutput:
actualIdent = output.Chain().ToAddress()
actualIdent = output.ChainID().ToAddress()
default:
require.Fail(t, "unsupported output type")
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/tests/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func Test_TransitionAccount(t *testing.T) {
PreviousUpdatedTime: 0,
PreviousExpirySlot: 1,
NewExpirySlot: 1,
NewOutputID: iotago.OutputIDFromTransactionIDAndIndex(lo.PanicOnErr(ts.TransactionFramework.Transaction("TX1").ID()), 0),
NewOutputID: iotago.OutputIDFromTransactionIDAndIndex(lo.PanicOnErr(ts.TransactionFramework.SignedTransaction("TX1").ID()), 0),
PreviousOutputID: genesisAccount.OutputID(),
BlockIssuerKeysRemoved: iotago.NewBlockIssuerKeys(),
BlockIssuerKeysAdded: iotago.NewBlockIssuerKeys(newGenesisOutputKey),
Expand All @@ -111,7 +111,7 @@ func Test_TransitionAccount(t *testing.T) {
ts.AssertAccountData(&accounts.AccountData{
ID: genesisAccountOutput.AccountID,
Credits: accounts.NewBlockIssuanceCredits(iotago.BlockIssuanceCredits(123), 0),
OutputID: iotago.OutputIDFromTransactionIDAndIndex(lo.PanicOnErr(ts.TransactionFramework.Transaction("TX1").ID()), 0),
OutputID: iotago.OutputIDFromTransactionIDAndIndex(lo.PanicOnErr(ts.TransactionFramework.SignedTransaction("TX1").ID()), 0),
BlockIssuerKeys: iotago.NewBlockIssuerKeys(oldGenesisOutputKey, newGenesisOutputKey),
ExpirySlot: 1,
}, ts.Nodes()...)
Expand Down Expand Up @@ -161,7 +161,7 @@ func Test_TransitionAccount(t *testing.T) {
NewExpirySlot: 0,
PreviousExpirySlot: 1,
NewOutputID: iotago.EmptyOutputID,
PreviousOutputID: iotago.OutputIDFromTransactionIDAndIndex(lo.PanicOnErr(ts.TransactionFramework.Transaction("TX1").ID()), 0),
PreviousOutputID: iotago.OutputIDFromTransactionIDAndIndex(lo.PanicOnErr(ts.TransactionFramework.SignedTransaction("TX1").ID()), 0),
BlockIssuerKeysAdded: iotago.NewBlockIssuerKeys(),
BlockIssuerKeysRemoved: iotago.NewBlockIssuerKeys(oldGenesisOutputKey, newGenesisOutputKey),
ValidatorStakeChange: 0,
Expand Down
Loading

0 comments on commit 6376c87

Please sign in to comment.