diff --git a/sharding/contracts/sharding_manager.go b/sharding/contracts/sharding_manager.go index b04ae675b..b68e7f480 100644 --- a/sharding/contracts/sharding_manager.go +++ b/sharding/contracts/sharding_manager.go @@ -19,7 +19,7 @@ import ( const SMCABI = "[{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"currentVote\",\"outputs\":[{\"name\":\"\",\"type\":\"bytes32\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_shardId\",\"type\":\"uint256\"},{\"name\":\"_period\",\"type\":\"uint256\"},{\"name\":\"_index\",\"type\":\"uint256\"},{\"name\":\"_chunkRoot\",\"type\":\"bytes32\"}],\"name\":\"submitVote\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"deregisterNotary\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_shardId\",\"type\":\"uint256\"},{\"name\":\"_index\",\"type\":\"uint256\"}],\"name\":\"hasVoted\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"registerNotary\",\"outputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"notaryRegistry\",\"outputs\":[{\"name\":\"deregisteredPeriod\",\"type\":\"uint256\"},{\"name\":\"poolIndex\",\"type\":\"uint256\"},{\"name\":\"deposited\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_shardId\",\"type\":\"uint256\"},{\"name\":\"_period\",\"type\":\"uint256\"},{\"name\":\"_chunkRoot\",\"type\":\"bytes32\"}],\"name\":\"addHeader\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"lastSubmittedCollation\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"lastApprovedCollation\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"releaseNotary\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"notaryPool\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_shardId\",\"type\":\"uint256\"},{\"name\":\"_index\",\"type\":\"uint256\"}],\"name\":\"getNotaryInCommittee\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_shardId\",\"type\":\"uint256\"}],\"name\":\"getVoteCount\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"},{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"collationRecords\",\"outputs\":[{\"name\":\"chunkRoot\",\"type\":\"bytes32\"},{\"name\":\"proposer\",\"type\":\"address\"},{\"name\":\"isElected\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"notaryPoolLength\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"shardId\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"chunkRoot\",\"type\":\"bytes32\"},{\"indexed\":false,\"name\":\"period\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"proposerAddress\",\"type\":\"address\"}],\"name\":\"HeaderAdded\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"notary\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"poolIndex\",\"type\":\"uint256\"}],\"name\":\"NotaryRegistered\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"notary\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"poolIndex\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"deregisteredPeriod\",\"type\":\"uint256\"}],\"name\":\"NotaryDeregistered\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"notary\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"poolIndex\",\"type\":\"uint256\"}],\"name\":\"NotaryReleased\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"shardId\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"chunkRoot\",\"type\":\"bytes32\"},{\"indexed\":false,\"name\":\"period\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"notaryAddress\",\"type\":\"address\"}],\"name\":\"VoteSubmitted\",\"type\":\"event\"}]" // SMCBin is the compiled bytecode used for deploying new contracts. -const SMCBin = `0x608060405234801561001057600080fd5b50610ca0806100206000396000f3006080604052600436106100da5763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630c8da4cc81146100df5780634f33ffa01461010957806358377bd11461012c57806364390ff11461014157806368e9513e146101705780636bdd32711461017857806375bd9912146101b957806383ceeabe146101d757806397d369a2146101ef5780639910851d14610207578063a81f45101461021c578063b09f427e14610250578063b2c2f2e81461026b578063e9e0b68314610283578063f6f67d36146102c6575b600080fd5b3480156100eb57600080fd5b506100f76004356102db565b60408051918252519081900360200190f35b34801561011557600080fd5b5061012a6004356024356044356064356102ed565b005b34801561013857600080fd5b5061012a610493565b34801561014d57600080fd5b5061015c6004356024356105be565b604080519115158252519081900360200190f35b61012a6105e1565b34801561018457600080fd5b50610199600160a060020a0360043516610790565b604080519384526020840192909252151582820152519081900360600190f35b3480156101c557600080fd5b5061012a6004356024356044356107b3565b3480156101e357600080fd5b506100f7600435610905565b3480156101fb57600080fd5b506100f7600435610917565b34801561021357600080fd5b5061012a610929565b34801561022857600080fd5b50610234600435610a55565b60408051600160a060020a039092168252519081900360200190f35b34801561025c57600080fd5b50610234600435602435610a7d565b34801561027757600080fd5b506100f7600435610b12565b34801561028f57600080fd5b5061029e600435602435610b27565b60408051938452600160a060020a039092166020840152151582820152519081900360600190f35b3480156102d257600080fd5b506100f7610b71565b60036020526000908152604090205481565b60008085101580156102ff5750606485105b151561030a57600080fd5b60054304841461031957600080fd5b600085815260056020526040902054841461033357600080fd5b6087831061034057600080fd5b6000858152600460209081526040808320878452909152902054821461036557600080fd5b600160a060020a03331660009081526001602052604090206002015460ff16151561038f57600080fd5b61039985846105be565b156103a357600080fd5b33600160a060020a03166103b78685610a7d565b600160a060020a0316146103ca57600080fd5b6103d48584610b77565b6103dd85610b12565b9050605a8110610441576000858152600660209081526040808320879055600482528083208784529091529020600101805474ff00000000000000000000000000000000000000001916740100000000000000000000000000000000000000001790555b6040805183815260208101869052600160a060020a03331681830152905186917fc99370212b708f699fb6945a17eb34d0fc1ccd5b45d88f4d9682593a45d6e833919081900360600190a25050505050565b33600160a060020a038116600090815260016020819052604082209081015460029091015490919060ff1615156104c957600080fd5b82600160a060020a03166000838154811015156104e257fe5b600091825260209091200154600160a060020a03161461050157600080fd5b610509610b9b565b50600160a060020a0382166000908152600160205260409020600543049081905561053382610bbe565b600080548390811061054157fe5b600091825260209182902001805473ffffffffffffffffffffffffffffffffffffffff191690556002805460001901905560408051600160a060020a0386168152918201849052818101839052517f90e5afdc8fd31453dcf6e37154fa117ddf3b0324c96c65015563df9d5e4b5a759181900360600190a1505050565b60009182526003602052604090912054600160ff9290920360020a900481161490565b33600160a060020a03811660009081526001602052604081206002015460ff161561060b57600080fd5b34683635c9adc5dea000001461062057600080fd5b610628610b9b565b610630610c2f565b156106935750600254600080546001810182559080527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e56301805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0384161790556106dc565b61069b610c36565b9050816000828154811015156106ad57fe5b9060005260206000200160006101000a815481600160a060020a030219169083600160a060020a031602179055505b60028054600190810182556040805160608101825260008082526020808301878152838501868152600160a060020a038a168452918690529390912091518255915192810192909255519101805460ff1916911515919091179055600a5481106107485760018101600a555b60408051600160a060020a03841681526020810183905281517fa4fe15c53db34d35a5117acc26c27a2653dc68e2dadfc21ed211e38b7864d7a7929181900390910190a15050565b600160208190526000918252604090912080549181015460029091015460ff1683565b600083101580156107c45750606483105b15156107cf57600080fd5b6005430482146107de57600080fd5b60008381526005602052604090205482116107f857600080fd5b610800610b9b565b604080516060808201835283825233600160a060020a03908116602080850182815260008688018181528b8252600484528882208b8352845288822097518855915160019097018054925173ffffffffffffffffffffffffffffffffffffffff19909316979095169690961774ff000000000000000000000000000000000000000019167401000000000000000000000000000000000000000091151591909102179092558784526005808352858520439190910490556003825284842093909355835185815290810186905280840192909252915185927f2d0a86178d2fd307b47be157a766e6bee19bc26161c32f9781ee0e818636f09c928290030190a2505050565b60056020526000908152604090205481565b60066020526000908152604090205481565b33600160a060020a038116600090815260016020819052604090912080820154600290910154909160ff90911615151461096257600080fd5b600160a060020a038216600090815260016020526040902054151561098657600080fd5b600160a060020a038216600090815260016020526040902054613f000160054304116109b157600080fd5b600160a060020a03821660008181526001602081905260408083208381559182018390556002909101805460ff1916905551683635c9adc5dea000009082818181858883f19350505050158015610a0c573d6000803e3d6000fd5b5060408051600160a060020a03841681526020810183905281517faee20171b64b7f3360a142659094ce929970d6963dcea8c34a9bf1ece8033680929181900390910190a15050565b6000805482908110610a6357fe5b600091825260209091200154600160a060020a0316905081565b60008080808080600543049450600b54851115610a9e57600a549350610aa4565b60095493505b6040805160001960058802018040808352602083018b90528284018c90529251918290036060019091209094509092508490811515610adf57fe5b069050600081815481101515610af157fe5b600091825260209091200154600160a060020a031698975050505050505050565b60009081526003602052604090205460ff1690565b600460209081526000928352604080842090915290825290208054600190910154600160a060020a0381169074010000000000000000000000000000000000000000900460ff1683565b60025481565b600091825260036020526040909120805460ff9290920360020a9091176001019055565b600b546005430490811015610baf57610bbb565b600a54600955600b8190555b50565b6008546007541415610c0457600780546001810182556000919091527fa66cc928b5edb82af9bd49922954155ab7b0942694bea4ce44661d9a8736c68801819055610c23565b806007600854815481101515610c1657fe5b6000918252602090912001555b50600880546001019055565b6008541590565b60006001600854111515610c4957600080fd5b600880546000190190819055600780549091908110610c6457fe5b90600052602060002001549050905600a165627a7a723058209e7734f55959c6ab2c5139ab8c4ad232e25e9ec5c53f4d3db0cbc291b52289a10029` +const SMCBin = `0x608060405234801561001057600080fd5b50610ca0806100206000396000f3006080604052600436106100da5763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630c8da4cc81146100df5780634f33ffa01461010957806358377bd11461012c57806364390ff11461014157806368e9513e146101705780636bdd32711461017857806375bd9912146101b957806383ceeabe146101d757806397d369a2146101ef5780639910851d14610207578063a81f45101461021c578063b09f427e14610250578063b2c2f2e81461026b578063e9e0b68314610283578063f6f67d36146102c6575b600080fd5b3480156100eb57600080fd5b506100f76004356102db565b60408051918252519081900360200190f35b34801561011557600080fd5b5061012a6004356024356044356064356102ed565b005b34801561013857600080fd5b5061012a610493565b34801561014d57600080fd5b5061015c6004356024356105be565b604080519115158252519081900360200190f35b61012a6105e1565b34801561018457600080fd5b50610199600160a060020a0360043516610790565b604080519384526020840192909252151582820152519081900360600190f35b3480156101c557600080fd5b5061012a6004356024356044356107b3565b3480156101e357600080fd5b506100f7600435610905565b3480156101fb57600080fd5b506100f7600435610917565b34801561021357600080fd5b5061012a610929565b34801561022857600080fd5b50610234600435610a55565b60408051600160a060020a039092168252519081900360200190f35b34801561025c57600080fd5b50610234600435602435610a7d565b34801561027757600080fd5b506100f7600435610b12565b34801561028f57600080fd5b5061029e600435602435610b27565b60408051938452600160a060020a039092166020840152151582820152519081900360600190f35b3480156102d257600080fd5b506100f7610b71565b60036020526000908152604090205481565b60008085101580156102ff5750606485105b151561030a57600080fd5b60054304841461031957600080fd5b600085815260056020526040902054841461033357600080fd5b6087831061034057600080fd5b6000858152600460209081526040808320878452909152902054821461036557600080fd5b600160a060020a03331660009081526001602052604090206002015460ff16151561038f57600080fd5b61039985846105be565b156103a357600080fd5b33600160a060020a03166103b78685610a7d565b600160a060020a0316146103ca57600080fd5b6103d48584610b77565b6103dd85610b12565b9050605a8110610441576000858152600660209081526040808320879055600482528083208784529091529020600101805474ff00000000000000000000000000000000000000001916740100000000000000000000000000000000000000001790555b6040805183815260208101869052600160a060020a03331681830152905186917fc99370212b708f699fb6945a17eb34d0fc1ccd5b45d88f4d9682593a45d6e833919081900360600190a25050505050565b33600160a060020a038116600090815260016020819052604082209081015460029091015490919060ff1615156104c957600080fd5b82600160a060020a03166000838154811015156104e257fe5b600091825260209091200154600160a060020a03161461050157600080fd5b610509610b9b565b50600160a060020a0382166000908152600160205260409020600543049081905561053382610bbe565b600080548390811061054157fe5b600091825260209182902001805473ffffffffffffffffffffffffffffffffffffffff191690556002805460001901905560408051600160a060020a0386168152918201849052818101839052517f90e5afdc8fd31453dcf6e37154fa117ddf3b0324c96c65015563df9d5e4b5a759181900360600190a1505050565b60009182526003602052604090912054600160ff9290920360020a900481161490565b33600160a060020a03811660009081526001602052604081206002015460ff161561060b57600080fd5b34683635c9adc5dea000001461062057600080fd5b610628610b9b565b610630610c2f565b156106935750600254600080546001810182559080527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e56301805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0384161790556106dc565b61069b610c36565b9050816000828154811015156106ad57fe5b9060005260206000200160006101000a815481600160a060020a030219169083600160a060020a031602179055505b60028054600190810182556040805160608101825260008082526020808301878152838501868152600160a060020a038a168452918690529390912091518255915192810192909255519101805460ff1916911515919091179055600a5481106107485760018101600a555b60408051600160a060020a03841681526020810183905281517fa4fe15c53db34d35a5117acc26c27a2653dc68e2dadfc21ed211e38b7864d7a7929181900390910190a15050565b600160208190526000918252604090912080549181015460029091015460ff1683565b600083101580156107c45750606483105b15156107cf57600080fd5b6005430482146107de57600080fd5b60008381526005602052604090205482116107f857600080fd5b610800610b9b565b604080516060808201835283825233600160a060020a03908116602080850182815260008688018181528b8252600484528882208b8352845288822097518855915160019097018054925173ffffffffffffffffffffffffffffffffffffffff19909316979095169690961774ff000000000000000000000000000000000000000019167401000000000000000000000000000000000000000091151591909102179092558784526005808352858520439190910490556003825284842093909355835185815290810186905280840192909252915185927f2d0a86178d2fd307b47be157a766e6bee19bc26161c32f9781ee0e818636f09c928290030190a2505050565b60056020526000908152604090205481565b60066020526000908152604090205481565b33600160a060020a038116600090815260016020819052604090912080820154600290910154909160ff90911615151461096257600080fd5b600160a060020a038216600090815260016020526040902054151561098657600080fd5b600160a060020a038216600090815260016020526040902054613f000160054304116109b157600080fd5b600160a060020a03821660008181526001602081905260408083208381559182018390556002909101805460ff1916905551683635c9adc5dea000009082818181858883f19350505050158015610a0c573d6000803e3d6000fd5b5060408051600160a060020a03841681526020810183905281517faee20171b64b7f3360a142659094ce929970d6963dcea8c34a9bf1ece8033680929181900390910190a15050565b6000805482908110610a6357fe5b600091825260209091200154600160a060020a0316905081565b60008080808080600543049450600b54851115610a9e57600a549350610aa4565b60095493505b6040805160001960058802018040808352602083018b90528284018c90529251918290036060019091209094509092508490811515610adf57fe5b069050600081815481101515610af157fe5b600091825260209091200154600160a060020a031698975050505050505050565b60009081526003602052604090205460ff1690565b600460209081526000928352604080842090915290825290208054600190910154600160a060020a0381169074010000000000000000000000000000000000000000900460ff1683565b60025481565b600091825260036020526040909120805460ff9290920360020a9091176001019055565b600b546005430490811015610baf57610bbb565b600a54600955600b8190555b50565b6008546007541415610c0457600780546001810182556000919091527fa66cc928b5edb82af9bd49922954155ab7b0942694bea4ce44661d9a8736c68801819055610c23565b806007600854815481101515610c1657fe5b6000918252602090912001555b50600880546001019055565b6008541590565b60006001600854111515610c4957600080fd5b600880546000190190819055600780549091908110610c6457fe5b90600052602060002001549050905600a165627a7a723058208dd9521bbeeb6bf4ded3b387567171f24e70da42c28607921836570ab0a254470029` // DeploySMC deploys a new Ethereum contract, binding an instance of SMC to it. func DeploySMC(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *SMC, error) { @@ -178,7 +178,7 @@ func (_SMC *SMCTransactorRaw) Transact(opts *bind.TransactOpts, method string, p // CollationRecords is a free data retrieval call binding the contract method 0xe9e0b683. // -// Solidity: function cgollationRecords( uint256, uint256) constant returns(chunkRoot bytes32, proposer address, isElected bool) +// Solidity: function collationRecords( uint256, uint256) constant returns(chunkRoot bytes32, proposer address, isElected bool) func (_SMC *SMCCaller) CollationRecords(opts *bind.CallOpts, arg0 *big.Int, arg1 *big.Int) (struct { ChunkRoot [32]byte Proposer common.Address diff --git a/sharding/contracts/sharding_manager_test.go b/sharding/contracts/sharding_manager_test.go index 3daa33989..9c08f3176 100644 --- a/sharding/contracts/sharding_manager_test.go +++ b/sharding/contracts/sharding_manager_test.go @@ -420,7 +420,7 @@ func TestNotaryInstantRelease(t *testing.T) { } func TestCommitteeListsAreDifferent(t *testing.T) { - const notaryCount = 10000 + const notaryCount = 1000 var notaryPoolAddr [notaryCount]common.Address var notaryPoolPrivKeys [notaryCount]*ecdsa.PrivateKey var txOpts [notaryCount]*bind.TransactOpts @@ -441,31 +441,27 @@ func TestCommitteeListsAreDifferent(t *testing.T) { backend := backends.NewSimulatedBackend(genesis) _, _, smc, _ := deploySMCContract(backend, notaryPoolPrivKeys[0]) - // register 10000 notaries to SMC + // register 1000 notaries to SMC for i := 0; i < notaryCount; i++ { smc.RegisterNotary(txOpts[i]) backend.Commit() } + numNotaries, _ := smc.NotaryPoolLength(&bind.CallOpts{}) - if numNotaries.Cmp(big.NewInt(10000)) != 0 { + if numNotaries.Cmp(big.NewInt(1000)) != 0 { t.Errorf("Incorrect count from notary pool. Want: 1000, Got: %v", numNotaries) } - // get a list of sampled notaries from shard 0 - var shard0CommitteeList []string - for i := 0; i < int(sharding.NotaryCommitSize); i++ { - addr, _ := smc.GetNotaryInCommittee(&bind.CallOpts{}, big.NewInt(0), big.NewInt(int64(i))) - shard0CommitteeList = append(shard0CommitteeList, addr.String()) - } - - // get a list of sampled notaries from shard 1, verify it's not identical to shard 0 - for i := 0; i < int(sharding.NotaryCommitSize); i++ { - addr, _ := smc.GetNotaryInCommittee(&bind.CallOpts{}, big.NewInt(1), big.NewInt(int64(i))) - if shard0CommitteeList[i] == addr.String() { + // compare sampled first 5 notaries of shard 0 to shard 1, they should not be identical + for i := 0; i < 5; i++ { + addr0, _ := smc.GetNotaryInCommittee(&bind.CallOpts{}, big.NewInt(0), big.NewInt(int64(i))) + addr1, _ := smc.GetNotaryInCommittee(&bind.CallOpts{}, big.NewInt(1), big.NewInt(int64(i))) + if addr0 == addr1 { t.Errorf("Shard 0 committee list is identical to shard 1's committee list") } } + } func TestGetCommitteeWithNonMember(t *testing.T) { @@ -772,7 +768,6 @@ func TestSubmitVote(t *testing.T) { // Check notary 0's vote is correctly casted v, err := smc.HasVoted(&bind.CallOpts{}, shard0, index0) - t.Log(v) if err != nil { t.Fatalf("Check notary's vote failed: %v", err) } @@ -801,4 +796,173 @@ func TestSubmitVote(t *testing.T) { if p.Cmp(big.NewInt(0)) != 0 { t.Errorf("Incorrect period submitted, want: 0, got: %v", p) } + +} + +func TestSubmitVoteTwice(t *testing.T) { + addr := crypto.PubkeyToAddress(mainKey.PublicKey) + backend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: accountBalance2000Eth}}) + txOpts := bind.NewKeyedTransactor(mainKey) + txOpts.Value = notaryDeposit + _, _, smc, _ := deploySMCContract(backend, mainKey) + + // Notary 0 registers + smc.RegisterNotary(txOpts) + backend.Commit() + + // Fast forward to the next period to submit header. Period 1 + for i := 0; i < int(sharding.PeriodLength); i++ { + backend.Commit() + } + + // Proposer adds header consists shard 0, period 1 and chunkroot 0xA + period1 := big.NewInt(1) + shard0 := big.NewInt(0) + index0 := big.NewInt(0) + chunkRoot := [32]byte{'A'} + txOpts.Value = big.NewInt(0) + _, err := smc.AddHeader(txOpts, shard0, period1, chunkRoot) + if err != nil { + t.Fatalf("Proposer adds header failed: %v", err) + } + backend.Commit() + + // Notary 0 votes on header + smc.SubmitVote(txOpts, shard0, period1, index0, chunkRoot) + backend.Commit() + + // Check notary 0's vote is correctly casted + c, _ := smc.GetVoteCount(&bind.CallOpts{}, shard0) + if c.Cmp(big.NewInt(1)) != 0 { + t.Errorf("Incorrect notary vote count, want: 1, got: %v", c) + } + + // Notary 0 votes on header again, it should fail + _, err = smc.SubmitVote(txOpts, shard0, period1, index0, chunkRoot) + if err == nil { + t.Errorf("notary voting twice should have failed") + } + backend.Commit() + + // Check notary 0's vote is correctly casted + c, _ = smc.GetVoteCount(&bind.CallOpts{}, shard0) + if c.Cmp(big.NewInt(1)) != 0 { + t.Errorf("Incorrect notary vote count, want: 1, got: %v", c) + } +} + +func TestSubmitVoteByNonEligibleNotary(t *testing.T) { + addr := crypto.PubkeyToAddress(mainKey.PublicKey) + backend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: accountBalance2000Eth}}) + txOpts := bind.NewKeyedTransactor(mainKey) + txOpts.Value = notaryDeposit + _, _, smc, _ := deploySMCContract(backend, mainKey) + + // Fast forward to the next period to submit header. Period 1 + for i := 0; i < int(sharding.PeriodLength); i++ { + backend.Commit() + } + + // Proposer adds header consists shard 0, period 1 and chunkroot 0xA + period1 := big.NewInt(1) + shard0 := big.NewInt(0) + index0 := big.NewInt(0) + chunkRoot := [32]byte{'A'} + txOpts.Value = big.NewInt(0) + _, err := smc.AddHeader(txOpts, shard0, period1, chunkRoot) + if err != nil { + t.Fatalf("Proposer adds header failed: %v", err) + } + backend.Commit() + + // Unregistered Notary 0 votes on header, it should fail + _, err = smc.SubmitVote(txOpts, shard0, period1, index0, chunkRoot) + backend.Commit() + if err == nil { + t.Errorf("Non registered notary submits vote should have failed") + } + c, _ := smc.GetVoteCount(&bind.CallOpts{}, shard0) + if c.Cmp(big.NewInt(0)) != 0 { + t.Errorf("Incorrect notary vote count, want: 0, got: %v", c) + } +} + +func TestSubmitVoteWithOutAHeader(t *testing.T) { + addr := crypto.PubkeyToAddress(mainKey.PublicKey) + backend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: accountBalance2000Eth}}) + txOpts := bind.NewKeyedTransactor(mainKey) + txOpts.Value = notaryDeposit + _, _, smc, _ := deploySMCContract(backend, mainKey) + + // Notary 0 registers + smc.RegisterNotary(txOpts) + backend.Commit() + + // Fast forward to the next period to submit header. Period 1 + for i := 0; i < int(sharding.PeriodLength); i++ { + backend.Commit() + } + + // Proposer adds header consists shard 0, period 1 and chunkroot 0xA + period1 := big.NewInt(1) + shard0 := big.NewInt(0) + index0 := big.NewInt(0) + chunkRoot := [32]byte{'A'} + txOpts.Value = big.NewInt(0) + + // Notary 0 votes on header, it should fail because no header has added + _, err := smc.SubmitVote(txOpts, shard0, period1, index0, chunkRoot) + if err == nil { + t.Errorf("Notary votes should have failed due to missing header") + } + backend.Commit() + + // Check notary 0's vote is correctly casted + c, _ := smc.GetVoteCount(&bind.CallOpts{}, shard0) + if c.Cmp(big.NewInt(0)) != 0 { + t.Errorf("Incorrect notary vote count, want: 1, got: %v", c) + } +} + +func TestSubmitVoteWithInvalidArgs(t *testing.T) { + addr := crypto.PubkeyToAddress(mainKey.PublicKey) + backend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: accountBalance2000Eth}}) + txOpts := bind.NewKeyedTransactor(mainKey) + txOpts.Value = notaryDeposit + _, _, smc, _ := deploySMCContract(backend, mainKey) + + // Notary 0 registers + smc.RegisterNotary(txOpts) + backend.Commit() + + // Fast forward to the next period to submit header. Period 1 + for i := 0; i < int(sharding.PeriodLength); i++ { + backend.Commit() + } + + // Proposer adds header consists shard 0, period 1 and chunkroot 0xA + period1 := big.NewInt(1) + shard0 := big.NewInt(0) + index0 := big.NewInt(0) + chunkRoot := [32]byte{'A'} + txOpts.Value = big.NewInt(0) + _, err := smc.AddHeader(txOpts, shard0, period1, chunkRoot) + if err != nil { + t.Fatalf("Proposer adds header failed: %v", err) + } + backend.Commit() + + // Notary voting with incorrect period + period2 := big.NewInt(2) + _, err = smc.SubmitVote(txOpts, shard0, period2, index0, chunkRoot) + if err == nil { + t.Errorf("Notary votes should have failed due to incorrect period") + } + + // Notary voting with incorrect chunk root + chunkRootWrong := [32]byte{'B'} + _, err = smc.SubmitVote(txOpts, shard0, period1, index0, chunkRootWrong) + if err == nil { + t.Errorf("Notary votes should have failed due to incorrect chunk root") + } }