Skip to content
Snippets Groups Projects
Unverified Commit b4d04a3d authored by Angelo Capossele's avatar Angelo Capossele Committed by GitHub
Browse files

Update double-spend tool (#664)

* :sparkles: Update double-spend tool

* :rotating_light: Fix linter warnings

* :green_heart: Fix review comments
parent 183b4f44
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ package main ...@@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sync"
"time" "time"
"github.com/iotaledger/goshimmer/client" "github.com/iotaledger/goshimmer/client"
...@@ -12,53 +13,107 @@ import ( ...@@ -12,53 +13,107 @@ import (
valuepayload "github.com/iotaledger/goshimmer/dapps/valuetransfers/packages/payload" valuepayload "github.com/iotaledger/goshimmer/dapps/valuetransfers/packages/payload"
"github.com/iotaledger/goshimmer/dapps/valuetransfers/packages/transaction" "github.com/iotaledger/goshimmer/dapps/valuetransfers/packages/transaction"
"github.com/iotaledger/goshimmer/dapps/valuetransfers/packages/wallet" "github.com/iotaledger/goshimmer/dapps/valuetransfers/packages/wallet"
"github.com/mr-tron/base58"
) )
func main() { func main() {
clients := make([]*client.GoShimmerAPI, 2)
client := client.NewGoShimmerAPI("http://localhost:8080", http.Client{Timeout: 30 * time.Second}) node1APIURL := "http://127.0.0.1:8080"
node2APIURL := "http://127.0.0.1:8090"
// genesis wallet if node1APIURL == node2APIURL {
genesisSeedBytes, err := base58.Decode("7R1itJx5hVuo9w9hjg5cwKFmek4HMSoBDgJZN8hKGxih") fmt.Println("Please use 2 different nodes to issue a double-spend")
if err != nil { return
}
clients[0] = client.NewGoShimmerAPI(node1APIURL, http.Client{Timeout: 60 * time.Second})
clients[1] = client.NewGoShimmerAPI(node2APIURL, http.Client{Timeout: 60 * time.Second})
myWallet := wallet.New()
myAddr := myWallet.Seed().Address(0)
if _, err := clients[0].SendFaucetRequest(myAddr.String()); err != nil {
fmt.Println(err) fmt.Println(err)
return
}
var myOutputID string
var confirmed bool
// wait for the funds
for i := 0; i < 10; i++ {
time.Sleep(5 * time.Second)
resp, err := clients[0].GetUnspentOutputs([]string{myAddr.String()})
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Waiting for funds to be confirmed...")
for _, v := range resp.UnspentOutputs {
if len(v.OutputIDs) > 0 {
myOutputID = v.OutputIDs[0].ID
confirmed = v.OutputIDs[0].InclusionState.Confirmed
break
}
}
if myOutputID != "" && confirmed {
break
}
} }
const genesisBalance = 1000000000 if myOutputID == "" {
genesisWallet := wallet.New(genesisSeedBytes) fmt.Println("Could not find OutputID")
genesisAddr := genesisWallet.Seed().Address(0) return
genesisOutputID := transaction.NewOutputID(genesisAddr, transaction.GenesisID) }
if !confirmed {
fmt.Println("OutputID not confirmed")
return
}
out, err := transaction.OutputIDFromBase58(myOutputID)
if err != nil {
fmt.Println("malformed OutputID")
return
}
// issue transactions which spend the same genesis output in all partitions // issue transactions which spend the same output
conflictingTxs := make([]*transaction.Transaction, 2) conflictingTxs := make([]*transaction.Transaction, 2)
conflictingTxIDs := make([]string, 2) conflictingMsgIDs := make([]string, 2)
receiverSeeds := make([]*wallet.Seed, 2) receiverSeeds := make([]*wallet.Seed, 2)
var wg sync.WaitGroup
for i := range conflictingTxs { for i := range conflictingTxs {
wg.Add(1)
go func(i int) {
defer wg.Done()
// create a new receiver wallet for the given conflict fmt.Println(i)
receiverSeeds[i] = wallet.NewSeed()
destAddr := receiverSeeds[i].Address(0)
tx := transaction.New( // create a new receiver wallet for the given conflict
transaction.NewInputs(genesisOutputID), receiverSeeds[i] = wallet.NewSeed()
transaction.NewOutputs(map[address.Address][]*balance.Balance{ destAddr := receiverSeeds[i].Address(0)
destAddr: {
{Value: genesisBalance, Color: balance.ColorIOTA},
},
}))
tx = tx.Sign(signaturescheme.ED25519(*genesisWallet.Seed().KeyPair(0)))
conflictingTxs[i] = tx
valueObject := valuepayload.New(valuepayload.GenesisID, valuepayload.GenesisID, tx) tx := transaction.New(
transaction.NewInputs(out),
transaction.NewOutputs(map[address.Address][]*balance.Balance{
destAddr: {
{Value: 1337, Color: balance.ColorIOTA},
},
}))
tx = tx.Sign(signaturescheme.ED25519(*myWallet.Seed().KeyPair(0)))
conflictingTxs[i] = tx
// issue the value object valueObject := valuepayload.New(valuepayload.GenesisID, valuepayload.GenesisID, tx)
txID, err := client.SendPayload(valueObject.Bytes())
if err != nil { // issue the tx
fmt.Println(err) conflictingMsgIDs[i], err = clients[i].SendPayload(valueObject.Bytes())
} if err != nil {
conflictingTxIDs[i] = txID fmt.Println(err)
fmt.Printf("issued conflict transaction %s\n", txID) return
//time.Sleep(7 * time.Second) }
fmt.Printf("issued conflict transaction %s\n", conflictingMsgIDs[i])
}(i)
} }
wg.Wait()
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment