以太坊源码分析(29)core-vm源码分析

## contract.go
contract 代表了以太坊 state database里面的一个合约。包含了合约代码,调用参数。


结构
    
    // ContractRef is a reference to the contract's backing object
    type ContractRef interface {
        Address() common.Address
    }
    
    // AccountRef implements ContractRef.
    //
    // Account references are used during EVM initialisation and
    // it's primary use is to fetch addresses. Removing this object
    // proves difficult because of the cached jump destinations which
    // are fetched from the parent contract (i.e. the caller), which
    // is a ContractRef.
    type AccountRef common.Address
    
    // Address casts AccountRef to a Address
    func (ar AccountRef) Address() common.Address { return (common.Address)(ar) }
    
    // Contract represents an ethereum contract in the state database. It contains
    // the the contract code, calling arguments. Contract implements ContractRef
    type Contract struct {
        // CallerAddress is the result of the caller which initialised this
        // contract. However when the "call method" is delegated this value
        // needs to be initialised to that of the caller's caller.
        // CallerAddress是初始化这个合约的人。 如果是delegate,这个值被设置为调用者的调用者。
        CallerAddress common.Address
        caller ContractRef
        self ContractRef
    
        jumpdests destinations // result of JUMPDEST analysis. JUMPDEST指令的分析
    
        Code []byte //代码
        CodeHash common.Hash //代码的HASH
        CodeAddr *common.Address //代码地址
        Input []byte // 入参
    
        Gas uint64       // 合约还有多少Gas
        value *big.Int
    
        Args []byte //好像没有使用
    
        DelegateCall bool
    }

构造
    
    // NewContract returns a new contract environment for the execution of EVM.
    func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract {
        c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object, Args: nil}
    
        if parent, ok := caller.(*Contract); ok {
            // Reuse JUMPDEST analysis from parent context if available.
            // 如果 caller 是一个合约,说明是合约调用了我们。 jumpdests设置为caller的jumpdests
            c.jumpdests = parent.jumpdests
        } else {
            c.jumpdests = make(destinations)
        }
    
        // Gas should be a pointer so it can safely be reduced through the run
        // This pointer will be off the state transition
        c.Gas = gas
        // ensures a value is set
        c.value = value
    
        return c
    }

AsDelegate将合约设置为委托调用并返回当前合同(用于链式调用)

    // AsDelegate sets the contract to be a delegate call and returns the current
    // contract (for chaining calls)
    func (c *Contract) AsDelegate() *Contract {
        c.DelegateCall = true
        // NOTE: caller must, at all times be a contract. It should never happen
        // that caller is something other than a Contract.
        parent := c.caller.(*Contract)
        c.CallerAddress = parent.CallerAddress
        c.value = parent.value
    
        return c
    }
        
GetOp 用来获取下一跳指令
    
    // GetOp returns the n'th element in the contract's byte array
    func (c *Contract) GetOp(n uint64) OpCode {
        return OpCode(c.GetByte(n))
    }
    
    // GetByte returns the n'th byte in the contract's byte array
    func (c *Contract) GetByte(n uint64) byte {
        if n < uint64(len(c.Code)) {
            return c.Code[n]
        }
    
        return 0
    }

    // Caller returns the caller of the contract.
    //
    // Caller will recursively call caller when the contract is a delegate
    // call, including that of caller's caller.
    func (c *Contract) Caller() common.Address {
        return c.CallerAddress
    }
UseGas使用Gas。
    
    // UseGas attempts the use gas and subtracts it and returns true on success
    func (c *Contract) UseGas(gas uint64) (ok bool) {
        if c.Gas < gas {
            return false
        }
        c.Gas -= gas
        return true
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值