Skip to content
Snippets Groups Projects
Commit 61a25ece authored by Wolfgang Welz's avatar Wolfgang Welz
Browse files

Require PoW for all meta transactions

parent dceedf17
No related branches found
No related tags found
No related merge requests found
......@@ -36,4 +36,6 @@ const (
MARSHALED_TOTAL_SIZE = NONCE_END
BRANCH_NULL_HASH = trinary.Trytes("999999999999999999999999999999999999999999999999999999999999999999999999999999999")
MIN_WEIGHT_MAGNITUDE = 12
)
package meta_transaction
import (
"fmt"
"sync"
"github.com/iotaledger/iota.go/consts"
......@@ -561,3 +562,13 @@ func (this *MetaTransaction) DoProofOfWork(mwm int) error {
return nil
}
func (this *MetaTransaction) Validate() error {
// check that the weight magnitude is valid
weightMagnitude := this.GetWeightMagnitude()
if weightMagnitude < MIN_WEIGHT_MAGNITUDE {
return fmt.Errorf("insufficient weight magnitude: got=%d, want=%d", weightMagnitude, MIN_WEIGHT_MAGNITUDE)
}
return nil
}
......@@ -7,12 +7,16 @@ import (
"github.com/golang/protobuf/proto"
"github.com/iotaledger/goshimmer/packages/gossip"
pb "github.com/iotaledger/goshimmer/packages/gossip/proto"
"github.com/iotaledger/goshimmer/packages/model/meta_transaction"
"github.com/iotaledger/goshimmer/packages/model/value_transaction"
"github.com/iotaledger/goshimmer/plugins/autopeering/local"
"github.com/iotaledger/goshimmer/plugins/tipselection"
"github.com/iotaledger/hive.go/daemon"
"github.com/iotaledger/hive.go/logger"
)
var log = logger.NewLogger("Transaction Spammer")
var spamming = false
var spammingMutex sync.Mutex
......@@ -54,6 +58,11 @@ func Start(tps uint) {
tx.SetValue(totalSentCounter)
tx.SetBranchTransactionHash(tipselection.GetRandomTip())
tx.SetTrunkTransactionHash(tipselection.GetRandomTip())
tx.SetTimestamp(uint(time.Now().Unix()))
if err := tx.DoProofOfWork(meta_transaction.MIN_WEIGHT_MAGNITUDE); err != nil {
log.Warning("PoW failed", err)
continue
}
mtx := &pb.Transaction{Body: tx.MetaTransaction.GetBytes()}
b, _ := proto.Marshal(mtx)
......
......@@ -98,7 +98,7 @@ func configureEvents() {
}))
tangle.Events.TransactionSolid.Attach(events.NewClosure(func(tx *value_transaction.ValueTransaction) {
log.Info("Tx solidified:", tx.MetaTransaction.GetHash())
log.Info("gossip solid tx", tx.MetaTransaction.GetHash())
t := &pb.Transaction{
Body: tx.MetaTransaction.GetBytes(),
}
......
......@@ -38,10 +38,18 @@ func configureSolidifier(plugin *node.Plugin) {
unsolidTxs = NewUnsolidTxs()
gossip.Events.TransactionReceived.Attach(events.NewClosure(func(ev *gossip.TransactionReceivedEvent) {
//log.Info("New Transaction", ev.Body)
pTx := &pb.Transaction{}
proto.Unmarshal(ev.Body, pTx)
if err := proto.Unmarshal(ev.Body, pTx); err != nil {
log.Warningf("invalid transaction: %s", err)
return
}
metaTx := meta_transaction.FromBytes(pTx.GetBody())
if err := metaTx.Validate(); err != nil {
log.Warningf("invalid transaction: %s", err)
return
}
workerPool.Submit(metaTx)
}))
......
......@@ -8,11 +8,12 @@ import (
"github.com/golang/protobuf/proto"
"github.com/iotaledger/goshimmer/packages/gossip"
pb "github.com/iotaledger/goshimmer/packages/gossip/proto"
"github.com/iotaledger/goshimmer/packages/model/meta_transaction"
"github.com/iotaledger/goshimmer/packages/model/value_transaction"
"github.com/iotaledger/hive.go/events"
"github.com/iotaledger/hive.go/node"
"github.com/iotaledger/hive.go/parameter"
"github.com/iotaledger/iota.go/trinary"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
......@@ -29,16 +30,23 @@ func TestSolidifier(t *testing.T) {
// create transactions and chain them together
transaction1 := value_transaction.New()
transaction1.SetNonce(trinary.Trytes("99999999999999999999999999A"))
transaction1.SetValue(1)
require.NoError(t, transaction1.DoProofOfWork(meta_transaction.MIN_WEIGHT_MAGNITUDE))
transaction2 := value_transaction.New()
transaction2.SetValue(2)
transaction2.SetBranchTransactionHash(transaction1.GetHash())
require.NoError(t, transaction2.DoProofOfWork(meta_transaction.MIN_WEIGHT_MAGNITUDE))
transaction3 := value_transaction.New()
transaction3.SetValue(3)
transaction3.SetBranchTransactionHash(transaction2.GetHash())
require.NoError(t, transaction3.DoProofOfWork(meta_transaction.MIN_WEIGHT_MAGNITUDE))
transaction4 := value_transaction.New()
transaction4.SetValue(4)
transaction4.SetBranchTransactionHash(transaction3.GetHash())
require.NoError(t, transaction4.DoProofOfWork(meta_transaction.MIN_WEIGHT_MAGNITUDE))
// setup event handlers
var wg sync.WaitGroup
......
......@@ -7,6 +7,7 @@ import (
"github.com/golang/protobuf/proto"
"github.com/iotaledger/goshimmer/packages/gossip"
pb "github.com/iotaledger/goshimmer/packages/gossip/proto"
"github.com/iotaledger/goshimmer/packages/model/meta_transaction"
"github.com/iotaledger/goshimmer/packages/model/value_transaction"
"github.com/iotaledger/goshimmer/plugins/autopeering/local"
"github.com/iotaledger/goshimmer/plugins/tipselection"
......@@ -53,6 +54,10 @@ func SendDataHandler(c echo.Context) error {
tx.SetSignatureMessageFragment(trytes)
tx.SetBranchTransactionHash(tipselection.GetRandomTip())
tx.SetTrunkTransactionHash(tipselection.GetRandomTip())
tx.SetTimestamp(uint(time.Now().Unix()))
if err := tx.DoProofOfWork(meta_transaction.MIN_WEIGHT_MAGNITUDE); err != nil {
log.Warning("PoW failed", err)
}
transactionHash := tx.GetHash()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment