vochain

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2021 License: AGPL-3.0 Imports: 50 Imported by: 4

Documentation

Overview

Package vochain provides all the functions for creating and managing a vocdoni voting blockchain

Index

Constants

View Source
const (
	// db names
	AppTree     = "app"
	ProcessTree = "process"
	VoteTree    = "vote"
)

Variables

View Source
var (
	ErrProcessNotFound = fmt.Errorf("process not found")
)
View Source
var Genesis = map[string]VochainGenesis{

	"main": {
		AutoUpdateGenesis: false,
		SeedNodes:         []string{"121e65eb5994874d9c05cd8d584a54669d23f294@seed.vocdoni.net:26656"},
		Genesis: `
   {
      "genesis_time":"2021-03-16T15:38:33.672114557Z",
      "chain_id":"vocdoni-release-1.0",
      "consensus_params":{
         "block":{
            "max_bytes":"10485760",
            "max_gas":"-1",
            "time_iota_ms":"10000"
         },
         "evidence":{
            "max_age_num_blocks":"100000",
            "max_age_duration":"10000"
         },
         "validator":{
            "pub_key_types":[
               "ed25519"
            ]
         }
      },
      "validators":[
         {
            "address":"6DB4FEE1D370907B31196B493714FC0F45C62DED",
            "pub_key":{
               "type":"tendermint/PubKeyEd25519",
               "value":"R7U+HxyTrvlXccEm1sc80ww83Fpp4xg247nmpjmkYTc="
            },
            "power":"10",
            "name":"miner1"
         },
         {
            "address":"71AA2FEFA96447BC5AEF9FD928F3F8ED57E695CF",
            "pub_key":{
               "type":"tendermint/PubKeyEd25519",
               "value":"ixI91P+MP1jiVIy1JwQqwRdZIZxsVI0WrytAzohMGCk="
            },
            "power":"10",
            "name":"miner2"
         },
         {
            "address":"AA9CC01B46BDD1AC9E2197BB9B84993CCDF880B2",
            "pub_key":{
               "type":"tendermint/PubKeyEd25519",
               "value":"H6oEMFrNFeQemr9Kgxjq/wVk1kZQ1VE/J1wVnVJ+K9I="
            },
            "power":"10",
            "name":"miner3"
         },
         {
            "address":"314D17BBE991FBD3D234E5C62CFD5D0717123C95",
            "pub_key":{
               "type":"tendermint/PubKeyEd25519",
               "value":"FLEg/pgdF4dZ060mved/z99p/EJePu9kSsyLnrsRNC0="
            },
            "power":"10",
            "name":"miner4"
         },
         {
            "address":"34B048A4A720E6B3918CF8B75CF12555080465E5",
            "pub_key":{
               "type":"tendermint/PubKeyEd25519",
               "value":"aF/+WaNs5tknRMRpTPO49TJZLmDctO+JH8uckE5fTNU="
            },
            "power":"10",
            "name":"miner5"
         }
      ],
      "app_hash":"",
      "app_state":{
         "validators":[
            {
               "address":"6DB4FEE1D370907B31196B493714FC0F45C62DED",
               "pub_key":{
                  "type":"tendermint/PubKeyEd25519",
                  "value":"R7U+HxyTrvlXccEm1sc80ww83Fpp4xg247nmpjmkYTc="
               },
               "power":"10",
               "name":"miner1"
            },
            {
               "address":"71AA2FEFA96447BC5AEF9FD928F3F8ED57E695CF",
               "pub_key":{
                  "type":"tendermint/PubKeyEd25519",
                  "value":"ixI91P+MP1jiVIy1JwQqwRdZIZxsVI0WrytAzohMGCk="
               },
               "power":"10",
               "name":"miner2"
            },
            {
               "address":"AA9CC01B46BDD1AC9E2197BB9B84993CCDF880B2",
               "pub_key":{
                  "type":"tendermint/PubKeyEd25519",
                  "value":"H6oEMFrNFeQemr9Kgxjq/wVk1kZQ1VE/J1wVnVJ+K9I="
               },
               "power":"10",
               "name":"miner3"
            },
            {
               "address":"314D17BBE991FBD3D234E5C62CFD5D0717123C95",
               "pub_key":{
                  "type":"tendermint/PubKeyEd25519",
                  "value":"FLEg/pgdF4dZ060mved/z99p/EJePu9kSsyLnrsRNC0="
               },
               "power":"10",
               "name":"miner4"
            },
            {
               "address":"34B048A4A720E6B3918CF8B75CF12555080465E5",
               "pub_key":{
                  "type":"tendermint/PubKeyEd25519",
                  "value":"aF/+WaNs5tknRMRpTPO49TJZLmDctO+JH8uckE5fTNU="
               },
               "power":"10",
               "name":"miner5"
            }
         ],
         "oracles":[
            "0xc2e396d6e6ae9b12551f0c6111f9766bec926bfe",
            "0x1a361c26e04a33effbf3bd8617b1e3e0aa6b704f"
         ]
      }
   }
 `},

	"dev": {
		AutoUpdateGenesis: true,
		SeedNodes:         []string{"7440a5b086e16620ce7b13198479016aa2b07988@seed.dev.vocdoni.net:26656"},
		Genesis: `
{
   "genesis_time":"2021-03-08T10:52:28.668436552Z",
   "chain_id":"vocdoni-development-37",
   "consensus_params":{
      "block":{
         "max_bytes":"10485760",
         "max_gas":"-1",
         "time_iota_ms":"10000"
      },
      "evidence":{
         "max_age_num_blocks":"100000",
         "max_age_duration":"10000"
      },
      "validator":{
         "pub_key_types":[
            "ed25519"
         ]
      }
   },
   "validators":[
      {
         "address":"5C69093136E0CB84E5CFA8E958DADB33C0D0CCCF",
         "pub_key":{
            "type":"tendermint/PubKeyEd25519",
            "value":"mXc5xXTKgDSYcy1lBCT1Ag7Lh1nPWHMa/p80XZPzAPY="
         },
         "power":"10",
         "name":"miner0"
      },
      {
         "address":"2E1B244B84E223747126EF621C022D5CEFC56F69",
         "pub_key":{
            "type":"tendermint/PubKeyEd25519",
            "value":"gaf2ZfdxpoielRXDXyBcMxkdzywcE10WsvLMe1K62UY="
         },
         "power":"10",
         "name":"miner1"
      },
      {
         "address":"4EF00A8C18BD472167E67F28694F31451A195581",
         "pub_key":{
            "type":"tendermint/PubKeyEd25519",
            "value":"dZXMBiQl4s0/YplfX9iMnCWonJp2gjrFHHXaIwqqtmc="
         },
         "power":"10",
         "name":"miner2"
      },
      {
         "address":"ECCC09A0DF8F4E5554A9C58F634E9D6AFD5F1598",
         "pub_key":{
            "type":"tendermint/PubKeyEd25519",
            "value":"BebelLYe4GZKwy9IuXCyBTySxQCNRrRoi1DSvAf6QxE="
         },
         "power":"10",
         "name":"miner3"
      }
   ],
   "app_hash":"",
   "app_state":{
      "validators":[
         {
            "address":"5C69093136E0CB84E5CFA8E958DADB33C0D0CCCF",
            "pub_key":{
               "type":"tendermint/PubKeyEd25519",
               "value":"mXc5xXTKgDSYcy1lBCT1Ag7Lh1nPWHMa/p80XZPzAPY="
            },
            "power":"10",
            "name":"miner0"
         },
         {
            "address":"2E1B244B84E223747126EF621C022D5CEFC56F69",
            "pub_key":{
               "type":"tendermint/PubKeyEd25519",
               "value":"gaf2ZfdxpoielRXDXyBcMxkdzywcE10WsvLMe1K62UY="
            },
            "power":"10",
            "name":"miner1"
         },
         {
            "address":"4EF00A8C18BD472167E67F28694F31451A195581",
            "pub_key":{
               "type":"tendermint/PubKeyEd25519",
               "value":"dZXMBiQl4s0/YplfX9iMnCWonJp2gjrFHHXaIwqqtmc="
            },
            "power":"10",
            "name":"miner2"
         },
         {
            "address":"ECCC09A0DF8F4E5554A9C58F634E9D6AFD5F1598",
            "pub_key":{
               "type":"tendermint/PubKeyEd25519",
               "value":"BebelLYe4GZKwy9IuXCyBTySxQCNRrRoi1DSvAf6QxE="
            },
            "power":"10",
            "name":"miner3"
         }
      ],
      "oracles":[
         "0xb926be24A9ca606B515a835E91298C7cF0f2846f",
         "0x2f4ed2773dcf7ad0ec15eb84ec896f4eebe0e08a"
      ]
   }
}
`},

	"stage": {
		AutoUpdateGenesis: true,
		SeedNodes:         []string{"588133b8309363a2a852e853424251cd6e8c5330@seed.stg.vocdoni.net:26656"},
		Genesis: `
{
   "genesis_time":"2021-02-09T17:41:19.055210151Z",
   "chain_id":"vocdoni-stage-7",
   "consensus_params":{
      "block":{
         "max_bytes":"22020096",
         "max_gas":"-1",
         "time_iota_ms":"10000"
      },
      "evidence":{
         "max_age_num_blocks":"100000",
         "max_age_duration":"10000"
      },
      "validator":{
         "pub_key_types":[
            "ed25519"
         ]
      }
   },
   "validators":[
      {
         "address":"B04F5541E932BB754B566969A3CD1F8E4193EFE8",
         "pub_key":{
            "type":"tendermint/PubKeyEd25519",
            "value":"JgZaEoVxLyv7jcMDikidK2HEbChqljSrZwN+humPh34="
         },
         "power":"10",
         "name":"miner1"
      },
      {
         "address":"2DECD25EBDD6E3FAB2F06AC0EE391C16C292DBAD",
         "pub_key":{
            "type":"tendermint/PubKeyEd25519",
            "value":"HfZWmadJhz647Gx8zpRsSz8FACcWVpU2z6jYwUwComA="
         },
         "power":"10",
         "name":"miner2"
      },
      {
         "address":"3C6FF3D424901733818B954AA3AB3BC2E3695332",
         "pub_key":{
            "type":"tendermint/PubKeyEd25519",
            "value":"sAsec4Da5SZrRAOmeIWDKKbwieDF5EwT28bjxtbPlpk="
         },
         "power":"10",
         "name":"miner3"
      },
      {
         "address":"92C9A63172DFB4E9637309DFBFE20B1D11EDC4E7",
         "pub_key":{
            "type":"tendermint/PubKeyEd25519",
            "value":"SeRU76Jq8DRKrjtSpPLR/W69khkbeQBeNLr8CMXiht8="
         },
         "power":"10",
         "name":"miner4"
      }
   ],
   "app_hash":"",
   "app_state":{
      "validators":[
         {
            "address":"B04F5541E932BB754B566969A3CD1F8E4193EFE8",
            "pub_key":{
               "type":"tendermint/PubKeyEd25519",
               "value":"JgZaEoVxLyv7jcMDikidK2HEbChqljSrZwN+humPh34="
            },
            "power":"10",
            "name":"miner1"
         },
         {
            "address":"2DECD25EBDD6E3FAB2F06AC0EE391C16C292DBAD",
            "pub_key":{
               "type":"tendermint/PubKeyEd25519",
               "value":"HfZWmadJhz647Gx8zpRsSz8FACcWVpU2z6jYwUwComA="
            },
            "power":"10",
            "name":"miner2"
         },
         {
            "address":"3C6FF3D424901733818B954AA3AB3BC2E3695332",
            "pub_key":{
               "type":"tendermint/PubKeyEd25519",
               "value":"sAsec4Da5SZrRAOmeIWDKKbwieDF5EwT28bjxtbPlpk="
            },
            "power":"10",
            "name":"miner3"
         },
         {
            "address":"92C9A63172DFB4E9637309DFBFE20B1D11EDC4E7",
            "pub_key":{
               "type":"tendermint/PubKeyEd25519",
               "value":"SeRU76Jq8DRKrjtSpPLR/W69khkbeQBeNLr8CMXiht8="
            },
            "power":"10",
            "name":"miner4"
         }
      ],
      "oracles":[
         "0x81ff945dda4b94690a13f49fdc8f0819970b2db0",
         "0xcf83836eab1a4697bb9f9d07d7fb82aed707d918",
         "0x949a4b6b5dc64cdc2518c15c8dfdead4ebd07df0"
      ]
   }
}
`},
}

