From 4e976c62f1d98d49fdb10b989d08c83ae4fe5406 Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Thu, 3 Feb 2022 15:28:26 +0530 Subject: [PATCH] Changes in service for removed support of watched storage slots --- cmd/serve.go | 12 +- test/helper.go | 29 +- test/watch_address_integration_test.go | 1170 +++++++---------- ...ss_gap_filling_service_integration_test.go | 297 +---- 4 files changed, 550 insertions(+), 958 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index 0fb8a78c..5843d229 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -30,7 +30,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/statediff" - sdtypes "github.com/ethereum/go-ethereum/statediff/types" "github.com/mailgun/groupcache/v2" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -305,7 +304,6 @@ func startStateTrieValidator(config *s.Config, server s.Server) { type WatchedAddress struct { Address string `db:"address"` - Kind int `db:"kind"` CreatedAt uint64 `db:"created_at"` WatchedAt uint64 `db:"watched_at"` LastFilledAt uint64 `db:"last_filled_at"` @@ -343,15 +341,7 @@ func startWatchedAddressGapFiller(config *s.Config) { fillAddresses := []interface{}{} for _, fillWatchedAddress := range fillWatchedAddresses { if blockNumber >= fillWatchedAddress.startBlock && blockNumber <= fillWatchedAddress.endBlock { - switch fillWatchedAddress.Kind { - case sdtypes.WatchedAddress.Int(): - params.WatchedAddresses = append(params.WatchedAddresses, common.HexToAddress(fillWatchedAddress.Address)) - case sdtypes.WatchedStorageSlot.Int(): - params.WatchedStorageSlots = append(params.WatchedStorageSlots, common.HexToHash(fillWatchedAddress.Address)) - default: - log.Fatalf("Unexpected kind %d:", fillWatchedAddress.Kind) - } - + params.WatchedAddresses = append(params.WatchedAddresses, common.HexToAddress(fillWatchedAddress.Address)) fillAddresses = append(fillAddresses, fillWatchedAddress.Address) } } diff --git a/test/helper.go b/test/helper.go index f7feea2c..c8ff01fe 100644 --- a/test/helper.go +++ b/test/helper.go @@ -36,6 +36,10 @@ type StorageKey struct { Key string `json:"key"` } +type CountIncremented struct { + BlockNumber int64 `json:"blockNumber"` +} + const srvUrl = "http://localhost:3000" func DeployContract() (*ContractDeployed, error) { @@ -105,22 +109,21 @@ func DeploySLVContract() (*ContractDeployed, error) { return &contract, nil } -func IncrementCountA(addr string) error { - _, err := http.Get(fmt.Sprintf("%s/v1/incrementCountA?addr=%s", srvUrl, addr)) +func IncrementCount(addr string, count string) (*CountIncremented, error) { + res, err := http.Get(fmt.Sprintf("%s/v1/incrementCount%s?addr=%s", srvUrl, count, addr)) if err != nil { - return err + return nil, err } - return nil -} + var blockNumber CountIncremented -func IncrementCountB(addr string) error { - _, err := http.Get(fmt.Sprintf("%s/v1/incrementCountB?addr=%s", srvUrl, addr)) + decoder := json.NewDecoder(res.Body) + err = decoder.Decode(&blockNumber) if err != nil { - return err + return nil, err } - return nil + return &blockNumber, nil } func GetStorageSlotKey(contract string, label string) (*StorageKey, error) { @@ -146,13 +149,7 @@ func ClearWatchedAddresses(gethRPCClient *rpc.Client) error { args := []sdtypes.WatchAddressArg{} // Clear watched addresses - gethErr := gethRPCClient.Call(nil, gethMethod, statediff.ClearAddresses, args) - if gethErr != nil { - return gethErr - } - - // Clear watched storage slots - gethErr = gethRPCClient.Call(nil, gethMethod, statediff.ClearStorageSlots, args) + gethErr := gethRPCClient.Call(nil, gethMethod, statediff.Clear, args) if gethErr != nil { return gethErr } diff --git a/test/watch_address_integration_test.go b/test/watch_address_integration_test.go index f551bfc4..fc26ec29 100644 --- a/test/watch_address_integration_test.go +++ b/test/watch_address_integration_test.go @@ -8,7 +8,6 @@ import ( "time" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/statediff" @@ -45,16 +44,31 @@ var _ = Describe("WatchAddress integration test", func() { var ( ctx = context.Background() + txErr error contractErr error + incErr error contract1 *integration.ContractDeployed contract2 *integration.ContractDeployed contract3 *integration.ContractDeployed - countAIndex string - countBIndex string - countAStorageHash common.Hash - countBStorageHash common.Hash + countAIndex string + + prevBalance1 *big.Int + prevBalance2 *big.Int + prevBalance3 *big.Int + + actualBalance1 *big.Int + actualBalance2 *big.Int + actualBalance3 *big.Int + + prevCountA1 *big.Int + prevCountA2 *big.Int + prevCountA3 *big.Int + + actualCountA1 *big.Int + actualCountA2 *big.Int + actualCountA3 *big.Int ) BeforeEach(func() { @@ -74,765 +88,559 @@ var _ = Describe("WatchAddress integration test", func() { contract3, contractErr = integration.DeploySLVContract() Expect(contractErr).ToNot(HaveOccurred()) - // Get storage slot keys + // Get the storage slot key storageSlotAKey, err := integration.GetStorageSlotKey("SLVToken", "countA") Expect(err).ToNot(HaveOccurred()) countAIndex = storageSlotAKey.Key - countAStorageHash = crypto.Keccak256Hash(common.HexToHash(countAIndex).Bytes()) - storageSlotBKey, err := integration.GetStorageSlotKey("SLVToken", "countB") + // Clear out watched addresses + err = integration.ClearWatchedAddresses(gethRPCClient) Expect(err).ToNot(HaveOccurred()) - countBIndex = storageSlotBKey.Key - countBStorageHash = crypto.Keccak256Hash(common.HexToHash(countBIndex).Bytes()) + + // Get initial balances for all the contracts + // Contract 1 + actualBalance1 = big.NewInt(0) + initBalance1, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract1.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(initBalance1.String()).To(Equal(actualBalance1.String())) + prevBalance1 = big.NewInt(0) + + // Contract 2 + actualBalance2 = big.NewInt(0) + initBalance2, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract2.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(initBalance2.String()).To(Equal(actualBalance2.String())) + prevBalance2 = big.NewInt(0) + + // Contract 3 + actualBalance3 = big.NewInt(0) + initBalance3, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract3.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(initBalance3.String()).To(Equal(actualBalance3.String())) + prevBalance3 = big.NewInt(0) + + // Get initial storage values for the contracts + // Contract 1, countA + actualCountA1 = big.NewInt(0) + ipldCountA1Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + ipldCountA1 := new(big.Int).SetBytes(ipldCountA1Storage) + Expect(ipldCountA1.String()).To(Equal(actualCountA1.String())) + prevCountA1 = big.NewInt(0) + + // Contract 2, countA + actualCountA2 = big.NewInt(0) + ipldCountA2Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + ipldCountA2 := new(big.Int).SetBytes(ipldCountA2Storage) + Expect(ipldCountA2.String()).To(Equal(actualCountA2.String())) + prevCountA2 = big.NewInt(0) + + // Contract 3, countA + actualCountA3 = big.NewInt(0) + ipldCountA3Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract3.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + ipldCountA3 := new(big.Int).SetBytes(ipldCountA3Storage) + Expect(ipldCountA3.String()).To(Equal(actualCountA2.String())) + prevCountA3 = big.NewInt(0) }) defer It("test cleanup", func() { - // Clear out watched addresses | storage slots + // Clear out watched addresses err := integration.ClearWatchedAddresses(gethRPCClient) Expect(err).ToNot(HaveOccurred()) }) - Describe("watched addresses", func() { - var ( - txErr error - - prevBalance1 *big.Int - prevBalance2 *big.Int - prevBalance3 *big.Int - - actualBalance1 *big.Int - actualBalance2 *big.Int - actualBalance3 *big.Int - ) - - It("watched addresses test init", func() { - // Clear out watched addresses | storage slots - err := integration.ClearWatchedAddresses(gethRPCClient) - Expect(err).ToNot(HaveOccurred()) - - // Get initial balances for all the contracts + Context("no contracts being watched", func() { + It("indexes state for all the contracts", func() { + // WatchedAddresses = [] + // Send eth to all three contract accounts // Contract 1 - actualBalance1 = big.NewInt(0) - prevBalance1 = big.NewInt(0) - initBalance1, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract1.Address), nil) + _, txErr = integration.SendEth(contract1.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance1.Add(actualBalance1, big.NewInt(10000000000000000)) + + balance1AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract1.Address), nil) Expect(err).ToNot(HaveOccurred()) - Expect(initBalance1.String()).To(Equal(actualBalance1.String())) + Expect(balance1AfterTransfer.String()).To(Equal(actualBalance1.String())) + prevBalance1.Set(actualBalance1) // Contract 2 - actualBalance2 = big.NewInt(0) - prevBalance2 = big.NewInt(0) - initBalance2, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract2.Address), nil) + _, txErr = integration.SendEth(contract2.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance2.Add(actualBalance2, big.NewInt(10000000000000000)) + + balance2AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract2.Address), nil) Expect(err).ToNot(HaveOccurred()) - Expect(initBalance2.String()).To(Equal(actualBalance2.String())) + Expect(balance2AfterTransfer.String()).To(Equal(actualBalance2.String())) + prevBalance2.Set(actualBalance2) // Contract 3 - actualBalance3 = big.NewInt(0) - prevBalance3 = big.NewInt(0) - initBalance3, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract3.Address), nil) + _, txErr = integration.SendEth(contract3.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance3.Add(actualBalance3, big.NewInt(10000000000000000)) + + balance3AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract3.Address), nil) Expect(err).ToNot(HaveOccurred()) - Expect(initBalance3.String()).To(Equal(actualBalance3.String())) - }) + Expect(balance3AfterTransfer.String()).To(Equal(actualBalance3.String())) + prevBalance3.Set(actualBalance3) - Context("no contracts being watched", func() { - It("indexes state for all the contracts", func() { - // WatchedAddresses = [] - // Send eth to all three contract accounts - // Contract 1 - _, txErr = integration.SendEth(contract1.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance1.Add(actualBalance1, big.NewInt(10000000000000000)) + // Increment counts + // Contract 1, countA + _, incErr = integration.IncrementCount(contract1.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA1.Add(actualCountA1, big.NewInt(1)) - balance1AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract1.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance1AfterTransfer.String()).To(Equal(actualBalance1.String())) - prevBalance1.Set(actualBalance1) + countA1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA1AfterIncrement := new(big.Int).SetBytes(countA1AfterIncrementStorage) + Expect(countA1AfterIncrement.String()).To(Equal(actualCountA1.String())) + prevCountA1.Set(actualCountA1) - // Contract 2 - _, txErr = integration.SendEth(contract2.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance2.Add(actualBalance2, big.NewInt(10000000000000000)) + // Contract 2, countA + _, incErr = integration.IncrementCount(contract2.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA2.Add(actualCountA2, big.NewInt(1)) - balance2AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract2.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance2AfterTransfer.String()).To(Equal(actualBalance2.String())) - prevBalance2.Set(actualBalance2) + countA2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA2AfterIncrement := new(big.Int).SetBytes(countA2AfterIncrementStorage) + Expect(countA2AfterIncrement.String()).To(Equal(actualCountA2.String())) + prevCountA2.Set(actualCountA2) - // Contract 3 - _, txErr = integration.SendEth(contract3.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance3.Add(actualBalance3, big.NewInt(10000000000000000)) + // Contract 3, countA + _, incErr = integration.IncrementCount(contract3.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA3.Add(actualCountA3, big.NewInt(1)) - balance3AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract3.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance3AfterTransfer.String()).To(Equal(actualBalance3.String())) - prevBalance3.Set(actualBalance3) - }) - }) - - Context("one contract being watched", func() { - It("indexes state only for the watched contract", func() { - operation := statediff.AddAddresses - args := []sdtypes.WatchAddressArg{ - { - Address: contract1.Address, - CreatedAt: uint64(contract1.BlockNumber), - }, - } - ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) - Expect(ipldErr).ToNot(HaveOccurred()) - - // WatchedAddresses = [Contract1] - // Send eth to all three contract accounts - // Contract 1 - _, txErr = integration.SendEth(contract1.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance1.Add(actualBalance1, big.NewInt(10000000000000000)) - - balance1AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract1.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance1AfterTransfer.String()).To(Equal(actualBalance1.String())) - prevBalance1.Set(actualBalance1) - - // Contract 2 - _, txErr = integration.SendEth(contract2.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance2.Add(actualBalance2, big.NewInt(10000000000000000)) - - balance2AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract2.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance2AfterTransfer.String()).To(Equal(prevBalance2.String())) - - // Contract 3 - _, txErr = integration.SendEth(contract3.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance3.Add(actualBalance3, big.NewInt(10000000000000000)) - - balance3AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract3.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance3AfterTransfer.String()).To(Equal(prevBalance3.String())) - }) - }) - - Context("contract added to a non-empty watch-list", func() { - It("indexes state only for the watched contracts", func() { - operation := statediff.AddAddresses - args := []sdtypes.WatchAddressArg{ - { - Address: contract2.Address, - CreatedAt: uint64(contract2.BlockNumber), - }, - } - ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) - Expect(ipldErr).ToNot(HaveOccurred()) - - // WatchedAddresses = [Contract1, Contract2] - // Send eth to all three contract accounts - // Contract 1 - _, txErr = integration.SendEth(contract1.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance1.Add(actualBalance1, big.NewInt(10000000000000000)) - - balance1AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract1.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance1AfterTransfer.String()).To(Equal(actualBalance1.String())) - prevBalance1.Set(actualBalance1) - - // Contract 2 - _, txErr = integration.SendEth(contract2.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance2.Add(actualBalance2, big.NewInt(10000000000000000)) - - balance2AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract2.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance2AfterTransfer.String()).To(Equal(actualBalance2.String())) - prevBalance2.Set(actualBalance2) - - // Contract 3 - _, txErr = integration.SendEth(contract3.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance3.Add(actualBalance3, big.NewInt(10000000000000000)) - - balance3AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract3.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance3AfterTransfer.String()).To(Equal(prevBalance3.String())) - }) - }) - - Context("contract removed from the watch-list", func() { - It("indexes state only for the watched contract", func() { - operation := statediff.RemoveAddresses - args := []sdtypes.WatchAddressArg{ - { - Address: contract1.Address, - CreatedAt: uint64(contract1.BlockNumber), - }, - } - ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) - Expect(ipldErr).ToNot(HaveOccurred()) - - // WatchedAddresses = [Contract2] - // Send eth to all three contract accounts - // Contract 1 - _, txErr = integration.SendEth(contract1.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance1.Add(actualBalance1, big.NewInt(10000000000000000)) - - balance1AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract1.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance1AfterTransfer.String()).To(Equal(prevBalance1.String())) - - // Contract 2 - _, txErr = integration.SendEth(contract2.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance2.Add(actualBalance2, big.NewInt(10000000000000000)) - - balance2AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract2.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance2AfterTransfer.String()).To(Equal(actualBalance2.String())) - prevBalance2.Set(actualBalance2) - - // Contract 3 - _, txErr = integration.SendEth(contract3.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance3.Add(actualBalance3, big.NewInt(10000000000000000)) - - balance3AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract3.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance3AfterTransfer.String()).To(Equal(prevBalance3.String())) - }) - }) - - Context("list of watched addresses set", func() { - It("indexes state only for the watched contracts", func() { - operation := statediff.SetAddresses - args := []sdtypes.WatchAddressArg{ - { - Address: contract1.Address, - CreatedAt: uint64(contract1.BlockNumber), - }, - { - Address: contract3.Address, - CreatedAt: uint64(contract3.BlockNumber), - }, - } - ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) - Expect(ipldErr).ToNot(HaveOccurred()) - - // WatchedAddresses = [Contract1, Contract3] - // Send eth to all three contract accounts - // Contract 1 - _, txErr = integration.SendEth(contract1.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance1.Add(actualBalance1, big.NewInt(10000000000000000)) - - balance1AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract1.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance1AfterTransfer.String()).To(Equal(actualBalance1.String())) - prevBalance1.Set(actualBalance1) - - // Contract 2 - _, txErr = integration.SendEth(contract2.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance2.Add(actualBalance2, big.NewInt(10000000000000000)) - - balance2AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract2.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance2AfterTransfer.String()).To(Equal(prevBalance2.String())) - - // Contract 3 - _, txErr = integration.SendEth(contract3.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance3.Add(actualBalance3, big.NewInt(10000000000000000)) - - balance3AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract3.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance3AfterTransfer.String()).To(Equal(actualBalance3.String())) - prevBalance3.Set(actualBalance3) - }) - }) - - Context("list of watched addresses cleared", func() { - It("indexes state for all the contracts", func() { - operation := statediff.ClearAddresses - args := []sdtypes.WatchAddressArg{} - ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) - Expect(ipldErr).ToNot(HaveOccurred()) - - // WatchedAddresses = [] - // Send eth to all three contract accounts - // Contract 1 - _, txErr = integration.SendEth(contract1.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance1.Add(actualBalance1, big.NewInt(10000000000000000)) - - balance1AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract1.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance1AfterTransfer.String()).To(Equal(actualBalance1.String())) - prevBalance1.Set(actualBalance1) - - // Contract 2 - _, txErr = integration.SendEth(contract2.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance2.Add(actualBalance2, big.NewInt(10000000000000000)) - - balance2AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract2.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance2AfterTransfer.String()).To(Equal(actualBalance2.String())) - prevBalance2.Set(actualBalance2) - - // Contract 3 - _, txErr = integration.SendEth(contract3.Address, "0.01") - time.Sleep(sleepInterval) - Expect(txErr).ToNot(HaveOccurred()) - actualBalance3.Add(actualBalance3, big.NewInt(10000000000000000)) - - balance3AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract3.Address), nil) - Expect(err).ToNot(HaveOccurred()) - Expect(balance3AfterTransfer.String()).To(Equal(actualBalance3.String())) - prevBalance3.Set(actualBalance3) - }) + countA3AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract3.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA3AfterIncrement := new(big.Int).SetBytes(countA3AfterIncrementStorage) + Expect(countA3AfterIncrement.String()).To(Equal(actualCountA3.String())) + prevCountA3.Set(actualCountA3) }) }) - Describe("watched storage slots", func() { - var ( - incErr error + Context("one contract being watched", func() { + It("indexes state only for the watched contract", func() { + operation := statediff.Add + args := []sdtypes.WatchAddressArg{ + { + Address: contract1.Address, + CreatedAt: uint64(contract1.BlockNumber), + }, + } + ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) + Expect(ipldErr).ToNot(HaveOccurred()) - prevCountA1 *big.Int - prevCountB1 *big.Int - prevCountA2 *big.Int - prevCountB2 *big.Int + // WatchedAddresses = [Contract1] + // Send eth to all three contract accounts + // Contract 1 + _, txErr = integration.SendEth(contract1.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance1.Add(actualBalance1, big.NewInt(10000000000000000)) - actualCountA1 *big.Int - actualCountB1 *big.Int - actualCountA2 *big.Int - actualCountB2 *big.Int - ) - - It("watched addresses test init", func() { - // Clear out watched addresses | storage slots - err := integration.ClearWatchedAddresses(gethRPCClient) + balance1AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract1.Address), nil) Expect(err).ToNot(HaveOccurred()) + Expect(balance1AfterTransfer.String()).To(Equal(actualBalance1.String())) + prevBalance1.Set(actualBalance1) - // Get initial storage values for the contracts + // Contract 2 + _, txErr = integration.SendEth(contract2.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance2.Add(actualBalance2, big.NewInt(10000000000000000)) + + balance2AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract2.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(balance2AfterTransfer.String()).To(Equal(prevBalance2.String())) + + // Contract 3 + _, txErr = integration.SendEth(contract3.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance3.Add(actualBalance3, big.NewInt(10000000000000000)) + + balance3AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract3.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(balance3AfterTransfer.String()).To(Equal(prevBalance3.String())) + + // Increment counts // Contract 1, countA - actualCountA1 = big.NewInt(0) - prevCountA1 = big.NewInt(0) - ipldCountA1Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - ipldCountA1 := new(big.Int).SetBytes(ipldCountA1Storage) - Expect(ipldCountA1.String()).To(Equal(actualCountA1.String())) + _, incErr = integration.IncrementCount(contract1.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA1.Add(actualCountA1, big.NewInt(1)) - // Contract 1, countB - actualCountB1 = big.NewInt(0) - prevCountB1 = big.NewInt(0) - ipldCountB1Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countBIndex), nil) + countA1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countAIndex), nil) Expect(err).ToNot(HaveOccurred()) - ipldCountB1 := new(big.Int).SetBytes(ipldCountB1Storage) - Expect(ipldCountB1.String()).To(Equal(actualCountB1.String())) + countA1AfterIncrement := new(big.Int).SetBytes(countA1AfterIncrementStorage) + Expect(countA1AfterIncrement.String()).To(Equal(actualCountA1.String())) + prevCountA1.Set(actualCountA1) // Contract 2, countA - actualCountA2 = big.NewInt(0) - prevCountA2 = big.NewInt(0) - ipldCountA2Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countAIndex), nil) + _, incErr = integration.IncrementCount(contract2.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA2.Add(actualCountA2, big.NewInt(1)) + + countA2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countAIndex), nil) Expect(err).ToNot(HaveOccurred()) - ipldCountA2 := new(big.Int).SetBytes(ipldCountA2Storage) - Expect(ipldCountA2.String()).To(Equal(actualCountA2.String())) + countA2AfterIncrement := new(big.Int).SetBytes(countA2AfterIncrementStorage) + Expect(countA2AfterIncrement.String()).To(Equal(prevCountA2.String())) - // Contract 2, countB - actualCountB2 = big.NewInt(0) - prevCountB2 = big.NewInt(0) - ipldCountB2Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countBIndex), nil) + // Contract 3, countA + _, incErr = integration.IncrementCount(contract3.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA3.Add(actualCountA3, big.NewInt(1)) + + countA3AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract3.Address), common.HexToHash(countAIndex), nil) Expect(err).ToNot(HaveOccurred()) - ipldCountB2 := new(big.Int).SetBytes(ipldCountB2Storage) - Expect(ipldCountB2.String()).To(Equal(actualCountB2.String())) + countA3AfterIncrement := new(big.Int).SetBytes(countA3AfterIncrementStorage) + Expect(countA3AfterIncrement.String()).To(Equal(prevCountA3.String())) }) + }) - Context("no addresses or storage slots being watched", func() { - It("indexes state for all the storage slots", func() { - // WatchedAddresses = [] - // WatchedStorageSlots = [] - // Increment counts - // Contract 1, countA - incErr = integration.IncrementCountA(contract1.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountA1.Add(actualCountA1, big.NewInt(1)) + Context("contract added to a non-empty watch-list", func() { + It("indexes state only for the watched contracts", func() { + operation := statediff.Add + args := []sdtypes.WatchAddressArg{ + { + Address: contract2.Address, + CreatedAt: uint64(contract2.BlockNumber), + }, + } + ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) + Expect(ipldErr).ToNot(HaveOccurred()) - countA1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA1AfterIncrement := new(big.Int).SetBytes(countA1AfterIncrementStorage) - Expect(countA1AfterIncrement.String()).To(Equal(actualCountA1.String())) - prevCountA1.Set(actualCountA1) + // WatchedAddresses = [Contract1, Contract2] + // Send eth to all three contract accounts + // Contract 1 + _, txErr = integration.SendEth(contract1.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance1.Add(actualBalance1, big.NewInt(10000000000000000)) - // Contract 1, countB - incErr = integration.IncrementCountB(contract1.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountB1.Add(actualCountB1, big.NewInt(1)) + balance1AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract1.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(balance1AfterTransfer.String()).To(Equal(actualBalance1.String())) + prevBalance1.Set(actualBalance1) - countB1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB1AfterIncrement := new(big.Int).SetBytes(countB1AfterIncrementStorage) - Expect(countB1AfterIncrement.String()).To(Equal(actualCountB1.String())) - prevCountB1.Set(actualCountB1) + // Contract 2 + _, txErr = integration.SendEth(contract2.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance2.Add(actualBalance2, big.NewInt(10000000000000000)) - // Contract 2, countA - incErr = integration.IncrementCountA(contract2.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountA2.Add(actualCountA2, big.NewInt(1)) + balance2AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract2.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(balance2AfterTransfer.String()).To(Equal(actualBalance2.String())) + prevBalance2.Set(actualBalance2) - countA2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA2AfterIncrement := new(big.Int).SetBytes(countA2AfterIncrementStorage) - Expect(countA2AfterIncrement.String()).To(Equal(actualCountA2.String())) - prevCountA2.Set(actualCountA2) + // Contract 3 + _, txErr = integration.SendEth(contract3.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance3.Add(actualBalance3, big.NewInt(10000000000000000)) - // Contract 2, countB - incErr = integration.IncrementCountB(contract2.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountB2.Add(actualCountB2, big.NewInt(1)) + balance3AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract3.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(balance3AfterTransfer.String()).To(Equal(prevBalance3.String())) - countB2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB2AfterIncrement := new(big.Int).SetBytes(countB2AfterIncrementStorage) - Expect(countB2AfterIncrement.String()).To(Equal(actualCountB2.String())) - prevCountB2.Set(actualCountB2) - }) + // Increment counts + // Contract 1, countA + _, incErr = integration.IncrementCount(contract1.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA1.Add(actualCountA1, big.NewInt(1)) + + countA1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA1AfterIncrement := new(big.Int).SetBytes(countA1AfterIncrementStorage) + Expect(countA1AfterIncrement.String()).To(Equal(actualCountA1.String())) + prevCountA1.Set(actualCountA1) + + // Contract 2, countA + _, incErr = integration.IncrementCount(contract2.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA2.Add(actualCountA2, big.NewInt(1)) + + countA2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA2AfterIncrement := new(big.Int).SetBytes(countA2AfterIncrementStorage) + Expect(countA2AfterIncrement.String()).To(Equal(actualCountA2.String())) + prevCountA2.Set(actualCountA2) + + // Contract 3, countA + _, incErr = integration.IncrementCount(contract3.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA3.Add(actualCountA3, big.NewInt(1)) + + countA3AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract3.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA3AfterIncrement := new(big.Int).SetBytes(countA3AfterIncrementStorage) + Expect(countA3AfterIncrement.String()).To(Equal(prevCountA3.String())) }) + }) - Context("one storage slot being watched", func() { - It("indexes state only for the watched storage slot", func() { - operation := statediff.AddStorageSlots - args := []sdtypes.WatchAddressArg{ - { - Address: countAStorageHash.Hex(), - CreatedAt: uint64(contract1.BlockNumber), - }, - } - ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) - Expect(ipldErr).ToNot(HaveOccurred()) + Context("contract removed from the watch-list", func() { + It("indexes state only for the watched contract", func() { + operation := statediff.Remove + args := []sdtypes.WatchAddressArg{ + { + Address: contract1.Address, + CreatedAt: uint64(contract1.BlockNumber), + }, + } + ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) + Expect(ipldErr).ToNot(HaveOccurred()) - // WatchedAddresses = [] - // WatchedStorageSlots = [countA] - // Increment counts - // Contract 1, countA - incErr = integration.IncrementCountA(contract1.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountA1.Add(actualCountA1, big.NewInt(1)) + // WatchedAddresses = [Contract2] + // Send eth to all three contract accounts + // Contract 1 + _, txErr = integration.SendEth(contract1.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance1.Add(actualBalance1, big.NewInt(10000000000000000)) - countA1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA1AfterIncrement := new(big.Int).SetBytes(countA1AfterIncrementStorage) - Expect(countA1AfterIncrement.String()).To(Equal(actualCountA1.String())) - prevCountA1.Set(actualCountA1) + balance1AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract1.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(balance1AfterTransfer.String()).To(Equal(prevBalance1.String())) - // Contract 1, countB - incErr = integration.IncrementCountB(contract1.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountB1.Add(actualCountB1, big.NewInt(1)) + // Contract 2 + _, txErr = integration.SendEth(contract2.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance2.Add(actualBalance2, big.NewInt(10000000000000000)) - countB1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB1AfterIncrement := new(big.Int).SetBytes(countB1AfterIncrementStorage) - Expect(countB1AfterIncrement.String()).To(Equal(prevCountB1.String())) + balance2AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract2.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(balance2AfterTransfer.String()).To(Equal(actualBalance2.String())) + prevBalance2.Set(actualBalance2) - // Contract 2, countA - incErr = integration.IncrementCountA(contract2.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountA2.Add(actualCountA2, big.NewInt(1)) + // Contract 3 + _, txErr = integration.SendEth(contract3.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance3.Add(actualBalance3, big.NewInt(10000000000000000)) - countA2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA2AfterIncrement := new(big.Int).SetBytes(countA2AfterIncrementStorage) - Expect(countA2AfterIncrement.String()).To(Equal(actualCountA2.String())) - prevCountA2.Set(actualCountA2) + balance3AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract3.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(balance3AfterTransfer.String()).To(Equal(prevBalance3.String())) - // Contract 2, countB - incErr = integration.IncrementCountB(contract2.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountB2.Add(actualCountB2, big.NewInt(1)) + // Increment counts + // Contract 1, countA + _, incErr = integration.IncrementCount(contract1.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA1.Add(actualCountA1, big.NewInt(1)) - countB2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB2AfterIncrement := new(big.Int).SetBytes(countB2AfterIncrementStorage) - Expect(countB2AfterIncrement.String()).To(Equal(prevCountB2.String())) - }) + countA1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA1AfterIncrement := new(big.Int).SetBytes(countA1AfterIncrementStorage) + Expect(countA1AfterIncrement.String()).To(Equal(prevCountA1.String())) + + // Contract 2, countA + _, incErr = integration.IncrementCount(contract2.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA2.Add(actualCountA2, big.NewInt(1)) + + countA2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA2AfterIncrement := new(big.Int).SetBytes(countA2AfterIncrementStorage) + Expect(countA2AfterIncrement.String()).To(Equal(actualCountA2.String())) + prevCountA2.Set(actualCountA2) + + // Contract 3, countA + _, incErr = integration.IncrementCount(contract3.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA3.Add(actualCountA3, big.NewInt(1)) + + countA3AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract3.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA3AfterIncrement := new(big.Int).SetBytes(countA3AfterIncrementStorage) + Expect(countA3AfterIncrement.String()).To(Equal(prevCountA3.String())) }) + }) - Context("list of watched storage slots set", func() { - It("indexes state only for the watched storage slots", func() { - operation := statediff.SetStorageSlots - args := []sdtypes.WatchAddressArg{ - { - Address: countAStorageHash.Hex(), - CreatedAt: uint64(contract1.BlockNumber), - }, - { - Address: countBStorageHash.Hex(), - CreatedAt: uint64(contract2.BlockNumber), - }, - } - ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) - Expect(ipldErr).ToNot(HaveOccurred()) + Context("list of watched addresses set", func() { + It("indexes state only for the watched contracts", func() { + operation := statediff.Set + args := []sdtypes.WatchAddressArg{ + { + Address: contract1.Address, + CreatedAt: uint64(contract1.BlockNumber), + }, + { + Address: contract3.Address, + CreatedAt: uint64(contract3.BlockNumber), + }, + } + ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) + Expect(ipldErr).ToNot(HaveOccurred()) - // WatchedAddresses = [] - // WatchedStorageSlots = [countA, countB] - // Increment counts - // Contract 1, countA - incErr = integration.IncrementCountA(contract1.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountA1.Add(actualCountA1, big.NewInt(1)) + // WatchedAddresses = [Contract1, Contract3] + // Send eth to all three contract accounts + // Contract 1 + _, txErr = integration.SendEth(contract1.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance1.Add(actualBalance1, big.NewInt(10000000000000000)) - countA1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA1AfterIncrement := new(big.Int).SetBytes(countA1AfterIncrementStorage) - Expect(countA1AfterIncrement.String()).To(Equal(actualCountA1.String())) - prevCountA1.Set(actualCountA1) + balance1AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract1.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(balance1AfterTransfer.String()).To(Equal(actualBalance1.String())) + prevBalance1.Set(actualBalance1) - // Contract 1, countB - incErr = integration.IncrementCountB(contract1.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountB1.Add(actualCountB1, big.NewInt(1)) + // Contract 2 + _, txErr = integration.SendEth(contract2.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance2.Add(actualBalance2, big.NewInt(10000000000000000)) - countB1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB1AfterIncrement := new(big.Int).SetBytes(countB1AfterIncrementStorage) - Expect(countB1AfterIncrement.String()).To(Equal(actualCountB1.String())) - prevCountB1.Set(actualCountB1) + balance2AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract2.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(balance2AfterTransfer.String()).To(Equal(prevBalance2.String())) - // Contract 2, countA - incErr = integration.IncrementCountA(contract2.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountA2.Add(actualCountA2, big.NewInt(1)) + // Contract 3 + _, txErr = integration.SendEth(contract3.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance3.Add(actualBalance3, big.NewInt(10000000000000000)) - countA2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA2AfterIncrement := new(big.Int).SetBytes(countA2AfterIncrementStorage) - Expect(countA2AfterIncrement.String()).To(Equal(actualCountA2.String())) - prevCountA2.Set(actualCountA2) + balance3AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract3.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(balance3AfterTransfer.String()).To(Equal(actualBalance3.String())) + prevBalance3.Set(actualBalance3) - // Contract 2, countB - incErr = integration.IncrementCountB(contract2.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountB2.Add(actualCountB2, big.NewInt(1)) + // Increment counts + // Contract 1, countA + _, incErr = integration.IncrementCount(contract1.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA1.Add(actualCountA1, big.NewInt(1)) - countB2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB2AfterIncrement := new(big.Int).SetBytes(countB2AfterIncrementStorage) - Expect(countB2AfterIncrement.String()).To(Equal(actualCountB2.String())) - prevCountB2.Set(actualCountB2) - }) + countA1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA1AfterIncrement := new(big.Int).SetBytes(countA1AfterIncrementStorage) + Expect(countA1AfterIncrement.String()).To(Equal(actualCountA1.String())) + prevCountA1.Set(actualCountA1) + + // Contract 2, countA + _, incErr = integration.IncrementCount(contract2.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA2.Add(actualCountA2, big.NewInt(1)) + + countA2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA2AfterIncrement := new(big.Int).SetBytes(countA2AfterIncrementStorage) + Expect(countA2AfterIncrement.String()).To(Equal(prevCountA2.String())) + + // Contract 3, countA + _, incErr = integration.IncrementCount(contract3.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA3.Add(actualCountA3, big.NewInt(1)) + + countA3AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract3.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA3AfterIncrement := new(big.Int).SetBytes(countA3AfterIncrementStorage) + Expect(countA3AfterIncrement.String()).To(Equal(actualCountA3.String())) + prevCountA3.Set(actualCountA3) }) + }) - Context("storage slot removed from the watch-list", func() { - It("indexes state only for the watched storage slots", func() { - operation := statediff.RemoveStorageSlots - args := []sdtypes.WatchAddressArg{ - { - Address: countAStorageHash.Hex(), - CreatedAt: uint64(contract1.BlockNumber), - }, - } - ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) - Expect(ipldErr).ToNot(HaveOccurred()) + Context("list of watched addresses cleared", func() { + It("indexes state for all the contracts", func() { + operation := statediff.Clear + args := []sdtypes.WatchAddressArg{} + ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) + Expect(ipldErr).ToNot(HaveOccurred()) - // WatchedAddresses = [] - // WatchedStorageSlots = [countB] - // Increment counts - // Contract 1, countA - incErr = integration.IncrementCountA(contract1.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountA1.Add(actualCountA1, big.NewInt(1)) + // WatchedAddresses = [] + // Send eth to all three contract accounts + // Contract 1 + _, txErr = integration.SendEth(contract1.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance1.Add(actualBalance1, big.NewInt(10000000000000000)) - countA1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA1AfterIncrement := new(big.Int).SetBytes(countA1AfterIncrementStorage) - Expect(countA1AfterIncrement.String()).To(Equal(prevCountA1.String())) + balance1AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract1.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(balance1AfterTransfer.String()).To(Equal(actualBalance1.String())) + prevBalance1.Set(actualBalance1) - // Contract 1, countB - incErr = integration.IncrementCountB(contract1.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountB1.Add(actualCountB1, big.NewInt(1)) + // Contract 2 + _, txErr = integration.SendEth(contract2.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance2.Add(actualBalance2, big.NewInt(10000000000000000)) - countB1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB1AfterIncrement := new(big.Int).SetBytes(countB1AfterIncrementStorage) - Expect(countB1AfterIncrement.String()).To(Equal(actualCountB1.String())) - prevCountB1.Set(actualCountB1) + balance2AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract2.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(balance2AfterTransfer.String()).To(Equal(actualBalance2.String())) + prevBalance2.Set(actualBalance2) - // Contract 2, countA - incErr = integration.IncrementCountA(contract2.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountA2.Add(actualCountA2, big.NewInt(1)) + // Contract 3 + _, txErr = integration.SendEth(contract3.Address, "0.01") + time.Sleep(sleepInterval) + Expect(txErr).ToNot(HaveOccurred()) + actualBalance3.Add(actualBalance3, big.NewInt(10000000000000000)) - countA2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA2AfterIncrement := new(big.Int).SetBytes(countA2AfterIncrementStorage) - Expect(countA2AfterIncrement.String()).To(Equal(prevCountA2.String())) + balance3AfterTransfer, err := ipldClient.BalanceAt(ctx, common.HexToAddress(contract3.Address), nil) + Expect(err).ToNot(HaveOccurred()) + Expect(balance3AfterTransfer.String()).To(Equal(actualBalance3.String())) + prevBalance3.Set(actualBalance3) - // Contract 2, countB - incErr = integration.IncrementCountB(contract2.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountB2.Add(actualCountB2, big.NewInt(1)) + // Increment counts + // Contract 1, countA + _, incErr = integration.IncrementCount(contract1.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA1.Add(actualCountA1, big.NewInt(1)) - countB2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB2AfterIncrement := new(big.Int).SetBytes(countB2AfterIncrementStorage) - Expect(countB2AfterIncrement.String()).To(Equal(actualCountB2.String())) - prevCountB2.Set(actualCountB2) - }) - }) + countA1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA1AfterIncrement := new(big.Int).SetBytes(countA1AfterIncrementStorage) + Expect(countA1AfterIncrement.String()).To(Equal(actualCountA1.String())) + prevCountA1.Set(actualCountA1) - Context("one contract and one storage slot being watched", func() { - It("indexes state only for the watched storage slots of watched contracts", func() { - operation := statediff.SetAddresses - args := []sdtypes.WatchAddressArg{ - { - Address: contract1.Address, - CreatedAt: uint64(contract1.BlockNumber), - }, - } - ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) - Expect(ipldErr).ToNot(HaveOccurred()) + // Contract 2, countA + _, incErr = integration.IncrementCount(contract2.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA2.Add(actualCountA2, big.NewInt(1)) - // WatchedAddresses = [Contract1] - // WatchedStorageSlots = [countB] - // Increment counts - // Contract 1, countA - incErr = integration.IncrementCountA(contract1.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountA1.Add(actualCountA1, big.NewInt(1)) + countA2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA2AfterIncrement := new(big.Int).SetBytes(countA2AfterIncrementStorage) + Expect(countA2AfterIncrement.String()).To(Equal(actualCountA2.String())) + prevCountA2.Set(actualCountA2) - countA1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA1AfterIncrement := new(big.Int).SetBytes(countA1AfterIncrementStorage) - Expect(countA1AfterIncrement.String()).To(Equal(prevCountA1.String())) + // Contract 3, countA + _, incErr = integration.IncrementCount(contract3.Address, "A") + time.Sleep(sleepInterval) + Expect(incErr).ToNot(HaveOccurred()) + actualCountA3.Add(actualCountA3, big.NewInt(1)) - // Contract 1, countB - incErr = integration.IncrementCountB(contract1.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountB1.Add(actualCountB1, big.NewInt(1)) - - countB1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB1AfterIncrement := new(big.Int).SetBytes(countB1AfterIncrementStorage) - Expect(countB1AfterIncrement.String()).To(Equal(actualCountB1.String())) - prevCountB1.Set(actualCountB1) - - // Contract 2, countA - incErr = integration.IncrementCountA(contract2.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountA2.Add(actualCountA2, big.NewInt(1)) - - countA2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA2AfterIncrement := new(big.Int).SetBytes(countA2AfterIncrementStorage) - Expect(countA2AfterIncrement.String()).To(Equal(prevCountA2.String())) - - // Contract 2, countB - incErr = integration.IncrementCountB(contract2.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountB2.Add(actualCountB2, big.NewInt(1)) - - countB2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB2AfterIncrement := new(big.Int).SetBytes(countB2AfterIncrementStorage) - Expect(countB2AfterIncrement.String()).To(Equal(prevCountB2.String())) - }) - }) - - Context("list of watched storage slots cleared", func() { - It("indexes state for all the storage slots of watched contracts", func() { - operation := statediff.ClearStorageSlots - args := []sdtypes.WatchAddressArg{} - ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) - Expect(ipldErr).ToNot(HaveOccurred()) - - // WatchedAddresses = [Contract1] - // WatchedStorageSlots = [] - // Increment counts - // Contract 1, countA - incErr = integration.IncrementCountA(contract1.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountA1.Add(actualCountA1, big.NewInt(1)) - - countA1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA1AfterIncrement := new(big.Int).SetBytes(countA1AfterIncrementStorage) - Expect(countA1AfterIncrement.String()).To(Equal(actualCountA1.String())) - prevCountA1.Set(actualCountA1) - - // Contract 1, countB - incErr = integration.IncrementCountB(contract1.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountB1.Add(actualCountB1, big.NewInt(1)) - - countB1AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract1.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB1AfterIncrement := new(big.Int).SetBytes(countB1AfterIncrementStorage) - Expect(countB1AfterIncrement.String()).To(Equal(actualCountB1.String())) - prevCountB1.Set(actualCountB1) - - // Contract 2, countA - incErr = integration.IncrementCountA(contract2.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountA2.Add(actualCountA2, big.NewInt(1)) - - countA2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA2AfterIncrement := new(big.Int).SetBytes(countA2AfterIncrementStorage) - Expect(countA2AfterIncrement.String()).To(Equal(prevCountA2.String())) - - // Contract 2, countB - incErr = integration.IncrementCountB(contract2.Address) - time.Sleep(sleepInterval) - Expect(incErr).ToNot(HaveOccurred()) - actualCountB2.Add(actualCountB2, big.NewInt(1)) - - countB2AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract2.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB2AfterIncrement := new(big.Int).SetBytes(countB2AfterIncrementStorage) - Expect(countB2AfterIncrement.String()).To(Equal(prevCountB2.String())) - }) + countA3AfterIncrementStorage, err := ipldClient.StorageAt(ctx, common.HexToAddress(contract3.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA3AfterIncrement := new(big.Int).SetBytes(countA3AfterIncrementStorage) + Expect(countA3AfterIncrement.String()).To(Equal(actualCountA3.String())) + prevCountA3.Set(actualCountA3) }) }) @@ -851,7 +659,7 @@ var _ = Describe("WatchAddress integration test", func() { }) It("returns an error on args of invalid type", func() { - operation := statediff.AddAddresses + operation := statediff.Add args := []string{"WrongArg"} gethErr := gethRPCClient.Call(nil, gethMethod, operation, args) diff --git a/test/watched_address_gap_filling_service_integration_test.go b/test/watched_address_gap_filling_service_integration_test.go index e81c4d76..a74da5ba 100644 --- a/test/watched_address_gap_filling_service_integration_test.go +++ b/test/watched_address_gap_filling_service_integration_test.go @@ -8,7 +8,6 @@ import ( "time" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/statediff" @@ -18,12 +17,6 @@ import ( integration "github.com/vulcanize/ipld-eth-server/test" ) -var ( - ctx = context.Background() - - ipldClient *ethclient.Client -) - var _ = Describe("Watched address gap filling service integration test", func() { dbWrite, err := strconv.ParseBool(os.Getenv("DB_WRITE")) Expect(err).To(BeNil()) @@ -39,42 +32,33 @@ var _ = Describe("Watched address gap filling service integration test", func() Expect(err).ToNot(HaveOccurred()) ipldEthHttpPath := "http://127.0.0.1:8081" - ipldClient, err = ethclient.Dial(ipldEthHttpPath) + ipldClient, err := ethclient.Dial(ipldEthHttpPath) Expect(err).ToNot(HaveOccurred()) ipldRPCClient, err := rpc.Dial(ipldEthHttpPath) Expect(err).ToNot(HaveOccurred()) var ( + ctx = context.Background() + contractErr error txErr error GLD1 *integration.ContractDeployed - GLD2 *integration.ContractDeployed SLV1 *integration.ContractDeployed SLV2 *integration.ContractDeployed - SLV3 *integration.ContractDeployed - countAIndex string - countBIndex string - countAStorageHash common.Hash - countBStorageHash common.Hash - - totalSupplyIndex = "0x2" - totalSuppyStorageHash = crypto.Keccak256Hash(common.HexToHash(totalSupplyIndex).Bytes()) + countAIndex string + countBIndex string oldCountA1 = big.NewInt(0) - oldCountB1 = big.NewInt(0) oldCountA2 = big.NewInt(0) oldCountB2 = big.NewInt(0) - oldCountA3 = big.NewInt(0) - oldCountB3 = big.NewInt(0) updatedCountA1 = big.NewInt(1) - updatedCountB1 = big.NewInt(1) updatedCountA2 = big.NewInt(1) updatedCountB2 = big.NewInt(1) - updatedCountA3 = big.NewInt(1) - updatedCountB3 = big.NewInt(1) + + SLV2CountBIncrementedAt *integration.CountIncremented ) BeforeEach(func() { @@ -84,7 +68,7 @@ var _ = Describe("Watched address gap filling service integration test", func() }) It("test init", func() { - // Clear out watched addresses | storage slots + // Clear out watched addresses err := integration.ClearWatchedAddresses(gethRPCClient) Expect(err).ToNot(HaveOccurred()) @@ -93,7 +77,7 @@ var _ = Describe("Watched address gap filling service integration test", func() Expect(contractErr).ToNot(HaveOccurred()) // Watch GLD1 contract - operation := statediff.AddAddresses + operation := statediff.Add args := []sdtypes.WatchAddressArg{ { Address: GLD1.Address, @@ -103,49 +87,33 @@ var _ = Describe("Watched address gap filling service integration test", func() ipldErr := ipldRPCClient.Call(nil, ipldMethod, operation, args) Expect(ipldErr).ToNot(HaveOccurred()) - // Deploy a GLD contract - GLD2, contractErr = integration.DeployContract() - Expect(contractErr).ToNot(HaveOccurred()) - - // Deploy three SLV contracts and update storage slots + // Deploy two SLV contracts and update storage slots SLV1, contractErr = integration.DeploySLVContract() Expect(contractErr).ToNot(HaveOccurred()) - txErr = integration.IncrementCountA(SLV1.Address) - Expect(txErr).ToNot(HaveOccurred()) - txErr = integration.IncrementCountB(SLV1.Address) + _, txErr = integration.IncrementCount(SLV1.Address, "A") Expect(txErr).ToNot(HaveOccurred()) SLV2, contractErr = integration.DeploySLVContract() Expect(contractErr).ToNot(HaveOccurred()) - txErr = integration.IncrementCountA(SLV2.Address) + _, txErr = integration.IncrementCount(SLV2.Address, "A") Expect(txErr).ToNot(HaveOccurred()) - txErr = integration.IncrementCountB(SLV2.Address) - Expect(txErr).ToNot(HaveOccurred()) - - SLV3, contractErr = integration.DeploySLVContract() - Expect(contractErr).ToNot(HaveOccurred()) - - txErr = integration.IncrementCountA(SLV3.Address) - Expect(txErr).ToNot(HaveOccurred()) - txErr = integration.IncrementCountB(SLV3.Address) + SLV2CountBIncrementedAt, txErr = integration.IncrementCount(SLV2.Address, "B") Expect(txErr).ToNot(HaveOccurred()) // Get storage slot keys storageSlotAKey, err := integration.GetStorageSlotKey("SLVToken", "countA") Expect(err).ToNot(HaveOccurred()) countAIndex = storageSlotAKey.Key - countAStorageHash = crypto.Keccak256Hash(common.HexToHash(countAIndex).Bytes()) storageSlotBKey, err := integration.GetStorageSlotKey("SLVToken", "countB") Expect(err).ToNot(HaveOccurred()) countBIndex = storageSlotBKey.Key - countBStorageHash = crypto.Keccak256Hash(common.HexToHash(countBIndex).Bytes()) }) defer It("test cleanup", func() { - // Clear out watched addresses | storage slots + // Clear out watched addresses err := integration.ClearWatchedAddresses(gethRPCClient) Expect(err).ToNot(HaveOccurred()) }) @@ -153,19 +121,12 @@ var _ = Describe("Watched address gap filling service integration test", func() Context("previously unwatched contract watched", func() { It("indexes state only for watched contract", func() { // WatchedAddresses = [GLD1] - // WatchedStorageSlots = [] // SLV1, countA countA1Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV1.Address), common.HexToHash(countAIndex), nil) Expect(err).ToNot(HaveOccurred()) countA1 := new(big.Int).SetBytes(countA1Storage) Expect(countA1.String()).To(Equal(oldCountA1.String())) - // SLV1, countB - countB1Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV1.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB1 := new(big.Int).SetBytes(countB1Storage) - Expect(countB1.String()).To(Equal(oldCountB1.String())) - // SLV2, countA countA2Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV2.Address), common.HexToHash(countAIndex), nil) Expect(err).ToNot(HaveOccurred()) @@ -177,18 +138,6 @@ var _ = Describe("Watched address gap filling service integration test", func() Expect(err).ToNot(HaveOccurred()) countB2 := new(big.Int).SetBytes(countB2Storage) Expect(countB2.String()).To(Equal(oldCountB2.String())) - - // SLV3, countA - countA3Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV3.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA3 := new(big.Int).SetBytes(countA3Storage) - Expect(countA3.String()).To(Equal(oldCountA3.String())) - - // SLV3, countB - countB3Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV3.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB3 := new(big.Int).SetBytes(countB3Storage) - Expect(countB3.String()).To(Equal(oldCountB3.String())) }) It("indexes past state on watching a contract", func() { @@ -199,26 +148,19 @@ var _ = Describe("Watched address gap filling service integration test", func() CreatedAt: uint64(SLV1.BlockNumber), }, } - ipldErr := ipldRPCClient.Call(nil, ipldMethod, statediff.AddAddresses, args) + ipldErr := ipldRPCClient.Call(nil, ipldMethod, statediff.Add, args) Expect(ipldErr).ToNot(HaveOccurred()) // Sleep for service interval + few extra seconds time.Sleep(time.Duration(serviceInterval+2) * time.Second) // WatchedAddresses = [GLD1, SLV1] - // WatchedStorageSlots = [] // SLV1, countA countA1Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV1.Address), common.HexToHash(countAIndex), nil) Expect(err).ToNot(HaveOccurred()) countA1 := new(big.Int).SetBytes(countA1Storage) Expect(countA1.String()).To(Equal(updatedCountA1.String())) - // SLV1, countB - countB1Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV1.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB1 := new(big.Int).SetBytes(countB1Storage) - Expect(countB1.String()).To(Equal(updatedCountB1.String())) - // SLV2, countA countA2Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV2.Address), common.HexToHash(countAIndex), nil) Expect(err).ToNot(HaveOccurred()) @@ -230,142 +172,57 @@ var _ = Describe("Watched address gap filling service integration test", func() Expect(err).ToNot(HaveOccurred()) countB2 := new(big.Int).SetBytes(countB2Storage) Expect(countB2.String()).To(Equal(oldCountB2.String())) - - // SLV3, countA - countA3Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV3.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA3 := new(big.Int).SetBytes(countA3Storage) - Expect(countA3.String()).To(Equal(oldCountA3.String())) - - // SLV3, countB - countB3Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV3.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB3 := new(big.Int).SetBytes(countB3Storage) - Expect(countB3.String()).To(Equal(oldCountB3.String())) }) }) - Context("one storage slot being watched", func() { - It("indexer past state only for watched storage slots of watched contracts", func() { - // Watch countA + Context("previously unwatched contract watched (different 'created_at')", func() { + It("indexes past state from 'created_at' onwards on watching a contract", func() { + // Watch SLV2 (created_at -> countB incremented) contract args := []sdtypes.WatchAddressArg{ { - Address: countAStorageHash.Hex(), - CreatedAt: uint64(SLV1.BlockNumber), + Address: SLV2.Address, + CreatedAt: uint64(SLV2CountBIncrementedAt.BlockNumber), }, } - ipldErr := ipldRPCClient.Call(nil, ipldMethod, statediff.AddStorageSlots, args) + ipldErr := ipldRPCClient.Call(nil, ipldMethod, statediff.Add, args) Expect(ipldErr).ToNot(HaveOccurred()) - // Watch SLV2 contract - args = []sdtypes.WatchAddressArg{ + // Sleep for service interval + few extra seconds + time.Sleep(time.Duration(serviceInterval+2) * time.Second) + + // WatchedAddresses = [GLD1, SLV1, SLV2] + // SLV2, countA + countA2Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV2.Address), common.HexToHash(countAIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countA2 := new(big.Int).SetBytes(countA2Storage) + Expect(countA2.String()).To(Equal(oldCountA2.String())) + + // SLV2, countB + countB2Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV2.Address), common.HexToHash(countBIndex), nil) + Expect(err).ToNot(HaveOccurred()) + countB2 := new(big.Int).SetBytes(countB2Storage) + Expect(countB2.String()).To(Equal(updatedCountB2.String())) + }) + + It("indexes missing past state on watching a contract from an earlier 'created_at'", func() { + // Clear out watched addresses + err := integration.ClearWatchedAddresses(gethRPCClient) + Expect(err).ToNot(HaveOccurred()) + + // Watch SLV2 (created_at -> deployment) contract + args := []sdtypes.WatchAddressArg{ { Address: SLV2.Address, CreatedAt: uint64(SLV2.BlockNumber), }, } - ipldErr = ipldRPCClient.Call(nil, ipldMethod, statediff.AddAddresses, args) + ipldErr := ipldRPCClient.Call(nil, ipldMethod, statediff.Add, args) Expect(ipldErr).ToNot(HaveOccurred()) // Sleep for service interval + few extra seconds time.Sleep(time.Duration(serviceInterval+2) * time.Second) - // WatchedAddresses = [GLD1, SLV1, SLV2] - // WatchedStorageSlots = [countA] - // SLV2, countA - countA2Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV2.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA2 := new(big.Int).SetBytes(countA2Storage) - Expect(countA2.String()).To(Equal(updatedCountA2.String())) - - // SLV2, countB - countB2Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV2.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB2 := new(big.Int).SetBytes(countB2Storage) - Expect(countB2.String()).To(Equal(oldCountB2.String())) - - // SLV3, countA - countA3Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV3.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA3 := new(big.Int).SetBytes(countA3Storage) - Expect(countA3.String()).To(Equal(oldCountA3.String())) - - // SLV3, countB - countB3Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV3.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB3 := new(big.Int).SetBytes(countB3Storage) - Expect(countB3.String()).To(Equal(oldCountB3.String())) - }) - }) - - Context("previously unwatched storage slot watched", func() { - It("indexes past state only for watched storage slots updated after created at", func() { - // Watch countB with created_at = SLV3.BlockNumber - args := []sdtypes.WatchAddressArg{ - { - Address: countBStorageHash.Hex(), - CreatedAt: uint64(SLV3.BlockNumber), - }, - } - ipldErr := ipldRPCClient.Call(nil, ipldMethod, statediff.AddStorageSlots, args) - Expect(ipldErr).ToNot(HaveOccurred()) - - // Sleep for service interval + few extra seconds - time.Sleep(time.Duration(serviceInterval+2) * time.Second) - - // WatchedAddresses = [GLD1, SLV1, SLV2] - // WatchedStorageSlots = [countA, countB] (countB -> created_at = SLV3.BlockNumber) - // SLV2, countA - countA2Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV2.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA2 := new(big.Int).SetBytes(countA2Storage) - Expect(countA2.String()).To(Equal(updatedCountA2.String())) - - // SLV2, countB - countB2Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV2.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB2 := new(big.Int).SetBytes(countB2Storage) - Expect(countB2.String()).To(Equal(oldCountB2.String())) - - // SLV3, countA - countA3Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV3.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA3 := new(big.Int).SetBytes(countA3Storage) - Expect(countA3.String()).To(Equal(oldCountA3.String())) - - // SLV3, countB - countB3Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV3.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB3 := new(big.Int).SetBytes(countB3Storage) - Expect(countB3.String()).To(Equal(oldCountB3.String())) - }) - - It("indexes past state for watched storage slots of watched contracts", func() { - // Unwatch countB - args := []sdtypes.WatchAddressArg{ - { - Address: countBStorageHash.Hex(), - CreatedAt: uint64(SLV3.BlockNumber), - }, - } - ipldErr := ipldRPCClient.Call(nil, ipldMethod, statediff.RemoveStorageSlots, args) - Expect(ipldErr).ToNot(HaveOccurred()) - - // Watch countB with created_at = SLV1.BlockNumber - args = []sdtypes.WatchAddressArg{ - { - Address: countBStorageHash.Hex(), - CreatedAt: uint64(SLV1.BlockNumber), - }, - } - ipldErr = ipldRPCClient.Call(nil, ipldMethod, statediff.AddStorageSlots, args) - Expect(ipldErr).ToNot(HaveOccurred()) - - // Sleep for service interval + few extra seconds - time.Sleep(time.Duration(serviceInterval+2) * time.Second) - - // WatchedAddresses = [GLD1, SLV1, SLV2] - // WatchedStorageSlots = [countA, countB] (countB -> created_at = SLV1.BlockNumber) + // WatchedAddresses = [SLV2] // SLV2, countA countA2Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV2.Address), common.HexToHash(countAIndex), nil) Expect(err).ToNot(HaveOccurred()) @@ -377,66 +234,6 @@ var _ = Describe("Watched address gap filling service integration test", func() Expect(err).ToNot(HaveOccurred()) countB2 := new(big.Int).SetBytes(countB2Storage) Expect(countB2.String()).To(Equal(updatedCountB2.String())) - - // SLV3, countA - countA3Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV3.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA3 := new(big.Int).SetBytes(countA3Storage) - Expect(countA3.String()).To(Equal(oldCountA3.String())) - - // SLV3, countB - countB3Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV3.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB3 := new(big.Int).SetBytes(countB3Storage) - Expect(countB3.String()).To(Equal(oldCountB3.String())) }) }) - - Context("contract watched along with other contract and its storage slots", func() { - It("indexes past state for watched storage slots of watched contracts", func() { - // Watch totalSupply - args := []sdtypes.WatchAddressArg{ - { - Address: totalSuppyStorageHash.Hex(), - CreatedAt: uint64(GLD1.BlockNumber), - }, - } - ipldErr := ipldRPCClient.Call(nil, ipldMethod, statediff.AddStorageSlots, args) - Expect(ipldErr).ToNot(HaveOccurred()) - - // Watch GLD2 and SLV3 contracts - args = []sdtypes.WatchAddressArg{ - { - Address: GLD2.Address, - CreatedAt: uint64(GLD2.BlockNumber), - }, - { - Address: SLV3.Address, - CreatedAt: uint64(SLV3.BlockNumber), - }, - } - ipldErr = ipldRPCClient.Call(nil, ipldMethod, statediff.AddAddresses, args) - Expect(ipldErr).ToNot(HaveOccurred()) - - // Sleep for service interval + few extra seconds - time.Sleep(time.Duration(serviceInterval+2) * time.Second) - - // WatchedAddresses = [GLD1, SLV1, SLV2, GLD2, SLV3] - // WatchedStorageSlots = [countA, countB, totalSupply] - // SLV3, countA - countA3Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV3.Address), common.HexToHash(countAIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countA3 := new(big.Int).SetBytes(countA3Storage) - Expect(countA3.String()).To(Equal(updatedCountA3.String())) - - // SLV3, countB - countB3Storage, err := ipldClient.StorageAt(ctx, common.HexToAddress(SLV3.Address), common.HexToHash(countBIndex), nil) - Expect(err).ToNot(HaveOccurred()) - countB3 := new(big.Int).SetBytes(countB3Storage) - Expect(countB3.String()).To(Equal(updatedCountB3.String())) - }) - }) - - // TODO: - // Add test: watched storage slot not having any previously watched related contracts. })