Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce composite key to delegate features to each function key #101

Merged
merged 2 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ jobs:
name: run localnet and exit on failure
command: |
set -x
docker run --rm -v "$PWD":/go/src/github.com/tendermint/tendermint -w /go/src/github.com/tendermint/tendermint golang:1.14.1-alpine /bin/sh -c "apk add --update git make gcc libc-dev build-base && make build"
make build-linux
make localnet-start &
./scripts/localnet-blocks-test.sh 40 5 10 localhost

Expand Down Expand Up @@ -370,7 +370,7 @@ jobs:
./scripts/get_nodejs.sh
# build the binaries with a proper version of Go
# Build Tendermint
docker run --rm -v "$PWD":/go/src/github.com/tendermint/tendermint -w /go/src/github.com/tendermint/tendermint golang:1.14.1-alpine /bin/sh -c "apk add --update git make gcc libc-dev build-base && make build"
make build-linux
# Build contract-tests
docker run --rm -v "$PWD":/go/src/github.com/tendermint/tendermint -w /go/src/github.com/tendermint/tendermint ubuntu:20.10 ./scripts/prepare_dredd_test.sh
# This docker image works with go 1.7, we can install here the hook handler that contract-tests is going to use
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- uses: actions/cache@v1
with:
path: ~/go/bin
key: ${{ runner.os }}-go-tm-binary
key: ${{ runner.os }}-go-tm-binary-${{ hashFiles('**/*.go', '**/go.sum', 'Makefile') }}

test_abci_apps:
runs-on: ubuntu-latest
Expand All @@ -43,7 +43,7 @@ jobs:
- uses: actions/cache@v1
with:
path: ~/go/bin
key: ${{ runner.os }}-go-tm-binary
key: ${{ runner.os }}-go-tm-binary-${{ hashFiles('**/*.go', '**/go.sum', 'Makefile') }}
- name: test_abci_apps
run: abci/tests/test_app/test.sh
shell: bash
Expand All @@ -60,7 +60,7 @@ jobs:
- uses: actions/cache@v1
with:
path: ~/go/bin
key: ${{ runner.os }}-go-tm-binary
key: ${{ runner.os }}-go-tm-binary-${{ hashFiles('**/*.go', '**/go.sum', 'Makefile') }}
- run: abci/tests/test_cli/test.sh
shell: bash

Expand All @@ -76,7 +76,7 @@ jobs:
- uses: actions/cache@v1
with:
path: ~/go/bin
key: ${{ runner.os }}-go-tm-binary
key: ${{ runner.os }}-go-tm-binary-${{ hashFiles('**/*.go', '**/go.sum', 'Makefile') }}
- name: test_apps
run: test/app/test.sh
shell: bash
3 changes: 2 additions & 1 deletion CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
- Apps

- P2P Protocol

- Go API

- Blockchain Protocol
- [consensus] [\#101](https:/line/tendermint/pull/101) Introduce composite key to delegate features to each function key

### FEATURES:

Expand Down
35 changes: 34 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,42 @@ build-docker:
### Local testnet using docker ###
###############################################################################

DOCKER_HOME = /go/src/github.com/tendermint/tendermint
DOCKER_CMD = docker run --rm \
-v `pwd`:$(DOCKER_HOME) \
-w $(DOCKER_HOME)
DOCKER_IMG = golang:1.14.6-alpine3.12
APK_CMD = apk add --update --no-cache git make gcc libc-dev build-base curl jq file gmp-dev clang
APK_CMD += && cd crypto/bls/internal/bls-eth-go-binary
APK_CMD += && make CXX=clang++
APK_CMD += && cd $(DOCKER_HOME)
APK_CMD += && go mod edit -replace github.com/herumi/bls-eth-go-binary=./crypto/bls/internal/bls-eth-go-binary
APK_CMD += && make build
APK_CMD += && go mod edit -dropreplace github.com/herumi/bls-eth-go-binary

# Login docker-container for confirmation building linux binary
build-shell:
$(DOCKER_CMD) -it --entrypoint '' ${DOCKER_IMG} /bin/sh
.PHONY: build-shell

# Build linux binary on other platforms

build-linux:
docker run --rm -v `pwd`:/go/src/github.com/tendermint/tendermint -w /go/src/github.com/tendermint/tendermint golang:1.14.1-alpine /bin/sh -c "apk add --update git make gcc libc-dev build-base && make build"
# Download, build and add the BSL local library to modules
if [ ! -d $(SRCPATH)/crypto/bls/internal/bls-eth-go-binary ]; then \
mkdir -p $(SRCPATH)/crypto/bls/internal && \
git clone https:/herumi/mcl $(SRCPATH)/crypto/bls/internal/mcl && \
git clone https:/herumi/bls $(SRCPATH)/crypto/bls/internal/bls && \
git clone https:/herumi/bls-eth-go-binary $(SRCPATH)/crypto/bls/internal/bls-eth-go-binary; \
fi

# Build Linux binary
$(DOCKER_CMD) ${DOCKER_IMG} /bin/sh -c "$(APK_CMD)"

# Remove the BLS local library from modules
rm -rf $(SRCPATH)/crypto/bls/internal/mcl
rm -rf $(SRCPATH)/crypto/bls/internal/bls
rm -rf $(SRCPATH)/crypto/bls/internal/bls-eth-go-binary
.PHONY: build-linux

build-docker-localnode:
Expand Down
7 changes: 5 additions & 2 deletions abci/example/kvstore/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package kvstore

import (
"github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/composite"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmtypes "github.com/tendermint/tendermint/types"
)

// RandVal creates one random validator, with a key derived
// from the input value
func RandVal(i int) types.ValidatorUpdate {
pubkey := tmrand.Bytes(32)
pk := composite.GenPrivKey().PubKey()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think choosing which key to use should be handled in one place. The same is true in codes for testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've defined another function that generates a private key for the example and tests.

pubkey := tmtypes.TM2PB.PubKey(pk)
power := tmrand.Uint16() + 1
v := types.Ed25519ValidatorUpdate(pubkey, int64(power))
v := types.NewValidatorUpdate(tmtypes.ABCIPubKeyTypeComposite, pubkey.Data, int64(power))
return v
}

Expand Down
12 changes: 8 additions & 4 deletions abci/example/kvstore/persistent_kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/tendermint/tendermint/abci/example/code"
"github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/libs/log"
tmtypes "github.com/tendermint/tendermint/types"
)
Expand Down Expand Up @@ -205,15 +204,20 @@ func (app *PersistentKVStoreApplication) execValidatorTx(tx []byte) types.Respon
}

