-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulatedClient.go
109 lines (83 loc) · 2.34 KB
/
simulatedClient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"context"
"fmt"
"log"
"math/big"
"time"
"SimulatedEthBackend/ethutils"
"SimulatedEthBackend/ipfsutils"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
)
/* func deleteFiles() {
err := os.Remove("Contract/Challenge.bin")
if err != nil {
log.Fatal(err)
}
err = os.Remove("Contract/Challenge.abi")
if err != nil {
log.Fatal(err)
}
err = os.Remove("Contract/challenge.go")
if err != nil {
log.Fatal(err)
}
} */
// PrintReciept executes a go routine that recieves reciepts via channel and prints them to STDOUT
func PrintReciept(done chan bool) chan string {
ch := make(chan string)
go func() {
for {
select {
case reciept, open := <-ch:
if open {
fmt.Println("Reciept has been added ", reciept)
} else {
done <- true
return
}
}
}
}()
return ch
}
//CheckForReciepts is a helper function that generate reciepts from hashes and store them on IPFS.
func CheckForReciepts(hashes []common.Hash, reciepts chan string, client *backends.SimulatedBackend, ipfsManager *ipfsutils.IpfsManager) {
for _, val := range hashes {
reciept, _ := client.TransactionReceipt(context.Background(), val)
recieptJSON, _ := reciept.MarshalJSON()
hash, _ := ipfsManager.DagPut(recieptJSON, "json", "cbor")
reciepts <- hash
}
close(reciepts)
}
func main() {
// defer deleteFiles()
privateKey := ethutils.NewPrivateKey()
addr := ethutils.DeriveAddress(privateKey)
fmt.Println("Account Address", addr)
client := ethutils.SimlatingClient(privateKey)
done := make(chan bool)
hashes := make([]common.Hash, 0)
challSession, contractAddr, tx := ethutils.Deploy(privateKey, client)
fmt.Println("New Contract Address", contractAddr.Hex())
ipfsManager, err := ipfsutils.NewManager("localhost:5001", time.Second)
if err != nil {
log.Fatal("Having error connecting with IPFS", err)
}
hashes = append(hashes, tx.Hash())
reciepts := PrintReciept(done)
uintb, _ := challSession.GetB()
tx1, _ := challSession.SetB(big.NewInt(1))
client.Commit()
hashes = append(hashes, tx1.Hash())
addressa, _ := challSession.GetA()
tx2, _ := challSession.SetA()
client.Commit()
hashes = append(hashes, tx2.Hash())
fmt.Println("uint b", uintb)
fmt.Println("address a", addressa.Hex())
CheckForReciepts(hashes, reciepts, client, ipfsManager)
<-done
}