Skip to content

Commit

Permalink
blockchain.go: utilize WriteStatus for GetBlockStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
0xalank committed Jul 12, 2022
1 parent 47dbf5c commit 67ead10
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 30 deletions.
2 changes: 1 addition & 1 deletion consensus/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ var (
ErrInvalidOntology = errors.New("invalid ontology")

// ErrGhostState is returned if a side chain is attempted to be re-imported into state.
ErrGhostState = errors.New("sidechain ghost-state attac")
ErrGhostState = errors.New("sidechain ghost-state attack")
)
22 changes: 7 additions & 15 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,7 @@ const (
NonStatTy WriteStatus = iota
CanonStatTy
SideStatTy
UnknownStatTy
)

// numberHash is just a container for a number and a hash, to represent a block
Expand Down Expand Up @@ -1713,25 +1714,16 @@ func (bc *BlockChain) addFutureBlock(block *types.Block) error {
return nil
}

var (
accepted = "accepted"
rejected = "rejected"
future = "future"
)

// GetBlockStatus returns the status of the block for a given header
// * `accepted` - If the block with given header is canonical
// * `rejected` - If the block with given header is an uncle
// * `future` - If the given header is of a future block
func (bc *BlockChain) GetBlockStatus(header *types.Header) (string, error) {
func (bc *BlockChain) GetBlockStatus(header *types.Header) WriteStatus {
canonHash := bc.GetCanonicalHash(header.Number[types.QuaiNetworkContext].Uint64())
if (canonHash == common.Hash{}) {
return "", errors.New("unable to find a block with the header hash")
}
if canonHash != header.Hash() {
return "", nil
return SideStatTy
}
if (canonHash == common.Hash{}) {
return UnknownStatTy
}
return accepted, nil
return CanonStatTy
}

// AddExternalBlocks adds a group of external blocks to the cache
Expand Down
2 changes: 1 addition & 1 deletion eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (b *EthAPIBackend) GetExternalBlockByHashAndContext(hash common.Hash, conte
return b.eth.blockchain.GetExternalBlockByHashAndContext(hash, context)
}

func (b *EthAPIBackend) GetBlockStatus(header *types.Header) (string, error) {
func (b *EthAPIBackend) GetBlockStatus(header *types.Header) core.WriteStatus {
return b.eth.blockchain.GetBlockStatus(header)
}

Expand Down
21 changes: 14 additions & 7 deletions ethclient/quaiclient/quaiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,21 @@ func (ec *Client) Close() {
ec.c.Close()
}

// WriteStatus status of write
type WriteStatus byte

const (
NonStatTy WriteStatus = iota
CanonStatTy
SideStatTy
UnknownStatTy
)

// GetBlockStatus returns the status of the block for a given header
// * `accepted` - If the block with given header is canonical
// * `rejected` - If the block with given header is an uncle
// * `future` - If the given header is of a future block
func (ec *Client) GetBlockStatus(ctx context.Context, header *types.Header) (string, error) {
var blockStatus string
func (ec *Client) GetBlockStatus(ctx context.Context, header *types.Header) WriteStatus {
var blockStatus WriteStatus
if err := ec.c.CallContext(ctx, &blockStatus, "quai_getBlockStatus", header); err != nil {
return "", err
return NonStatTy
}
return blockStatus, nil
return blockStatus
}
2 changes: 1 addition & 1 deletion internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type Backend interface {
PendingBlockAndReceipts() (*types.Block, types.Receipts)
AddExternalBlock(block *types.ExternalBlock) error
GetExternalBlockByHashAndContext(hash common.Hash, context int) (*types.ExternalBlock, error)
GetBlockStatus(header *types.Header) (string, error)
GetBlockStatus(header *types.Header) core.WriteStatus
EventMux() *event.TypeMux
CalculateBaseFee(header *types.Header) *big.Int
GetUncleFromWorker(uncleHash common.Hash) (*types.Block, error)
Expand Down
6 changes: 3 additions & 3 deletions internal/ethapi/quai_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,14 +702,14 @@ func (s *PublicBlockChainQuaiAPI) GetExternalBlockByHashAndContext(ctx context.C
// * `accepted` - If the block with given header is canonical
// * `rejected` - If the block with given header is an uncle
// * `future` - If the given header is of a future block
func (s *PublicBlockChainQuaiAPI) GetBlockStatus(ctx context.Context, raw json.RawMessage) (string, error) {
func (s *PublicBlockChainQuaiAPI) GetBlockStatus(ctx context.Context, raw json.RawMessage) core.WriteStatus {
var head *types.Header
if err := json.Unmarshal(raw, &head); err != nil {
return "", err
return core.NonStatTy
}

if head == nil {
return "", errors.New("header provided to check canonical is nil")
return core.NonStatTy
}

fmt.Println("head ", head)
Expand Down
4 changes: 2 additions & 2 deletions les/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,6 @@ func (b *LesApiBackend) CalculateBaseFee(header *types.Header) *big.Int {
return b.CalculateBaseFee(header)
}

func (b *LesApiBackend) GetBlockStatus(header *types.Header) (bool, error) {
return false, nil
func (b *LesApiBackend) GetBlockStatus(header *types.Header) core.WriteStatus {
return core.NonStatTy
}

0 comments on commit 67ead10

Please sign in to comment.