2021-02-02 09:34:27 +00:00
|
|
|
package stateroot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
2021-03-25 08:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2021-05-06 19:17:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2021-02-02 09:34:27 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-05-07 09:22:29 +00:00
|
|
|
const rootValidEndInc = 100
|
|
|
|
|
2021-02-02 09:34:27 +00:00
|
|
|
// RelayCallback represents callback for sending validated state roots.
|
|
|
|
type RelayCallback = func(*payload.Extensible)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// AddSignature adds a state root signature.
|
2021-02-02 09:34:27 +00:00
|
|
|
func (s *service) AddSignature(height uint32, validatorIndex int32, sig []byte) error {
|
|
|
|
if !s.MainCfg.Enabled {
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-07 10:19:40 +00:00
|
|
|
myIndex, acc := s.getAccount()
|
2021-03-25 08:55:06 +00:00
|
|
|
if acc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-02 09:34:27 +00:00
|
|
|
|
2021-05-07 10:19:40 +00:00
|
|
|
incRoot := s.getIncompleteRoot(height, myIndex)
|
2021-02-02 09:34:27 +00:00
|
|
|
if incRoot == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
incRoot.Lock()
|
2021-05-06 20:29:43 +00:00
|
|
|
defer incRoot.Unlock()
|
|
|
|
|
|
|
|
if validatorIndex < 0 || int(validatorIndex) >= len(incRoot.svList) {
|
|
|
|
return errors.New("invalid validator index")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub := incRoot.svList[validatorIndex]
|
2021-02-02 09:34:27 +00:00
|
|
|
if incRoot.root != nil {
|
2021-03-25 19:11:55 +00:00
|
|
|
ok := pub.VerifyHashable(sig, uint32(s.Network), incRoot.root)
|
2021-02-02 09:34:27 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid state root signature for %d", validatorIndex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
incRoot.addSignature(pub, sig)
|
2021-05-06 20:29:43 +00:00
|
|
|
s.trySendRoot(incRoot, acc)
|
2021-02-02 09:34:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfig returns service configuration.
|
|
|
|
func (s *service) GetConfig() config.StateRoot {
|
|
|
|
return s.MainCfg
|
|
|
|
}
|
|
|
|
|
2021-05-07 10:19:40 +00:00
|
|
|
func (s *service) getIncompleteRoot(height uint32, myIndex byte) *incompleteRoot {
|
2021-02-02 09:34:27 +00:00
|
|
|
s.srMtx.Lock()
|
|
|
|
defer s.srMtx.Unlock()
|
|
|
|
if incRoot, ok := s.incompleteRoots[height]; ok {
|
|
|
|
return incRoot
|
|
|
|
}
|
2021-05-07 10:19:40 +00:00
|
|
|
incRoot := &incompleteRoot{
|
|
|
|
myIndex: int(myIndex),
|
|
|
|
svList: s.GetStateValidators(height),
|
|
|
|
sigs: make(map[string]*rootSig),
|
|
|
|
}
|
2021-02-02 09:34:27 +00:00
|
|
|
s.incompleteRoots[height] = incRoot
|
|
|
|
return incRoot
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// trySendRoot attempts to finalize and send MPTRoot, it must be called with the ir locked.
|
2021-05-06 20:29:43 +00:00
|
|
|
func (s *service) trySendRoot(ir *incompleteRoot, acc *wallet.Account) {
|
2021-05-07 10:19:40 +00:00
|
|
|
if !ir.isSenderNow() {
|
2021-05-06 20:29:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
sr, ready := ir.finalize()
|
|
|
|
if ready {
|
|
|
|
err := s.AddStateRoot(sr)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("can't add validated state root", zap.Error(err))
|
|
|
|
}
|
|
|
|
s.sendValidatedRoot(sr, acc)
|
|
|
|
ir.isSent = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:17:09 +00:00
|
|
|
func (s *service) sendValidatedRoot(r *state.MPTRoot, acc *wallet.Account) {
|
2021-02-02 09:34:27 +00:00
|
|
|
w := io.NewBufBinWriter()
|
2021-03-25 19:11:55 +00:00
|
|
|
m := NewMessage(RootT, r)
|
2021-02-02 09:34:27 +00:00
|
|
|
m.EncodeBinary(w.BinWriter)
|
|
|
|
ep := &payload.Extensible{
|
2021-03-26 19:16:46 +00:00
|
|
|
Category: Category,
|
2021-02-02 09:34:27 +00:00
|
|
|
ValidBlockStart: r.Index,
|
2021-05-07 09:22:29 +00:00
|
|
|
ValidBlockEnd: r.Index + rootValidEndInc,
|
2022-09-01 16:04:47 +00:00
|
|
|
Sender: acc.ScriptHash(),
|
2021-02-02 09:34:27 +00:00
|
|
|
Data: w.Bytes(),
|
2021-03-25 08:55:06 +00:00
|
|
|
Witness: transaction.Witness{
|
2021-05-06 19:17:09 +00:00
|
|
|
VerificationScript: acc.GetVerificationScript(),
|
2021-03-25 08:55:06 +00:00
|
|
|
},
|
2021-02-02 09:34:27 +00:00
|
|
|
}
|
2022-09-01 17:42:42 +00:00
|
|
|
sig := acc.SignHashable(s.Network, ep)
|
2021-03-25 08:55:06 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
emit.Bytes(buf.BinWriter, sig)
|
|
|
|
ep.Witness.InvocationScript = buf.Bytes()
|
2021-05-06 19:29:19 +00:00
|
|
|
s.relayExtensible(ep)
|
2021-02-02 09:34:27 +00:00
|
|
|
}
|