// update
return app.updateValidator(types.Ed25519ValidatorUpdate(pubkey, power))
// TODO The type of public key to be restored should be kept its original type.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have any plan to change the type of public key later?
Because you point it as TODO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This TODO is a comment intended for #107. It'll be fixed when #107 fixed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the relevant part, but since there is no NewValidatorUpdate in the develop branch, the PR of 107 needs to wait for the source code of 101 to be merged.

return app.updateValidator(types.NewValidatorUpdate(tmtypes.ABCIPubKeyTypeComposite, pubkey, power))
}

// add, update, or remove a validator
func (app *PersistentKVStoreApplication) updateValidator(v types.ValidatorUpdate) types.ResponseDeliverTx {
key := []byte("val:" + string(v.PubKey.Data))

pubkey := ed25519.PubKeyEd25519{}
copy(pubkey[:], v.PubKey.Data)
pubkey, err := tmtypes.PB2TM.PubKey(v.PubKey)
if err != nil {
return types.ResponseDeliverTx{
Code: code.CodeTypeEncodingError,
Log: fmt.Sprintf("Error encoding validator: %v", err)}
}

if v.Power == 0 {
// remove validator
Expand Down
8 changes: 6 additions & 2 deletions abci/tests/server/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"errors"
"fmt"

"github.com/tendermint/tendermint/crypto/composite"
tmtypes "github.com/tendermint/tendermint/types"

abcicli "github.com/tendermint/tendermint/abci/client"
"github.com/tendermint/tendermint/abci/types"
tmrand "github.com/tendermint/tendermint/libs/rand"
Expand All @@ -14,9 +17,10 @@ func InitChain(client abcicli.Client) error {
total := 10
vals := make([]types.ValidatorUpdate, total)
for i := 0; i < total; i++ {
pubkey := tmrand.Bytes(33)
pk := composite.GenPrivKey().PubKey()
pubkey := tmtypes.TM2PB.PubKey(pk).Data
power := tmrand.Int()
vals[i] = types.Ed25519ValidatorUpdate(pubkey, int64(power))
vals[i] = types.NewValidatorUpdate("composite", pubkey, int64(power))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know ABCIPubKeyTypeComposite is same string const type with "composite".
So, I think it's the better to use ABCIPubKeyTypeComposite if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, I'll fix it.

}
_, err := client.InitChainSync(types.RequestInitChain{
Validators: vals,
Expand Down
8 changes: 6 additions & 2 deletions abci/types/pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ const (
PubKeyEd25519 = "ed25519"
)

func Ed25519ValidatorUpdate(pubkey []byte, power int64) ValidatorUpdate {
func NewValidatorUpdate(keyType string, pubkey []byte, power int64) ValidatorUpdate {
return ValidatorUpdate{
// Address:
PubKey: PubKey{
Type: PubKeyEd25519,
Type: keyType,
Data: pubkey,
},
Power: power,
}
}

func Ed25519ValidatorUpdate(pubkey []byte, power int64) ValidatorUpdate {
return NewValidatorUpdate(PubKeyEd25519, pubkey, power)
}
4 changes: 2 additions & 2 deletions cmd/contract_tests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"strings"

"github.com/tendermint/tendermint/cmd/contract_tests/unmarshaler"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why this import sorting is changed?
lint version is changed? or golang version is changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do different versions of goimports give different results? I'm not sure why it was corrected, but this is the result of applying make format (gofmt) locally.

% go version
go version go1.14.4 darwin/amd64


"github.com/snikch/goodman/hooks"
"github.com/snikch/goodman/transaction"

"github.com/tendermint/tendermint/cmd/contract_tests/unmarshaler"
)

func main() {
Expand Down
5 changes: 3 additions & 2 deletions consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/go-kit/kit/log/term"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"

dbm "github.com/tendermint/tm-db"

abcicli "github.com/tendermint/tendermint/abci/client"
Expand All @@ -27,7 +28,6 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
cfg "github.com/tendermint/tendermint/config"
cstypes "github.com/tendermint/tendermint/consensus/types"
"github.com/tendermint/tendermint/crypto/vrf"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
Expand Down Expand Up @@ -495,7 +495,8 @@ func forceProposer(cs *State, vals []*validatorStub, index []int, height []int64
if j+1 < len(height) && height[j+1] > height[j] {
message := types.MakeRoundHash(currentHash, height[j]-1, round[j])
proof, _ := curVal.PrivValidator.GenerateVRFProof(message)
currentHash, _ = vrf.ProofToHash(proof)
pubKey, _ := curVal.PrivValidator.GetPubKey()
currentHash, _ = pubKey.VRFVerify(proof, message)
}
}
if allMatch {
Expand Down
25 changes: 23 additions & 2 deletions crypto/bls/bls.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ func GenPrivKey() PrivKeyBLS12 {
sigKey := bls.SecretKey{}
sigKey.SetByCSPRNG()
sigKeyBinary := PrivKeyBLS12{}
copy(sigKeyBinary[:], sigKey.Serialize())
binary := sigKey.Serialize()
if len(binary) != PrivKeyBLS12Size {
panic(fmt.Sprintf("unexpected BLS private key size: %d != %d", len(binary), PrivKeyBLS12Size))
}
copy(sigKeyBinary[:], binary)
return sigKeyBinary
}

Expand All @@ -62,6 +66,9 @@ func (privKey PrivKeyBLS12) Bytes() []byte {

// Sign produces a signature on the provided message.
func (privKey PrivKeyBLS12) Sign(msg []byte) ([]byte, error) {
if msg == nil {
panic(fmt.Sprintf("Nil specified as the message"))
}
blsKey := bls.SecretKey{}
err := blsKey.Deserialize(privKey[:])
if err != nil {
Expand All @@ -71,6 +78,11 @@ func (privKey PrivKeyBLS12) Sign(msg []byte) ([]byte, error) {
return sign.Serialize(), nil
}

// VRFProve is not supported in BLS12.
func (privKey PrivKeyBLS12) VRFProve(seed []byte) (crypto.Proof, error) {
return nil, fmt.Errorf("VRF prove is not supported by the BLS12")
}

// PubKey gets the corresponding public key from the private key.
func (privKey PrivKeyBLS12) PubKey() crypto.PubKey {
blsKey := bls.SecretKey{}
Expand All @@ -80,7 +92,11 @@ func (privKey PrivKeyBLS12) PubKey() crypto.PubKey {
}
pubKey := blsKey.GetPublicKey()
pubKeyBinary := PubKeyBLS12{}
copy(pubKeyBinary[:], pubKey.Serialize())
binary := pubKey.Serialize()
if len(binary) != PubKeyBLS12Size {
panic(fmt.Sprintf("unexpected BLS public key size: %d != %d", len(binary), PubKeyBLS12Size))
}
copy(pubKeyBinary[:], binary)
return pubKeyBinary
}

Expand Down Expand Up @@ -130,6 +146,11 @@ func (pubKey PubKeyBLS12) VerifyBytes(msg []byte, sig []byte) bool {
return blsSign.VerifyByte(&blsPubKey, msg)
}

// VRFVerify is not supported in BLS12.
func (pubKey PubKeyBLS12) VRFVerify(proof crypto.Proof, seed []byte) (crypto.Output, error) {
return nil, fmt.Errorf("VRF verify is not supported by the BLS12")
}

func (pubKey PubKeyBLS12) String() string {
return fmt.Sprintf("PubKeyBLS12{%X}", pubKey[:])
}
Expand Down
Loading