[#42] Share InnerRingList function between contracts

Define InnerRingList function that calls "innerRingList" method of
particular smart contract. Define InnerRingListViaStorage function
that reads address of smart contract from the storage by key, and
calls InnerRingList with the result. Reuse these functions in all
contracts.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-02-02 22:20:31 +03:00 committed by Alex Vanin
parent 68882b5b3c
commit 8ceba2a7c2
6 changed files with 51 additions and 35 deletions

View file

@ -79,8 +79,7 @@ func balance(hash string, addr []byte) int {
} }
func irList() []common.IRNode { func irList() []common.IRNode {
netmapContractAddr := storage.Get(ctx, netmapKey).([]byte) return common.InnerRingListViaStorage(ctx, netmapKey)
return contract.Call(netmapContractAddr, "innerRingList").([]common.IRNode)
} }
func currentEpoch() int { func currentEpoch() int {

View file

@ -2,7 +2,6 @@ package auditcontract
import ( import (
"github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/crypto" "github.com/nspcc-dev/neo-go/pkg/interop/crypto"
"github.com/nspcc-dev/neo-go/pkg/interop/iterator" "github.com/nspcc-dev/neo-go/pkg/interop/iterator"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime" "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
@ -11,10 +10,6 @@ import (
) )
type ( type (
irNode struct {
key interop.PublicKey
}
auditHeader struct { auditHeader struct {
epoch int epoch int
cid []byte cid []byte
@ -70,15 +65,14 @@ func Init(addrNetmap interop.Hash160) {
} }
func Put(rawAuditResult []byte) bool { func Put(rawAuditResult []byte) bool {
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte) innerRing := common.InnerRingListViaStorage(ctx, netmapContractKey)
innerRing := contract.Call(netmapContractAddr, "innerRingList").([]irNode)
hdr := newAuditHeader(rawAuditResult) hdr := newAuditHeader(rawAuditResult)
presented := false presented := false
for i := range innerRing { for i := range innerRing {
ir := innerRing[i] ir := innerRing[i]
if common.BytesEqual(ir.key, hdr.from) { if common.BytesEqual(ir.PublicKey, hdr.from) {
presented = true presented = true
break break

View file

@ -3,7 +3,6 @@ package balancecontract
import ( import (
"github.com/nspcc-dev/neo-go/pkg/interop" "github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/binary"
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/iterator" "github.com/nspcc-dev/neo-go/pkg/interop/iterator"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime" "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/storage" "github.com/nspcc-dev/neo-go/pkg/interop/storage"
@ -109,8 +108,7 @@ func TransferX(from, to interop.Hash160, amount int, details []byte) bool {
hashTxID []byte // ballot key of the inner ring invocation hashTxID []byte // ballot key of the inner ring invocation
) )
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte) innerRing := irList()
innerRing := contract.Call(netmapContractAddr, "innerRingList").([]common.IRNode)
threshold := len(innerRing)/3*2 + 1 threshold := len(innerRing)/3*2 + 1
irKey := common.InnerRingInvoker(innerRing) irKey := common.InnerRingInvoker(innerRing)
@ -145,8 +143,7 @@ func TransferX(from, to interop.Hash160, amount int, details []byte) bool {
} }
func Lock(txID []byte, from, to interop.Hash160, amount, until int) bool { func Lock(txID []byte, from, to interop.Hash160, amount, until int) bool {
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte) innerRing := irList()
innerRing := contract.Call(netmapContractAddr, "innerRingList").([]common.IRNode)
threshold := len(innerRing)/3*2 + 1 threshold := len(innerRing)/3*2 + 1
irKey := common.InnerRingInvoker(innerRing) irKey := common.InnerRingInvoker(innerRing)
@ -183,8 +180,7 @@ func Lock(txID []byte, from, to interop.Hash160, amount, until int) bool {
} }
func NewEpoch(epochNum int) bool { func NewEpoch(epochNum int) bool {
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte) innerRing := irList()
innerRing := contract.Call(netmapContractAddr, "innerRingList").([]common.IRNode)
threshold := len(innerRing)/3*2 + 1 threshold := len(innerRing)/3*2 + 1
irKey := common.InnerRingInvoker(innerRing) irKey := common.InnerRingInvoker(innerRing)
@ -222,8 +218,7 @@ func NewEpoch(epochNum int) bool {
} }
func Mint(to interop.Hash160, amount int, details []byte) bool { func Mint(to interop.Hash160, amount int, details []byte) bool {
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte) innerRing := irList()
innerRing := contract.Call(netmapContractAddr, "innerRingList").([]common.IRNode)
threshold := len(innerRing)/3*2 + 1 threshold := len(innerRing)/3*2 + 1
irKey := common.InnerRingInvoker(innerRing) irKey := common.InnerRingInvoker(innerRing)
@ -253,8 +248,7 @@ func Mint(to interop.Hash160, amount int, details []byte) bool {
} }
func Burn(from interop.Hash160, amount int, details []byte) bool { func Burn(from interop.Hash160, amount int, details []byte) bool {
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte) innerRing := irList()
innerRing := contract.Call(netmapContractAddr, "innerRingList").([]common.IRNode)
threshold := len(innerRing)/3*2 + 1 threshold := len(innerRing)/3*2 + 1
irKey := common.InnerRingInvoker(innerRing) irKey := common.InnerRingInvoker(innerRing)
@ -411,3 +405,7 @@ func fromKnownContract(caller interop.Hash160) bool {
return common.BytesEqual(caller, containerContractAddr) return common.BytesEqual(caller, containerContractAddr)
} }
func irList() []common.IRNode {
return common.InnerRingListViaStorage(ctx, netmapContractKey)
}

24
common/ir.go Normal file
View file

@ -0,0 +1,24 @@
package common
import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)
const irListMethod = "innerRingList"
// InnerRingList returns list of inner ring nodes through calling
// "innerRingList" method of smart contract.
//
// Address of smart contract is received from storage by key.
func InnerRingListViaStorage(ctx storage.Context, key interface{}) []IRNode {
sc := storage.Get(ctx, key).([]byte)
return InnerRingList(sc)
}
// InnerRingList gets list of inner ring through
// calling "innerRingList" method of smart contract.
func InnerRingList(sc interop.Hash160) []IRNode {
return contract.Call(sc, irListMethod).([]IRNode)
}

View file

@ -83,7 +83,7 @@ func Init(addrNetmap, addrBalance, addrID []byte) {
func Put(container, signature, publicKey []byte) bool { func Put(container, signature, publicKey []byte) bool {
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte) netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte)
innerRing := contract.Call(netmapContractAddr, "innerRingList").([]common.IRNode) innerRing := common.InnerRingList(netmapContractAddr)
threshold := len(innerRing)/3*2 + 1 threshold := len(innerRing)/3*2 + 1
offset := int(container[1]) offset := int(container[1])
@ -149,8 +149,7 @@ func Put(container, signature, publicKey []byte) bool {
} }
func Delete(containerID, signature []byte) bool { func Delete(containerID, signature []byte) bool {
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte) innerRing := irList()
innerRing := contract.Call(netmapContractAddr, "innerRingList").([]common.IRNode)
threshold := len(innerRing)/3*2 + 1 threshold := len(innerRing)/3*2 + 1
ownerID := getOwnerByID(ctx, containerID) ownerID := getOwnerByID(ctx, containerID)
@ -336,8 +335,7 @@ func ListContainerSizes(epoch int) [][]byte {
} }
func ProcessEpoch(epochNum int) { func ProcessEpoch(epochNum int) {
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte) innerRing := irList()
innerRing := contract.Call(netmapContractAddr, "innerRingList").([]common.IRNode)
threshold := len(innerRing)/3*2 + 1 threshold := len(innerRing)/3*2 + 1
irKey := common.InnerRingInvoker(innerRing) irKey := common.InnerRingInvoker(innerRing)
@ -360,8 +358,7 @@ func ProcessEpoch(epochNum int) {
} }
func StartContainerEstimation(epoch int) bool { func StartContainerEstimation(epoch int) bool {
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte) innerRing := irList()
innerRing := contract.Call(netmapContractAddr, "innerRingList").([]common.IRNode)
threshold := len(innerRing)/3*2 + 1 threshold := len(innerRing)/3*2 + 1
irKey := common.InnerRingInvoker(innerRing) irKey := common.InnerRingInvoker(innerRing)
@ -384,8 +381,7 @@ func StartContainerEstimation(epoch int) bool {
} }
func StopContainerEstimation(epoch int) bool { func StopContainerEstimation(epoch int) bool {
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte) innerRing := irList()
innerRing := contract.Call(netmapContractAddr, "innerRingList").([]common.IRNode)
threshold := len(innerRing)/3*2 + 1 threshold := len(innerRing)/3*2 + 1
irKey := common.InnerRingInvoker(innerRing) irKey := common.InnerRingInvoker(innerRing)
@ -595,3 +591,7 @@ func keysToDelete(epoch int) [][]byte {
return results return results
} }
func irList() []common.IRNode {
return common.InnerRingListViaStorage(ctx, netmapContractKey)
}

View file

@ -2,7 +2,6 @@ package neofsidcontract
import ( import (
"github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/binary"
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/crypto" "github.com/nspcc-dev/neo-go/pkg/interop/crypto"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime" "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/storage" "github.com/nspcc-dev/neo-go/pkg/interop/storage"
@ -59,8 +58,7 @@ func AddKey(owner []byte, keys [][]byte) bool {
panic("addKey: incorrect owner") panic("addKey: incorrect owner")
} }
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte) innerRing := irList()
innerRing := contract.Call(netmapContractAddr, "innerRingList").([]common.IRNode)
threshold := len(innerRing)/3*2 + 1 threshold := len(innerRing)/3*2 + 1
irKey := common.InnerRingInvoker(innerRing) irKey := common.InnerRingInvoker(innerRing)
@ -113,8 +111,7 @@ func RemoveKey(owner []byte, keys [][]byte) bool {
panic("removeKey: incorrect owner") panic("removeKey: incorrect owner")
} }
netmapContractAddr := storage.Get(ctx, netmapContractKey).([]byte) innerRing := irList()
innerRing := contract.Call(netmapContractAddr, "innerRingList").([]common.IRNode)
threshold := len(innerRing)/3*2 + 1 threshold := len(innerRing)/3*2 + 1
irKey := common.InnerRingInvoker(innerRing) irKey := common.InnerRingInvoker(innerRing)
@ -199,3 +196,7 @@ func fromKnownContract(caller []byte) bool {
return false return false
} }
func irList() []common.IRNode {
return common.InnerRingListViaStorage(ctx, netmapContractKey)
}