diff options
author | Jhih-Ming Huang <jm@byzantine-lab.io> | 2019-06-19 17:49:27 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-09-17 16:57:30 +0800 |
commit | c3c6574c9053a361bfe4f77bf4e1ae99d19f60dc (patch) | |
tree | 1a2b1db4b51bb084b5340d9875b968aaee87976c /core/vm/oracle_contracts.go | |
parent | aa9c5b22941aadfefcde64d2570d6c6863bc2ebe (diff) | |
download | go-tangerine-c3c6574c9053a361bfe4f77bf4e1ae99d19f60dc.tar go-tangerine-c3c6574c9053a361bfe4f77bf4e1ae99d19f60dc.tar.gz go-tangerine-c3c6574c9053a361bfe4f77bf4e1ae99d19f60dc.tar.bz2 go-tangerine-c3c6574c9053a361bfe4f77bf4e1ae99d19f60dc.tar.lz go-tangerine-c3c6574c9053a361bfe4f77bf4e1ae99d19f60dc.tar.xz go-tangerine-c3c6574c9053a361bfe4f77bf4e1ae99d19f60dc.tar.zst go-tangerine-c3c6574c9053a361bfe4f77bf4e1ae99d19f60dc.zip |
core: vm: remove opRand and use oracle contract to get random number
Diffstat (limited to 'core/vm/oracle_contracts.go')
-rw-r--r-- | core/vm/oracle_contracts.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/core/vm/oracle_contracts.go b/core/vm/oracle_contracts.go index 484ed82b8..7e245e6bd 100644 --- a/core/vm/oracle_contracts.go +++ b/core/vm/oracle_contracts.go @@ -19,6 +19,7 @@ package vm import ( "bytes" + "encoding/binary" "errors" "fmt" "math" @@ -2952,3 +2953,34 @@ func PackResetDKG(newSignedCRS []byte) ([]byte, error) { data := append(method.Id(), res...) return data, nil } + +// RandomContract provides access to on chain randomness. +type RandomContract struct { + evm *EVM + contract *Contract +} + +func (*RandomContract) Run(evm *EVM, input []byte, + contract *Contract) (ret []byte, err error) { + nonce := evm.StateDB.GetNonce(evm.Origin) + + cost := params.RandGas + if !contract.UseGas(cost) { + return nil, ErrOutOfGas + } + + binaryOriginNonce := make([]byte, binary.MaxVarintLen64) + binary.PutUvarint(binaryOriginNonce, nonce) + + binaryUsedIndex := make([]byte, binary.MaxVarintLen64) + binary.PutUvarint(binaryUsedIndex, evm.RandCallIndex) + + evm.RandCallIndex += 1 + + ret = crypto.Keccak256( + evm.Randomness, + evm.Origin.Bytes(), + binaryOriginNonce, + binaryUsedIndex) + return +} |