Skip to content

Commit

Permalink
Merge pull request #55 from qianbin/fix-authority-get-listed
Browse files Browse the repository at this point in the history
fix(builtin): incorrect 'listed' value when there's only one node
  • Loading branch information
libotony authored Jun 29, 2018
2 parents e723351 + b9152b8 commit a788cfd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
13 changes: 10 additions & 3 deletions builtin/authority/authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ func (a *Authority) setAddressPtr(key thor.Bytes32, addr *thor.Address) {
// Get get candidate by node master address.
func (a *Authority) Get(nodeMaster thor.Address) (listed bool, endorsor thor.Address, identity thor.Bytes32, active bool) {
entry := a.getEntry(nodeMaster)
return entry.IsListed(), entry.Endorsor, entry.Identity, entry.Active
if entry.IsLinked() {
return true, entry.Endorsor, entry.Identity, entry.Active
}
// if it's the only node, IsLinked will be false.
// check whether it's the head.
ptr := a.getAddressPtr(headKey)
listed = ptr != nil && *ptr == nodeMaster
return listed, entry.Endorsor, entry.Identity, entry.Active
}

// Add add a new candidate.
Expand Down Expand Up @@ -105,7 +112,7 @@ func (a *Authority) Add(nodeMaster thor.Address, endorsor thor.Address, identity
// The entry is not removed, but set unlisted and inactive.
func (a *Authority) Revoke(nodeMaster thor.Address) bool {
entry := a.getEntry(nodeMaster)
if !entry.IsListed() {
if !entry.IsLinked() {
return false
}

Expand Down Expand Up @@ -135,7 +142,7 @@ func (a *Authority) Revoke(nodeMaster thor.Address) bool {
// Update update candidate's status.
func (a *Authority) Update(nodeMaster thor.Address, active bool) bool {
entry := a.getEntry(nodeMaster)
if !entry.IsListed() {
if !entry.IsLinked() {
return false
}
entry.Active = active
Expand Down
1 change: 1 addition & 0 deletions builtin/authority/authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestAuthority(t *testing.T) {
expected interface{}
}{
{aut.Add(p1, p1, thor.Bytes32{}), true},
{M(aut.Get(p1)), []interface{}{true, p1, thor.Bytes32{}, true}},
{aut.Add(p2, p2, thor.Bytes32{}), true},
{aut.Add(p3, p3, thor.Bytes32{}), true},
{M(aut.Candidates(big.NewInt(10), thor.MaxBlockProposers)), []interface{}{
Expand Down
2 changes: 1 addition & 1 deletion builtin/authority/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ func (e *entry) IsEmpty() bool {
e.Next == nil
}

func (e *entry) IsListed() bool {
func (e *entry) IsLinked() bool {
return e.Prev != nil || e.Next != nil
}
4 changes: 2 additions & 2 deletions builtin/authority_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func init() {
var nodeMaster common.Address
env.ParseArgs(&nodeMaster)

env.UseGas(thor.SloadGas)
env.UseGas(thor.SloadGas * 2)
listed, endorsor, identity, active := Authority.Native(env.State()).Get(thor.Address(nodeMaster))

return []interface{}{listed, endorsor, identity, active}
Expand All @@ -82,7 +82,7 @@ func init() {
var nodeMaster common.Address
env.ParseArgs(&nodeMaster)

env.UseGas(thor.SloadGas)
env.UseGas(thor.SloadGas * 2)
listed, endorsor, _, _ := Authority.Native(env.State()).Get(thor.Address(nodeMaster))
if !listed {
return []interface{}{false}
Expand Down

0 comments on commit a788cfd

Please sign in to comment.