Skip to content

Commit

Permalink
Merge pull request #107 from Zilliqa/develop
Browse files Browse the repository at this point in the history
fix: verifier logic
  • Loading branch information
xiaohuo committed Sep 29, 2021
2 parents 46bdd63 + 4c733f2 commit 2ff222c
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 18 deletions.
6 changes: 4 additions & 2 deletions core/common.go
Expand Up @@ -21,8 +21,10 @@ const (
)

type Pair struct {
First map[uint32]uint32
Second map[uint32]uint32
FirstList []uint32
First map[uint32]uint32
SecondList []uint32
Second map[uint32]uint32
}

type DsBlockHeaderT struct {
Expand Down
42 changes: 30 additions & 12 deletions core/dsblock_header.go
Expand Up @@ -35,15 +35,18 @@ type DsBlockHeader struct {
// Block index, starting from 0 in the genesis block
BlockNum uint64
// Tx Epoch Num then the DS block was generated
EpochNum uint64
GasPrice string
SwInfo SWInfo
EpochNum uint64
GasPrice string
SwInfo SWInfo
PowDSWinnersList []string
// key is (base16) public key
PoWDSWinners map[string]Peer
// (base16) public key
RemoveDSNodePubKeys []string
// todo concrete data type
DSBlockHashSet DSBlockHashSet
DSBlockHashSet DSBlockHashSet

ProposalIds []uint32
GovDSShardVotesMap map[uint32]Pair
}

Expand Down Expand Up @@ -91,6 +94,7 @@ func NewDsBlockHeaderFromDsBlockT(dst *DsBlockT) *DsBlockHeader {
winnermap[dst.Header.PoWWinners[i]] = peer
}

dsBlockHeader.PowDSWinnersList = dst.Header.PoWWinners
dsBlockHeader.PoWDSWinners = winnermap

var removeDSNodePubKeys []string
Expand All @@ -105,28 +109,37 @@ func NewDsBlockHeaderFromDsBlockT(dst *DsBlockT) *DsBlockHeader {

governance := make(map[uint32]Pair, 0)
govs := dst.Header.Governance
var proposals []uint32
for _, gov := range govs {
proposalId := gov.ProposalId
proposals = append(proposals, proposalId)
dsmap := make(map[uint32]uint32, 0)
var dsList []uint32
dsvotes := gov.DSVotes
for _, dsvote := range dsvotes {
dsmap[dsvote.VoteValue] = dsvote.VoteCount
dsList = append(dsList, dsvote.VoteValue)
}

shardmap := make(map[uint32]uint32, 0)
var shardList []uint32
shardvotes := gov.ShardVotes
for _, shardvote := range shardvotes {
shardmap[shardvote.VoteValue] = shardvote.VoteCount
shardList = append(shardList, shardvote.VoteValue)
}

pair := Pair{
First: dsmap,
Second: shardmap,
First: dsmap,
FirstList: dsList,
Second: shardmap,
SecondList: shardList,
}
governance[proposalId] = pair
}

dsBlockHeader.GovDSShardVotesMap = governance
dsBlockHeader.ProposalIds = proposals

dsBlockHeader.BlockHeaderBase.Version = dst.Header.Version
ch := util.DecodeHex(dst.Header.CommitteeHash)
Expand Down Expand Up @@ -165,22 +178,26 @@ func (d *DsBlockHeader) ToProtobuf(concreteVarsOnly bool) *protobuf.ProtoDSBlock
}

var protobufWinners []*protobuf.ProtoDSBlock_DSBlockHeader_PowDSWinners
for key, winner := range d.PoWDSWinners {
for _, winner := range d.PowDSWinnersList {
peer := d.PoWDSWinners[winner]
protobufWinner := &protobuf.ProtoDSBlock_DSBlockHeader_PowDSWinners{
Key: &protobuf.ByteArray{Data: util.DecodeHex(key)},
Val: &protobuf.ByteArray{Data: winner.Serialize()},
Key: &protobuf.ByteArray{Data: util.DecodeHex(winner)},
Val: &protobuf.ByteArray{Data: peer.Serialize()},
}
protobufWinners = append(protobufWinners, protobufWinner)
}

protoDSBlockHeader.Dswinners = protobufWinners

var proposals []*protobuf.ProtoDSBlock_DSBlockHeader_Proposal
for proposal, pair := range d.GovDSShardVotesMap {
for _, proposal := range d.ProposalIds {
pair := d.GovDSShardVotesMap[proposal]
protoproposal := &protobuf.ProtoDSBlock_DSBlockHeader_Proposal{}
protoproposal.Proposalid = proposal

var dsvotes []*protobuf.ProtoDSBlock_DSBlockHeader_Vote
for value, count := range pair.First {
for _, value := range pair.FirstList {
count := pair.First[value]
dsvote := &protobuf.ProtoDSBlock_DSBlockHeader_Vote{
Value: value,
Count: count,
Expand All @@ -189,7 +206,8 @@ func (d *DsBlockHeader) ToProtobuf(concreteVarsOnly bool) *protobuf.ProtoDSBlock
}

var minerVotes []*protobuf.ProtoDSBlock_DSBlockHeader_Vote
for value, count := range pair.Second {
for _, value := range pair.SecondList {
count := pair.Second[value]
minerVote := &protobuf.ProtoDSBlock_DSBlockHeader_Vote{
Value: value,
Count: count,
Expand Down
2 changes: 1 addition & 1 deletion core/txblock.go
Expand Up @@ -27,7 +27,7 @@ type TxBlock struct {
}

func (t *TxBlock) Hash() []byte {
return util.Sha256(t.Serialize())
return util.Sha256(t.BlockHeader.Serialize())
}

func (t *TxBlock) Serialize() []byte {
Expand Down
134 changes: 134 additions & 0 deletions crosschain/polynetwork/cross_chain_manager.go
Expand Up @@ -14,6 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package polynetwork

import (
Expand Down Expand Up @@ -409,3 +410,136 @@ func (p *Proxy) VerifyHeaderAndExecuteTxWithNonce(proof *ProofEntity, rawHeader

return p.callWithNonce(args, "VerifyHeaderAndExecuteTx", nonce)
}

func (p *Proxy) PopulateWhiteListFromContract(addr, val string) (*transaction.Transaction, error) {
args := []core.ContractValue{
{
VName: "addr",
Type: "ByStr20",
Value: addr,
}, {
VName: "val",
Type: "Bool",
Value: val,
},
}

return p.call(args, "PopulateWhiteListFromContract")
}

func (p *Proxy) PopulateWhiteListToContract(addr, val string) (*transaction.Transaction, error) {
args := []core.ContractValue{
{
VName: "addr",
Type: "ByStr20",
Value: addr,
}, {
VName: "val",
Type: "Bool",
Value: val,
},
}

return p.call(args, "PopulateWhiteListToContract")
}

func (p *Proxy) PopulateWhiteListMethod(method, val string) (*transaction.Transaction, error) {
args := []core.ContractValue{
{
VName: "method",
Type: "String",
Value: method,
}, {
VName: "val",
Type: "Bool",
Value: val,
},
}

return p.call(args, "PopulateWhiteListMethod")
}

func (p *Proxy) PopulateConKeepersPublicKeyList(keepers []string) (*transaction.Transaction, error) {
args := []core.ContractValue{{
VName: "keepers",
Type: "List ByStr20",
Value: keepers,
}}

return p.call(args, "PopulateConKeepersPublicKeyList")
}

func (p *Proxy) PopulateCurEpochStartHeight(height string) (*transaction.Transaction, error) {
args := []core.ContractValue{
{
VName: "height",
Type: "Uint32",
Value: height,
},
}

return p.call(args, "PopulateCurEpochStartHeight")
}

func (p *Proxy) PopulateZilToPolyTxHashMap(index, val string) (*transaction.Transaction, error) {
args := []core.ContractValue{
{
VName: "index",
Type: "Uint256",
Value: index,
},
{
VName: "val",
Type: "ByStr32",
Value: val,
},
}

return p.call(args, "PopulateZilToPolyTxHashMap")
}

func (p *Proxy) PopulateZilToPolyTxHashIndex(index string) (*transaction.Transaction, error) {
args := []core.ContractValue{
{
VName: "index",
Type: "Uint256",
Value: index,
},
}

return p.call(args, "PopulateZilToPolyTxHashIndex")
}

func (p *Proxy) PopulateFromChainTxExist(chainId, txId string) (*transaction.Transaction, error) {
args := []core.ContractValue{
{
VName: "chainId",
Type: "Uint64",
Value: chainId,
},
{
VName: "txId",
Type: "ByStr32",
Value: txId,
},
}

return p.call(args, "PopulateFromChainTxExist")
}

func (p *Proxy) PopulateFromChainTxExistWithNonce(chainId, txId, nonce string) (*transaction.Transaction, error) {
args := []core.ContractValue{
{
VName: "chainId",
Type: "Uint64",
Value: chainId,
},
{
VName: "txId",
Type: "ByStr32",
Value: txId,
},
}

return p.callWithNonce(args, "PopulateFromChainTxExist", nonce)
}
27 changes: 27 additions & 0 deletions crosschain/polynetwork/lock_proxy.go
Expand Up @@ -14,6 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package polynetwork

import (
Expand Down Expand Up @@ -153,6 +154,32 @@ func (l *LockProxy) Unlock(txData, fromContractAddr, fromChainId string) (*trans
return l.call(args, "unlock", "0")
}

func (l *LockProxy) Pause() (*transaction.Transaction, error) {
args := []core.ContractValue{}
return l.call(args, "Pause", "0")
}

func (l *LockProxy) UnPause() (*transaction.Transaction, error) {
args := []core.ContractValue{}
return l.call(args, "UnPause", "0")
}

func (l *LockProxy) UpdateAdmin(newAdmin string) (*transaction.Transaction, error) {
args := []core.ContractValue{
{
"newAdmin",
"ByStr20",
newAdmin,
},
}
return l.call(args, "UpdateAdmin", "0")
}

func (l *LockProxy) ClaimAdmin() (*transaction.Transaction, error) {
args := []core.ContractValue{}
return l.call(args, "ClaimAdmin", "0")
}

func (l *LockProxy) call(args []core.ContractValue, transition string, amount string) (*transaction.Transaction, error) {
bech32Addr, err := bech32.ToBech32Address(l.Addr)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions crosschain/polynetwork/proof.go
Expand Up @@ -14,6 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package polynetwork

import (
Expand Down
1 change: 1 addition & 0 deletions crosschain/polynetwork/safe_math.go
Expand Up @@ -15,6 +15,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/

package polynetwork

import "math"
Expand Down
1 change: 1 addition & 0 deletions crosschain/polynetwork/util.go
Expand Up @@ -14,6 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package polynetwork

import (
Expand Down
1 change: 1 addition & 0 deletions crosschain/polynetwork/util_test.go
Expand Up @@ -14,6 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package polynetwork

import (
Expand Down
1 change: 1 addition & 0 deletions crosschain/polynetwork/zero_copy_source.go
Expand Up @@ -15,6 +15,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/

package polynetwork

import (
Expand Down
7 changes: 4 additions & 3 deletions verifier/verifier.go
Expand Up @@ -148,7 +148,7 @@ func (v *Verifier) updateDSCommitteeComposition(selfKeyPub string, dsComm *list.

// 1. get the map of all pow winners from the DS block
winners := dsBlock.BlockHeader.PoWDSWinners
numOfWinners := len(winners)
numOfWinners := len(dsBlock.BlockHeader.PowDSWinnersList)

// 2. get the array of all non-performant nodes to be removed
removeDSNodePubkeys := dsBlock.BlockHeader.RemoveDSNodePubKeys
Expand All @@ -169,9 +169,10 @@ func (v *Verifier) updateDSCommitteeComposition(selfKeyPub string, dsComm *list.
}

// 4. add new winners
for pubKey, peer := range winners {
for _, pubKey := range dsBlock.BlockHeader.PowDSWinnersList {
peer := winners[pubKey]
w := core.PairOfNode{
PubKey: pubKey[2:],
PubKey: pubKey,
Peer: peer,
}
// Place the current winner node's information in front of the DS Committee
Expand Down

0 comments on commit 2ff222c

Please sign in to comment.