Genesis is a map containing the defaut Genesis details

View Source
var PrefixDBCacheSize = 0

PrefixDBCacheSize is the size of the cache for the MutableTree IAVL databases

Functions

func AddTx

func AddTx(vtx *models.Tx, txBytes, signature []byte, state *State,
	txID [32]byte, commit bool) ([]byte, error)

AddTx check the validity of a transaction and adds it to the state if commit=true

func AdminTxCheck

func AdminTxCheck(vtx *models.Tx, txBytes, signature []byte, state *State) error

AdminTxCheck is an abstraction of ABCI checkTx for an admin transaction

func AminoKeys

func AminoKeys(key crypto25519.PrivKey) (private, public []byte, err error)

AminoKeys is a helper function that transforms a standard EDDSA key into Tendermint like amino format useful for creating genesis files.

func CheckProof

func CheckProof(proof *models.Proof, censusOrigin models.CensusOrigin, censusRoot, processID, key []byte) (bool, *big.Int, error)

CheckProof checks the validity of a census proof (depending on the origin). key is the data to be proof in behalf the censusRoot. In case of weighted proof, this function will return the weight as second parameter.

func GenerateNullifier

func GenerateNullifier(address ethcommon.Address, processID []byte) []byte

GenerateNullifier generates the nullifier of a vote (hash(address+processId))

