From fff0a594fec2631252401eddcb55b78eefb42e8d Mon Sep 17 00:00:00 2001 From: Mustafa Al-Bassam Date: Thu, 25 Apr 2019 00:02:46 +0100 Subject: [PATCH] add measurements --- app_registrar.go | 15 +- cmd/lazyledger_measurements_2/main.go | 2 +- cmd/lazyledger_measurements_4/.gitignore | 1 + cmd/lazyledger_measurements_4/main.go | 150 +++++++++++++++++++ cmd/lazyledger_measurements_5/.gitignore | 1 + cmd/lazyledger_measurements_5/main.go | 178 +++++++++++++++++++++++ 6 files changed, 343 insertions(+), 4 deletions(-) create mode 100644 cmd/lazyledger_measurements_4/.gitignore create mode 100644 cmd/lazyledger_measurements_4/main.go create mode 100644 cmd/lazyledger_measurements_5/.gitignore create mode 100644 cmd/lazyledger_measurements_5/main.go diff --git a/app_registrar.go b/app_registrar.go index 3d6ad99..125ddd3 100644 --- a/app_registrar.go +++ b/app_registrar.go @@ -12,6 +12,7 @@ type Registrar struct { state MapStore currency *Currency owner []byte + namespace [namespaceSize]byte } func NewRegistrar(state MapStore, currency *Currency, owner []byte) Application { @@ -56,9 +57,17 @@ func (app *Registrar) ProcessMessage(message Message) { } func (app *Registrar) Namespace() [namespaceSize]byte { - var namespace [namespaceSize]byte - copy(namespace[:], append([]byte("reggie"), app.owner[:namespaceSize-6]...)) - return namespace + var empty [namespaceSize]byte + if app.namespace == empty { + var namespace [namespaceSize]byte + copy(namespace[:], []byte("reggie")) + return namespace + } + return app.namespace +} + +func (app *Registrar) SetNamespace(namespace [namespaceSize]byte) { + app.namespace = namespace } func (app *Registrar) SetBlockHead(hash []byte) { diff --git a/cmd/lazyledger_measurements_2/main.go b/cmd/lazyledger_measurements_2/main.go index b927192..bec61f9 100644 --- a/cmd/lazyledger_measurements_2/main.go +++ b/cmd/lazyledger_measurements_2/main.go @@ -49,7 +49,7 @@ func main() { } } - fmt.Println(txes, sbBandwidthMax, pbBandwidthMax) + fmt.Println(txes*txSize, sbBandwidthMax, pbBandwidthMax) } } diff --git a/cmd/lazyledger_measurements_4/.gitignore b/cmd/lazyledger_measurements_4/.gitignore new file mode 100644 index 0000000..b8a07b7 --- /dev/null +++ b/cmd/lazyledger_measurements_4/.gitignore @@ -0,0 +1 @@ +lazyledger_measurements_4 diff --git a/cmd/lazyledger_measurements_4/main.go b/cmd/lazyledger_measurements_4/main.go new file mode 100644 index 0000000..be5ef74 --- /dev/null +++ b/cmd/lazyledger_measurements_4/main.go @@ -0,0 +1,150 @@ +package main + +import ( + "crypto/rand" + "encoding/binary" + "fmt" + + "github.com/musalbas/lazyledger-prototype" + "github.com/libp2p/go-libp2p-crypto" +) + +const namespaceSize = 8 + +func main() { + registrarTxes := 10 + txAmounts := []int{128, 256, 384, 512, 640, 768, 896, 1024} + txSize := 256 + for _, txes := range txAmounts { + sbBandwidthMax := 0 + pbBandwidthMax := 0 + + for i := 0; i < 10; i++ { + sb, cns, rns := generateSimpleBlock(registrarTxes, txes, txSize) + sbBandwidth := 0 + _, _, proofs1, messages1, _ := sb.ApplicationProof(cns) + for _, msg := range *messages1 { + sbBandwidth += len(msg.Marshal()) + } + for _, hash := range proofs1 { + sbBandwidth += len(hash) + } + if sbBandwidth > sbBandwidthMax { + sbBandwidthMax = sbBandwidth + } + _, _, proofs1, _, hashes1 := sb.ApplicationProof(rns) + for _, hash := range hashes1 { + sbBandwidth += len(hash) + } + for _, hash := range proofs1 { + sbBandwidth += len(hash) + } + if sbBandwidth > sbBandwidthMax { + sbBandwidthMax = sbBandwidth + } + + pb, cns, rns := generateProbabilisticBlock(registrarTxes, txes, txSize) + _, _, proofs2, messages2, _ := pb.ApplicationProof(cns) + pbBandwidth := 0 + for _, msg := range *messages2 { + pbBandwidth += len(msg.Marshal()) + } + for _, proof := range proofs2 { + for _, hash := range proof { + pbBandwidth += len(hash) + } + } + _, _, proofs2, _, hashes2 := pb.ApplicationProof(rns) + for _, hash := range hashes2 { + pbBandwidth += len(hash) + } + for _, proof := range proofs2 { + for _, hash := range proof { + pbBandwidth += len(hash) + } + } + if pbBandwidth > pbBandwidthMax { + pbBandwidthMax = pbBandwidth + } + } + + fmt.Println(txes, sbBandwidthMax, pbBandwidthMax) + } +} + +func generateSimpleBlock(registrarTxes int, otherTxes, txSize int) (*lazyledger.SimpleBlock, [namespaceSize]byte, [namespaceSize]byte) { + txSize -= namespaceSize + + bs := lazyledger.NewSimpleBlockStore() + b := lazyledger.NewBlockchain(bs) + + sb := lazyledger.NewSimpleBlock([]byte{0}) + + ms1 := lazyledger.NewSimpleMap() + currencyApp := lazyledger.NewCurrency(ms1, b) + b.RegisterApplication(¤cyApp) + + privA, pubA, _ := crypto.GenerateSecp256k1Key(rand.Reader) + _, pubB, _ := crypto.GenerateSecp256k1Key(rand.Reader) + pubABytes, _ := pubA.Bytes() + pubBBytes, _ := pubB.Bytes() + pubABalanceBytes := make([]byte, binary.MaxVarintLen64) + binary.BigEndian.PutUint64(pubABalanceBytes, 1000000) + ms1.Put(pubABytes, pubABalanceBytes) + + ms2 := lazyledger.NewSimpleMap() + registrarApp := lazyledger.NewRegistrar(ms2, currencyApp.(*lazyledger.Currency), pubBBytes) + b.RegisterApplication(®istrarApp) + + for i := 0; i < registrarTxes; i++ { + sb.AddMessage(currencyApp.(*lazyledger.Currency).GenerateTransaction(privA, pubB, 1, nil)) + } + + ms3 := lazyledger.NewSimpleMap() + registrarApp2 := lazyledger.NewRegistrar(ms3, currencyApp.(*lazyledger.Currency), pubBBytes) + b.RegisterApplication(®istrarApp2) + + for i := 0; i < otherTxes; i++ { + sb.AddMessage(currencyApp.(*lazyledger.Currency).GenerateTransaction(privA, pubB, 1, nil)) + } + + return sb.(*lazyledger.SimpleBlock), currencyApp.Namespace(), registrarApp.Namespace() +} + +func generateProbabilisticBlock(registrarTxes int, otherTxes, txSize int) (*lazyledger.ProbabilisticBlock, [namespaceSize]byte, [namespaceSize]byte) { + pb := lazyledger.NewProbabilisticBlock([]byte{0}, txSize) + txSize -= namespaceSize + 2 + + bs := lazyledger.NewSimpleBlockStore() + b := lazyledger.NewBlockchain(bs) + + ms1 := lazyledger.NewSimpleMap() + currencyApp := lazyledger.NewCurrency(ms1, b) + b.RegisterApplication(¤cyApp) + + privA, pubA, _ := crypto.GenerateSecp256k1Key(rand.Reader) + _, pubB, _ := crypto.GenerateSecp256k1Key(rand.Reader) + pubABytes, _ := pubA.Bytes() + pubBBytes, _ := pubB.Bytes() + pubABalanceBytes := make([]byte, binary.MaxVarintLen64) + binary.BigEndian.PutUint64(pubABalanceBytes, 1000000) + ms1.Put(pubABytes, pubABalanceBytes) + + ms2 := lazyledger.NewSimpleMap() + registrarApp := lazyledger.NewRegistrar(ms2, currencyApp.(*lazyledger.Currency), pubBBytes) + b.RegisterApplication(®istrarApp) + + for i := 0; i < registrarTxes; i++ { + pb.AddMessage(currencyApp.(*lazyledger.Currency).GenerateTransaction(privA, pubB, 1, nil)) + } + + ms3 := lazyledger.NewSimpleMap() + registrarApp2 := lazyledger.NewRegistrar(ms3, currencyApp.(*lazyledger.Currency), pubBBytes) + b.RegisterApplication(®istrarApp2) + + for i := 0; i < otherTxes; i++ { + pb.AddMessage(currencyApp.(*lazyledger.Currency).GenerateTransaction(privA, pubB, 1, nil)) + } + + return pb.(*lazyledger.ProbabilisticBlock), currencyApp.Namespace(), registrarApp.Namespace() +} diff --git a/cmd/lazyledger_measurements_5/.gitignore b/cmd/lazyledger_measurements_5/.gitignore new file mode 100644 index 0000000..6dd5bcb --- /dev/null +++ b/cmd/lazyledger_measurements_5/.gitignore @@ -0,0 +1 @@ +lazyledger_measurements_5 diff --git a/cmd/lazyledger_measurements_5/main.go b/cmd/lazyledger_measurements_5/main.go new file mode 100644 index 0000000..6be943c --- /dev/null +++ b/cmd/lazyledger_measurements_5/main.go @@ -0,0 +1,178 @@ +package main + +import ( + "crypto/rand" + "encoding/binary" + "fmt" + + "github.com/musalbas/lazyledger-prototype" + "github.com/libp2p/go-libp2p-crypto" +) + +const namespaceSize = 8 + +func main() { + registrarTxes := 10 + txAmounts := []int{128, 256, 384, 512, 640, 768, 896, 1024} + txSize := 256 + for _, txes := range txAmounts { + sbBandwidthMax := 0 + pbBandwidthMax := 0 + + for i := 0; i < 10; i++ { + sb, cns, rns := generateSimpleBlock(registrarTxes, txes, txSize) + sbBandwidth := 0 + _, _, proofs1, messages1, _ := sb.ApplicationProof(cns) + for _, msg := range *messages1 { + sbBandwidth += len(msg.Marshal()) + } + for _, hash := range proofs1 { + sbBandwidth += len(hash) + } + if sbBandwidth > sbBandwidthMax { + sbBandwidthMax = sbBandwidth + } + _, _, proofs1, messages1, _ = sb.ApplicationProof(rns) + for _, msg := range *messages1 { + sbBandwidth += len(msg.Marshal()) + } + for _, hash := range proofs1 { + sbBandwidth += len(hash) + } + if sbBandwidth > sbBandwidthMax { + sbBandwidthMax = sbBandwidth + } + + pb, cns, rns := generateProbabilisticBlock(registrarTxes, txes, txSize) + _, _, proofs2, messages2, _ := pb.ApplicationProof(cns) + pbBandwidth := 0 + for _, msg := range *messages2 { + pbBandwidth += len(msg.Marshal()) + } + for _, proof := range proofs2 { + for _, hash := range proof { + pbBandwidth += len(hash) + } + } + _, _, proofs2, messages2, _ = pb.ApplicationProof(rns) + for _, msg := range *messages2 { + pbBandwidth += len(msg.Marshal()) + } + for _, proof := range proofs2 { + for _, hash := range proof { + pbBandwidth += len(hash) + } + } + if pbBandwidth > pbBandwidthMax { + pbBandwidthMax = pbBandwidth + } + } + + fmt.Println(txes, sbBandwidthMax, pbBandwidthMax) + } +} + +func generateSimpleBlock(registrarTxes int, otherTxes, txSize int) (*lazyledger.SimpleBlock, [namespaceSize]byte, [namespaceSize]byte) { + txSize -= namespaceSize + + bs := lazyledger.NewSimpleBlockStore() + b := lazyledger.NewBlockchain(bs) + + sb := lazyledger.NewSimpleBlock([]byte{0}) + + ms1 := lazyledger.NewSimpleMap() + currencyApp := lazyledger.NewCurrency(ms1, b) + b.RegisterApplication(¤cyApp) + + privA, pubA, _ := crypto.GenerateSecp256k1Key(rand.Reader) + _, pubB, _ := crypto.GenerateSecp256k1Key(rand.Reader) + _, pubC, _ := crypto.GenerateSecp256k1Key(rand.Reader) + pubABytes, _ := pubA.Bytes() + pubBBytes, _ := pubB.Bytes() + pubCBytes, _ := pubC.Bytes() + pubABalanceBytes := make([]byte, binary.MaxVarintLen64) + binary.BigEndian.PutUint64(pubABalanceBytes, 1000000) + ms1.Put(pubABytes, pubABalanceBytes) + + ms2 := lazyledger.NewSimpleMap() + registrarApp := lazyledger.NewRegistrar(ms2, currencyApp.(*lazyledger.Currency), pubBBytes) + var rns1 [namespaceSize]byte + copy(rns1[:], []byte("reg1")) + registrarApp.(*lazyledger.Registrar).SetNamespace(rns1) + b.RegisterApplication(®istrarApp) + sb.AddMessage(currencyApp.(*lazyledger.Currency).GenerateTransaction(privA, pubB, 100000, nil)) + + for i := 0; i < registrarTxes; i++ { + name := make([]byte, 8) + rand.Read(name) + sb.AddMessage(registrarApp.(*lazyledger.Registrar).GenerateTransaction(privA, name)) + } + + ms3 := lazyledger.NewSimpleMap() + registrarApp2 := lazyledger.NewRegistrar(ms3, currencyApp.(*lazyledger.Currency), pubCBytes) + var rns2 [namespaceSize]byte + copy(rns2[:], []byte("reg2")) + registrarApp2.(*lazyledger.Registrar).SetNamespace(rns2) + b.RegisterApplication(®istrarApp2) + sb.AddMessage(currencyApp.(*lazyledger.Currency).GenerateTransaction(privA, pubC, 100000, nil)) + + for i := 0; i < otherTxes; i++ { + name := make([]byte, 8) + rand.Read(name) + sb.AddMessage(registrarApp2.(*lazyledger.Registrar).GenerateTransaction(privA, name)) + } + + return sb.(*lazyledger.SimpleBlock), currencyApp.Namespace(), registrarApp.Namespace() +} + +func generateProbabilisticBlock(registrarTxes int, otherTxes, txSize int) (*lazyledger.ProbabilisticBlock, [namespaceSize]byte, [namespaceSize]byte) { + pb := lazyledger.NewProbabilisticBlock([]byte{0}, txSize) + txSize -= namespaceSize + 2 + + bs := lazyledger.NewSimpleBlockStore() + b := lazyledger.NewBlockchain(bs) + + ms1 := lazyledger.NewSimpleMap() + currencyApp := lazyledger.NewCurrency(ms1, b) + b.RegisterApplication(¤cyApp) + + privA, pubA, _ := crypto.GenerateSecp256k1Key(rand.Reader) + _, pubB, _ := crypto.GenerateSecp256k1Key(rand.Reader) + _, pubC, _ := crypto.GenerateSecp256k1Key(rand.Reader) + pubABytes, _ := pubA.Bytes() + pubBBytes, _ := pubB.Bytes() + pubCBytes, _ := pubC.Bytes() + pubABalanceBytes := make([]byte, binary.MaxVarintLen64) + binary.BigEndian.PutUint64(pubABalanceBytes, 1000000) + ms1.Put(pubABytes, pubABalanceBytes) + + ms2 := lazyledger.NewSimpleMap() + registrarApp := lazyledger.NewRegistrar(ms2, currencyApp.(*lazyledger.Currency), pubBBytes) + var rns1 [namespaceSize]byte + copy(rns1[:], []byte("reg1")) + registrarApp.(*lazyledger.Registrar).SetNamespace(rns1) + b.RegisterApplication(®istrarApp) + pb.AddMessage(currencyApp.(*lazyledger.Currency).GenerateTransaction(privA, pubB, 100000, nil)) + + for i := 0; i < registrarTxes; i++ { + name := make([]byte, 8) + rand.Read(name) + pb.AddMessage(registrarApp.(*lazyledger.Registrar).GenerateTransaction(privA, name)) + } + + ms3 := lazyledger.NewSimpleMap() + registrarApp2 := lazyledger.NewRegistrar(ms3, currencyApp.(*lazyledger.Currency), pubCBytes) + var rns2 [namespaceSize]byte + copy(rns2[:], []byte("reg2")) + registrarApp2.(*lazyledger.Registrar).SetNamespace(rns2) + b.RegisterApplication(®istrarApp2) + pb.AddMessage(currencyApp.(*lazyledger.Currency).GenerateTransaction(privA, pubC, 100000, nil)) + + for i := 0; i < otherTxes; i++ { + name := make([]byte, 8) + rand.Read(name) + pb.AddMessage(registrarApp2.(*lazyledger.Registrar).GenerateTransaction(privA, name)) + } + + return pb.(*lazyledger.ProbabilisticBlock), currencyApp.Namespace(), registrarApp.Namespace() +}