Skip to content

Commit 1a1ccaf

Browse files
committed
Ethereum Oracle
1 parent 689c838 commit 1a1ccaf

40 files changed

Lines changed: 5380 additions & 69 deletions

cmd/cli/main.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"time"
1616

1717
"github.com/canopy-network/canopy/cmd/rpc"
18+
"github.com/canopy-network/canopy/cmd/rpc/oracle"
19+
"github.com/canopy-network/canopy/cmd/rpc/oracle/eth"
1820
"github.com/canopy-network/canopy/controller"
1921
"github.com/canopy-network/canopy/fsm"
2022
"github.com/canopy-network/canopy/lib"
@@ -87,8 +89,21 @@ func Start() {
8789
if err != nil {
8890
l.Fatal(err.Error())
8991
}
92+
// create a seperate logger for the oracle and all oracle components
93+
oracleLogger := lib.NewOracleLogger(lib.LoggerConfig{Level: config.GetLogLevel()}, config.OracleConfig.LogPath)
94+
// create a new ethereum disk storage instance for the oracle order store
95+
oracleStorage, e := oracle.NewOracleDiskStorage(config.OracleConfig.OrderStorePath, oracleLogger)
96+
if e != nil {
97+
l.Fatal(err.Error())
98+
}
99+
// create the ethereum block provider
100+
ethBlockProvider := eth.NewEthBlockProvider(config.EthBlockProviderConfig, oracleLogger)
101+
// create a new oracle instance and pass the ethereum block provider
102+
oracle := oracle.NewOracle(config.OracleConfig, ethBlockProvider, oracleStorage, oracleLogger)
103+
// start the oracle
104+
oracle.Start()
90105
// create a new instance of the application
91-
app, err := controller.New(sm, config, validatorKey, metrics, l)
106+
app, err := controller.New(sm, oracle, config, validatorKey, metrics, l)
92107
if err != nil {
93108
l.Fatal(err.Error())
94109
}
@@ -102,6 +117,8 @@ func Start() {
102117
rpcServer.Start()
103118
// block until a kill signal is received
104119
waitForKill()
120+
// gracefuly stop oracle
121+
oracle.Stop()
105122
// gracefully stop the app
106123
app.Stop()
107124
// gracefully stop the metrics server

cmd/rpc/oracle/eth/block.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package eth
2+
3+
import (
4+
"github.com/canopy-network/canopy/cmd/rpc/oracle/types"
5+
"github.com/canopy-network/canopy/lib"
6+
ethtypes "github.com/ethereum/go-ethereum/core/types"
7+
)
8+
9+
var _ types.BlockI = &Block{} // Ensures *Block implements BlockI
10+
11+
// Block represents an ethereum block that implements BlockI interface
12+
type Block struct {
13+
hash string // block hash as hex string
14+
number uint64 // block number
15+
transactions []*Transaction // array of transactions in this block
16+
}
17+
18+
// NewBlock creates a new Block from an ethereum block
19+
func NewBlock(ethBlock *ethtypes.Block) (*Block, error) {
20+
// validate input block is not nil
21+
if ethBlock == nil {
22+
return nil, lib.ErrNilBlock()
23+
}
24+
// create new block instance
25+
block := &Block{
26+
hash: ethBlock.Hash().Hex(), // convert block hash to hex string
27+
number: ethBlock.NumberU64(), // get block number as uint64
28+
transactions: make([]*Transaction, 0), // initialize empty transaction slice
29+
}
30+
return block, nil // return successfully created block
31+
}
32+
33+
// Hash returns the block hash as a string
34+
func (b *Block) Hash() string {
35+
return b.hash // return the stored block hash
36+
}
37+
38+
// Number returns the block number
39+
func (b *Block) Number() uint64 {
40+
return b.number // return the stored block number
41+
}
42+
43+
// Transactions returns all transactions in this block
44+
func (b *Block) Transactions() []types.TransactionI {
45+
// create slice to hold transaction interfaces
46+
txs := make([]types.TransactionI, len(b.transactions))
47+
// convert each transaction to interface type
48+
for i, tx := range b.transactions {
49+
txs[i] = tx // assign transaction to interface slice
50+
}
51+
return txs // return slice of transaction interfaces
52+
}

0 commit comments

Comments
 (0)