From 3a39e4a58a584af67cca8767b77e7502c06700d9 Mon Sep 17 00:00:00 2001 From: Terence Tsao Date: Tue, 22 May 2018 20:36:38 -0700 Subject: [PATCH 1/3] sharding/contracts: tests for event logs Former-commit-id: 22adee68bad19ba219792eb4f7e91b89999979f7 [formerly 632857d61c53191ea4cc54a25706a4b2b14da39a] Former-commit-id: 17bcf754eb5a14aeaa16407e8c1c0d90579cc209 --- sharding/contracts/sharding_manager_test.go | 64 ++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/sharding/contracts/sharding_manager_test.go b/sharding/contracts/sharding_manager_test.go index 0ca772287..bad4d7431 100644 --- a/sharding/contracts/sharding_manager_test.go +++ b/sharding/contracts/sharding_manager_test.go @@ -92,6 +92,21 @@ func (s *smcTestHelper) registerNotaries(deposit *big.Int, params ...int) error "Got - deposited:%v, index:%v, period:%v ", i, notary.Deposited, notary.PoolIndex, notary.DeregisteredPeriod) } } + // Filter SMC logs by notaryRegistered. + log, err := s.smc.FilterNotaryRegistered(&bind.FilterOpts{}) + if err != nil { + return err + } + // Iterate notaryRegistered logs, compare each address and poolIndex. + for i := 0; i < params[1]; i++ { + log.Next() + if log.Event.Notary != s.testAccounts[i].addr { + return fmt.Errorf("incorrect address in notaryRegistered log. Want: %v Got: %v", s.testAccounts[i].addr, log.Event.Notary) + } + if log.Event.PoolIndex.Cmp(big.NewInt(int64(i))) != 0 { + return fmt.Errorf("incorrect index in notaryRegistered log. Want: %v Got: %v", i, log.Event.Notary) + } + } return nil } @@ -110,6 +125,24 @@ func (s *smcTestHelper) deregisterNotaries(params ...int) error { return fmt.Errorf("Degistered period can not be 0 right after deregistration") } } + // Filter SMC logs by notaryDeregistered. + log, err := s.smc.FilterNotaryDeregistered(&bind.FilterOpts{}) + if err != nil { + return err + } + // Iterate notaryDeregistered logs, compare each address, poolIndex and verify period is set. + for i := 0; i < params[1]; i++ { + log.Next() + if log.Event.Notary != s.testAccounts[i].addr { + return fmt.Errorf("incorrect address in notaryDeregistered log. Want: %v Got: %v", s.testAccounts[i].addr, log.Event.Notary) + } + if log.Event.PoolIndex.Cmp(big.NewInt(int64(i))) != 0 { + return fmt.Errorf("incorrect index in notaryDeregistered log. Want: %v Got: %v", i, log.Event.Notary) + } + if log.Event.DeregisteredPeriod.Cmp(big.NewInt(0)) == 0 { + return fmt.Errorf("incorrect period in notaryDeregistered log. Got: %v", log.Event.DeregisteredPeriod) + } + } return nil } @@ -134,10 +167,25 @@ func (s *smcTestHelper) addHeader(a *testAccount, shard *big.Int, period *big.In if cr.ChunkRoot != [32]byte{chunkRoot} { return fmt.Errorf("Chunkroot mismatched. Want: %v, Got: %v", chunkRoot, cr) } + + // Filter SMC logs by headerAdded. + shardIndex := []*big.Int{shard} + logPeriod := uint64(period.Int64() * sharding.PeriodLength) + log, err := s.smc.FilterHeaderAdded(&bind.FilterOpts{Start: logPeriod}, shardIndex) + if err != nil { + return err + } + log.Next() + if log.Event.ProposerAddress != s.testAccounts[0].addr { + return fmt.Errorf("incorrect proposer address in addHeader log. Want: %v Got: %v", s.testAccounts[0].addr, log.Event.ProposerAddress) + } + if log.Event.ChunkRoot != [32]byte{chunkRoot} { + return fmt.Errorf("chunk root missmatch in addHeader log. Want: %v Got: %v", [32]byte{chunkRoot}, log.Event.ChunkRoot) + } return nil } -// addHeader is a helper function for notary to submit vote on a given header, +// submitVote is a helper function for notary to submit vote on a given header, // it also verifies vote is properly submitted and casted. func (s *smcTestHelper) submitVote(a *testAccount, shard *big.Int, period *big.Int, index *big.Int, chunkRoot uint8) error { _, err := s.smc.SubmitVote(a.txOpts, shard, period, index, [32]byte{chunkRoot}) @@ -153,6 +201,20 @@ func (s *smcTestHelper) submitVote(a *testAccount, shard *big.Int, period *big.I if !v { return fmt.Errorf("Notary's indexd bit did not cast to 1 in index %v", index) } + // Filter SMC logs by submitVote. + shardIndex := []*big.Int{shard} + logPeriod := uint64(period.Int64() * sharding.PeriodLength) + log, err := s.smc.FilterVoteSubmitted(&bind.FilterOpts{Start: logPeriod}, shardIndex) + if err != nil { + return err + } + log.Next() + if log.Event.NotaryAddress != a.addr { + return fmt.Errorf("incorrect notary address in submitVote log. Want: %v Got: %v", s.testAccounts[0].addr, a.addr) + } + if log.Event.ChunkRoot != [32]byte{chunkRoot} { + return fmt.Errorf("chunk root missmatch in submitVote log. Want: %v Got: %v", [32]byte{chunkRoot}, log.Event.ChunkRoot) + } return nil } From fd2124d6c0e43a6547bb44f53a9e6785f548f6dc Mon Sep 17 00:00:00 2001 From: Terence Tsao Date: Tue, 22 May 2018 21:20:56 -0700 Subject: [PATCH 2/3] sharding/contracts: comment for comparison Former-commit-id: f1d5319758ab85bd5cfe63a66e0e8b960bab56a4 [formerly 4b80f9ccb7ce74b702774fc40af2e8cdf35ebb04] Former-commit-id: fe5be3e1449f47bd73b50ea7db2418ec1dd1b16a --- sharding/contracts/sharding_manager_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/sharding/contracts/sharding_manager_test.go b/sharding/contracts/sharding_manager_test.go index bad4d7431..d4d0f4b95 100644 --- a/sharding/contracts/sharding_manager_test.go +++ b/sharding/contracts/sharding_manager_test.go @@ -103,6 +103,7 @@ func (s *smcTestHelper) registerNotaries(deposit *big.Int, params ...int) error if log.Event.Notary != s.testAccounts[i].addr { return fmt.Errorf("incorrect address in notaryRegistered log. Want: %v Got: %v", s.testAccounts[i].addr, log.Event.Notary) } + // Verify notaryPoolIndex is incremental starting from 1st registered Notary. if log.Event.PoolIndex.Cmp(big.NewInt(int64(i))) != 0 { return fmt.Errorf("incorrect index in notaryRegistered log. Want: %v Got: %v", i, log.Event.Notary) } From bac0c74b6bdb06a5390d0d82e831524f29b2a18b Mon Sep 17 00:00:00 2001 From: Terence Tsao Date: Fri, 25 May 2018 10:20:51 -0700 Subject: [PATCH 3/3] sharding/contracts: hash digest instead of byte array Former-commit-id: e6d1149f737e0c21b8ef45712c30265ee699ec96 [formerly 72863268f45b7ca71d9990422cc3bd5b0a75b24d] Former-commit-id: 9059a3971e64abda1939a5aaa6fee8b2e8e7e605 --- sharding/contracts/sharding_manager_test.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/sharding/contracts/sharding_manager_test.go b/sharding/contracts/sharding_manager_test.go index d4d0f4b95..ed94bf954 100644 --- a/sharding/contracts/sharding_manager_test.go +++ b/sharding/contracts/sharding_manager_test.go @@ -73,8 +73,7 @@ func (s *smcTestHelper) fastForward(p int) { } } -// registerNotaries is a helper function register notaries in batch, -// it also verifies notary registration is successful. +// registerNotaries is a helper function register notaries in batch. func (s *smcTestHelper) registerNotaries(deposit *big.Int, params ...int) error { for i := params[0]; i < params[1]; i++ { s.testAccounts[i].txOpts.Value = deposit @@ -111,8 +110,7 @@ func (s *smcTestHelper) registerNotaries(deposit *big.Int, params ...int) error return nil } -// deregisterNotaries is a helper function that deregister notaries in batch, -// it also verifies notary deregistration is successful. +// deregisterNotaries is a helper function that deregister notaries in batch. func (s *smcTestHelper) deregisterNotaries(params ...int) error { for i := params[0]; i < params[1]; i++ { s.testAccounts[i].txOpts.Value = big.NewInt(0) @@ -147,8 +145,7 @@ func (s *smcTestHelper) deregisterNotaries(params ...int) error { return nil } -// addHeader is a helper function to add header to smc, -// it also verifies header is correctly added to the SMC. +// addHeader is a helper function to add header to smc. func (s *smcTestHelper) addHeader(a *testAccount, shard *big.Int, period *big.Int, chunkRoot uint8) error { _, err := s.smc.AddHeader(a.txOpts, shard, period, [32]byte{chunkRoot}) if err != nil { @@ -186,8 +183,7 @@ func (s *smcTestHelper) addHeader(a *testAccount, shard *big.Int, period *big.In return nil } -// submitVote is a helper function for notary to submit vote on a given header, -// it also verifies vote is properly submitted and casted. +// submitVote is a helper function for notary to submit vote on a given header. func (s *smcTestHelper) submitVote(a *testAccount, shard *big.Int, period *big.Int, index *big.Int, chunkRoot uint8) error { _, err := s.smc.SubmitVote(a.txOpts, shard, period, index, [32]byte{chunkRoot}) if err != nil { @@ -214,7 +210,7 @@ func (s *smcTestHelper) submitVote(a *testAccount, shard *big.Int, period *big.I return fmt.Errorf("incorrect notary address in submitVote log. Want: %v Got: %v", s.testAccounts[0].addr, a.addr) } if log.Event.ChunkRoot != [32]byte{chunkRoot} { - return fmt.Errorf("chunk root missmatch in submitVote log. Want: %v Got: %v", [32]byte{chunkRoot}, log.Event.ChunkRoot) + return fmt.Errorf("chunk root missmatch in submitVote log. Want: %v Got: %v", common.BytesToHash([]byte{chunkRoot}), common.BytesToHash(log.Event.ChunkRoot[:])) } return nil }