diff --git a/integration/obscurogateway/obscurogateway_test.go b/integration/obscurogateway/obscurogateway_test.go index b4888aebd2..5e3a6075ae 100644 --- a/integration/obscurogateway/obscurogateway_test.go +++ b/integration/obscurogateway/obscurogateway_test.go @@ -5,10 +5,6 @@ import ( "crypto/ecdsa" "encoding/hex" "fmt" - "github.com/ethereum/go-ethereum/accounts/abi/bind" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethclient" - "github.com/ethereum/go-ethereum/rlp" "io" "math/big" "net/http" @@ -16,6 +12,11 @@ import ( "testing" "time" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethclient" + "github.com/ethereum/go-ethereum/rlp" + "github.com/stretchr/testify/require" "github.com/valyala/fasthttp" @@ -53,7 +54,7 @@ const ( ) func TestObscuroGateway(t *testing.T) { - //t.Skip("Commented it out until more testing is driven from this test") + // t.Skip("Commented it out until more testing is driven from this test") startPort := integration.StartPortObscuroGatewayUnitTest wallets := createObscuroNetwork(t, startPort) @@ -113,7 +114,7 @@ func TestObscuroGateway(t *testing.T) { } func TestObscuroGatewaySubscriptionsWithMultipleAccounts(t *testing.T) { - //t.Skip("Commented it out until more testing is driven from this test") + // t.Skip("Commented it out until more testing is driven from this test") startPort := integration.StartPortObscuroGatewayUnitTest wallets := createObscuroNetwork(t, startPort) @@ -151,96 +152,52 @@ func TestObscuroGatewaySubscriptionsWithMultipleAccounts(t *testing.T) { // Server is now ready and we can create requests - // user0 owns faucet wallet - w := wallets.L2FaucetWallet - fmt.Printf("user0 owns addresses: %s\n", w.Address()) - - // Get wallets that we want to use for different users - // user1 owns the following wallets - user1W1 := wallets.SimObsWallets[0] - user1W2 := wallets.SimObsWallets[1] - fmt.Printf("user1 owns addresses: %s, %s\n", user1W1.Address(), user1W2.Address()) - - // user2 owns the following wallets - user2W1 := wallets.SimObsWallets[2] - user2W2 := wallets.SimObsWallets[3] - fmt.Printf("user2 owns addresses: %s, %s\n", user2W1.Address(), user2W2.Address()) - - // join Obscuro Gateway as three different users (user0, user1, user2) - // user0 deploys smart contracts - userID0, err := joinObscuroGateway(serverAddressHTTP) + // Create users + user0, err := NewUser([]wallet.Wallet{wallets.L2FaucetWallet}, serverAddressHTTP, serverAddressWS) require.NoError(t, err) - fmt.Println(fmt.Sprintf("user0 joined Obscuro Gateway with userID: %s", userID0)) - httpConnectionURLUser0 := serverAddressHTTP + "/v1/" + "?u=" + userID0 - httpClientUser0, err := ethclient.Dial(httpConnectionURLUser0) + fmt.Printf("Created user with UserID: %s\n", user0.UserID) - userID1, err := joinObscuroGateway(serverAddressHTTP) + user1, err := NewUser(wallets.SimObsWallets[0:2], serverAddressHTTP, serverAddressWS) require.NoError(t, err) - fmt.Println(fmt.Sprintf("user1 joined Obscuro Gateway with userID: %s", userID1)) - httpConnectionURLUser1 := serverAddressHTTP + "/v1/" + "?u=" + userID1 - httpClientUser1, err := ethclient.Dial(httpConnectionURLUser1) + fmt.Printf("Created user with UserID: %s\n", user1.UserID) - userID2, err := joinObscuroGateway(serverAddressHTTP) + user2, err := NewUser(wallets.SimObsWallets[2:4], serverAddressHTTP, serverAddressWS) require.NoError(t, err) - fmt.Println(fmt.Sprintf("user2 joined Obscuro Gateway with userID: %s", userID2)) - httpConnectionURLUser2 := serverAddressHTTP + "/v1/" + "?u=" + userID2 - httpClientUser2, err := ethclient.Dial(httpConnectionURLUser2) + fmt.Printf("Created user with UserID: %s\n", user2.UserID) - // register one address with user0 - resp, err := registerAccount(serverAddressHTTP, userID0, w.PrivateKey(), w.Address().Hex()) + // register all the accounts for that user + err = user0.RegisterAccounts() require.NoError(t, err) - fmt.Printf("Successfully registered address %s for user: %s with response: %s \n", w.Address().Hex(), userID0, resp) - - // register two addresses with user1 - resp, err = registerAccount(serverAddressHTTP, userID1, user1W1.PrivateKey(), user1W1.Address().Hex()) + err = user1.RegisterAccounts() require.NoError(t, err) - fmt.Printf("Successfully registered address %s for user: %s with response: %s \n", user1W1.Address().Hex(), userID1, resp) - - resp, err = registerAccount(serverAddressHTTP, userID1, user1W2.PrivateKey(), user1W2.Address().Hex()) + err = user2.RegisterAccounts() require.NoError(t, err) - fmt.Printf("Successfully registered address %s for user: %s with response: %s \n", user1W2.Address().Hex(), userID1, resp) - - // register two addresses with user2 - resp, err = registerAccount(serverAddressHTTP, userID2, user2W1.PrivateKey(), user2W1.Address().Hex()) - require.NoError(t, err) - fmt.Printf("Successfully registered address %s for user: %s with response: %s \n", user2W1.Address().Hex(), userID2, resp) - - resp, err = registerAccount(serverAddressHTTP, userID2, user2W2.PrivateKey(), user2W2.Address().Hex()) - require.NoError(t, err) - fmt.Printf("Successfully registered address %s for user: %s with response: %s \n", user2W2.Address().Hex(), userID2, resp) + // Transfer some funds to user1 and user2 wallets, because they need it to make transactions var amountToTransfer int64 = 1_000_000_000_000_000_000 // Transfer some funds to user1 and user2 wallets, because they need it to make transactions - err = TransferETHToAddress(httpClientUser0, w, user1W1.Address(), amountToTransfer) + err = TransferETHToAddress(user0.HTTPClient, user0.Wallets[0], user1.Wallets[0].Address(), amountToTransfer) require.NoError(t, err) - err = TransferETHToAddress(httpClientUser0, w, user1W2.Address(), amountToTransfer) + time.Sleep(5 * time.Second) + err = TransferETHToAddress(user0.HTTPClient, user0.Wallets[0], user1.Wallets[1].Address(), amountToTransfer) require.NoError(t, err) - err = TransferETHToAddress(httpClientUser0, w, user2W1.Address(), amountToTransfer) + err = TransferETHToAddress(user0.HTTPClient, user0.Wallets[0], user2.Wallets[0].Address(), amountToTransfer) require.NoError(t, err) - err = TransferETHToAddress(httpClientUser0, w, user2W2.Address(), amountToTransfer) + err = TransferETHToAddress(user0.HTTPClient, user0.Wallets[0], user2.Wallets[1].Address(), amountToTransfer) require.NoError(t, err) // wait for the TXs to be mined - time.Sleep(2 * time.Second) + time.Sleep(5 * time.Second) - // Get balances of all registered accounts - balance, err := httpClientUser0.BalanceAt(context.Background(), w.Address(), nil) - require.NoError(t, err) - fmt.Println("Balance for account ", w.Address().Hex(), " - ", balance.String()) - balance, err = httpClientUser1.BalanceAt(context.Background(), user1W1.Address(), nil) - require.NoError(t, err) - fmt.Println("Balance for account ", user1W1.Address().Hex(), " - ", balance.String()) - balance, err = httpClientUser1.BalanceAt(context.Background(), user1W2.Address(), nil) + // Print balances of all registered accounts to check if all accounts have funds + err = user0.PrintUserAccountsBalances() require.NoError(t, err) - fmt.Println("Balance for account ", user1W2.Address().Hex(), " - ", balance.String()) - balance, err = httpClientUser2.BalanceAt(context.Background(), user2W1.Address(), nil) + err = user1.PrintUserAccountsBalances() require.NoError(t, err) - fmt.Println("Balance for account ", user2W1.Address().Hex(), " - ", balance.String()) - balance, err = httpClientUser2.BalanceAt(context.Background(), user2W2.Address(), nil) + err = user2.PrintUserAccountsBalances() require.NoError(t, err) - fmt.Println("Balance for account ", user2W2.Address().Hex(), " - ", balance.String()) // Deploy a contract that emits events (with user0) - //bytecode := `60806040523480156200001157600080fd5b506040518060200160405280600081525060009081620000329190620002d4565b506040518060200160405280600081525060019081620000539190620002d4565b50620003bb565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680620000dc57607f821691505b602082108103620000f257620000f162000094565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026200015c7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff826200011d565b6200016886836200011d565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b6000620001b5620001af620001a98462000180565b6200018a565b62000180565b9050919050565b6000819050919050565b620001d18362000194565b620001e9620001e082620001bc565b8484546200012a565b825550505050565b600090565b62000200620001f1565b6200020d818484620001c6565b505050565b5b81811015620002355762000229600082620001f6565b60018101905062000213565b5050565b601f82111562000284576200024e81620000f8565b62000259846200010d565b8101602085101562000269578190505b6200028162000278856200010d565b83018262000212565b50505b505050565b600082821c905092915050565b6000620002a96000198460080262000289565b1980831691505092915050565b6000620002c4838362000296565b9150826002028217905092915050565b620002df826200005a565b67ffffffffffffffff811115620002fb57620002fa62000065565b5b620003078254620000c3565b6200031482828562000239565b600060209050601f8311600181146200034c576000841562000337578287015190505b620003438582620002b6565b865550620003b3565b601f1984166200035c86620000f8565b60005b8281101562000386578489015182556001820191506020850194506020810190506200035f565b86831015620003a65784890151620003a2601f89168262000296565b8355505b6001600288020188555050505b505050505050565b6107ee80620003cb6000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c8063368b877214610051578063c2d366581461006d578063c5ced0361461008b578063e21f37ce146100a7575b600080fd5b61006b600480360381019061006691906103e6565b6100c5565b005b610075610126565b60405161008291906104ae565b60405180910390f35b6100a560048036038101906100a091906103e6565b6101b4565b005b6100af6101fe565b6040516100bc91906104ae565b60405180910390f35b80600090816100d491906106e6565b503373ffffffffffffffffffffffffffffffffffffffff167fe31c2ad953ded70272b94617f9181f8cc33755f1b40f4c706660f6ee0dfb634a8260405161011b91906104ae565b60405180910390a250565b60018054610133906104ff565b80601f016020809104026020016040519081016040528092919081815260200182805461015f906104ff565b80156101ac5780601f10610181576101008083540402835291602001916101ac565b820191906000526020600020905b81548152906001019060200180831161018f57829003601f168201915b505050505081565b80600190816101c391906106e6565b507f4fcdf2659dcf2254d2bce07af2baaf0c6ddf6da052dd241b2445a2f6398ae575816040516101f391906104ae565b60405180910390a150565b6000805461020b906104ff565b80601f0160208091040260200160405190810160405280929190818152602001828054610237906104ff565b80156102845780601f1061025957610100808354040283529160200191610284565b820191906000526020600020905b81548152906001019060200180831161026757829003601f168201915b505050505081565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6102f3826102aa565b810181811067ffffffffffffffff82111715610312576103116102bb565b5b80604052505050565b600061032561028c565b905061033182826102ea565b919050565b600067ffffffffffffffff821115610351576103506102bb565b5b61035a826102aa565b9050602081019050919050565b82818337600083830152505050565b600061038961038484610336565b61031b565b9050828152602081018484840111156103a5576103a46102a5565b5b6103b0848285610367565b509392505050565b600082601f8301126103cd576103cc6102a0565b5b81356103dd848260208601610376565b91505092915050565b6000602082840312156103fc576103fb610296565b5b600082013567ffffffffffffffff81111561041a5761041961029b565b5b610426848285016103b8565b91505092915050565b600081519050919050565b600082825260208201905092915050565b60005b8381101561046957808201518184015260208101905061044e565b60008484015250505050565b60006104808261042f565b61048a818561043a565b935061049a81856020860161044b565b6104a3816102aa565b840191505092915050565b600060208201905081810360008301526104c88184610475565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061051757607f821691505b60208210810361052a576105296104d0565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026105927fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82610555565b61059c8683610555565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b60006105e36105de6105d9846105b4565b6105be565b6105b4565b9050919050565b6000819050919050565b6105fd836105c8565b610611610609826105ea565b848454610562565b825550505050565b600090565b610626610619565b6106318184846105f4565b505050565b5b818110156106555761064a60008261061e565b600181019050610637565b5050565b601f82111561069a5761066b81610530565b61067484610545565b81016020851015610683578190505b61069761068f85610545565b830182610636565b50505b505050565b600082821c905092915050565b60006106bd6000198460080261069f565b1980831691505092915050565b60006106d683836106ac565b9150826002028217905092915050565b6106ef8261042f565b67ffffffffffffffff811115610708576107076102bb565b5b61071282546104ff565b61071d828285610659565b600060209050601f831160018114610750576000841561073e578287015190505b61074885826106ca565b8655506107b0565b601f19841661075e86610530565b60005b8281101561078657848901518255600182019150602085019450602081019050610761565b868310156107a3578489015161079f601f8916826106ac565b8355505b6001600288020188555050505b50505050505056fea2646970667358221220e4e772e2ddf401fe8b1b737e3e4e3852532ce880f04bb28f41e4d17d4ae8227964736f6c63430008120033` + // bytecode := `60806040523480156200001157600080fd5b506040518060200160405280600081525060009081620000329190620002d4565b506040518060200160405280600081525060019081620000539190620002d4565b50620003bb565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680620000dc57607f821691505b602082108103620000f257620000f162000094565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026200015c7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff826200011d565b6200016886836200011d565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b6000620001b5620001af620001a98462000180565b6200018a565b62000180565b9050919050565b6000819050919050565b620001d18362000194565b620001e9620001e082620001bc565b8484546200012a565b825550505050565b600090565b62000200620001f1565b6200020d818484620001c6565b505050565b5b81811015620002355762000229600082620001f6565b60018101905062000213565b5050565b601f82111562000284576200024e81620000f8565b62000259846200010d565b8101602085101562000269578190505b6200028162000278856200010d565b83018262000212565b50505b505050565b600082821c905092915050565b6000620002a96000198460080262000289565b1980831691505092915050565b6000620002c4838362000296565b9150826002028217905092915050565b620002df826200005a565b67ffffffffffffffff811115620002fb57620002fa62000065565b5b620003078254620000c3565b6200031482828562000239565b600060209050601f8311600181146200034c576000841562000337578287015190505b620003438582620002b6565b865550620003b3565b601f1984166200035c86620000f8565b60005b8281101562000386578489015182556001820191506020850194506020810190506200035f565b86831015620003a65784890151620003a2601f89168262000296565b8355505b6001600288020188555050505b505050505050565b6107ee80620003cb6000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c8063368b877214610051578063c2d366581461006d578063c5ced0361461008b578063e21f37ce146100a7575b600080fd5b61006b600480360381019061006691906103e6565b6100c5565b005b610075610126565b60405161008291906104ae565b60405180910390f35b6100a560048036038101906100a091906103e6565b6101b4565b005b6100af6101fe565b6040516100bc91906104ae565b60405180910390f35b80600090816100d491906106e6565b503373ffffffffffffffffffffffffffffffffffffffff167fe31c2ad953ded70272b94617f9181f8cc33755f1b40f4c706660f6ee0dfb634a8260405161011b91906104ae565b60405180910390a250565b60018054610133906104ff565b80601f016020809104026020016040519081016040528092919081815260200182805461015f906104ff565b80156101ac5780601f10610181576101008083540402835291602001916101ac565b820191906000526020600020905b81548152906001019060200180831161018f57829003601f168201915b505050505081565b80600190816101c391906106e6565b507f4fcdf2659dcf2254d2bce07af2baaf0c6ddf6da052dd241b2445a2f6398ae575816040516101f391906104ae565b60405180910390a150565b6000805461020b906104ff565b80601f0160208091040260200160405190810160405280929190818152602001828054610237906104ff565b80156102845780601f1061025957610100808354040283529160200191610284565b820191906000526020600020905b81548152906001019060200180831161026757829003601f168201915b505050505081565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6102f3826102aa565b810181811067ffffffffffffffff82111715610312576103116102bb565b5b80604052505050565b600061032561028c565b905061033182826102ea565b919050565b600067ffffffffffffffff821115610351576103506102bb565b5b61035a826102aa565b9050602081019050919050565b82818337600083830152505050565b600061038961038484610336565b61031b565b9050828152602081018484840111156103a5576103a46102a5565b5b6103b0848285610367565b509392505050565b600082601f8301126103cd576103cc6102a0565b5b81356103dd848260208601610376565b91505092915050565b6000602082840312156103fc576103fb610296565b5b600082013567ffffffffffffffff81111561041a5761041961029b565b5b610426848285016103b8565b91505092915050565b600081519050919050565b600082825260208201905092915050565b60005b8381101561046957808201518184015260208101905061044e565b60008484015250505050565b60006104808261042f565b61048a818561043a565b935061049a81856020860161044b565b6104a3816102aa565b840191505092915050565b600060208201905081810360008301526104c88184610475565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061051757607f821691505b60208210810361052a576105296104d0565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026105927fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82610555565b61059c8683610555565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b60006105e36105de6105d9846105b4565b6105be565b6105b4565b9050919050565b6000819050919050565b6105fd836105c8565b610611610609826105ea565b848454610562565b825550505050565b600090565b610626610619565b6106318184846105f4565b505050565b5b818110156106555761064a60008261061e565b600181019050610637565b5050565b601f82111561069a5761066b81610530565b61067484610545565b81016020851015610683578190505b61069761068f85610545565b830182610636565b50505b505050565b600082821c905092915050565b60006106bd6000198460080261069f565b1980831691505092915050565b60006106d683836106ac565b9150826002028217905092915050565b6106ef8261042f565b67ffffffffffffffff811115610708576107076102bb565b5b61071282546104ff565b61071d828285610659565b600060209050601f831160018114610750576000841561073e578287015190505b61074885826106ca565b8655506107b0565b601f19841661075e86610530565b60005b8281101561078657848901518255600182019150602085019450602081019050610761565b868310156107a3578489015161079f601f8916826106ac565b8355505b6001600288020188555050505b50505050505056fea2646970667358221220e4e772e2ddf401fe8b1b737e3e4e3852532ce880f04bb28f41e4d17d4ae8227964736f6c63430008120033` bytecode := `60806040523480156200001157600080fd5b506040518060400160405280600381526020017f666f6f00000000000000000000000000000000000000000000000000000000008152506000908162000058919062000320565b506040518060400160405280600381526020017f666f6f0000000000000000000000000000000000000000000000000000000000815250600190816200009f919062000320565b5062000407565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806200012857607f821691505b6020821081036200013e576200013d620000e0565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b600060088302620001a87fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8262000169565b620001b4868362000169565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b600062000201620001fb620001f584620001cc565b620001d6565b620001cc565b9050919050565b6000819050919050565b6200021d83620001e0565b620002356200022c8262000208565b84845462000176565b825550505050565b600090565b6200024c6200023d565b6200025981848462000212565b505050565b5b8181101562000281576200027560008262000242565b6001810190506200025f565b5050565b601f821115620002d0576200029a8162000144565b620002a58462000159565b81016020851015620002b5578190505b620002cd620002c48562000159565b8301826200025e565b50505b505050565b600082821c905092915050565b6000620002f560001984600802620002d5565b1980831691505092915050565b6000620003108383620002e2565b9150826002028217905092915050565b6200032b82620000a6565b67ffffffffffffffff811115620003475762000346620000b1565b5b6200035382546200010f565b6200036082828562000285565b600060209050601f83116001811462000398576000841562000383578287015190505b6200038f858262000302565b865550620003ff565b601f198416620003a88662000144565b60005b82811015620003d257848901518255600182019150602085019450602081019050620003ab565b86831015620003f25784890151620003ee601f891682620002e2565b8355505b6001600288020188555050505b505050505050565b6107ee80620004176000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c8063368b877214610051578063c2d366581461006d578063c5ced0361461008b578063e21f37ce146100a7575b600080fd5b61006b600480360381019061006691906103e6565b6100c5565b005b610075610126565b60405161008291906104ae565b60405180910390f35b6100a560048036038101906100a091906103e6565b6101b4565b005b6100af6101fe565b6040516100bc91906104ae565b60405180910390f35b80600090816100d491906106e6565b503373ffffffffffffffffffffffffffffffffffffffff167fe31c2ad953ded70272b94617f9181f8cc33755f1b40f4c706660f6ee0dfb634a8260405161011b91906104ae565b60405180910390a250565b60018054610133906104ff565b80601f016020809104026020016040519081016040528092919081815260200182805461015f906104ff565b80156101ac5780601f10610181576101008083540402835291602001916101ac565b820191906000526020600020905b81548152906001019060200180831161018f57829003601f168201915b505050505081565b80600190816101c391906106e6565b507f4fcdf2659dcf2254d2bce07af2baaf0c6ddf6da052dd241b2445a2f6398ae575816040516101f391906104ae565b60405180910390a150565b6000805461020b906104ff565b80601f0160208091040260200160405190810160405280929190818152602001828054610237906104ff565b80156102845780601f1061025957610100808354040283529160200191610284565b820191906000526020600020905b81548152906001019060200180831161026757829003601f168201915b505050505081565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6102f3826102aa565b810181811067ffffffffffffffff82111715610312576103116102bb565b5b80604052505050565b600061032561028c565b905061033182826102ea565b919050565b600067ffffffffffffffff821115610351576103506102bb565b5b61035a826102aa565b9050602081019050919050565b82818337600083830152505050565b600061038961038484610336565b61031b565b9050828152602081018484840111156103a5576103a46102a5565b5b6103b0848285610367565b509392505050565b600082601f8301126103cd576103cc6102a0565b5b81356103dd848260208601610376565b91505092915050565b6000602082840312156103fc576103fb610296565b5b600082013567ffffffffffffffff81111561041a5761041961029b565b5b610426848285016103b8565b91505092915050565b600081519050919050565b600082825260208201905092915050565b60005b8381101561046957808201518184015260208101905061044e565b60008484015250505050565b60006104808261042f565b61048a818561043a565b935061049a81856020860161044b565b6104a3816102aa565b840191505092915050565b600060208201905081810360008301526104c88184610475565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061051757607f821691505b60208210810361052a576105296104d0565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026105927fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82610555565b61059c8683610555565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b60006105e36105de6105d9846105b4565b6105be565b6105b4565b9050919050565b6000819050919050565b6105fd836105c8565b610611610609826105ea565b848454610562565b825550505050565b600090565b610626610619565b6106318184846105f4565b505050565b5b818110156106555761064a60008261061e565b600181019050610637565b5050565b601f82111561069a5761066b81610530565b61067484610545565b81016020851015610683578190505b61069761068f85610545565b830182610636565b50505b505050565b600082821c905092915050565b60006106bd6000198460080261069f565b1980831691505092915050565b60006106d683836106ac565b9150826002028217905092915050565b6106ef8261042f565b67ffffffffffffffff811115610708576107076102bb565b5b61071282546104ff565b61071d828285610659565b600060209050601f831160018114610750576000841561073e578287015190505b61074885826106ca565b8655506107b0565b601f19841661075e86610530565b60005b8281101561078657848901518255600182019150602085019450602081019050610761565b868310156107a3578489015161079f601f8916826106ac565b8355505b6001600288020188555050505b50505050505056fea264697066735822122076146d8c796917af248ecb981f38094293788d92ad21704dc623fd8412cb9dc964736f6c63430008120033` abiString := ` [ @@ -336,35 +293,36 @@ func TestObscuroGatewaySubscriptionsWithMultipleAccounts(t *testing.T) { ] ` contractTx := types.LegacyTx{ - Nonce: w.GetNonceAndIncrement(), + Nonce: user0.Wallets[0].GetNonceAndIncrement(), Gas: uint64(1_000_000), GasPrice: gethcommon.Big1, Data: gethcommon.FromHex(bytecode), } - signedTx, err := w.SignTransaction(&contractTx) + signedTx, err := user0.Wallets[0].SignTransaction(&contractTx) require.NoError(t, err) - err = httpClientUser0.SendTransaction(context.Background(), signedTx) + err = user0.HTTPClient.SendTransaction(context.Background(), signedTx) require.NoError(t, err) fmt.Printf("Created Tx: %s \n", signedTx.Hash().Hex()) // wait for the TX to be mined time.Sleep(2 * time.Second) - txReceiptContractCreation, err := httpClientUser0.TransactionReceipt(context.Background(), signedTx.Hash()) + txReceiptContractCreation, err := user0.HTTPClient.TransactionReceipt(context.Background(), signedTx.Hash()) assert.Nil(t, err) fmt.Printf("Tx %s was included in block %d\n", signedTx.Hash().Hex(), txReceiptContractCreation.BlockNumber) // get contract address - contractAddress, err := ComputeContractAddress(w.Address(), w.GetNonce()-1) + contractAddress, err := ComputeContractAddress(user0.Wallets[0].Address(), user0.Wallets[0].GetNonce()-1) assert.Nil(t, err) // contract abi contractAbi, err := abi.JSON(strings.NewReader(abiString)) + require.NoError(t, err) // check if contract was deployed and call one of the implicit getter functions // call getter for a message - contract := bind.NewBoundContract(contractAddress, contractAbi, httpClientUser1, httpClientUser1, httpClientUser1) + contract := bind.NewBoundContract(contractAddress, contractAbi, user1.HTTPClient, user1.HTTPClient, user1.HTTPClient) var resultMessage string callOpts := &bind.CallOpts{} results := make([]interface{}, 1) @@ -379,9 +337,9 @@ func TestObscuroGatewaySubscriptionsWithMultipleAccounts(t *testing.T) { newMessageValue := "bar" // interact with smart contract and cause events to be emitted - intTx1, err := InteractWithSmartContract(httpClientUser1, user1W1, contractAbi, "setMessage", newMessageValue, contractAddress) + intTx1, err := InteractWithSmartContract(user1.HTTPClient, user1.Wallets[0], contractAbi, "setMessage", newMessageValue, contractAddress) require.NoError(t, err) - intTx2, err := InteractWithSmartContract(httpClientUser1, user1W1, contractAbi, "setMessage2", newMessageValue, contractAddress) + intTx2, err := InteractWithSmartContract(user1.HTTPClient, user1.Wallets[0], contractAbi, "setMessage2", newMessageValue, contractAddress) require.NoError(t, err) time.Sleep(5 * time.Second) @@ -395,24 +353,20 @@ func TestObscuroGatewaySubscriptionsWithMultipleAccounts(t *testing.T) { // Get Tx Receipts to check if txs were included in block - intTx1Receipt, err := httpClientUser1.TransactionReceipt(context.Background(), intTx1) + intTx1Receipt, err := user1.HTTPClient.TransactionReceipt(context.Background(), intTx1) assert.Nil(t, err) fmt.Printf("Tx2 %s was included in block %d\n", intTx1.Hex(), intTx1Receipt.BlockNumber) - intTx2Receipt, err := httpClientUser1.TransactionReceipt(context.Background(), intTx2) + intTx2Receipt, err := user1.HTTPClient.TransactionReceipt(context.Background(), intTx2) assert.Nil(t, err) fmt.Printf("Tx2 %s was included in block %d\n", intTx2.Hex(), intTx2Receipt.BlockNumber) - at2, err := httpClientUser0.BalanceAt(context.Background(), w.Address(), nil) - require.NoError(t, err) - fmt.Println("Final balance for faucet account ", w.Address().Hex(), " - ", at2.String()) - - //wsConnectionURL := serverAddressWS + "/v1/" + "?u=" + userID0 - //wsClient, err := ethclient.Dial(wsConnectionURL) - //require.NoError(t, err) - //bn, err := wsClient.BlockNumber(context.Background()) - //require.NoError(t, err) - //fmt.Printf("Block number is: %d\n", bn) + // wsConnectionURL := serverAddressWS + "/v1/" + "?u=" + userID0 + // wsClient, err := ethclient.Dial(wsConnectionURL) + // require.NoError(t, err) + // bn, err := wsClient.BlockNumber(context.Background()) + // require.NoError(t, err) + // fmt.Printf("Block number is: %d\n", bn) // Gracefully shutdown err = obscuroGwContainer.Stop() @@ -592,6 +546,10 @@ func TransferETHToAddress(client *ethclient.Client, wallet wallet.Wallet, toAddr func InteractWithSmartContract(client *ethclient.Client, wallet wallet.Wallet, contractAbi abi.ABI, methodName string, methodParam string, contractAddress gethcommon.Address) (gethcommon.Hash, error) { contractInteractionData, err := contractAbi.Pack(methodName, methodParam) + if err != nil { + return gethcommon.Hash{}, err + } + interactionTx := types.LegacyTx{ Nonce: wallet.GetNonceAndIncrement(), To: &contractAddress, @@ -610,3 +568,64 @@ func InteractWithSmartContract(client *ethclient.Client, wallet wallet.Wallet, c return signedTx.Hash(), nil } + +type User struct { + UserID string + Wallets []wallet.Wallet + HTTPClient *ethclient.Client + WSClient *ethclient.Client + ServerAddressHTTP string + ServerAddressWS string +} + +func NewUser(wallets []wallet.Wallet, serverAddressHTTP string, serverAddressWS string) (*User, error) { + // automatically join OG + userID, err := joinObscuroGateway(serverAddressHTTP) + if err != nil { + return nil, err + } + + // create clients + httpConnectionHTTPURL := serverAddressHTTP + "/v1/" + "?u=" + userID + httpClient, err := ethclient.Dial(httpConnectionHTTPURL) + if err != nil { + return nil, err + } + httpConnectionWSURL := serverAddressWS + "/v1/" + "?u=" + userID + wsClient, err := ethclient.Dial(httpConnectionWSURL) + if err != nil { + return nil, err + } + + return &User{ + UserID: userID, + Wallets: wallets, + HTTPClient: httpClient, + WSClient: wsClient, + ServerAddressHTTP: serverAddressHTTP, + ServerAddressWS: serverAddressWS, + }, nil +} + +func (u User) RegisterAccounts() error { + for _, w := range u.Wallets { + response, err := registerAccount(u.ServerAddressHTTP, u.UserID, w.PrivateKey(), w.Address().Hex()) + if err != nil { + return err + } + fmt.Printf("Successfully registered address %s for user: %s with response: %s \n", w.Address().Hex(), u.UserID, response) + } + + return nil +} + +func (u User) PrintUserAccountsBalances() error { + for _, w := range u.Wallets { + balance, err := u.HTTPClient.BalanceAt(context.Background(), w.Address(), nil) + if err != nil { + return err + } + fmt.Println("Balance for account ", w.Address().Hex(), " - ", balance.String()) + } + return nil +}