From ea13800459deb82ad5f403f7a454b448bc11949e Mon Sep 17 00:00:00 2001 From: Terence Tsao Date: Wed, 2 May 2018 11:50:31 -0700 Subject: [PATCH] sharding: test for adding multiple headers within same period Former-commit-id: fc458cbf2960c15c0fa689b22b03614578ab8be3 [formerly 715efa2aeaf765dcab6fc87e1c3da729f2447f8a] Former-commit-id: 420db139f99b7cb667ea3d5113d36037a88e0904 --- sharding/contracts/sharding_manager.go | 2 +- sharding/contracts/sharding_manager.sol | 4 +- sharding/contracts/sharding_manager_test.go | 99 +++++++++++++-------- 3 files changed, 66 insertions(+), 39 deletions(-) diff --git a/sharding/contracts/sharding_manager.go b/sharding/contracts/sharding_manager.go index fc51910d6..02830a629 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\":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\":\"\",\"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\":\"Voted\",\"type\":\"event\"}]" // SMCBin is the compiled bytecode used for deploying new contracts. -const SMCBin = `0x608060405234801561001057600080fd5b50610bf8806100206000396000f3006080604052600436106100c45763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630c8da4cc81146100c95780634f33ffa0146100f357806358377bd11461011657806368e9513e1461012b5780636bdd32711461013357806375bd99121461017457806383ceeabe1461019257806397d369a2146101aa5780639910851d146101c2578063a81f4510146101d7578063b09f427e1461020b578063e9e0b68314610226578063f6f67d3614610269575b600080fd5b3480156100d557600080fd5b506100e160043561027e565b60408051918252519081900360200190f35b3480156100ff57600080fd5b50610114600435602435604435606435610290565b005b34801561012257600080fd5b506101146103dd565b610114610508565b34801561013f57600080fd5b50610154600160a060020a03600435166106b7565b604080519384526020840192909252151582820152519081900360600190f35b34801561018057600080fd5b506101146004356024356044356106da565b34801561019e57600080fd5b506100e160043561081b565b3480156101b657600080fd5b506100e160043561082d565b3480156101ce57600080fd5b5061011461083f565b3480156101e357600080fd5b506101ef60043561096b565b60408051600160a060020a039092168252519081900360200190f35b34801561021757600080fd5b506101ef600435602435610993565b34801561023257600080fd5b50610241600435602435610a28565b60408051938452600160a060020a039092166020840152151582820152519081900360600190f35b34801561027557600080fd5b506100e1610a72565b60036020526000908152604090205481565b600160a060020a03331660009081526001602052604081206002015460ff1615156102ba57600080fd5b600085815260046020908152604080832087845290915290205482146102df57600080fd5b6102e98584610a78565b156102f357600080fd5b33600160a060020a03166103078685610993565b600160a060020a03161461031a57600080fd5b6103248584610a98565b9050605a60ff82161061038b576000858152600660209081526040808320879055600482528083208784529091529020600101805474ff00000000000000000000000000000000000000001916740100000000000000000000000000000000000000001790555b6040805183815260208101869052600160a060020a03331681830152905186917f851c50486c052e1ef001debf138975070f2b85ac90df7276fc8eb410675ed644919081900360600190a25050505050565b33600160a060020a038116600090815260016020819052604082209081015460029091015490919060ff16151561041357600080fd5b82600160a060020a031660008381548110151561042c57fe5b600091825260209091200154600160a060020a03161461044b57600080fd5b610453610af3565b50600160a060020a0382166000908152600160205260409020600543049081905561047d82610b16565b600080548390811061048b57fe5b600091825260209182902001805473ffffffffffffffffffffffffffffffffffffffff191690556002805460001901905560408051600160a060020a0386168152918201849052818101839052517f90e5afdc8fd31453dcf6e37154fa117ddf3b0324c96c65015563df9d5e4b5a759181900360600190a1505050565b33600160a060020a03811660009081526001602052604081206002015460ff161561053257600080fd5b34683635c9adc5dea000001461054757600080fd5b61054f610af3565b610557610b87565b156105ba5750600254600080546001810182559080527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e56301805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a038416179055610603565b6105c2610b8e565b9050816000828154811015156105d457fe5b9060005260206000200160006101000a815481600160a060020a030219169083600160a060020a031602179055505b60028054600190810182556040805160608101825260008082526020808301878152838501868152600160a060020a038a168452918690529390912091518255915192810192909255519101805460ff1916911515919091179055600a54811061066f5760018101600a555b60408051600160a060020a03841681526020810183905281517fa4fe15c53db34d35a5117acc26c27a2653dc68e2dadfc21ed211e38b7864d7a7929181900390910190a15050565b600160208190526000918252604090912080549181015460029091015460ff1683565b600083101580156106eb5750606483105b15156106f657600080fd5b60054304821461070557600080fd5b600083815260056020526040902054821161071f57600080fd5b604080516060808201835283825233600160a060020a03908116602080850182815260008688018181528b8252600484528882208b8352845288822097518855915160019097018054925173ffffffffffffffffffffffffffffffffffffffff19909316979095169690961774ff000000000000000000000000000000000000000019167401000000000000000000000000000000000000000091151591909102179092558784526005808352938590204394909404909355835185815290810186905280840192909252915185927f2d0a86178d2fd307b47be157a766e6bee19bc26161c32f9781ee0e818636f09c928290030190a2505050565b60056020526000908152604090205481565b60066020526000908152604090205481565b33600160a060020a038116600090815260016020819052604090912080820154600290910154909160ff90911615151461087857600080fd5b600160a060020a038216600090815260016020526040902054151561089c57600080fd5b600160a060020a038216600090815260016020526040902054613f000160054304116108c757600080fd5b600160a060020a03821660008181526001602081905260408083208381559182018390556002909101805460ff1916905551683635c9adc5dea000009082818181858883f19350505050158015610922573d6000803e3d6000fd5b5060408051600160a060020a03841681526020810183905281517faee20171b64b7f3360a142659094ce929970d6963dcea8c34a9bf1ece8033680929181900390910190a15050565b600080548290811061097957fe5b600091825260209091200154600160a060020a0316905081565b60008080808080600543049450600b548511156109b457600a5493506109ba565b60095493505b6040805160001960058802018040808352602083018b90528284018c905292519182900360600190912090945090925084908115156109f557fe5b069050600081815481101515610a0757fe5b600091825260209091200154600160a060020a031698975050505050505050565b600460209081526000928352604080842090915290825290208054600190910154600160a060020a0381169074010000000000000000000000000000000000000000900460ff1683565b60025481565b6000918252600360205260409091205460ff9190910360020a9081161490565b600091825260036020526040909120805460ff1960ff93840360020a9091179081167f0100000000000000000000000000000000000000000000000000000000000000601f9290921a82029190910460010192831617905590565b600b546005430490811015610b0757610b13565b600a54600955600b8190555b50565b6008546007541415610b5c57600780546001810182556000919091527fa66cc928b5edb82af9bd49922954155ab7b0942694bea4ce44661d9a8736c68801819055610b7b565b806007600854815481101515610b6e57fe5b6000918252602090912001555b50600880546001019055565b6008541590565b60006001600854111515610ba157600080fd5b600880546000190190819055600780549091908110610bbc57fe5b90600052602060002001549050905600a165627a7a72305820e909b36baed63c3da02155279f0d768187a36927498d91de48a1f9e8b40e25c00029` +const SMCBin = `0x608060405234801561001057600080fd5b50610c01806100206000396000f3006080604052600436106100c45763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630c8da4cc81146100c95780634f33ffa0146100f357806358377bd11461011657806368e9513e1461012b5780636bdd32711461013357806375bd99121461017457806383ceeabe1461019257806397d369a2146101aa5780639910851d146101c2578063a81f4510146101d7578063b09f427e1461020b578063e9e0b68314610226578063f6f67d3614610269575b600080fd5b3480156100d557600080fd5b506100e160043561027e565b60408051918252519081900360200190f35b3480156100ff57600080fd5b50610114600435602435604435606435610290565b005b34801561012257600080fd5b506101146103dd565b610114610508565b34801561013f57600080fd5b50610154600160a060020a03600435166106b7565b604080519384526020840192909252151582820152519081900360600190f35b34801561018057600080fd5b506101146004356024356044356106da565b34801561019e57600080fd5b506100e1600435610824565b3480156101b657600080fd5b506100e1600435610836565b3480156101ce57600080fd5b50610114610848565b3480156101e357600080fd5b506101ef600435610974565b60408051600160a060020a039092168252519081900360200190f35b34801561021757600080fd5b506101ef60043560243561099c565b34801561023257600080fd5b50610241600435602435610a31565b60408051938452600160a060020a039092166020840152151582820152519081900360600190f35b34801561027557600080fd5b506100e1610a7b565b60036020526000908152604090205481565b600160a060020a03331660009081526001602052604081206002015460ff1615156102ba57600080fd5b600085815260046020908152604080832087845290915290205482146102df57600080fd5b6102e98584610a81565b156102f357600080fd5b33600160a060020a0316610307868561099c565b600160a060020a03161461031a57600080fd5b6103248584610aa1565b9050605a60ff82161061038b576000858152600660209081526040808320879055600482528083208784529091529020600101805474ff00000000000000000000000000000000000000001916740100000000000000000000000000000000000000001790555b6040805183815260208101869052600160a060020a03331681830152905186917f851c50486c052e1ef001debf138975070f2b85ac90df7276fc8eb410675ed644919081900360600190a25050505050565b33600160a060020a038116600090815260016020819052604082209081015460029091015490919060ff16151561041357600080fd5b82600160a060020a031660008381548110151561042c57fe5b600091825260209091200154600160a060020a03161461044b57600080fd5b610453610afc565b50600160a060020a0382166000908152600160205260409020600543049081905561047d82610b1f565b600080548390811061048b57fe5b600091825260209182902001805473ffffffffffffffffffffffffffffffffffffffff191690556002805460001901905560408051600160a060020a0386168152918201849052818101839052517f90e5afdc8fd31453dcf6e37154fa117ddf3b0324c96c65015563df9d5e4b5a759181900360600190a1505050565b33600160a060020a03811660009081526001602052604081206002015460ff161561053257600080fd5b34683635c9adc5dea000001461054757600080fd5b61054f610afc565b610557610b90565b156105ba5750600254600080546001810182559080527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e56301805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a038416179055610603565b6105c2610b97565b9050816000828154811015156105d457fe5b9060005260206000200160006101000a815481600160a060020a030219169083600160a060020a031602179055505b60028054600190810182556040805160608101825260008082526020808301878152838501868152600160a060020a038a168452918690529390912091518255915192810192909255519101805460ff1916911515919091179055600a54811061066f5760018101600a555b60408051600160a060020a03841681526020810183905281517fa4fe15c53db34d35a5117acc26c27a2653dc68e2dadfc21ed211e38b7864d7a7929181900390910190a15050565b600160208190526000918252604090912080549181015460029091015460ff1683565b600083101580156106eb5750606483105b15156106f657600080fd5b60054304821461070557600080fd5b600083815260056020526040902054821161071f57600080fd5b604080516060808201835283825233600160a060020a03908116602080850182815260008688018181528b8252600484528882208b8352845288822097518855915160019097018054925173ffffffffffffffffffffffffffffffffffffffff19909316979095169690961774ff000000000000000000000000000000000000000019167401000000000000000000000000000000000000000091151591909102179092558784526005808352858520439190910490556003825284842093909355835185815290810186905280840192909252915185927f2d0a86178d2fd307b47be157a766e6bee19bc26161c32f9781ee0e818636f09c928290030190a2505050565b60056020526000908152604090205481565b60066020526000908152604090205481565b33600160a060020a038116600090815260016020819052604090912080820154600290910154909160ff90911615151461088157600080fd5b600160a060020a03821660009081526001602052604090205415156108a557600080fd5b600160a060020a038216600090815260016020526040902054613f000160054304116108d057600080fd5b600160a060020a03821660008181526001602081905260408083208381559182018390556002909101805460ff1916905551683635c9adc5dea000009082818181858883f1935050505015801561092b573d6000803e3d6000fd5b5060408051600160a060020a03841681526020810183905281517faee20171b64b7f3360a142659094ce929970d6963dcea8c34a9bf1ece8033680929181900390910190a15050565b600080548290811061098257fe5b600091825260209091200154600160a060020a0316905081565b60008080808080600543049450600b548511156109bd57600a5493506109c3565b60095493505b6040805160001960058802018040808352602083018b90528284018c905292519182900360600190912090945090925084908115156109fe57fe5b069050600081815481101515610a1057fe5b600091825260209091200154600160a060020a031698975050505050505050565b600460209081526000928352604080842090915290825290208054600190910154600160a060020a0381169074010000000000000000000000000000000000000000900460ff1683565b60025481565b6000918252600360205260409091205460ff9190910360020a9081161490565b600091825260036020526040909120805460ff1960ff93840360020a9091179081167f0100000000000000000000000000000000000000000000000000000000000000601f9290921a82029190910460010192831617905590565b600b546005430490811015610b1057610b1c565b600a54600955600b8190555b50565b6008546007541415610b6557600780546001810182556000919091527fa66cc928b5edb82af9bd49922954155ab7b0942694bea4ce44661d9a8736c68801819055610b84565b806007600854815481101515610b7757fe5b6000918252602090912001555b50600880546001019055565b6008541590565b60006001600854111515610baa57600080fd5b600880546000190190819055600780549091908110610bc557fe5b90600052602060002001549050905600a165627a7a72305820e489c3a71c7e5d17a9de496d26c34576878a7a182ee5dd35db62447cd210575c0029` // 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) { diff --git a/sharding/contracts/sharding_manager.sol b/sharding/contracts/sharding_manager.sol index c27752f6e..ba2402f47 100644 --- a/sharding/contracts/sharding_manager.sol +++ b/sharding/contracts/sharding_manager.sol @@ -180,6 +180,7 @@ contract SMC { }); lastSubmittedCollation[_shardId] = block.number / PERIOD_LENGTH; + delete currentVote[_shardId]; emit HeaderAdded(_shardId, _chunkRoot, _period, msg.sender); } @@ -253,5 +254,6 @@ contract SMC { bytes32 mask = bytes32(0xff); currentVote[_shardId] = (currentVote[_shardId] & ~mask) | bytes32(voteCount); return voteCount; - } + } + } \ No newline at end of file diff --git a/sharding/contracts/sharding_manager_test.go b/sharding/contracts/sharding_manager_test.go index 474852610..4273f5075 100644 --- a/sharding/contracts/sharding_manager_test.go +++ b/sharding/contracts/sharding_manager_test.go @@ -564,44 +564,27 @@ func TestContractCreation(t *testing.T) { //} func TestNormalAddHeader(t *testing.T) { - const notaryCount = 2 - var notaryPoolAddr [notaryCount]common.Address - var notaryPoolPrivKeys [notaryCount]*ecdsa.PrivateKey - var txOpts [notaryCount]*bind.TransactOpts - genesis := make(core.GenesisAlloc) + addr := crypto.PubkeyToAddress(mainKey.PublicKey) + backend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: accountBalance2000Eth}}) + txOpts := bind.NewKeyedTransactor(mainKey) + _, _, smc, _ := deploySMCContract(backend, mainKey) - // initialize back end with 2 accounts and each with 2000 eth balances - for i := 0; i < notaryCount; i++ { - key, _ := crypto.GenerateKey() - notaryPoolPrivKeys[i] = key - notaryPoolAddr[i] = crypto.PubkeyToAddress(key.PublicKey) - txOpts[i] = bind.NewKeyedTransactor(key) - genesis[notaryPoolAddr[i]] = core.GenesisAccount{ - Balance: accountBalance2000Eth, - } - } - - backend := backends.NewSimulatedBackend(genesis) - _, _, smc, _ := deploySMCContract(backend, notaryPoolPrivKeys[0]) - - // Fast forward to the next period + // Fast forward to the next period to submit header. Period 1 for i := 0; i < int(sharding.PeriodLength); i++ { backend.Commit() } - // register 2 notaries to SMC - for i := 0; i < notaryCount; i++ { - smc.RegisterNotary(txOpts[i]) - backend.Commit() - } + // Proposer adds header consists of shard 0, period 1 and chunkroot 0xA + period1 := big.NewInt(1) + shard0 := big.NewInt(0) chunkRoot := [32]byte{'A'} - _, err := smc.AddHeader(txOpts[0],big.NewInt(0),big.NewInt(1),chunkRoot) + _, err := smc.AddHeader(txOpts, shard0, period1, chunkRoot) if err != nil { t.Fatalf("Proposer adds header failed: %v", err) } backend.Commit() - p, err := smc.LastSubmittedCollation(&bind.CallOpts{}, big.NewInt(0)) + p, err := smc.LastSubmittedCollation(&bind.CallOpts{}, shard0) if err != nil { t.Fatalf("Can't get last submitted collation's period number:", err) } @@ -609,44 +592,86 @@ func TestNormalAddHeader(t *testing.T) { t.Errorf("Incorrect last period, when header was added. Got:", p) } - cr, err := smc.CollationRecords(&bind.CallOpts{}, big.NewInt(0), big.NewInt(1)) + cr, err := smc.CollationRecords(&bind.CallOpts{}, shard0, period1) if cr.ChunkRoot != chunkRoot { t.Errorf("Chunkroot mismatched. Want: %v, Got: %v", chunkRoot, cr) } - // Fast forward to the next period + // Fast forward to the next period. Period 2 for i := 0; i < int(sharding.PeriodLength); i++ { backend.Commit() } + // Proposer adds header consists of shard 0, period 2 and chunkroot 0xB + period2 := big.NewInt(2) chunkRoot = [32]byte{'B'} - _, err = smc.AddHeader(txOpts[0],big.NewInt(0),big.NewInt(3),chunkRoot) + _, err = smc.AddHeader(txOpts, shard0, period2, chunkRoot) if err != nil { t.Fatalf("Proposer adds header failed: %v", err) } backend.Commit() - p, err = smc.LastSubmittedCollation(&bind.CallOpts{}, big.NewInt(0)) - if p.Cmp(big.NewInt(3)) != 0 { + p, err = smc.LastSubmittedCollation(&bind.CallOpts{}, shard0) + if p.Cmp(big.NewInt(2)) != 0 { t.Errorf("Incorrect last period, when header was added. Got:", p) } - cr, err = smc.CollationRecords(&bind.CallOpts{}, big.NewInt(0), big.NewInt(3)) + cr, err = smc.CollationRecords(&bind.CallOpts{}, shard0, period2) if cr.ChunkRoot != chunkRoot { t.Errorf("Chunkroot mismatched. Want: %v, Got: %v", chunkRoot, cr) } + // Proposer adds header consists of shard 1, period 2 and chunkroot 0xC + shard1 := big.NewInt(1) chunkRoot = [32]byte{'C'} - _, err = smc.AddHeader(txOpts[0],big.NewInt(1),big.NewInt(3),chunkRoot) + _, err = smc.AddHeader(txOpts, shard1, period2, chunkRoot) backend.Commit() - p, err = smc.LastSubmittedCollation(&bind.CallOpts{}, big.NewInt(1)) - if p.Cmp(big.NewInt(3)) != 0 { + p, err = smc.LastSubmittedCollation(&bind.CallOpts{}, shard1) + if p.Cmp(big.NewInt(2)) != 0 { t.Errorf("Incorrect last period, when header was added. Got:", p) } - cr, err = smc.CollationRecords(&bind.CallOpts{}, big.NewInt(1), big.NewInt(3)) + cr, err = smc.CollationRecords(&bind.CallOpts{}, shard1, period2) if cr.ChunkRoot != chunkRoot { t.Errorf("Chunkroot mismatched. Want: %v, Got: %v", cr, chunkRoot) } +} + +func TestAddHeadersInSamePeriod(t *testing.T) { + addr := crypto.PubkeyToAddress(mainKey.PublicKey) + backend := backends.NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: accountBalance2000Eth}}) + txOpts := bind.NewKeyedTransactor(mainKey) + _, _, 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 of shard 0, period 1 and chunkroot 0xA + period1 := big.NewInt(1) + shard0 := big.NewInt(0) + chunkRoot := [32]byte{'A'} + _, err := smc.AddHeader(txOpts, shard0, period1, chunkRoot) + if err != nil { + t.Fatalf("Proposer adds header failed: %v", err) + } + backend.Commit() + + p, err := smc.LastSubmittedCollation(&bind.CallOpts{}, shard0) + if p.Cmp(big.NewInt(1)) != 0 { + t.Errorf("Incorrect last period, when header was added. Got:", p) + } + cr, err := smc.CollationRecords(&bind.CallOpts{}, shard0, period1) + if cr.ChunkRoot != chunkRoot { + t.Errorf("Chunkroot mismatched. Want: %v, Got: %v", chunkRoot, cr) + } + + // Proposer attemps to add another header chunkroot 0xB on the same period for the same shard + chunkRoot = [32]byte{'B'} + _, err = smc.AddHeader(txOpts, shard0, period1, chunkRoot) + if err == nil { + t.Errorf("Proposer is not allowed to add 2 headers within same period") + } } \ No newline at end of file