Skip to content

Commit

Permalink
native: add getCommitteeAddress method
Browse files Browse the repository at this point in the history
Port neo-project/neo#3154.

Close #3334

Signed-off-by: Ekaterina Pavlova <ekt@morphbits.io>
  • Loading branch information
AliceInHunterland committed Mar 22, 2024
1 parent b028c77 commit 77c921a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/fakechain/fakechain.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ func (chain *FakeChain) GetCommittee() (keys.PublicKeys, error) {
panic("TODO")
}

// GetCommitteeAddress implements the Blockchainer interface.
func (chain *FakeChain) GetCommitteeAddress() (util.Uint160, error) {
panic("TODO")
}

// GetContractState implements the Blockchainer interface.
func (chain *FakeChain) GetContractState(hash util.Uint160) *state.Contract {
panic("TODO")
Expand Down
1 change: 1 addition & 0 deletions pkg/compiler/native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func TestNativeHelpersCompile(t *testing.T) {
{"getAllCandidates", nil},
{"getCandidateVote", []string{pub}},
{"getCommittee", nil},
{"getCommitteeAddress", nil},
{"getGasPerBlock", nil},
{"getNextBlockValidators", nil},
{"getRegisterPrice", nil},
Expand Down
8 changes: 8 additions & 0 deletions pkg/core/native/native_neo.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ func newNEO(cfg config.ProtocolConfiguration) *NEO {
md = newMethodAndPrice(n.getCommittee, 1<<16, callflag.ReadStates)
n.AddMethod(md, desc)

desc = newDescriptor("getCommitteeAddress", smartcontract.Hash160Type)
md = newMethodAndPrice(n.getCommitteeAddress, 1<<16, callflag.ReadStates)
n.AddMethod(md, desc)

desc = newDescriptor("getNextBlockValidators", smartcontract.ArrayType)
md = newMethodAndPrice(n.getNextBlockValidators, 1<<16, callflag.ReadStates)
n.AddMethod(md, desc)
Expand Down Expand Up @@ -1071,6 +1075,10 @@ func (n *NEO) getCandidatesCall(ic *interop.Context, _ []stackitem.Item) stackit
return stackitem.NewArray(arr)
}

func (n *NEO) getCommitteeAddress(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
return stackitem.NewByteArray(n.GetCommitteeAddress(ic.DAO).BytesBE())
}

func (n *NEO) getAllCandidatesCall(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
ctx, cancel := context.WithCancel(context.Background())
prefix := []byte{prefixCandidate}
Expand Down
15 changes: 15 additions & 0 deletions pkg/core/native/native_test/neo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/neotest"
"github.com/nspcc-dev/neo-go/pkg/neotest/chain"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util"
Expand Down Expand Up @@ -396,6 +398,19 @@ func TestNEO_RecursiveGASMint(t *testing.T) {
neoValidatorInvoker.Invoke(t, true, "transfer", e.Validator.ScriptHash(), c.Hash, int64(1), nil)
}

func TestNEO_GetCommitteeAddress(t *testing.T) {
neoValidatorInvoker := newNeoValidatorsClient(t)
e := neoValidatorInvoker.Executor
standByCommitteePublicKeys, err := keys.NewPublicKeysFromStrings(e.Chain.GetConfig().StandbyCommittee)
require.NoError(t, err)
sort.Sort(standByCommitteePublicKeys)
expectedCommitteeAddress, err := smartcontract.CreateMajorityMultiSigRedeemScript(standByCommitteePublicKeys)
require.NoError(t, err)
stack, err := neoValidatorInvoker.TestInvoke(t, "getCommitteeAddress")
require.NoError(t, err)
require.Equal(t, hash.Hash160(expectedCommitteeAddress).BytesBE(), stack.Pop().Item().Value().([]byte))
}

func TestNEO_GetAccountState(t *testing.T) {
neoValidatorInvoker := newNeoValidatorsClient(t)
e := neoValidatorInvoker.Executor
Expand Down
6 changes: 6 additions & 0 deletions pkg/interop/native/neo/neo.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,9 @@ func UnclaimedGAS(addr interop.Hash160, end int) int {
func GetAccountState(addr interop.Hash160) *AccountState {
return neogointernal.CallWithToken(Hash, "getAccountState", int(contract.ReadStates), addr).(*AccountState)
}

// GetCommitteeAddress represents `getCommitteeAddress` method of NEO native contract.
func GetCommitteeAddress() interop.Hash160 {
return neogointernal.CallWithToken(Hash, "getCommitteeAddress", int(contract.ReadStates)).(interop.Hash160)

}
5 changes: 5 additions & 0 deletions pkg/rpcclient/neo/neo.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ func (c *ContractReader) GetCommittee() (keys.PublicKeys, error) {
return unwrap.ArrayOfPublicKeys(c.invoker.Call(Hash, "getCommittee"))
}

// GetCommitteeAddress returns the committee address.
func (c *ContractReader) GetCommitteeAddress() (util.Uint160, error) {
return unwrap.Uint160(c.invoker.Call(Hash, "getCommitteeAddress"))
}

// GetNextBlockValidators returns the list of validator keys that will sign the
// next block. This method is mostly useful for historic invocations because the
// RPC protocol provides direct getnextblockvalidators call that provides more
Expand Down

0 comments on commit 77c921a

Please sign in to comment.