Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

network: implement Oracle module #1427

Merged
merged 7 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func newTestChain(t *testing.T, f func(*config.Config)) (*core.Blockchain, *serv
netSrv, err := network.NewServer(serverConfig, chain, zap.NewNop())
require.NoError(t, err)
go netSrv.Start(make(chan error, 1))
rpcServer := server.New(chain, cfg.ApplicationConfiguration.RPC, netSrv, logger)
rpcServer := server.New(chain, cfg.ApplicationConfiguration.RPC, netSrv, nil, logger)
errCh := make(chan error, 2)
rpcServer.Start(errCh)

Expand Down
2 changes: 1 addition & 1 deletion cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func startServer(ctx *cli.Context) error {
if err != nil {
return cli.NewExitError(fmt.Errorf("failed to create network server: %w", err), 1)
}
rpcServer := server.New(chain, cfg.ApplicationConfiguration.RPC, serv, log)
rpcServer := server.New(chain, cfg.ApplicationConfiguration.RPC, serv, serv.GetOracle(), log)
errChan := make(chan error)

go serv.Start(errChan)
Expand Down
3 changes: 3 additions & 0 deletions config/protocol.mainnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ ApplicationConfiguration:
MaxPeers: 100
AttemptConnPeers: 20
MinPeers: 5
Oracle:
Enabled: false

RPC:
Enabled: true
MaxGasInvoke: 15
Expand Down
11 changes: 11 additions & 0 deletions config/protocol.privnet.docker.four.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ ApplicationConfiguration:
MaxPeers: 10
AttemptConnPeers: 5
MinPeers: 3
Oracle:
Enabled: false
Nodes:
- 172.200.0.1:30333
- 172.200.0.2:30334
- 172.200.0.3:30335
- 172.200.0.4:30336
RequestTimeout: 5s
UnlockWallet:
Path: "/wallet4.json"
Password: "four"
RPC:
Enabled: true
MaxGasInvoke: 15
Expand Down
11 changes: 11 additions & 0 deletions config/protocol.privnet.docker.one.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ ApplicationConfiguration:
MaxPeers: 10
AttemptConnPeers: 5
MinPeers: 3
Oracle:
Enabled: false
Nodes:
- 172.200.0.1:30333
- 172.200.0.2:30334
- 172.200.0.3:30335
- 172.200.0.4:30336
RequestTimeout: 5s
UnlockWallet:
Path: "/wallet1.json"
Password: "one"
RPC:
Enabled: true
MaxGasInvoke: 15
Expand Down
8 changes: 8 additions & 0 deletions config/protocol.privnet.docker.single.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ ApplicationConfiguration:
MaxPeers: 10
AttemptConnPeers: 5
MinPeers: 0
Oracle:
Enabled: false
Nodes:
- 172.200.0.1:30333
RequestTimeout: 5s
UnlockWallet:
Path: "/wallet1_solo.json"
Password: "one"
RPC:
Enabled: true
EnableCORSWorkaround: false
Expand Down
11 changes: 11 additions & 0 deletions config/protocol.privnet.docker.three.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ ApplicationConfiguration:
MaxPeers: 10
AttemptConnPeers: 5
MinPeers: 3
Oracle:
Enabled: false
Nodes:
- 172.200.0.1:30333
- 172.200.0.2:30334
- 172.200.0.3:30335
- 172.200.0.4:30336
RequestTimeout: 5s
UnlockWallet:
Path: "/wallet3.json"
Password: "three"
RPC:
Enabled: true
MaxGasInvoke: 15
Expand Down
11 changes: 11 additions & 0 deletions config/protocol.privnet.docker.two.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ ApplicationConfiguration:
MaxPeers: 10
AttemptConnPeers: 5
MinPeers: 3
Oracle:
Enabled: false
Nodes:
- 172.200.0.1:30333
- 172.200.0.2:30334
- 172.200.0.3:30335
- 172.200.0.4:30336
RequestTimeout: 5s
UnlockWallet:
Path: "/wallet2.json"
Password: "two"
RPC:
Enabled: true
MaxGasInvoke: 15
Expand Down
3 changes: 3 additions & 0 deletions config/protocol.testnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ ApplicationConfiguration:
MaxPeers: 100
AttemptConnPeers: 20
MinPeers: 5
Oracle:
Enabled: false

RPC:
Enabled: true
MaxGasInvoke: 15
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/nspcc-dev/neo-go

require (
github.com/PaesslerAG/jsonpath v0.1.1
github.com/Workiva/go-datastructures v1.0.50
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db
github.com/alicebob/miniredis v2.5.0+incompatible
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ github.com/DataDog/zstd v1.4.1 h1:3oxKN3wbHibqx897utPC2LTQU4J+IHWWJO+glkAkpFM=
github.com/DataDog/zstd v1.4.1/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PaesslerAG/gval v1.0.0 h1:GEKnRwkWDdf9dOmKcNrar9EA1bz1z9DqPIO1+iLzhd8=
github.com/PaesslerAG/gval v1.0.0/go.mod h1:y/nm5yEyTeX6av0OfKJNp9rBNj2XrGhAf5+v24IBN1I=
github.com/PaesslerAG/jsonpath v0.1.0/go.mod h1:4BzmtoM/PI8fPO4aQGIusjGxGir2BzcV0grWtFzq1Y8=
github.com/PaesslerAG/jsonpath v0.1.1 h1:c1/AToHQMVsduPAa4Vh6xp2U0evy4t8SWp8imEsylIk=
github.com/PaesslerAG/jsonpath v0.1.1/go.mod h1:lVboNxFGal/VwW6d9JzIy56bUsYAP6tH/x80vjnCseY=
github.com/Workiva/go-datastructures v1.0.50 h1:slDmfW6KCHcC7U+LP3DDBbm4fqTwZGn1beOFPfGaLvo=
github.com/Workiva/go-datastructures v1.0.50/go.mod h1:Z+F2Rca0qCsVYDS8z7bAGm8f3UkzuWYS/oBZz5a7VVA=
github.com/abiosoft/ishell v2.0.0+incompatible h1:zpwIuEHc37EzrsIYah3cpevrIc8Oma7oZPxr03tlmmw=
Expand Down
1 change: 1 addition & 0 deletions pkg/config/application_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ type ApplicationConfiguration struct {
Relay bool `yaml:"Relay"`
RPC rpc.Config `yaml:"RPC"`
UnlockWallet Wallet `yaml:"UnlockWallet"`
Oracle OracleConfiguration `yaml:"Oracle"`
}
16 changes: 16 additions & 0 deletions pkg/config/oracle_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package config

import "time"

// OracleConfiguration is a config for the oracle module.
type OracleConfiguration struct {
Enabled bool `yaml:"Enabled"`
AllowPrivateHost bool `yaml:"AllowPrivateHost"`
Nodes []string `yaml:"Nodes"`
MaxTaskTimeout time.Duration `yaml:"MaxTaskTimeout"`
RefreshInterval time.Duration `yaml:"RefreshInterval"`
MaxConcurrentRequests int `yaml:"MaxConcurrentRequests"`
RequestTimeout time.Duration `yaml:"RequestTimeout"`
ResponseTimeout time.Duration `yaml:"ResponseTimeout"`
UnlockWallet Wallet `yaml:"UnlockWallet"`
}
32 changes: 22 additions & 10 deletions pkg/core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer"
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer/services"
"github.com/nspcc-dev/neo-go/pkg/core/dao"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/interop/contract"
Expand All @@ -34,7 +35,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -192,6 +192,13 @@ func NewBlockchain(s storage.Store, cfg config.ProtocolConfiguration, log *zap.L
return bc, nil
}

// SetOracle sets oracle module. It doesn't protected by mutex and
// must be called before `bc.Run()` to avoid data race.
func (bc *Blockchain) SetOracle(mod services.Oracle) {
bc.contracts.Oracle.Module.Store(mod)
bc.contracts.Designate.OracleService.Store(mod)
}

func (bc *Blockchain) init() error {
// If we could not find the version in the Store, we know that there is nothing stored.
ver, err := bc.dao.GetVersion()
Expand Down Expand Up @@ -1654,6 +1661,7 @@ var (

// initVerificationVM initializes VM for witness check.
func (bc *Blockchain) initVerificationVM(ic *interop.Context, hash util.Uint160, witness *transaction.Witness) error {
isNative := false
v := ic.VM
if len(witness.VerificationScript) != 0 {
if witness.ScriptHash() != hash {
Expand All @@ -1677,18 +1685,22 @@ func (bc *Blockchain) initVerificationVM(ic *interop.Context, hash util.Uint160,
v.Context().NEF = &cs.NEF
v.Jump(v.Context(), md.Offset)

if cs.ID <= 0 {
w := io.NewBufBinWriter()
emit.String(w.BinWriter, manifest.MethodVerify)
if w.Err != nil {
return w.Err
}
v.LoadScript(w.Bytes())
} else if initMD != nil {
isNative = cs.ID <= 0
if !isNative && initMD != nil {
v.Call(v.Context(), initMD.Offset)
}
}
v.LoadScript(witness.InvocationScript)
if len(witness.InvocationScript) != 0 {
v.LoadScript(witness.InvocationScript)
if isNative {
if err := v.StepOut(); err != nil {
return err
}
}
}
if isNative {
v.Estack().PushVal(manifest.MethodVerify)
}
return nil
}

Expand Down
1 change: 0 additions & 1 deletion pkg/core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,6 @@ func TestVerifyTx(t *testing.T) {
transaction.NotaryServiceFeePerKey + // fee for Notary attribute
fee.Opcode(bc.GetBaseExecFee(), // Notary verification script
opcode.PUSHDATA1, opcode.RET, // invocation script
opcode.PUSHDATA1, opcode.RET, // arguments for native verification call
opcode.PUSHINT8, opcode.SYSCALL, opcode.RET) + // Neo.Native.Call
native.NotaryVerificationPrice // Notary witness verification price
tx.Scripts = []transaction.Witness{
Expand Down
2 changes: 2 additions & 0 deletions pkg/core/blockchainer/blockchainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer/services"
"github.com/nspcc-dev/neo-go/pkg/core/mempool"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
Expand Down Expand Up @@ -57,6 +58,7 @@ type Blockchainer interface {
GetStorageItems(id int32) (map[string]*state.StorageItem, error)
GetTestVM(t trigger.Type, tx *transaction.Transaction, b *block.Block) *vm.VM
GetTransaction(util.Uint256) (*transaction.Transaction, uint32, error)
SetOracle(service services.Oracle)
mempool.Feer // fee interface
ManagementContractHash() util.Uint160
PoolTx(t *transaction.Transaction, pools ...*mempool.Pool) error
Expand Down
20 changes: 20 additions & 0 deletions pkg/core/blockchainer/services/oracle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package services

import (
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
)

// Oracle specifies oracle service interface.
type Oracle interface {
// AddRequests processes new requests.
AddRequests(map[uint64]*state.OracleRequest)
// RemoveRequests removes already processed requests.
RemoveRequests([]uint64)
// UpdateOracleNodes updates oracle nodes.
UpdateOracleNodes(keys.PublicKeys)
// Run runs oracle module. Must be invoked in a separate goroutine.
Run()
// Shutdown shutdowns oracle module.
Shutdown()
}
7 changes: 6 additions & 1 deletion pkg/core/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func newTestChainWithCustomCfg(t *testing.T, f func(*config.Config)) *Blockchain
}

func newTestChainWithCustomCfgAndStore(t *testing.T, st storage.Store, f func(*config.Config)) *Blockchain {
chain := initTestChain(t, st, f)
go chain.Run()
return chain
}

func initTestChain(t *testing.T, st storage.Store, f func(*config.Config)) *Blockchain {
unitTestNetCfg, err := config.Load("../../config", testchain.Network())
require.NoError(t, err)
if f != nil {
Expand All @@ -61,7 +67,6 @@ func newTestChainWithCustomCfgAndStore(t *testing.T, st storage.Store, f func(*c
}
chain, err := NewBlockchain(st, unitTestNetCfg.ProtocolConfiguration, zaptest.NewLogger(t))
require.NoError(t, err)
go chain.Run()
return chain
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/core/native/designate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"
"sync/atomic"

"github.com/nspcc-dev/neo-go/pkg/core/blockchainer/services"
"github.com/nspcc-dev/neo-go/pkg/core/dao"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/interop/runtime"
Expand All @@ -32,6 +33,8 @@ type Designate struct {

// p2pSigExtensionsEnabled defines whether the P2P signature extensions logic is relevant.
p2pSigExtensionsEnabled bool

OracleService atomic.Value
}

type oraclesData struct {
Expand Down Expand Up @@ -117,6 +120,9 @@ func (s *Designate) PostPersist(ic *interop.Context) error {
height: height,
}
s.oracles.Store(od)
if orc, _ := s.OracleService.Load().(services.Oracle); orc != nil {
orc.UpdateOracleNodes(od.nodes.Copy())
}
s.rolesChangedFlag.Store(false)
return nil
}
Expand Down
Loading