fix seqkey uses chain and app

This commit is contained in:
rigel rozanski 2017-07-17 22:50:10 -04:00 committed by Ethan Frey
parent 71276a53b5
commit f43fceeb4d
3 changed files with 28 additions and 15 deletions

View File

@ -38,6 +38,7 @@ func (a Actor) Equals(b Actor) bool {
bytes.Equal(a.Address, b.Address)
}
// Empty checks if the actor is not initialized
func (a Actor) Empty() bool {
return a.ChainID == "" && a.App == "" && len(a.Address) == 0
}
@ -57,19 +58,34 @@ type Context interface {
}
//////////////////////////////// Sort Interface
// USAGE sort.Sort(ByAddress(<actor instance>))
// USAGE sort.Sort(ByAll(<actor instance>))
func (a Actor) String() string {
return fmt.Sprintf("%x", a.Address)
}
// ByAddress implements sort.Interface for []Actor based on
// the Address field.
type ByAddress []Actor
// ByAll implements sort.Interface for []Actor.
// It sorts be the ChainID, followed by the App, followed by the Address
type ByAll []Actor
// Verify the sort interface at compile time
var _ sort.Interface = ByAddress{}
var _ sort.Interface = ByAll{}
func (a ByAddress) Len() int { return len(a) }
func (a ByAddress) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAddress) Less(i, j int) bool { return bytes.Compare(a[i].Address, a[j].Address) == -1 }
func (a ByAll) Len() int { return len(a) }
func (a ByAll) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAll) Less(i, j int) bool {
if a[i].ChainID < a[j].ChainID {
return true
}
if a[i].ChainID > a[j].ChainID {
return false
}
if a[i].App < a[j].App {
return true
}
if a[i].App > a[j].App {
return false
}
return bytes.Compare(a[i].Address, a[j].Address) == -1
}

View File

@ -98,11 +98,10 @@ func (n Tx) getSeqKey() (seqKey []byte) {
// First copy the list of signers to sort as sort is done in place
signers2sort := make([]basecoin.Actor, len(n.Signers))
copy(signers2sort, n.Signers)
sort.Sort(basecoin.ByAddress(n.Signers))
sort.Sort(basecoin.ByAll(n.Signers))
for _, signer := range n.Signers {
// rigel: use signer.Bytes()... instead of signer.Address
seqKey = append(seqKey, signer.Address...)
seqKey = append(seqKey, signer.Bytes()...)
}
//seqKey = merkle.SimpleHashFromBinary(n.Signers)
return

View File

@ -85,15 +85,13 @@ func TestNonce(t *testing.T) {
{true, 2, set321, set321}, // other order is the same
{false, 2, set321, set321}, // no repetition
// signers from different chain and apps
// signers with different chain-IDs and apps from actors
{false, 3, set123, set123Chain2}, // sign with different chain actors
{false, 3, set123, set123App2}, // sign with different app actors
{false, 3, set123, set123MixedChains}, // sign with mixed chain actor
{false, 3, set123, set123MixedApps}, // sign with mixed app actors
// Rigel: this is the problem I was refering to.
// The sig checks are proper. But the seqkey is not unique
// all of these demand 3, as that what is expected for set123
// signers from different chain-IDs and apps, working
{true, 1, set123Chain2, set123Chain2},
{true, 1, set123App2, set123App2},
{true, 1, set123MixedChains, set123MixedChains},