forked from TrueCloudLab/frostfs-s3-gw
73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
package util
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/ns"
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
)
|
|
|
|
// ResolveContractHash determine contract hash by resolving NNS name.
|
|
func ResolveContractHash(contractHash, rpcAddress string) (util.Uint160, error) {
|
|
if hash, err := util.Uint160DecodeStringLE(contractHash); err == nil {
|
|
return hash, nil
|
|
}
|
|
|
|
splitName := strings.Split(contractHash, ".")
|
|
if len(splitName) != 2 {
|
|
return util.Uint160{}, fmt.Errorf("invalid contract name: '%s'", contractHash)
|
|
}
|
|
|
|
var domain container.Domain
|
|
domain.SetName(splitName[0])
|
|
domain.SetZone(splitName[1])
|
|
|
|
var nns ns.NNS
|
|
if err := nns.Dial(rpcAddress); err != nil {
|
|
return util.Uint160{}, fmt.Errorf("dial nns %s: %w", rpcAddress, err)
|
|
}
|
|
defer nns.Close()
|
|
|
|
return nns.ResolveContractHash(domain)
|
|
}
|
|
|
|
func TimeToEpoch(ni *netmap.NetworkInfo, now, t time.Time) (uint64, error) {
|
|
duration := t.Sub(now)
|
|
durationAbs := duration.Abs()
|
|
|
|
durEpoch := ni.EpochDuration()
|
|
if durEpoch == 0 {
|
|
return 0, fmt.Errorf("epoch duration is missing or zero")
|
|
}
|
|
|
|
msPerEpoch := durEpoch * uint64(ni.MsPerBlock())
|
|
epochLifetime := uint64(durationAbs.Milliseconds()) / msPerEpoch
|
|
|
|
if uint64(durationAbs.Milliseconds())%msPerEpoch != 0 {
|
|
epochLifetime++
|
|
}
|
|
|
|
curr := ni.CurrentEpoch()
|
|
|
|
var epoch uint64
|
|
if duration > 0 {
|
|
if epochLifetime >= math.MaxUint64-curr {
|
|
epoch = math.MaxUint64
|
|
} else {
|
|
epoch = curr + epochLifetime
|
|
}
|
|
} else {
|
|
if epochLifetime >= curr {
|
|
epoch = 0
|
|
} else {
|
|
epoch = curr - epochLifetime
|
|
}
|
|
}
|
|
|
|
return epoch, nil
|
|
}
|