diff --git a/packages/model/meta_transaction/constants.go b/packages/model/meta_transaction/constants.go index 814e958c82ca4e359b1553650fedc71a25d0561f..399fd041fc35e8a338734cc8ada6c5e53e11ba65 100644 --- a/packages/model/meta_transaction/constants.go +++ b/packages/model/meta_transaction/constants.go @@ -36,4 +36,6 @@ const ( MARSHALED_TOTAL_SIZE = NONCE_END BRANCH_NULL_HASH = trinary.Trytes("999999999999999999999999999999999999999999999999999999999999999999999999999999999") + + MIN_WEIGHT_MAGNITUDE = 12 ) diff --git a/packages/model/meta_transaction/meta_transaction.go b/packages/model/meta_transaction/meta_transaction.go index c20bab96a35c761f6ee85bb17b8b679113df08dd..d85160a6155ab106d5efef7080a5168e2fe28712 100644 --- a/packages/model/meta_transaction/meta_transaction.go +++ b/packages/model/meta_transaction/meta_transaction.go @@ -1,6 +1,7 @@ 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 +} diff --git a/packages/transactionspammer/transactionspammer.go b/packages/transactionspammer/transactionspammer.go index d189acde38ffe2d381ef18cd6cf3fd68bbd56a83..75ccf787b4a02e72228b2fba0468f8e2f3c8184e 100644 --- a/packages/transactionspammer/transactionspammer.go +++ b/packages/transactionspammer/transactionspammer.go @@ -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) diff --git a/plugins/gossip/gossip.go b/plugins/gossip/gossip.go index 12045c6369b2908fdda1b0b0af3efb7fa0e23f7f..efbb650e226574bbcf0141674aaf2126a6c29757 100644 --- a/plugins/gossip/gossip.go +++ b/plugins/gossip/gossip.go @@ -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(), } diff --git a/plugins/tangle/solidifier.go b/plugins/tangle/solidifier.go index f799848033d57030fc925c38ccb514b1bc5078cd..130deda4fc2a92178621bc7fd869eb60de9030f6 100644 --- a/plugins/tangle/solidifier.go +++ b/plugins/tangle/solidifier.go @@ -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) })) diff --git a/plugins/tangle/solidifier_test.go b/plugins/tangle/solidifier_test.go index 263294f59e8cc036a283e1bc1d16798c75e2757f..be845077fab3fdc134dd5bf9ea3b44154f2a7e1c 100644 --- a/plugins/tangle/solidifier_test.go +++ b/plugins/tangle/solidifier_test.go @@ -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 diff --git a/plugins/webapi-send-data/plugin.go b/plugins/webapi-send-data/plugin.go index 98c01bb5eb5f7562a08553abed6667eda89d4204..8fdf3746346a9c715bf5619dbd546e4f36e22726 100644 --- a/plugins/webapi-send-data/plugin.go +++ b/plugins/webapi-send-data/plugin.go @@ -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()