2017-10-23 15:56:29 +00:00
|
|
|
package fakes
|
|
|
|
|
2017-11-28 16:45:21 +00:00
|
|
|
import (
|
2017-11-28 20:03:21 +00:00
|
|
|
"sort"
|
2017-11-29 15:31:23 +00:00
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
"math/big"
|
|
|
|
|
2017-11-29 15:31:23 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/pkg/core"
|
2017-11-28 16:45:21 +00:00
|
|
|
)
|
2017-10-23 15:56:29 +00:00
|
|
|
|
|
|
|
type Blockchain struct {
|
2017-11-27 15:39:53 +00:00
|
|
|
blocks map[int64]core.Block
|
|
|
|
contractAttributes map[string]map[string]string
|
|
|
|
blocksChannel chan core.Block
|
|
|
|
WasToldToStop bool
|
|
|
|
}
|
|
|
|
|
2017-12-04 22:54:35 +00:00
|
|
|
func (blockchain *Blockchain) GetAttribute(contract core.Contract, attributeName string, blockNumber *big.Int) (interface{}, error) {
|
2017-12-04 18:54:33 +00:00
|
|
|
var result interface{}
|
|
|
|
if blockNumber == nil {
|
2017-12-04 22:54:35 +00:00
|
|
|
result = blockchain.contractAttributes[contract.Hash+"-1"][attributeName]
|
2017-12-04 18:54:33 +00:00
|
|
|
} else {
|
2017-12-04 22:54:35 +00:00
|
|
|
result = blockchain.contractAttributes[contract.Hash+blockNumber.String()][attributeName]
|
2017-12-04 18:54:33 +00:00
|
|
|
}
|
2017-11-27 15:39:53 +00:00
|
|
|
return result, nil
|
2017-10-23 15:56:29 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 20:36:12 +00:00
|
|
|
func NewBlockchain() *Blockchain {
|
2017-11-27 15:39:53 +00:00
|
|
|
return &Blockchain{
|
|
|
|
blocks: make(map[int64]core.Block),
|
|
|
|
contractAttributes: make(map[string]map[string]string),
|
|
|
|
}
|
2017-11-06 20:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlockchainWithBlocks(blocks []core.Block) *Blockchain {
|
|
|
|
blockNumberToBlocks := make(map[int64]core.Block)
|
|
|
|
for _, block := range blocks {
|
|
|
|
blockNumberToBlocks[block.Number] = block
|
|
|
|
}
|
|
|
|
return &Blockchain{
|
|
|
|
blocks: blockNumberToBlocks,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blockchain *Blockchain) GetBlockByNumber(blockNumber int64) core.Block {
|
|
|
|
return blockchain.blocks[blockNumber]
|
|
|
|
}
|
|
|
|
|
2017-11-02 11:41:24 +00:00
|
|
|
func (blockchain *Blockchain) SubscribeToBlocks(outputBlocks chan core.Block) {
|
2017-11-06 20:36:12 +00:00
|
|
|
blockchain.blocksChannel = outputBlocks
|
2017-10-23 15:56:29 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 20:36:12 +00:00
|
|
|
func (blockchain *Blockchain) AddBlock(block core.Block) {
|
|
|
|
blockchain.blocks[block.Number] = block
|
|
|
|
blockchain.blocksChannel <- block
|
2017-10-23 15:56:29 +00:00
|
|
|
}
|
2017-10-23 18:58:33 +00:00
|
|
|
|
2017-11-02 11:41:24 +00:00
|
|
|
func (*Blockchain) StartListening() {}
|
2017-11-03 13:54:32 +00:00
|
|
|
|
|
|
|
func (blockchain *Blockchain) StopListening() {
|
|
|
|
blockchain.WasToldToStop = true
|
|
|
|
}
|
2017-11-27 15:39:53 +00:00
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
func (blockchain *Blockchain) SetContractStateAttribute(contractHash string, blockNumber *big.Int, attributeName string, attributeValue string) {
|
|
|
|
var key string
|
|
|
|
if blockNumber == nil {
|
|
|
|
key = contractHash + "-1"
|
|
|
|
} else {
|
|
|
|
key = contractHash + blockNumber.String()
|
|
|
|
}
|
|
|
|
contractStateAttributes := blockchain.contractAttributes[key]
|
2017-11-27 15:39:53 +00:00
|
|
|
if contractStateAttributes == nil {
|
2017-12-04 18:54:33 +00:00
|
|
|
blockchain.contractAttributes[key] = make(map[string]string)
|
2017-11-27 15:39:53 +00:00
|
|
|
}
|
2017-12-04 18:54:33 +00:00
|
|
|
blockchain.contractAttributes[key][attributeName] = attributeValue
|
2017-11-27 15:39:53 +00:00
|
|
|
}
|
2017-11-30 22:15:32 +00:00
|
|
|
|
2017-12-04 22:54:35 +00:00
|
|
|
func (blockchain *Blockchain) GetAttributes(contract core.Contract) (core.ContractAttributes, error) {
|
2017-11-30 22:15:32 +00:00
|
|
|
var contractAttributes core.ContractAttributes
|
2017-12-04 22:54:35 +00:00
|
|
|
attributes, ok := blockchain.contractAttributes[contract.Hash+"-1"]
|
2017-11-30 22:15:32 +00:00
|
|
|
if ok {
|
|
|
|
for key, _ := range attributes {
|
|
|
|
contractAttributes = append(contractAttributes, core.ContractAttribute{Name: key, Type: "string"})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Sort(contractAttributes)
|
|
|
|
return contractAttributes, nil
|
|
|
|
}
|