func NewGenesis

func NewGenesis(cfg *config.VochainCfg, chainID string, consensusParams *types.ConsensusParams, validators []privval.FilePV, oracles []string) ([]byte, error)

NewGenesis creates a new genesis and return its bytes

func NewNodeKey

func NewNodeKey(tmPrivKey string, tconfig *cfg.Config) (*p2p.NodeKey, error)

NewNodeKey returns and saves to the disk storage a tendermint node key

func NewPrivateValidator

func NewPrivateValidator(tmPrivKey string, tconfig *cfg.Config) (*privval.FilePV, error)

NewPrivateValidator returns a tendermint file private validator (key and state) if tmPrivKey not specified, uses the existing one or generates a new one

func NewProcessTxCheck

func NewProcessTxCheck(vtx *models.Tx, txBytes, signature []byte, state *State) (*models.Process, error)

NewProcessTxCheck is an abstraction of ABCI checkTx for creating a new process

func SetProcessTxCheck

func SetProcessTxCheck(vtx *models.Tx, txBytes, signature []byte, state *State) error

SetProcessTxCheck is an abstraction of ABCI checkTx for canceling an existing process

func TxKey

func TxKey(tx tmtypes.Tx) [32]byte

func UnmarshalTx

func UnmarshalTx(content []byte) (*models.Tx, []byte, []byte, error)

UnmarshalTx unarshal the content of a bytes serialized transaction. Returns the transaction struct, the original bytes and the signature of those bytes.

func VoteTxCheck

func VoteTxCheck(vtx *models.Tx, txBytes, signature []byte, state *State,
	txID [32]byte, forCommit bool) (*models.Vote, error)

VoteTxCheck is an abstraction of ABCI checkTx for submitting a vote All hexadecimal strings should be already sanitized (without 0x)

Types

type BaseApplication

type BaseApplication struct {
	State *State
	Node  *nm.Node
}

BaseApplication reflects the ABCI application implementation.

func NewBaseApplication

func NewBaseApplication(dbpath string) (*BaseApplication, error)

NewBaseApplication creates a new BaseApplication given a name an a DB backend

func NewVochain

func NewVochain(vochaincfg *config.VochainCfg, genesis []byte) *BaseApplication

NewVochain starts a node with an ABCI application

func (*BaseApplication) BeginBlock

BeginBlock signals the beginning of a new block. Called prior to any DeliverTxs. The header contains the height, timestamp, and more - it exactly matches the Tendermint block header. The LastCommitInfo and ByzantineValidators can be used to determine rewards and punishments for the validators.

func (*BaseApplication) CheckTx

func (*BaseApplication) Commit

func (*BaseApplication) DeliverTx

func (*BaseApplication) EndBlock

func (*BaseApplication) Info

Info Return information about the application state. Used to sync Tendermint with the application during a handshake that happens on startup. The returned AppVersion will be included in the Header of every block. Tendermint expects LastBlockAppHash and LastBlockHeight to be updated during Commit, ensuring that Commit is never called twice for the same block height.

func (*BaseApplication) InitChain

InitChain called once upon genesis ResponseInitChain can return a list of validators. If the list is empty, Tendermint will use the validators loaded in the genesis file.

func (*BaseApplication) ListSnapshots

func (*BaseApplication) OfferSnapshot

func (*BaseApplication) Query

func (*BaseApplication) SendTX

func (app *BaseApplication) SendTX(tx []byte) (*ctypes.ResultBroadcastTx, error)

SendTX sends a transaction to the mempool (sync)

func (BaseApplication) SetOption

type EventListener

type EventListener interface {
	OnVote(*models.Vote)
	OnProcess(pid, eid []byte, censusRoot, censusURI string)
	OnProcessStatusChange(pid []byte, status models.ProcessStatus)
	OnCancel(pid []byte)
	OnProcessKeys(pid []byte, encryptionPub, commitment string)
	OnRevealKeys(pid []byte, encryptionPriv, reveal string)
	Commit(height int64)
	Rollback()
}

EventListener is an interface used for executing custom functions during the events of the block creation process. The order in which events are executed is: Rollback, OnVote or OnProcess, Commit. The process is concurrency safe, meaning that there cannot be two sequences happening in parallel.

type ImmutableState

type ImmutableState struct {
	// Note that the mutex locks the entirety of the three IAVL trees, both
	// their mutable and immutable components. An immutable tree is not safe
	// for concurrent use with its parent mutable tree.
	sync.RWMutex
}

ImmutableState holds the latest trees version saved on disk

type State

type State struct {
	Store statedb.StateDB

	ImmutableState
	MemPoolRemoveTxKey func([32]byte, bool)
	// contains filtered or unexported fields
}

State represents the state of the vochain application

func NewState

func NewState(dataDir string) (*State, error)

NewState creates a new State

func (*State) AddEventListener

func (v *State) AddEventListener(l EventListener)

AddEventListener adds a new event listener, to receive method calls on block events as documented in EventListener.

func (*State) AddOracle

func (v *State) AddOracle(address common.Address) error

AddOracle adds a trusted oracle given its address if not exists

func (*State) AddProcess

func (v *State) AddProcess(p *models.Process) error

AddProcess adds or overides a new process to vochain

func (*State) AddProcessKeys

func (v *State) AddProcessKeys(tx *models.AdminTx) error

AddProcessKeys adds the keys to the process

func (*State) AddValidator

func (v *State) AddValidator(validator *models.Validator) error

AddValidator adds a tendemint validator if it is not already added

func (*State) AddVote

func (v *State) AddVote(vote *models.Vote) error

AddVote adds a new vote to a process if the process exists and the vote is not already submmited

func (*State) AppHash

func (v *State) AppHash(isQuery bool) []byte

AppHash returns last hash of the application

func (*State) CacheAdd

func (v *State) CacheAdd(id [32]byte, vc *types.CacheTx)

CacheAdd adds a new vote proof to the local cache

func (*State) CacheDel

func (v *State) CacheDel(id [32]byte)

CacheDel deletes an existing vote proof from the local cache

func (*State) CacheGet

func (v *State) CacheGet(id [32]byte) *types.CacheTx

CacheGet fetch an existing vote proof from the local cache

func (*State) CachePurge

func (v *State) CachePurge(height int64)

CachePurge removes the old cache saved votes

func (*State) CacheSize

func (v *State) CacheSize() int

CacheSize returns the current size of the vote cache

func (*State) CancelProcess

func (v *State) CancelProcess(pid []byte) error

CancelProcess sets the process canceled attribute to true

func (*State) CountProcesses

func (v *State) CountProcesses(isQuery bool) int64

CountProcesses returns the overall number of processes the vochain has

func (*State) CountVotes

func (v *State) CountVotes(processID []byte, isQuery bool) uint32

CountVotes returns the number of votes registered for a given process id

func (*State) Envelope

func (v *State) Envelope(processID, nullifier []byte, isQuery bool) (_ *models.Vote, err error)

Envelope returns the info of a vote if already exists. voteID must be equals to processID_Nullifier

func (*State) EnvelopeExists

func (v *State) EnvelopeExists(processID, nullifier []byte, isQuery bool) bool

EnvelopeExists returns true if the envelope identified with voteID exists

func (*State) EnvelopeList

func (v *State) EnvelopeList(processID []byte, from, listSize int, isQuery bool) (nullifiers [][]byte)

EnvelopeList returns a list of registered envelopes nullifiers given a processId

func (*State) Header

func (v *State) Header(isQuery bool) *models.TendermintHeader

Header returns the blockchain last block committed height

func (*State) Oracles

func (v *State) Oracles(isQuery bool) ([]common.Address, error)

Oracles returns the current oracle list

func (*State) Process

func (v *State) Process(pid []byte, isQuery bool) (*models.Process, error)

Process returns a process info given a processId if exists

func (*State) RemoveOracle

func (v *State) RemoveOracle(address common.Address) error

RemoveOracle removes a trusted oracle given its address if exists

func (*State) RemoveValidator

func (v *State) RemoveValidator(address []byte) error

RemoveValidator removes a tendermint validator identified by its address

func (*State) RevealProcessKeys

func (v *State) RevealProcessKeys(tx *models.AdminTx) error

RevealProcessKeys reveals the keys of a process

func (*State) Rollback

func (v *State) Rollback()

Rollback rollbacks to the last persistent db data version

func (*State) Save

func (v *State) Save() []byte

Save persistent save of vochain mem trees

func (*State) SetProcessCensus

func (v *State) SetProcessCensus(pid, censusRoot []byte, censusURI string, commit bool) error

func (*State) SetProcessResults

func (v *State) SetProcessResults(pid []byte, result *models.ProcessResult, commit bool) error

SetProcessResults sets the results for a given process. Only if the process status allows and the format of the results allows to do so TODO: allow result confirm with another Tx (in order to have N/M oracle signatures)

func (*State) SetProcessStatus

func (v *State) SetProcessStatus(pid []byte, newstatus models.ProcessStatus, commit bool) error

SetProcessStatus changes the process status to the one provided. One of ready, ended, canceled, paused, results. Transition checks are handled inside this function, so the caller does not need to worry about it.

func (*State) Validators

func (v *State) Validators(isQuery bool) ([]*models.Validator, error)

Validators returns a list of the validators saved on persistent storage

func (*State) WorkingHash

func (v *State) WorkingHash() []byte

WorkingHash returns the hash of the vochain trees censusRoots hash(appTree+processTree+voteTree)

type TenderLogger

type TenderLogger struct {
	Artifact string
	Disabled bool
	// contains filtered or unexported fields
}

TenderLogger implements tendermint's Logger interface, with a couple of modifications.

First, it routes the logs to go-dvote's logger, so that we don't end up with two loggers writing directly to stdout or stderr.

Second, because we generally don't care about tendermint errors such as failures to connect to peers, we route all log levels to our debug level. They will only surface if dvote's log level is "debug".

func NewTenderLogger

func NewTenderLogger(artifact string, disabled bool) *TenderLogger

NewTenderLogger creates a Tenderming compatible logger for specified artifact

func (*TenderLogger) Debug

func (l *TenderLogger) Debug(msg string, keyvals ...interface{})

func (*TenderLogger) Error

func (l *TenderLogger) Error(msg string, keyvals ...interface{})

func (*TenderLogger) Info

func (l *TenderLogger) Info(msg string, keyvals ...interface{})

func (*TenderLogger) With

func (l *TenderLogger) With(keyvals ...interface{}) tmlog.Logger

type VochainGenesis

type VochainGenesis struct {
	SeedNodes         []string
	Genesis           string
	AutoUpdateGenesis bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL