Skip to content
Snippets Groups Projects
Unverified Commit 92bc053c authored by Hans Moog's avatar Hans Moog Committed by GitHub
Browse files

Feat: Add additional transaction fields (sequenceNumber + time) (#298)

* Feat: added additional transaction fields (sequenceNumber + time)

* Feat: added getters + adjusted new constructor use
parent 682a9a1b
Branches
Tags
No related merge requests found
...@@ -53,7 +53,7 @@ func (spammer *Spammer) run(tps int, processId int64) { ...@@ -53,7 +53,7 @@ func (spammer *Spammer) run(tps int, processId int64) {
trunkTransactionId, branchTransactionId := spammer.tipSelector.GetTips() trunkTransactionId, branchTransactionId := spammer.tipSelector.GetTips()
spammer.transactionParser.Parse( spammer.transactionParser.Parse(
transaction.New(trunkTransactionId, branchTransactionId, spammingIdentity, data.New([]byte("SPAM"))).Bytes(), transaction.New(trunkTransactionId, branchTransactionId, spammingIdentity, time.Now(), 0, data.New([]byte("SPAM"))).Bytes(),
nil, nil,
) )
...@@ -83,7 +83,7 @@ func (spammer *Spammer) sendBurst(transactions int, processId int64) { ...@@ -83,7 +83,7 @@ func (spammer *Spammer) sendBurst(transactions int, processId int64) {
return return
} }
spamTransaction := transaction.New(previousTransactionId, previousTransactionId, spammingIdentity, data.New([]byte("SPAM"))) spamTransaction := transaction.New(previousTransactionId, previousTransactionId, spammingIdentity, time.Now(), 0, data.New([]byte("SPAM")))
previousTransactionId = spamTransaction.GetId() previousTransactionId = spamTransaction.GetId()
burstBuffer[i] = spamTransaction.Bytes() burstBuffer[i] = spamTransaction.Bytes()
} }
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"runtime" "runtime"
"sync" "sync"
"testing" "testing"
"time"
"github.com/iotaledger/hive.go/async" "github.com/iotaledger/hive.go/async"
...@@ -20,7 +21,7 @@ func BenchmarkVerifyDataTransactions(b *testing.B) { ...@@ -20,7 +21,7 @@ func BenchmarkVerifyDataTransactions(b *testing.B) {
transactions := make([][]byte, b.N) transactions := make([][]byte, b.N)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
tx := transaction.New(transaction.EmptyId, transaction.EmptyId, ed25119.GenerateKeyPair(), data.New([]byte("some data"))) tx := transaction.New(transaction.EmptyId, transaction.EmptyId, ed25119.GenerateKeyPair(), time.Now(), 0, data.New([]byte("some data")))
if marshaledTransaction, err := tx.MarshalBinary(); err != nil { if marshaledTransaction, err := tx.MarshalBinary(); err != nil {
b.Error(err) b.Error(err)
...@@ -50,7 +51,7 @@ func BenchmarkVerifySignature(b *testing.B) { ...@@ -50,7 +51,7 @@ func BenchmarkVerifySignature(b *testing.B) {
transactions := make([]*transaction.Transaction, b.N) transactions := make([]*transaction.Transaction, b.N)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
transactions[i] = transaction.New(transaction.EmptyId, transaction.EmptyId, ed25119.GenerateKeyPair(), data.New([]byte("test"))) transactions[i] = transaction.New(transaction.EmptyId, transaction.EmptyId, ed25119.GenerateKeyPair(), time.Now(), 0, data.New([]byte("test")))
transactions[i].Bytes() transactions[i].Bytes()
} }
......
...@@ -2,6 +2,7 @@ package transaction ...@@ -2,6 +2,7 @@ package transaction
import ( import (
"sync" "sync"
"time"
"github.com/iotaledger/hive.go/stringify" "github.com/iotaledger/hive.go/stringify"
...@@ -24,6 +25,8 @@ type Transaction struct { ...@@ -24,6 +25,8 @@ type Transaction struct {
trunkTransactionId Id trunkTransactionId Id
branchTransactionId Id branchTransactionId Id
issuerPublicKey ed25119.PublicKey issuerPublicKey ed25119.PublicKey
issuingTime time.Time
sequenceNumber uint64
payload payload.Payload payload payload.Payload
bytes []byte bytes []byte
bytesMutex sync.RWMutex bytesMutex sync.RWMutex
...@@ -40,14 +43,17 @@ type Transaction struct { ...@@ -40,14 +43,17 @@ type Transaction struct {
issuerPrivateKey ed25119.PrivateKey issuerPrivateKey ed25119.PrivateKey
} }
// Allows us to "issue" a transaction. // New creates a new transaction with the details provided by the issuer.
func New(trunkTransactionId Id, branchTransactionId Id, issuerKeyPair ed25119.KeyPair, payload payload.Payload) (result *Transaction) { func New(trunkTransactionId Id, branchTransactionId Id, issuerKeyPair ed25119.KeyPair, issuingTime time.Time, sequenceNumber uint64, payload payload.Payload) (result *Transaction) {
return &Transaction{ return &Transaction{
trunkTransactionId: trunkTransactionId, trunkTransactionId: trunkTransactionId,
branchTransactionId: branchTransactionId, branchTransactionId: branchTransactionId,
issuerPublicKey: issuerKeyPair.PublicKey, issuerPublicKey: issuerKeyPair.PublicKey,
issuerPrivateKey: issuerKeyPair.PrivateKey, issuingTime: issuingTime,
sequenceNumber: sequenceNumber,
payload: payload, payload: payload,
issuerPrivateKey: issuerKeyPair.PrivateKey,
} }
} }
...@@ -88,6 +94,12 @@ func FromBytes(bytes []byte, optionalTargetObject ...*Transaction) (result *Tran ...@@ -88,6 +94,12 @@ func FromBytes(bytes []byte, optionalTargetObject ...*Transaction) (result *Tran
if result.issuerPublicKey, err = ed25119.ParsePublicKey(marshalUtil); err != nil { if result.issuerPublicKey, err = ed25119.ParsePublicKey(marshalUtil); err != nil {
return return
} }
if result.issuingTime, err = marshalUtil.ReadTime(); err != nil {
return
}
if result.sequenceNumber, err = marshalUtil.ReadUint64(); err != nil {
return
}
if result.payload, err = payload.Parse(marshalUtil); err != nil { if result.payload, err = payload.Parse(marshalUtil); err != nil {
return return
} }
...@@ -146,6 +158,16 @@ func (transaction *Transaction) GetBranchTransactionId() Id { ...@@ -146,6 +158,16 @@ func (transaction *Transaction) GetBranchTransactionId() Id {
return transaction.branchTransactionId return transaction.branchTransactionId
} }
// IssuingTime returns the time when the transaction was created.
func (transaction *Transaction) IssuingTime() time.Time {
return transaction.issuingTime
}
// SequenceNumber returns the sequence number of this transaction.
func (transaction *Transaction) SequenceNumber() uint64 {
return transaction.sequenceNumber
}
func (transaction *Transaction) GetPayload() payload.Payload { func (transaction *Transaction) GetPayload() payload.Payload {
return transaction.payload return transaction.payload
} }
...@@ -218,6 +240,8 @@ func (transaction *Transaction) Bytes() []byte { ...@@ -218,6 +240,8 @@ func (transaction *Transaction) Bytes() []byte {
marshalUtil.WriteBytes(transaction.trunkTransactionId.Bytes()) marshalUtil.WriteBytes(transaction.trunkTransactionId.Bytes())
marshalUtil.WriteBytes(transaction.branchTransactionId.Bytes()) marshalUtil.WriteBytes(transaction.branchTransactionId.Bytes())
marshalUtil.WriteBytes(transaction.issuerPublicKey.Bytes()) marshalUtil.WriteBytes(transaction.issuerPublicKey.Bytes())
marshalUtil.WriteTime(transaction.issuingTime)
marshalUtil.WriteUint64(transaction.sequenceNumber)
marshalUtil.WriteBytes(transaction.payload.Bytes()) marshalUtil.WriteBytes(transaction.payload.Bytes())
marshalUtil.WriteBytes(transaction.issuerPrivateKey.Sign(marshalUtil.Bytes()).Bytes()) marshalUtil.WriteBytes(transaction.issuerPrivateKey.Sign(marshalUtil.Bytes()).Bytes())
......
...@@ -36,7 +36,7 @@ func BenchmarkTangle_AttachTransaction(b *testing.B) { ...@@ -36,7 +36,7 @@ func BenchmarkTangle_AttachTransaction(b *testing.B) {
transactionBytes := make([]*transaction.Transaction, b.N) transactionBytes := make([]*transaction.Transaction, b.N)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
transactionBytes[i] = transaction.New(transaction.EmptyId, transaction.EmptyId, testIdentity, data.New([]byte("some data"))) transactionBytes[i] = transaction.New(transaction.EmptyId, transaction.EmptyId, testIdentity, time.Now(), 0, data.New([]byte("some data")))
transactionBytes[i].Bytes() transactionBytes[i].Bytes()
} }
...@@ -91,8 +91,8 @@ func TestTangle_AttachTransaction(t *testing.T) { ...@@ -91,8 +91,8 @@ func TestTangle_AttachTransaction(t *testing.T) {
fmt.Println("REMOVED:", transactionId) fmt.Println("REMOVED:", transactionId)
})) }))
newTransaction1 := transaction.New(transaction.EmptyId, transaction.EmptyId, ed25119.GenerateKeyPair(), data.New([]byte("some data"))) newTransaction1 := transaction.New(transaction.EmptyId, transaction.EmptyId, ed25119.GenerateKeyPair(), time.Now(), 0, data.New([]byte("some data")))
newTransaction2 := transaction.New(newTransaction1.GetId(), newTransaction1.GetId(), ed25119.GenerateKeyPair(), data.New([]byte("some other data"))) newTransaction2 := transaction.New(newTransaction1.GetId(), newTransaction1.GetId(), ed25119.GenerateKeyPair(), time.Now(), 0, data.New([]byte("some other data")))
tangle.AttachTransaction(newTransaction2) tangle.AttachTransaction(newTransaction2)
......
...@@ -2,6 +2,7 @@ package tipselector ...@@ -2,6 +2,7 @@ package tipselector
import ( import (
"testing" "testing"
"time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
...@@ -20,7 +21,7 @@ func Test(t *testing.T) { ...@@ -20,7 +21,7 @@ func Test(t *testing.T) {
assert.Equal(t, transaction.EmptyId, branch1) assert.Equal(t, transaction.EmptyId, branch1)
// create a transaction and attach it // create a transaction and attach it
transaction1 := transaction.New(trunk1, branch1, ed25119.GenerateKeyPair(), data.New([]byte("testtransaction"))) transaction1 := transaction.New(trunk1, branch1, ed25119.GenerateKeyPair(), time.Now(), 0, data.New([]byte("testtransaction")))
tipSelector.AddTip(transaction1) tipSelector.AddTip(transaction1)
// check if the tip shows up in the tip count // check if the tip shows up in the tip count
...@@ -32,7 +33,7 @@ func Test(t *testing.T) { ...@@ -32,7 +33,7 @@ func Test(t *testing.T) {
assert.Equal(t, transaction1.GetId(), branch2) assert.Equal(t, transaction1.GetId(), branch2)
// create a 2nd transaction and attach it // create a 2nd transaction and attach it
transaction2 := transaction.New(transaction.EmptyId, transaction.EmptyId, ed25119.GenerateKeyPair(), data.New([]byte("testtransaction"))) transaction2 := transaction.New(transaction.EmptyId, transaction.EmptyId, ed25119.GenerateKeyPair(), time.Now(), 0, data.New([]byte("testtransaction")))
tipSelector.AddTip(transaction2) tipSelector.AddTip(transaction2)
// check if the tip shows up in the tip count // check if the tip shows up in the tip count
...@@ -40,7 +41,7 @@ func Test(t *testing.T) { ...@@ -40,7 +41,7 @@ func Test(t *testing.T) {
// attach a transaction to our two tips // attach a transaction to our two tips
trunk3, branch3 := tipSelector.GetTips() trunk3, branch3 := tipSelector.GetTips()
transaction3 := transaction.New(trunk3, branch3, ed25119.GenerateKeyPair(), data.New([]byte("testtransaction"))) transaction3 := transaction.New(trunk3, branch3, ed25119.GenerateKeyPair(), time.Now(), 0, data.New([]byte("testtransaction")))
tipSelector.AddTip(transaction3) tipSelector.AddTip(transaction3)
// check if the tip shows replaces the current tips // check if the tip shows replaces the current tips
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"testing" "testing"
"time"
"github.com/iotaledger/hive.go/events" "github.com/iotaledger/hive.go/events"
...@@ -13,7 +14,7 @@ import ( ...@@ -13,7 +14,7 @@ import (
) )
func BenchmarkTransactionParser_ParseBytesSame(b *testing.B) { func BenchmarkTransactionParser_ParseBytesSame(b *testing.B) {
txBytes := transaction.New(transaction.EmptyId, transaction.EmptyId, ed25119.GenerateKeyPair(), data.New([]byte("Test"))).Bytes() txBytes := transaction.New(transaction.EmptyId, transaction.EmptyId, ed25119.GenerateKeyPair(), time.Now(), 0, data.New([]byte("Test"))).Bytes()
txParser := New() txParser := New()
b.ResetTimer() b.ResetTimer()
...@@ -28,7 +29,7 @@ func BenchmarkTransactionParser_ParseBytesSame(b *testing.B) { ...@@ -28,7 +29,7 @@ func BenchmarkTransactionParser_ParseBytesSame(b *testing.B) {
func BenchmarkTransactionParser_ParseBytesDifferent(b *testing.B) { func BenchmarkTransactionParser_ParseBytesDifferent(b *testing.B) {
transactionBytes := make([][]byte, b.N) transactionBytes := make([][]byte, b.N)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
transactionBytes[i] = transaction.New(transaction.EmptyId, transaction.EmptyId, ed25119.GenerateKeyPair(), data.New([]byte("Test"+strconv.Itoa(i)))).Bytes() transactionBytes[i] = transaction.New(transaction.EmptyId, transaction.EmptyId, ed25119.GenerateKeyPair(), time.Now(), 0, data.New([]byte("Test"+strconv.Itoa(i)))).Bytes()
} }
txParser := New() txParser := New()
...@@ -43,7 +44,7 @@ func BenchmarkTransactionParser_ParseBytesDifferent(b *testing.B) { ...@@ -43,7 +44,7 @@ func BenchmarkTransactionParser_ParseBytesDifferent(b *testing.B) {
} }
func TestTransactionParser_ParseTransaction(t *testing.T) { func TestTransactionParser_ParseTransaction(t *testing.T) {
tx := transaction.New(transaction.EmptyId, transaction.EmptyId, ed25119.GenerateKeyPair(), data.New([]byte("Test"))) tx := transaction.New(transaction.EmptyId, transaction.EmptyId, ed25119.GenerateKeyPair(), time.Now(), 0, data.New([]byte("Test")))
txParser := New() txParser := New()
txParser.Parse(tx.Bytes(), nil) txParser.Parse(tx.Bytes(), nil)
......
...@@ -3,6 +3,7 @@ package test ...@@ -3,6 +3,7 @@ package test
import ( import (
"fmt" "fmt"
"testing" "testing"
"time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
...@@ -61,6 +62,12 @@ func ExamplePayload() { ...@@ -61,6 +62,12 @@ func ExamplePayload() {
// issuer of the transaction (signs automatically) // issuer of the transaction (signs automatically)
ed25119.GenerateKeyPair(), ed25119.GenerateKeyPair(),
// the time when the transaction was created
time.Now(),
// the ever increasing sequence number of this transaction
0,
// payload // payload
valuePayload, valuePayload,
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment