mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-01-11 11:20:38 +00:00
core: nit refactoring and add godoc
This commit is contained in:
parent
8ae88593dd
commit
672f314be0
1 changed files with 60 additions and 53 deletions
|
@ -356,26 +356,9 @@ func (bc *Blockchain) storeBlock(block *Block) error {
|
||||||
chainState.unspentCoins[tx.Hash()] = NewUnspentCoinState(len(tx.Outputs))
|
chainState.unspentCoins[tx.Hash()] = NewUnspentCoinState(len(tx.Outputs))
|
||||||
|
|
||||||
// Process TX outputs.
|
// Process TX outputs.
|
||||||
for index, output := range tx.Outputs {
|
if err := processOutputs(tx, chainState); err != nil {
|
||||||
account, err := chainState.accounts.getAndUpdate(bc.store, output.ScriptHash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
account.Balances[output.AssetID] = append(account.Balances[output.AssetID], UnspentBalance{
|
|
||||||
Tx: tx.Hash(),
|
|
||||||
Index: uint16(index),
|
|
||||||
Value: output.Amount,
|
|
||||||
})
|
|
||||||
if output.AssetID.Equals(governingTokenTX().Hash()) && len(account.Votes) > 0 {
|
|
||||||
for _, vote := range account.Votes {
|
|
||||||
validatorState, err := chainState.validators.getAndUpdate(bc.store, vote)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
validatorState.Votes += output.Amount
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process TX inputs that are grouped by previous hash.
|
// Process TX inputs that are grouped by previous hash.
|
||||||
for prevHash, inputs := range tx.GroupInputsByPrevHash() {
|
for prevHash, inputs := range tx.GroupInputsByPrevHash() {
|
||||||
|
@ -479,26 +462,13 @@ func (bc *Blockchain) storeBlock(block *Block) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case *transaction.EnrollmentTX:
|
case *transaction.EnrollmentTX:
|
||||||
validator, err := chainState.validators.getAndUpdate(bc.store, t.PublicKey)
|
if err := processEnrollmentTX(chainState, t); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
validator.Registered = true
|
|
||||||
case *transaction.StateTX:
|
case *transaction.StateTX:
|
||||||
for _, descriptor := range t.Descriptors {
|
if err := processStateTX(chainState, t); err != nil {
|
||||||
switch descriptor.Type {
|
|
||||||
case transaction.Account:
|
|
||||||
err := processAccountStateDescriptor(descriptor, chainState)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case transaction.Validator:
|
|
||||||
err := processValidatorStateDescriptor(descriptor, chainState)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case *transaction.PublishTX:
|
case *transaction.PublishTX:
|
||||||
var properties smartcontract.PropertyState
|
var properties smartcontract.PropertyState
|
||||||
if t.NeedStorage {
|
if t.NeedStorage {
|
||||||
|
@ -516,7 +486,6 @@ func (bc *Blockchain) storeBlock(block *Block) error {
|
||||||
Description: t.Description,
|
Description: t.Description,
|
||||||
}
|
}
|
||||||
chainState.contracts[contract.ScriptHash()] = contract
|
chainState.contracts[contract.ScriptHash()] = contract
|
||||||
|
|
||||||
case *transaction.InvocationTX:
|
case *transaction.InvocationTX:
|
||||||
systemInterop := newInteropContext(0x10, bc, chainState.store, block, tx)
|
systemInterop := newInteropContext(0x10, bc, chainState.store, block, tx)
|
||||||
v := bc.spawnVMWithInterops(systemInterop)
|
v := bc.spawnVMWithInterops(systemInterop)
|
||||||
|
@ -586,6 +555,31 @@ func (bc *Blockchain) storeBlock(block *Block) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// processOutputs processes transaction outputs.
|
||||||
|
func processOutputs(tx *transaction.Transaction, chainState *BlockChainState) error {
|
||||||
|
for index, output := range tx.Outputs {
|
||||||
|
account, err := chainState.accounts.getAndUpdate(chainState.store, output.ScriptHash)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
account.Balances[output.AssetID] = append(account.Balances[output.AssetID], UnspentBalance{
|
||||||
|
Tx: tx.Hash(),
|
||||||
|
Index: uint16(index),
|
||||||
|
Value: output.Amount,
|
||||||
|
})
|
||||||
|
if output.AssetID.Equals(governingTokenTX().Hash()) && len(account.Votes) > 0 {
|
||||||
|
for _, vote := range account.Votes {
|
||||||
|
validatorState, err := chainState.validators.getAndUpdate(chainState.store, vote)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
validatorState.Votes += output.Amount
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func processValidatorStateDescriptor(descriptor *transaction.StateDescriptor, state *BlockChainState) error {
|
func processValidatorStateDescriptor(descriptor *transaction.StateDescriptor, state *BlockChainState) error {
|
||||||
publicKey := &keys.PublicKey{}
|
publicKey := &keys.PublicKey{}
|
||||||
err := publicKey.DecodeBytes(descriptor.Key)
|
err := publicKey.DecodeBytes(descriptor.Key)
|
||||||
|
@ -1254,6 +1248,7 @@ func (bc *Blockchain) GetValidators(txes... *transaction.Transaction) ([]*keys.P
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// process inputs
|
||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
prevOutput := prevTx.Outputs[input.PrevIndex]
|
prevOutput := prevTx.Outputs[input.PrevIndex]
|
||||||
accountState, err := chainState.accounts.getAndUpdate(chainState.store, prevOutput.ScriptHash)
|
accountState, err := chainState.accounts.getAndUpdate(chainState.store, prevOutput.ScriptHash)
|
||||||
|
@ -1261,6 +1256,7 @@ func (bc *Blockchain) GetValidators(txes... *transaction.Transaction) ([]*keys.P
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// process account state votes: if there are any -> validators will be updated.
|
||||||
if prevOutput.AssetID.Equals(governingTokenTX().Hash()) {
|
if prevOutput.AssetID.Equals(governingTokenTX().Hash()) {
|
||||||
if len(accountState.Votes) > 0 {
|
if len(accountState.Votes) > 0 {
|
||||||
for _, vote := range accountState.Votes {
|
for _, vote := range accountState.Votes {
|
||||||
|
@ -1281,27 +1277,13 @@ func (bc *Blockchain) GetValidators(txes... *transaction.Transaction) ([]*keys.P
|
||||||
|
|
||||||
switch t := tx.Data.(type) {
|
switch t := tx.Data.(type) {
|
||||||
case *transaction.EnrollmentTX:
|
case *transaction.EnrollmentTX:
|
||||||
validatorState, err := chainState.validators.getAndUpdate(chainState.store, t.PublicKey)
|
if err := processEnrollmentTX(chainState, t); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
validatorState.Registered = true
|
|
||||||
|
|
||||||
case *transaction.StateTX:
|
case *transaction.StateTX:
|
||||||
for _, desc := range t.Descriptors {
|
if err := processStateTX(chainState, t); err != nil {
|
||||||
switch desc.Type {
|
|
||||||
case transaction.Account:
|
|
||||||
err := processAccountStateDescriptor(desc, chainState)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
case transaction.Validator:
|
|
||||||
err := processValidatorStateDescriptor(desc, chainState)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1351,6 +1333,31 @@ func (bc *Blockchain) GetValidators(txes... *transaction.Transaction) ([]*keys.P
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func processStateTX(chainState *BlockChainState, tx *transaction.StateTX, ) error {
|
||||||
|
for _, desc := range tx.Descriptors {
|
||||||
|
switch desc.Type {
|
||||||
|
case transaction.Account:
|
||||||
|
if err := processAccountStateDescriptor(desc, chainState); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case transaction.Validator:
|
||||||
|
if err := processValidatorStateDescriptor(desc, chainState); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func processEnrollmentTX(chainState *BlockChainState, tx *transaction.EnrollmentTX) error {
|
||||||
|
validatorState, err := chainState.validators.getAndUpdate(chainState.store, tx.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
validatorState.Registered = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetScriptHashesForVerifying returns all the ScriptHashes of a transaction which will be use
|
// GetScriptHashesForVerifying returns all the ScriptHashes of a transaction which will be use
|
||||||
// to verify whether the transaction is bonafide or not.
|
// to verify whether the transaction is bonafide or not.
|
||||||
// Golang implementation of GetScriptHashesForVerifying method in C# (https://github.com/neo-project/neo/blob/master/neo/Network/P2P/Payloads/Transaction.cs#L190)
|
// Golang implementation of GetScriptHashesForVerifying method in C# (https://github.com/neo-project/neo/blob/master/neo/Network/P2P/Payloads/Transaction.cs#L190)
|
||||||
|
|
Loading…
Reference in a new issue