Skip to content

Commit

Permalink
Merge pull request #91 from Zilliqa/develop
Browse files Browse the repository at this point in the history
feat: cross chain
  • Loading branch information
xiaohuo committed Jul 3, 2021
2 parents dd0ecad + 58d61a4 commit 7a254f7
Show file tree
Hide file tree
Showing 66 changed files with 6,743 additions and 428 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
@@ -1,6 +1,7 @@
FROM golang:1.12.9
FROM golang:1.15.8-alpine
LABEL maintainer="Ren xiaohuo <lulu@zilliqa.com>"
WORKDIR /app
COPY ./ .
RUN apk add build-base
RUN go test -c -o ./test github.com/Zilliqa/gozilliqa-sdk/provider
RUN CI=true go tool test2json -t ./test -test.v
19 changes: 19 additions & 0 deletions README.md
Expand Up @@ -24,6 +24,25 @@ the section `quick start` first to get a basic understanding before you start to

#### Supports

##### Multisig support

- [x] aggregatedPubKey
- [x] multiVerify
- [x] tx block serialization
- [x] ds block serialization
- [x] verify tx block
- [x] verify ds block

#### State proof

- [x] getStateProof
- [x] verifyStateProof

#### ChainWalker

- [x] traversalBlock
- [x] websocket

##### Account API

- [x] fromFile
Expand Down
5 changes: 3 additions & 2 deletions contract/contract.go
Expand Up @@ -98,7 +98,7 @@ func (c *Contract) DeployTo(network string) (*transaction.Transaction, error) {
return nil, err
}
parameter := DeployParams{
Version: strconv.FormatInt(int64(util.Pack(1, 1)), 10),
Version: strconv.FormatInt(int64(util.Pack(222, 1)), 10),
Nonce: "",
GasPrice: gasPrice,
GasLimit: "40000",
Expand Down Expand Up @@ -129,6 +129,7 @@ func (c *Contract) Deploy(params DeployParams) (*transaction.Transaction, error)
Code: strings.ReplaceAll(c.Code, "/\\", ""),
Data: c.Init,
Status: 0,
Priority: params.Priority,
}

err2 := c.Signer.Sign(tx, *c.Provider)
Expand Down Expand Up @@ -232,7 +233,7 @@ func (c *Contract) CallFor(transition string, args []core.ContractValue, priorit
return nil, err
}
params := CallParams{
Version: strconv.FormatInt(int64(util.Pack(1, 1)), 10),
Version: strconv.FormatInt(int64(util.Pack(222, 1)), 10),
Nonce: "",
GasPrice: gasPrice,
GasLimit: "40000",
Expand Down
1 change: 1 addition & 0 deletions contract/deploy_params.go
Expand Up @@ -23,4 +23,5 @@ type DeployParams struct {
GasPrice string
GasLimit string
SenderPubKey string
Priority bool
}
49 changes: 49 additions & 0 deletions core/account.go
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2021 Zilliqa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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 core

import (
protobuf "github.com/Zilliqa/gozilliqa-sdk/protobuf"
"github.com/golang/protobuf/proto"
)

type AccountBase struct {
Version uint32
Balance uint64
Nonce uint64
StorageRoot []byte
CodeHash []byte
}

func AccountBaseFromBytes(bytes []byte) (*AccountBase, error) {
var protoAccountBase protobuf.ProtoAccountBase
err := proto.Unmarshal(bytes, &protoAccountBase)
if err != nil {
return nil, err
}

var accountBase AccountBase
accountBase.Version = *protoAccountBase.Version

balanceNum := ByteArrayToUint(protoAccountBase.Balance.Data, 0, 16)
accountBase.Balance = balanceNum.Uint64()
accountBase.Nonce = *protoAccountBase.Nonce
accountBase.CodeHash = protoAccountBase.Codehash
accountBase.StorageRoot = protoAccountBase.Storageroot

return &accountBase, nil
}
32 changes: 32 additions & 0 deletions core/account_test.go
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2021 Zilliqa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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 core

import (
"github.com/Zilliqa/gozilliqa-sdk/util"
"testing"
)

func TestAccountBaseFromBytes(t *testing.T) {
bytes := util.DecodeHex("080112120a100000000000000000000000000000000018002220f74e858d851b7035161c66546fc183a5b162a8ee187d10324acb1fa8cf1391ea2a20f95d81f1e266a74b57e3bd6ec484ac9c1b2a006a23a6f3a911ce4cfe73ecd335")
accountBase, err := AccountBaseFromBytes(bytes)
if err != nil {
t.Fatal(err)
}

t.Log(util.EncodeHex(accountBase.StorageRoot))
}
53 changes: 53 additions & 0 deletions core/bignum_serialize.go
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2021 Zilliqa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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 core

import (
"math/big"
)

type BIGNumSerialize struct{}

func (bn *BIGNumSerialize) SetNumber(dst []byte, offset uint, size uint, value *big.Int) []byte {
// check for offset overflow
if offset+size < size {
// overflow detected
return nil
}

bytes := value.Bytes()
actualByteNumber := len(bytes)
if actualByteNumber <= int(size) {
if offset+size > uint(len(dst)) {
newDst := make([]byte, int(offset+size))
copy(newDst, dst)
dst = newDst
}
// pad with zeroes as needed
sizeDiff := size - uint(actualByteNumber)
for i := uint(0); i < sizeDiff; i++ {
dst[offset+i] = 0x00
}

for i := 0; i < actualByteNumber; i++ {
dst[offset+sizeDiff+uint(i)] = bytes[i]
}
} else {
// big num size > declared size
}
return dst
}
58 changes: 58 additions & 0 deletions core/bitvector.go
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2021 Zilliqa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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 core

type BitVector struct{}

func (b *BitVector) GetBitVectorLengthInBytes(lengthInBits uint) uint {
a := lengthInBits & 0x07
if a > 0 {
return (lengthInBits >> 3) + 1
} else {
return lengthInBits >> 3
}
}

func (b *BitVector) GetBitVectorSerializedSize(lengthInBits uint) uint {
return 2 + b.GetBitVectorLengthInBytes(lengthInBits)
}

func (b *BitVector) SetBitVector(dst []byte, offset uint, value []bool) []byte {
lengthNeeded := b.GetBitVectorSerializedSize(uint(len(value)))
if (offset + lengthNeeded) > uint(len(dst)) {
newDst := make([]byte, offset+lengthNeeded)
copy(newDst, dst)
dst = newDst
}

for i := offset; i < offset+lengthNeeded; i++ {
dst[i] = 0x00
}

dst[offset] = byte(len(value) >> 8)
dst[offset+1] = byte(len(value))

index := uint(0)
for _, b := range value {
if b {
dst[offset+2+(index>>3)] |= 1 << (7 - (index & 0x07))
}
index++
}

return dst
}
48 changes: 48 additions & 0 deletions core/blockbase.go
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2021 Zilliqa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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 core

import "github.com/Zilliqa/gozilliqa-sdk/protobuf"

type BlockBase struct {
BlockHash [32]byte
Cosigs CoSignatures
Timestamp uint64
}

func (b *BlockBase) ToProtobuf() *protobuf.ProtoBlockBase {
blockBase := &protobuf.ProtoBlockBase{}
blockBase.Blockhash = b.BlockHash[:]
blockBase.Timestamp = b.Timestamp

cs1 := make([]byte, 0)
cs2 := make([]byte, 0)

cosig := &protobuf.ProtoBlockBase_CoSignatures{
Cs1: &protobuf.ByteArray{
Data: b.Cosigs.CS1.Serialize(cs1, 0),
},
B1: b.Cosigs.B1,
Cs2: &protobuf.ByteArray{
Data: b.Cosigs.CS2.Serialize(cs2, 0),
},
B2: b.Cosigs.B2,
}
blockBase.Cosigs = cosig

return blockBase
}
35 changes: 35 additions & 0 deletions core/blockhashset.go
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2021 Zilliqa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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 core

type DSBlockHashSet struct {
// should be 32 bytes
ShardingHash []byte
ReservedField [128]byte
}

type TxBlockHashSet struct {
// State merkle tree root hash only valid in vacuous epoch
// should be 32 bytes as well
StateRootHash [32]byte
// State Delta Hash on DS
// 32 bytes
DeltaHash [32]byte
// Hash concatenated from all microblock infos
// 32 bytes
MbInfoHash [32]byte
}
34 changes: 34 additions & 0 deletions core/blockheaderbase.go
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2021 Zilliqa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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 core

import "github.com/Zilliqa/gozilliqa-sdk/protobuf"

type BlockHeaderBase struct {
Version uint32
// Hash for the committee that generated the block
CommitteeHash [32]byte
PrevHash [32]byte
}

func (b *BlockHeaderBase) ToProtobuf() *protobuf.ProtoBlockHeaderBase {
protoBlockHeaderBase := &protobuf.ProtoBlockHeaderBase{}
protoBlockHeaderBase.Version = b.Version
protoBlockHeaderBase.Committeehash = b.CommitteeHash[:]
protoBlockHeaderBase.Prevhash = b.PrevHash[:]
return protoBlockHeaderBase
}

0 comments on commit 7a254f7

Please sign in to comment.