cosmos-sdk/examples/democoin/x/oracle
Alexander Bezobchuk 2d92803b9f Merge PR #2040: Refactor Validator Account Types/Bech32 Prefixing
* Add new account bech32 prefixes with godocs

* Restructure spacing of existing account code

* Update account godocs

* More account godoc updates + new tm pub/addr helpers

* Update validator type to use new account types/bech32 prefixes

* Fix account documentation errors

* Update Bech32 prefix for consensus nodes

* Update Bech32 spec doc

* Fix account type tests

* Add missing account consensus functions, clear up godocs, and fix tests

* Add to TestRandBech32PubkeyConsistency check

* Update initialization of validator public keys

* Update query signing info command

* Implement new ConsAddress type with associated unit tests

* [WIP] Update stake and slashing parameters

* Update all calls to MustBech32ifyValPub

* [WIP] Validator operator API updates

* [WIP] Fix and update unit tests

* Fix gov logs (helping to debug failing tests)

* Fix gov tally

* Fix all broken x/ unit tests

* Update gaia app genesis address logic

* Fix linting errors

* Fix broken LCD tests

* Fix broken CLI tests

* Implement command to get validator address and pubkey from key name

* Add support for getting validator key information via REST endpoint

* Update PENDING log

* Update docs

* Revert GaiaGenTx.PubKey bech32 prefix

* Fix broken docs and cli tests

* Update genesis to use correct Bech32 (cons) prefix for pubkeys

* Update docs and unit tests to reflect new cosmos account bech32 prefix

* minor formatting
2018-08-31 00:06:44 -04:00
..
README.md Merge pull request #1819: rational -> decimal 2018-08-14 20:15:02 -04:00
errors.go asdf 2018-07-09 00:59:51 -07:00
handler.go Merge PR #2040: Refactor Validator Account Types/Bech32 Prefixing 2018-08-31 00:06:44 -04:00
keeper.go Merge pull request #1819: rational -> decimal 2018-08-14 20:15:02 -04:00
keeper_keys.go asdf 2018-07-09 00:59:51 -07:00
oracle_test.go Merge pull request #1819: rational -> decimal 2018-08-14 20:15:02 -04:00
types.go asdf 2018-07-09 00:59:51 -07:00

README.md

Oracle Module

x/oracle provides a way to receive external information(real world price, events from other chains, etc.) with validators' vote. Each validator make transaction which contains those informations, and Oracle aggregates them until the supermajority signed on it. After then, Oracle sends the information to the actual module that processes the information, and prune the votes from the state.

Integration

See x/oracle/oracle_test.go for the code that using Oracle

To use Oracle in your module, first define a payload. It should implement oracle.Payload and contain nessesary information for your module. Including nonce is recommended.

type MyPayload struct {
    Data  int
    Nonce int
}

When you write a payload, its .Type() should return same name with your module is registered on the router. It is because oracle.Msg inherits .Type() from its embedded payload and it should be handled on the user modules.

Then route every incoming oracle.Msg to oracle.Keeper.Handler() with the function that implements oracle.Handler.

func NewHandler(keeper Keeper) sdk.Handler {
    return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
        switch msg := msg.(type) {
        case oracle.Msg: 
            return keeper.oracle.Handle(ctx sdk.Context, p oracle.Payload) sdk.Error {
                switch p := p.(type) {
                case MyPayload:
                    return handleMyPayload(ctx, keeper, p)
                }
            }
        }
    }
}

In the previous example, the keeper has an oracle.Keeper. oracle.Keepers are generated by NewKeeper.

func NewKeeper(key sdk.StoreKey, cdc *wire.Codec, valset sdk.ValidatorSet, supermaj sdk.Dec, timeout int64) Keeper {
    return Keeper {
        cdc: cdc,
        key: key,
    
        // ValidatorSet to get validators infor
        valset: valset,

        // The keeper will pass payload
        // when more than 2/3 signed on it
        supermaj: supermaj,
        // The keeper will prune votes after 100 blocks from last sign
        timeout: timeout,
    }
}

Now the validators can send oracle.Msgs with MyPayload when they want to witness external events.