Skip to content

Commit

Permalink
Merge branch 'main' into KEE01
Browse files Browse the repository at this point in the history
  • Loading branch information
tkxkd0159 authored Oct 17, 2022
2 parents e6b0f06 + 00ab960 commit 1ae25f1
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/foundation) [\#698](https://github.com/line/lbm-sdk/pull/698) update x/group relevant logic in x/foundation
* (x/auth,bank,foundation,wasm) [\#691](https://github.com/line/lbm-sdk/pull/691) change AccAddressFromBech32 to MustAccAddressFromBech32
* (x/wasm) [\#690](https://github.com/line/lbm-sdk/pull/690) fix to prevent accepting file name
* (x/wasm) [\#690](https://github.com/line/lbm-sdk/pull/690) fix to prevent accepting file name
* (cli) [\#708](https://github.com/line/lbm-sdk/pull/708) In CLI, allow 1 SIGN_MODE_DIRECT signer in transactions with multiple signers.
* (x/bank) [\#716](https://github.com/line/lbm-sdk/pull/716) remove useless DenomMetadata key function


### Bug Fixes
* (x/wasm) [\#453](https://github.com/line/lbm-sdk/pull/453) modify wasm grpc query api path
* (client) [\#476](https://github.com/line/lbm-sdk/pull/476) change the default value of the client output format in the config
Expand All @@ -98,6 +101,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/foundation) [\#687](https://github.com/line/lbm-sdk/pull/687) fix bugs on aborting x/foundation proposals
* (global) [\#694](https://github.com/line/lbm-sdk/pull/694) replace deprecated functions since go 1.16 or 1.17
* (x/bankplus) [\#705](https://github.com/line/lbm-sdk/pull/705) add missing blockedAddr checking in bankplus
* (x/foundation) [\#712](https://github.com/line/lbm-sdk/pull/712) fix x/foundation EndBlocker

### Breaking Changes
* (proto) [\#564](https://github.com/line/lbm-sdk/pull/564) change gRPC path to original cosmos path
Expand Down
55 changes: 47 additions & 8 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,41 @@ func SignWithPrivKey(
return sigV2, nil
}

func checkMultipleSigners(mode signing.SignMode, tx authsigning.Tx) error {
if mode == signing.SignMode_SIGN_MODE_DIRECT &&
len(tx.GetSigners()) > 1 {
return sdkerrors.Wrap(sdkerrors.ErrNotSupported, "Signing in DIRECT mode is only supported for transactions with one signer only")
// countDirectSigners counts the number of DIRECT signers in a signature data.
func countDirectSigners(data signing.SignatureData) int {
switch data := data.(type) {
case *signing.SingleSignatureData:
if data.SignMode == signing.SignMode_SIGN_MODE_DIRECT {
return 1
}

return 0
case *signing.MultiSignatureData:
directSigners := 0
for _, d := range data.Signatures {
directSigners += countDirectSigners(d)
}

return directSigners
default:
panic("unreachable case")
}
}

// checkMultipleSigners checks that there can be maximum one DIRECT signer in a tx.
func checkMultipleSigners(tx authsigning.Tx) error {
directSigners := 0
sigsV2, err := tx.GetSignaturesV2()
if err != nil {
return err
}
for _, sig := range sigsV2 {
directSigners += countDirectSigners(sig.Data)
if directSigners > 1 {
return sdkerrors.ErrNotSupported.Wrap("txs signed with CLI can have maximum 1 DIRECT signer")
}
}

return nil
}

Expand All @@ -341,9 +371,6 @@ func Sign(txf Factory, name string, txBuilder client.TxBuilder, overwriteSig boo
// use the SignModeHandler's default mode if unspecified
signMode = txf.txConfig.SignModeHandler().DefaultMode()
}
if err := checkMultipleSigners(signMode, txBuilder.GetTx()); err != nil {
return err
}

key, err := txf.keybase.Key(name)
if err != nil {
Expand Down Expand Up @@ -373,14 +400,26 @@ func Sign(txf Factory, name string, txBuilder client.TxBuilder, overwriteSig boo
Data: &sigData,
Sequence: txf.Sequence(),
}

var prevSignatures []signing.SignatureV2
if !overwriteSig {
prevSignatures, err = txBuilder.GetTx().GetSignaturesV2()
if err != nil {
return err
}
}
if err := txBuilder.SetSignatures(sig); err != nil {
// Overwrite or append signer infos.
var sigs []signing.SignatureV2
if overwriteSig {
sigs = []signing.SignatureV2{sig}
} else {
sigs = append(prevSignatures, sig)
}
if err := txBuilder.SetSignatures(sigs...); err != nil {
return err
}

if err := checkMultipleSigners(txBuilder.GetTx()); err != nil {
return err
}

Expand Down
26 changes: 20 additions & 6 deletions client/tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,25 +306,39 @@ func TestSign(t *testing.T) {
},

/**** test double sign Direct mode
signing transaction with more than 2 signers should fail in DIRECT mode ****/
signing transaction with 2 or more DIRECT signers should fail in DIRECT mode ****/
{
"direct: should fail to append a signature with different mode",
"direct: should append a DIRECT signature with existing AMINO",
// txb already has 1 AMINO signature
txfDirect, txb, from1, false,
[]cryptotypes.PubKey{},
[]cryptotypes.PubKey{pubKey2, pubKey1},
nil,
},
{
"direct: should fail to sign multi-signers tx",
"direct: should add single DIRECT sig in multi-signers tx",
txfDirect, txb2, from1, false,
[]cryptotypes.PubKey{pubKey1},
nil,
},
{
"direct: should fail to append 2nd DIRECT sig in multi-signers tx",
txfDirect, txb2, from2, false,
[]cryptotypes.PubKey{},
nil,
},
{
"direct: should fail to overwrite multi-signers tx",
txfDirect, txb2, from1, true,
"amino: should append 2nd AMINO sig in multi-signers tx with 1 DIRECT sig",
// txb2 already has 1 DIRECT signature
txfAmino, txb2, from2, false,
[]cryptotypes.PubKey{},
nil,
},
{
"direct: should overwrite multi-signers tx with DIRECT sig",
txfDirect, txb2, from1, true,
[]cryptotypes.PubKey{pubKey1},
nil,
},
}
var prevSigs []signingtypes.SignatureV2
for _, tc := range testCases {
Expand Down
3 changes: 2 additions & 1 deletion x/auth/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
authtypes "github.com/line/lbm-sdk/x/auth/types"
bankcli "github.com/line/lbm-sdk/x/bank/client/testutil"
banktypes "github.com/line/lbm-sdk/x/bank/types"
"github.com/line/lbm-sdk/x/genutil/client/cli"
)

type IntegrationTestSuite struct {
Expand Down Expand Up @@ -1247,7 +1248,7 @@ func (s *IntegrationTestSuite) TestTxWithoutPublicKey() {
unsignedTxFile := testutil.WriteToNewTempFile(s.T(), string(txJSON))

// Sign the file with the unsignedTx.
signedTx, err := TxSignExec(val1.ClientCtx, val1.Address, unsignedTxFile.Name())
signedTx, err := TxSignExec(val1.ClientCtx, val1.Address, unsignedTxFile.Name(), fmt.Sprintf("--%s=true", cli.FlagOverwrite))
s.Require().NoError(err)

// Remove the signerInfo's `public_key` field manually from the signedTx.
Expand Down
72 changes: 72 additions & 0 deletions x/foundation/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,75 @@ func (s *KeeperTestSuite) TestBeginBlocker() {
// s.balance + s.balance * 0.5
s.Require().Equal(sdk.NewDecFromInt(s.balance.Add(s.balance.Quo(sdk.NewInt(2)))), after[0].Amount)
}

func (s *KeeperTestSuite) TestEndBlocker() {
ctx, _ := s.ctx.CacheContext()

// check preconditions
for name, tc := range map[string]struct {
id uint64
status foundation.ProposalStatus
}{
"active proposal": {
s.activeProposal,
foundation.PROPOSAL_STATUS_SUBMITTED,
},
"voted proposal": {
s.votedProposal,
foundation.PROPOSAL_STATUS_SUBMITTED,
},
"withdrawn proposal": {
s.withdrawnProposal,
foundation.PROPOSAL_STATUS_WITHDRAWN,
},
"invalid proposal": {
s.invalidProposal,
foundation.PROPOSAL_STATUS_SUBMITTED,
},
} {
proposal, err := s.keeper.GetProposal(ctx, tc.id)
s.Require().NoError(err, name)
s.Require().NotNil(proposal, name)
s.Require().Equal(tc.status, proposal.Status, name)
}

// voting periods end
votingPeriod := s.keeper.GetFoundationInfo(ctx).GetDecisionPolicy().GetVotingPeriod()
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(votingPeriod))
keeper.EndBlocker(ctx, s.keeper)

for name, tc := range map[string]struct {
id uint64
status foundation.ProposalStatus
}{
"active proposal": {
s.activeProposal,
foundation.PROPOSAL_STATUS_ACCEPTED,
},
"voted proposal": {
s.votedProposal,
foundation.PROPOSAL_STATUS_ACCEPTED,
},
"withdrawn proposal": {
s.withdrawnProposal,
foundation.PROPOSAL_STATUS_WITHDRAWN,
},
"invalid proposal": {
s.invalidProposal,
foundation.PROPOSAL_STATUS_ACCEPTED,
},
} {
proposal, err := s.keeper.GetProposal(ctx, tc.id)
s.Require().NoError(err, name)
s.Require().NotNil(proposal, name)
s.Require().Equal(tc.status, proposal.Status, name)
}

// proposals expire
maxExecutionPeriod := foundation.DefaultConfig().MaxExecutionPeriod
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(maxExecutionPeriod))
keeper.EndBlocker(ctx, s.keeper)

// all proposals must be pruned
s.Require().Empty(s.keeper.GetProposals(ctx))
}
79 changes: 45 additions & 34 deletions x/foundation/keeper/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ var (
poolKey = []byte{0x30}
)

// must be constant
var lenTime = len(sdk.FormatTimeBytes(time.Now()))

// Uint64FromBytes converts a byte array to uint64
func Uint64FromBytes(bz []byte) uint64 {
return binary.BigEndian.Uint64(bz)
Expand All @@ -38,30 +41,36 @@ func Uint64ToBytes(number uint64) []byte {

// memberKey key for a specific member from the store
func memberKey(address sdk.AccAddress) []byte {
key := make([]byte, len(memberKeyPrefix)+len(address))
copy(key, memberKeyPrefix)
copy(key[len(memberKeyPrefix):], address)
prefix := memberKeyPrefix
key := make([]byte, len(prefix)+len(address))

copy(key, prefix)
copy(key[len(prefix):], address)

return key
}

// proposalKey key for a specific proposal from the store
func proposalKey(id uint64) []byte {
prefix := proposalKeyPrefix
idBz := Uint64ToBytes(id)
key := make([]byte, len(prefix)+len(idBz))

copy(key, prefix)
copy(key[len(prefix):], idBz)

key := make([]byte, len(proposalKeyPrefix)+len(idBz))
copy(key, proposalKeyPrefix)
copy(key[len(proposalKeyPrefix):], idBz)
return key
}

func voteKey(proposalID uint64, voter sdk.AccAddress) []byte {
prefix := voteKeyPrefix
idBz := Uint64ToBytes(proposalID)
key := make([]byte, len(voteKeyPrefix)+len(idBz)+len(voter))
key := make([]byte, len(prefix)+len(idBz)+len(voter))

begin := 0
copy(key[begin:], voteKeyPrefix)
copy(key[begin:], prefix)

begin += len(voteKeyPrefix)
begin += len(prefix)
copy(key[begin:], idBz)

begin += len(idBz)
Expand All @@ -70,39 +79,38 @@ func voteKey(proposalID uint64, voter sdk.AccAddress) []byte {
return key
}

func proposalByVPEndKey(id uint64, end time.Time) []byte {
func proposalByVPEndKey(vpEnd time.Time, id uint64) []byte {
prefix := proposalByVPEndKeyPrefix
vpEndBz := sdk.FormatTimeBytes(vpEnd)
idBz := Uint64ToBytes(id)
endBz := sdk.FormatTimeBytes(end)
key := make([]byte, len(proposalByVPEndKeyPrefix)+1+len(idBz)+len(endBz))
key := make([]byte, len(prefix)+lenTime+len(idBz))

begin := 0
copy(key[begin:], proposalByVPEndKeyPrefix)
copy(key[begin:], prefix)

begin += len(proposalByVPEndKeyPrefix)
key[begin] = byte(len(idBz))
begin += len(prefix)
copy(key[begin:], vpEndBz)

begin++
begin += len(vpEndBz)
copy(key[begin:], idBz)

begin += len(idBz)
copy(key[begin:], endBz)

return key
}

// func splitProposalByVPEndKey(key []byte) (proposalID uint64, vpEnd time.Time) {
// begin := len(proposalByVPEndKeyPrefix) + 1
// end := begin + int(key[begin-1]) // uint64
// proposalID = Uint64FromBytes(key[begin:end])
func splitProposalByVPEndKey(key []byte) (vpEnd time.Time, id uint64) {
prefix := proposalByVPEndKeyPrefix
begin := len(prefix)
end := begin + lenTime
vpEnd, err := sdk.ParseTimeBytes(key[begin:end])
if err != nil {
panic(err)
}

// begin = end
// vpEnd, err := sdk.ParseTimeBytes(key[begin:])
// if err != nil {
// panic(err)
// }
begin = end
id = Uint64FromBytes(key[begin:])

// return
// }
return
}

func grantKey(grantee sdk.AccAddress, url string) []byte {
prefix := grantKeyPrefixByGrantee(grantee)
Expand All @@ -115,12 +123,13 @@ func grantKey(grantee sdk.AccAddress, url string) []byte {
}

func grantKeyPrefixByGrantee(grantee sdk.AccAddress) []byte {
key := make([]byte, len(grantKeyPrefix)+1+len(grantee))
prefix := grantKeyPrefix
key := make([]byte, len(prefix)+1+len(grantee))

begin := 0
copy(key[begin:], grantKeyPrefix)
copy(key[begin:], prefix)

begin += len(grantKeyPrefix)
begin += len(prefix)
key[begin] = byte(len(grantee))

begin++
Expand All @@ -130,7 +139,9 @@ func grantKeyPrefixByGrantee(grantee sdk.AccAddress) []byte {
}

func splitGrantKey(key []byte) (grantee sdk.AccAddress, url string) {
begin := len(grantKeyPrefix) + 1
prefix := grantKeyPrefix

begin := len(prefix) + 1
end := begin + int(key[begin-1])
grantee = key[begin:end]

Expand Down
Loading

0 comments on commit 1ae25f1

Please sign in to comment.